Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 1.48 KB

README.md

File metadata and controls

64 lines (48 loc) · 1.48 KB

wasvm

A simple wasm interpreter/runtime (for leaning purpouses)

Examples

In the resources folder there is some .wasm files that you can use to run using this project.

Given a .wat file that export a function that sum two integers and return the value

(module
    (func (export "sum_i32") (param i32) (param i32) (result i32)
        local.get 0
        local.get 1
        i32.sum
    )
)

then you should compile it into .wasm (the binary format) using npx wat2wasm {name}.wat -o {name}.wasm. Once you have the .wasm you can use:

wasm, err := parser.BinaryFormat("path to the .wasm file")
if err != nil { panic(err) }

rt, err := vm.NewRuntime(wasm)
if err != nil { panic(err) }

results, err := rt.Exported["sum_i32"].Call(int32(10), int32(5))
if err != nil { panic(err) }

// as we expect only one result then the value will be in the index 0
fmt.Printf("The result of 10 + 5: %v\n", results[0])

Limitations

  • Conditions and Loops
  • Float points
  • Sections:
    • Memory:
    • Global
    • Start
    • Element
    • Data
    • Data Count
    • Import
    • Table

Running tests

go test ./...

Useful links