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

bcdady avatar matiasinsaurralde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-wasm3's Issues

build fails on arm

I'm working on incorporating go-wasm3 into my project, compiled on an arm64 board. go build claims the m3.a file is "incompatible". Is this a supported platform for go-wasm3?

cd src/gluon; go build -o ../../bin/
# github.com/matiasinsaurralde/go-wasm3
/usr/bin/ld: skipping incompatible /home/donp/go/pkg/mod/github.com/matiasinsaurralde/[email protected]/lib/linux/libm3.a when searching for -lm3
/usr/bin/ld: cannot find -lm3

the board is a Pine64 (Allwinner A64 CPU) and the OS is Armbian (ARM Port) of Ubuntu 18.04.

I can't run the examples SIGILL: illegal instruction

Go Version:

go1.13.6 linux/amd64

CPU /Architecture/S.O

Aspire-E1-531 5.3.0-26-generic #28~18.04.1-Ubuntu SMP Wed Dec 18 16:40:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

SIGILL: illegal instruction
PC=0x4a8a6d m=0 sigcode=2

goroutine 0 [idle]:
runtime: unknown pc 0x4a8a6d
stack: frame={sp:0x7fff1d1173c0, fp:0x0} stack=[0x7fff1c918448,0x7fff1d117480)
00007fff1d1172c0: 0000006c0001a0f0 0000000000000007
00007fff1d1172d0: 0000000000000000 0000000000a411a0
00007fff1d1172e0: 0000000000002360 ffffffffffffffa8
00007fff1d1172f0: 000000000000008d 000000bd00000234
00007fff1d117300: 0000000000000011 0000000000000002
00007fff1d117310: 0000000000000000 000000770000007c
00007fff1d117320: 0000006c00000070 000000770000007c
00007fff1d117330: 0000005b0000006e 0000000000020810
00007fff1d117340: 0000000000002330 00007fc8f94858f8
00007fff1d117350: 0000000000a407f0 00007fc8f9480c40
00007fff1d117360: 0000000000000000 00007fc8f912f3a2
00007fff1d117370: 0000000000000000 00007fff1d1173c8
00007fff1d117380: 000000c000049000 0000000000000000
00007fff1d117390: 000000000050c9b4 00000000004a7dbd
00007fff1d1173a0: 0000000000000025 0000000000100000
00007fff1d1173b0: 0000000000a407e0 00000000004a8a63
00007fff1d1173c0: <000000c000048e38 0000000000a40800
00007fff1d1173d0: 00007fc8f9c0b988 000000c000048e40
00007fff1d1173e0: 000000c000048e00 00000000004a501e
00007fff1d1173f0: 000000c000048e40 000000c000048e00
00007fff1d117400: 0000000000000018 00000000004594f0 <runtime.asmcgocall+112>
00007fff1d117410: 000000000045607c <runtime.(*mheap).alloc.func1+76> 00000000007afd00
00007fff1d117420: 0000000000000001 00000000004d000c
00007fff1d117430: 00007fc8f9c0b988 0000000000000230
00007fff1d117440: 000000c000000180 0000000000457d16 <runtime.systemstack+102>
00007fff1d117450: 00000000004343c0 <runtime.mstart+0> 0000000000457ba4 <runtime.rt0_go+308>
00007fff1d117460: 00000000004b87c0 00007fff1c918448
00007fff1d117470: 00000000004b87c0 0000000000457bab <runtime.rt0_go+315>
runtime: unknown pc 0x4a8a6d
stack: frame={sp:0x7fff1d1173c0, fp:0x0} stack=[0x7fff1c918448,0x7fff1d117480)
00007fff1d1172c0: 0000006c0001a0f0 0000000000000007
00007fff1d1172d0: 0000000000000000 0000000000a411a0
00007fff1d1172e0: 0000000000002360 ffffffffffffffa8
00007fff1d1172f0: 000000000000008d 000000bd00000234
00007fff1d117300: 0000000000000011 0000000000000002
00007fff1d117310: 0000000000000000 000000770000007c
00007fff1d117320: 0000006c00000070 000000770000007c
00007fff1d117330: 0000005b0000006e 0000000000020810
00007fff1d117340: 0000000000002330 00007fc8f94858f8
00007fff1d117350: 0000000000a407f0 00007fc8f9480c40
00007fff1d117360: 0000000000000000 00007fc8f912f3a2
00007fff1d117370: 0000000000000000 00007fff1d1173c8
00007fff1d117380: 000000c000049000 0000000000000000
00007fff1d117390: 000000000050c9b4 00000000004a7dbd
00007fff1d1173a0: 0000000000000025 0000000000100000
00007fff1d1173b0: 0000000000a407e0 00000000004a8a63
00007fff1d1173c0: <000000c000048e38 0000000000a40800
00007fff1d1173d0: 00007fc8f9c0b988 000000c000048e40
00007fff1d1173e0: 000000c000048e00 00000000004a501e
00007fff1d1173f0: 000000c000048e40 000000c000048e00
00007fff1d117400: 0000000000000018 00000000004594f0 <runtime.asmcgocall+112>
00007fff1d117410: 000000000045607c <runtime.(*mheap).alloc.func1+76> 00000000007afd00
00007fff1d117420: 0000000000000001 00000000004d000c
00007fff1d117430: 00007fc8f9c0b988 0000000000000230
00007fff1d117440: 000000c000000180 0000000000457d16 <runtime.systemstack+102>
00007fff1d117450: 00000000004343c0 <runtime.mstart+0> 0000000000457ba4 <runtime.rt0_go+308>
00007fff1d117460: 00000000004b87c0 00007fff1c918448
00007fff1d117470: 00000000004b87c0 0000000000457bab <runtime.rt0_go+315>

