Skip to content

Commit a00be61

Browse files
authored
Replace emval_get_global. NFC (#25169)
- Use a constant `emGlobalThis` instead of a function call - Calculate the constant once at startup time if its needed. The plan is to then use this library symbol whenever we need access to globalThis.
1 parent 2bc5497 commit a00be61

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/lib/libemval.js

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

125+
$emGlobalThis__internal: true,
125126
#if SUPPORTS_GLOBALTHIS
126-
$emval_get_global: () => globalThis,
127-
#elif !DYNAMIC_EXECUTION
128-
$emval_get_global: () => {
129-
if (typeof globalThis == 'object') {
127+
$emGlobalThis: 'globalThis',
128+
#else
129+
$getGlobalThis__internal: true,
130+
$getGlobalThis: () => {
131+
if (typeof globalThis != 'undefined') {
130132
return globalThis;
131133
}
134+
#if DYNAMIC_EXECUTION
135+
return new Function('return this')();
136+
#else
132137
function testGlobal(obj) {
133-
obj['$$$embind_global$$$'] = obj;
134-
var success = typeof $$$embind_global$$$ == 'object' && obj['$$$embind_global$$$'] == obj;
135-
if (!success) {
136-
delete obj['$$$embind_global$$$'];
137-
}
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'];
138143
return success;
139144
}
140-
if (typeof $$$embind_global$$$ == 'object') {
141-
return $$$embind_global$$$;
145+
if (typeof self != 'undefined' && testGlobal(self)) {
146+
return self; // This works for both "window" and "self" (Web Workers) global objects
142147
}
143-
if (typeof global == 'object' && testGlobal(global)) {
144-
$$$embind_global$$$ = global;
145-
} else if (typeof self == 'object' && testGlobal(self)) {
146-
$$$embind_global$$$ = self; // This works for both "window" and "self" (Web Workers) global objects
147-
}
148-
if (typeof $$$embind_global$$$ == 'object') {
149-
return $$$embind_global$$$;
150-
}
151-
throw Error('unable to get global object.');
152-
},
153-
#else
154-
$emval_get_global: () => {
155-
if (typeof globalThis == 'object') {
156-
return globalThis;
148+
#if ENVIRONMENT_MAY_BE_NODE
149+
if (typeof global != 'undefined' && testGlobal(global)) {
150+
return global;
157151
}
158-
return new Function('return this')();
159-
},
160152
#endif
161-
_emval_get_global__deps: ['$Emval', '$getStringOrSymbol', '$emval_get_global'],
153+
abort('unable to get global object.');
154+
#endif // DYNAMIC_EXECUTION
155+
},
156+
$emGlobalThis__deps: ['$getGlobalThis'],
157+
$emGlobalThis: 'getGlobalThis()',
158+
#endif // SUPPORTS_GLOBALTHIS
159+
160+
_emval_get_global__deps: ['$Emval', '$getStringOrSymbol', '$emGlobalThis'],
162161
_emval_get_global: (name) => {
163-
if (name===0) {
164-
return Emval.toHandle(emval_get_global());
165-
} else {
166-
name = getStringOrSymbol(name);
167-
return Emval.toHandle(emval_get_global()[name]);
162+
if (!name) {
163+
return Emval.toHandle(emGlobalThis);
168164
}
165+
name = getStringOrSymbol(name);
166+
return Emval.toHandle(emGlobalThis[name]);
169167
},
170168

171169
_emval_get_module_property__deps: ['$getStringOrSymbol', '$Emval'],
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 552,
33
"a.html.gz": 373,
4-
"a.js": 5356,
5-
"a.js.gz": 2526,
4+
"a.js": 5350,
5+
"a.js.gz": 2527,
66
"a.wasm": 5852,
77
"a.wasm.gz": 2743,
8-
"total": 11760,
9-
"total_gz": 5642
8+
"total": 11754,
9+
"total_gz": 5643
1010
}

test/test_core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7505,10 +7505,14 @@ def test2():
75057505
do_test(test2, level=2, prefix='hello_libcxx')
75067506

75077507
@parameterized({
7508-
'': (['-lembind', '-sDYNAMIC_EXECUTION=0'],),
7508+
'': (['-lembind'],),
75097509
'flag': (['--bind'],),
7510+
'legacy': (['--bind', '-sLEGACY_VM_SUPPORT'],),
7511+
'no_dynamic': (['--bind', '-sDYNAMIC_EXECUTION=0', '-sLEGACY_VM_SUPPORT'],),
75107512
})
75117513
def test_embind_val_basics(self, args):
7514+
if '-sLEGACY_VM_SUPPORT' in args and (self.get_setting('MODULARIZE') == 'instance' or self.get_setting('WASM_ESM_INTEGRATION')):
7515+
self.skipTest('LEGACY_VM_SUPPORT is not compatible with EXPORT_ES6')
75127516
self.maybe_closure()
75137517
self.do_run_in_out_file_test('embind/test_embind_val_basics.cpp', cflags=args)
75147518

0 commit comments

Comments
 (0)