Transfer arraybuffer from framescript

I have a framescript, it used the documents canvas to do some canvas work. I now need that framescript to transfer that arraybuffer to bootstrap.js, and bootstrap.js will transfer it to my ChromeWorker.

I can’t figure out how to transfer from framescript to bootstrap though via messaing (sendAsyncMessage) does anyone know how?

What problem are you having? Are you passing it in data or objects? The properties in data are serialised. A lot of documentation says they are converted to json, but I believe they actually use the structured clone algorithm now. I don’t know if this works with arraybuffer, but I would have thought so. Last resort is to pass them in objects, which just generates a CPOW around any javascript object and should work (slowly and synchronously) with anything.

1 Like

Thanks man, I didn’t know just sending it with sendAsyncMessage would cause it to transfer. I’ll test that right away.

It seems sendAsyncMessage is actually sending a copy.

I found window.postMessage supports transferring, do you think I can use this method somehow from framescript: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage?redirectlocale=en-US&redirectslug=DOM%2Fwindow.postMessage

So my issue is:

I know postMessage works but I’m trying to get arraybuffer between framescript and parent, there is no postMessage between those two

Yes, a clone is a copy even when it is “structured”. That is almost inevitable since the arraybuffer is in one process and the listener is in another. Is a copy not suitable? If you need access to modify it then you can either use the CPOW or establish an asynchronous 2-way communication. arraybuffer is supported for structured cloning although I haven’t used it/

I haven’t used postMessage(), but the same principle would apply. If the sender is in one process and the listener in another then you either get a copy or a CPOW. Or are you using the transferable parameter of postMessage()? Transferred objects are still effectively copies (readable in the new context, gone from the old), just by a different mechanism.

Thanks Litho, I’ll do the non-cpow method for now. But ideally I want to switch it up to doing a transfer. I do transfer between my chromeworker and bootstrap right now. So my large multi meg arraybuffer is instantly moved from worker to bootstrap (or other way around) and the buffer is neutered in the one that sent it out, so its memory friendly :slight_smile:

Otherwise I can watch my memory growing as i copy it from one to the other. It does get freed on GC, so for now Ill do copy, but I hope to hook up transfer in future. Thanks so much for the chat on this!

I think the way the transfer works, is it makes a pointer to the arraybuffer. which is a byte array, and then just use it over in the other thread, not sure, but thats how I used to do it before they banned that method lol. Cant get pointers to my ArrayBuffers anymore with the ctypes trick :frowning:

I think you’re trying to do something impossible. Transfers are between different contexts in the same process, not between processes. There is a bug out related to transferring certain types of object between multiple content processes, but it isn’t clear if that is even possible.

Does the data really have to go to a chrome worker? In many respects, what they did can be done in a frame script without blocking chrome. It would block content though :wink:

1 Like

Ooo you’re right, different processes can’t do sharing huh shoot.

I was trying to do only dom stuff in the framescript because I heard we should try not to do any XPCOM or anything there.

But actually the whole reason I needed this framescript was because there is no canvas in workers yet, so I needed to get a canvas and do some manipulation, then send back array buffer so I can make an ICO, ICNS, etc out of it.

Thanks Litho!