diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index ebc28058a269ad1acb83a7c8d12427806d85110b..13dc2813e56e4c0c4252843a517be58ca355cdc0 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3154,6 +3154,11 @@ checker::Type *ETSAnalyzer::Check(ir::ImportNamespaceSpecifier *st) const ES2PANDA_UNREACHABLE(); } + auto topScopeCtx = varbinder::TopScopeContext(checker->VarBinder(), + importDecl->Parent() != nullptr + ? importDecl->Parent()->AsETSModule()->Scope()->AsGlobalScope() + : checker->VarBinder()->GetScope()->AsGlobalScope()); + if (importDecl->IsPureDynamic()) { auto *type = checker->GetImportSpecifierObjectType(importDecl, st->Local()->AsIdentifier())->AsETSObjectType(); checker->SetrModuleObjectTsType(st->Local(), type); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 313c539f8257d04f9892e4a9076a387a11c857a2..07a792c63281942be0a06ce6ddfe43998676f120 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1877,12 +1877,11 @@ void ETSChecker::SetPropertiesForModuleObject(checker::ETSObjectType *moduleObjT RemoveStatus(CheckerStatus::IN_EXTERNAL); } auto savedProgram = Program(); + auto topScopeCtx = varbinder::TopScopeContext(VarBinder(), program->GlobalScope()); VarBinder()->AsETSBinder()->SetProgram(program); - VarBinder()->AsETSBinder()->ResetTopScope(program->GlobalScope()); program->SetASTChecked(); program->Ast()->Check(this); VarBinder()->AsETSBinder()->SetProgram(savedProgram); - VarBinder()->AsETSBinder()->ResetTopScope(savedProgram->GlobalScope()); } BindingsModuleObjectAddProperty( diff --git a/ets2panda/varbinder/varbinder.h b/ets2panda/varbinder/varbinder.h index ff674f639bedec5df0df93fdecf5d21aab8b3379..e5e215d7430a8c5770d0b363651e8c5cdd35ec28 100644 --- a/ets2panda/varbinder/varbinder.h +++ b/ets2panda/varbinder/varbinder.h @@ -17,6 +17,7 @@ #define ES2PANDA_VARBINDER_VARBINDER_H #include "lexer/token/sourceLocation.h" +#include "scope.h" #include "varbinder/scope.h" namespace ark::es2panda::parser { @@ -176,6 +177,7 @@ public: template friend class LexicalScope; + friend class TopScopeContext; [[nodiscard]] ArenaAllocator *Allocator() const noexcept { @@ -430,5 +432,33 @@ std::tuple VarBinder::NewVarDecl(const lexer::Source ES2PANDA_ASSERT(var != nullptr); return {decl, var}; } + +class TopScopeContext { +public: + TopScopeContext(VarBinder *varbinder, GlobalScope *globalScope) + : varbinder_(varbinder), + prevTopscope_(varbinder->TopScope()), + prevScope_(varbinder->GetScope()), + prevVarScope_(varbinder->VarScope()) + { + varbinder->topScope_ = globalScope; + varbinder->scope_ = globalScope; + varbinder->varScope_ = globalScope; + } + + ~TopScopeContext() + { + varbinder_->topScope_ = prevTopscope_; + varbinder_->scope_ = prevScope_; + varbinder_->varScope_ = prevVarScope_; + } + +private: + VarBinder *varbinder_ {}; + GlobalScope *prevTopscope_ {}; + Scope *prevScope_ {}; + VariableScope *prevVarScope_ {}; +}; + } // namespace ark::es2panda::varbinder #endif