]> Cypherpunks.ru repositories - gostls13.git/commitdiff
misc/wasm: make wasm_exec more robust against uncommon environments
authorRichard Musiol <mail@richard-musiol.de>
Sat, 15 Aug 2020 19:15:35 +0000 (21:15 +0200)
committerRichard Musiol <neelance@gmail.com>
Tue, 25 Aug 2020 21:15:43 +0000 (21:15 +0000)
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>
misc/wasm/wasm_exec.js

index 8501ae7cd843ac443d46d3ff0f9fb864f5f11f1f..ef97c4e311dcc16119306b3bf0db0dfa7f500301 100644 (file)
@@ -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;
                }
        }
        }
 
        if (
+               typeof module !== "undefined" &&
                global.require &&
                global.require.main === module &&
                global.process &&