Stream: wasm

Topic: testing WAT's output inside Rust code


view this post on Zulip Rizary (Apr 14 2021 at 01:14):

I write my first wasm implementation for my programming language. The output of the WAT using wasm2wat is:

(module
  (type (;0;) (func (result i64)))
  (func $add (type 1) (param i64 i64) (result i64)
    local.get 0
    local.get 1
    i64.add
    return)
  (export "add" (func $add)))

I want to test this using assert inside my rust code. What is the best way to do that? Is there any library that I can use?

Currently, I use walrus' emit_wasm() to produce wasm buffer.

view this post on Zulip Andrew Brown (Apr 14 2021 at 15:38):

I would take a look at https://docs.wasmtime.dev/lang-rust.html: it has an example of how to embed Wasmtime in your Rust program and call your add function`.

view this post on Zulip Rizary (Apr 15 2021 at 01:35):

Thank you for the link. I have seen it but it seems we should have .wat file first to do that. What I want is more like my_language -> wasm -> wat or my_language -> wat but in buffer. Similar to what emit_wasm() function does, but to produce wat based on wasm spec.

view this post on Zulip Andrew Brown (Apr 15 2021 at 15:45):

Are you looking for Module::new? I guess you could use Module::from_binary directly if you already have the compiled Wasm bytes available.

view this post on Zulip fitzgen (he/him) (Apr 15 2021 at 20:50):

You can use the wasmprinter crate to disassemble a wasm buffer into wat: https://docs.rs/wasmprinter/0.2.25/wasmprinter/

view this post on Zulip Rizary (Apr 20 2021 at 00:14):

Andrew Brown said:

Are you looking for Module::new? I guess you could use Module::from_binary directly if you already have the compiled Wasm bytes available.

Yes, thank for the suggestion.

view this post on Zulip Rizary (Apr 20 2021 at 00:14):

@_fitzgen (he/him)|253990 said:

You can use the wasmprinter crate to disassemble a wasm buffer into wat: https://docs.rs/wasmprinter/0.2.25/wasmprinter/

thanks, I'll look into it.


Last updated: Oct 23 2024 at 20:03 UTC