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 TypedArray.prototype.at #18

Merged
merged 2 commits into from
Nov 5, 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
56 changes: 56 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51864,6 +51864,61 @@ static JSValue js_typed_array_set_internal(JSContext *ctx,
return JS_EXCEPTION;
}

static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSObject *p;
int64_t idx, len;

p = get_typed_array(ctx, this_val, /*is_dataview*/0);
if (!p)
return JS_EXCEPTION;

if (typed_array_is_detached(ctx, p)) {
JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
return JS_EXCEPTION;
}

if (JS_ToInt64Sat(ctx, &idx, argv[0]))
return JS_EXCEPTION;

len = p->u.array.count;
if (idx < 0)
idx = len + idx;

if (idx < 0 || idx >= len)
return JS_UNDEFINED;

switch (p->class_id) {
case JS_CLASS_INT8_ARRAY:
return JS_NewInt32(ctx, p->u.array.u.int8_ptr[idx]);
case JS_CLASS_UINT8C_ARRAY:
case JS_CLASS_UINT8_ARRAY:
return JS_NewInt32(ctx, p->u.array.u.uint8_ptr[idx]);
case JS_CLASS_INT16_ARRAY:
return JS_NewInt32(ctx, p->u.array.u.int16_ptr[idx]);
case JS_CLASS_UINT16_ARRAY:
return JS_NewInt32(ctx, p->u.array.u.uint16_ptr[idx]);
case JS_CLASS_INT32_ARRAY:
return JS_NewInt32(ctx, p->u.array.u.int32_ptr[idx]);
case JS_CLASS_UINT32_ARRAY:
return JS_NewUint32(ctx, p->u.array.u.uint32_ptr[idx]);
case JS_CLASS_FLOAT32_ARRAY:
return __JS_NewFloat64(ctx, p->u.array.u.float_ptr[idx]);
case JS_CLASS_FLOAT64_ARRAY:
return __JS_NewFloat64(ctx, p->u.array.u.double_ptr[idx]);
#ifdef CONFIG_BIGNUM
case JS_CLASS_BIG_INT64_ARRAY:
return JS_NewBigInt64(ctx, p->u.array.u.int64_ptr[idx]);
case JS_CLASS_BIG_UINT64_ARRAY:
return JS_NewBigUint64(ctx, p->u.array.u.uint64_ptr[idx]);
#endif
}

abort(); /* unreachable */
return JS_UNDEFINED;
}

static JSValue js_typed_array_set(JSContext *ctx,
JSValueConst this_val,
int argc, JSValueConst *argv)
Expand Down Expand Up @@ -53054,6 +53109,7 @@ static const JSCFunctionListEntry js_typed_array_base_funcs[] = {

static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
JS_CGETSET_DEF("length", js_typed_array_get_length, NULL ),
JS_CFUNC_DEF("at", 1, js_typed_array_at ),
JS_CGETSET_MAGIC_DEF("buffer", js_typed_array_get_buffer, NULL, 0 ),
JS_CGETSET_MAGIC_DEF("byteLength", js_typed_array_get_byteLength, NULL, 0 ),
JS_CGETSET_MAGIC_DEF("byteOffset", js_typed_array_get_byteOffset, NULL, 0 ),
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ template
Temporal=skip
top-level-await
TypedArray
TypedArray.prototype.at=skip
TypedArray.prototype.at
u180e
Uint16Array
Uint32Array
Expand Down