diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 80da256ea0771cff34142b2c037396d5e33ce787..f58ba747b64f0be0bbeca3d05fd5b524c4e042c2 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3123,6 +3123,9 @@ checker::Type *ETSAnalyzer::Check(ir::ImportNamespaceSpecifier *st) const ES2PANDA_UNREACHABLE(); } + auto topScopeCtx = + varbinder::TopScopeContext(checker->VarBinder(), importDecl->Parent()->AsETSModule()->Scope()->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 77e246ead2eb2bd330185b66324b0776b12b25f4..9cbdb705276c3778031bdbfe2876f3ad7fe192d7 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1857,12 +1857,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