Skip to content

Commit 47b155d

Browse files
authored
Extract emAudioWorkletPostMessage helper. NFC (#25666)
This avoids duplication and also means we can add consistent checks on the audio context that is passed in. Its also a code size win. We don't have a specific code size test that references these functions but I ran `browser.test_audio_worklet_post_function_closure` before and after this change and got these results: Before: 13297 out/test/test.js After: 12951 out/test/test.js
1 parent f69a8f6 commit 47b155d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/lib/libwebaudio.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,28 @@ var LibraryWebAudio = {
383383
srcNode.connect(dstNode.destination || dstNode, outputIndex, inputIndex);
384384
},
385385

386+
$emAudioWorkletPostFunction__internal: true,
387+
$emAudioWorkletPostFunction(audioContext, funcPtr, args) {
388+
#if ASSERTIONS
389+
assert(funcPtr);
390+
#endif
391+
#if ASSERTIONS || WEBAUDIO_DEBUG
392+
if (audioContext) emAudioExpectContext(audioContext, 'emAudioWorkletPostFunction');
393+
#endif
394+
// _wsc = "WaSm Call"
395+
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args});
396+
},
397+
386398
emscripten_current_thread_is_audio_worklet: () => ENVIRONMENT_IS_AUDIO_WORKLET,
387399

400+
emscripten_audio_worklet_post_function_v__deps: ['$emAudioWorkletPostFunction'],
388401
emscripten_audio_worklet_post_function_v: (audioContext, funcPtr) => {
389-
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args: [] }); // "WaSm Call"
402+
emAudioWorkletPostFunction(audioContext, funcPtr, []);
390403
},
391404

405+
$emscripten_audio_worklet_post_function_1__deps: ['$emAudioWorkletPostFunction'],
392406
$emscripten_audio_worklet_post_function_1: (audioContext, funcPtr, arg0) => {
393-
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args: [arg0] }); // "WaSm Call"
407+
emAudioWorkletPostFunction(audioContext, funcPtr, [arg0]);
394408
},
395409

396410
emscripten_audio_worklet_post_function_vi__deps: ['$emscripten_audio_worklet_post_function_1'],
@@ -403,8 +417,9 @@ var LibraryWebAudio = {
403417
emscripten_audio_worklet_post_function_1(audioContext, funcPtr, arg0)
404418
},
405419

420+
$emscripten_audio_worklet_post_function_2__deps: ['$emAudioWorkletPostFunction'],
406421
$emscripten_audio_worklet_post_function_2: (audioContext, funcPtr, arg0, arg1) => {
407-
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args: [arg0, arg1] }); // "WaSm Call"
422+
emAudioWorkletPostFunction(audioContext, funcPtr, [arg0, arg1]);
408423
},
409424

410425
emscripten_audio_worklet_post_function_vii__deps: ['$emscripten_audio_worklet_post_function_2'],
@@ -417,8 +432,9 @@ var LibraryWebAudio = {
417432
emscripten_audio_worklet_post_function_2(audioContext, funcPtr, arg0, arg1);
418433
},
419434

435+
$emscripten_audio_worklet_post_function_3__deps: ['$emAudioWorkletPostFunction'],
420436
$emscripten_audio_worklet_post_function_3: (audioContext, funcPtr, arg0, arg1, arg2) => {
421-
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args: [arg0, arg1, arg2] }); // "WaSm Call"
437+
emAudioWorkletPostFunction(audioContext, funcPtr, [arg0, arg1, arg2]);
422438
},
423439
emscripten_audio_worklet_post_function_viii__deps: ['$emscripten_audio_worklet_post_function_3'],
424440
emscripten_audio_worklet_post_function_viii: (audioContext, funcPtr, arg0, arg1, arg2) => {
@@ -429,16 +445,14 @@ var LibraryWebAudio = {
429445
emscripten_audio_worklet_post_function_3(audioContext, funcPtr, arg0, arg1, arg2);
430446
},
431447

432-
emscripten_audio_worklet_post_function_sig__deps: ['$readEmAsmArgs'],
448+
emscripten_audio_worklet_post_function_sig__deps: ['$readEmAsmArgs', '$emAudioWorkletPostFunction'],
433449
emscripten_audio_worklet_post_function_sig: (audioContext, funcPtr, sigPtr, varargs) => {
434450
#if ASSERTIONS
435-
assert(audioContext >= 0);
436-
assert(funcPtr);
437451
assert(sigPtr);
438452
assert(UTF8ToString(sigPtr)[0] != 'v', 'Do NOT specify the return argument in the signature string for a call to emscripten_audio_worklet_post_function_sig(), just pass the function arguments.');
439453
assert(varargs);
440454
#endif
441-
(audioContext ? EmAudio[audioContext].audioWorklet['port'] : port).postMessage({'_wsc': funcPtr, args: readEmAsmArgs(sigPtr, varargs) });
455+
emAudioWorkletPostFunction(audioContext, funcPtr, readEmAsmArgs(sigPtr, varargs));
442456
}
443457
};
444458

0 commit comments

Comments
 (0)