0

I am compiling my Rust code which has a linked in C++ static library to wasm32-unknown-emcc. Out of curiosity, I converted the generated wasm file to a .wat file for better readability and saw this at the top. I understand the last line, as I can see the invoke_viii being defined in the accompanying .js file. Where can I find the function definitions/documentation about the top 18 function declarations?

(module
  (type (;0;) (func (param i32 i32) (result i32)))
  (type (;1;) (func (param i32) (result i32)))
  (type (;2;) (func (param i32 i32)))
  (type (;3;) (func (param i32 i32 i32) (result i32)))
  (type (;4;) (func (param i32)))
  (type (;5;) (func (param i32 i32 i32)))
  (type (;6;) (func (param i32 i32 i32 i32)))
  (type (;7;) (func))
  (type (;8;) (func (param i32 i32 i32 i32 i32)))
  (type (;9;) (func (result i32)))
  (type (;10;) (func (param i32 i32 i32 i32) (result i32)))
  (type (;11;) (func (param i32) (result i64)))
  (type (;12;) (func (param i32 i32 i32 i32 i32 i32)))
  (type (;13;) (func (param i32 i32 i32 i32 i32 i32) (result i32)))
  (type (;14;) (func (param i32 i32 i32 i32 i32 i32 i32)))
  (type (;15;) (func (param i32 i32 i32 i32 i32) (result i32)))
  (type (;16;) (func (param i32 i32 i32 i32 i32 i32 i32) (result i32)))
  (type (;17;) (func (param i64 i32 i32) (result i32)))
  (type (;18;) (func (param i32 i32 i32 i32 i32 i32 i32 i32)))
  (import "env" "invoke_viii" (func $invoke_viii (type 6)))
// More stuff below

1 Answers1

0

Those aren't function declarations, they're type declarations declaring the types of functions.

  (import "env" "invoke_viii" (func $invoke_viii (type 6)))

This is importing the function named "invoke_viii" and the type of that function is type 6, this one:

  (type (;6;) (func (param i32 i32 i32 i32)))

In your .wat file the (func ...) declarations (whether inside an import or not) are your function declarations and definitions.

Nick Lewycky
  • 1,182
  • 6
  • 14
  • Thanks for the swift response Nick! Might be a dumb question, but does this mean that type 6 is an anonymous function that takes 4 i32s? Or does (func (param i32 i32 i32 i32))) just mean it's a type that can only be passed as a param to a function? –  Sep 01 '22 at 00:45
  • I'd say neither? It's not a function, anonymous or otherwise, it's only the type. No value can ever have that type in webassembly, but all functions must have some function type. You can't pass a function as a parameter in webassembly at all. What WASM achieves this way is a space savings, every function must have a type, and if two functions happen to have the same type, we save space by reusing the same type. – Nick Lewycky Sep 01 '22 at 06:56