diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 10410d94915a965db6615b908386a6194691aea6..449184ab61a6d47baabdfeafdd81cb942e1b2656 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 81434e630b0a4cdccc30fa37dc54886355150f91..da8771ee59ff2b911b363d406aaf54d1c75a57f2 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 6acc7b83fb53a24cf1004237abdbab7734ca6349..efb4e5ebf4cdfac23af6bc1ddc8527ad7f6939aa 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 5561346b974793c47b68f0e8599a6c22d805f6e7..8ab5a861f586326fa1400a54e50f4dad833f764c 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 53064fdc1dea20e7c450060e75a09504cadcdc15..e70a40c4bdbb46a1ee3aeab7fd51393c286ad617 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 e56e28c72dc42d6fc0fdc2f07c3049f0955913d6..d435d8842af2f7faa7e7e2e3d98a5f8182b6a42e 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 64fe2d6132bd853e72f05f55102588d262f135a5..f6af822e7e294fe0a35160ac3d39dcd0dbd62803 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 ef72517fc345d13ab85fdcd41b5696aac7552292..e45a7740a9de98133584fd4edaaac09ddd4f7ee7 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 9eb543f4d0935305ceec061f210805ac0593e45d..2505356c970df98efd417fb629a640828a73c2e0 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 b8302270254e34d7535b8cc14df7819cea242c25..4cf695b7611ff14bb9cd8c04cc84c31e03323e10 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();