diff --git a/src/examples/wasm/Makefile b/src/examples/wasm/Makefile index 78cdaea5..9b02bf67 100644 --- a/src/examples/wasm/Makefile +++ b/src/examples/wasm/Makefile @@ -8,6 +8,12 @@ callback: clean wasm_exec cp ./callback/wasm.js ./html/ cp ./callback/index.html ./html/ +slices: clean wasm_exec + tinygo build -o ./html/wasm.wasm -target wasm -no-debug ./slices/wasm.go + cp ./slices/wasm.js ./html/ + cp ./slices/index.html ./html/ + + main: clean wasm_exec tinygo build -o ./html/wasm.wasm -target wasm -no-debug ./main/main.go cp ./main/index.html ./html/ diff --git a/src/examples/wasm/slices/index.html b/src/examples/wasm/slices/index.html new file mode 100644 index 00000000..4ae15839 --- /dev/null +++ b/src/examples/wasm/slices/index.html @@ -0,0 +1,19 @@ + + + + + + + Go WebAssembly + + + + + + +

WebAssembly

+

type values separated by comma, using WebAssembly:

+ ==
+ + + diff --git a/src/examples/wasm/slices/wasm.go b/src/examples/wasm/slices/wasm.go new file mode 100644 index 00000000..618fdae4 --- /dev/null +++ b/src/examples/wasm/slices/wasm.go @@ -0,0 +1,23 @@ +package main + +import ( + "strings" + "syscall/js" +) + +func splitter(this js.Value, args []js.Value) interface{} { + values := strings.Split(args[0].String(), ",") + + result := make([]interface{}, 0) + for _, each := range values { + result = append(result, each) + } + + return js.ValueOf(result) +} + +func main() { + wait := make(chan struct{}, 0) + js.Global().Set("splitter", js.FuncOf(splitter)) + <-wait +} diff --git a/src/examples/wasm/slices/wasm.js b/src/examples/wasm/slices/wasm.js new file mode 100644 index 00000000..78da85bc --- /dev/null +++ b/src/examples/wasm/slices/wasm.js @@ -0,0 +1,33 @@ +'use strict'; + +const WASM_URL = 'wasm.wasm'; + +var wasm; + +function update() { + const value = document.getElementById("a").value; + document.getElementById("b").innerHTML = JSON.stringify(window.splitter(value)); +} + +function init() { + document.querySelector('#a').oninput = update; + + const go = new Go(); + if ('instantiateStreaming' in WebAssembly) { + WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function (obj) { + wasm = obj.instance; + go.run(wasm); + }) + } else { + fetch(WASM_URL).then(resp => + resp.arrayBuffer() + ).then(bytes => + WebAssembly.instantiate(bytes, go.importObject).then(function (obj) { + wasm = obj.instance; + go.run(wasm); + }) + ) + } +} + +init(); diff --git a/targets/wasm_exec.js b/targets/wasm_exec.js index 951b085c..3f2a6b5a 100644 --- a/targets/wasm_exec.js +++ b/targets/wasm_exec.js @@ -245,9 +245,9 @@ }, // valueSetIndex(v ref, i int, x ref) - //"syscall/js.valueSetIndex": (sp) => { - // Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24)); - //}, + "syscall/js.valueSetIndex": (v_addr, i, x_addr) => { + Reflect.set(loadValue(v_addr), i, loadValue(x_addr)); + }, // func valueCall(v ref, m string, args []ref) (ref, bool) "syscall/js.valueCall": (ret_addr, v_addr, m_ptr, m_len, args_ptr, args_len, args_cap) => {