]> git.kaiwu.me - quickjs.git/commitdiff
fixed object spread with strings (#611)
authorFabrice Bellard <fabrice@bellard.org>
Tue, 10 Nov 2026 09:07:24 +0000 (10:07 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Tue, 10 Nov 2026 09:07:24 +0000 (10:07 +0100)
quickjs.c
tests/test_language.js

index 8f36a55117d7e3d2ff7bf9e29aaecbe66fe79432..03af563d47256b385910941be89c6605b3d9adf3 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -17379,12 +17379,12 @@ exception:
 
 static __exception int JS_CopyDataProperties(JSContext *ctx,
                                              JSValueConst target,
-                                             JSValueConst source,
+                                             JSValueConst source0,
                                              JSValueConst excluded,
                                              BOOL setprop)
 {
     JSPropertyEnum *tab_atom;
-    JSValue val;
+    JSValue val, source;
     uint32_t i, tab_atom_count;
     JSObject *p;
     JSObject *pexcl = NULL;
@@ -17392,8 +17392,11 @@ static __exception int JS_CopyDataProperties(JSContext *ctx,
     JSPropertyDescriptor desc;
     BOOL is_enumerable;
 
-    if (JS_VALUE_GET_TAG(source) != JS_TAG_OBJECT)
+    if (JS_IsNull(source0) || JS_IsUndefined(source0))
         return 0;
+    source = JS_ToObject(ctx, source0);
+    if (JS_IsException(source))
+        return -1;
 
     if (JS_VALUE_GET_TAG(excluded) == JS_TAG_OBJECT)
         pexcl = JS_VALUE_GET_OBJ(excluded);
@@ -17410,8 +17413,10 @@ static __exception int JS_CopyDataProperties(JSContext *ctx,
         }
     }
     if (JS_GetOwnPropertyNamesInternal(ctx, &tab_atom, &tab_atom_count, p,
-                                       gpn_flags))
+                                       gpn_flags)) {
+        JS_FreeValue(ctx, source);
         return -1;
+    }
 
     for (i = 0; i < tab_atom_count; i++) {
         if (pexcl) {
@@ -17446,9 +17451,11 @@ static __exception int JS_CopyDataProperties(JSContext *ctx,
             goto exception;
     }
     JS_FreePropertyEnum(ctx, tab_atom, tab_atom_count);
+    JS_FreeValue(ctx, source);
     return 0;
  exception:
     JS_FreePropertyEnum(ctx, tab_atom, tab_atom_count);
+    JS_FreeValue(ctx, source);
     return -1;
 }
 
@@ -41851,27 +41858,19 @@ static JSValue js_object_assign(JSContext *ctx, JSValueConst this_val,
                                 int argc, JSValueConst *argv)
 {
     // Object.assign(obj, source1)
-    JSValue obj, s;
+    JSValue obj;
     int i;
 
-    s = JS_UNDEFINED;
     obj = JS_ToObject(ctx, argv[0]);
     if (JS_IsException(obj))
         goto exception;
     for (i = 1; i < argc; i++) {
-        if (!JS_IsNull(argv[i]) && !JS_IsUndefined(argv[i])) {
-            s = JS_ToObject(ctx, argv[i]);
-            if (JS_IsException(s))
+        if (JS_CopyDataProperties(ctx, obj, argv[i], JS_UNDEFINED, TRUE))
                 goto exception;
-            if (JS_CopyDataProperties(ctx, obj, s, JS_UNDEFINED, TRUE))
-                goto exception;
-            JS_FreeValue(ctx, s);
-        }
     }
     return obj;
 exception:
     JS_FreeValue(ctx, obj);
-    JS_FreeValue(ctx, s);
     return JS_EXCEPTION;
 }
 
index aa96c223cd735ce2e6dbac4e1bc3493cd4ce8fc0..ba479bac4b8a1f152d86c21c639aac721ccd1498 100644 (file)
@@ -472,6 +472,8 @@ function test_spread()
 
     x = [ ...[ , ] ];
     assert(Object.getOwnPropertyNames(x).toString(), "0,length");
+
+    assert(JSON.stringify({ ..."ab" }), '{"0":"a","1":"b"}');
 }
 
 function test_function_length()