Code Monkey home page Code Monkey logo

gasm's Introduction

gasm

A minimal implementation of v1 WASM spec compatible virtual machine purely written in go. The vm can be embedded in your go program without any dependency like cgo, and enables Gophers to write wasm host environments easily.

The vm should be used only for providing sandbox environments embedded in your Go program since we have not implemented validation of wasm binary.

The implementation is quite straightforward and I hope this code would be a good starting point for novices to learn WASM spec.

examples

call expoerted function from host

func main() {
	buf, _ := ioutil.ReadFile("./fib.wasm")
	mod, _ := wasm.DecodeModule(bytes.NewBuffer(buf))
	mod.BuildIndexSpaces(wasi.Modules) // fd_write must be injected
	vm, _ := wasm.NewVM(mod)

	for _, c := range []struct {
		in, exp int32
	}{
		{in: 20, exp: 6765},
		{in: 10, exp: 55},
		{in: 5, exp: 5},
	} {
		ret, _, _ := vm.ExecExportedFunction("fib", uint64(c.in))
		if int32(ret[0]) != c.exp {
			panic(fmt.Sprintf("result must be %d but got %d\n", c.exp, ret[0]))
		}
	}
}

call host function from WASM module

func main() {
	buf, _ := ioutil.ReadFile("./host_func.wasm")
	mod, _ := wasm.DecodeModule(bytes.NewBuffer(buf))

	var cnt uint64
	hostFunc := func(*wasm.VirtualMachine) reflect.Value {
		return reflect.ValueOf(func() {
			cnt++
		})
	}

	builder := hostmodule.NewBuilderWith(wasi.Modules)
	builder.MustAddFunction("env", "host_func", hostFunc)
	mod.BuildIndexSpaces(builder.Done())
	vm, _ := wasm.NewVM(mod)

	for _, exp := range []uint64{5, 10, 15} {
		vm.ExecExportedFunction("call_host_func", exp)
		if cnt != exp {
			panic(fmt.Sprintf("want %d but got %d", exp, cnt))
		}
		cnt = 0
	}
}

๐Ÿšง WASI support ๐Ÿšง

WebAssembly System Interface(WASI) will partly be supported in wasi package. Currently only fd_write is implemented and you can see it works in examples/panic example where the WASM module causes panic and the host prints the message through fd_write ABI.

references

https://webassembly.github.io/spec/core/index.html

gasm's People

Contributors

mathetake avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.