writing to SharedArrayBuffer inside webassembly? (or similar)

1,720 views
Skip to first unread message

Van Catha

unread,
Oct 23, 2017, 11:29:27 PM10/23/17
to emscripten-discuss
Is there a way to write to a shared array buffer from inside webassembly?  I recall trying before and the javascript side just showed a fully zeroed buffer.

I have an app that produces about 40-160mb of memory every second depending in what mode its running in.  This is returned to javascript side by creating a copy. via HEAPU8.subarray (does this indeed copy?).  Ideally I would like to pass a SharedArrayBuffer to the WASM side and write to it.  Second ideal scenario pass a ArrayBuffer to WASM side and write to it,  worst case scenario, pass a read only ArrayBuffer to WASM side, and allocate a new buffer to return (which is what I am doing now).




Jukka Jylänki

unread,
Oct 24, 2017, 7:56:17 AM10/24/17
to emscripte...@googlegroups.com
Currently WebAssembly and SharedArrayBuffer are still incompatible.
The spec for integrating these two is still yet not quite finished,
but we are working hard to get this done, this is one of the hottest
active development items at the moment.

Keep an eye out for the Multithreading series of pull requests in the
Emscripten tracker. Once that is all finished, Wasm multithreading
should be possible (in Firefox Nightly and Chrome Canary).
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-disc...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jan Kretschmer

unread,
Nov 6, 2017, 10:38:27 AM11/6/17
to emscripten-discuss
@jj

Just to clarify:
you are talking about sharing memory across threads.
the original question was about sharing memory between emscripten and the plain javascript context.

the way i read your answer these two are linked (i.e. both being worked on) is that right?

thank you for the insight!

Jukka Jylänki

unread,
Nov 10, 2017, 8:09:00 AM11/10/17
to emscripte...@googlegroups.com
That's right, my understanding was that this was in a multithreaded
Web Worker context since SharedArrayBuffer was mentioned.

To share memory between JavaScript and Wasm in the same thread,
SharedArrayBuffer is not needed. You can malloc() a block of memory in
the WebAssembly heap either on C side or JS side, and pass the pointer
to the other side and access the memory directly.

Unfortunately it is not possible to pass a whole separate
ArrayBuffer(View) from outside JavaScript to inside a WebAssembly
Module, but the WebAssembly Module is limited to operating only on the
one buffer that it is initialized with.
>> > email to emscripten-disc...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-disc...@googlegroups.com.

Jan Kretschmer

unread,
May 2, 2018, 5:55:40 AM5/2/18
to emscripten-discuss
Thank you so much

so my current understanding is that:
  • because web assmebly modules (or better instances) operate on their own linear memory chunk, there will never be a way to share memory between two wasm instances (or js context+wasm instances) on one page without copying.
  • One exception is if memory is allocated in a wasm instance from the get go you can have a shallow memory view on that buffer from js as well (but NOT from another wasm instance)
  • In a setup where you have a wasm-based pool of algorithms which are split into modules, the only way to share memory between modules might currently be to dynamically link the modules at load time so they share a memory context at instantiation time.
am i understanding things correctly??

>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Alon Zakai

unread,
May 3, 2018, 2:42:00 PM5/3/18
to emscripten-discuss
No, wasm can import a Memory. That means that it can see the same Memory as JS (that's how emscripten works in fact, both JS and wasm can write to the memory that C programs see). And also multiple wasm instances can share the same Memory, if they import the same ones.

What you can't do yet is share that memory to a web worker - that would require multithreading support. So this is all single-threaded and in the same JS context.

You also can't share other stuff than a Memory with JS. For example if JS receives a new ArrayBuffer from somewhere, existing wasm can't see it without copying it into the Memory it is aware of. (But you could create a new wasm module that uses the new ArrayBuffer as imported Memory.)

Thank you so much
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages