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

Update to using go1.11beta1

This commit is contained in:
Johan Brandhorst 2018-06-27 07:57:28 +01:00
parent c7ec9fde8c
commit 1bb4e7fb1e
No known key found for this signature in database
GPG Key ID: 266C7D9B44EAA057
4 changed files with 169 additions and 99 deletions

File diff suppressed because one or more lines are too long

View File

@ -16,7 +16,7 @@ import (
// Build with Go WASM fork // Build with Go WASM fork
//go:generate rm -f ./html/test.wasm //go:generate rm -f ./html/test.wasm
//go:generate bash -c "GOOS=js GOARCH=wasm GOROOT=$GOPATH/src/github.com/johanbrandhorst/go/ $GOPATH/src/github.com/johanbrandhorst/go/bin/go build -o ./html/test.wasm frontend.go" //go:generate bash -c "GOOS=js GOARCH=wasm go1.11beta1 build -o ./html/test.wasm frontend.go"
// Integrate generated JS into a Go file for static loading. // Integrate generated JS into a Go file for static loading.
//go:generate bash -c "go run assets_generate.go" //go:generate bash -c "go run assets_generate.go"

View File

@ -31,7 +31,7 @@
let outputBuf = ""; let outputBuf = "";
global.fs = { global.fs = {
constants: {}, constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_NONBLOCK: -1, O_SYNC: -1 }, // unused
writeSync(fd, buf) { writeSync(fd, buf) {
outputBuf += decoder.decode(buf); outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n"); const nl = outputBuf.lastIndexOf("\n");
@ -81,21 +81,72 @@
} }
const loadValue = (addr) => { const loadValue = (addr) => {
const f = mem().getFloat64(addr, true);
if (!isNaN(f)) {
return f;
}
const id = mem().getUint32(addr, true); const id = mem().getUint32(addr, true);
return this._values[id]; return this._values[id];
} }
const storeValue = (addr, v) => { const storeValue = (addr, v) => {
if (v === undefined) { if (typeof v === "number") {
if (isNaN(v)) {
mem().setUint32(addr + 4, 0x7FF80000, true); // NaN
mem().setUint32(addr, 0, true); mem().setUint32(addr, 0, true);
return; return;
} }
if (v === null) { mem().setFloat64(addr, v, true);
mem().setUint32(addr, 1, true);
return; return;
} }
mem().setUint32(addr + 4, 0x7FF80000, true); // NaN
switch (v) {
case undefined:
mem().setUint32(addr, 1, true);
return;
case null:
mem().setUint32(addr, 2, true);
return;
case true:
mem().setUint32(addr, 3, true);
return;
case false:
mem().setUint32(addr, 4, true);
return;
}
if (typeof v === "string") {
let ref = this._stringRefs.get(v);
if (ref === undefined) {
ref = this._values.length;
this._values.push(v); this._values.push(v);
mem().setUint32(addr, this._values.length - 1, true); this._stringRefs.set(v, ref);
}
mem().setUint32(addr, ref, true);
return;
}
if (typeof v === "symbol") {
let ref = this._symbolRefs.get(v);
if (ref === undefined) {
ref = this._values.length;
this._values.push(v);
this._symbolRefs.set(v, ref);
}
mem().setUint32(addr, ref, true);
return;
}
let ref = v[this._refProp];
if (ref === undefined) {
ref = this._values.length;
this._values.push(v);
v[this._refProp] = ref;
}
mem().setUint32(addr, ref, true);
} }
const loadSlice = (addr) => { const loadSlice = (addr) => {
@ -109,8 +160,7 @@
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); a[i] = loadValue(array + i * 8);
a[i] = this._values[id];
} }
return a; return a;
} }
@ -173,21 +223,6 @@
crypto.getRandomValues(loadSlice(sp + 8)); crypto.getRandomValues(loadSlice(sp + 8));
}, },
// func boolVal(value bool) ref
"syscall/js.boolVal": (sp) => {
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
},
// func intVal(value int) ref
"syscall/js.intVal": (sp) => {
storeValue(sp + 16, getInt64(sp + 8));
},
// func floatVal(value float64) ref
"syscall/js.floatVal": (sp) => {
storeValue(sp + 16, mem().getFloat64(sp + 8, true));
},
// func stringVal(value string) ref // 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));
@ -220,10 +255,10 @@
const m = Reflect.get(v, loadString(sp + 16)); const m = Reflect.get(v, loadString(sp + 16));
const args = loadSliceOfValues(sp + 32); const args = loadSliceOfValues(sp + 32);
storeValue(sp + 56, Reflect.apply(m, v, args)); storeValue(sp + 56, Reflect.apply(m, v, args));
mem().setUint8(sp + 60, 1); mem().setUint8(sp + 64, 1);
} catch (err) { } catch (err) {
storeValue(sp + 56, err); storeValue(sp + 56, err);
mem().setUint8(sp + 60, 0); mem().setUint8(sp + 64, 0);
} }
}, },
@ -233,10 +268,10 @@
const v = loadValue(sp + 8); const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16); const args = loadSliceOfValues(sp + 16);
storeValue(sp + 40, Reflect.apply(v, undefined, args)); storeValue(sp + 40, Reflect.apply(v, undefined, args));
mem().setUint8(sp + 44, 1); mem().setUint8(sp + 48, 1);
} catch (err) { } catch (err) {
storeValue(sp + 40, err); storeValue(sp + 40, err);
mem().setUint8(sp + 44, 0); mem().setUint8(sp + 48, 0);
} }
}, },
@ -246,28 +281,13 @@
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.construct(v, args));
mem().setUint8(sp + 44, 1); mem().setUint8(sp + 48, 1);
} catch (err) { } catch (err) {
storeValue(sp + 40, err); storeValue(sp + 40, err);
mem().setUint8(sp + 44, 0); mem().setUint8(sp + 48, 0);
} }
}, },
// func valueFloat(v ref) float64
"syscall/js.valueFloat": (sp) => {
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
},
// func valueInt(v ref) int
"syscall/js.valueInt": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8)));
},
// func valueBool(v ref) bool
"syscall/js.valueBool": (sp) => {
mem().setUint8(sp + 16, !!loadValue(sp + 8));
},
// func valueLength(v ref) int // func valueLength(v ref) int
"syscall/js.valueLength": (sp) => { "syscall/js.valueLength": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8).length)); setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
@ -286,6 +306,11 @@
loadSlice(sp + 16).set(str); loadSlice(sp + 16).set(str);
}, },
// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
},
"debug": (value) => { "debug": (value) => {
console.log(value); console.log(value);
}, },
@ -296,8 +321,11 @@
async run(instance) { async run(instance) {
this._inst = instance; this._inst = instance;
this._values = [ // TODO: garbage collection this._values = [ // TODO: garbage collection
NaN,
undefined, undefined,
null, null,
true,
false,
global, global,
this._inst.exports.mem, this._inst.exports.mem,
() => { // resolveCallbackPromise () => { // resolveCallbackPromise
@ -307,6 +335,9 @@
setTimeout(this._resolveCallbackPromise, 0); // make sure it is asynchronous setTimeout(this._resolveCallbackPromise, 0); // make sure it is asynchronous
}, },
]; ];
this._stringRefs = new Map();
this._symbolRefs = new Map();
this._refProp = Symbol();
this.exited = false; this.exited = false;
const mem = new DataView(this._inst.exports.mem.buffer) const mem = new DataView(this._inst.exports.mem.buffer)

