Stream: wasm

Topic: ✔ console.log from lib


view this post on Zulip Scott Waye (Aug 04 2021 at 20:03):

I'm trying to debug my call to a witx-bindgen wrapper by adding log statements to the function. I think I'm hitting https://www.gitmemory.com/issue/rustwasm/wasm-bindgen/2554/842354646 if I try to use #[wasm_bindgen(js_namespace = console)] Is there another way?

view this post on Zulip Alex Crichton (Aug 04 2021 at 20:10):

Can you describe your use case better? In general the two aren't intended to compose (witx-bindgen and wasm-bindgen) and you should probably stick to one or the other. If you're on the web you should be able to put them together theoretically but it's probably not worth it. Off-the-web there's no real meaning for wasm-bindgen, so you wouldn't want to use wasm-bindgen and you'd want to use witx-bindgen instead

view this post on Zulip Scott Waye (Aug 04 2021 at 20:27):

I think so. I'm trying to witx-bindgen a string from c# to rust. I've used the witx-bindgen to generate the rust import, and handcrafted the c# export - no doubt I've got this wrong. So I'm trying to see what rust receives but get

std::panicking::begin_panic_fmt::h5f1c42481fee165b@http://localhost:8000/hello_wasi.wasm:wasm-function[393]:0x18f62
std::io::stdio::_print::h622370e54afc2a27@http://localhost:8000/hello_wasi.wasm:wasm-function[15]:0x5e1c
reverse@http://localhost:8000/hello_wasi.wasm:wasm-function[33]:0x9831

with

mod w1 {
    #[export_name = "reverse"]
    unsafe extern "C" fn __witx_bindgen_reverse(arg0: i32, arg1: i32, ) -> i32{
      println!("__witx_bindgen_reverse");
      let len0 = arg1 as usize;

So I'm wondering if there's any way to debug. As far as I can tell the the 2 args are ok - I've pasted the first few byes from arg0 and they come out as the right UTF8 chars, and the length, arg1 looks correct

view this post on Zulip Alex Crichton (Aug 04 2021 at 21:01):

oh sorry, you linked to a wasm-bindgen bug so I thought there might be wasm-bindgen somewhere

view this post on Zulip Alex Crichton (Aug 04 2021 at 21:02):

sorry I'm not entirely sure what the issue is, can you describe a bit more?

view this post on Zulip Scott Waye (Aug 04 2021 at 21:08):

I linked that because I first tried to wasm-bindgen console.log so I could use that to debug my pointers. I'm jumping around, sorry for that. Ignoring the println! as I've removed that for now. I get

exception thrown: RuntimeError: memory access out of bounds,RuntimeError: memory access out of bounds
    at core::str::converts::from_utf8::h044e2484382de369 (http://localhost:8000/hello_wasi.wasm:wasm-function[21]:0x7104)
    at alloc::string::String::from_utf8::hdff6602827172de1 (http://localhost:8000/hello_wasi.wasm:wasm-function[24]:0x7b06)
    at reverse (http://localhost:8000/hello_wasi.wasm:wasm-function[35]:0x99a6)

view this post on Zulip Scott Waye (Aug 04 2021 at 21:09):

So basically I'm trying to debug that, i.e. am I passing a valid set of bytes to the function

view this post on Zulip Scott Waye (Aug 04 2021 at 21:15):

https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts says "ptr needs to have been previously allocated via String/Vec<T> (at least, it’s highly likely to be incorrect if it wasn’t)." . Hmm, well its not, its just the memory address passed from the caller...

view this post on Zulip Scott Waye (Aug 04 2021 at 21:22):

I don't think I really understand how this works, these are 2 different wasm modules, with their own linear memory spaces and share nothing as I understand it. but the pointer to the byte[] is the same on both wasm modules which seems to contradict my understanding.

view this post on Zulip Scott Waye (Aug 04 2021 at 21:35):

I suppose for this to work I'd need some shared memory, and then have both sides use that. This would mean changing both sides to read and write to that shared memory and not their own linear memory. I think I didn't read well enough the use cases which seem to be for communicating between the host and the wasm module, not inter wasm modules.

view this post on Zulip Till Schneidereit (Aug 04 2021 at 22:18):

witx-bindgen is very much meant to enable inter-module communication, too. The wasmlink crate might point you in the right direction

A language binding generator for `witx` (a precursor to WebAssembly interface types) - witx-bindgen/crates/wasmlink at main · bytecodealliance/witx-bindgen

view this post on Zulip Scott Waye (Aug 04 2021 at 23:47):

Thanks, yes, that is the missing link(er) in my knowledge.

view this post on Zulip Notification Bot (Aug 04 2021 at 23:47):

Scott Waye has marked this topic as resolved.


Last updated: Nov 22 2024 at 17:03 UTC