0

I'm trying to create a small WASM project for image compression.

After some search in github, I noticed that oxipng 2.2.2 has a target for wasm32-unknown-unknown, hence why I'm using that.

I'm using wasm-pack for creating the wasm file + JS bindings with target -t web

This is the code:

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();
}

I don't ever get an error message, the last log I have is "after options".

In a nutshell, I'm using a Flutter web application that gets a PNG file, converts it into a Uint8List, and I send it as an integer List to the JS bindings.

When called, the following error happens:

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

Since this version is old, I don't know if I should revert back to an older version of 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)`

Thank you in advance

Vanethos
  • 691
  • 1
  • 8
  • 20

1 Answers1

0

The problem is you're using a very old version of oxipng (v2.2.2) that didn't support wasm yet. I believe wasm support was added in v2.3.0 (link to issue that was fixed). Anyways, you should be able to use the latest version just fine with wasm, just make sure you disable the default features when adding the crate to your Cargo.toml:

[dependencies]
oxipng = { version = "5", default-features = false }
Brian Bowman
  • 892
  • 6
  • 9