Skip to content

Commit ef6746a

Browse files
committed
Additional nil checks around NodeJS APIs.
These changes make gopherjs work correctly in environments with fake `$global.process` object, which may be missing some of the properties. Specifically, when Go's `wasm_exec.js` is used, it fakes that object, but only partially. So if a gopherjs program was loaded in the same context as a Go wasm program, gopherjs would throw an undefined property exception.
1 parent 59aa136 commit ef6746a

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

compiler/natives/src/runtime/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func init() {
8080

8181
func GOROOT() string {
8282
process := js.Global.Get("process")
83-
if process == js.Undefined {
83+
if process == js.Undefined || process.Get("env") == js.Undefined {
8484
return "/"
8585
}
8686
if v := process.Get("env").Get("GOPHERJS_GOROOT"); v != js.Undefined && v.String() != "" {

compiler/natives/src/syscall/syscall_js_wasm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ func runtime_envs() []string {
1010
return nil
1111
}
1212
jsEnv := process.Get("env")
13+
if jsEnv.IsUndefined() {
14+
return nil
15+
}
1316
envkeys := js.Global().Get("Object").Call("keys", jsEnv)
1417
envs := make([]string, envkeys.Length())
1518
for i := 0; i < envkeys.Length(); i++ {

0 commit comments

Comments
 (0)