Skip to content

Improve js_array_lastIndexOf and friends #359

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

Merged
merged 1 commit into from
Apr 8, 2024
Merged
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
41 changes: 23 additions & 18 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37487,7 +37487,7 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
if (js_get_length64(ctx, &len, obj))
goto exception;

res = FALSE;
res = TRUE;
if (len > 0) {
n = 0;
if (argc > 1) {
Expand All @@ -37498,7 +37498,6 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
for (; n < count; n++) {
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
JS_EQ_SAME_VALUE_ZERO)) {
res = TRUE;
goto done;
}
}
Expand All @@ -37509,11 +37508,11 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
goto exception;
if (js_strict_eq2(ctx, js_dup(argv[0]), val,
JS_EQ_SAME_VALUE_ZERO)) {
res = TRUE;
break;
goto done;
}
}
}
res = FALSE;
done:
JS_FreeValue(ctx, obj);
return js_bool(res);
Expand All @@ -37527,15 +37526,14 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
JSValue obj, val;
int64_t len, n, res;
int64_t len, n;
JSValue *arrp;
uint32_t count;

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

res = -1;
if (len > 0) {
n = 0;
if (argc > 1) {
Expand All @@ -37546,7 +37544,6 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
for (; n < count; n++) {
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
JS_EQ_STRICT)) {
res = n;
goto done;
}
}
Expand All @@ -37557,15 +37554,15 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
goto exception;
if (present) {
if (js_strict_eq2(ctx, js_dup(argv[0]), val, JS_EQ_STRICT)) {
res = n;
break;
goto done;
}
}
}
}
n = -1;
done:
JS_FreeValue(ctx, obj);
return JS_NewInt64(ctx, res);
return JS_NewInt64(ctx, n);

exception:
JS_FreeValue(ctx, obj);
Expand All @@ -37576,35 +37573,43 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
JSValue obj, val;
int64_t len, n, res;
int present;
int64_t len, n;
JSValue *arrp;
uint32_t count;

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

res = -1;
if (len > 0) {
n = len - 1;
if (argc > 1) {
if (JS_ToInt64Clamp(ctx, &n, argv[1], -1, len - 1, len))
goto exception;
}
/* XXX: should special case fast arrays */
if (js_get_fast_array(ctx, obj, &arrp, &count) && count == len) {
for (; n >= 0; n--) {
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
JS_EQ_STRICT)) {
goto done;
}
}
}
for (; n >= 0; n--) {
present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
int present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
if (present < 0)
goto exception;
if (present) {
if (js_strict_eq2(ctx, js_dup(argv[0]), val, JS_EQ_STRICT)) {
res = n;
break;
goto done;
}
}
}
}
n = -1;
done:
JS_FreeValue(ctx, obj);
return JS_NewInt64(ctx, res);
return JS_NewInt64(ctx, n);

exception:
JS_FreeValue(ctx, obj);
Expand Down