Skip to content

Commit

Permalink
Implement Array.prototype.at
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Nov 4, 2023
1 parent 7be933e commit bc6b200
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38412,6 +38412,38 @@ static int JS_isConcatSpreadable(JSContext *ctx, JSValueConst obj)
return JS_IsArray(ctx, obj);
}

static JSValue js_array_at(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue obj, ret;
int64_t len, idx;
JSValue *arrp;
uint32_t count;

ret = JS_EXCEPTION;
obj = JS_ToObject(ctx, this_val);
if (js_get_length64(ctx, &len, obj))
goto exception;

if (JS_ToInt64Sat(ctx, &idx, argv[0]))
goto exception;

if (idx < 0)
idx = len + idx;

if (idx < 0 || idx >= len) {
ret = JS_UNDEFINED;
} else if (js_get_fast_array(ctx, obj, &arrp, &count) && count == len) {
ret = JS_DupValue(ctx, arrp[idx]);
} else if (!JS_TryGetPropertyInt64(ctx, obj, idx, &ret)) {
ret = JS_UNDEFINED;
}

exception:
JS_FreeValue(ctx, obj);
return ret;
}

static JSValue js_array_concat(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
Expand Down Expand Up @@ -39773,6 +39805,7 @@ static const JSCFunctionListEntry js_iterator_proto_funcs[] = {
};

static const JSCFunctionListEntry js_array_proto_funcs[] = {
JS_CFUNC_DEF("at", 1, js_array_at ),
JS_CFUNC_DEF("concat", 1, js_array_concat ),
JS_CFUNC_MAGIC_DEF("every", 1, js_array_every, special_every ),
JS_CFUNC_MAGIC_DEF("some", 1, js_array_every, special_some ),
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ arbitrary-module-namespace-names=skip
array-find-from-last=skip
array-grouping=skip
Array.fromAsync=skip
Array.prototype.at=skip
Array.prototype.at
Array.prototype.flat
Array.prototype.flatMap
Array.prototype.flatten
Expand Down

0 comments on commit bc6b200

Please sign in to comment.