Skip to content

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

@DavidPHH

Description

@DavidPHH

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)')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions