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 Array.prototype.at #13

Merged
merged 1 commit 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
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