0

我正在尝试创建一个用于图像压缩的小型 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,然后将其作为整数 List 发送到 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)`

先感谢您

4

1 回答 1

0

问题是您使用的oxipng( v2.2.2) 版本非常旧,还不支持 wasm。我相信 wasm 支持已添加v2.3.0链接到已修复的问题)。无论如何,您应该能够使用 wasm 很好地使用最新版本,只需确保在将 crate 添加到您的时禁用默认功能Cargo.toml

[dependencies]
oxipng = { version = "5", default-features = false }
于 2021-11-08T05:53:41.760 回答