From 1697fc39f39c831bac3161b73690b380f02676d2 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 1 Oct 2023 13:24:07 -0700 Subject: [PATCH] Fix rest/spread bug with empty object (#1642) --- Jint.Tests/Runtime/DestructuringTests.cs | 6 ++++++ Jint/Runtime/Environments/FunctionEnvironmentRecord.cs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Jint.Tests/Runtime/DestructuringTests.cs b/Jint.Tests/Runtime/DestructuringTests.cs index 4c1ac13cea..f62264cc7b 100644 --- a/Jint.Tests/Runtime/DestructuringTests.cs +++ b/Jint.Tests/Runtime/DestructuringTests.cs @@ -62,4 +62,10 @@ public void WithNestedRest() { _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({});"); + } } diff --git a/Jint/Runtime/Environments/FunctionEnvironmentRecord.cs b/Jint/Runtime/Environments/FunctionEnvironmentRecord.cs index ed5ade0f2f..20c593bab6 100644 --- a/Jint/Runtime/Environments/FunctionEnvironmentRecord.cs +++ b/Jint/Runtime/Environments/FunctionEnvironmentRecord.cs @@ -221,7 +221,7 @@ private void HandleObjectPattern(EvaluationContext context, bool initiallyEmpty, { 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); SetItemSafely(restIdentifier.Name, rest, initiallyEmpty); }