<!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 "var"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 "b" when
there should only be one.
* parser/Parser.cpp:
(JSC::Parser<LexerType>::parseVariableDeclarationList):
(JSC::Parser<LexerType>::createBindingPattern):
(JSC::Parser<LexerType>::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 <sbarati@apple.com>
+
+ 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 "var"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 "b" when
+ there should only be one.
+
+ * parser/Parser.cpp:
+ (JSC::Parser<LexerType>::parseVariableDeclarationList):
+ (JSC::Parser<LexerType>::createBindingPattern):
+ (JSC::Parser<LexerType>::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 <bpoulain@apple.com>
</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 <typename LexerType>
</span><del>-template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& name, int depth, JSToken token, AssignmentContext bindingContext, const Identifier** duplicateIdentifier)
</del><ins>+template <class TreeBuilder> TreeDestructuringPattern Parser<LexerType>::createBindingPattern(TreeBuilder& context, DestructuringKind kind, ExportType exportType, const Identifier& 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 & DeclarationResult::InvalidDuplicateDeclaration, "Cannot declare a lexical variable twice: '", name.impl(), "'");
</span><span class="cx"> }
</span><span class="cx"> } else if (kind == DestructureToParameters) {
</span><del>- if (depth) {
- auto bindingResult = declareBoundParameter(&name);
- if (bindingResult == Scope::StrictBindingFailed && strictMode()) {
- semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode");
- if (m_lastFunctionName && name == *m_lastFunctionName)
- semanticFail("Cannot destructure to '", name.impl(), "' as it shadows the name of a strict mode function");
- semanticFailureDueToKeyword("bound parameter name");
- if (hasDeclaredParameter(name))
- semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared");
- semanticFail("Cannot bind to a parameter named '", name.impl(), "' in strict mode");
- }
- if (bindingResult == Scope::BindingFailed) {
- semanticFailureDueToKeyword("bound parameter name");
- if (hasDeclaredParameter(name))
- semanticFail("Cannot destructure to '", name.impl(), "' as it has already been declared");
- semanticFail("Cannot destructure to a parameter named '", name.impl(), "'");
- }
- } else {
- DeclarationResultMask declarationResult = declareParameter(&name);
- if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) {
- semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode");
- if (m_lastFunctionName && name == *m_lastFunctionName)
- semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function");
- semanticFailureDueToKeyword("parameter name");
- if (hasDeclaredParameter(name))
- semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared");
- semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode");
- }
- if (declarationResult & 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 = &name;
- }
</del><ins>+ DeclarationResultMask declarationResult = declareParameter(&name);
+ if ((declarationResult & DeclarationResult::InvalidStrictMode) && strictMode()) {
+ semanticFailIfTrue(isEvalOrArguments(&name), "Cannot destructure to a parameter name '", name.impl(), "' in strict mode");
+ if (m_lastFunctionName && name == *m_lastFunctionName)
+ semanticFail("Cannot declare a parameter named '", name.impl(), "' as it shadows the name of a strict mode function");
+ semanticFailureDueToKeyword("parameter name");
+ if (hasDeclaredParameter(name))
+ semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode as it has already been declared");
+ semanticFail("Cannot declare a parameter named '", name.impl(), "' in strict mode");
</ins><span class="cx"> }
</span><ins>+ if (declarationResult & 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 = &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("Expected a parameter pattern or a ')' in parameter list");
</span><span class="cx"> }
</span><span class="cx"> failIfTrue(match(LET) && (kind == DestructureToLet || kind == DestructureToConst), "Can't use 'let' as an identifier name for a LexicalDeclaration");
</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->impl());
- addResult.iterator->value.setIsVar(); // Treat destructuring parameters as "var"s.
- bool isValidStrictMode = addResult.isNewEntry && !isEval(m_vm, ident) && !isArgumentsIdent;
- m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode;
-
- if (isArgumentsIdent)
- m_shadowsArguments = true;
- if (!addResult.isNewEntry)
- return BindingFailed;
- return isValidStrictMode ? BindingSucceeded : StrictBindingFailed;
- }
-
</del><span class="cx"> void getUsedVariables(IdentifierSet& 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()->strictMode(); }
</span><span class="cx"> bool isValidStrictMode() { return currentScope()->isValidStrictMode(); }
</span><span class="cx"> DeclarationResultMask declareParameter(const Identifier* ident) { return currentScope()->declareParameter(ident); }
</span><del>- Scope::BindingResult declareBoundParameter(const Identifier* ident) { return currentScope()->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 <class TreeBuilder> TreeExpression parseVariableDeclarationList(TreeBuilder&, int& declarations, TreeDestructuringPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd, VarDeclarationListContext, DeclarationType, ExportType, bool& forLoopConstDoesNotHaveInitializer);
</span><span class="cx"> template <class TreeBuilder> TreeSourceElements parseArrowFunctionSingleExpressionBodySourceElements(TreeBuilder&);
</span><span class="cx"> template <class TreeBuilder> TreeExpression parseArrowFunctionExpression(TreeBuilder&);
</span><del>- template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern createBindingPattern(TreeBuilder&, DestructuringKind, ExportType, const Identifier&, int depth, JSToken, AssignmentContext, const Identifier** duplicateIdentifier);
</del><ins>+ template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern createBindingPattern(TreeBuilder&, DestructuringKind, ExportType, const Identifier&, JSToken, AssignmentContext, const Identifier** duplicateIdentifier);
</ins><span class="cx"> template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern createAssignmentElement(TreeBuilder&, TreeExpression&, const JSTextPosition&, const JSTextPosition&);
</span><span class="cx"> template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern parseBindingOrAssignmentElement(TreeBuilder& context, DestructuringKind, ExportType, const Identifier** duplicateIdentifier, bool* hasDestructuringPattern, AssignmentContext bindingContext, int depth);
</span><span class="cx"> template <class TreeBuilder> NEVER_INLINE TreeDestructuringPattern parseAssignmentElement(TreeBuilder& 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 < 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("function b(a = 20, a = 40) {}");
</span><span class="cx"> shouldThrowSyntaxError("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) {}");
</span></span></pre>
</div>
</div>
</body>
</html>