wasm_exec_node.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. "use strict";
  5. if (process.argv.length < 3) {
  6. console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
  7. process.exit(1);
  8. }
  9. globalThis.require = require;
  10. globalThis.fs = require("fs");
  11. globalThis.TextEncoder = require("util").TextEncoder;
  12. globalThis.TextDecoder = require("util").TextDecoder;
  13. globalThis.performance ??= require("performance");
  14. globalThis.crypto ??= require("crypto");
  15. require("./wasm_exec");
  16. const go = new Go();
  17. go.argv = process.argv.slice(2);
  18. go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
  19. go.exit = process.exit;
  20. WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
  21. process.on("exit", (code) => { // Node.js exits if no event handler is pending
  22. if (code === 0 && !go.exited) {
  23. // deadlock, make Go print error and stack traces
  24. go._pendingEvent = { id: 0 };
  25. go._resume();
  26. }
  27. });
  28. return go.run(result.instance);
  29. }).catch((err) => {
  30. console.error(err);
  31. process.exit(1);
  32. });