-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc/wasm: expect environment to provide polyfills
The list of environments to support with wasm_exec.js was becoming too large to maintain. With this change, wasm_exec.js expects that the environment provides all necessary polyfills. The standardized "globalThis" is used for accessing the environment. wasm_exec.js now only provides stub fallbacks for globalThis.fs and globalThis.process. All code specific to Node.js is now in a separate file. Change-Id: I076febbd94d4d7845260faad972f450f74a7b983 Reviewed-on: https://go-review.googlesource.com/c/go/+/347353 Trust: Richard Musiol <neelance@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
- Loading branch information
Showing
3 changed files
with
66 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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"); | ||
globalThis.TextEncoder = require("util").TextEncoder; | ||
globalThis.TextDecoder = require("util").TextDecoder; | ||
|
||
globalThis.performance = { | ||
now() { | ||
const [sec, nsec] = process.hrtime(); | ||
return sec * 1000 + nsec / 1000000; | ||
}, | ||
}; | ||
|
||
const crypto = require("crypto"); | ||
globalThis.crypto = { | ||
getRandomValues(b) { | ||
crypto.randomFillSync(b); | ||
}, | ||
}; | ||
|
||
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); | ||
}); |