Skip to content

Commit 1422e34

Browse files
authored
Don't use quotes for worker_threads properties. NFC (emscripten-core#26321)
Instead, make closure compiler aware of the various properties. The reason or the codesize change is that I think the unquoted `isMainThread` was being minified incorrectly in some cases before this change.
1 parent 83e1f4c commit 1422e34

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

src/closure-externs/node-externs.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,25 @@ path.isAbsolute;
144144
path.posix;
145145

146146
crypto.randomFillSync;
147+
148+
/**
149+
* @suppress {duplicate}
150+
*/
151+
var worker_threads = {};
152+
153+
/**
154+
* @type {boolean}
155+
*/
156+
worker_threads.isMainThread;
157+
158+
/**
159+
* @type {function()}
160+
*/
161+
worker_threads.Worker;
162+
163+
/**
164+
* @type {Object}
165+
*/
166+
worker_threads.workerData;
167+
168+
worker_threads.parentPort;

src/pthread_esm_startup.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ if ({{{ nodeDetectionCode() }}}) {
1919
globalThis.self = globalThis;
2020
var worker_threads = await import('node:worker_threads');
2121
globalThis.Worker = worker_threads.Worker;
22-
var parentPort = worker_threads['parentPort'];
22+
var parentPort = worker_threads.parentPort;
2323
// Deno and Bun already have `postMessage` defined on the global scope and
2424
// deliver messages to `globalThis.onmessage`, so we must not duplicate that
2525
// behavior here if `postMessage` is already present.
2626
if (!globalThis.postMessage) {
2727
parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg }));
28-
globalThis.postMessage = (msg) => parentPort['postMessage'](msg);
28+
globalThis.postMessage = (msg) => parentPort.postMessage(msg);
2929
}
3030
}
3131
#endif

src/runtime_common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ var readyPromiseResolve, readyPromiseReject;
3939
if (ENVIRONMENT_IS_NODE && {{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) {
4040
// Create as web-worker-like an environment as we can.
4141
globalThis.self = globalThis;
42-
var parentPort = worker_threads['parentPort'];
42+
var parentPort = worker_threads.parentPort;
4343
// Deno and Bun already have `postMessage` defined on the global scope and
4444
// deliver messages to `globalThis.onmessage`, so we must not duplicate that
4545
// behavior here if `postMessage` is already present.
4646
if (!globalThis.postMessage) {
4747
parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg }));
48-
globalThis.postMessage = (msg) => parentPort['postMessage'](msg);
48+
globalThis.postMessage = (msg) => parentPort.postMessage(msg);
4949
}
5050
// Node.js Workers do not pass postMessage()s and uncaught exception events to the parent
5151
// thread necessarily in the same order where they were generated in sequential program order.

src/shell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ if (ENVIRONMENT_IS_NODE) {
124124
#if PTHREADS
125125
// Under node we set `workerData` to `em-pthread` to signal that the worker
126126
// is hosting a pthread.
127-
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && worker_threads['workerData'] == 'em-pthread'
127+
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && worker_threads.workerData == 'em-pthread'
128128
#endif // PTHREADS
129129
#if WASM_WORKERS
130-
ENVIRONMENT_IS_WASM_WORKER = ENVIRONMENT_IS_WORKER && worker_threads['workerData'] == 'em-ww'
130+
ENVIRONMENT_IS_WASM_WORKER = ENVIRONMENT_IS_WORKER && worker_threads.workerData == 'em-ww'
131131
#endif
132132
#endif // PTHREADS || WASM_WORKERS
133133
}

src/shell_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var ENVIRONMENT_IS_WASM_WORKER = {{{ wasmWorkerDetection() }}};
7474
if (ENVIRONMENT_IS_NODE) {
7575
// The way we signal to a worker that it is hosting a pthread is to construct
7676
// it with a specific name.
77-
ENVIRONMENT_IS_WASM_WORKER = worker_threads['workerData'] == 'em-ww'
77+
ENVIRONMENT_IS_WASM_WORKER = worker_threads.workerData == 'em-ww'
7878
}
7979
#endif
8080

@@ -156,7 +156,7 @@ if (ENVIRONMENT_IS_NODE) {
156156
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
157157
// Under node we set `workerData` to `em-pthread` to signal that the worker
158158
// is hosting a pthread.
159-
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && worker_threads['workerData'] == 'em-pthread'
159+
ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && worker_threads.workerData == 'em-pthread'
160160
#if !EXPORT_ES6
161161
_scriptName = __filename;
162162
#endif

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 7820,
3-
"a.out.js.gz": 3849,
2+
"a.out.js": 7894,
3+
"a.out.js.gz": 3862,
44
"a.out.nodebug.wasm": 19726,
55
"a.out.nodebug.wasm.gz": 9135,
6-
"total": 27546,
7-
"total_gz": 12984,
6+
"total": 27620,
7+
"total_gz": 12997,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 8243,
3-
"a.out.js.gz": 4056,
2+
"a.out.js": 8316,
3+
"a.out.js.gz": 4063,
44
"a.out.nodebug.wasm": 19727,
55
"a.out.nodebug.wasm.gz": 9137,
6-
"total": 27970,
7-
"total_gz": 13193,
6+
"total": 28043,
7+
"total_gz": 13200,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

0 commit comments

Comments
 (0)