Skip to content

Commit 8b9aad7

Browse files
committed
Change bottom-up to top-down check
1 parent 72bba12 commit 8b9aad7

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

src/parser/CodeBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ class InterpretedCodeBlock : public CodeBlock {
990990
#endif
991991

992992
#ifndef ESCARGOT_DEBUGGER
993-
uint16_t m_parameterUsed : 16;
993+
uint16_t m_parameterUsed : 16; // 0xFFFF means all parameters are used or function has more than 16 parameters
994994
#endif
995995

996996
uint16_t m_functionLength : 16;

src/parser/ast/ASTContext.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ struct ASTScopeContext {
322322
AtomicStringTightVector m_parameters;
323323
AtomicString m_functionName;
324324
#ifndef ESCARGOT_DEBUGGER
325-
uint16_t m_parameterUsed : 16; // 0xFFFF means all parameters are used or function has more than 16 parameters
326-
327-
ASTScopeContext *m_parentScope;
325+
uint16_t m_parameterUsed : 16;
328326
#endif
329327
ASTScopeContext *m_firstChild;
330328
ASTScopeContext *m_nextSibling;
@@ -709,7 +707,6 @@ struct ASTScopeContext {
709707
, m_classPrivateNames(nullptr)
710708
#ifndef ESCARGOT_DEBUGGER
711709
, m_parameterUsed(0)
712-
, m_parentScope(nullptr)
713710
#endif
714711
, m_firstChild(nullptr)
715712
, m_nextSibling(nullptr)

src/parser/esprima_cpp/esprima.cpp

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,50 @@ class Parser {
317317
}
318318
}
319319

320-
ASTScopeContext* popScopeContext(ASTScopeContext* lastPushedScopeContext)
321-
{
322-
auto ret = this->currentScopeContext;
323-
this->lastUsingName = AtomicString();
324-
this->lastPoppedScopeContext = ret;
325-
this->currentScopeContext = lastPushedScopeContext;
326320
#ifndef ESCARGOT_DEBUGGER
327-
ASTScopeContext* scopeCtx = ret;
321+
void setParameterUsedValue(ASTScopeContext* scopeCtx)
322+
{
328323
while (scopeCtx) {
329-
if (!scopeCtx->m_parameters.size()) {
330-
scopeCtx->m_parameterUsed = 0xFFFF;
331-
}
332-
333-
if (scopeCtx->m_parameterUsed == 0xFFFF) {
334-
scopeCtx = scopeCtx->m_parentScope;
335-
continue;
336-
}
324+
for (size_t i = 0; i < this->currentScopeContext->m_parameters.size(); i++) {
325+
if (this->currentScopeContext->m_parameterUsed == 0xFFFF) {
326+
break;
327+
}
337328

338-
for (size_t i = 0; i < scopeCtx->m_parameters.size(); i++) {
339-
AtomicString paramName = scopeCtx->m_parameters[i];
340-
for (size_t j = 0; j < ret->m_childBlockScopes.size(); j++) {
341-
ASTBlockContext* block = ret->m_childBlockScopes[j];
342-
if (VectorUtil::findInVector(block->m_usingNames, paramName) != VectorUtil::invalidIndex) {
343-
scopeCtx->m_parameterUsed |= (1 << i);
329+
AtomicString name = this->currentScopeContext->m_parameters[i];
330+
for (size_t j = 0; j < scopeCtx->m_childBlockScopes.size(); j++) {
331+
ASTBlockContext* blockCtx = scopeCtx->m_childBlockScopes[j];
332+
if (VectorUtil::findInVector(blockCtx->m_usingNames, name) != VectorUtil::invalidIndex) {
333+
this->currentScopeContext->m_parameterUsed |= (1 << i);
334+
break;
335+
} else if (UNLIKELY(VectorUtil::findInVector(blockCtx->m_usingNames, stringArguments) != VectorUtil::invalidIndex)) {
336+
this->currentScopeContext->m_parameterUsed = 0xFFFF;
344337
break;
345338
}
346339
}
347340
}
348341

349-
scopeCtx = scopeCtx->m_parentScope;
342+
if (scopeCtx->firstChild()) {
343+
setParameterUsedValue(scopeCtx->firstChild());
344+
}
345+
346+
if (scopeCtx != this->currentScopeContext) {
347+
scopeCtx = scopeCtx->nextSibling();
348+
} else {
349+
break;
350+
}
350351
}
352+
}
353+
#endif
354+
355+
ASTScopeContext* popScopeContext(ASTScopeContext* lastPushedScopeContext)
356+
{
357+
#ifndef ESCARGOT_DEBUGGER
358+
setParameterUsedValue(this->currentScopeContext);
351359
#endif
360+
auto ret = this->currentScopeContext;
361+
this->lastUsingName = AtomicString();
362+
this->lastPoppedScopeContext = ret;
363+
this->currentScopeContext = lastPushedScopeContext;
352364
return ret;
353365
}
354366

@@ -381,10 +393,6 @@ class Parser {
381393
parentContext->appendChild(this->currentScopeContext);
382394
}
383395

384-
#ifndef ESCARGOT_DEBUGGER
385-
this->currentScopeContext->m_parentScope = parentContext;
386-
#endif
387-
388396
return parentContext;
389397
}
390398

@@ -1075,16 +1083,6 @@ class Parser {
10751083
}
10761084
}
10771085

1078-
#ifndef ESCARGOT_DEBUGGER
1079-
if (UNLIKELY(ret->asIdentifier()->name() == this->stringArguments)) {
1080-
ASTScopeContext* scopeCtx = this->currentScopeContext;
1081-
while (scopeCtx) {
1082-
scopeCtx->m_parameterUsed = 0xFFFF;
1083-
scopeCtx = scopeCtx->m_parentScope;
1084-
}
1085-
}
1086-
#endif
1087-
10881086
if (this->trackUsingNames) {
10891087
this->insertUsingName(ret->asIdentifier()->name());
10901088
}
@@ -1755,9 +1753,6 @@ class Parser {
17551753
this->currentScopeContext->m_parameterCount = 1;
17561754
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
17571755
this->currentScopeContext->m_parameters[0] = className;
1758-
#ifndef ESCARGOT_DEBUGGER
1759-
this->currentScopeContext->m_parameterUsed |= 1;
1760-
#endif
17611756
this->currentScopeContext->insertVarName(className, 0, true, true, true);
17621757
}
17631758

@@ -3584,9 +3579,6 @@ class Parser {
35843579
this->currentScopeContext->m_parameterCount = 1;
35853580
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
35863581
this->currentScopeContext->m_parameters[0] = paramName;
3587-
#ifndef ESCARGOT_DEBUGGER
3588-
this->currentScopeContext->m_parameterUsed |= 1;
3589-
#endif
35903582
this->currentScopeContext->insertVarName(paramName, 0, true, true, true);
35913583
}
35923584

@@ -5108,7 +5100,7 @@ class Parser {
51085100
}
51095101
case RestElement: {
51105102
#ifndef ESCARGOT_DEBUGGER
5111-
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || param->asRestElement()->argument()->type() != Identifier || this->codeBlock->parameterUsed() == 0xFFFF) {
5103+
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || this->codeBlock->parameterUsed() == 0xFFFF || param->asRestElement()->argument()->type() != Identifier) {
51125104
#endif
51135105
Node* statement = this->finalize(node, builder.createExpressionStatementNode(param));
51145106
container->appendChild(statement);

0 commit comments

Comments
 (0)