quickjs-ng / quickjs

QuickJS, the Next Generation: a mighty JavaScript engine
MIT License
929 stars 77 forks source link

Support "external" buffers when creating a SharedArrayBuffer #471

Open saghul opened 1 month ago

saghul commented 1 month ago

When creating a SAB using JS_NewArrayBuffer, the alloc_flag is always set to FALSE, which means we assume A SAB was previously allocated and we are just going to "dup" it.

This doesn't work when all we want to is to maneuver a chunk of memory (I have this use case with FFI) since we didn't allocate the SAB in the first place.

I reckon we might want some JS_NewSharedArrayBuffer function to provide extra flexibility. In addition JSFreeArrayBufferDataFunc doesn't apply for a SAB so we'd make it more specific.

I worked this around on txiki.js by adding a magic number to the SAB header, this way I can detect if we allocated the SAB or if it's just external memory, but it's a hack at best.

Thoughts @chqrlie / @bnoordhuis ?

bnoordhuis commented 1 month ago

There's likely some overlap with the resizable (Shared)ArrayBuffer proposal?

saghul commented 1 month ago

Hola! Not sure what that overlap would be, at a glance. Can you elaborate what you have in mind?

bnoordhuis commented 1 month ago

Your ask is for support of unmanaged buffers, right, where the embedder is responsible for allocating and releasing the buffer?

With resizable buffers, resizing also needs to be delegated to the embedder.

saghul commented 1 month ago

Oh I see now, thanks!