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:
parent
e16f079d16
commit
0bc494b8b2
1
html/.gitignore
vendored
Normal file
1
html/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
test.wasm
|
@ -1,4 +1,9 @@
|
|||||||
<!doctype html>
|
<!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>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
@ -12,13 +17,14 @@
|
|||||||
async function loadAndCompile() {
|
async function loadAndCompile() {
|
||||||
let resp = await fetch("test.wasm");
|
let resp = await fetch("test.wasm");
|
||||||
let bytes = await resp.arrayBuffer();
|
let bytes = await resp.arrayBuffer();
|
||||||
await compile(bytes);
|
await go.compile(bytes);
|
||||||
document.getElementById("runButton").disabled = false;
|
document.getElementById("runButton").disabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadAndCompile();
|
loadAndCompile();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button onClick="console.clear(); run();" id="runButton" disabled>Run</button>
|
<button onClick="console.clear(); go.run();" id="runButton" disabled>Run</button>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,387 +1,357 @@
|
|||||||
let args = ["js"];
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
let trace = false;
|
// 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 () {
|
||||||
if (process.argv.length < 3) {
|
let args = ["js"];
|
||||||
process.stderr.write("usage: go_js_wasm_exec [wasm binary]\n");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
args = args.concat(process.argv.slice(3));
|
// Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API).
|
||||||
trace = process.env.TRACE === "1";
|
const isNodeJS = typeof process !== "undefined";
|
||||||
global.require = require;
|
if (isNodeJS) {
|
||||||
|
if (process.argv.length < 3) {
|
||||||
|
process.stderr.write("usage: go_js_wasm_exec [wasm binary]\n");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
global.fs = require("fs");
|
args = args.concat(process.argv.slice(3));
|
||||||
|
global.require = require;
|
||||||
|
|
||||||
const nodeCrypto = require("crypto");
|
global.fs = require("fs");
|
||||||
global.crypto = {
|
|
||||||
getRandomValues(b) {
|
|
||||||
nodeCrypto.randomFillSync(b);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const now = () => {
|
const nodeCrypto = require("crypto");
|
||||||
const [sec, nsec] = process.hrtime();
|
global.crypto = {
|
||||||
return sec * 1000 + nsec / 1000000;
|
getRandomValues(b) {
|
||||||
};
|
nodeCrypto.randomFillSync(b);
|
||||||
global.performance = {
|
},
|
||||||
timeOrigin: Date.now() - now(),
|
};
|
||||||
now: now,
|
|
||||||
};
|
|
||||||
|
|
||||||
const util = require("util");
|
const now = () => {
|
||||||
global.TextEncoder = util.TextEncoder;
|
const [sec, nsec] = process.hrtime();
|
||||||
global.TextDecoder = util.TextDecoder;
|
return sec * 1000 + nsec / 1000000;
|
||||||
|
};
|
||||||
|
global.performance = {
|
||||||
|
timeOrigin: Date.now() - now(),
|
||||||
|
now: now,
|
||||||
|
};
|
||||||
|
|
||||||
compileAndRun(fs.readFileSync(process.argv[2])).catch((err) => {
|
const util = require("util");
|
||||||
console.error(err);
|
global.TextEncoder = util.TextEncoder;
|
||||||
});
|
global.TextDecoder = util.TextDecoder;
|
||||||
} else {
|
} else {
|
||||||
window.global = window;
|
window.global = window;
|
||||||
|
|
||||||
global.process = {
|
global.process = {
|
||||||
env: {},
|
env: {},
|
||||||
exit(code) {
|
exit(code) {
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
console.warn("exit code:", code);
|
console.warn("exit code:", code);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let outputBuf = "";
|
let outputBuf = "";
|
||||||
global.fs = {
|
global.fs = {
|
||||||
constants: {},
|
constants: {},
|
||||||
writeSync(fd, buf) {
|
writeSync(fd, buf) {
|
||||||
outputBuf += decoder.decode(buf);
|
outputBuf += decoder.decode(buf);
|
||||||
const nl = outputBuf.lastIndexOf("\n");
|
const nl = outputBuf.lastIndexOf("\n");
|
||||||
if (nl != -1) {
|
if (nl != -1) {
|
||||||
console.log(outputBuf.substr(0, nl));
|
console.log(outputBuf.substr(0, nl));
|
||||||
outputBuf = outputBuf.substr(nl + 1);
|
outputBuf = outputBuf.substr(nl + 1);
|
||||||
}
|
}
|
||||||
return buf.length;
|
return buf.length;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const encoder = new TextEncoder("utf-8");
|
const encoder = new TextEncoder("utf-8");
|
||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
|
||||||
let mod, inst;
|
let mod, inst;
|
||||||
let names = [];
|
let values = []; // TODO: garbage collection
|
||||||
let prevSP = 0;
|
let resolveResume = function () { };
|
||||||
let values = []; // TODO: GC
|
|
||||||
|
|
||||||
function mem() {
|
function mem() {
|
||||||
return new DataView(inst.exports.mem.buffer);
|
// The buffer may change when requesting more memory.
|
||||||
}
|
return new DataView(inst.exports.mem.buffer);
|
||||||
|
}
|
||||||
|
|
||||||
function setInt64(addr, v) {
|
function setInt64(addr, v) {
|
||||||
mem().setUint32(addr + 0, v, true);
|
mem().setUint32(addr + 0, v, true);
|
||||||
if (v >= 0) {
|
if (v >= 0) {
|
||||||
mem().setUint32(addr + 4, v / 4294967296, true);
|
mem().setUint32(addr + 4, v / 4294967296, true);
|
||||||
} else {
|
} else {
|
||||||
mem().setUint32(addr + 4, -1, true); // FIXME
|
mem().setUint32(addr + 4, -1, true); // FIXME
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInt64(addr) {
|
function getInt64(addr) {
|
||||||
const low = mem().getUint32(addr + 0, true);
|
const low = mem().getUint32(addr + 0, true);
|
||||||
const high = mem().getInt32(addr + 4, true);
|
const high = mem().getInt32(addr + 4, true);
|
||||||
return low + high * 4294967296;
|
return low + high * 4294967296;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadValue(addr) {
|
function loadValue(addr) {
|
||||||
const id = mem().getUint32(addr, true);
|
const id = mem().getUint32(addr, true);
|
||||||
return values[id];
|
return values[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeValue(addr, v) {
|
function storeValue(addr, v) {
|
||||||
if (v === undefined) {
|
if (v === undefined) {
|
||||||
mem().setUint32(addr, 0, true);
|
mem().setUint32(addr, 0, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (v === null) {
|
if (v === null) {
|
||||||
mem().setUint32(addr, 1, true);
|
mem().setUint32(addr, 1, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
values.push(v);
|
values.push(v);
|
||||||
mem().setUint32(addr, values.length - 1, true);
|
mem().setUint32(addr, values.length - 1, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSlice(addr) {
|
function loadSlice(addr) {
|
||||||
const array = getInt64(addr + 0);
|
const array = getInt64(addr + 0);
|
||||||
const len = getInt64(addr + 8);
|
const len = getInt64(addr + 8);
|
||||||
return new Uint8Array(inst.exports.mem.buffer, array, len);
|
return new Uint8Array(inst.exports.mem.buffer, array, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSliceOfValues(addr) {
|
function loadSliceOfValues(addr) {
|
||||||
const array = getInt64(addr + 0);
|
const array = getInt64(addr + 0);
|
||||||
const len = getInt64(addr + 8);
|
const len = getInt64(addr + 8);
|
||||||
const a = new Array(len);
|
const a = new Array(len);
|
||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
const id = mem().getUint32(array + i * 4, true);
|
const id = mem().getUint32(array + i * 4, true);
|
||||||
a[i] = values[id];
|
a[i] = values[id];
|
||||||
}
|
}
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadString(addr) {
|
function loadString(addr) {
|
||||||
const saddr = getInt64(addr + 0);
|
const saddr = getInt64(addr + 0);
|
||||||
const len = getInt64(addr + 8);
|
const len = getInt64(addr + 8);
|
||||||
return decoder.decode(new DataView(inst.exports.mem.buffer, saddr, len));
|
return decoder.decode(new DataView(inst.exports.mem.buffer, saddr, len));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compileAndRun(source) {
|
global.go = {
|
||||||
await compile(source);
|
exited: false,
|
||||||
await run();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function compile(source) {
|
compileAndRun: async function (source) {
|
||||||
mod = await WebAssembly.compile(source);
|
await go.compile(source);
|
||||||
}
|
await go.run();
|
||||||
|
},
|
||||||
|
|
||||||
async function run() {
|
compile: async function (source) {
|
||||||
let importObject = {
|
mod = await WebAssembly.compile(source);
|
||||||
js: {
|
},
|
||||||
// func wasmexit(code int32)
|
|
||||||
"runtime.wasmexit": function (sp) {
|
|
||||||
process.exit(mem().getInt32(sp + 8, true));
|
|
||||||
},
|
|
||||||
|
|
||||||
// func wasmwrite(fd uintptr, p unsafe.Pointer, n int32)
|
run: async function () {
|
||||||
"runtime.wasmwrite": function (sp) {
|
let importObject = {
|
||||||
const fd = getInt64(sp + 8);
|
go: {
|
||||||
const p = getInt64(sp + 16);
|
// func wasmExit(code int32)
|
||||||
const n = mem().getInt32(sp + 24, true);
|
"runtime.wasmExit": function (sp) {
|
||||||
fs.writeSync(fd, new Uint8Array(inst.exports.mem.buffer, p, n));
|
go.exited = true;
|
||||||
},
|
process.exit(mem().getInt32(sp + 8, true));
|
||||||
|
},
|
||||||
|
|
||||||
// func nanotime() int64
|
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
||||||
"runtime.nanotime": function (sp) {
|
"runtime.wasmWrite": function (sp) {
|
||||||
setInt64(sp + 8, (performance.timeOrigin + performance.now()) * 1000000);
|
const fd = getInt64(sp + 8);
|
||||||
},
|
const p = getInt64(sp + 16);
|
||||||
|
const n = mem().getInt32(sp + 24, true);
|
||||||
|
fs.writeSync(fd, new Uint8Array(inst.exports.mem.buffer, p, n));
|
||||||
|
},
|
||||||
|
|
||||||
// func walltime() (sec int64, nsec int32)
|
// func nanotime() int64
|
||||||
"runtime.walltime": function (sp) {
|
"runtime.nanotime": function (sp) {
|
||||||
const msec = (new Date).getTime();
|
setInt64(sp + 8, (performance.timeOrigin + performance.now()) * 1000000);
|
||||||
setInt64(sp + 8, msec / 1000);
|
},
|
||||||
mem().setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
|
||||||
},
|
|
||||||
|
|
||||||
// func boolVal(value bool) Value
|
// func walltime() (sec int64, nsec int32)
|
||||||
"runtime/js.boolVal": function (sp) {
|
"runtime.walltime": function (sp) {
|
||||||
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
|
const msec = (new Date).getTime();
|
||||||
},
|
setInt64(sp + 8, msec / 1000);
|
||||||
|
mem().setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
||||||
|
},
|
||||||
|
|
||||||
// func intVal(value int) Value
|
// func boolVal(value bool) Value
|
||||||
"runtime/js.intVal": function (sp) {
|
"syscall/js.boolVal": function (sp) {
|
||||||
storeValue(sp + 16, getInt64(sp + 8));
|
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func floatVal(value float64) Value
|
// func intVal(value int) Value
|
||||||
"runtime/js.floatVal": function (sp) {
|
"syscall/js.intVal": function (sp) {
|
||||||
storeValue(sp + 16, mem().getFloat64(sp + 8, true));
|
storeValue(sp + 16, getInt64(sp + 8));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func stringVal(value string) Value
|
// func floatVal(value float64) Value
|
||||||
"runtime/js.stringVal": function (sp) {
|
"syscall/js.floatVal": function (sp) {
|
||||||
storeValue(sp + 24, loadString(sp + 8));
|
storeValue(sp + 16, mem().getFloat64(sp + 8, true));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) Get(key string) Value
|
// func stringVal(value string) Value
|
||||||
"runtime/js.Value.Get": function (sp) {
|
"syscall/js.stringVal": function (sp) {
|
||||||
storeValue(sp + 32, Reflect.get(loadValue(sp + 8), loadString(sp + 16)));
|
storeValue(sp + 24, loadString(sp + 8));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) set(key string, value Value)
|
// func (v Value) Get(key string) Value
|
||||||
"runtime/js.Value.set": function (sp) {
|
"syscall/js.Value.Get": function (sp) {
|
||||||
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
storeValue(sp + 32, Reflect.get(loadValue(sp + 8), loadString(sp + 16)));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) Index(i int) Value
|
// func (v Value) set(key string, value Value)
|
||||||
"runtime/js.Value.Index": function (sp) {
|
"syscall/js.Value.set": function (sp) {
|
||||||
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) setIndex(i int, value Value)
|
// func (v Value) Index(i int) Value
|
||||||
"runtime/js.Value.setIndex": function (sp) {
|
"syscall/js.Value.Index": function (sp) {
|
||||||
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) call(name string, args []Value) (Value, bool)
|
// func (v Value) setIndex(i int, value Value)
|
||||||
"runtime/js.Value.call": function (sp) {
|
"syscall/js.Value.setIndex": function (sp) {
|
||||||
try {
|
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
||||||
const v = loadValue(sp + 8);
|
},
|
||||||
const m = Reflect.get(v, loadString(sp + 16));
|
|
||||||
const args = loadSliceOfValues(sp + 32);
|
|
||||||
storeValue(sp + 56, Reflect.apply(m, v, args));
|
|
||||||
mem().setUint8(sp + 60, 1);
|
|
||||||
} catch (err) {
|
|
||||||
storeValue(sp + 56, err);
|
|
||||||
mem().setUint8(sp + 60, 0);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// func (v Value) invoke(args []Value) (Value, bool)
|
// func (v Value) call(name string, args []Value) (Value, bool)
|
||||||
"runtime/js.Value.invoke": function (sp) {
|
"syscall/js.Value.call": function (sp) {
|
||||||
try {
|
try {
|
||||||
const v = loadValue(sp + 8);
|
const v = loadValue(sp + 8);
|
||||||
const args = loadSliceOfValues(sp + 16);
|
const m = Reflect.get(v, loadString(sp + 16));
|
||||||
storeValue(sp + 40, Reflect.apply(v, undefined, args));
|
const args = loadSliceOfValues(sp + 32);
|
||||||
mem().setUint8(sp + 44, 1);
|
storeValue(sp + 56, Reflect.apply(m, v, args));
|
||||||
} catch (err) {
|
mem().setUint8(sp + 60, 1);
|
||||||
storeValue(sp + 40, err);
|
} catch (err) {
|
||||||
mem().setUint8(sp + 44, 0);
|
storeValue(sp + 56, err);
|
||||||
}
|
mem().setUint8(sp + 60, 0);
|
||||||
},
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// func (v Value) wasmnew(args []Value) (Value, bool)
|
// func (v Value) invoke(args []Value) (Value, bool)
|
||||||
"runtime/js.Value.wasmnew": function (sp) {
|
"syscall/js.Value.invoke": function (sp) {
|
||||||
try {
|
try {
|
||||||
const v = loadValue(sp + 8);
|
const v = loadValue(sp + 8);
|
||||||
const args = loadSliceOfValues(sp + 16);
|
const args = loadSliceOfValues(sp + 16);
|
||||||
storeValue(sp + 40, Reflect.construct(v, args));
|
storeValue(sp + 40, Reflect.apply(v, undefined, args));
|
||||||
mem().setUint8(sp + 44, 1);
|
mem().setUint8(sp + 44, 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
storeValue(sp + 40, err);
|
storeValue(sp + 40, err);
|
||||||
mem().setUint8(sp + 44, 0);
|
mem().setUint8(sp + 44, 0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) Float() float64
|
// func (v Value) new(args []Value) (Value, bool)
|
||||||
"runtime/js.Value.Float": function (sp) {
|
"syscall/js.Value.new": function (sp) {
|
||||||
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
|
try {
|
||||||
},
|
const v = loadValue(sp + 8);
|
||||||
|
const args = loadSliceOfValues(sp + 16);
|
||||||
|
storeValue(sp + 40, Reflect.construct(v, args));
|
||||||
|
mem().setUint8(sp + 44, 1);
|
||||||
|
} catch (err) {
|
||||||
|
storeValue(sp + 40, err);
|
||||||
|
mem().setUint8(sp + 44, 0);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// func (v Value) Int() int
|
// func (v Value) Float() float64
|
||||||
"runtime/js.Value.Int": function (sp) {
|
"syscall/js.Value.Float": function (sp) {
|
||||||
setInt64(sp + 16, parseInt(loadValue(sp + 8)));
|
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) Bool() bool
|
// func (v Value) Int() int
|
||||||
"runtime/js.Value.Bool": function (sp) {
|
"syscall/js.Value.Int": function (sp) {
|
||||||
mem().setUint8(sp + 16, !!loadValue(sp + 8));
|
setInt64(sp + 16, parseInt(loadValue(sp + 8)));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) Length() int
|
// func (v Value) Bool() bool
|
||||||
"runtime/js.Value.Length": function (sp) {
|
"syscall/js.Value.Bool": function (sp) {
|
||||||
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
mem().setUint8(sp + 16, !!loadValue(sp + 8));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func (v Value) prepareString() (Value, int)
|
// func (v Value) Length() int
|
||||||
"runtime/js.Value.prepareString": function (sp) {
|
"syscall/js.Value.Length": function (sp) {
|
||||||
const str = encoder.encode(String(loadValue(sp + 8)));
|
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
||||||
storeValue(sp + 16, str);
|
},
|
||||||
setInt64(sp + 24, str.length);
|
|
||||||
},
|
|
||||||
|
|
||||||
// func (v Value) loadString(b []byte)
|
// func (v Value) prepareString() (Value, int)
|
||||||
"runtime/js.Value.loadString": function (sp) {
|
"syscall/js.Value.prepareString": function (sp) {
|
||||||
const str = loadValue(sp + 8);
|
const str = encoder.encode(String(loadValue(sp + 8)));
|
||||||
loadSlice(sp + 16).set(str);
|
storeValue(sp + 16, str);
|
||||||
},
|
setInt64(sp + 24, str.length);
|
||||||
|
},
|
||||||
|
|
||||||
"trace": function (pcF, pcB, sp) {
|
// func (v Value) loadString(b []byte)
|
||||||
console.log(
|
"syscall/js.Value.loadString": function (sp) {
|
||||||
`trace`,
|
const str = loadValue(sp + 8);
|
||||||
String(sp).padStart(10),
|
loadSlice(sp + 16).set(str);
|
||||||
String(pcF).padStart(10),
|
},
|
||||||
String(pcB).padStart(10),
|
|
||||||
sp <= prevSP ? "->" : "<-",
|
|
||||||
names[pcF],
|
|
||||||
);
|
|
||||||
prevSP = sp;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const nameSec = new SimpleStream(new Uint8Array(WebAssembly.Module.customSections(mod, "name")[0]));
|
"debug": function (value) {
|
||||||
while (!nameSec.atEnd()) {
|
console.log(value);
|
||||||
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);
|
inst = await WebAssembly.instantiate(mod, importObject);
|
||||||
values = [undefined, null, global, inst.exports.mem];
|
values = [
|
||||||
|
undefined,
|
||||||
|
null,
|
||||||
|
global,
|
||||||
|
inst.exports.mem,
|
||||||
|
function () { resolveResume(); },
|
||||||
|
];
|
||||||
|
|
||||||
let offset = 4096;
|
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
||||||
|
let offset = 4096;
|
||||||
|
|
||||||
const strPtr = (str) => {
|
const strPtr = (str) => {
|
||||||
let ptr = offset;
|
let ptr = offset;
|
||||||
new Uint8Array(inst.exports.mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
|
new Uint8Array(inst.exports.mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
|
||||||
offset += str.length + (8 - (str.length % 8));
|
offset += str.length + (8 - (str.length % 8));
|
||||||
return ptr;
|
return ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
const argc = args.length;
|
const argc = args.length;
|
||||||
|
|
||||||
const argvPtrs = [];
|
const argvPtrs = [];
|
||||||
args.forEach((arg) => {
|
args.forEach((arg) => {
|
||||||
argvPtrs.push(strPtr(arg));
|
argvPtrs.push(strPtr(arg));
|
||||||
});
|
});
|
||||||
|
|
||||||
const keys = Object.keys(process.env).sort();
|
const keys = Object.keys(process.env).sort();
|
||||||
argvPtrs.push(keys.length);
|
argvPtrs.push(keys.length);
|
||||||
keys.forEach((key) => {
|
keys.forEach((key) => {
|
||||||
argvPtrs.push(strPtr(`${key}=${process.env[key]}`));
|
argvPtrs.push(strPtr(`${key}=${process.env[key]}`));
|
||||||
});
|
});
|
||||||
|
|
||||||
const argv = offset;
|
const argv = offset;
|
||||||
argvPtrs.forEach((ptr) => {
|
argvPtrs.forEach((ptr) => {
|
||||||
mem().setUint32(offset, ptr, true);
|
mem().setUint32(offset, ptr, true);
|
||||||
mem().setUint32(offset + 4, 0, true);
|
mem().setUint32(offset + 4, 0, true);
|
||||||
offset += 8;
|
offset += 8;
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
inst.exports.run(argc, argv, trace);
|
while (true) {
|
||||||
} catch (err) {
|
inst.exports.run(argc, argv);
|
||||||
console.error(err);
|
if (go.exited) {
|
||||||
process.exit(1);
|
break;
|
||||||
}
|
}
|
||||||
}
|
await new Promise((resolve) => {
|
||||||
|
resolveResume = resolve;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
class SimpleStream {
|
if (isNodeJS) {
|
||||||
constructor(array) {
|
go.compileAndRun(fs.readFileSync(process.argv[2])).catch((err) => {
|
||||||
this.array = array;
|
console.error(err);
|
||||||
this.offset = 0;
|
process.exit(1);
|
||||||
}
|
});
|
||||||
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user