Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HelloWasm #6342

Merged
merged 1 commit into from
Oct 21, 2021
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
2 changes: 2 additions & 0 deletions apps/HelloWasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
js/*
build/
bin/
2 changes: 1 addition & 1 deletion apps/HelloWasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ clean:

$(BIN)/$(HL_TARGET)/run: $(BIN)/$(HL_TARGET)/reaction_diffusion_init.a $(BIN)/$(HL_TARGET)/reaction_diffusion_update.a $(BIN)/$(HL_TARGET)/reaction_diffusion_render.a $(BIN)/$(HL_TARGET)/runtime.a
mkdir -p $(@D)
$(CXX) $(HALIDE_DISTRIB_PATH)/tools/RunGenMain.cpp $(BIN)/$(HL_TARGET)/reaction_diffusion_*.registration.cpp $^ -o $@ -I $(HALIDE_DISTRIB_PATH)/include $(IMAGE_IO_FLAGS) $(LDFLAGS)
$(CXX) $(CXXFLAGS) $(HALIDE_DISTRIB_PATH)/tools/RunGenMain.cpp $(BIN)/$(HL_TARGET)/reaction_diffusion_*.registration.cpp $^ -o $@ -I $(HALIDE_DISTRIB_PATH)/include $(IMAGE_IO_FLAGS) $(LDFLAGS)

benchmark_native: $(BIN)/$(HL_TARGET)/run
$(BIN)/$(HL_TARGET)/run --benchmarks=all --benchmark_min_time=1 --name=reaction_diffusion_init --output_extents=[1024,1024] --parsable_output
Expand Down
7 changes: 7 additions & 0 deletions apps/HelloWasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
oninput="show_threads.value=threads.value">
</div>

<!-- Workaround for cross-origin isolation, see
https://developer.chrome.com/blog/enabling-shared-array-buffer/
https://dev.to/stefnotch/enabling-coop-coep-without-touching-the-server-2d3n
-->
<script type='module' src="./main.js">
</script>

<!-- Allow the C++ to access the canvas element -->
<script type='text/javascript'>
var Module = {
Expand Down
19 changes: 19 additions & 0 deletions apps/HelloWasm/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// from https://dev.to/stefnotch/enabling-coop-coep-without-touching-the-server-2d3n
// main.js
if ("serviceWorker" in navigator) {
// Register service worker
navigator.serviceWorker.register(new URL("./sw.js", import.meta.url)).then(
function (registration) {
console.log("COOP/COEP Service Worker registered", registration.scope);
// If the registration is active, but it's not controlling the page
if (registration.active && !navigator.serviceWorker.controller) {
window.location.reload();
}
},
function (err) {
console.log("COOP/COEP Service Worker failed to register", err);
}
);
} else {
console.warn("Cannot register a service worker");
}
39 changes: 39 additions & 0 deletions apps/HelloWasm/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// from https://dev.to/stefnotch/enabling-coop-coep-without-touching-the-server-2d3n
// sw.js
self.addEventListener("install", function () {
self.skipWaiting();
});

self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
});

self.addEventListener("fetch", function (event) {
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") {
return;
}

event.respondWith(
fetch(event.request)
.then(function (response) {
// It seems like we only need to set the headers for index.html
// If you want to be on the safe side, comment this out
// if (!response.url.includes("index.html")) return response;

const newHeaders = new Headers(response.headers);
newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp");
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin");

const moddedResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
});

return moddedResponse;
})
.catch(function (e) {
console.error(e);
})
);
});