From: Fabrice Bellard Date: Sat, 28 Nov 2026 09:52:20 +0000 (+0100) Subject: fixed the fast case of Array.prototype.indexOf/includes (#632) X-Git-Url: https://git.kaiwu.me/http/%22../$%7BGITURL%7D/static/%7B@url%7D?a=commitdiff_plain;h=5e0646c9cb621ebfabcda3999f2d2bf064b7a3c5;p=quickjs.git fixed the fast case of Array.prototype.indexOf/includes (#632) --- diff --git a/quickjs.c b/quickjs.c index b39add0..498ac39 100644 --- 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; diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 64c21fc..ba9c5a3 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -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()