Skip to content

Commit 2e3efe1

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

File tree

3 files changed

+43
-47
lines changed

3 files changed

+43
-47
lines changed

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: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,48 @@ 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;
328-
while (scopeCtx) {
329-
if (!scopeCtx->m_parameters.size()) {
330-
scopeCtx->m_parameterUsed = 0xFFFF;
331-
}
321+
void checkUsedParameters(ASTScopeContext* scopeCtx)
322+
{
323+
AtomicStringTightVector& parameters = this->currentScopeContext->m_parameters;
332324

333-
if (scopeCtx->m_parameterUsed == 0xFFFF) {
334-
scopeCtx = scopeCtx->m_parentScope;
335-
continue;
325+
for (size_t i = 0; i < parameters.size(); i++) {
326+
AtomicString& name = parameters[i];
327+
for (size_t j = 0; j < scopeCtx->m_childBlockScopes.size(); j++) {
328+
ASTBlockContext& blockCtx = *scopeCtx->m_childBlockScopes[j];
329+
if (VectorUtil::findInVector(blockCtx.m_usingNames, name) != VectorUtil::invalidIndex) {
330+
this->currentScopeContext->m_parameterUsed |= (1 << i);
331+
break;
332+
} else if (scopeCtx->m_parameterUsed == DISABLE_PARAM_CHECK && VectorUtil::findInVector(blockCtx.m_usingNames, stringArguments) != VectorUtil::invalidIndex) {
333+
this->currentScopeContext->m_parameterUsed = DISABLE_PARAM_CHECK;
334+
return;
335+
}
336336
}
337+
}
337338

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);
344-
break;
345-
}
346-
}
339+
// check nested functions
340+
size_t childCount = scopeCtx->childCount();
341+
if (childCount > 0) {
342+
ASTScopeContext* child = scopeCtx->firstChild();
343+
for (size_t i = 0; i < childCount; i++) {
344+
checkUsedParameters(child);
345+
child = child->nextSibling();
347346
}
347+
}
348+
}
349+
#endif
348350

349-
scopeCtx = scopeCtx->m_parentScope;
351+
ASTScopeContext* popScopeContext(ASTScopeContext* lastPushedScopeContext)
352+
{
353+
#ifndef ESCARGOT_DEBUGGER
354+
if (LIKELY(this->currentScopeContext->m_parameterUsed != DISABLE_PARAM_CHECK)) {
355+
checkUsedParameters(this->currentScopeContext);
350356
}
351357
#endif
358+
auto ret = this->currentScopeContext;
359+
this->lastUsingName = AtomicString();
360+
this->lastPoppedScopeContext = ret;
361+
this->currentScopeContext = lastPushedScopeContext;
352362
return ret;
353363
}
354364

@@ -381,10 +391,6 @@ class Parser {
381391
parentContext->appendChild(this->currentScopeContext);
382392
}
383393

384-
#ifndef ESCARGOT_DEBUGGER
385-
this->currentScopeContext->m_parentScope = parentContext;
386-
#endif
387-
388394
return parentContext;
389395
}
390396

@@ -514,7 +520,7 @@ class Parser {
514520
#endif
515521
#ifndef ESCARGOT_DEBUGGER
516522
if (UNLIKELY(paramNames.size() > 16)) {
517-
this->currentScopeContext->m_parameterUsed = 0xFFFF;
523+
this->currentScopeContext->m_parameterUsed = DISABLE_PARAM_CHECK;
518524
}
519525
#endif
520526
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(paramNames.size());
@@ -1077,11 +1083,7 @@ class Parser {
10771083

10781084
#ifndef ESCARGOT_DEBUGGER
10791085
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-
}
1086+
this->currentScopeContext->m_parameterUsed = DISABLE_PARAM_CHECK;
10851087
}
10861088
#endif
10871089

@@ -1755,9 +1757,6 @@ class Parser {
17551757
this->currentScopeContext->m_parameterCount = 1;
17561758
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
17571759
this->currentScopeContext->m_parameters[0] = className;
1758-
#ifndef ESCARGOT_DEBUGGER
1759-
this->currentScopeContext->m_parameterUsed |= 1;
1760-
#endif
17611760
this->currentScopeContext->insertVarName(className, 0, true, true, true);
17621761
}
17631762

@@ -2611,7 +2610,7 @@ class Parser {
26112610
if (exprNode->isIdentifier() && exprNode->asIdentifier()->name() == escargotContext->staticStrings().eval) {
26122611
this->currentScopeContext->m_hasEval = true;
26132612
#ifndef ESCARGOT_DEBUGGER
2614-
this->currentScopeContext->m_parameterUsed = 0xFFFF;
2613+
this->currentScopeContext->m_parameterUsed = DISABLE_PARAM_CHECK;
26152614
#endif
26162615
}
26172616
exprNode = this->finalize(this->startNode(startToken), builder.createCallExpressionNode(exprNode, args, optional));
@@ -3584,9 +3583,6 @@ class Parser {
35843583
this->currentScopeContext->m_parameterCount = 1;
35853584
this->currentScopeContext->m_parameters.resizeWithUninitializedValues(1);
35863585
this->currentScopeContext->m_parameters[0] = paramName;
3587-
#ifndef ESCARGOT_DEBUGGER
3588-
this->currentScopeContext->m_parameterUsed |= 1;
3589-
#endif
35903586
this->currentScopeContext->insertVarName(paramName, 0, true, true, true);
35913587
}
35923588

@@ -5088,7 +5084,7 @@ class Parser {
50885084
switch (param->type()) {
50895085
case Identifier: {
50905086
#ifndef ESCARGOT_DEBUGGER
5091-
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || this->codeBlock->parameterUsed() == 0xFFFF) {
5087+
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || this->codeBlock->parameterUsed() == DISABLE_PARAM_CHECK) {
50925088
#endif
50935089
Node* init = this->finalize(node, builder.createInitializeParameterExpressionNode(param, paramIndex));
50945090
Node* statement = this->finalize(node, builder.createExpressionStatementNode(init));
@@ -5108,7 +5104,7 @@ class Parser {
51085104
}
51095105
case RestElement: {
51105106
#ifndef ESCARGOT_DEBUGGER
5111-
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || param->asRestElement()->argument()->type() != Identifier || this->codeBlock->parameterUsed() == 0xFFFF) {
5107+
if (this->codeBlock->parameterUsed() & (1 << paramIndex) || this->codeBlock->parameterUsed() == DISABLE_PARAM_CHECK || param->asRestElement()->argument()->type() != Identifier) {
51125108
#endif
51135109
Node* statement = this->finalize(node, builder.createExpressionStatementNode(param));
51145110
container->appendChild(statement);

src/parser/esprima_cpp/esprima.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ struct Error : public gc {
5555
};
5656

5757
#define ESPRIMA_RECURSIVE_LIMIT 1024
58+
#ifndef ESCARGOT_DEBUGGER
59+
#define DISABLE_PARAM_CHECK 0xFFFF
60+
#endif
5861

5962
ProgramNode* parseProgram(::Escargot::Context* ctx, StringView source, ASTClassInfo* outerClassInfo,
6063
bool isModule, bool strictFromOutside, bool inWith, bool allowSuperCallFromOutside,

0 commit comments

Comments
 (0)