Skip to content

Support resizable externally managed ArrayBuffers - #1594

Merged
saghul merged 1 commit into
masterfrom
ab-transfer-fix
Jul 31, 2026
Merged

Support resizable externally managed ArrayBuffers#1594
saghul merged 1 commit into
masterfrom
ab-transfer-fix

Conversation

@saghul

@saghul saghul commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@saghul
saghul force-pushed the ab-transfer-fix branch from 9f0e7c9 to a004d4a Compare July 21, 2026 08:56
Comment thread quickjs.h Outdated
Comment thread quickjs.c Outdated
@saghul

saghul commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@bnoordhuis can you PTAL?

Comment thread api-test.c Outdated
Comment thread api-test.c Outdated
Comment thread api-test.c Outdated
Comment thread api-test.c Outdated
Comment thread quickjs.h Outdated
@saghul
saghul force-pushed the ab-transfer-fix branch from a004d4a to e141294 Compare July 29, 2026 08:07
@saghul

saghul commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@bnoordhuis PTAL. Sorry for the back and forth on this one. I had a new idea in the shower today, which I think solves all problems: actually support externally managed RABs.

I did so by turning that free_func into a realloc_func (a single allocation function like we use elsewhere like lre), so externally managed buffers can be resized no problem.

@saghul
saghul force-pushed the ab-transfer-fix branch 2 times, most recently from 658d676 to 65df479 Compare July 30, 2026 07:28
@saghul saghul changed the title Throw on ArrayBuffer.transfer* for externally managed ABs Support resizable externally managed ArrayBuffers Jul 30, 2026
Replace the free-only callback for externally managed ArrayBuffer data:

    typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque,
                                           void *ptr);

with a realloc style one, which can both resize and free:

    typedef void *JSReallocArrayBufferDataFunc(JSRuntime *rt, void *opaque,
                                               void *ptr, size_t size);

JS_NewArrayBuffer() grows a max_len parameter so external resizable
ArrayBuffers can actually be created; zero means not resizable.

Externally managed SharedArrayBuffers are exempt from the realloc
function requirement: growable SABs commit their maximum size upfront and
are never reallocated, so 'buf' must simply be max_len bytes big.
@saghul
saghul force-pushed the ab-transfer-fix branch from 65df479 to 94f60a5 Compare July 30, 2026 07:29
@saghul
saghul merged commit 6a2c5f3 into master Jul 31, 2026
128 checks passed
@saghul
saghul deleted the ab-transfer-fix branch July 31, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants