diff options
author | Richard Musiol <mail@richard-musiol.de> | 2020-08-15 21:15:35 +0200 |
---|---|---|
committer | Richard Musiol <neelance@gmail.com> | 2020-08-25 21:15:43 +0000 |
commit | 758ac371ab930734053ed226ac62681e62ab8eea (patch) | |
tree | dab99467fb308676867a7b095b6e6b43fca01acc /misc | |
parent | 8381408048018aa2b6eec874f3161b4641191522 (diff) | |
download | go-758ac371ab930734053ed226ac62681e62ab8eea.tar.gz go-758ac371ab930734053ed226ac62681e62ab8eea.zip |
misc/wasm: make wasm_exec more robust against uncommon environments
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 #40730
Change-Id: I2ce65fc7db64bbbb0b60eec79a4cfe5c3fec99c0
Reviewed-on: https://go-review.googlesource.com/c/go/+/248758
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Diffstat (limited to 'misc')
-rw-r--r-- | misc/wasm/wasm_exec.js | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 8501ae7cd8..ef97c4e311 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -11,6 +11,7 @@ // - Node.js // - Electron // - Parcel + // - Webpack if (typeof global !== "undefined") { // global already exists @@ -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; } } @@ -556,6 +557,7 @@ } if ( + typeof module !== "undefined" && global.require && global.require.main === module && global.process && |