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

Update wasm_exec in /html

This commit is contained in:
Johan Brandhorst 2018-06-17 17:09:59 +01:00
parent f34699c420
commit 3d89b1de82

View File

@ -16,13 +16,11 @@
}, },
}; };
const now = () => {
const [sec, nsec] = process.hrtime();
return sec * 1000 + nsec / 1000000;
};
global.performance = { global.performance = {
timeOrigin: Date.now() - now(), now() {
now: now, const [sec, nsec] = process.hrtime();
return sec * 1000 + nsec / 1000000;
},
}; };
const util = require("util"); const util = require("util");
@ -43,6 +41,11 @@
} }
return buf.length; return buf.length;
}, },
openSync(path, flags, mode) {
const err = new Error("not implemented");
err.code = "ENOSYS";
throw err;
},
}; };
} }
@ -51,13 +54,15 @@
global.Go = class { global.Go = class {
constructor() { constructor() {
this.argv = []; this.argv = ["js"];
this.env = {}; this.env = {};
this.exit = (code) => { this.exit = (code) => {
if (code !== 0) { if (code !== 0) {
console.warn("exit code:", code); console.warn("exit code:", code);
} }
}; };
this._callbackTimeouts = new Map();
this._nextCallbackTimeoutID = 1;
const mem = () => { const mem = () => {
// The buffer may change when requesting more memory. // The buffer may change when requesting more memory.
@ -116,6 +121,7 @@
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len)); return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
} }
const timeOrigin = Date.now() - performance.now();
this.importObject = { this.importObject = {
go: { go: {
// func wasmExit(code int32) // func wasmExit(code int32)
@ -134,7 +140,7 @@
// func nanotime() int64 // func nanotime() int64
"runtime.nanotime": (sp) => { "runtime.nanotime": (sp) => {
setInt64(sp + 8, (performance.timeOrigin + performance.now()) * 1000000); setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
}, },
// func walltime() (sec int64, nsec int32) // func walltime() (sec int64, nsec int32)
@ -144,9 +150,22 @@
mem().setInt32(sp + 16, (msec % 1000) * 1000000, true); mem().setInt32(sp + 16, (msec % 1000) * 1000000, true);
}, },
// func scheduleCallback(delay int64) // func scheduleCallback(delay int64) int32
"runtime.scheduleCallback": (sp) => { "runtime.scheduleCallback": (sp) => {
setTimeout(() => { this._resolveCallbackPromise(); }, getInt64(sp + 8)); const id = this._nextCallbackTimeoutID;
this._nextCallbackTimeoutID++;
this._callbackTimeouts.set(id, setTimeout(
() => { this._resolveCallbackPromise(); },
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
));
mem().setInt32(sp + 16, id, true);
},
// func clearScheduledCallback(id int32)
"runtime.clearScheduledCallback": (sp) => {
const id = mem().getInt32(sp + 8, true);
clearTimeout(this._callbackTimeouts.get(id));
this._callbackTimeouts.delete(id);
}, },
// func getRandomData(r []byte) // func getRandomData(r []byte)
@ -154,48 +173,48 @@
crypto.getRandomValues(loadSlice(sp + 8)); crypto.getRandomValues(loadSlice(sp + 8));
}, },
// func boolVal(value bool) Value // func boolVal(value bool) ref
"syscall/js.boolVal": (sp) => { "syscall/js.boolVal": (sp) => {
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0); storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
}, },
// func intVal(value int) Value // func intVal(value int) ref
"syscall/js.intVal": (sp) => { "syscall/js.intVal": (sp) => {
storeValue(sp + 16, getInt64(sp + 8)); storeValue(sp + 16, getInt64(sp + 8));
}, },
// func floatVal(value float64) Value // func floatVal(value float64) ref
"syscall/js.floatVal": (sp) => { "syscall/js.floatVal": (sp) => {
storeValue(sp + 16, mem().getFloat64(sp + 8, true)); storeValue(sp + 16, mem().getFloat64(sp + 8, true));
}, },
// func stringVal(value string) Value // func stringVal(value string) ref
"syscall/js.stringVal": (sp) => { "syscall/js.stringVal": (sp) => {
storeValue(sp + 24, loadString(sp + 8)); storeValue(sp + 24, loadString(sp + 8));
}, },
// func (v Value) Get(key string) Value // func valueGet(v ref, p string) ref
"syscall/js.Value.Get": (sp) => { "syscall/js.valueGet": (sp) => {
storeValue(sp + 32, Reflect.get(loadValue(sp + 8), loadString(sp + 16))); storeValue(sp + 32, Reflect.get(loadValue(sp + 8), loadString(sp + 16)));
}, },
// func (v Value) set(key string, value Value) // func valueSet(v ref, p string, x ref)
"syscall/js.Value.set": (sp) => { "syscall/js.valueSet": (sp) => {
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32)); Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
}, },
// func (v Value) Index(i int) Value // func valueIndex(v ref, i int) ref
"syscall/js.Value.Index": (sp) => { "syscall/js.valueIndex": (sp) => {
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16))); storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
}, },
// func (v Value) setIndex(i int, value Value) // valueSetIndex(v ref, i int, x ref)
"syscall/js.Value.setIndex": (sp) => { "syscall/js.valueSetIndex": (sp) => {
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24)); Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
}, },
// func (v Value) call(name string, args []Value) (Value, bool) // func valueCall(v ref, m string, args []ref) (ref, bool)
"syscall/js.Value.call": (sp) => { "syscall/js.valueCall": (sp) => {
try { try {
const v = loadValue(sp + 8); const v = loadValue(sp + 8);
const m = Reflect.get(v, loadString(sp + 16)); const m = Reflect.get(v, loadString(sp + 16));
@ -208,8 +227,8 @@
} }
}, },
// func (v Value) invoke(args []Value) (Value, bool) // func valueInvoke(v ref, args []ref) (ref, bool)
"syscall/js.Value.invoke": (sp) => { "syscall/js.valueInvoke": (sp) => {
try { try {
const v = loadValue(sp + 8); const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16); const args = loadSliceOfValues(sp + 16);
@ -221,8 +240,8 @@
} }
}, },
// func (v Value) new(args []Value) (Value, bool) // func valueNew(v ref, args []ref) (ref, bool)
"syscall/js.Value.new": (sp) => { "syscall/js.valueNew": (sp) => {
try { try {
const v = loadValue(sp + 8); const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16); const args = loadSliceOfValues(sp + 16);
@ -234,35 +253,35 @@
} }
}, },
// func (v Value) Float() float64 // func valueFloat(v ref) float64
"syscall/js.Value.Float": (sp) => { "syscall/js.valueFloat": (sp) => {
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true); mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
}, },
// func (v Value) Int() int // func valueInt(v ref) int
"syscall/js.Value.Int": (sp) => { "syscall/js.valueInt": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8))); setInt64(sp + 16, parseInt(loadValue(sp + 8)));
}, },
// func (v Value) Bool() bool // func valueBool(v ref) bool
"syscall/js.Value.Bool": (sp) => { "syscall/js.valueBool": (sp) => {
mem().setUint8(sp + 16, !!loadValue(sp + 8)); mem().setUint8(sp + 16, !!loadValue(sp + 8));
}, },
// func (v Value) Length() int // func valueLength(v ref) int
"syscall/js.Value.Length": (sp) => { "syscall/js.valueLength": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8).length)); setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
}, },
// func (v Value) prepareString() (Value, int) // valuePrepareString(v ref) (ref, int)
"syscall/js.Value.prepareString": (sp) => { "syscall/js.valuePrepareString": (sp) => {
const str = encoder.encode(String(loadValue(sp + 8))); const str = encoder.encode(String(loadValue(sp + 8)));
storeValue(sp + 16, str); storeValue(sp + 16, str);
setInt64(sp + 24, str.length); setInt64(sp + 24, str.length);
}, },
// func (v Value) loadString(b []byte) // valueLoadString(v ref, b []byte)
"syscall/js.Value.loadString": (sp) => { "syscall/js.valueLoadString": (sp) => {
const str = loadValue(sp + 8); const str = loadValue(sp + 8);
loadSlice(sp + 16).set(str); loadSlice(sp + 16).set(str);
}, },
@ -281,7 +300,7 @@
null, null,
global, global,
this._inst.exports.mem, this._inst.exports.mem,
() => { () => { // resolveCallbackPromise
if (this.exited) { if (this.exited) {
throw new Error("bad callback: Go program has already exited"); throw new Error("bad callback: Go program has already exited");
} }
@ -355,6 +374,7 @@
return go.run(result.instance); return go.run(result.instance);
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
go.exited = true;
process.exit(1); process.exit(1);
}); });
} }