Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use this as a WASM module? #130

Closed
DavidPHH opened this issue Aug 19, 2024 · 2 comments
Closed

Is it possible to use this as a WASM module? #130

DavidPHH opened this issue Aug 19, 2024 · 2 comments

Comments

@DavidPHH
Copy link

Other issues here mentioning wasm and the fact that I'm able to build a project using this via wasm-pack --target web made me think it's possible, but my lack of success and #15 being open made me ask this question to make sure.

I'm new to rust and haven't been having an easy time debugging or finding more info, so if it is possible to do this I'd appreciate any pointers on what to try.

cargo.toml

[package]
name = "test-lzma-wasm"
version = "0.1.0"
edition = "2021"

[dependencies]
console_error_panic_hook = "0.1.7"
liblzma = { version = "0.3", features = ["static"] }
wasm-bindgen = "0.2"

[lib]
crate-type = ["cdylib"]

lib.rs (example copied from the docs)

use wasm_bindgen::prelude::*;
extern crate console_error_panic_hook;
use liblzma::read::{XzDecoder, XzEncoder};
use std::io::prelude::*;

#[wasm_bindgen]
pub fn example() -> String {
    console_error_panic_hook::set_once();
    let data = "Hello, World!".as_bytes();
    let compressor = XzEncoder::new(data, 9); // ""Narrowed"" down the error to this line.
    let mut decompressor = XzDecoder::new(compressor);

    let mut contents = String::new();
    decompressor.read_to_string(&mut contents).unwrap();
    return contents;
}

index.ts I'm able to run other functions, like a simple hello world one, but if the XzEncoder::new() line is uncommented this errors.
I tried in node and also as a <script> in a html file.

import init, { example } from "./test-lzma-wasm/pkg";

await init();

example();

This is the error I get:

panicked at /rust/deps/dlmalloc-0.2.6/src/dlmalloc.rs:1198:13:
assertion failed: psize <= size + max_overhead

Stack:

Error
    at <anonymous> (/home/david/Projects/test/test-lzma-wasm/pkg/test_lzma_wasm.js:169:21)
    at <?>.wasm-function[253] (native)
    at <?>.wasm-function[88] (native)
    at <?>.wasm-function[146] (native)
    at <?>.wasm-function[140] (native)
    at <?>.wasm-function[228] (native)
    at <?>.wasm-function[203] (native)
    at <?>.wasm-function[163] (native)
    at <?>.wasm-function[125] (native)
    at <?>.wasm-function[20] (native)


RuntimeError: Unreachable code should not be executed (evaluating 'wasm.example(retptr)')
@ChanTsune
Copy link

Hi @DavidPHH

This issue may help you.
rustwasm/wasm-pack#1389

@DavidPHH
Copy link
Author

That was it! Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants