1

I'm a WebAssembly newb just looking to get started, but I have a question I can't seem to find a reasonable answer to. I have an idea of how I would like to design this software, but I don't know if I'm asking the wrong thing of WebAssembly.

Do external JavaScript calls interrupt WebAssembly? Say I'm running a game loop for some visualization, and I want a HTML button bound to JavaScript to change some value within the currently executing WebAssembly context. Is that possible? Would it be more ideal to update values between each frame and only use WebAssembly for per-frame rendering?

The thing is, I really like the targetability of WebAssembly from other languages like C++, as I could never really get into either JavaScript or Typescript, and I'm also looking to build a more performant game-type application. However, since I was planning on making this a web game anyway, I wanted to see if I could use HTML + CSS for my UI elements instead of replicating a UI into WebAssembly, essentially turning it into a terrible canned Java applet.

1 Answer 1

1

Yes and no. The short answer is that the browser cannot interrupt long running WebAssembly code.

However, the browser also cannot interrupt long running JS code or deliver any events if you have long running code of any kind. WebAssembly and JS execute on the same execution stack, and in the web model you need to return back the event loop at reasonable intervals. The recommended approach here (for both JS and WebAssembly code) is to break up your game loop and have it run one frame at a time on a callback.

(There are some ways around this, such as using workers to run you long running code or compiler tricks such as binaryen's asynchify to break up your long running code).

Sign up to request clarification or add additional context in comments.

2 Comments

That's good to know. Freezing the browser is not something I had considered. It seems strange to me that the JavaScript model would be so inherently synchronous. I think my real answer came in finding Web Workers, which are in fact threaded, and while they still rely on an event loop, I can separate my WebAssembly execution from my UI elements and allow them to be entirely asynchronous with one another (including transferring frames back to the canvas).
@gajbooks Sure, if your wasm runs in a worker it won't cause any freezing, but it still follows the worker event loop afaik. There is no way to interrupt a script (except for termination of course), the best you could try is polling some shared memory. Also you might want to have a loo at the exceptions propsal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.