wasdk / WebAssemblyStudio

Learn, Teach, Work and Play in the WebAssembly Studio
http://webassembly.studio
MIT License
2.92k stars 273 forks source link

funcref vs anyfunc #448

Open tpmccallum opened 4 years ago

tpmccallum commented 4 years ago

I compiled a simple Rust library

#[no_mangle]
pub extern fn one_plus_one() -> i32 {
 1 + 1
}

The wat text representation of this code is as follows

(module
  (type (;0;) (func (result i32)))
  (func $one_plus_one (type 0) (result i32)
    i32.const 2)
  (table (;0;) 1 1 funcref)
  (memory (;0;) 16)
  (global (;0;) (mut i32) (i32.const 1048576))
  (global (;1;) i32 (i32.const 1048576))
  (global (;2;) i32 (i32.const 1048576))
  (export "memory" (memory 0))
  (export "__data_end" (global 1))
  (export "__heap_base" (global 2))
  (export "one_plus_one" (func $one_plus_one)))

Please note the line (table (;0;) 1 1 funcref)

Issue

When I build and execute the above wat, I get the following error.

test.wat:5:20: error: unexpected token funcref, expected anyfunc.
  (table (;0;) 1 1 funcref)

Solution

I note that the WebAssembly wat specification has been updated [1], specifically to use funcref instead of anyfunc for table declaration. The Rust compiler and the wasm to wat conversion tool wabt [2], that I am using appear to adhere to the newerfuncref spec.

Of course, if I change funcref back to anyfunc then the WebAssemblyStudio IDE will build and run the code.

Just wanted to let you know my experience and I am wondering if/when WebAssemblyStudio will adopt the wat spec as per the link below.

Thanks Tim

[1] https://github.com/WebAssembly/spec/issues/884#issuecomment-426433329 [2] https://github.com/WebAssembly/wabt

pfandzelter commented 4 years ago

I actually ran into the same issue with funcref vs anyfunc. Link [1] you mention does include some more updates. For example, get_local has officially been replaced with local.get. WebAssemblyStudio uses the old version while my compiler (wasm2wat) uses the newer spec.