Stream: git-wasmtime

Topic: wasmtime / issue #3445 Running .cwasm files without CLI i...


view this post on Zulip Wasmtime GitHub notifications bot (Oct 12 2021 at 11:38):

Kroeg93 edited issue #3445:

Hi there,

is there any possibility to run .cwasm files in a rust embedding?

Currently i'm storing a .wat file within a buffer and use it as input for Engine::precompile_module

    let buffer = fs::read("./wasm/add.wat")?;
    let precompiled_file = engine.precompile_module(&buffer)?;

Afterwards the buffer is stored to a .cwasm file.

fn save_precompiled_file(buffer: &[u8]) -> std::io::Result<()> {
    let mut precompiled_module = File::create("add.cwasm")?;
    precompiled_module.write_all(&buffer)?;
    Ok(())
}

Is there any function to load the module from the .cwasm file? Or does this work differently?

Thanks :-)

view this post on Zulip Wasmtime GitHub notifications bot (Oct 12 2021 at 11:44):

Kroeg93 edited issue #3445:

Hi there,

is there any possibility to run .cwasm files in a rust embedding?

Currently i'm storing a .wat file within a buffer and use it as input for Engine::precompile_module

    let buffer = fs::read("./wasm/add.wat")?;
    let precompiled_file = engine.precompile_module(&buffer)?;

Afterwards the buffer is stored to a .cwasm file.

fn save_precompiled_file(buffer: &[u8]) -> std::io::Result<()> {
    let mut precompiled_module = File::create("add.cwasm")?;
    precompiled_module.write_all(&buffer)?;
    Ok(())
}

Is there any function to load the module from the .cwasm file? Or does this work differently?

Thanks :-)

Maybe this helps but I think its wrong:

I tried to create a new Module via Module::new

fn load_precompiled_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> {
    match Module::new(
        engine,
        &fs::read(&file).with_context(|| "failed to read input file")?,
    ) {
        Ok(m) => Ok(m),
        Err(e) => panic!("Error while loading from precompiled file: {}", e),
    }
}

The Error i got from this is:

input bytes aren't valid utf-8

view this post on Zulip Wasmtime GitHub notifications bot (Oct 12 2021 at 12:15):

bjorn3 commented on issue #3445:

I believe you can use Module::deserialize and Module::deserialize_file. The wasmtime cli uses the following logic to support both .wasm and .cwasm files: https://github.com/bytecodealliance/wasmtime/blob/bcf3544924880edc085e74497b0d3932909a456e/src/commands/run.rs#L376-L398

view this post on Zulip Wasmtime GitHub notifications bot (Oct 12 2021 at 14:09):

Kroeg93 commented on issue #3445:

Thank you, this was the thing I was looking for

Here is how I solved it:
Calling the function:

let pc_module = load_precompiled_file(&engine, Path::new("add.cwasm")).unwrap();

fn load_precompiled_file(engine: &Engine, path: &Path) -> Result<Module> {
    let module =  unsafe { Module::deserialize_file(engine, path) };
    module
}

view this post on Zulip Wasmtime GitHub notifications bot (Oct 12 2021 at 14:09):

Kroeg93 closed issue #3445:

Hi there,

is there any possibility to run .cwasm files in a rust embedding?

Currently i'm storing a .wat file within a buffer and use it as input for Engine::precompile_module

    let buffer = fs::read("./wasm/add.wat")?;
    let precompiled_file = engine.precompile_module(&buffer)?;

Afterwards the buffer is stored to a .cwasm file.

fn save_precompiled_file(buffer: &[u8]) -> std::io::Result<()> {
    let mut precompiled_module = File::create("add.cwasm")?;
    precompiled_module.write_all(&buffer)?;
    Ok(())
}

Is there any function to load the module from the .cwasm file? Or does this work differently?

Thanks :-)

Maybe this helps but I think its wrong:

I tried to create a new Module via Module::new

fn load_precompiled_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> {
    match Module::new(
        engine,
        &fs::read(&file).with_context(|| "failed to read input file")?,
    ) {
        Ok(m) => Ok(m),
        Err(e) => panic!("Error while loading from precompiled file: {}", e),
    }
}

The Error i got from this is:

input bytes aren't valid utf-8


Last updated: Nov 22 2024 at 16:03 UTC