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
35 changes: 20 additions & 15 deletions src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@

{{{
const workerSupportsFutexWait = () => AUDIO_WORKLET ? "typeof AudioWorkletGlobalScope === 'undefined'" : '1';
const wasmWorkerJs = `
#if WASM_WORKERS == 2
_wasmWorkerBlobUrl
#elif MINIMAL_RUNTIME
Module['$wb']
#else
locateFile('${WASM_WORKER_FILE}')
#endif
`;
null;
}}}

Expand Down Expand Up @@ -170,21 +179,17 @@ if (ENVIRONMENT_IS_WASM_WORKER
return 0;
}
#endif
let worker = _wasmWorkers[_wasmWorkersID] = new Worker(
#if WASM_WORKERS == 2
// WASM_WORKERS=2 mode embeds .ww.js file contents into the main .js file
// as a Blob URL. (convenient, but not CSP security safe, since this is
// eval-like)
_wasmWorkerBlobUrl
#elif MINIMAL_RUNTIME
// MINIMAL_RUNTIME has a structure where the .ww.js file is loaded from
// the main HTML file in parallel to all other files for best performance
Module['$wb'] // $wb="Wasm worker Blob", abbreviated since not DCEable
#else
// default runtime loads the .ww.js file on demand.
locateFile('{{{ WASM_WORKER_FILE }}}')
#endif
);
let worker;
#if TRUSTED_TYPES
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy(
'emscripten#workerPolicy1', { createScriptURL: (ignored) => {{{ wasmWorkerJs }}}}
);
worker = _wasmWorkers[_wasmWorkersID] = new Worker(p.createScriptURL('ignored'));
} else
#endif
worker = _wasmWorkers[_wasmWorkersID] = new Worker({{{ wasmWorkerJs }}});
// Craft the Module object for the Wasm Worker scope:
worker.postMessage({
// Signal with a non-zero value that this Worker will be a Wasm Worker,
Expand Down
7 changes: 7 additions & 0 deletions src/wasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ if (ENVIRONMENT_IS_NODE) {
#endif
#if !MINIMAL_RUNTIME
d['instantiateWasm'] = (info, receiveInstance) => { var instance = new WebAssembly.Instance(d['wasm'], info); return receiveInstance(instance, d['wasm']); }
#endif
#if TRUSTED_TYPES
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy('emscripten#scriptPolicy1', { createScriptURL: (ignored) => d.js });
importScripts(p.createScriptURL('ignored'));
} else
#endif
importScripts(d.js);
#if MODULARIZE
Expand Down
24 changes: 12 additions & 12 deletions test/code_size/hello_wasm_worker_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ var b = Module, c = !!b.$ww, e = b.mem || new WebAssembly.Memory({
d && h.get(d)(...a.x);
}, l = a => {
g.push(a);
}, m = {}, n = 1, p, q;
}, n = {}, p = 1, q, r;

c && (m[0] = this, addEventListener("message", l));
c && (n[0] = this, addEventListener("message", l));

WebAssembly.instantiate(b.wasm, {
a: {
b: (a, d) => {
let r = m[n] = new Worker(b.$wb);
r.postMessage({
$ww: n,
let m = n[p] = new Worker(b.$wb);
m.postMessage({
$ww: p,
wasm: b.wasm,
js: b.js,
mem: e,
sb: a,
sz: d
});
r.onmessage = k;
return n++;
m.onmessage = k;
return p++;
},
c: () => !1,
d: (a, d) => {
m[a].postMessage({
n[a].postMessage({
_wsc: d,
x: []
});
Expand All @@ -41,10 +41,10 @@ WebAssembly.instantiate(b.wasm, {
}
}).then((a => {
a = a.instance.exports;
p = a.g;
q = a.i;
q = a.g;
r = a.i;
h = a.h;
c ? (a = b, q(a.sb, a.sz), removeEventListener("message", l), g = g.forEach(k),
c ? (a = b, r(a.sb, a.sz), removeEventListener("message", l), g = g.forEach(k),
addEventListener("message", k)) : a.f();
c || p();
c || q();
}));
8 changes: 6 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14411,8 +14411,12 @@ def test_debug_opt_warning(self, should_fail, args):
else:
self.run_process([EMCC, test_file('hello_world.c'), '-Werror'] + args)

def test_wasm_worker_hello(self):
self.do_runf('wasm_worker/hello_wasm_worker.c', emcc_args=['-sWASM_WORKERS'])
@parameterized({
'': [[]],
'trusted': [['-sTRUSTED_TYPES']]
})
def test_wasm_worker_hello(self, args):
self.do_runf('wasm_worker/hello_wasm_worker.c', emcc_args=['-sWASM_WORKERS'] + args)

def test_wasm_worker_terminate(self):
self.do_runf('wasm_worker/terminate_wasm_worker.c', emcc_args=['-sWASM_WORKERS'])
Expand Down