Skip to content

Commit

Permalink
misc/wasm: make wasm_exec more robust against uncommon environments
Browse files Browse the repository at this point in the history
JavaScript environments are quite unpredictable because bundlers add
mocks for compatibility and libraries can polute the global namespace.
Detect more of such situations:

- Add check that require("fs") returns an object.
- Fix check that require("fs") returns an non-empty object.
- Add check that "module" is defined.

Fixes golang#40730

Change-Id: I2ce65fc7db64bbbb0b60eec79a4cfe5c3fec99c0
Reviewed-on: https://go-review.googlesource.com/c/go/+/248758
Run-TryBot: Richard Musiol <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Cherry Zhang <[email protected]>
  • Loading branch information
neelance authored and Richard Musiol committed Aug 25, 2020
1 parent 8381408 commit 758ac37
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion misc/wasm/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// - Node.js
// - Electron
// - Parcel
// - Webpack

if (typeof global !== "undefined") {
// global already exists
Expand All @@ -28,7 +29,7 @@

if (!global.fs && global.require) {
const fs = require("fs");
if (Object.keys(fs) !== 0) {
if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) {
global.fs = fs;
}
}
Expand Down Expand Up @@ -556,6 +557,7 @@
}

if (
typeof module !== "undefined" &&
global.require &&
global.require.main === module &&
global.process &&
Expand Down

0 comments on commit 758ac37

Please sign in to comment.