Skip to content

Commit 52e8c4b

Browse files
authored
Allow minification of pthread postMessage objects. NFC (emscripten-core#22555)
In the part using unquoted key here meant that closure would minify these key and this was a problem because the separate worker.js files would not then be able to interpret them. However since emscripten-core#21701 landed there is no longer separate worker.js file so closure can minify these keys and it will effect both sending and receiving code.
1 parent 3fb7caf commit 52e8c4b

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

src/library_pthread.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,16 @@ var LibraryPThread = {
229229
loadWasmModuleToWorker: (worker) => new Promise((onFinishedLoading) => {
230230
worker.onmessage = (e) => {
231231
var d = e['data'];
232-
var cmd = d['cmd'];
232+
var cmd = d.cmd;
233233

234234
// If this message is intended to a recipient that is not the main
235235
// thread, forward it to the target thread.
236-
if (d['targetThread'] && d['targetThread'] != _pthread_self()) {
237-
var targetWorker = PThread.pthreads[d['targetThread']];
236+
if (d.targetThread && d.targetThread != _pthread_self()) {
237+
var targetWorker = PThread.pthreads[d.targetThread];
238238
if (targetWorker) {
239-
targetWorker.postMessage(d, d['transferList']);
239+
targetWorker.postMessage(d, d.transferList);
240240
} else {
241-
err(`Internal error! Worker sent a message "${cmd}" to target pthread ${d['targetThread']}, but that thread no longer exists!`);
241+
err(`Internal error! Worker sent a message "${cmd}" to target pthread ${d.targetThread}, but that thread no longer exists!`);
242242
}
243243
return;
244244
}
@@ -248,10 +248,10 @@ var LibraryPThread = {
248248
} else if (cmd === 'spawnThread') {
249249
spawnThread(d);
250250
} else if (cmd === 'cleanupThread') {
251-
cleanupThread(d['thread']);
251+
cleanupThread(d.thread);
252252
#if MAIN_MODULE
253253
} else if (cmd === 'markAsFinished') {
254-
markAsFinished(d['thread']);
254+
markAsFinished(d.thread);
255255
#endif
256256
} else if (cmd === 'loaded') {
257257
worker.loaded = true;
@@ -266,13 +266,13 @@ var LibraryPThread = {
266266
#endif
267267
onFinishedLoading(worker);
268268
} else if (cmd === 'alert') {
269-
alert(`Thread ${d['threadId']}: ${d['text']}`);
269+
alert(`Thread ${d.threadId}: ${d.text}`);
270270
} else if (d.target === 'setimmediate') {
271271
// Worker wants to postMessage() to itself to implement setImmediate()
272272
// emulation.
273273
worker.postMessage(d);
274274
} else if (cmd === 'callHandler') {
275-
Module[d['handler']](...d['args']);
275+
Module[d.handler](...d.args);
276276
} else if (cmd) {
277277
// The received message looks like something that should be handled by this message
278278
// handler, (since there is a e.data.cmd field present), but is not one of the
@@ -333,28 +333,28 @@ var LibraryPThread = {
333333

334334
// Ask the new worker to load up the Emscripten-compiled page. This is a heavy operation.
335335
worker.postMessage({
336-
'cmd': 'load',
337-
'handlers': handlers,
336+
cmd: 'load',
337+
handlers: handlers,
338338
#if WASM2JS
339339
// the polyfill WebAssembly.Memory instance has function properties,
340340
// which will fail in postMessage, so just send a custom object with the
341341
// property we need, the buffer
342-
'wasmMemory': { 'buffer': wasmMemory.buffer },
342+
wasmMemory: { 'buffer': wasmMemory.buffer },
343343
#else // WASM2JS
344-
'wasmMemory': wasmMemory,
344+
wasmMemory,
345345
#endif // WASM2JS
346-
'wasmModule': wasmModule,
346+
wasmModule,
347347
#if LOAD_SOURCE_MAP
348-
'wasmSourceMap': wasmSourceMap,
348+
wasmSourceMap,
349349
#endif
350350
#if USE_OFFSET_CONVERTER
351-
'wasmOffsetConverter': wasmOffsetConverter,
351+
wasmOffsetConverter,
352352
#endif
353353
#if MAIN_MODULE
354-
'dynamicLibraries': dynamicLibraries,
354+
dynamicLibraries,
355355
// Share all modules that have been loaded so far. New workers
356356
// won't start running threads until these are all loaded.
357-
'sharedModules': sharedModules,
357+
sharedModules,
358358
#endif
359359
#if ASSERTIONS
360360
'workerID': worker.workerID,
@@ -503,7 +503,7 @@ var LibraryPThread = {
503503
// the onmessage handlers if the message was coming from valid worker.
504504
worker.onmessage = (e) => {
505505
#if ASSERTIONS
506-
var cmd = e['data']['cmd'];
506+
var cmd = e['data'].cmd;
507507
err(`received "${cmd}" command from terminated worker: ${worker.workerID}`);
508508
#endif
509509
};
@@ -519,7 +519,7 @@ var LibraryPThread = {
519519
dbg(`_emscripten_thread_cleanup: ${ptrToString(thread)}`)
520520
#endif
521521
if (!ENVIRONMENT_IS_PTHREAD) cleanupThread(thread);
522-
else postMessage({ 'cmd': 'cleanupThread', 'thread': thread });
522+
else postMessage({ cmd: 'cleanupThread', thread });
523523
},
524524

525525
_emscripten_thread_set_strongref: (thread) => {
@@ -618,10 +618,10 @@ var LibraryPThread = {
618618

619619
worker.pthread_ptr = threadParams.pthread_ptr;
620620
var msg = {
621-
'cmd': 'run',
622-
'start_routine': threadParams.startRoutine,
623-
'arg': threadParams.arg,
624-
'pthread_ptr': threadParams.pthread_ptr,
621+
cmd: 'run',
622+
start_routine: threadParams.startRoutine,
623+
arg: threadParams.arg,
624+
pthread_ptr: threadParams.pthread_ptr,
625625
};
626626
#if OFFSCREENCANVAS_SUPPORT
627627
// Note that we do not need to quote these names because they are only used
@@ -1110,7 +1110,7 @@ var LibraryPThread = {
11101110
// running, but remain around waiting to be joined. In this state they
11111111
// cannot run any more proxied work.
11121112
if (!ENVIRONMENT_IS_PTHREAD) markAsFinished(thread);
1113-
else postMessage({ 'cmd': 'markAsFinished', 'thread': thread });
1113+
else postMessage({ cmd: 'markAsFinished', thread });
11141114
},
11151115
11161116
$markAsFinished: (pthread_ptr) => {
@@ -1242,20 +1242,20 @@ var LibraryPThread = {
12421242
// new thread that has not had a chance to initialize itself and execute
12431243
// Atomics.waitAsync to prepare for the notification.
12441244
_emscripten_notify_mailbox_postmessage__deps: ['$checkMailbox'],
1245-
_emscripten_notify_mailbox_postmessage: (targetThreadId, currThreadId) => {
1246-
if (targetThreadId == currThreadId) {
1245+
_emscripten_notify_mailbox_postmessage: (targetThread, currThreadId) => {
1246+
if (targetThread == currThreadId) {
12471247
setTimeout(checkMailbox);
12481248
} else if (ENVIRONMENT_IS_PTHREAD) {
1249-
postMessage({'targetThread' : targetThreadId, 'cmd' : 'checkMailbox'});
1249+
postMessage({targetThread, cmd: 'checkMailbox'});
12501250
} else {
1251-
var worker = PThread.pthreads[targetThreadId];
1251+
var worker = PThread.pthreads[targetThread];
12521252
if (!worker) {
12531253
#if ASSERTIONS
1254-
err(`Cannot send message to thread with ID ${targetThreadId}, unknown thread ID!`);
1254+
err(`Cannot send message to thread with ID ${targetThread}, unknown thread ID!`);
12551255
#endif
12561256
return;
12571257
}
1258-
worker.postMessage({'cmd' : 'checkMailbox'});
1258+
worker.postMessage({cmd: 'checkMailbox'});
12591259
}
12601260
}
12611261
};

src/runtime_pthread.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ if (ENVIRONMENT_IS_PTHREAD) {
103103
try {
104104
var msgData = e['data'];
105105
//dbg('msgData: ' + Object.keys(msgData));
106-
var cmd = msgData['cmd'];
106+
var cmd = msgData.cmd;
107107
if (cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
108108
#if ASSERTIONS
109-
workerID = msgData['workerID'];
109+
workerID = msgData.workerID;
110110
#endif
111111
#if PTHREADS_DEBUG
112112
dbg('worker: loading module')
@@ -119,7 +119,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
119119
// And add a callback for when the runtime is initialized.
120120
self.startWorker = (instance) => {
121121
// Notify the main thread that this thread has loaded.
122-
postMessage({ 'cmd': 'loaded' });
122+
postMessage({ cmd: 'loaded' });
123123
// Process any messages that were queued before the thread was ready.
124124
for (let msg of messageQueue) {
125125
handleMessage(msg);
@@ -129,16 +129,16 @@ if (ENVIRONMENT_IS_PTHREAD) {
129129
};
130130

131131
#if MAIN_MODULE
132-
dynamicLibraries = msgData['dynamicLibraries'];
133-
sharedModules = msgData['sharedModules'];
132+
dynamicLibraries = msgData.dynamicLibraries;
133+
sharedModules = msgData.sharedModules;
134134
#if RUNTIME_DEBUG
135-
dbg(`worker: received ${Object.keys(msgData['sharedModules']).length} shared modules: ${Object.keys(msgData.sharedModules)}`);
135+
dbg(`worker: received ${Object.keys(msgData.sharedModules).length} shared modules: ${Object.keys(msgData.sharedModules)}`);
136136
#endif
137137
#endif
138138

139139
// Use `const` here to ensure that the variable is scoped only to
140140
// that iteration, allowing safe reference from a closure.
141-
for (const handler of msgData['handlers']) {
141+
for (const handler of msgData.handlers) {
142142
// The the main module has a handler for a certain even, but no
143143
// handler exists on the pthread worker, then proxy that handler
144144
// back to the main thread.
@@ -161,41 +161,41 @@ if (ENVIRONMENT_IS_PTHREAD) {
161161
#endif
162162
}
163163

164-
wasmMemory = msgData['wasmMemory'];
164+
wasmMemory = msgData.wasmMemory;
165165
updateMemoryViews();
166166

167167
#if LOAD_SOURCE_MAP
168-
wasmSourceMap = resetPrototype(WasmSourceMap, msgData['wasmSourceMap']);
168+
wasmSourceMap = resetPrototype(WasmSourceMap, msgData.wasmSourceMap);
169169
#endif
170170
#if USE_OFFSET_CONVERTER
171-
wasmOffsetConverter = resetPrototype(WasmOffsetConverter, msgData['wasmOffsetConverter']);
171+
wasmOffsetConverter = resetPrototype(WasmOffsetConverter, msgData.wasmOffsetConverter);
172172
#endif
173173

174174
#if MINIMAL_RUNTIME
175175
// Pass the shared Wasm module in the Module object for MINIMAL_RUNTIME.
176-
Module['wasm'] = msgData['wasmModule'];
176+
Module['wasm'] = msgData.wasmModule;
177177
loadModule();
178178
#else
179-
wasmPromiseResolve(msgData['wasmModule']);
179+
wasmPromiseResolve(msgData.wasmModule);
180180
#endif // MINIMAL_RUNTIME
181181
} else if (cmd === 'run') {
182182
#if ASSERTIONS
183-
assert(msgData['pthread_ptr']);
183+
assert(msgData.pthread_ptr);
184184
#endif
185185
// Call inside JS module to set up the stack frame for this pthread in JS module scope.
186186
// This needs to be the first thing that we do, as we cannot call to any C/C++ functions
187187
// until the thread stack is initialized.
188-
establishStackSpace(msgData['pthread_ptr']);
188+
establishStackSpace(msgData.pthread_ptr);
189189

190190
// Pass the thread address to wasm to store it for fast access.
191-
__emscripten_thread_init(msgData['pthread_ptr'], /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1, 0, 0);
191+
__emscripten_thread_init(msgData.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1, 0, 0);
192192

193193
PThread.receiveObjectTransfer(msgData);
194194
PThread.threadInitTLS();
195195

196196
// Await mailbox notifications with `Atomics.waitAsync` so we can start
197197
// using the fast `Atomics.notify` notification path.
198-
__emscripten_thread_mailbox_await(msgData['pthread_ptr']);
198+
__emscripten_thread_mailbox_await(msgData.pthread_ptr);
199199

200200
if (!initializedJS) {
201201
#if EMBIND
@@ -210,7 +210,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
210210
}
211211

212212
try {
213-
invokeEntryPoint(msgData['start_routine'], msgData['arg']);
213+
invokeEntryPoint(msgData.start_routine, msgData.arg);
214214
} catch(ex) {
215215
if (ex != 'unwind') {
216216
// The pthread "crashed". Do not call `_emscripten_thread_exit` (which
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5012
1+
4959
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10825
1+
10602

0 commit comments

Comments
 (0)