-
Notifications
You must be signed in to change notification settings - Fork 694
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
Ability to pause
wasm execution and call back JavaScript (async JS support)
#1294
Comments
pause
wasm execution on call back JavaScript (async JS support)pause
wasm execution and call back JavaScript (async JS support)
@amirouche I'm not sure what you mean by Asyncify only supporting "top-level invocation"? If I understood the rest correctly then Asyncify does support that: pause and resume wasm at any location, and JS can be given a callback that will resume the wasm from where it was paused. Here is an example: https://emscripten.org/docs/porting/asyncify.html#making-async-web-apis-behave-as-if-they-were-synchronous Asyncify does have overhead, though (something like 50%), so it would be good to have native wasm support for stack switching/coroutines, etc. But that would just be an optimization. Happy to help with this in your Scheme compiler if you want! |
Yes. I will experiment with asyncify and my trampoline and report back. |
I am working on coroutine in web assembly level. |
Coroutine support is orthogonal to the ability to pause / resume the whole wasm program. |
Pause/resume is enough to implement coroutines, I think? For example Julia's wasm port implements coroutines using Asyncify's pause/resume. |
indeed, but (actually) :
|
@pmp-p I'm not sure I understand. How does asyncify interact with the issues you mention? Are there bugs filed on them? |
@kripken the context is porting VM over wasm VM @amirouche is on a scheme flavour and i'm on micropython/cpython : apparently related to bugs filed : for setjmp need to drop dlopen and so FFI
asyncify (behaviour) is hard to get right on fastcomp because of pointers :
|
Thanks @pmp-p! For that GOT.func issue, it sounds like in the linked bug they don't have a testcase yet and @sbc100 was asking for one. Perhaps you can add instructions from micropython to emscripten-core/emscripten#8995 ? About the dynamic linking issue with indirect calls, yes, that is a known issue on fastcomp, but it should work on upstream. About the pasted |
I opened a related thread here WebAssembly/WASI#276 |
Thanks for the updates. For what it is worth, I did not implement the wasm backend in my scheme compiler mostly because it requires The approach taken in schism is to rely on tail-call but that as far as I understand does not help to deal with asynchronous calls (xhr) or resume execution on a DOM event. I mocked the target web assembly code, the gist of the idea is to rely on continuous-passing-style (CPS) transformation and a trampoline, arguments, and multiple return values are passed as def trampoline(init):
global thunk
if init:
thunk = scheme_program_entrypoint
init = False
while True:
continuation = thunk()
if continuation.is_pause:
thunk = continuation.continuation
return
else:
thunk = continuation.thunk Mind the fact, that ultimately given the state of web assembly, the design I am thinking about only speeds up the trampoline which is based on my current compiler where most of the time is spent. (On on the somewhat related note: maybe the overhead of switching between wasm and JavaScript because of |
I read through the await proposal that is closed at the moment and part of the continuations issue. I even re-read this thread and figured I forgot to mention and stress the fact that I am/was trying to implement a scheme to wasm compiler.
The trampoline trick I was mentioning in the previous comment requires at least one non-standard feature Commit that introduce emscripten build in chibi scheme which definitly works great except slow startup time. In anycase, whether it is my toy compiler, Gambit Scheme or Chibi, my needs are already satisfied. I will continue to experiment, even try to build something useful, with Scheme in the browser. I am closing the issue to avoid noise (and because, if there is a performance issue, it will be better handled by someone else). Thanks! |
Here is a fork of wasmtime that implements something similar to Scheme's |
I continued my exploration of targeting web assembly to have Scheme in the browser (Chibi via emscripten produce a payload that is too big, Schism does not have I put together a web assembly module that demonstrate how to implement call/cc, while making it possible to pause wasm execution and resume it from JavaScript that works with nodejs 14 and nightly spidermonkey's jsshell. My take away is that Similarly coroutines, and exceptions can be implemented with a global CPS transformation, but given that that transformation might be slow, adding support for call/cc inside wasm engines will cost a lot of effort and will probably have performance impact on all web assembly modules (whether or not they use call/cc). My next goal is to produce a compiler that targets web assembly for a subset of Scheme that support |
There is an active group within the wasm community group that is looking at related topics. It would be good if you could join us. |
I forwarded my question to w3c's wasm mailing list, see https://lists.w3.org/Archives/Public/public-webassembly/2021May/0000.html |
@amirouche, I'd be interested to see whether you get any other replies on that list, but @fgmccabe was probably referring to the stack subgroup, which is meeting today and every two weeks after. The meeting today in fact seems to be about coroutines in Scheme. I don't know if there's any special registration required to join, but maybe someone who attends the group can clarify (the zoom link seems public). |
Thanks for the heads-up. I will be able to attend, if it is open. |
You're welcome to join today's meeting. No special registration is required, but if you plan to attend regularly then you should (if you haven't already) sign up to the WebAssembly Community Group: |
There is as of right now no way to interop JavaScript asynchronous behaviours (DOM events, XHR, ...) with web assembly program.
There is workarounds like emscripten life-cycle function
pause
andresume
and its modern incarnation that will be based on binaryen asyncifybut I think the latter only support top-level invocation.As far as I understand, it would be enough to be able to
pause
the execution of the wasm program passing to a JavaScript callbackon_pause
what is on the stack. The control of execution will be given to JavaScript which will have the responsibility to callresume
with an argument that will be added to the stack web assembly side.Otherwise said, general continuations or continuable exception are not required.
The workaround I found to implement Scheme in web assembly is to rely on a trampoline that receive severals arguments via mutable globals, I think it could be better with
pause
.Let me know what you think.
ref: #1171
ref: #1252
The text was updated successfully, but these errors were encountered: