diff --git a/eslint.config.mjs b/eslint.config.mjs index 9cdd1157b2c66..84f59b4d0173b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -28,6 +28,7 @@ export default [{ 'src/polyfill/', 'src/lib/', 'src/binaryDecode.js', + 'src/build_as_worker.js', 'src/minimum_runtime_check.js', 'src/runtime_*.js', 'src/shell*.js', diff --git a/src/build_as_worker.js b/src/build_as_worker.js new file mode 100644 index 0000000000000..51d6bd5053cd0 --- /dev/null +++ b/src/build_as_worker.js @@ -0,0 +1,51 @@ +var workerResponded = false, workerCallbackId = -1; + +(() => { + var messageBuffer = null, buffer = 0; + + function flushMessages() { + if (!messageBuffer) return; + if (runtimeInitialized) { + var temp = messageBuffer; + messageBuffer = null; + temp.forEach((message) => onmessage(message)); + } + } + + function messageResender() { + flushMessages(); + if (messageBuffer) { + setTimeout(messageResender, 100); // still more to do + } + } + + onmessage = (msg) => { + // if main has not yet been called (mem init file, other async things), buffer messages + if (!runtimeInitialized) { + if (!messageBuffer) { + messageBuffer = []; + setTimeout(messageResender, 100); + } + messageBuffer.push(msg); + return; + } + flushMessages(); + + var func = Module['_' + msg.data['funcName']]; + if (!func) abort('invalid worker function to call: ' + msg.data['funcName']); + var data = msg.data['data']; + if (data) { + if (!data.byteLength) data = new Uint8Array(data); + buffer = _realloc(buffer, data.length); + HEAPU8.set(data, buffer); + } + + workerResponded = false; + workerCallbackId = msg.data['callbackId']; + if (data) { + func(buffer, data.length); + } else { + func(0, 0); + } + } +})(); diff --git a/src/postamble.js b/src/postamble.js index 414ab920ff616..d0ecc3b16bed3 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -373,59 +373,3 @@ run(); #endif #endif // MODULARIZE != instance - -#if BUILD_AS_WORKER - -var workerResponded = false, workerCallbackId = -1; - -(() => { - var messageBuffer = null, buffer = 0; - - function flushMessages() { - if (!messageBuffer) return; - if (runtimeInitialized) { - var temp = messageBuffer; - messageBuffer = null; - temp.forEach((message) => onmessage(message)); - } - } - - function messageResender() { - flushMessages(); - if (messageBuffer) { - setTimeout(messageResender, 100); // still more to do - } - } - - onmessage = (msg) => { - // if main has not yet been called (mem init file, other async things), buffer messages - if (!runtimeInitialized) { - if (!messageBuffer) { - messageBuffer = []; - setTimeout(messageResender, 100); - } - messageBuffer.push(msg); - return; - } - flushMessages(); - - var func = Module['_' + msg.data['funcName']]; - if (!func) abort('invalid worker function to call: ' + msg.data['funcName']); - var data = msg.data['data']; - if (data) { - if (!data.byteLength) data = new Uint8Array(data); - buffer = _realloc(buffer, data.length); - HEAPU8.set(data, buffer); - } - - workerResponded = false; - workerCallbackId = msg.data['callbackId']; - if (data) { - func(buffer, data.length); - } else { - func(0, 0); - } - } -})(); - -#endif diff --git a/tools/link.py b/tools/link.py index 532c072591f63..20787b822bb74 100644 --- a/tools/link.py +++ b/tools/link.py @@ -1715,9 +1715,10 @@ def limit_incoming_module_api(): # need to be able to call these explicitly. settings.REQUIRED_EXPORTS += ['__funcs_on_exit'] - # The worker code in src/postamble.js depends on realloc if settings.BUILD_AS_WORKER: + # The worker code in src/build_as_worker.js depends on realloc settings.REQUIRED_EXPORTS += ['realloc'] + options.post_js.append(utils.path_from_root('src/build_as_worker.js')) if not settings.DISABLE_EXCEPTION_CATCHING: settings.REQUIRED_EXPORTS += [