From 1055024894443916f00d3f369c2998e86b7f0e7f Mon Sep 17 00:00:00 2001 From: hufeng Date: Fri, 23 Sep 2022 11:54:44 +0800 Subject: [PATCH] Support [Function].toString with es2abc Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5SPVR?from=project-issue Signed-off-by: hufeng Change-Id: Id6c4a0588d7e0a08a66610fc4d3d0e200ed28794 --- es2panda/aot/options.cpp | 35 +++++++++++++++------- es2panda/compiler/core/compilerContext.cpp | 5 ++-- es2panda/compiler/core/compilerContext.h | 8 ++++- es2panda/compiler/core/compilerImpl.cpp | 2 +- es2panda/compiler/core/emitter/emitter.cpp | 7 +++++ es2panda/compiler/core/pandagen.cpp | 5 ++++ es2panda/compiler/core/pandagen.h | 1 + es2panda/es2panda.h | 1 + es2panda/ir/base/scriptFunction.h | 11 +++++++ es2panda/lexer/lexer.h | 6 ++-- 10 files changed, 64 insertions(+), 17 deletions(-) diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 10410d9491..449184ab61 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -136,18 +136,24 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg opDebugInfo("debug-info", false, "Compile with debug info"); panda::PandArg opDumpDebugInfo("dump-debug-info", false, "Dump debug info"); panda::PandArg opOptLevel("opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)"); + panda::PandArg opFunctionSourceCode("function-sourcecode", false, + "Record functions' source code to support [function].toString()"); panda::PandArg opFunctionThreadCount("function-threads", 0, "Number of worker threads to compile function"); panda::PandArg opFileThreadCount("file-threads", 0, "Number of worker threads to compile file"); panda::PandArg opSizeStat("dump-size-stat", false, "Dump size statistics"); panda::PandArg opDumpLiteralBuffer("dump-literal-buffer", false, "Dump literal buffer"); panda::PandArg outputFile("output", "", "Compiler binary output (.abc)"); panda::PandArg recordName("record-name", "", "Specify the record name"); + panda::PandArg sourceFile("source-file", "", + "specify the file path info recorded in generated abc"); + + // watch & debugger panda::PandArg debuggerEvaluateExpression("debugger-evaluate-expression", false, "evaluate expression in debugger mode"); panda::PandArg base64Input("base64Input", "", "base64 input of js content"); panda::PandArg base64Output("base64Output", false, "output panda file content as base64 to std out"); - panda::PandArg sourceFile("source-file", "", - "specify the file path info recorded in generated abc"); + + // mergeABC panda::PandArg outputProto("outputProto", "", "compiler proto serialize binary output (.proto)"); panda::PandArg opCacheFile("cache-file", "", "cache file for incremental compile"); panda::PandArg opNpmModuleEntryList("npm-module-entry-list", "", "entry list file for module compile"); @@ -165,38 +171,46 @@ bool Options::Parse(int argc, const char **argv) // tail arguments panda::PandArg inputFile("input", "", "input file"); + // parser argparser_->Add(&opHelp); + argparser_->Add(&inputExtension); argparser_->Add(&opModule); argparser_->Add(&opCommonjs); - argparser_->Add(&opDumpAst); argparser_->Add(&opParseOnly); argparser_->Add(&opEnableTypeCheck); + argparser_->Add(&opDumpAst); + + // compiler argparser_->Add(&opDumpAssembly); argparser_->Add(&opDebugInfo); argparser_->Add(&opDumpDebugInfo); - argparser_->Add(&debuggerEvaluateExpression); - argparser_->Add(&base64Input); - argparser_->Add(&base64Output); - argparser_->Add(&opOptLevel); + argparser_->Add(&opFunctionSourceCode); argparser_->Add(&opFunctionThreadCount); argparser_->Add(&opFileThreadCount); argparser_->Add(&opSizeStat); argparser_->Add(&opDumpLiteralBuffer); - - argparser_->Add(&inputExtension); argparser_->Add(&outputFile); - argparser_->Add(&sourceFile); argparser_->Add(&recordName); + argparser_->Add(&sourceFile); + + // watch & debugger + argparser_->Add(&debuggerEvaluateExpression); + argparser_->Add(&base64Input); + argparser_->Add(&base64Output); + + // mergeABC argparser_->Add(&outputProto); argparser_->Add(&opCacheFile); argparser_->Add(&opNpmModuleEntryList); argparser_->Add(&opMergeAbc); + // hotfix argparser_->Add(&opDumpSymbolTable); argparser_->Add(&opInputSymbolTable); argparser_->Add(&opGeneratePatch); + // version argparser_->Add(&bcVersion); argparser_->Add(&bcMinVersion); @@ -354,6 +368,7 @@ bool Options::Parse(int argc, const char **argv) compilerOptions_.functionThreadCount = functionThreadCount_; compilerOptions_.fileThreadCount = fileThreadCount_; compilerOptions_.output = compilerOutput_; + compilerOptions_.functionSource = opFunctionSourceCode.GetValue(); compilerOptions_.debugInfoSourceFile = sourceFile.GetValue(); compilerOptions_.optLevel = opOptLevel.GetValue(); compilerOptions_.sourceFiles = sourceFiles_; diff --git a/es2panda/compiler/core/compilerContext.cpp b/es2panda/compiler/core/compilerContext.cpp index 81434e630b..da8771ee59 100644 --- a/es2panda/compiler/core/compilerContext.cpp +++ b/es2panda/compiler/core/compilerContext.cpp @@ -20,9 +20,10 @@ namespace panda::es2panda::compiler { CompilerContext::CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, std::string sourceFile) + bool isMergeAbc, bool isRecordFunctionSourceCode, std::string sourceFile) : binder_(binder), isDebug_(isDebug), isDebuggerEvaluateExpressionMode_(isDebuggerEvaluateExpressionMode), - isMergeAbc_(isMergeAbc), sourceFile_(sourceFile), emitter_(std::make_unique(this)) + isMergeAbc_(isMergeAbc), isRecordFunctionSourceCode_(isRecordFunctionSourceCode), sourceFile_(sourceFile), + emitter_(std::make_unique(this)) { } diff --git a/es2panda/compiler/core/compilerContext.h b/es2panda/compiler/core/compilerContext.h index 6acc7b83fb..efb4e5ebf4 100644 --- a/es2panda/compiler/core/compilerContext.h +++ b/es2panda/compiler/core/compilerContext.h @@ -35,7 +35,7 @@ class Emitter; class CompilerContext { public: CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, std::string sourceFile); + bool isRecordFunctionSourceCode, bool isMergeAbc, std::string sourceFile); NO_COPY_SEMANTIC(CompilerContext); NO_MOVE_SEMANTIC(CompilerContext); ~CompilerContext() = default; @@ -81,6 +81,11 @@ public: return isMergeAbc_; } + bool IsRecordFunctionSourceCode() const + { + return isRecordFunctionSourceCode_; + } + std::string SourceFile() const { return sourceFile_; @@ -103,6 +108,7 @@ private: bool isDebug_; bool isDebuggerEvaluateExpressionMode_; bool isMergeAbc_; + bool isRecordFunctionSourceCode_; std::string sourceFile_; std::unique_ptr emitter_; util::Hotfix *hotfixHelper_ {nullptr}; diff --git a/es2panda/compiler/core/compilerImpl.cpp b/es2panda/compiler/core/compilerImpl.cpp index 5561346b97..8ab5a861f5 100644 --- a/es2panda/compiler/core/compilerImpl.cpp +++ b/es2panda/compiler/core/compilerImpl.cpp @@ -39,7 +39,7 @@ panda::pandasm::Program *CompilerImpl::Compile(parser::Program *program, const e const std::string &debugInfoSourceFile) { CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode, - options.mergeAbc, debugInfoSourceFile); + options.mergeAbc, options.functionSource, debugInfoSourceFile); if (hotfixHelper_ != nullptr) { context.AddHotfixHelper(hotfixHelper_); diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 53064fdc1d..e70a40c4bd 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -280,6 +281,12 @@ void FunctionEmitter::GenSourceFileDebugInfo() func_->source_file = std::string {pg_->Binder()->Program()->SourceFile()}; } + if (pg_->IsRecordFunctionSourceCode() && pg_->RootNode()->IsScriptFunction()) { + std::cerr << "function source: " << "\n" << pg_->RootNode()->AsScriptFunction()->SourceCode() << "\n"; + func_->source_code = + pg_->RootNode()->AsScriptFunction()->SourceCode().EscapeSymbol(); + } + if (!pg_->IsDebug()) { return; } diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index e56e28c72d..d435d8842a 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -63,6 +63,11 @@ bool PandaGen::isDebuggerEvaluateExpressionMode() const return context_->isDebuggerEvaluateExpressionMode(); } +bool PandaGen::IsRecordFunctionSourceCode() const +{ + return context_->IsRecordFunctionSourceCode(); +} + std::string PandaGen::SourceFile() const { return context_->SourceFile(); diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index 64fe2d6132..f6af822e7e 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -188,6 +188,7 @@ public: bool IsDebug() const; bool isDebuggerEvaluateExpressionMode() const; + bool IsRecordFunctionSourceCode() const; std::string SourceFile() const; uint32_t ParamCount() const; uint32_t FormalParametersCount() const; diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index ef72517fc3..e45a7740a9 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -73,6 +73,7 @@ struct CompilerOptions { bool dumpLiteralBuffer {false}; bool isDebuggerEvaluateExpressionMode {false}; bool mergeAbc {false}; + bool functionSource {false}; ScriptExtension extension {}; int fileThreadCount {0}; int functionThreadCount {0}; diff --git a/es2panda/ir/base/scriptFunction.h b/es2panda/ir/base/scriptFunction.h index 9eb543f4d0..2505356c97 100644 --- a/es2panda/ir/base/scriptFunction.h +++ b/es2panda/ir/base/scriptFunction.h @@ -64,6 +64,11 @@ public: return id_; } + util::StringView SourceCode() const + { + return source_; + } + const ArenaVector &Params() const { return params_; @@ -139,6 +144,11 @@ public: id_ = id; } + void SetSourceCode(util::StringView source) + { + source_ = source; + } + void SetAsExportDefault() { exportDefault_ = true; @@ -172,6 +182,7 @@ private: ir::ScriptFunctionFlags flags_; bool declare_; bool exportDefault_; + util::StringView source_; }; } // namespace panda::es2panda::ir diff --git a/es2panda/lexer/lexer.h b/es2panda/lexer/lexer.h index b830227025..4cf695b761 100644 --- a/es2panda/lexer/lexer.h +++ b/es2panda/lexer/lexer.h @@ -81,6 +81,9 @@ public: void BackwardToken(TokenType type, size_t offset); void ForwardToken(TokenType type, size_t offset); + util::StringView SourceView(const util::StringView::Iterator &begin, const util::StringView::Iterator &end) const; + util::StringView SourceView(size_t begin, size_t end) const; + char32_t Lookahead(); bool CheckArrow(); @@ -113,9 +116,6 @@ private: return pos_.iterator; } - util::StringView SourceView(const util::StringView::Iterator &begin, const util::StringView::Iterator &end) const; - util::StringView SourceView(size_t begin, size_t end) const; - void SkipWhiteSpaces(); void SkipSingleLineComment(); void SkipMultiLineComment(); -- Gitee