diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 95085c90674b020d4236b8a1e6ed23ff22511fc8..6fd8a714695d3bfe737383710568f9cbda404615 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 78edb8abbd9f7cfcec64daeff7d1846375945dcc..b55778a32dec8f02f270730b0d1c7933f45ebab3 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 0581f4c2d1b6b212cdc6688d4513b3d8784963e0..f3d3198e5bf0f5c16eb71a28b6ae677d23944fd2 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 1d2c8bffc56730af7b2df07c0ea5ab218ffcb00b..5aa88b184a3e41bc7724a1892a14ddca956b4bed 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 6a4a4b98cfc1270cc079220a378c9bc93253fce8..b3e2062dfced56dca39dab0c5e83bc50121afc4c 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 13bde8798416ab7b8bf121cdf0a798289df6e496..09dfcc5da9f83f6bc9e4b3554c6bad643623ea34 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 a1778653f5c8e369929059fe08b43f1989e707e6..76552a3563245e2b6aa4e5c9e608654df11e5ed5 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 0000000000000000000000000000000000000000..b5bcd3907416d2d563bc2d9f92bb6c9c92941749 --- /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 0000000000000000000000000000000000000000..fa6a8c944d90fca3e0c75a5f844eba33db8ef547 --- /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 0000000000000000000000000000000000000000..9112aa7cf1d1748de9137465391cc25b4012205d --- /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 0000000000000000000000000000000000000000..fa6a8c944d90fca3e0c75a5f844eba33db8ef547 --- /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 0b9428c5cebba35f6c520d095617fd3a553475b7..e8da871da2cd341ff0025a5d82d9cdedc64ffe3d 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)