Update wasm_exec.js
This commit is contained in:
parent
cecc77044d
commit
9ca0744c08
|
@ -1 +1,2 @@
|
||||||
dist/
|
dist/
|
||||||
|
assets/main.wasm
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
build:
|
build:
|
||||||
mkdir -p dist/
|
mkdir -p dist/
|
||||||
GOOS=js GOARCH=wasm go build -o assets/json.wasm ./cmd/wasm
|
GOOS=js GOARCH=wasm go build -o assets/main.wasm ./cmd/wasm
|
||||||
go build -o dist/wasm-example-server .
|
go build -o dist/wasm-example-server .
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<script src="wasm_exec.js"></script>
|
<script src="wasm_exec.js"></script>
|
||||||
<script>
|
<script>
|
||||||
const go = new Go();
|
const go = new Go();
|
||||||
WebAssembly.instantiateStreaming(fetch("json.wasm"), go.importObject).then((result) => {
|
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
|
||||||
go.run(result.instance);
|
go.run(result.instance);
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
BIN
assets/json.wasm
BIN
assets/json.wasm
Binary file not shown.
|
@ -2,54 +2,25 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
// Map multiple JavaScript environments to a single common API,
|
|
||||||
// preferring web standards over Node.js API.
|
|
||||||
//
|
|
||||||
// Environments considered:
|
|
||||||
// - Browsers
|
|
||||||
// - Node.js
|
|
||||||
// - Electron
|
|
||||||
// - Parcel
|
|
||||||
// - Webpack
|
|
||||||
|
|
||||||
if (typeof global !== "undefined") {
|
|
||||||
// global already exists
|
|
||||||
} else if (typeof window !== "undefined") {
|
|
||||||
window.global = window;
|
|
||||||
} else if (typeof self !== "undefined") {
|
|
||||||
self.global = self;
|
|
||||||
} else {
|
|
||||||
throw new Error("cannot export Go (neither global, window nor self is defined)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!global.require && typeof require !== "undefined") {
|
|
||||||
global.require = require;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!global.fs && global.require) {
|
|
||||||
const fs = require("fs");
|
|
||||||
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
|
|
||||||
global.fs = fs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const enosys = () => {
|
const enosys = () => {
|
||||||
const err = new Error("not implemented");
|
const err = new Error("not implemented");
|
||||||
err.code = "ENOSYS";
|
err.code = "ENOSYS";
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!global.fs) {
|
if (!globalThis.fs) {
|
||||||
let outputBuf = "";
|
let outputBuf = "";
|
||||||
global.fs = {
|
globalThis.fs = {
|
||||||
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -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");
|
||||||
if (nl != -1) {
|
if (nl != -1) {
|
||||||
console.log(outputBuf.substr(0, nl));
|
console.log(outputBuf.substring(0, nl));
|
||||||
outputBuf = outputBuf.substr(nl + 1);
|
outputBuf = outputBuf.substring(nl + 1);
|
||||||
}
|
}
|
||||||
return buf.length;
|
return buf.length;
|
||||||
},
|
},
|
||||||
|
@ -87,8 +58,8 @@
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.process) {
|
if (!globalThis.process) {
|
||||||
global.process = {
|
globalThis.process = {
|
||||||
getuid() { return -1; },
|
getuid() { return -1; },
|
||||||
getgid() { return -1; },
|
getgid() { return -1; },
|
||||||
geteuid() { return -1; },
|
geteuid() { return -1; },
|
||||||
|
@ -102,47 +73,26 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.crypto && global.require) {
|
if (!globalThis.crypto) {
|
||||||
const nodeCrypto = require("crypto");
|
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
|
||||||
global.crypto = {
|
|
||||||
getRandomValues(b) {
|
|
||||||
nodeCrypto.randomFillSync(b);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (!global.crypto) {
|
|
||||||
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.performance) {
|
if (!globalThis.performance) {
|
||||||
global.performance = {
|
throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
|
||||||
now() {
|
|
||||||
const [sec, nsec] = process.hrtime();
|
|
||||||
return sec * 1000 + nsec / 1000000;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.TextEncoder && global.require) {
|
if (!globalThis.TextEncoder) {
|
||||||
global.TextEncoder = require("util").TextEncoder;
|
throw new Error("globalThis.TextEncoder is not available, polyfill required");
|
||||||
}
|
|
||||||
if (!global.TextEncoder) {
|
|
||||||
throw new Error("global.TextEncoder is not available, polyfill required");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.TextDecoder && global.require) {
|
if (!globalThis.TextDecoder) {
|
||||||
global.TextDecoder = require("util").TextDecoder;
|
throw new Error("globalThis.TextDecoder is not available, polyfill required");
|
||||||
}
|
}
|
||||||
if (!global.TextDecoder) {
|
|
||||||
throw new Error("global.TextDecoder is not available, polyfill required");
|
|
||||||
}
|
|
||||||
|
|
||||||
// End of polyfills for common API.
|
|
||||||
|
|
||||||
const encoder = new TextEncoder("utf-8");
|
const encoder = new TextEncoder("utf-8");
|
||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
|
||||||
global.Go = class {
|
globalThis.Go = class {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.argv = ["js"];
|
this.argv = ["js"];
|
||||||
this.env = {};
|
this.env = {};
|
||||||
|
@ -163,6 +113,10 @@
|
||||||
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setInt32 = (addr, v) => {
|
||||||
|
this.mem.setUint32(addr + 0, v, true);
|
||||||
|
}
|
||||||
|
|
||||||
const getInt64 = (addr) => {
|
const getInt64 = (addr) => {
|
||||||
const low = this.mem.getUint32(addr + 0, true);
|
const low = this.mem.getUint32(addr + 0, true);
|
||||||
const high = this.mem.getInt32(addr + 4, true);
|
const high = this.mem.getInt32(addr + 4, true);
|
||||||
|
@ -256,7 +210,10 @@
|
||||||
|
|
||||||
const timeOrigin = Date.now() - performance.now();
|
const timeOrigin = Date.now() - performance.now();
|
||||||
this.importObject = {
|
this.importObject = {
|
||||||
go: {
|
_gotest: {
|
||||||
|
add: (a, b) => a + b,
|
||||||
|
},
|
||||||
|
gojs: {
|
||||||
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
||||||
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
||||||
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
||||||
|
@ -319,7 +276,7 @@
|
||||||
this._resume();
|
this._resume();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
getInt64(sp + 8),
|
||||||
));
|
));
|
||||||
this.mem.setInt32(sp + 16, id, true);
|
this.mem.setInt32(sp + 16, id, true);
|
||||||
},
|
},
|
||||||
|
@ -517,7 +474,7 @@
|
||||||
null,
|
null,
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
global,
|
globalThis,
|
||||||
this,
|
this,
|
||||||
];
|
];
|
||||||
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
||||||
|
@ -526,7 +483,7 @@
|
||||||
[null, 2],
|
[null, 2],
|
||||||
[true, 3],
|
[true, 3],
|
||||||
[false, 4],
|
[false, 4],
|
||||||
[global, 5],
|
[globalThis, 5],
|
||||||
[this, 6],
|
[this, 6],
|
||||||
]);
|
]);
|
||||||
this._idPool = []; // unused ids that have been garbage collected
|
this._idPool = []; // unused ids that have been garbage collected
|
||||||
|
@ -601,36 +558,4 @@
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
|
||||||
typeof module !== "undefined" &&
|
|
||||||
global.require &&
|
|
||||||
global.require.main === module &&
|
|
||||||
global.process &&
|
|
||||||
global.process.versions &&
|
|
||||||
!global.process.versions.electron
|
|
||||||
) {
|
|
||||||
if (process.argv.length < 3) {
|
|
||||||
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const go = new Go();
|
|
||||||
go.argv = process.argv.slice(2);
|
|
||||||
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
|
|
||||||
go.exit = process.exit;
|
|
||||||
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
|
||||||
process.on("exit", (code) => { // Node.js exits if no event handler is pending
|
|
||||||
if (code === 0 && !go.exited) {
|
|
||||||
// deadlock, make Go print error and stack traces
|
|
||||||
go._pendingEvent = { id: 0 };
|
|
||||||
go._resume();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return go.run(result.instance);
|
|
||||||
}).catch((err) => {
|
|
||||||
console.error(err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})();
|
})();
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module wasm-canvas-example
|
module wasm-canvas-example
|
||||||
|
|
||||||
go 1.17
|
go 1.21.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
|
github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
|
||||||
|
|
Reference in New Issue