Giter VIP home page Giter VIP logo

go-wasm3's Introduction

go-wasm3

GoDoc Build status

Golang wrapper for WASM3, WIP.

This is part of a series of WASM-related experiments: go-wavm and go-wasm-benchmark.

Install/build

This package ships with pre-built WASM3 libraries (static builds) for OS X and Linux. If you want to hack around it, check the original repository.

If you're using one of the mentioned platforms, you may install the package using go get:

$ go get -u github.com/matiasinsaurralde/go-wasm3

To inspect or run the little sample use:

$ cd $GOPATH/src/github.com/matiasinsaurralde/go-wasm3/examples/sum
$ go build # or "go run sum.go"
$ ./sum

The output will look as follows:

2020/01/15 09:51:24 Initializing WASM3
2020/01/15 09:51:24 Runtime ok
2020/01/15 09:51:24 Read WASM module (139 bytes)
2020/01/15 09:51:24 Module loaded
2020/01/15 09:51:24 Calling function
Result: 3
Result: 4

You will find additional sample projects in the next section.

Sample projects

boa

This program uses the boa engine to evaluate JS code. Boa is an embeddable JS engine written in Rust, for this sample it was compiled targeting WASM.

Link here.

libxml

This program loads libxml2 as a WASM module (it's a custom build, full instructions here). The library is used to validate a XML file against a XSD (both loaded from the Go side).

Link here.

Memory access

Take the following sample program:

#include <stdlib.h>
#include <string.h>

char* somecall() {
    // Allocate a few bytes on the heap:
    char* test = (char*) malloc(12*sizeof(char));

    // Copy a string into the previously defined address:
    strcpy(test, "testingonly");

    // Return the pointer:
    return test;
}

Build it using wasicc, this will generate a cstring.wasm file (WASM module):

wasicc cstring.c -Wl,--export-all -o cstring.wasm

The following Go code will load the WASM module and retrieve the data after calling somecall:

    // Initialize the runtime and load the module:
    env := wasm3.NewEnvironment()
	defer env.Destroy()
	runtime := wasm3.NewRuntime(env, 64*1024)
	defer runtime.Destroy()
    wasmBytes, err := ioutil.ReadFile("program.wasm")
	module, _ := env.ParseModule(wasmBytes)
	runtime.LoadModule(module)
    fn, _ := runtime.FindFunction(fnName)

    // Call somecall and get the pointer to our data:
    result := fn()
    
    // Reconstruct the string from memory:
    memoryLength = runtime.GetAllocatedMemoryLength()
    mem := runtime.GetMemory(memoryLength, 0)
    
    // Initialize a Go buffer:
	buf := new(bytes.Buffer)
	for n := 0; n < memoryLength; n++ {
		if n < result {
			continue
		}
		value := mem[n]
		if value == 0 {
			break
        }
		buf.WriteByte(value)
    }

    // Print the string: "testingonly"
    str := buf.String()
    fmt.Println(str)

For more details check this.

Limitations and future

This is a WIP. Stay tuned!

Related projects

A Rust wrapper is available here.

License

MIT.

wasm3 is also under this license.

go-wasm3's People

Contributors

matiasinsaurralde avatar ltearno avatar

Watchers

James Cloos avatar  avatar

Forkers

iden3

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.