Browse Source

Follow the BoundNames spec in BlockDeclarationInstantiation (#774)

Marko Lahma 5 years ago
parent
commit
57d56921ea
2 changed files with 28 additions and 18 deletions
  1. 11 0
      Jint.Tests/Runtime/ConstTests.cs
  2. 17 18
      Jint/Runtime/Interpreter/JintStatementList.cs

+ 11 - 0
Jint.Tests/Runtime/ConstTests.cs

@@ -28,5 +28,16 @@ namespace Jint.Tests.Runtime
                 })();
             ");
         }
+
+        [Fact]
+        public void ConstDestructuring()
+        {
+            _engine.Execute(@"
+                let obj = {};
+                for (var i = 0; i < 1; i++) {
+                    const { subElement } = obj;
+                }
+            ");
+        }
     }
 }

+ 17 - 18
Jint/Runtime/Interpreter/JintStatementList.cs

@@ -114,31 +114,30 @@ namespace Jint.Runtime.Interpreter
             List<VariableDeclaration> lexicalDeclarations)
         {
             var envRec = env._record;
+            var boundNames = new List<string>();
             for (var i = 0; i < lexicalDeclarations.Count; i++)
             {
                 var variableDeclaration = lexicalDeclarations[i];
-                ref readonly var nodeList = ref variableDeclaration.Declarations;
-                for (var j = 0; j < nodeList.Count; j++)
+                boundNames.Clear();
+                variableDeclaration.GetBoundNames(boundNames);
+                for (var j = 0; j < boundNames.Count; j++)
                 {
-                    var declaration = nodeList[j];
-                    if (declaration.Id is Identifier identifier)
+                    var dn = boundNames[j];
+                    if (variableDeclaration.Kind == VariableDeclarationKind.Const)
                     {
-                        if (variableDeclaration.Kind == VariableDeclarationKind.Const)
-                        {
-                            envRec.CreateImmutableBinding(identifier.Name, strict: true);
-                        }
-                        else
-                        {
-                            envRec.CreateMutableBinding(identifier.Name, canBeDeleted: false);
-                        }
+                        envRec.CreateImmutableBinding(dn, strict: true);
+                    }
+                    else
+                    {
+                        envRec.CreateMutableBinding(dn, canBeDeleted: false);
                     }
-                    // else if 
-                    /*  If d is a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
-                     * Let fn be the sole element of the BoundNames of d.
-                     * Let fo be the result of performing InstantiateFunctionObject for d with argument env.
-                     * Perform envRec.InitializeBinding(fn, fo).
-                     */
                 }
+
+                /*  If d is a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
+                 * Let fn be the sole element of the BoundNames of d.
+                 * Let fo be the result of performing InstantiateFunctionObject for d with argument env.
+                 * Perform envRec.InitializeBinding(fn, fo).
+                 */
             }
         }
     }