1
0
mirror of https://github.com/taigrr/wasm-experiments synced 2025-01-18 04:03:21 -08:00

Update html folder for new wasm-go structure

This commit is contained in:
Johan Brandhorst 2018-05-12 15:07:14 +01:00
parent e16f079d16
commit 0bc494b8b2
3 changed files with 313 additions and 336 deletions

1
html/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test.wasm

View File

@ -1,4 +1,9 @@
<!doctype html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>
<head>
@ -12,13 +17,14 @@
async function loadAndCompile() {
let resp = await fetch("test.wasm");
let bytes = await resp.arrayBuffer();
await compile(bytes);
await go.compile(bytes);
document.getElementById("runButton").disabled = false;
}
loadAndCompile();
</script>
<button onClick="console.clear(); run();" id="runButton" disabled>Run</button>
<button onClick="console.clear(); go.run();" id="runButton" disabled>Run</button>
</body>
</html>

View File

@ -1,14 +1,19 @@
let args = ["js"];
let trace = false;
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
if (typeof process !== "undefined") {
(function () {
let args = ["js"];
// Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API).
const isNodeJS = typeof process !== "undefined";
if (isNodeJS) {
if (process.argv.length < 3) {
process.stderr.write("usage: go_js_wasm_exec [wasm binary]\n");
process.exit(1);
}
args = args.concat(process.argv.slice(3));
trace = process.env.TRACE === "1";
global.require = require;
global.fs = require("fs");
@ -32,10 +37,6 @@ if (typeof process !== "undefined") {
const util = require("util");
global.TextEncoder = util.TextEncoder;
global.TextDecoder = util.TextDecoder;
compileAndRun(fs.readFileSync(process.argv[2])).catch((err) => {
console.error(err);
});
} else {
window.global = window;
@ -67,11 +68,11 @@ const encoder = new TextEncoder("utf-8");
const decoder = new TextDecoder("utf-8");
let mod, inst;
let names = [];
let prevSP = 0;
let values = []; // TODO: GC
let values = []; // TODO: garbage collection
let resolveResume = function () { };
function mem() {
// The buffer may change when requesting more memory.
return new DataView(inst.exports.mem.buffer);
}
@ -131,25 +132,29 @@ function loadString(addr) {
return decoder.decode(new DataView(inst.exports.mem.buffer, saddr, len));
}
async function compileAndRun(source) {
await compile(source);
await run();
}
global.go = {
exited: false,
async function compile(source) {
compileAndRun: async function (source) {
await go.compile(source);
await go.run();
},
compile: async function (source) {
mod = await WebAssembly.compile(source);
}
},
async function run() {
run: async function () {
let importObject = {
js: {
// func wasmexit(code int32)
"runtime.wasmexit": function (sp) {
go: {
// func wasmExit(code int32)
"runtime.wasmExit": function (sp) {
go.exited = true;
process.exit(mem().getInt32(sp + 8, true));
},
// func wasmwrite(fd uintptr, p unsafe.Pointer, n int32)
"runtime.wasmwrite": function (sp) {
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
"runtime.wasmWrite": function (sp) {
const fd = getInt64(sp + 8);
const p = getInt64(sp + 16);
const n = mem().getInt32(sp + 24, true);
@ -169,47 +174,47 @@ async function run() {
},
// func boolVal(value bool) Value
"runtime/js.boolVal": function (sp) {
"syscall/js.boolVal": function (sp) {
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
},
// func intVal(value int) Value
"runtime/js.intVal": function (sp) {
"syscall/js.intVal": function (sp) {
storeValue(sp + 16, getInt64(sp + 8));
},
// func floatVal(value float64) Value
"runtime/js.floatVal": function (sp) {
"syscall/js.floatVal": function (sp) {
storeValue(sp + 16, mem().getFloat64(sp + 8, true));
},
// func stringVal(value string) Value
"runtime/js.stringVal": function (sp) {
"syscall/js.stringVal": function (sp) {
storeValue(sp + 24, loadString(sp + 8));
},
// func (v Value) Get(key string) Value
"runtime/js.Value.Get": function (sp) {
"syscall/js.Value.Get": function (sp) {
storeValue(sp + 32, Reflect.get(loadValue(sp + 8), loadString(sp + 16)));
},
// func (v Value) set(key string, value Value)
"runtime/js.Value.set": function (sp) {
"syscall/js.Value.set": function (sp) {
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
},
// func (v Value) Index(i int) Value
"runtime/js.Value.Index": function (sp) {
"syscall/js.Value.Index": function (sp) {
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
},
// func (v Value) setIndex(i int, value Value)
"runtime/js.Value.setIndex": function (sp) {
"syscall/js.Value.setIndex": function (sp) {
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
},
// func (v Value) call(name string, args []Value) (Value, bool)
"runtime/js.Value.call": function (sp) {
"syscall/js.Value.call": function (sp) {
try {
const v = loadValue(sp + 8);
const m = Reflect.get(v, loadString(sp + 16));
@ -223,7 +228,7 @@ async function run() {
},
// func (v Value) invoke(args []Value) (Value, bool)
"runtime/js.Value.invoke": function (sp) {
"syscall/js.Value.invoke": function (sp) {
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
@ -235,8 +240,8 @@ async function run() {
}
},
// func (v Value) wasmnew(args []Value) (Value, bool)
"runtime/js.Value.wasmnew": function (sp) {
// func (v Value) new(args []Value) (Value, bool)
"syscall/js.Value.new": function (sp) {
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
@ -249,71 +254,54 @@ async function run() {
},
// func (v Value) Float() float64
"runtime/js.Value.Float": function (sp) {
"syscall/js.Value.Float": function (sp) {
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
},
// func (v Value) Int() int
"runtime/js.Value.Int": function (sp) {
"syscall/js.Value.Int": function (sp) {
setInt64(sp + 16, parseInt(loadValue(sp + 8)));
},
// func (v Value) Bool() bool
"runtime/js.Value.Bool": function (sp) {
"syscall/js.Value.Bool": function (sp) {
mem().setUint8(sp + 16, !!loadValue(sp + 8));
},
// func (v Value) Length() int
"runtime/js.Value.Length": function (sp) {
"syscall/js.Value.Length": function (sp) {
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
},
// func (v Value) prepareString() (Value, int)
"runtime/js.Value.prepareString": function (sp) {
"syscall/js.Value.prepareString": function (sp) {
const str = encoder.encode(String(loadValue(sp + 8)));
storeValue(sp + 16, str);
setInt64(sp + 24, str.length);
},
// func (v Value) loadString(b []byte)
"runtime/js.Value.loadString": function (sp) {
"syscall/js.Value.loadString": function (sp) {
const str = loadValue(sp + 8);
loadSlice(sp + 16).set(str);
},
"trace": function (pcF, pcB, sp) {
console.log(
`trace`,
String(sp).padStart(10),
String(pcF).padStart(10),
String(pcB).padStart(10),
sp <= prevSP ? "->" : "<-",
names[pcF],
);
prevSP = sp;
"debug": function (value) {
console.log(value);
},
}
};
const nameSec = new SimpleStream(new Uint8Array(WebAssembly.Module.customSections(mod, "name")[0]));
while (!nameSec.atEnd()) {
const nameType = nameSec.readByte();
const namePayloadLen = nameSec.readUleb128();
const namePayloadData = new SimpleStream(nameSec.read(namePayloadLen));
if (nameType === 1) { // function names
const count = namePayloadData.readUleb128();
for (let i = 0; i < count; i++) {
const index = namePayloadData.readUleb128();
const nameLen = namePayloadData.readUleb128();
const nameStr = String.fromCharCode.apply(null, namePayloadData.read(nameLen));
names[index] = nameStr;
}
}
}
inst = await WebAssembly.instantiate(mod, importObject);
values = [undefined, null, global, inst.exports.mem];
values = [
undefined,
null,
global,
inst.exports.mem,
function () { resolveResume(); },
];
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
let offset = 4096;
const strPtr = (str) => {
@ -344,44 +332,26 @@ async function run() {
});
try {
inst.exports.run(argc, argv, trace);
while (true) {
inst.exports.run(argc, argv);
if (go.exited) {
break;
}
await new Promise((resolve) => {
resolveResume = resolve;
});
}
} catch (err) {
console.error(err);
process.exit(1);
}
},
}
class SimpleStream {
constructor(array) {
this.array = array;
this.offset = 0;
}
atEnd() {
return this.offset >= this.array.length;
}
read(len) {
const a = this.array.subarray(this.offset, this.offset + len);
this.offset += len;
return a;
}
readByte() {
const b = this.array[this.offset];
this.offset++;
return b;
}
readUleb128() {
let value = 0;
let shift = 0;
while (true) {
let byte = this.readByte();
value |= (byte & 0x7F) << shift;
if ((byte & 0x80) === 0) { break; }
shift += 7;
}
return value;
}
if (isNodeJS) {
go.compileAndRun(fs.readFileSync(process.argv[2])).catch((err) => {
console.error(err);
process.exit(1);
});
}
})();