Skip to content

Commit 4664f0b

Browse files
committed
loadWorkerViaBlob workaround for loading workers cross-origin
1 parent b94a019 commit 4664f0b

6 files changed

+67
-5
lines changed

box2d3-wasm/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/cmake-build*/
22
/build/*
3-
!/build/*.template.d.ts
3+
!/build/*.template.*
44
!/build/dist/
55
/build/dist/es/*
66
!/build/dist/es/entry.mjs

box2d3-wasm/build/Box2D.template.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export interface OurModuleOptions {
2121
// some browsers (e.g. Safari <16.4) cannot grow shared memory, so turning this off
2222
// could improve compatibility in that situation.
2323
sharedMemEnabled: boolean;
24+
// default: false
25+
// enable this if you're disallowed from loading workers cross-origin but allowed to download assets cross-origin.
26+
// we will fetch the worker asset, turn it into a blob and create a worker from the blob.
27+
loadWorkerViaBlob: boolean;
2428
}
2529

2630
// Combine known options with arbitrary properties
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
allocateUnusedWorkerViaBlob: async function() {
2+
if (!globalThis._workerPromise) {
3+
globalThis._workerPromise = (async function() {
4+
const response = await fetch(new URL("Box2D.deluxe.mjs", import.meta.url));
5+
const text = await response.text();
6+
const blob = new Blob([text], { type: 'application/javascript' });
7+
const workerUrl = URL.createObjectURL(blob);
8+
9+
setTimeout(() => {
10+
URL.revokeObjectURL(workerUrl);
11+
delete globalThis._workerPromise;
12+
}, 0);
13+
14+
return workerUrl;
15+
})();
16+
}
17+
18+
const workerUrl = await globalThis._workerPromise;
19+
20+
var workerOptions = {
21+
"type": "module",
22+
"workerData": "em-pthread",
23+
"name": "em-pthread-" + PThread.nextWorkerID
24+
};
25+
26+
const worker = new Worker(workerUrl, workerOptions);
27+
PThread.unusedWorkers.push(worker);
28+
return worker;
29+
},
30+
allocateUnusedWorker: async function() {
31+
if (loadWorkerViaBlob) {
32+
await PThread.allocateUnusedWorkerViaBlob();
33+
} else {
34+
PThread.allocateUnusedWorkerDirect();
35+
}
36+
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
moduleArg = {
2+
pthreadCount: globalThis.navigator?.hardwareConcurrency ?? 4,
3+
sharedMemEnabled: true,
4+
loadWorkerViaBlob: false,
5+
...moduleArg,
6+
};
7+
const {pthreadCount, sharedMemEnabled, loadWorkerViaBlob} = moduleArg;

box2d3-wasm/shell/1_build_wasm.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ case "$FLAVOUR" in
159159
# all of our text replacements are just for disabling/tuning deluxe functionality. compat flavour doesn't need any text replacements.
160160
;;
161161
deluxe)
162-
awk -f "$DIR/modify_emscripten_mjs.$FLAVOUR.awk" "$ES_PRECURSOR" > "$ES_FILE"
162+
awk -f "$DIR/modify_emscripten_mjs.$FLAVOUR.awk" \
163+
-v module_arg_template="$BUILD_DIR/module-arg.template.mjs" \
164+
-v allocate_unused_worker_template="$BUILD_DIR/allocate-unused-worker.template.mjs" \
165+
"$ES_PRECURSOR" > "$ES_FILE"
163166
;;
164167
esac
165168
awk -f "$DIR/modify_emscripten_dts.awk" -v template="$BUILD_DIR/Box2D.template.d.ts" "$ES_TSD_PRECURSOR" > "$ES_TSD"

box2d3-wasm/shell/modify_emscripten_mjs.deluxe.awk

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env awk -f
22

3-
BEGIN { found1=0; found2=0; found3=0; found4=0 }
3+
BEGIN { found1=0; found2=0; found3=0; found4=0; found5=0 }
44
!found1 && $0 ~ /^async function\(moduleArg = \{\}\) \{$/ {
55
print $0
6-
print " moduleArg = {pthreadCount: globalThis.navigator?.hardwareConcurrency ?? 4, sharedMemEnabled:true, ...moduleArg};"
7-
print " const {pthreadCount, sharedMemEnabled} = moduleArg;"
6+
while ((getline line < module_arg_template) > 0) {
7+
print line
8+
}
9+
close(module_arg_template)
810
found1=1
911
next
1012
}
@@ -25,5 +27,15 @@ BEGIN { found1=0; found2=0; found3=0; found4=0 }
2527
found4=1
2628
next
2729
}
30+
!found5 && /^[[:space:]]*allocateUnusedWorker\(\) \{$/ {
31+
while ((getline line < allocate_unused_worker_template) > 0) {
32+
print line
33+
}
34+
close(allocate_unused_worker_template)
35+
sub(/allocateUnusedWorker/, "allocateUnusedWorkerDirect")
36+
print
37+
found5=1
38+
next
39+
}
2840
{ print }
2941
END { exit !(found1 && found2 && found3 && found4) }

0 commit comments

Comments
 (0)