goroutine 1> [sy>scall]:>
runtime.cgo>call>(0x4a5000, 0xc000048e40, 0xc000000000)>
/usr/lo>cal/>go/src/runtime/cgocall.go:128 +0x5b fp=0xc000048e10 sp=0xc>000>048dd8 pc=0x40896b
github.com/>mati>asinsaurralde/go-wasm3._Cfunc_m3_NewRuntime(0xa407e0, 0x10>000>0, 0x0, 0x0)
_cgo_go>type>s.go:554 +0x4e fp=0xc000048e40 sp=0xc000048e10 pc=0x4a1b8e>
github.com/>mati>asinsaurralde/go-wasm3.NewRuntime(0xc00008c020, 0xc00008c020)>
/home/e>lvis>/go/src/github.com/matiasinsaurralde/go-wasm3/wasm3.go:211 +0x46 fp=0xc000048e78 sp=0xc0000>48e40 >pc=0x4a>22d6>
main.initRu>ntimeAndModule(0x4f6f76, 0x16)>
/home/e>lvis/go/src/github.com/matiasinsaurralde/go-wasm3/examples/boa/boa.go:24 +0x99 fp=0xc000048ed0 >sp=0xc0>00048e78 pc=0x4a3f19>
main.main()>
/home/elvis/go/src/github.com/matiasinsaurralde/go-wasm3/examples/boa/boa.go:92 +0x34 fp=0xc000048f60 >sp=0xc000048ed0 pc=0x4a4724
runtime.main()
/usr/local/go/src/runtime/proc.go:203 +0x21e fp=0xc000048fe0 sp=0xc000048f60 pc=0x4317ae
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc000048fe8 sp=0xc000048fe0 pc=0x459d71

rax 0xa40800
rbx 0x100000
rcx 0xa42b00
rdx 0xa42b00
rdi 0xa40800
rsi 0x0
rbp 0xa407e0
rsp 0x7fff1d1173c0
r8 0xa40800
r9 0x0
r10 0xa40010
r11 0x0
r12 0xc000049000
r13 0x0
r14 0x50c9b4
r15 0x0
rip 0x4a8a6d
rflags 0x10206
cs 0x33
fs 0x0
gs 0x0

Windows support

Do you plan to add a precompiled WASM3 library for windows?

Update Wasm3, make use of exciting new API

With latest Wasm3 (v0.4.9), new API is available which fixes many things in the public API:

  • Exported function signature introspection
  • Introspection of imported functions (see _ctx argument)
  • Public api for calls with raw arguments (instead of strings)
  • Public API to get return values
  • Support recursive m3_Call invocation. I.e. calling exported function from within imported function callback.
  • m3_RunStart can be used to force the module initialization.

I think all this can greatly help your bindings implementation.
@matiasinsaurralde please let me know it you need any other public API, or have any ideas!

This is already implemented in Python bindings, you can use it as a reference:
https://github.com/wasm3/wasm3/blob/master/platforms/python/m3module.c

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.