View File

@ -31,7 +31,7 @@
let outputBuf = ""; let outputBuf = "";
global.fs = { global.fs = {
constants: {}, constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_NONBLOCK: -1, O_SYNC: -1 }, // unused
writeSync(fd, buf) { writeSync(fd, buf) {
outputBuf += decoder.decode(buf); outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n"); const nl = outputBuf.lastIndexOf("\n");
@ -81,21 +81,72 @@
} }
const loadValue = (addr) => { const loadValue = (addr) => {
const f = mem().getFloat64(addr, true);
if (!isNaN(f)) {
return f;
}
const id = mem().getUint32(addr, true); const id = mem().getUint32(addr, true);
return this._values[id]; return this._values[id];
} }
const storeValue = (addr, v) => { const storeValue = (addr, v) => {
if (v === undefined) { if (typeof v === "number") {
if (isNaN(v)) {
mem().setUint32(addr + 4, 0x7FF80000, true); // NaN
mem().setUint32(addr, 0, true); mem().setUint32(addr, 0, true);
return; return;
} }
if (v === null) { mem().setFloat64(addr, v, true);
mem().setUint32(addr, 1, true);
return; return;
} }
mem().setUint32(addr + 4, 0x7FF80000, true); // NaN
switch (v) {
case undefined:
mem().setUint32(addr, 1, true);
return;
case null:
mem().setUint32(addr, 2, true);
return;
case true:
mem().setUint32(addr, 3, true);
return;
case false:
mem().setUint32(addr, 4, true);
return;
}
if (typeof v === "string") {
let ref = this._stringRefs.get(v);
if (ref === undefined) {
ref = this._values.length;
this._values.push(v); this._values.push(v);
mem().setUint32(addr, this._values.length - 1, true); this._stringRefs.set(v, ref);
}
mem().setUint32(addr, ref, true);
return;
}
if (typeof v === "symbol") {
let ref = this._symbolRefs.get(v);
if (ref === undefined) {
ref = this._values.length;
this._values.push(v);
this._symbolRefs.set(v, ref);
}
mem().setUint32(addr, ref, true);
return;
}
let ref = v[this._refProp];
if (ref === undefined) {
ref = this._values.length;
this._values.push(v);
v[this._refProp] = ref;
}
mem().setUint32(addr, ref, true);
} }
const loadSlice = (addr) => { const loadSlice = (addr) => {
@ -109,8 +160,7 @@
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); a[i] = loadValue(array + i * 8);
a[i] = this._values[id];
} }
return a; return a;
} }
@ -173,21 +223,6 @@
crypto.getRandomValues(loadSlice(sp + 8)); crypto.getRandomValues(loadSlice(sp + 8));
}, },
// func boolVal(value bool) ref
"syscall/js.boolVal": (sp) => {
storeValue(sp + 16, mem().getUint8(sp + 8) !== 0);
},
// func intVal(value int) ref
"syscall/js.intVal": (sp) => {
storeValue(sp + 16, getInt64(sp + 8));
},
// func floatVal(value float64) ref
"syscall/js.floatVal": (sp) => {
storeValue(sp + 16, mem().getFloat64(sp + 8, true));
},
// func stringVal(value string) ref // 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));
@ -220,10 +255,10 @@
const m = Reflect.get(v, loadString(sp + 16)); const m = Reflect.get(v, loadString(sp + 16));
const args = loadSliceOfValues(sp + 32); const args = loadSliceOfValues(sp + 32);
storeValue(sp + 56, Reflect.apply(m, v, args)); storeValue(sp + 56, Reflect.apply(m, v, args));
mem().setUint8(sp + 60, 1); mem().setUint8(sp + 64, 1);
} catch (err) { } catch (err) {
storeValue(sp + 56, err); storeValue(sp + 56, err);
mem().setUint8(sp + 60, 0); mem().setUint8(sp + 64, 0);
} }
}, },
@ -233,10 +268,10 @@
const v = loadValue(sp + 8); const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16); const args = loadSliceOfValues(sp + 16);
storeValue(sp + 40, Reflect.apply(v, undefined, args)); storeValue(sp + 40, Reflect.apply(v, undefined, args));
mem().setUint8(sp + 44, 1); mem().setUint8(sp + 48, 1);
} catch (err) { } catch (err) {
storeValue(sp + 40, err); storeValue(sp + 40, err);
mem().setUint8(sp + 44, 0); mem().setUint8(sp + 48, 0);
} }
}, },
@ -246,28 +281,13 @@
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.construct(v, args));
mem().setUint8(sp + 44, 1); mem().setUint8(sp + 48, 1);
} catch (err) { } catch (err) {
storeValue(sp + 40, err); storeValue(sp + 40, err);
mem().setUint8(sp + 44, 0); mem().setUint8(sp + 48, 0);
} }
}, },
// func valueFloat(v ref) float64
"syscall/js.valueFloat": (sp) => {
mem().setFloat64(sp + 16, parseFloat(loadValue(sp + 8)), true);
},
// func valueInt(v ref) int
"syscall/js.valueInt": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8)));
},
// func valueBool(v ref) bool
"syscall/js.valueBool": (sp) => {
mem().setUint8(sp + 16, !!loadValue(sp + 8));
},
// func valueLength(v ref) int // func valueLength(v ref) int
"syscall/js.valueLength": (sp) => { "syscall/js.valueLength": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8).length)); setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
@ -286,6 +306,11 @@
loadSlice(sp + 16).set(str); loadSlice(sp + 16).set(str);
}, },
// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
},
"debug": (value) => { "debug": (value) => {
console.log(value); console.log(value);
}, },
@ -296,8 +321,11 @@
async run(instance) { async run(instance) {
this._inst = instance; this._inst = instance;
this._values = [ // TODO: garbage collection this._values = [ // TODO: garbage collection
NaN,
undefined, undefined,
null, null,
true,
false,
global, global,
this._inst.exports.mem, this._inst.exports.mem,
() => { // resolveCallbackPromise () => { // resolveCallbackPromise
@ -307,6 +335,9 @@
setTimeout(this._resolveCallbackPromise, 0); // make sure it is asynchronous setTimeout(this._resolveCallbackPromise, 0); // make sure it is asynchronous
}, },
]; ];
this._stringRefs = new Map();
this._symbolRefs = new Map();
this._refProp = Symbol();
this.exited = false; this.exited = false;
const mem = new DataView(this._inst.exports.mem.buffer) const mem = new DataView(this._inst.exports.mem.buffer)