]> git.kaiwu.me - quickjs.git/commitdiff
Write back every element in Array.prototype.sort (theaddonn)
authorFabrice Bellard <fabrice@bellard.org>
Mon, 26 Oct 2026 11:11:59 +0000 (12:11 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Mon, 26 Oct 2026 11:11:59 +0000 (12:11 +0100)
quickjs.c
tests/test_builtin.js

index 9b5daae1ffd9c8c78e4c97bfe5d712f447499524..13d2d756dbcfbb288d3fc5dbb62af053aee814e6 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -44884,13 +44884,9 @@ static JSValue js_array_sort(JSContext *ctx, JSValueConst this_val,
     while (n < pos) {
         if (array[n].str)
             JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, array[n].str));
-        if (array[n].pos == n) {
-            JS_FreeValue(ctx, array[n].val);
-        } else {
-            if (JS_SetPropertyInt64(ctx, obj, n, array[n].val) < 0) {
-                n++;
-                goto exception;
-            }
+        if (JS_SetPropertyInt64(ctx, obj, n, array[n].val) < 0) {
+            n++;
+            goto exception;
         }
         n++;
     }
index c20d740c6709d0aeb1a1bdee160eec5df6f20599..a7fb65a32c97377b92266e03022a62dcb58fcb96 100644 (file)
@@ -230,6 +230,10 @@ function test_array()
         err = true;
     }
     assert(err && a.toString() === "1,2,3,4");
+
+    /* sort writes back every element, even those already in place */
+    assert_throws(TypeError, () => Array.prototype.sort.call("aa"));
+    assert_throws(TypeError, () => Object.freeze([1, 2]).sort());
 }
 
 function test_string()