Skip to content

Commit 300a596

Browse files
authored
Move embind globalThis polyfill to shared code. NFC (#25184)
This will allow us to use it from other places. Split out from #25172
1 parent c387bec commit 300a596

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/lib/libcore.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,41 @@ addToLibrary({
16121612
emscripten_asm_const_async_on_main_thread: (emAsmAddr, sigPtr, argbuf) => runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 0),
16131613
#endif
16141614

1615+
$emGlobalThis__internal: true,
1616+
#if SUPPORTS_GLOBALTHIS
1617+
$emGlobalThis: 'globalThis',
1618+
#else
1619+
$getGlobalThis__internal: true,
1620+
$getGlobalThis: () => {
1621+
if (typeof globalThis != 'undefined') {
1622+
return globalThis;
1623+
}
1624+
#if DYNAMIC_EXECUTION
1625+
return new Function('return this')();
1626+
#else
1627+
function testGlobal(obj) {
1628+
// Use __emGlobalThis as a test symbol to see if `obj` is indeed the
1629+
// global object.
1630+
obj['__emGlobalThis'] = obj;
1631+
var success = typeof __emGlobalThis == 'object' && obj['__emGlobalThis'] === obj;
1632+
delete obj['__emGlobalThis'];
1633+
return success;
1634+
}
1635+
if (typeof self != 'undefined' && testGlobal(self)) {
1636+
return self; // This works for both "window" and "self" (Web Workers) global objects
1637+
}
1638+
#if ENVIRONMENT_MAY_BE_NODE
1639+
if (typeof global != 'undefined' && testGlobal(global)) {
1640+
return global;
1641+
}
1642+
#endif
1643+
abort('unable to get global object.');
1644+
#endif // DYNAMIC_EXECUTION
1645+
},
1646+
$emGlobalThis__deps: ['$getGlobalThis'],
1647+
$emGlobalThis: 'getGlobalThis()',
1648+
#endif // SUPPORTS_GLOBALTHIS
1649+
16151650
#if !DECLARE_ASM_MODULE_EXPORTS
16161651
// When DECLARE_ASM_MODULE_EXPORTS is not set we export native symbols
16171652
// at runtime rather than statically in JS code.

src/lib/libemval.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,6 @@ var LibraryEmVal = {
122122
_emval_new_u16string__deps: ['$Emval'],
123123
_emval_new_u16string: (v) => Emval.toHandle(UTF16ToString(v)),
124124

125-
$emGlobalThis__internal: true,
126-
#if SUPPORTS_GLOBALTHIS
127-
$emGlobalThis: 'globalThis',
128-
#else
129-
$getGlobalThis__internal: true,
130-
$getGlobalThis: () => {
131-
if (typeof globalThis != 'undefined') {
132-
return globalThis;
133-
}
134-
#if DYNAMIC_EXECUTION
135-
return new Function('return this')();
136-
#else
137-
function testGlobal(obj) {
138-
// Use __emGlobalThis as a test symbol to see if `obj` is indeed the
139-
// global object.
140-
obj['__emGlobalThis'] = obj;
141-
var success = typeof __emGlobalThis == 'object' && obj['__emGlobalThis'] === obj;
142-
delete obj['__emGlobalThis'];
143-
return success;
144-
}
145-
if (typeof self != 'undefined' && testGlobal(self)) {
146-
return self; // This works for both "window" and "self" (Web Workers) global objects
147-
}
148-
#if ENVIRONMENT_MAY_BE_NODE
149-
if (typeof global != 'undefined' && testGlobal(global)) {
150-
return global;
151-
}
152-
#endif
153-
abort('unable to get global object.');
154-
#endif // DYNAMIC_EXECUTION
155-
},
156-
$emGlobalThis__deps: ['$getGlobalThis'],
157-
$emGlobalThis: 'getGlobalThis()',
158-
#endif // SUPPORTS_GLOBALTHIS
159-
160125
_emval_get_global__deps: ['$Emval', '$getStringOrSymbol', '$emGlobalThis'],
161126
_emval_get_global: (name) => {
162127
if (!name) {

0 commit comments

Comments
 (0)