[quickjs-devel] Re: Suspending and Resuming the VM

  • From: Chris Cowan <agentme49@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Sat, 27 Jun 2020 14:55:09 -0700

WebAssembly in web browsers doesn't provide a direct way to suspend with an
active stack.

I think there's several possible strategies you can do:

A. You can use Emscripten's asyncify feature to rewrite the entire
WebAssembly binary so that it can be suspended and resumed during function
calls. This can impact performance throughout the whole thing though.
https://emscripten.org/docs/porting/asyncify.html

B. Make it so any JS functions running in quickjs that need to do an async
operation are also async, and make the getter functions in quickjs return
promises. This strategy has the benefit of making the cost of the operation
more obvious, and allows the sandboxed code to do other things in the
meantime.

C. If you're comfortable with depending on SharedArrayBuffer (which is
currently not supported in some browsers like Firefox while they figure out
spectre mitigations), then you can use it to accomplish synchronous message
passing to a web worker. This will allow you to communicate with the host
page within a synchronous function call.


On Thu, Jun 25, 2020, 2:47 PM Atif Ali <atif@xxxxxxxxxxxxx> wrote:

I am using QuickJS compiled to webassembly inside a web page to allow for
user-defined scripts.

Specifically, I have been defining the props so that I lazily add data
into the VM as needed by the code. This works great, however, if I am to
run the VM inside a web worker, then the getter/functions needs to work
asynchronously (i.e. return promises -- not desired as user code will be
laden with async/awaits then).

I am wondering if there is a way to suspend / pause the VM so that when a
getter is called? Then, I can pause the VM, go fetch the value
asynchronously from the main thread, then resume the VM once I have the
value in the web worker.

I suspect the suspend/pause functionality will also be really helpful for
adding debugging capabilities later.

- Atif

Other related posts: