Skip to content
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

Implement ArrayBuffer.prototype.transfer #101

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -47230,6 +47230,18 @@ static JSValue JS_ThrowTypeErrorDetachedArrayBuffer(JSContext *ctx)
return JS_ThrowTypeError(ctx, "ArrayBuffer is detached");
}

// #sec-get-arraybuffer.prototype.detached
static JSValue js_array_buffer_get_detached(JSContext *ctx,
littledivy marked this conversation as resolved.
Show resolved Hide resolved
JSValueConst this_val)
{
JSArrayBuffer *abuf = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_BUFFER);
if (!abuf)
return JS_EXCEPTION;
if (abuf->shared)
return JS_ThrowTypeError(ctx, "detached called on SharedArrayBuffer");
bnoordhuis marked this conversation as resolved.
Show resolved Hide resolved
return JS_NewBool(ctx, abuf->detached);
}

static JSValue js_array_buffer_get_byteLength(JSContext *ctx,
JSValueConst this_val,
int class_id)
Expand Down Expand Up @@ -47302,6 +47314,52 @@ uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj)
return NULL;
}

// ES #sec-arraybuffer.prototype.transfer
static JSValue js_array_buffer_transfer(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSArrayBuffer *abuf;
uint64_t new_len, old_len;
uint8_t *bs, *new_bs;

abuf = JS_GetOpaque2(ctx, this_val, JS_CLASS_ARRAY_BUFFER);
if (!abuf)
return JS_EXCEPTION;
if (abuf->shared)
return JS_ThrowTypeError(ctx, "cannot transfer a SharedArrayBuffer");
if (argc < 1 || JS_IsUndefined(argv[0]))
littledivy marked this conversation as resolved.
Show resolved Hide resolved
new_len = abuf->byte_length;
else if (JS_ToIndex(ctx, &new_len, argv[0]))
return JS_EXCEPTION;
if (abuf->detached)
return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
/* create an empty AB */
if (new_len == 0) {
JS_DetachArrayBuffer(ctx, this_val);
return js_array_buffer_constructor2(ctx, JS_UNDEFINED, 0, JS_CLASS_ARRAY_BUFFER);
}
bs = abuf->data;
old_len = abuf->byte_length;
/* if length mismatch, realloc. Otherwise, use the same backing buffer. */
if (new_len != old_len) {
new_bs = js_realloc(ctx, bs, new_len);
if (!new_bs)
return JS_EXCEPTION;
bs = new_bs;
if (new_len > old_len)
memset(bs + old_len, 0, new_len - old_len);
}
/* neuter the backing buffer */
abuf->data = NULL;
abuf->byte_length = 0;
abuf->detached = TRUE;
return js_array_buffer_constructor3(ctx, JS_UNDEFINED, new_len,
littledivy marked this conversation as resolved.
Show resolved Hide resolved
JS_CLASS_ARRAY_BUFFER,
bs, abuf->free_func,
NULL, FALSE);
}

static JSValue js_array_buffer_slice(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv, int class_id)
Expand Down Expand Up @@ -47370,7 +47428,10 @@ static JSValue js_array_buffer_slice(JSContext *ctx,

static const JSCFunctionListEntry js_array_buffer_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("byteLength", js_array_buffer_get_byteLength, NULL, JS_CLASS_ARRAY_BUFFER ),
JS_CGETSET_DEF("detached", js_array_buffer_get_detached, NULL ),
JS_CFUNC_MAGIC_DEF("slice", 2, js_array_buffer_slice, JS_CLASS_ARRAY_BUFFER ),
JS_CFUNC_DEF("transfer", 0, js_array_buffer_transfer ),
JS_CFUNC_DEF("transferToFixedLength", 0, js_array_buffer_transfer ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "ArrayBuffer", JS_PROP_CONFIGURABLE ),
};

Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Array.prototype.flatten
Array.prototype.includes
Array.prototype.values
ArrayBuffer
arraybuffer-transfer=skip
arraybuffer-transfer
arrow-function
async-functions
async-iteration
Expand Down