【问题标题】:oxipng throwing RuntimeError: unreachable when calledoxipng throwing RuntimeError: unreachable when called
【发布时间】:2021-12-08 00:30:06
【问题描述】:

我正在尝试创建一个用于图像压缩的小型 WASM 项目。

在 github 中搜索后,我注意到 oxipng 2.2.2 的目标是 wasm32-unknown-unknown,因此我使用它。

我使用wasm-pack 来创建wasm 文件+ JS 绑定与目标-t web

这是代码:

extern crate oxipng;

mod utils;

use std::error::Error;

use wasm_bindgen::prelude::*;

use oxipng::*;

#[wasm_bindgen]
extern "C" {
    // Use `js_namespace` here to bind `console.log(..)` instead of just
    // `log(..)`
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

// Next let's define a macro that's like `println!`, only it works for
// `console.log`. Note that `println!` doesn't actually work on the wasm target
// because the standard library currently just eats all output. To get
// `println!`-like behavior in your app you'll likely want a macro like this.
#[macro_export]
macro_rules! console_log {
    // Note that this is using the `log` function imported above during
    // `bare_bones`
    ($($t:tt)*) => (crate::log(&format_args!($($t)*).to_string()))
}

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn compress(data: &[u8])  -> Vec<u8> {
    console_log!("{}", data.len());
    let opts = Options::from_preset(6);
    console_log!("after options");
    let res = match optimize_from_memory(data, &&opts) {
        Ok(res) => Ok(res),
        Err(err) => Err(err),
      };
    
    match &res {
        Ok(_) => console_log!("Optimized"),
        Err(err) =>             console_log!("Error: {}", err),
    }

    return res.unwrap();
}

我从来没有收到错误消息,我最后的日志是“选项之后”。

简而言之,我使用的是 Flutter Web 应用程序,它获取 PNG 文件,将其转换为 Uint8List,然后将其作为整数列表发送到 JS 绑定。

调用时出现如下错误:

RuntimeError: unreachable
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[1019]:0x5c6be
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[414]:0x4cd37
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[619]:0x54c96
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[915]:0x5b4ba
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[986]:0x5c139
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[645]:0x55885
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[569]:0x5324b
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[594]:0x53ff1
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[2]:0x554f
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[84]:0x2cbf2
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[73]:0x2a501
    at http://localhost:3000/pkg/rust_png_module_bg.wasm:wasm-function[563]:0x52eaa
    at compress (http://localhost:3000/pkg/rust_png_module.js:49:14)
    at compressImage (http://localhost:3000/packages/rust_wasm/ui/screens/home/home_page.dart.lib.js:568:72)
    at compressImage.next (<anonymous>)
    at http://localhost:3000/dart_sdk.js:38640:33
    at _RootZone.runUnary (http://localhost:3000/dart_sdk.js:38511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:3000/dart_sdk.js:33713:29)
    at handleValueCallback (http://localhost:3000/dart_sdk.js:34265:49)
    at Function._propagateToListeners (http://localhost:3000/dart_sdk.js:34303:17)
    at _Future.new.[_completeWithValue] (http://localhost:3000/dart_sdk.js:34151:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:3000/dart_sdk.js:34172:35)
    at Object._microtaskLoop (http://localhost:3000/dart_sdk.js:38778:13)
    at _startMicrotaskLoop (http://localhost:3000/dart_sdk.js:38784:13)
    at http://localhost:3000/dart_sdk.js:34519:9

由于这个版本太旧了,我不知道我是否应该回到旧版本的 Rust

$ rustup --version     
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.55.0 (c8dfcfe04 2021-09-06)`

提前谢谢你

【问题讨论】:

    标签: rust rust-wasm


    【解决方案1】:

    问题是您使用的oxipng (v2.2.2) 的旧版本还不支持wasm。我相信在 v2.3.0 (link to issue that was fixed) 中添加了对 wasm 的支持。无论如何,你应该可以使用最新版本的 wasm,只要确保在将 crate 添加到 Cargo.toml 时禁用默认功能即可:

    [dependencies]
    oxipng = { version = "5", default-features = false }
    

    【讨论】:

      猜你喜欢
      • 2022-12-19
      • 1970-01-01
      • 2022-12-02
      • 2022-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多