#1316·gopherjs

Support registering to globalThis

Author: anuraagaCreated Jun 12, 2024Updated Oct 25, 2024
LabelsenhancementNeedsHelp

I am using a gopherjs binary with QuickJS, which uses globalThis for its global. I wonder if it's possible to support globalThis in the prelude to work out of the box with QuickJS.

https://github.com/gopherjs/gopherjs/blob/cfa0783a7b031caf2220882339fa14d879ece43e/compiler/prelude/prelude.js#L17

javascript
if (typeof window !== "undefined") { /* web page */
    $global = window;
} else if (typeof self !== "undefined") { /* web worker */
    $global = self;
} else if (typeof global !== "undefined") { /* Node.js */
    $global = global;
    $global.require = require;
} else if (typeof globalThis !== "undefined") { /* QuickJS */
  $global = globalThis;
} else { /* others (e.g. Nashorn) */
    $global = this;
}

It's not difficult to shim though so not a big deal either way.

https://github.com/wasilibs/go-prettier/blob/main/buildtools/wasm/global.ts#L1 (globalThis as any).self = globalThis;