go-interpreter / wagon

wagon, a WebAssembly-based Go interpreter, for Go.
BSD 3-Clause "New" or "Revised" License
904 stars 148 forks source link

inital value of global not understood #102

Closed donpdonp closed 5 years ago

donpdonp commented 5 years ago

I'm using zig to build a .wasm. The wasm decodes to the following wast (export "inbuf" (global $9)) (global $9 i32 (i32.const 1024))

which is what I expect so the wasm is good (decoded with the WAVM project). using wagon in my own project can read the global variable "inbuf" but the initial value is 4 bytes that I do not understand.

wasm.go:154: module.Export global "inbuf" #3 wasm.go:159: module.Global #3 i32 value []byte{0x41, 0x80, 0x8, 0xb}

I would expect the GlobalEntry.Init []byte to be equal to 1024. Can someone give some explanation of this 0x4180080b value?

donpdonp commented 5 years ago

found the docs for globals in a wasm.

https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#primitive-encoding-types

(a type-indicator byte followed by a signed LEB128 value)