Skip to content

Commit 9b3b308

Browse files
authored
Implement TypedArray.prototype.at (#18)
1 parent 83d909e commit 9b3b308

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

quickjs.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51864,6 +51864,61 @@ static JSValue js_typed_array_set_internal(JSContext *ctx,
5186451864
return JS_EXCEPTION;
5186551865
}
5186651866

51867+
static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val,
51868+
int argc, JSValueConst *argv)
51869+
{
51870+
JSObject *p;
51871+
int64_t idx, len;
51872+
51873+
p = get_typed_array(ctx, this_val, /*is_dataview*/0);
51874+
if (!p)
51875+
return JS_EXCEPTION;
51876+
51877+
if (typed_array_is_detached(ctx, p)) {
51878+
JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
51879+
return JS_EXCEPTION;
51880+
}
51881+
51882+
if (JS_ToInt64Sat(ctx, &idx, argv[0]))
51883+
return JS_EXCEPTION;
51884+
51885+
len = p->u.array.count;
51886+
if (idx < 0)
51887+
idx = len + idx;
51888+
51889+
if (idx < 0 || idx >= len)
51890+
return JS_UNDEFINED;
51891+
51892+
switch (p->class_id) {
51893+
case JS_CLASS_INT8_ARRAY:
51894+
return JS_NewInt32(ctx, p->u.array.u.int8_ptr[idx]);
51895+
case JS_CLASS_UINT8C_ARRAY:
51896+
case JS_CLASS_UINT8_ARRAY:
51897+
return JS_NewInt32(ctx, p->u.array.u.uint8_ptr[idx]);
51898+
case JS_CLASS_INT16_ARRAY:
51899+
return JS_NewInt32(ctx, p->u.array.u.int16_ptr[idx]);
51900+
case JS_CLASS_UINT16_ARRAY:
51901+
return JS_NewInt32(ctx, p->u.array.u.uint16_ptr[idx]);
51902+
case JS_CLASS_INT32_ARRAY:
51903+
return JS_NewInt32(ctx, p->u.array.u.int32_ptr[idx]);
51904+
case JS_CLASS_UINT32_ARRAY:
51905+
return JS_NewUint32(ctx, p->u.array.u.uint32_ptr[idx]);
51906+
case JS_CLASS_FLOAT32_ARRAY:
51907+
return __JS_NewFloat64(ctx, p->u.array.u.float_ptr[idx]);
51908+
case JS_CLASS_FLOAT64_ARRAY:
51909+
return __JS_NewFloat64(ctx, p->u.array.u.double_ptr[idx]);
51910+
#ifdef CONFIG_BIGNUM
51911+
case JS_CLASS_BIG_INT64_ARRAY:
51912+
return JS_NewBigInt64(ctx, p->u.array.u.int64_ptr[idx]);
51913+
case JS_CLASS_BIG_UINT64_ARRAY:
51914+
return JS_NewBigUint64(ctx, p->u.array.u.uint64_ptr[idx]);
51915+
#endif
51916+
}
51917+
51918+
abort(); /* unreachable */
51919+
return JS_UNDEFINED;
51920+
}
51921+
5186751922
static JSValue js_typed_array_set(JSContext *ctx,
5186851923
JSValueConst this_val,
5186951924
int argc, JSValueConst *argv)
@@ -53054,6 +53109,7 @@ static const JSCFunctionListEntry js_typed_array_base_funcs[] = {
5305453109

5305553110
static const JSCFunctionListEntry js_typed_array_base_proto_funcs[] = {
5305653111
JS_CGETSET_DEF("length", js_typed_array_get_length, NULL ),
53112+
JS_CFUNC_DEF("at", 1, js_typed_array_at ),
5305753113
JS_CGETSET_MAGIC_DEF("buffer", js_typed_array_get_buffer, NULL, 0 ),
5305853114
JS_CGETSET_MAGIC_DEF("byteLength", js_typed_array_get_byteLength, NULL, 0 ),
5305953115
JS_CGETSET_MAGIC_DEF("byteOffset", js_typed_array_get_byteOffset, NULL, 0 ),

test262.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ template
197197
Temporal=skip
198198
top-level-await
199199
TypedArray
200-
TypedArray.prototype.at=skip
200+
TypedArray.prototype.at
201201
u180e
202202
Uint16Array
203203
Uint32Array

0 commit comments

Comments
 (0)