Add syscall/js.valueInvoke support
Этот коммит содержится в:
родитель
61750be9aa
коммит
4eb34b36f8
5 изменённых файлов: 93 добавлений и 11 удалений
|
@ -1,3 +1,8 @@
|
|||
invoke: clean wasm_exec
|
||||
tinygo build -o ./html/wasm.wasm -target wasm -no-debug ./invoke/wasm.go
|
||||
cp ./invoke/wasm.js ./html/
|
||||
cp ./invoke/index.html ./html/
|
||||
|
||||
export: clean wasm_exec
|
||||
tinygo build -o ./html/wasm.wasm -target wasm -no-debug ./export/wasm.go
|
||||
cp ./export/wasm.js ./html/
|
||||
|
|
19
src/examples/wasm/invoke/index.html
Обычный файл
19
src/examples/wasm/invoke/index.html
Обычный файл
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Go WebAssembly</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<script src="wasm_exec.js" defer></script>
|
||||
<script src="wasm.js" defer></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>WebAssembly</h1>
|
||||
<p>Edit on either side to mimic values, using WebAssembly:</p>
|
||||
<input type="text" id="a" value=""/>==<input type="text" id="b" value=""/>
|
||||
</body>
|
||||
|
||||
</html>
|
15
src/examples/wasm/invoke/wasm.go
Обычный файл
15
src/examples/wasm/invoke/wasm.go
Обычный файл
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func runner(this js.Value, args []js.Value) interface{} {
|
||||
return args[0].Invoke(args[1]).String()
|
||||
}
|
||||
|
||||
func main() {
|
||||
wait := make(chan struct{}, 0)
|
||||
js.Global().Set("runner", js.FuncOf(runner))
|
||||
<-wait
|
||||
}
|
43
src/examples/wasm/invoke/wasm.js
Обычный файл
43
src/examples/wasm/invoke/wasm.js
Обычный файл
|
@ -0,0 +1,43 @@
|
|||
'use strict';
|
||||
|
||||
const WASM_URL = 'wasm.wasm';
|
||||
|
||||
var wasm;
|
||||
|
||||
function updateRight() {
|
||||
const value = document.getElementById("a").value;
|
||||
window.runner(function (value) {
|
||||
document.getElementById("b").value = value;
|
||||
}, value);
|
||||
}
|
||||
|
||||
function updateLeft() {
|
||||
const value = document.getElementById("b").value;
|
||||
window.runner(function (value) {
|
||||
document.getElementById("a").value = value;
|
||||
}, value);
|
||||
}
|
||||
|
||||
function init() {
|
||||
document.querySelector('#a').oninput = updateRight;
|
||||
document.querySelector('#b').oninput = updateLeft;
|
||||
|
||||
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();
|
|
@ -265,17 +265,17 @@
|
|||
},
|
||||
|
||||
// func valueInvoke(v ref, args []ref) (ref, bool)
|
||||
//"syscall/js.valueInvoke": (sp) => {
|
||||
// try {
|
||||
// const v = loadValue(sp + 8);
|
||||
// const args = loadSliceOfValues(sp + 16);
|
||||
// storeValue(sp + 40, Reflect.apply(v, undefined, args));
|
||||
// mem().setUint8(sp + 48, 1);
|
||||
// } catch (err) {
|
||||
// storeValue(sp + 40, err);
|
||||
// mem().setUint8(sp + 48, 0);
|
||||
// }
|
||||
//},
|
||||
"syscall/js.valueInvoke": (ret_addr, v_addr, args_ptr, args_len, args_cap) => {
|
||||
try {
|
||||
const v = loadValue(v_addr);
|
||||
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
|
||||
storeValue(ret_addr, Reflect.apply(v, undefined, args));
|
||||
mem().setUint8(ret_addr + 8, 1);
|
||||
} catch (err) {
|
||||
storeValue(ret_addr, err);
|
||||
mem().setUint8(ret_addr + 8, 0);
|
||||
}
|
||||
},
|
||||
|
||||
// func valueNew(v ref, args []ref) (ref, bool)
|
||||
"syscall/js.valueNew": (ret_addr, v_addr, args_ptr, args_len, args_cap) => {
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче