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

Proposal: Use web worker to run wasm binaries #612

Closed
bummoblizard opened this issue Oct 18, 2022 · 8 comments
Closed

Proposal: Use web worker to run wasm binaries #612

bummoblizard opened this issue Oct 18, 2022 · 8 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@bummoblizard
Copy link
Member

bummoblizard commented Oct 18, 2022

Background

Currently there is no way to terminate a running C / C++ program in WKWebView as wasm in web is single-threaded and blocking. Calling WKWebView.reload() or releasing the reference to the existing web view doesn't do anything.

Proposal

I propose to run binaries in web workers and use SharedArrayBuffer and worker.postMessage() to facilitate sync / async communication between native code and the web worker.

Caveat

Due to a security requirement for using SharedArrayBuffer, the following headers from the web server are required to use the API. As we are currently loading the resources using WKWebView.loadFileURL, there doesn't seem to be a way to set custom headers. From what I've tested, the api is blocked by default when loading a local file. We probably need to serve the files with a local web server (GCDWebServer) instead.

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
@bummoblizard bummoblizard added enhancement New feature or request help wanted Extra attention is needed labels Oct 18, 2022
@bummoblizard bummoblizard pinned this issue Oct 18, 2022
@hg0428
Copy link

hg0428 commented Oct 20, 2022

I would... But I don't know Swift

@bummoblizard
Copy link
Member Author

I would... But I don't know Swift

This is mainly a JavaScript implementation.

@hg0428
Copy link

hg0428 commented Oct 20, 2022

I would... But I don't know Swift

This is mainly a JavaScript implementation.

Oh, well, I know JS, so I'll see how much I can help out.

@luni-moon
Copy link

luni-moon commented Oct 21, 2022 via email

@hg0428
Copy link

hg0428 commented Oct 21, 2022

The app is an iOS app, therefore, it does require Swift, not JS to add. You
can’t make an iOS/iPadOS app effectively, unless it’s a PWA.

On Thu, Oct 20, 2022 at 19:48, hg0428 @.***> wrote:

I would... But I don't know Swift

This is mainly a JavaScript implementation.

Oh, well, I know JS, so I'll see how much I can help out.


Reply to this email directly, view it on GitHub
#612 (comment),
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ANI5RBNHG7Y23OVJHV3BTUTWEHK6LANCNFSM6AAAAAARIBEPTQ
.
You are receiving this because you are subscribed to this thread.Message
ID: @.***>

--
Best Wishes,
Luke Batema (JavaScript and Front End Developer, Creator of cubelang)

Well then, maybe I can't help much.

@143mailliw
Copy link

The app is an iOS app, therefore, it does require Swift, not JS to add. You
can’t make an iOS/iPadOS app effectively, unless it’s a PWA.
On Thu, Oct 20, 2022 at 19:48, hg0428 @.***> wrote:

I would... But I don't know Swift
This is mainly a JavaScript implementation.
Oh, well, I know JS, so I'll see how much I can help out.

Reply to this email directly, view it on GitHub
#612 (comment),
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ANI5RBNHG7Y23OVJHV3BTUTWEHK6LANCNFSM6AAAAAARIBEPTQ
.
You are receiving this because you are subscribed to this thread.Message
ID: @.***>

--
Best Wishes,
Luke Batema (JavaScript and Front End Developer, Creator of cubelang)

Well then, maybe I can't help much.

You could still help. This issue is about using a JS WebWorker and a JS SharedArrayBuffer to move WebAssembly into another thread, as I understand, and the only Swift code required is to move everything to a web server instead of just serving a local file.

@bummoblizard
Copy link
Member Author

Alternative way to terminate a running wasm: https://github.com/ewasm/wasm-metering

@bummoblizard
Copy link
Member Author

bummoblizard commented Aug 16, 2023

While working on adding stdc++.h, I noticed this in signal.h:

#ifndef _WASI_EMULATED_SIGNAL
#error "wasm lacks signal support; to enable minimal signal emulation, \
compile with -D_WASI_EMULATED_SIGNAL and link with -lwasi-emulated-signal"
#else
#ifndef _SIGNAL_H
#define _SIGNAL_H

TODO: Investigate if emulated signal can be used to terminate programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Development

No branches or pull requests

4 participants