Skip to content

Commit e26a586

Browse files
committed
[MERGE #5400 @akroshg] Fix for destructuring declaration completion result.
Merge pull request #5400 from akroshg:fix5196 The detstructuring declaration node is a normal assignment node, so it was returing the RHS as completion result. In order to fix that marking that node as Destructuring declaration node and check that before emitting the return bytecode.
2 parents ad4b203 + afed5a4 commit e26a586

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

lib/Parser/Parse.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9156,6 +9156,7 @@ ParseNodePtr Parser::ParseVariableDeclaration(
91569156
if (pnodeThis != nullptr)
91579157
{
91589158
pnodeThis->ichMin = ichMin;
9159+
pnodeThis->SetIsPatternDeclaration();
91599160
}
91609161
}
91619162
else

lib/Parser/ptree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ParseNode::ParseNode(OpCode nop, charcount_t ichMin, charcount_t ichLim)
1212
this->isUsed = true;
1313
this->notEscapedUse = false;
1414
this->isInList = false;
15+
this->isPatternDeclaration = false;
1516
this->isCallApplyTargetLoad = false;
1617
this->ichMin = ichMin;
1718
this->ichLim = ichLim;

lib/Parser/ptree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ class ParseNode
210210
bool IsCallApplyTargetLoad() { return isCallApplyTargetLoad; }
211211
void SetIsCallApplyTargetLoad() { isCallApplyTargetLoad = true; }
212212

213+
bool IsPatternDeclaration() { return isPatternDeclaration; }
214+
void SetIsPatternDeclaration() { isPatternDeclaration = true; }
215+
213216
bool IsUserIdentifier();
214217

215218
bool IsVarLetOrConst() const
@@ -241,6 +244,9 @@ class ParseNode
241244
bool notEscapedUse : 1; // Currently, only used by child of knopComma
242245
bool isCallApplyTargetLoad : 1;
243246

247+
// Use by bytecodegen to identify the current node is a destructuring pattern declaration node.
248+
bool isPatternDeclaration : 1;
249+
244250
public:
245251
ushort grfpn;
246252

lib/Runtime/ByteCode/ByteCodeEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@ void ByteCodeGenerator::EmitGlobalBody(FuncInfo *funcInfo)
24382438
// return value.
24392439
ParseNode *pnode = funcInfo->root->pnodeBody;
24402440
ParseNode *pnodeLastVal = funcInfo->root->AsParseNodeProg()->pnodeLastValStmt;
2441-
if (pnodeLastVal == nullptr)
2441+
if (pnodeLastVal == nullptr || pnodeLastVal->IsPatternDeclaration())
24422442
{
24432443
// We're not guaranteed to compute any values, so fix up the return register at the top
24442444
// in case.
@@ -11716,7 +11716,7 @@ void Emit(ParseNode *pnode, ByteCodeGenerator *byteCodeGenerator, FuncInfo *func
1171611716
break;
1171711717
}
1171811718

11719-
if (fReturnValue && IsExpressionStatement(pnode, byteCodeGenerator->GetScriptContext()))
11719+
if (fReturnValue && IsExpressionStatement(pnode, byteCodeGenerator->GetScriptContext()) && !pnode->IsPatternDeclaration())
1172011720
{
1172111721
// If this statement may produce the global function's return value, copy its result to the return register.
1172211722
// fReturnValue implies global function, which implies that "return" is a parse error.

test/Bugs/misc_bugs.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,17 @@ var tests = [
154154

155155
var obj2 = {__proto__ : p}; // This should not call the getPrototypeOf
156156
}
157-
}
157+
},
158+
{
159+
name: "Destructuring declaration should return undefined",
160+
body: function () {
161+
assert.areEqual(undefined, eval("var {x} = {};"));
162+
assert.areEqual(undefined, eval("let {x,y} = {};"));
163+
assert.areEqual(undefined, eval("const [z] = [];"));
164+
assert.areEqual(undefined, eval("let {x} = {}, y = 1, {z} = {};"));
165+
assert.areEqual([1], eval("let {x} = {}; [x] = [1]"));
166+
}
167+
},
158168

159169
];
160170

0 commit comments

Comments
 (0)