]> Cypherpunks.ru repositories - gostls13.git/blob - misc/wasm/wasm_exec_node.js
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / misc / wasm / wasm_exec_node.js
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.TextEncoder = require("util").TextEncoder;
15 globalThis.TextDecoder = require("util").TextDecoder;
16
17 globalThis.performance ??= require("performance");
18
19 globalThis.crypto ??= require("crypto");
20
21 require("./wasm_exec");
22
23 const go = new Go();
24 go.argv = process.argv.slice(2);
25 go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
26 go.exit = process.exit;
27 WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
28         process.on("exit", (code) => { // Node.js exits if no event handler is pending
29                 if (code === 0 && !go.exited) {
30                         // deadlock, make Go print error and stack traces
31                         go._pendingEvent = { id: 0 };
32                         go._resume();
33                 }
34         });
35         return go.run(result.instance);
36 }).catch((err) => {
37         console.error(err);
38         process.exit(1);
39 });