| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
"use strict"; |
| 6 |
|
| 7 |
if (process.argv.length < 3) { |
| 8 |
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]"); |
| 9 |
process.exit(1); |
| 10 |
} |
| 11 |
|
| 12 |
globalThis.require = require; |
| 13 |
globalThis.fs = require("fs"); |
| 14 |
globalThis.path = require("path"); |
| 15 |
globalThis.TextEncoder = require("util").TextEncoder; |
| 16 |
globalThis.TextDecoder = require("util").TextDecoder; |
| 17 |
|
| 18 |
globalThis.performance ??= require("performance"); |
| 19 |
|
| 20 |
globalThis.crypto ??= require("crypto"); |
| 21 |
|
| 22 |
require("./wasm_exec"); |
| 23 |
|
| 24 |
const go = new Go(); |
| 25 |
go.argv = process.argv.slice(2); |
| 26 |
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env); |
| 27 |
go.exit = process.exit; |
| 28 |
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => { |
| 29 |
process.on("exit", (code) => { // Node.js exits if no event handler is pending |
| 30 |
if (code === 0 && !go.exited) { |
| 31 |
// deadlock, make Go print error and stack traces |
| 32 |
go._pendingEvent = { id: 0 }; |
| 33 |
go._resume(); |
| 34 |
} |
| 35 |
}); |
| 36 |
return go.run(result.instance); |
| 37 |
}).catch((err) => { |
| 38 |
console.error(err); |
| 39 |
process.exit(1); |
| 40 |
}); |