Skip to content

Commit 6d801de

Browse files
authored
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.
1 parent 0658d9c commit 6d801de

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

quickjs.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37468,7 +37468,7 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
3746837468
if (js_get_length64(ctx, &len, obj))
3746937469
goto exception;
3747037470

37471-
res = FALSE;
37471+
res = TRUE;
3747237472
if (len > 0) {
3747337473
n = 0;
3747437474
if (argc > 1) {
@@ -37479,7 +37479,6 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
3747937479
for (; n < count; n++) {
3748037480
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
3748137481
JS_EQ_SAME_VALUE_ZERO)) {
37482-
res = TRUE;
3748337482
goto done;
3748437483
}
3748537484
}
@@ -37490,11 +37489,11 @@ static JSValue js_array_includes(JSContext *ctx, JSValue this_val,
3749037489
goto exception;
3749137490
if (js_strict_eq2(ctx, js_dup(argv[0]), val,
3749237491
JS_EQ_SAME_VALUE_ZERO)) {
37493-
res = TRUE;
37494-
break;
37492+
goto done;
3749537493
}
3749637494
}
3749737495
}
37496+
res = FALSE;
3749837497
done:
3749937498
JS_FreeValue(ctx, obj);
3750037499
return js_bool(res);
@@ -37508,15 +37507,14 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
3750837507
int argc, JSValue *argv)
3750937508
{
3751037509
JSValue obj, val;
37511-
int64_t len, n, res;
37510+
int64_t len, n;
3751237511
JSValue *arrp;
3751337512
uint32_t count;
3751437513

3751537514
obj = JS_ToObject(ctx, this_val);
3751637515
if (js_get_length64(ctx, &len, obj))
3751737516
goto exception;
3751837517

37519-
res = -1;
3752037518
if (len > 0) {
3752137519
n = 0;
3752237520
if (argc > 1) {
@@ -37527,7 +37525,6 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
3752737525
for (; n < count; n++) {
3752837526
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
3752937527
JS_EQ_STRICT)) {
37530-
res = n;
3753137528
goto done;
3753237529
}
3753337530
}
@@ -37538,15 +37535,15 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValue this_val,
3753837535
goto exception;
3753937536
if (present) {
3754037537
if (js_strict_eq2(ctx, js_dup(argv[0]), val, JS_EQ_STRICT)) {
37541-
res = n;
37542-
break;
37538+
goto done;
3754337539
}
3754437540
}
3754537541
}
3754637542
}
37543+
n = -1;
3754737544
done:
3754837545
JS_FreeValue(ctx, obj);
37549-
return JS_NewInt64(ctx, res);
37546+
return JS_NewInt64(ctx, n);
3755037547

3755137548
exception:
3755237549
JS_FreeValue(ctx, obj);
@@ -37557,35 +37554,43 @@ static JSValue js_array_lastIndexOf(JSContext *ctx, JSValue this_val,
3755737554
int argc, JSValue *argv)
3755837555
{
3755937556
JSValue obj, val;
37560-
int64_t len, n, res;
37561-
int present;
37557+
int64_t len, n;
37558+
JSValue *arrp;
37559+
uint32_t count;
3756237560

3756337561
obj = JS_ToObject(ctx, this_val);
3756437562
if (js_get_length64(ctx, &len, obj))
3756537563
goto exception;
3756637564

37567-
res = -1;
3756837565
if (len > 0) {
3756937566
n = len - 1;
3757037567
if (argc > 1) {
3757137568
if (JS_ToInt64Clamp(ctx, &n, argv[1], -1, len - 1, len))
3757237569
goto exception;
3757337570
}
37574-
/* XXX: should special case fast arrays */
37571+
if (js_get_fast_array(ctx, obj, &arrp, &count) && count == len) {
37572+
for (; n >= 0; n--) {
37573+
if (js_strict_eq2(ctx, js_dup(argv[0]), js_dup(arrp[n]),
37574+
JS_EQ_STRICT)) {
37575+
goto done;
37576+
}
37577+
}
37578+
}
3757537579
for (; n >= 0; n--) {
37576-
present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
37580+
int present = JS_TryGetPropertyInt64(ctx, obj, n, &val);
3757737581
if (present < 0)
3757837582
goto exception;
3757937583
if (present) {
3758037584
if (js_strict_eq2(ctx, js_dup(argv[0]), val, JS_EQ_STRICT)) {
37581-
res = n;
37582-
break;
37585+
goto done;
3758337586
}
3758437587
}
3758537588
}
3758637589
}
37590+
n = -1;
37591+
done:
3758737592
JS_FreeValue(ctx, obj);
37588-
return JS_NewInt64(ctx, res);
37593+
return JS_NewInt64(ctx, n);
3758937594

3759037595
exception:
3759137596
JS_FreeValue(ctx, obj);

0 commit comments

Comments
 (0)