Improve js_array_lastIndexOf and friends (#359)

- special case fast arrays in `js_array_lastIndexOf`
- simplify `js_array_indexOf` and `js_array_includes` for consistency.
This commit is contained in:
Charlie Gordon 2024-04-08 23:08:49 +02:00 committed by GitHub
parent 0658d9c3e9
commit 6d801de3e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37468,7 +37468,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) {
@ -37479,7 +37479,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;
}
}
@ -37490,11 +37489,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);
@ -37508,7 +37507,7 @@ 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;
@ -37516,7 +37515,6 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
if (js_get_length64(ctx, &len, obj))
goto exception;
res = -1;
if (len > 0) {
n = 0;
if (argc > 1) {
@ -37527,7 +37525,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;
}
}
@ -37538,15 +37535,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);
@ -37557,35 +37554,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--) {
present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
JS_EQ_STRICT)) {
goto done;
}
}
}
for (; n >= 0; n--) {
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);