@@ 2,54 2,25 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-(() => {
- // 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;
- }
- }
+"use strict";
+(() => {
const enosys = () => {
const err = new Error("not implemented");
err.code = "ENOSYS";
return err;
};
- if (!global.fs) {
+ if (!globalThis.fs) {
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
writeSync(fd, buf) {
outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n");
if (nl != -1) {
- console.log(outputBuf.substr(0, nl));
- outputBuf = outputBuf.substr(nl + 1);
+ console.log(outputBuf.substring(0, nl));
+ outputBuf = outputBuf.substring(nl + 1);
}
return buf.length;
},
@@ 87,8 58,8 @@
};
}
- if (!global.process) {
- global.process = {
+ if (!globalThis.process) {
+ globalThis.process = {
getuid() { return -1; },
getgid() { return -1; },
geteuid() { return -1; },
@@ 102,47 73,26 @@
}
}
- if (!global.crypto && global.require) {
- const nodeCrypto = require("crypto");
- global.crypto = {
- getRandomValues(b) {
- nodeCrypto.randomFillSync(b);
- },
- };
- }
- if (!global.crypto) {
- throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
+ if (!globalThis.crypto) {
+ throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
}
- if (!global.performance) {
- global.performance = {
- now() {
- const [sec, nsec] = process.hrtime();
- return sec * 1000 + nsec / 1000000;
- },
- };
+ if (!globalThis.performance) {
+ throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
}
- if (!global.TextEncoder && global.require) {
- global.TextEncoder = require("util").TextEncoder;
- }
- if (!global.TextEncoder) {
- throw new Error("global.TextEncoder is not available, polyfill required");
+ if (!globalThis.TextEncoder) {
+ throw new Error("globalThis.TextEncoder is not available, polyfill required");
}
- if (!global.TextDecoder && global.require) {
- global.TextDecoder = require("util").TextDecoder;
+ if (!globalThis.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 decoder = new TextDecoder("utf-8");
- global.Go = class {
+ globalThis.Go = class {
constructor() {
this.argv = ["js"];
this.env = {};
@@ 296,8 246,8 @@
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
},
- // func walltime1() (sec int64, nsec int32)
- "runtime.walltime1": (sp) => {
+ // func walltime() (sec int64, nsec int32)
+ "runtime.walltime": (sp) => {
sp >>>= 0;
const msec = (new Date).getTime();
setInt64(sp + 8, msec / 1000);
@@ 401,6 351,7 @@
storeValue(sp + 56, result);
this.mem.setUint8(sp + 64, 1);
} catch (err) {
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 56, err);
this.mem.setUint8(sp + 64, 0);
}
@@ 417,6 368,7 @@
storeValue(sp + 40, result);
this.mem.setUint8(sp + 48, 1);
} catch (err) {
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, err);
this.mem.setUint8(sp + 48, 0);
}
@@ 433,6 385,7 @@
storeValue(sp + 40, result);
this.mem.setUint8(sp + 48, 1);
} catch (err) {
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
storeValue(sp + 40, err);
this.mem.setUint8(sp + 48, 0);
}
@@ 514,7 467,7 @@
null,
true,
false,
- global,
+ globalThis,
this,
];
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
@@ 523,7 476,7 @@
[null, 2],
[true, 3],
[false, 4],
- [global, 5],
+ [globalThis, 5],
[this, 6],
]);
this._idPool = []; // unused ids that have been garbage collected
@@ 564,6 517,13 @@
offset += 8;
});
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
+ const wasmMinDataAddr = 4096 + 8192;
+ if (offset >= wasmMinDataAddr) {
+ throw new Error("total length of command line and environment variables exceeds limit");
+ }
+
this._inst.exports.run(argc, argv);
if (this.exited) {
this._resolveExitPromise();
@@ 591,36 551,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);
- });
- }
-})();
+})();<
\ No newline at end of file