]> git.kaiwu.me - quickjs.git/commitdiff
fixed the fast case of Array.prototype.indexOf/includes (#632)
authorFabrice Bellard <fabrice@bellard.org>
Sat, 28 Nov 2026 09:52:20 +0000 (10:52 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Sat, 28 Nov 2026 09:52:20 +0000 (10:52 +0100)
quickjs.c
tests/test_builtin.js

index b39add08e8c947488dbffab0c18e073993676d1c..498ac39bbd5e726930545a6eeb19e716d96ac272 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -44444,6 +44444,8 @@ static JSValue js_array_includes(JSContext *ctx, JSValueConst this_val,
                 goto exception;
         }
         if (js_get_fast_array(ctx, obj, &arrp, &count)) {
+            if (count > len)
+                count = len;
             for (; n < count; n++) {
                 if (js_strict_eq2(ctx, argv[0], arrp[n],
                                   JS_EQ_SAME_VALUE_ZERO)) {
@@ -44496,6 +44498,8 @@ static JSValue js_array_indexOf(JSContext *ctx, JSValueConst this_val,
                 goto exception;
         }
         if (js_get_fast_array(ctx, obj, &arrp, &count)) {
+            if (count > len)
+                count = len;
             for (; n < count; n++) {
                 if (js_strict_eq2(ctx, argv[0], arrp[n], JS_EQ_STRICT)) {
                     res = n;
index 64c21fcd382fdc43cc6fdba598936f517cd47d8d..ba9c5a32899102b7f388aa65601f782446189381 100644 (file)
@@ -259,6 +259,11 @@ function test_array()
 
     test_array_extension();
     test_array_iterator();
+
+    a = [4, 2, null];
+    assert(a.indexOf(1.25, { valueOf() { a.push(1.25); return 0; } }), -1);
+    a = [4, 2, null];
+    assert(a.includes(1.25, { valueOf() { a.push(1.25); return 0; } }), false);
 }
 
 function test_array_extension()