Browse Source

Fix rest/spread bug with empty object (#1642)

David 1 year ago
parent
commit
1697fc39f3

+ 6 - 0
Jint.Tests/Runtime/DestructuringTests.cs

@@ -62,4 +62,10 @@ public class DestructuringTests
     {
     {
         _engine.Execute("return function([x, ...[y, ...z]]) { equal(1, x); equal(2, y); equal('3,4', z + ''); }([1, 2, 3, 4]);");
         _engine.Execute("return function([x, ...[y, ...z]]) { equal(1, x); equal(2, y); equal('3,4', z + ''); }([1, 2, 3, 4]);");
     }
     }
+
+    [Fact]
+    public void EmptyRest()
+    {
+        _engine.Execute("function test({ ...props }){}; test({});");
+    }
 }
 }

+ 1 - 1
Jint/Runtime/Environments/FunctionEnvironmentRecord.cs

@@ -221,7 +221,7 @@ namespace Jint.Runtime.Environments
                     {
                     {
                         if (((RestElement) property).Argument is Identifier restIdentifier)
                         if (((RestElement) property).Argument is Identifier restIdentifier)
                         {
                         {
-                            var rest = _engine.Realm.Intrinsics.Object.Construct(argumentObject.Properties!.Count - processedProperties!.Count);
+                            var rest = _engine.Realm.Intrinsics.Object.Construct((argumentObject.Properties?.Count ?? 0) - processedProperties!.Count);
                             argumentObject.CopyDataProperties(rest, processedProperties);
                             argumentObject.CopyDataProperties(rest, processedProperties);
                             SetItemSafely(restIdentifier.Name, rest, initiallyEmpty);
                             SetItemSafely(restIdentifier.Name, rest, initiallyEmpty);
                         }
                         }