From fcf7144c6686e7cb6e04e5f3facb0a252b14244a Mon Sep 17 00:00:00 2001 From: dong Date: Thu, 27 Jul 2023 11:03:52 +0800 Subject: [PATCH] Add the argument of compile command Issue: I7ODQS Signed-off-by: dong Change-Id: I64678d5204dad80fd2795db8efb3d178a297e885 --- es2panda/aot/options.cpp | 4 ++++ es2panda/compiler/core/compilerContext.cpp | 10 +++++----- es2panda/compiler/core/compilerContext.h | 13 +++++++++++-- es2panda/compiler/core/compilerImpl.cpp | 4 ++-- es2panda/compiler/core/emitter/emitter.cpp | 7 +++---- es2panda/es2panda.cpp | 2 +- es2panda/es2panda.h | 1 + .../toString-without-record-source-expected.txt | 1 + .../with-off/toString-without-record-source.js | 4 ++++ .../toString-with-record-source-expected.txt | 2 ++ .../with-on/toString-with-record-source.js | 4 ++++ es2panda/test/runner.py | 2 ++ 12 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 es2panda/test/compiler/recordsource/with-off/toString-without-record-source-expected.txt create mode 100644 es2panda/test/compiler/recordsource/with-off/toString-without-record-source.js create mode 100644 es2panda/test/compiler/recordsource/with-on/toString-with-record-source-expected.txt create mode 100644 es2panda/test/compiler/recordsource/with-on/toString-with-record-source.js diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 95085c90674..6fd8a714695 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -199,6 +199,8 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg opDumpTransformedAst("dump-transformed-ast", false, "Dump the parsed AST after transform"); panda::PandArg opCheckTransformedAstStructure("check-transformed-ast-structure", false, "Check the AST structure after transform"); + panda::PandArg opRecordSource("record-source", false, "Record all functions' source codes to support the "\ + "using of [function].toString()"); // type extractor panda::PandArg opTypeExtractor("type-extractor", false, "Enable type extractor for typescript"); @@ -249,6 +251,7 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&opDumpAst); argparser_->Add(&opDumpTransformedAst); argparser_->Add(&opCheckTransformedAstStructure); + argparser_->Add(&opRecordSource); argparser_->Add(&opParseOnly); argparser_->Add(&opEnableTypeCheck); argparser_->Add(&opTypeExtractor); @@ -433,6 +436,7 @@ bool Options::Parse(int argc, const char **argv) options_ |= OptionFlags::SIZE_STAT; } + compilerOptions_.recordSource = opRecordSource.GetValue(); compilerOptions_.dumpAsm = opDumpAssembly.GetValue(); compilerOptions_.dumpAst = opDumpAst.GetValue(); compilerOptions_.dumpTransformedAst = opDumpTransformedAst.GetValue(); diff --git a/es2panda/compiler/core/compilerContext.cpp b/es2panda/compiler/core/compilerContext.cpp index 78edb8abbd9..b55778a32de 100644 --- a/es2panda/compiler/core/compilerContext.cpp +++ b/es2panda/compiler/core/compilerContext.cpp @@ -21,13 +21,13 @@ namespace panda::es2panda::compiler { CompilerContext::CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, bool isTypeExtractorEnabled, bool isJsonInputFile, - std::string sourceFile, std::string pkgName, util::StringView recordName, - util::PatchFix *patchFixHelper) + bool isMergeAbc, bool isTypeExtractorEnabled, bool isJsonInputFile, + bool isRecordSource, std::string sourceFile, std::string pkgName, + util::StringView recordName, util::PatchFix *patchFixHelper) : binder_(binder), isDebug_(isDebug), isDebuggerEvaluateExpressionMode_(isDebuggerEvaluateExpressionMode), isMergeAbc_(isMergeAbc), isTypeExtractorEnabled_(isTypeExtractorEnabled), isJsonInputFile_(isJsonInputFile), - sourceFile_(sourceFile), pkgName_(pkgName), recordName_(recordName), patchFixHelper_(patchFixHelper), - emitter_(std::make_unique(this)) + isRecordSource_(isRecordSource), sourceFile_(sourceFile), pkgName_(pkgName), recordName_(recordName), + patchFixHelper_(patchFixHelper), emitter_(std::make_unique(this)) { } diff --git a/es2panda/compiler/core/compilerContext.h b/es2panda/compiler/core/compilerContext.h index 0581f4c2d1b..f3d3198e5bf 100644 --- a/es2panda/compiler/core/compilerContext.h +++ b/es2panda/compiler/core/compilerContext.h @@ -44,8 +44,10 @@ class Emitter; class CompilerContext { public: CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, bool isTypeExtractorEnabled, bool isJsonInputFile, std::string sourceFile, - std::string pkgName, util::StringView recordName, util::PatchFix *patchFixHelper); + bool isMergeAbc, bool isTypeExtractorEnabled, bool isJsonInputFile, bool isRecordSource, + std::string sourceFile, std::string pkgName, util::StringView recordName, + util::PatchFix *patchFixHelper); + NO_COPY_SEMANTIC(CompilerContext); NO_MOVE_SEMANTIC(CompilerContext); ~CompilerContext() = default; @@ -134,6 +136,12 @@ public: return isJsonInputFile_; } + bool IsRecordSource() const + { + return isRecordSource_; +; + } + private: binder::Binder *binder_; int32_t literalBufferIdx_ {0}; @@ -145,6 +153,7 @@ private: bool isTypeExtractorEnabled_; // true when input file is json file bool isJsonInputFile_; + bool isRecordSource_; extractor::TypeRecorder *recorder_ {}; extractor::TypeExtractor *extractor_ {}; std::string sourceFile_; diff --git a/es2panda/compiler/core/compilerImpl.cpp b/es2panda/compiler/core/compilerImpl.cpp index 1d2c8bffc56..5aa88b184a3 100644 --- a/es2panda/compiler/core/compilerImpl.cpp +++ b/es2panda/compiler/core/compilerImpl.cpp @@ -41,8 +41,8 @@ panda::pandasm::Program *CompilerImpl::Compile(parser::Program *program, const e { bool isTypeExtractorEnabled = ((program->Extension() == ScriptExtension::TS) && options.typeExtractor); CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode, - options.mergeAbc, isTypeExtractorEnabled, false, debugInfoSourceFile, pkgName, - program->RecordName(), patchFixHelper_); + options.mergeAbc, isTypeExtractorEnabled, false, options.recordSource, debugInfoSourceFile, + pkgName, program->RecordName(), patchFixHelper_); ArenaAllocator localAllocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true); diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 6a4a4b98cfc..b3e2062dfce 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -247,11 +247,10 @@ void FunctionEmitter::GenFunctionSource() return; } - if (!(static_cast(pg_->RootNode()))->ShowSource()) { - return; + if (pg_->Context()->IsRecordSource() || (static_cast(pg_->RootNode()))->ShowSource()) { + func_->source_code = SourceCode().Mutf8(); } - - func_->source_code = SourceCode().Mutf8(); + } void FunctionEmitter::GenScopeVariableInfo(const binder::Scope *scope) diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 13bde879841..09dfcc5da9f 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -50,7 +50,7 @@ Compiler::~Compiler() panda::pandasm::Program *CreateJsonContentProgram(std::string src, std::string rname, util::PatchFix *patchFixHelper) { - panda::es2panda::compiler::CompilerContext context(nullptr, false, false, false, false, true, + panda::es2panda::compiler::CompilerContext context(nullptr, false, false, false, false, true, false, src, "", util::StringView(rname), patchFixHelper); context.GetEmitter()->GenRecordNameInfo(); return context.GetEmitter()->Finalize(false, nullptr); diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index a1778653f5c..76552a35632 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -84,6 +84,7 @@ struct CompilerOptions { bool mergeAbc {false}; bool typeExtractor {false}; bool typeDtsBuiltin {false}; + bool recordSource {false}; int fileThreadCount {0}; int functionThreadCount {0}; int optLevel {0}; diff --git a/es2panda/test/compiler/recordsource/with-off/toString-without-record-source-expected.txt b/es2panda/test/compiler/recordsource/with-off/toString-without-record-source-expected.txt new file mode 100644 index 00000000000..b5bcd390741 --- /dev/null +++ b/es2panda/test/compiler/recordsource/with-off/toString-without-record-source-expected.txt @@ -0,0 +1 @@ +Cannot get source code of funtion diff --git a/es2panda/test/compiler/recordsource/with-off/toString-without-record-source.js b/es2panda/test/compiler/recordsource/with-off/toString-without-record-source.js new file mode 100644 index 00000000000..fa6a8c944d9 --- /dev/null +++ b/es2panda/test/compiler/recordsource/with-off/toString-without-record-source.js @@ -0,0 +1,4 @@ +function foo(){ +} + +print(foo.toString()) \ No newline at end of file diff --git a/es2panda/test/compiler/recordsource/with-on/toString-with-record-source-expected.txt b/es2panda/test/compiler/recordsource/with-on/toString-with-record-source-expected.txt new file mode 100644 index 00000000000..9112aa7cf1d --- /dev/null +++ b/es2panda/test/compiler/recordsource/with-on/toString-with-record-source-expected.txt @@ -0,0 +1,2 @@ +function foo(){ +} diff --git a/es2panda/test/compiler/recordsource/with-on/toString-with-record-source.js b/es2panda/test/compiler/recordsource/with-on/toString-with-record-source.js new file mode 100644 index 00000000000..fa6a8c944d9 --- /dev/null +++ b/es2panda/test/compiler/recordsource/with-on/toString-with-record-source.js @@ -0,0 +1,4 @@ +function foo(){ +} + +print(foo.toString()) \ No newline at end of file diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 0b9428c5ceb..e8da871da2c 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1484,6 +1484,8 @@ def main(): runner.add_directory("compiler/ts/projects", "ts", ["--module", "--merge-abc"]) runner.add_directory("compiler/dts", "d.ts", ["--module", "--opt-level=0"]) runner.add_directory("compiler/commonjs", "js", ["--commonjs"]) + runner.add_directory("compiler/recordsource/with-on", "js", ["--record-source"]) + runner.add_directory("compiler/recordsource/with-off", "js", []) runners.append(runner) -- Gitee