2021-09-03 13:58:57 -06:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
if (process.argv.length < 3) {
|
|
|
|
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.require = require;
|
|
|
|
globalThis.fs = require("fs");
|
2024-08-09 11:43:11 -06:00
|
|
|
globalThis.path = require("path");
|
2021-09-03 13:58:57 -06:00
|
|
|
globalThis.TextEncoder = require("util").TextEncoder;
|
|
|
|
globalThis.TextDecoder = require("util").TextDecoder;
|
|
|
|
|
2023-01-27 23:48:57 -07:00
|
|
|
globalThis.performance ??= require("performance");
|
2021-09-03 13:58:57 -06:00
|
|
|
|
2023-01-27 23:49:46 -07:00
|
|
|
globalThis.crypto ??= require("crypto");
|
2021-09-03 13:58:57 -06:00
|
|
|
|
|
|
|
require("./wasm_exec");
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|