<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[192586] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/192586">192586</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2015-11-18 14:01:19 -0800 (Wed, 18 Nov 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>There is a bug when default parameter values are mixed with destructuring parameter values
https://bugs.webkit.org/show_bug.cgi?id=151369

Reviewed by Geoffrey Garen and Mark Lam.

This patch changes our parser to no longer declare destructuring
parameters as &quot;var&quot;s. This is a weird bug that just happened
to work in a world without default parameter values. In a world with
default parameter values this is just completely wrong. It would
incorrectly transform this program:
```function foo(a = function() { b = 40; }, {b}) { a(); return b; }; foo(undefined, {b: 42}); // Should return 40```
into
```function foo(a = function() { b = 40; }, {b}) { var b; a(); return b; }; foo(undefined, {b:42}); // Returns 42, not 40.```
Which is wrong because we end up with two distinct bindings of &quot;b&quot; when
there should only be one.

* parser/Parser.cpp:
(JSC::Parser&lt;LexerType&gt;::parseVariableDeclarationList):
(JSC::Parser&lt;LexerType&gt;::createBindingPattern):
(JSC::Parser&lt;LexerType&gt;::parseDestructuringPattern):
* parser/Parser.h:
(JSC::Scope::declareParameter):
(JSC::Scope::getUsedVariables):
(JSC::Parser::strictMode):
(JSC::Parser::isValidStrictMode):
(JSC::Parser::declareParameter):
(JSC::Parser::breakIsValid):
(JSC::Scope::declareBoundParameter): Deleted.
(JSC::Parser::declareBoundParameter): Deleted.
* tests/stress/es6-default-parameters.js:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreparserParsercpp">trunk/Source/JavaScriptCore/parser/Parser.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreparserParserh">trunk/Source/JavaScriptCore/parser/Parser.h</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstresses6defaultparametersjs">trunk/Source/JavaScriptCore/tests/stress/es6-default-parameters.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (192585 => 192586)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-11-18 21:46:33 UTC (rev 192585)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-11-18 22:01:19 UTC (rev 192586)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2015-11-18  Saam barati  &lt;sbarati@apple.com&gt;
+
+        There is a bug when default parameter values are mixed with destructuring parameter values
+        https://bugs.webkit.org/show_bug.cgi?id=151369
+
+        Reviewed by Geoffrey Garen and Mark Lam.
+
+        This patch changes our parser to no longer declare destructuring
+        parameters as &quot;var&quot;s. This is a weird bug that just happened
+        to work in a world without default parameter values. In a world with
+        default parameter values this is just completely wrong. It would
+        incorrectly transform this program:
+        ```function foo(a = function() { b = 40; }, {b}) { a(); return b; }; foo(undefined, {b: 42}); // Should return 40```
+        into
+        ```function foo(a = function() { b = 40; }, {b}) { var b; a(); return b; }; foo(undefined, {b:42}); // Returns 42, not 40.```
+        Which is wrong because we end up with two distinct bindings of &quot;b&quot; when
+        there should only be one.
+
+        * parser/Parser.cpp:
+        (JSC::Parser&lt;LexerType&gt;::parseVariableDeclarationList):
+        (JSC::Parser&lt;LexerType&gt;::createBindingPattern):
+        (JSC::Parser&lt;LexerType&gt;::parseDestructuringPattern):
+        * parser/Parser.h:
+        (JSC::Scope::declareParameter):
+        (JSC::Scope::getUsedVariables):
+        (JSC::Parser::strictMode):
+        (JSC::Parser::isValidStrictMode):
+        (JSC::Parser::declareParameter):
+        (JSC::Parser::breakIsValid):
+        (JSC::Scope::declareBoundParameter): Deleted.
+        (JSC::Parser::declareBoundParameter): Deleted.
+        * tests/stress/es6-default-parameters.js:
+
</ins><span class="cx"> 2015-11-17  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] Untangle the dependencies on FTLAbbreviations a bit
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreparserParsercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/parser/Parser.cpp (192585 => 192586)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/parser/Parser.cpp        2015-11-18 21:46:33 UTC (rev 192585)
+++ trunk/Source/JavaScriptCore/parser/Parser.cpp        2015-11-18 22:01:19 UTC (rev 192586)
</span><span class="lines">@@ -667,7 +667,7 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> template &lt;typename LexerType&gt;
</span><del>-template &lt;class TreeBuilder&gt; TreeDestructuringPattern Parser&lt;LexerType&gt;::createBindingPattern(TreeBuilder&amp; context, DestructuringKind kind, ExportType exportType, const Identifier&amp; name, int depth, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier)
</del><ins>+template &lt;class TreeBuilder&gt; TreeDestructuringPattern Parser&lt;LexerType&gt;::createBindingPattern(TreeBuilder&amp; context, DestructuringKind kind, ExportType exportType, const Identifier&amp; name, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(!name.isNull());
</span><span class="cx">     
</span><span class="lines">@@ -685,42 +685,23 @@
</span><span class="cx">             failIfTrue(declarationResult &amp; DeclarationResult::InvalidDuplicateDeclaration, &quot;Cannot declare a lexical variable twice: '&quot;, name.impl(), &quot;'&quot;);
</span><span class="cx">         }
</span><span class="cx">     } else if (kind == DestructureToParameters) {
</span><del>-        if (depth) {
-            auto bindingResult = declareBoundParameter(&amp;name);
-            if (bindingResult == Scope::StrictBindingFailed &amp;&amp; strictMode()) {
-                semanticFailIfTrue(isEvalOrArguments(&amp;name), &quot;Cannot destructure to a parameter name '&quot;, name.impl(), &quot;' in strict mode&quot;);
-                if (m_lastFunctionName &amp;&amp; name == *m_lastFunctionName)
-                    semanticFail(&quot;Cannot destructure to '&quot;, name.impl(), &quot;' as it shadows the name of a strict mode function&quot;);
-                semanticFailureDueToKeyword(&quot;bound parameter name&quot;);
-                if (hasDeclaredParameter(name))
-                    semanticFail(&quot;Cannot destructure to '&quot;, name.impl(), &quot;' as it has already been declared&quot;);
-                semanticFail(&quot;Cannot bind to a parameter named '&quot;, name.impl(), &quot;' in strict mode&quot;);
-            }
-            if (bindingResult == Scope::BindingFailed) {
-                semanticFailureDueToKeyword(&quot;bound parameter name&quot;);
-                if (hasDeclaredParameter(name))
-                    semanticFail(&quot;Cannot destructure to '&quot;, name.impl(), &quot;' as it has already been declared&quot;);
-                semanticFail(&quot;Cannot destructure to a parameter named '&quot;, name.impl(), &quot;'&quot;);
-            }
-        } else {
-            DeclarationResultMask declarationResult = declareParameter(&amp;name);
-            if ((declarationResult &amp; DeclarationResult::InvalidStrictMode) &amp;&amp; strictMode()) {
-                semanticFailIfTrue(isEvalOrArguments(&amp;name), &quot;Cannot destructure to a parameter name '&quot;, name.impl(), &quot;' in strict mode&quot;);
-                if (m_lastFunctionName &amp;&amp; name == *m_lastFunctionName)
-                    semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' as it shadows the name of a strict mode function&quot;);
-                semanticFailureDueToKeyword(&quot;parameter name&quot;);
-                if (hasDeclaredParameter(name))
-                    semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' in strict mode as it has already been declared&quot;);
-                semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' in strict mode&quot;);
-            }
-            if (declarationResult &amp; DeclarationResult::InvalidDuplicateDeclaration) {
-                // It's not always an error to define a duplicate parameter.
-                // It's only an error when there are default parameter values or destructuring parameters.
-                // We note this value now so we can check it later.
-                if (duplicateIdentifier)
-                    *duplicateIdentifier = &amp;name;
-            }
</del><ins>+        DeclarationResultMask declarationResult = declareParameter(&amp;name);
+        if ((declarationResult &amp; DeclarationResult::InvalidStrictMode) &amp;&amp; strictMode()) {
+            semanticFailIfTrue(isEvalOrArguments(&amp;name), &quot;Cannot destructure to a parameter name '&quot;, name.impl(), &quot;' in strict mode&quot;);
+            if (m_lastFunctionName &amp;&amp; name == *m_lastFunctionName)
+                semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' as it shadows the name of a strict mode function&quot;);
+            semanticFailureDueToKeyword(&quot;parameter name&quot;);
+            if (hasDeclaredParameter(name))
+                semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' in strict mode as it has already been declared&quot;);
+            semanticFail(&quot;Cannot declare a parameter named '&quot;, name.impl(), &quot;' in strict mode&quot;);
</ins><span class="cx">         }
</span><ins>+        if (declarationResult &amp; DeclarationResult::InvalidDuplicateDeclaration) {
+            // It's not always an error to define a duplicate parameter.
+            // It's only an error when there are default parameter values or destructuring parameters.
+            // We note this value now so we can check it later.
+            if (duplicateIdentifier)
+                *duplicateIdentifier = &amp;name;
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (exportType == ExportType::Exported) {
</span><span class="lines">@@ -879,7 +860,7 @@
</span><span class="cx">                 if (consume(COLON))
</span><span class="cx">                     innerPattern = parseBindingOrAssignmentElement(context, kind, exportType, duplicateIdentifier, hasDestructuringPattern, bindingContext, depth + 1);
</span><span class="cx">                 else
</span><del>-                    innerPattern = createBindingPattern(context, kind, exportType, *propertyName, depth + 1, identifierToken, bindingContext, duplicateIdentifier);
</del><ins>+                    innerPattern = createBindingPattern(context, kind, exportType, *propertyName, identifierToken, bindingContext, duplicateIdentifier);
</ins><span class="cx">             } else {
</span><span class="cx">                 JSTokenType tokenType = m_token.m_type;
</span><span class="cx">                 switch (m_token.m_type) {
</span><span class="lines">@@ -935,7 +916,7 @@
</span><span class="cx">             failWithMessage(&quot;Expected a parameter pattern or a ')' in parameter list&quot;);
</span><span class="cx">         }
</span><span class="cx">         failIfTrue(match(LET) &amp;&amp; (kind == DestructureToLet || kind == DestructureToConst), &quot;Can't use 'let' as an identifier name for a LexicalDeclaration&quot;);
</span><del>-        pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, depth, m_token, bindingContext, duplicateIdentifier);
</del><ins>+        pattern = createBindingPattern(context, kind, exportType, *m_token.m_data.ident, m_token, bindingContext, duplicateIdentifier);
</ins><span class="cx">         next();
</span><span class="cx">         break;
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreparserParserh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/parser/Parser.h (192585 => 192586)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/parser/Parser.h        2015-11-18 21:46:33 UTC (rev 192585)
+++ trunk/Source/JavaScriptCore/parser/Parser.h        2015-11-18 22:01:19 UTC (rev 192586)
</span><span class="lines">@@ -424,26 +424,6 @@
</span><span class="cx">         return result;
</span><span class="cx">     }
</span><span class="cx">     
</span><del>-    enum BindingResult {
-        BindingFailed,
-        StrictBindingFailed,
-        BindingSucceeded
-    };
-    BindingResult declareBoundParameter(const Identifier* ident)
-    {
-        bool isArgumentsIdent = isArguments(m_vm, ident);
-        auto addResult = m_declaredVariables.add(ident-&gt;impl());
-        addResult.iterator-&gt;value.setIsVar(); // Treat destructuring parameters as &quot;var&quot;s.
-        bool isValidStrictMode = addResult.isNewEntry &amp;&amp; !isEval(m_vm, ident) &amp;&amp; !isArgumentsIdent;
-        m_isValidStrictMode = m_isValidStrictMode &amp;&amp; isValidStrictMode;
-    
-        if (isArgumentsIdent)
-            m_shadowsArguments = true;
-        if (!addResult.isNewEntry)
-            return BindingFailed;
-        return isValidStrictMode ? BindingSucceeded : StrictBindingFailed;
-    }
-
</del><span class="cx">     void getUsedVariables(IdentifierSet&amp; usedVariables)
</span><span class="cx">     {
</span><span class="cx">         usedVariables.swap(m_usedVariables);
</span><span class="lines">@@ -1054,7 +1034,6 @@
</span><span class="cx">     bool strictMode() { return currentScope()-&gt;strictMode(); }
</span><span class="cx">     bool isValidStrictMode() { return currentScope()-&gt;isValidStrictMode(); }
</span><span class="cx">     DeclarationResultMask declareParameter(const Identifier* ident) { return currentScope()-&gt;declareParameter(ident); }
</span><del>-    Scope::BindingResult declareBoundParameter(const Identifier* ident) { return currentScope()-&gt;declareBoundParameter(ident); }
</del><span class="cx">     bool breakIsValid()
</span><span class="cx">     {
</span><span class="cx">         ScopeRef current = currentScope();
</span><span class="lines">@@ -1159,7 +1138,7 @@
</span><span class="cx">     template &lt;class TreeBuilder&gt; TreeExpression parseVariableDeclarationList(TreeBuilder&amp;, int&amp; declarations, TreeDestructuringPattern&amp; lastPattern, TreeExpression&amp; lastInitializer, JSTextPosition&amp; identStart, JSTextPosition&amp; initStart, JSTextPosition&amp; initEnd, VarDeclarationListContext, DeclarationType, ExportType, bool&amp; forLoopConstDoesNotHaveInitializer);
</span><span class="cx">     template &lt;class TreeBuilder&gt; TreeSourceElements parseArrowFunctionSingleExpressionBodySourceElements(TreeBuilder&amp;);
</span><span class="cx">     template &lt;class TreeBuilder&gt; TreeExpression parseArrowFunctionExpression(TreeBuilder&amp;);
</span><del>-    template &lt;class TreeBuilder&gt; NEVER_INLINE TreeDestructuringPattern createBindingPattern(TreeBuilder&amp;, DestructuringKind, ExportType, const Identifier&amp;, int depth, JSToken, AssignmentContext, const Identifier** duplicateIdentifier);
</del><ins>+    template &lt;class TreeBuilder&gt; NEVER_INLINE TreeDestructuringPattern createBindingPattern(TreeBuilder&amp;, DestructuringKind, ExportType, const Identifier&amp;, JSToken, AssignmentContext, const Identifier** duplicateIdentifier);
</ins><span class="cx">     template &lt;class TreeBuilder&gt; NEVER_INLINE TreeDestructuringPattern createAssignmentElement(TreeBuilder&amp;, TreeExpression&amp;, const JSTextPosition&amp;, const JSTextPosition&amp;);
</span><span class="cx">     template &lt;class TreeBuilder&gt; NEVER_INLINE TreeDestructuringPattern parseBindingOrAssignmentElement(TreeBuilder&amp; context, DestructuringKind, ExportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth);
</span><span class="cx">     template &lt;class TreeBuilder&gt; NEVER_INLINE TreeDestructuringPattern parseAssignmentElement(TreeBuilder&amp; context, DestructuringKind, ExportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstresses6defaultparametersjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/stress/es6-default-parameters.js (192585 => 192586)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/es6-default-parameters.js        2015-11-18 21:46:33 UTC (rev 192585)
+++ trunk/Source/JavaScriptCore/tests/stress/es6-default-parameters.js        2015-11-18 22:01:19 UTC (rev 192586)
</span><span class="lines">@@ -235,7 +235,39 @@
</span><span class="cx"> })();
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+// Test proper variable binding.
+;(function() {
+    function foo(a = function() { return b; }, {b}) {
+        assert(a() === 34);
+        assert(b === 34);
+        b = 50;
+        assert(a() === 50);
+        assert(b === 50);
+    }
+    function bar(a = function(x) { b = x; }, {b}) {
+        assert(b === 34);
+        a(50);
+        assert(b === 50);
+    }
+    function baz(f1 = function(x) { b = x; }, f2 = function() { return b; }, {b}) {
+        var b;
+        assert(b === 34);
+        assert(f2() === 34);
+        f1(50);
+        assert(b === 34);
+        assert(f2() === 50);
+    }
+    noInline(foo);
+    noInline(bar);
+    noInline(baz);
+    for (let i = 0; i &lt; 1000; i++) {
+        foo(undefined, {b: 34});
+        bar(undefined, {b: 34});
+        baz(undefined, undefined, {b: 34});
+    }
+})();
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> // Syntax errors.
</span><span class="cx"> shouldThrowSyntaxError(&quot;function b(a = 20, a = 40) {}&quot;);
</span><span class="cx"> shouldThrowSyntaxError(&quot;function b(aaaaa = 20,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v, aaaaa = 40) {}&quot;);
</span></span></pre>
</div>
</div>

</body>
</html>