diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index 2204455776d91abb5132b49546d2734c266ea558..47b959830925cb2ad4928d36cbb8d4ce06cf330a 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -34,11 +34,36 @@ #include "ir/ts/tsInterfaceBody.h" #include "ir/ts/tsTypeAliasDeclaration.h" #include "ir/ts/tsTypeParameter.h" +#include "compiler/lowering/util.h" + +#include "generated/es2panda_lib/es2panda_lib_include.inc" #define DEBUG_PRINT 0 namespace ark::es2panda::declgen_ets2ts { +char *StringViewToCString(ArenaAllocator *allocator, std::string_view const utf8) +{ + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic, readability-simplify-subscript-expr) + if (!utf8.empty() && (utf8.back() == '\0')) { + // Avoid superfluous allocation. + return const_cast(utf8.data()); + } + char *res = reinterpret_cast(allocator->Alloc(utf8.size() + 1)); + if (!utf8.empty()) { + [[maybe_unused]] auto err = memmove_s(res, utf8.size() + 1, utf8.cbegin(), utf8.size()); + ES2PANDA_ASSERT(err == EOK); + } + res[utf8.size()] = '\0'; + return res; + // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic, readability-simplify-subscript-expr) +} + +__attribute__((unused)) char *StringViewToCString(ArenaAllocator *allocator, util::StringView const sv) +{ + return StringViewToCString(allocator, sv.Utf8()); +} + static void DebugPrint([[maybe_unused]] const std::string &msg) { #if DEBUG_PRINT @@ -46,6 +71,8 @@ static void DebugPrint([[maybe_unused]] const std::string &msg) #endif } +es2panda_Context * TSDeclGen::ctx = nullptr; + bool TSDeclGen::Generate() { if (!GenGlobalDescriptor()) { @@ -261,16 +288,33 @@ void TSDeclGen::AddObjectDependencies(const util::StringView &typeName, const st void TSDeclGen::GenDeclarations() { + // const auto *impl = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + std::cout << "1 TSDeclGen::ctx" << TSDeclGen::ctx << std::endl; for (auto *globalStatement : program_->Ast()->Statements()) { ResetState(); ResetClassNode(); - if (globalStatement->IsClassDeclaration()) { + const auto jsdoc = compiler::JsdocStringFromDeclaration(globalStatement); + std::cout << "1" << jsdoc << std::endl; + std::cout << "2" << jsdoc.Utf8() << std::endl; + if (TSDeclGen::ctx) { + auto *context = reinterpret_cast(TSDeclGen::ctx); + std::cout << "3" << StringViewToCString(context->allocator, jsdoc) << std::endl; + //std::cout << "3" << impl->JsdocStringFromDeclaration(TSDeclGen::ctx, reinterpret_cast(globalStatement)) << std::endl; + } + if (jsdoc.Utf8().find(nonInteropFlag) != std::string_view::npos) { + std::cout << "skip 1" << std::endl; + continue; + } else if (globalStatement->IsClassDeclaration()) { + std::cout << "mrh GenClassDeclaration" << std::endl; GenClassDeclaration(globalStatement->AsClassDeclaration()); } else if (globalStatement->IsTSInterfaceDeclaration()) { + std::cout << "mrh GenInterfaceDeclaration" << std::endl; GenInterfaceDeclaration(globalStatement->AsTSInterfaceDeclaration()); } else if (globalStatement->IsTSTypeAliasDeclaration()) { + std::cout << "mrh GenTypeAliasDeclaration" << std::endl; GenTypeAliasDeclaration(globalStatement->AsTSTypeAliasDeclaration()); } else if (globalStatement->IsETSReExportDeclaration()) { + std::cout << "mrh GenReExportDeclaration" << std::endl; GenReExportDeclaration(globalStatement->AsETSReExportDeclaration()); } } @@ -1862,10 +1906,23 @@ void TSDeclGen::ProcessMethodsFromInterfaces(std::unordered_set &pr void TSDeclGen::ProcessClassBody(const ir::ClassDefinition *classDef) { + std::cout << "2 TSDeclGen::ctx" << TSDeclGen::ctx << std::endl; + //const auto *impl = es2panda_GetImpl(ES2PANDA_LIB_VERSION); state_.inClass = true; std::unordered_set processedMethods; - for (const auto *prop : classDef->Body()) { - if (classDef->IsEnumTransformed()) { + for (auto *prop : classDef->Body()) { + const auto jsdoc = compiler::JsdocStringFromDeclaration(prop); + std::cout << "4" << jsdoc.Utf8() << std::endl; + std::cout << "5" << jsdoc << std::endl; + if (TSDeclGen::ctx) { + auto *context = reinterpret_cast(TSDeclGen::ctx); + std::cout << "6" << StringViewToCString(context->allocator, jsdoc) << std::endl; + //std::cout << "6" << impl->JsdocStringFromDeclaration(TSDeclGen::ctx, reinterpret_cast(prop)) << std::endl; + } + if (jsdoc.Utf8().find(nonInteropFlag) != std::string_view::npos) { + std::cout << "skip 2" << std::endl; + continue; + } else if (classDef->IsEnumTransformed()) { if (prop->IsClassProperty()) { state_.inEnum = true; GenPropDeclaration(prop->AsClassProperty()); @@ -2104,11 +2161,11 @@ void TSDeclGen::EmitPropGlueCode(const ir::ClassProperty *classProp, const std:: const bool isConst = classProp->IsConst(); const bool isDefaultExported = classProp->IsDefaultExported(); if (isDefaultExported) { - OutTs(isConst ? "const " : "let ", propName, propAccess); + OutTs(isConst ? "const " : "letlet ", propName, propAccess); OutEndlTs(); OutTs("export default ", propName, ";"); } else { - OutTs(isConst ? "export const " : "export let ", propName, propAccess); + OutTs(isConst ? "export const " : "export letlet ", propName, propAccess); } OutEndlTs(); } diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index 94471dab3b11bf0c4893662444b3f65d517b41bd..c0c04abe9ffd008e6015330069b6439690aaddcc 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -26,6 +26,8 @@ namespace ark::es2panda::declgen_ets2ts { +constexpr const char* nonInteropFlag = "@noninterop"; + struct DeclgenOptions { bool exportAll = false; bool isolated = false; @@ -88,6 +90,8 @@ public: static constexpr std::string_view INDENT = " "; + static es2panda_Context *ctx; + private: void LogError(const diagnostic::DiagnosticKind &kind, const util::DiagnosticMessageParams ¶ms, const lexer::SourcePosition &pos); diff --git a/ets2panda/declgen_ets2ts/main.cpp b/ets2panda/declgen_ets2ts/main.cpp index a13937b367dffa3a1c49c3d7a97040eb9d6177c0..df836e58db9a93fc1220b2a64736a7f33d471105 100644 --- a/ets2panda/declgen_ets2ts/main.cpp +++ b/ets2panda/declgen_ets2ts/main.cpp @@ -92,7 +92,8 @@ static int Run(int argc, const char **argv) auto parserInputCStr = cfgImpl->options->CStrParserInputContents().first; es2panda_Context *ctx = impl->CreateContextFromString(cfg, parserInputCStr, cfgImpl->options->SourceFileName().c_str()); - + TSDeclGen::ctx = ctx; + std::cout << "3 TSDeclGen::ctx" << TSDeclGen::ctx << std::endl; auto *ctxImpl = reinterpret_cast(ctx); auto *checker = reinterpret_cast(ctxImpl->GetChecker()); @@ -102,7 +103,8 @@ static int Run(int argc, const char **argv) if (!GenerateTsDeclarations(checker, ctxImpl->parserProgram, declgenOptions)) { res = 1; } - + TSDeclGen::ctx = nullptr; + std::cout << "4 TSDeclGen::ctx" << TSDeclGen::ctx << std::endl; impl->DestroyContext(ctx); impl->DestroyConfig(cfg); delete[] newArgv; diff --git a/ets2panda/parser/JsdocHelper.cpp b/ets2panda/parser/JsdocHelper.cpp index 1d9f34cb3344cbad0c035d36fdb67db1bb0c386a..6d35b29e6247a0508a18ffc42166749c376a0414 100644 --- a/ets2panda/parser/JsdocHelper.cpp +++ b/ets2panda/parser/JsdocHelper.cpp @@ -147,6 +147,7 @@ util::StringView JsdocHelper::GetJsdocBackward() size_t jsdocEndPos = Iterator().Index() + COLLECT_CURRENT_POS; size_t backwardPos = jsdocEndPos; auto currentSourceView = SourceView(START_POS, jsdocEndPos); + std::cout << "currentSourceView" << currentSourceView << jsdocEndPos << backwardPos << std::endl; while (currentSourceView.EndsWith(JSDOC_END)) { BackwardAndSkipSpace(JSDOC_END.length()); BackWardUntilJsdocStart();