@@ -10,9 +10,31 @@ declare global {
1010import Go from './go' ;
1111import instantiateWasm from './instantiateWasm' ;
1212
13- const g = global || window || self ;
14- if ( ! g . __go_wasm__ ) {
15- g . __go_wasm__ = { } ;
13+ let globalObject : typeof globalThis ;
14+
15+ // Check if globalThis is defined (modern environments)
16+ if ( typeof globalThis !== 'undefined' ) {
17+ globalObject = globalThis ;
18+ }
19+ // Check if window is defined (browser environment)
20+ else if ( typeof window !== 'undefined' ) {
21+ globalObject = window ;
22+ }
23+ // Check if self is defined (web workers)
24+ else if ( typeof self !== 'undefined' ) {
25+ globalObject = self ;
26+ }
27+ // Fallback to global (Node.js environment)
28+ else if ( typeof global !== 'undefined' ) {
29+ globalObject = global ;
30+ } else {
31+ throw new Error (
32+ 'cannot export Go (neither globalThis, global, window nor self is defined)'
33+ ) ;
34+ }
35+
36+ if ( ! globalObject . __go_wasm__ ) {
37+ globalObject . __go_wasm__ = { } ;
1638}
1739
1840/**
@@ -25,7 +47,7 @@ const maxTime = 3 * 1000;
2547/**
2648 * bridge is an easier way to refer to the Go WASM object.
2749 */
28- const bridge = g . __go_wasm__ ;
50+ const bridge = globalObject . __go_wasm__ ;
2951
3052/**
3153 * Wrapper is used by Go to run all Go functions in JS.
@@ -86,7 +108,7 @@ export default function () {
86108
87109 const wasm = await instantiateWasm ( go ) ;
88110
89- go . run ( wasm , g ) ;
111+ go . run ( wasm , globalObject ) ;
90112 }
91113
92114 init ( ) ;
0 commit comments