From a55401a1ad619d9d1585456a17e1e5d9eb7b98df Mon Sep 17 00:00:00 2001 From: Pogosov Date: Wed, 3 Apr 2024 15:29:36 +0300 Subject: [PATCH] add support for global constants Signed-off-by: Pogosov --- ets2panda/declgen_ets2ts/declgenEts2Ts.cpp | 28 ++++++++++++++++++++-- ets2panda/declgen_ets2ts/declgenEts2Ts.h | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index d46d1c6fac..bbea99ee9d 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -66,6 +66,8 @@ void TSDeclGen::Generate() license << " */\n\n"; Out(license.str()); Out("declare const exports: any;"); + OutEndl(); + Out("let ETSGLOBAL: any = (globalThis as any).Panda.getClass('LETSGLOBAL;');"); OutEndl(2U); for (auto *globalStatement : program_->Ast()->Statements()) { @@ -556,8 +558,7 @@ void TSDeclGen::GenMethodDeclaration(const ir::MethodDefinition *methodDef) OutEndl(); if (state_.inGlobalClass) { - Out("(", methodName, " as any) = (globalThis as any).Panda.getFunction('", state_.currentClassDescriptor, - "', '", methodName, "');"); + Out("(", methodName, " as any) = ETSGLOBAL.", methodName, ";"); OutEndl(); GenExports(methodName); if (methodName == compiler::Signatures::INIT_METHOD) { @@ -570,6 +571,7 @@ void TSDeclGen::GenMethodDeclaration(const ir::MethodDefinition *methodDef) void TSDeclGen::GenPropDeclaration(const ir::ClassProperty *classProp) { if (state_.inGlobalClass) { + GenGlobalVarDeclaration(classProp); return; } @@ -589,6 +591,28 @@ void TSDeclGen::GenPropDeclaration(const ir::ClassProperty *classProp) OutEndl(); } +void TSDeclGen::GenGlobalVarDeclaration(const ir::ClassProperty *globalVar) +{ + if (!globalVar->IsExported() && !globalVar->IsDefaultExported()) { + return; + } + + const auto varName = GetKeyName(globalVar->Key()); + DebugPrint("GenGlobalVarDeclaration: " + varName); + if (!globalVar->IsConst()) { + Warning("Not constant global variables are not supported, variable \"" + varName + "\" was skipped"); + return; + } + + Out("const ", varName, ": "); + GenType(globalVar->TsType()); + Out(" = ETSGLOBAL.", varName, ';'); + OutEndl(); + + GenExports(varName); + OutEndl(); +} + bool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program, const std::string &outPath) { diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 1934c19cab..984f1cd316 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -61,6 +61,7 @@ private: void GenClassDeclaration(const ir::ClassDeclaration *classDecl); void GenMethodDeclaration(const ir::MethodDefinition *methodDef); void GenPropDeclaration(const ir::ClassProperty *classProp); + void GenGlobalVarDeclaration(const ir::ClassProperty *globalVar); void GenLiteral(const ir::Literal *literal); template -- Gitee