Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
51 changes: 51 additions & 0 deletions src/build_as_worker.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
})();
56 changes: 0 additions & 56 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down