diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 10410d94915a965db6615b908386a6194691aea6..533164d1648ef6850f2d7847dfdb31aa5c6b9077 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -148,7 +148,8 @@ bool Options::Parse(int argc, const char **argv) 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"); - panda::PandArg outputProto("outputProto", "", "compiler proto serialize binary output (.proto)"); + panda::PandArg outputProto("outputProto", "", + "specify the output name for serializd protobuf file (.protoBin)"); panda::PandArg opCacheFile("cache-file", "", "cache file for incremental compile"); panda::PandArg opNpmModuleEntryList("npm-module-entry-list", "", "entry list file for module compile"); panda::PandArg opMergeAbc("merge-abc", false, "Compile as merge abc"); diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index f8709aa36dccf91705432f5920f71668ccd9f809..ea11d2bd33e33b34c914a75c9296c6559dc50e12 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -78,7 +78,7 @@ void CompileFunctionJob::Run() FunctionEmitter funcEmitter(&allocator, &pg); funcEmitter.Generate(context_->HotfixHelper()); - context_->GetEmitter()->AddFunction(&funcEmitter); + context_->GetEmitter()->AddFunction(&funcEmitter, context_); if (dependant_) { dependant_->Signal(); diff --git a/es2panda/compiler/core/compilerContext.cpp b/es2panda/compiler/core/compilerContext.cpp index 81434e630b0a4cdccc30fa37dc54886355150f91..f3549110db087121ab07c049dfe76cea3a76e401 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, std::string sourceFile, util::StringView recordName) : binder_(binder), isDebug_(isDebug), isDebuggerEvaluateExpressionMode_(isDebuggerEvaluateExpressionMode), - isMergeAbc_(isMergeAbc), sourceFile_(sourceFile), emitter_(std::make_unique(this)) + isMergeAbc_(isMergeAbc), sourceFile_(sourceFile), emitter_(std::make_unique(this)), + recordName_(recordName) { } diff --git a/es2panda/compiler/core/compilerContext.h b/es2panda/compiler/core/compilerContext.h index 6acc7b83fb53a24cf1004237abdbab7734ca6349..5348d48fd36f327edbe0bc513932a09d4c209635 100644 --- a/es2panda/compiler/core/compilerContext.h +++ b/es2panda/compiler/core/compilerContext.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,7 @@ class Emitter; class CompilerContext { public: CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode, - bool isMergeAbc, std::string sourceFile); + bool isMergeAbc, std::string sourceFile, util::StringView recordName); NO_COPY_SEMANTIC(CompilerContext); NO_MOVE_SEMANTIC(CompilerContext); ~CompilerContext() = default; @@ -96,6 +97,11 @@ public: return hotfixHelper_; } + util::StringView RecordName() const + { + return recordName_; + } + private: binder::Binder *binder_; int32_t literalBufferIdx_ {0}; @@ -106,6 +112,7 @@ private: std::string sourceFile_; std::unique_ptr emitter_; util::Hotfix *hotfixHelper_ {nullptr}; + util::StringView recordName_; }; } // namespace panda::es2panda::compiler diff --git a/es2panda/compiler/core/compilerImpl.cpp b/es2panda/compiler/core/compilerImpl.cpp index 5561346b974793c47b68f0e8599a6c22d805f6e7..368783be9633a58dc2a8ce8f99c98582a2344101 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, debugInfoSourceFile, program->RecordName()); if (hotfixHelper_ != nullptr) { context.AddHotfixHelper(hotfixHelper_); diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index 53064fdc1dea20e7c450060e75a09504cadcdc15..b217dca3c4b0b037ad8c21e09613a63cf79fe5d3 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -359,7 +359,7 @@ Emitter::~Emitter() delete prog_; } -void Emitter::AddFunction(FunctionEmitter *func) +void Emitter::AddFunction(FunctionEmitter *func, CompilerContext *context) { std::lock_guard lock(m_); @@ -369,7 +369,8 @@ void Emitter::AddFunction(FunctionEmitter *func) for (auto &[idx, buf] : func->LiteralBuffers()) { auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(buf)); - prog_->literalarray_table.emplace(std::to_string(idx), std::move(literalArrayInstance)); + auto litId = std::string(context->Binder()->Program()->RecordName()) + "_" + std::to_string(idx); + prog_->literalarray_table.emplace(litId, std::move(literalArrayInstance)); } auto *function = func->Function(); @@ -380,12 +381,13 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon { std::lock_guard lock(m_); + auto moduleLiteral = std::string(context->Binder()->Program()->RecordName()) + "_" + std::to_string(module->Index()); if (context->IsMergeAbc()) { auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = "moduleRecordIdx"; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(module->Index()))); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(moduleLiteral))); rec_->field_list.emplace_back(std::move(moduleIdxField)); if (context->HotfixHelper()) { @@ -398,8 +400,8 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon auto moduleIdxField = panda::pandasm::Field(LANG_EXT); moduleIdxField.name = std::string {context->Binder()->Program()->SourceFile()}; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(module->Index()))); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(moduleLiteral))); ecmaModuleRecord.field_list.emplace_back(std::move(moduleIdxField)); if (context->HotfixHelper()) { @@ -409,7 +411,7 @@ void Emitter::AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerCon } auto &moduleLiteralsBuffer = module->Buffer(); auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(moduleLiteralsBuffer)); - prog_->literalarray_table.emplace(std::to_string(module->Index()), std::move(literalArrayInstance)); + prog_->literalarray_table.emplace(static_cast(moduleLiteral), std::move(literalArrayInstance)); } void Emitter::DumpAsm(const panda::pandasm::Program *prog) diff --git a/es2panda/compiler/core/emitter/emitter.h b/es2panda/compiler/core/emitter/emitter.h index f689a4e57c80e973f81be76d7c0064134343f076..979356faab3d4654209a431d7d7ad74847f8d631 100644 --- a/es2panda/compiler/core/emitter/emitter.h +++ b/es2panda/compiler/core/emitter/emitter.h @@ -100,7 +100,7 @@ public: NO_COPY_SEMANTIC(Emitter); NO_MOVE_SEMANTIC(Emitter); - void AddFunction(FunctionEmitter *func); + void AddFunction(FunctionEmitter *func, CompilerContext *context); void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context); static void DumpAsm(const panda::pandasm::Program *prog); panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::Hotfix *hotfixHelper); diff --git a/merge_abc/HowToWriteProtoForAssemblyStuff.md b/merge_abc/HowToWriteProtoForAssemblyStuff.md new file mode 100644 index 0000000000000000000000000000000000000000..83f76ebe34a0002579cc29342f7bb7b908f1a7f3 --- /dev/null +++ b/merge_abc/HowToWriteProtoForAssemblyStuff.md @@ -0,0 +1,281 @@ +# How To Write Proto For Assembly + +## 消息格式 + +``` +syntax = "proto3" // 指定版本信息 +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; +} +``` + +消息由字段组合而成 +``` +限定修饰符 数据类型 字段名称 = 唯一编号标签值 +``` + +## 限定修饰符说明 + +### required + +required 在发送消息之前必须设置该字段的值。对于接收方,必须能够识别该字段的意思。发送之前没有设置required字段或者无法识别required字段都会引发编解码异常,导致消息被丢弃。 + +``` +syntax = "proto3" +message Test +{ + required string test = 1; +} +``` + +### repeated + +repeated 代表可重复,我们可以理解为数组 + +``` +syntax = "proto3" +message Test +{ + string test = 1; +} + +message TestArr +{ + repeated Test arr = 1; +} +``` + +### optional + +optional 表示是一个可选字段,可选对于发送方,在发送消息时,可以有选择性的设置或者不设置该字段的值。如果无法识别,则忽略该字段,消息中的其它字段正常处理。 + +## 数据类型 + +| proto类型 | C++类型 | 备注 | +|:-----------|:---------------|:------------| +|double |double | 64位浮点数 +|float |float | 32位浮点数 +|int32 |int32 | 32位整数 +|int64 |int64 | 64位整数 +|uint32 |uint32 | 32位无符号整数 +|uint64 |uint64 | 64位无符号整数 +|sint32 |int32 | 32位整数,处理负数效率比int32更高 +|sint32 |sint64 | 64位整数,处理负数效率比int64更高 +|fixed32 | uint32 | 总是4个字节。如果数值总是比总是比228大的话,这个类型会比uint32高效。 +|fixed64 | uint64 | 总是8个字节。如果数值总是比总是比256大的话,这个类型会比uint64高效。 +|sfixed32 | int32 | 总是4个字节 +|sfixed64 | int64 | 总是8个字节 +|bool | bool | 布尔类型 +|string | string | 一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本 +|bytes | string | 处理多字节的语言字符、如中文 +|enum | enum | 枚举(proto2 从1开始,proto3从0开始) +|message | object of class | 自定义的消息类型 + + +## 字段名称 + +protobuf建议以下划线命名而非驼峰式 + +## 唯一的编号标签 + +代表每个字段的一个唯一的编号标签,在同一个消息里不可以重复。这些编号标签用与在消息二进制格式中标识你的字段,并且消息一旦定义就不能更改。需要说明的是标签在1到15范围的采用一个字节进行编码,所以通常将标签1到15用于频繁发生的消息字段。编号标签大小的范围是1到229。此外不能使用protobuf系统预留的编号标签(19000 ~19999) + +## import + +protobuf 接口文件可以像C语言的h文件一个,分离为多个,在需要的时候通过 import导入需要对文件。其行为和C语言的#include或者java的import的行为大致相同。 + +## enum + +``` +syntax = "proto3" // 指定版本信息 +enum COLOR +{ + RED = 0; + BLUE = 1; + YELLOW = 2; +} + +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; + COLOR type = 2; +} +``` + +## package + +``` +syntax = "proto3" // 指定版本信息 + +package tutorial; + +message Test // message为关键字,作用为定义一种消息类型 +{ + string test = 1; +} +``` + +## oneof + +message 包含许多可选字段,并且最多只能同时设置其中一个字段,则可以使用 oneof 功能强制执行此行为并节省内存 + +# Assembly对应Proto + +## class\struct + +protobuf中将类型信息结构化处理,通过自定义消息message结构对C++中class或struct进行解析处理 + +C++ +``` +struct Ins { + size_t line_number = 0; + uint32_t column_number = 0; + std::string whole_line = ""; // TODO(mbolshov): redundant given file and line_number + size_t bound_left = 0; + size_t bound_right = 0; +} +``` + +proto +``` +message DebuginfoIns { + uint64 lineNumber = 1; + uint32 columnNumber = 2; + bytes wholeLine = 3; + uint64 boundLeft = 4; + uint64 boundRight = 5; +} +``` + + +## enum + +protobuf中存在enum类型,proto2 从1开始,proto3从0开始 + +对于Assembly中的转换,优化序列化速度,通过uint32位直接进行转换: + +C++ +``` +class Value { +public: + enum class Type { + U1, + I8, + U8, + I16, + U16, + I32, + U32, + I64, + U64, + F32, + F64, + STRING, + STRING_NULLPTR, + RECORD, + METHOD, + ENUM, + ANNOTATION, + ARRAY, + VOID, + METHOD_HANDLE, + UNKNOWN + }; + ... +} + +``` + + +proto +``` +message Value { + uint32 type = 1; +} +``` +## inheritance + +protobuf中message无继承信息,开发者自己实现继承信息 + +C++ +``` +class Value { +... +} + +class ArrayValue : public Value { +... +} +``` + +proto +``` + +message Value { + uint32 type = 1; +} + +message ArrayValue { + Value father = 1; +} + +``` + +## std::variant + +通过oneof关键字实现std::variant + +C++ +``` +std::variant value_; +``` + +proto +``` +oneof value { + uint64 valueU64 = 2; + float valueFloat = 3; + double valueDouble = 4; + bytes valueStr = 5; + Type valueType = 6; + AnnotationData valueAnno = 7; +} +``` + +## unique_ptr + +查看源码中的变量关系,通过oneof实现智能指针 + +C++ +``` +class Value { +} + +class ArrayValue { +} + +class ScalarValue { +} + +std::unique_ptr value_; +``` + +proto +``` +message Value { +} + +message ScalarValue { + Value father = 1; +} + +message ArrayValue { + Value father = 1; +} + +oneof value { + ScalarValue scalar = 2; + ArrayValue array = 3; +} +``` diff --git a/merge_abc/src/assemblyFileLocationProto.cpp b/merge_abc/src/assemblyFileLocationProto.cpp index 9f4a0f23bd09b33e9a20c4497a43ff80d3fbb763..e3ef08e9764142893e15997b033ce9207fba1b32 100644 --- a/merge_abc/src/assemblyFileLocationProto.cpp +++ b/merge_abc/src/assemblyFileLocationProto.cpp @@ -21,14 +21,16 @@ void FileLocation::Serialize(const panda::pandasm::FileLocation &location, proto protoLocation.set_wholeline(location.whole_line); protoLocation.set_boundleft(location.bound_left); protoLocation.set_boundright(location.bound_right); + protoLocation.set_linenumber(location.line_number); protoLocation.set_isdefined(location.is_defined); } -void FileLocation::Deserialize(const protoPanda::FileLocation &protoLocation, panda::pandasm::FileLocation &location) +void FileLocation::Deserialize(const protoPanda::FileLocation &protoLocation, + std::optional &location) { - location.whole_line = protoLocation.wholeline(); - location.bound_left = protoLocation.boundleft(); - location.bound_right = protoLocation.boundright(); - location.is_defined = protoLocation.isdefined(); + std::string wholeLine = protoLocation.wholeline(); + panda::pandasm::FileLocation fileLocation(wholeLine, protoLocation.boundleft(), protoLocation.boundright(), + protoLocation.linenumber(), protoLocation.isdefined()); + location = fileLocation; } } // panda::proto diff --git a/merge_abc/src/assemblyFileLocationProto.h b/merge_abc/src/assemblyFileLocationProto.h index 83afe39022584f2820b3e615a3714085a5b00be2..2c13264974648c2248d800d34773ade3329be0b7 100644 --- a/merge_abc/src/assemblyFileLocationProto.h +++ b/merge_abc/src/assemblyFileLocationProto.h @@ -23,7 +23,8 @@ namespace panda::proto { class FileLocation { public: static void Serialize(const panda::pandasm::FileLocation &location, protoPanda::FileLocation &protoLocation); - static void Deserialize(const protoPanda::FileLocation &protoLocation, panda::pandasm::FileLocation &location); + static void Deserialize(const protoPanda::FileLocation &protoLocation, + std::optional &location); }; } // panda::proto #endif diff --git a/merge_abc/src/assemblyFunctionProto.cpp b/merge_abc/src/assemblyFunctionProto.cpp index 5b3e6387033626342f7b81d95a3e946740a65378..97b8d19bdc1a342b26197eb11b3b8ca08f9b0462 100644 --- a/merge_abc/src/assemblyFunctionProto.cpp +++ b/merge_abc/src/assemblyFunctionProto.cpp @@ -159,7 +159,7 @@ void Function::Deserialize(const protoPanda::Function &protoFunction, panda::pan SourceLocation::Deserialize(protoFunction.bodylocation(), function.body_location); if (protoFunction.has_filelocation()) { - FileLocation::Deserialize(protoFunction.filelocation(), function.file_location.value()); + FileLocation::Deserialize(protoFunction.filelocation(), function.file_location); } } } // panda::proto diff --git a/merge_abc/src/assemblyLabelProto.cpp b/merge_abc/src/assemblyLabelProto.cpp index f355a80490d50fe5e558267315b95a726058d764..63b18cefc1927df9c51b472a40f4b67e395d288e 100644 --- a/merge_abc/src/assemblyLabelProto.cpp +++ b/merge_abc/src/assemblyLabelProto.cpp @@ -31,7 +31,7 @@ void Label::Deserialize(const protoPanda::Label &protoLabel, panda::pandasm::Lab label.name = protoLabel.name(); if (protoLabel.has_filelocation()) { protoPanda::FileLocation protoLocation = protoLabel.filelocation(); - FileLocation::Deserialize(protoLocation, label.file_location.value()); + FileLocation::Deserialize(protoLocation, label.file_location); } } } // panda::proto diff --git a/merge_abc/src/assemblyRecordProto.cpp b/merge_abc/src/assemblyRecordProto.cpp index cfd7d34d44076f92dc9c976d744e2f2a04a18040..9409ff12972b829349903b3fe7b4c3b4ef17a8d6 100644 --- a/merge_abc/src/assemblyRecordProto.cpp +++ b/merge_abc/src/assemblyRecordProto.cpp @@ -56,7 +56,7 @@ void Record::Deserialize(const protoPanda::Record &protoRecord, panda::pandasm:: record.source_file = protoRecord.sourcefile(); if (protoRecord.has_filelocation()) { const auto &protoLocation = protoRecord.filelocation(); - FileLocation::Deserialize(protoLocation, record.file_location.value()); + FileLocation::Deserialize(protoLocation, record.file_location); } } } // panda::proto diff --git a/test262/run_sunspider.py b/test262/run_sunspider.py index f7f6ea757ac1b824728332a2db959b198383a85f..e7c6eb3d2aef275cff3a8b1f7b25e5b5e9fb80e1 100755 --- a/test262/run_sunspider.py +++ b/test262/run_sunspider.py @@ -230,9 +230,23 @@ class ArkProgram(): output_file = os.path.splitext(os.path.join(BASE_OUT_DIR, os.path.split(dependency)[1]))[0] output_abc = f"{output_file}.abc" + proto_bin_file = f"{output_file}.bin" + output_abc_name = os.path.basename(output_abc) + frontend_tool = self.ark_frontend_binary - cmd_args = [frontend_tool, dependency, '--output', output_abc, - '--module'] + merge_abc_binary = self.args.merge_abc_binary + merge_abc_mode = self.merge_abc_mode + + if merge_abc_mode != "0": + cmd_args = [frontend_tool, dependency, '--outputProto', + proto_bin_file, '--module', '--merge-abc'] + proc = subprocess.call(cmd_args) + cmd_args = [merge_abc_binary, '--input', proto_bin_file, '--suffix', + "bin", '--outputFilePath', BASE_OUT_DIR, '--output', + output_abc_name] + else: + cmd_args = [frontend_tool, dependency, '--output', output_abc, + '--module', '--merge-abc'] proc = subprocess.Popen(cmd_args) proc.wait() @@ -261,8 +275,14 @@ class ArkProgram(): if self.ark_frontend == ARK_FRONTEND_LIST[0]: mod_opt_index = 3 - cmd_args = ['node', '--expose-gc', frontend_tool, - js_file, '-o', out_file] + if merge_abc_mode != "0": + # '--opt-level=0' is added due to failure in optimizer, should be removed later + cmd_args = ['node', '--expose-gc', frontend_tool, + js_file, '--output-proto', proto_bin_file, + '--opt-level=0'] + else: + cmd_args = ['node', '--expose-gc', frontend_tool, + js_file, '-o', out_file] if file_name in self.module_list: cmd_args.insert(mod_opt_index, "-m") self.module = True @@ -270,7 +290,7 @@ class ArkProgram(): mod_opt_index = 1 if merge_abc_mode != "0": cmd_args = [frontend_tool, '--outputProto', - proto_bin_file, js_file] + proto_bin_file, js_file, '--merge-abc'] else: cmd_args = [frontend_tool, '--opt-level=' + str(self.opt_level), '--function-threads=' + @@ -294,7 +314,7 @@ class ArkProgram(): self.abc_file += f':{abc_file}' retcode = exec_command(cmd_args) self.abc_cmd = cmd_args - if self.ark_frontend == ARK_FRONTEND_LIST[1] and merge_abc_mode != "0": + if merge_abc_mode != "0": cmd_args = [merge_abc_binary, '--input', proto_bin_file, '--suffix', "bin", '--outputFilePath', file_dir, '--output', proto_abc_file] @@ -387,6 +407,8 @@ class ArkProgram(): cmd_args = [self.ark_tool, ICU_PATH, f'{file_name_pre}.abc'] + record_name = os.path.splitext(os.path.split(self.js_file)[1])[0] + cmd_args.append(f'--entry-point={record_name}') retcode = exec_command(cmd_args) if retcode: print_command(cmd_args) diff --git a/ts2panda/src/base/literal.ts b/ts2panda/src/base/literal.ts index 50d7c9d52c9f9d91839eef7a44b90885d4af03f0..2cfb24623694f2d639513a3d2b40c1ad4ee24fe8 100644 --- a/ts2panda/src/base/literal.ts +++ b/ts2panda/src/base/literal.ts @@ -14,59 +14,66 @@ */ export enum LiteralTag { - BOOLEAN = 1, - INTEGER = 2, - DOUBLE = 4, - STRING = 5, - METHOD = 6, - GENERATOR = 7, - ACCESSOR = 8, - METHODAFFILIATE = 9, - // 0x0a - 0x15 for ARRAY_Type - ASYNCGENERATOR = 22, - LITERALBUFFERINDEX = 23, - NULLVALUE = 255 + BOOLEAN = 1, + INTEGER = 2, + DOUBLE = 4, + STRING = 5, + METHOD = 6, + GENERATOR = 7, + ACCESSOR = 8, + METHODAFFILIATE = 9, + // 0x0a - 0x15 for ARRAY_Type + ASYNCGENERATOR = 22, + LITERALBUFFERINDEX = 23, + LITERALARRAY = 24, + BUILTINTYPEINDEX = 25, + NULLVALUE = 255 } export class Literal { - private t: LiteralTag; - private v: any; + private t: LiteralTag; + private v: any; - constructor(t: LiteralTag, v: any) { - this.t = t; - this.v = v; - } + constructor(t: LiteralTag, v: any) { + this.t = t; + this.v = v; + } - getTag() { - return this.t; - } + getTag() { + return this.t; + } - getValue() { - return this.v; - } + getValue() { + return this.v; + } } export class LiteralBuffer { - private lb: Literal[] = []; + private k: string; + private lb: Literal[] = []; - constructor() { }; + constructor() {}; - addLiterals(...literals: Array) { - this.lb.push(...literals); - } + addLiterals(...literals: Array) { + this.lb.push(...literals); + } - getLiterals() { - return this.lb; - } + getLiterals() { + return this.lb; + } - isEmpty() { - return this.lb.length == 0; - } + isEmpty() { + return this.lb.length == 0; + } + + getLiteral(index: number) { + if (index >= this.lb.length || this.lb.length <=0) { + return ; + } + return this.lb[index]; + } - getLiteral(index: number) { - if (index >= this.lb.length || this.lb.length <=0) { - return ; + setKey(key: string) { + this.k = key; } - return this.lb[index]; - } -} \ No newline at end of file +} diff --git a/ts2panda/src/base/typeSystem.ts b/ts2panda/src/base/typeSystem.ts index 43bdf6e64f06046a31eae1783f476faf7d2c5820..4a7583519f0e32bde14db983372eb0ebed4c5c27 100644 --- a/ts2panda/src/base/typeSystem.ts +++ b/ts2panda/src/base/typeSystem.ts @@ -26,6 +26,8 @@ import { } from "./literal"; import { hasAbstractModifier } from "./util"; import { CmdOptions } from "../cmdOptions"; +import { getLiteralKey } from "../index"; +import { CompilerDriver } from "../compilerDriver"; export enum PrimitiveType { ANY, @@ -143,7 +145,8 @@ export enum AccessFlag { PROTECTED } -type ClassMemberFunction = ts.MethodDeclaration | ts.ConstructorDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration; +type ClassMemberFunction = ts.MethodDeclaration | ts.ConstructorDeclaration | + ts.GetAccessorDeclaration | ts.SetAccessorDeclaration; export abstract class BaseType { @@ -151,6 +154,15 @@ export abstract class BaseType { protected typeChecker = TypeChecker.getInstance(); protected typeRecorder = TypeRecorder.getInstance(); + protected transferType2Literal(type: number, literals: Array) { + if (type >= literalBufferIndexShift) { + let litId = getLiteralKey(CompilerDriver.srcNode, type - literalBufferIndexShift); + literals.push(new Literal(LiteralTag.LITERALARRAY, litId)); + } else { + literals.push(new Literal(LiteralTag.BUILTINTYPEINDEX, type)); + } + } + protected addCurrentType(node: ts.Node, index: number) { this.typeRecorder.addType2Index(node, index); } @@ -233,12 +245,17 @@ export class TypeSummary extends BaseType { transfer2LiteralBuffer(): LiteralBuffer { let countBuf = new LiteralBuffer(); let summaryLiterals: Array = new Array(); - summaryLiterals.push(new Literal(LiteralTag.INTEGER, L2Type._COUNTER)); let definedTypeNum = this.userDefinedClassNum; if (isGlobalDeclare()) { definedTypeNum += userDefinedTypeStartIndex - BuiltinType._HEAD; } + // summaryLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, definedTypeNum)); summaryLiterals.push(new Literal(LiteralTag.INTEGER, definedTypeNum)); + let literalIdx = PandaGen.getLiteralArrayBuffer.length; + for (let i = 1; i <= definedTypeNum; i++) { + summaryLiterals.push(new Literal(LiteralTag.LITERALARRAY, + getLiteralKey(CompilerDriver.srcNode, literalIdx + i))); + } summaryLiterals.push(new Literal(LiteralTag.INTEGER, this.anonymousRedirect.length)); for (let element of this.anonymousRedirect) { summaryLiterals.push(new Literal(LiteralTag.STRING, element)); @@ -422,10 +439,10 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.CLASS)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.modifier)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.extendsHeritage)); + classTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.extendsHeritage)); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.implementsHeritages.length)); - this.implementsHeritages.forEach(heritage => { - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, heritage)); + this.implementsHeritages.forEach(heritage => { // heritage types + this.transferType2Literal(heritage, classTypeLiterals); }); // record unstatic fields and methods @@ -446,7 +463,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[0])); // typeIndex + this.transferType2Literal(typeInfo[0], classTypeLiterals); classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -458,7 +475,7 @@ export class ClassType extends BaseType { classTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { classTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - classTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo)); + this.transferType2Literal(typeInfo, classTypeLiterals); }); } } @@ -480,7 +497,7 @@ export class ClassInstType extends BaseType { let classInstLiterals: Array = new Array(); classInstLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.CLASSINST)); - classInstLiterals.push(new Literal(LiteralTag.INTEGER, this.shiftedReferredClassIndex)); + this.transferType2Literal(this.shiftedReferredClassIndex, classInstLiterals); classInstBuf.addLiterals(...classInstLiterals); return classInstBuf; @@ -585,20 +602,20 @@ export class FunctionType extends BaseType { funcTypeLiterals.push(new Literal(LiteralTag.STRING, this.name)); if (this.containThisParam) { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, 1)); // marker for having 'this' param - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[0])); + this.transferType2Literal(this.parameters[0], funcTypeLiterals); funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length - 1)); - for (let i = 1; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[i])); + for (let i = 1; i < this.parameters.length; i++) { // normal parameter types + this.transferType2Literal(this.parameters[i], funcTypeLiterals); } } else { funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, 0)); // marker for not having 'this' param funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters.length)); for (let i = 0; i < this.parameters.length; i++) { - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.parameters[i])); + this.transferType2Literal(this.parameters[i], funcTypeLiterals); } } - funcTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.returnType)); + funcTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.returnType)); funcTypeBuf.addLiterals(...funcTypeLiterals); return funcTypeBuf; } @@ -675,7 +692,7 @@ export class UnionType extends BaseType { UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.UNION)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.unionedTypeArray.length)); for (let type of this.unionedTypeArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, type)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; @@ -720,7 +737,7 @@ export class ArrayType extends BaseType { let arrayBuf = new LiteralBuffer(); let arrayLiterals: Array = new Array(); arrayLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.ARRAY)); - arrayLiterals.push(new Literal(LiteralTag.INTEGER, this.referedTypeIndex)); + this.transferType2Literal(this.referedTypeIndex, arrayLiterals); arrayBuf.addLiterals(...arrayLiterals); return arrayBuf; } @@ -755,7 +772,7 @@ export class ObjectType extends BaseType { objLiterals.push(new Literal(LiteralTag.INTEGER, this.properties.size)); this.properties.forEach((typeIndex, name) => { objLiterals.push(new Literal(LiteralTag.STRING, name)); - objLiterals.push(new Literal(LiteralTag.INTEGER, typeIndex)); + this.transferType2Literal(typeIndex, objLiterals); }); objTypeBuf.addLiterals(...objLiterals); return objTypeBuf; @@ -868,7 +885,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.heritages.length)); this.heritages.forEach(heritage => { - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, heritage)); + this.transferType2Literal(heritage, interfaceTypeLiterals) }); // record fields and methods @@ -885,7 +902,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.size)); transferredTarget.forEach((typeInfo, name) => { interfaceTypeLiterals.push(new Literal(LiteralTag.STRING, name)); - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[0])); // typeIndex + this.transferType2Literal(typeInfo[0], interfaceTypeLiterals); interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[1])); // accessFlag interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, typeInfo[2])); // readonly }); @@ -896,7 +913,7 @@ export class InterfaceType extends BaseType { interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, transferredTarget.length)); transferredTarget.forEach(method => { - interfaceTypeLiterals.push(new Literal(LiteralTag.INTEGER, method)); + this.transferType2Literal(method, interfaceTypeLiterals); }); } } @@ -925,10 +942,10 @@ export class BuiltinContainerType extends BaseType { let UnionTypeBuf = new LiteralBuffer(); let UnionTypeLiterals: Array = new Array(); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, L2Type.BUILTINCONTAINER)); - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.builtinTypeIndex)); + UnionTypeLiterals.push(new Literal(LiteralTag.LITERALBUFFERINDEX, this.builtinTypeIndex)); UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, this.containerArray.length)); for (let type of this.containerArray) { - UnionTypeLiterals.push(new Literal(LiteralTag.INTEGER, type)); + this.transferType2Literal(type, UnionTypeLiterals); } UnionTypeBuf.addLiterals(...UnionTypeLiterals); return UnionTypeBuf; diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index bf19d72cd51dce6f019b031fb97c0b04d61618c4..9cba00c744304dcf7d7dc8633af7db7e4b74be6e 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -51,6 +51,8 @@ const ts2pandaOptions = [ { name: 'expression-watch-toolchain', type: String, defaultValue: "es2panda", description: "Specify the tool chain used to transform the expression" }, { name: 'source-file', type: String, defaultValue: "", description: "specify the file path info recorded in generated abc" }, { name: 'generate-tmp-file', type: Boolean, defaultValue: false, description: "whether to generate intermediate temporary files"}, + { name: 'record-name', type: String, defaultValue: "", description: "specify the record name." }, + { name: 'output-proto', type: String, defaultValue: "", description: "specify the output name for serializd protobuf file (.protoBin)" }, ] @@ -209,6 +211,13 @@ export class CmdOptions { return outputFile; } + static getRecordName(): string { + if (!this.options) { + return ""; + } + return this.options["record-name"]; + } + static getTimeOut(): Number { if (!this.options) { return 0; @@ -273,7 +282,7 @@ export class CmdOptions { return false; } - return !this.options["record-type"]; + return this.options["record-type"]; } static needRecordDtsType(): boolean { @@ -315,6 +324,10 @@ export class CmdOptions { return this.options["generate-tmp-file"]; } + static getOutputproto(): string { + return this.options["output-proto"]; + } + // @ts-ignore static parseUserCmd(args: string[]): ts.ParsedCommandLine | undefined { this.options = commandLineArgs(ts2pandaOptions, { partial: true }); diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index 934449b3a2c3ab9636efe4441ceab5a9540e58f6..042ba6fafde050c5fc2d27c28c704118b5cd63f0 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -17,7 +17,8 @@ import { writeFileSync } from "fs"; import * as ts from "typescript"; import { addVariableToScope } from "./addVariable2Scope"; import { AssemblyDumper } from "./assemblyDumper"; -import { hasDefaultKeywordModifier, hasExportKeywordModifier, initiateTs2abc, listenChildExit, listenErrorEvent, terminateWritePipe, getRecordTypeFlag } from "./base/util"; +import { hasDefaultKeywordModifier, hasExportKeywordModifier, initiateTs2abc, listenChildExit, + listenErrorEvent, terminateWritePipe, getRecordTypeFlag } from "./base/util"; import { CmdOptions } from "./cmdOptions"; import { Compiler @@ -45,6 +46,7 @@ import { Ts2Panda } from "./ts2panda"; import { TypeRecorder } from "./typeRecorder"; import { LiteralBuffer } from "./base/literal"; import { findOuterNodeOfParenthesis } from "./expression/parenthesizedExpression"; +import { getRecordName } from "./index"; export class PendingCompilationUnit { constructor( @@ -59,8 +61,10 @@ export class PendingCompilationUnit { * It handles all dependencies and run passes. */ export class CompilerDriver { + static srcNode: ts.SourceFile | undefined = undefined; static isTsFile: boolean = false; private fileName: string; + private recordName: string; private passes: Pass[] = []; private compilationUnits: PandaGen[]; pendingCompilationUnits: PendingCompilationUnit[]; @@ -70,8 +74,9 @@ export class CompilerDriver { private needDumpHeader: boolean = true; private ts2abcProcess: any = undefined; - constructor(fileName: string) { + constructor(fileName: string, recordName: string) { this.fileName = fileName; + this.recordName = recordName; // register passes here this.passes = [ new CacheExpander(), @@ -178,7 +183,10 @@ export class CompilerDriver { listenErrorEvent(ts2abcProc); try { + // must keep [dumpRecord] at first + Ts2Panda.dumpRecord(ts2abcProc, this.recordName); Ts2Panda.dumpCmdOptions(ts2abcProc); + Ts2Panda.dumpRecordName(ts2abcProc, getRecordName(CompilerDriver.srcNode)); for (let i = 0; i < this.pendingCompilationUnits.length; i++) { let unit: PendingCompilationUnit = this.pendingCompilationUnits[i]; @@ -356,20 +364,20 @@ export class CompilerDriver { name = "func_main_0"; } else if (ts.isConstructorDeclaration(node)) { let classNode = node.parent; - name = this.getInternalNameForCtor(classNode, node); + return this.getInternalNameForCtor(classNode, node); } else { let funcNode = node; name = (recorder.getScopeOfNode(funcNode)).getFuncName(); if (name == '') { if ((ts.isFunctionDeclaration(node) && hasExportKeywordModifier(node) && hasDefaultKeywordModifier(node)) || ts.isExportAssignment(findOuterNodeOfParenthesis(node))) { - return 'default'; + return `${this.recordName}.default`; } - return `#${this.getFuncId(funcNode)}#`; + return `${this.recordName}.#${this.getFuncId(funcNode)}#`; } if (name == "func_main_0") { - return `#${this.getFuncId(funcNode)}#${name}`; + return `${this.recordName}.#${this.getFuncId(funcNode)}#${name}`; } let funcNameMap = recorder.getFuncNameMap(); @@ -386,7 +394,7 @@ export class CompilerDriver { name = `#${this.getFuncId(funcNode)}#` } } - return name; + return `${this.recordName}.${name}`; } getInternalNameForCtor(node: ts.ClassLikeDeclaration, ctor: ts.ConstructorDeclaration) { @@ -395,7 +403,7 @@ export class CompilerDriver { if (name.lastIndexOf(".") != -1) { name = `#${this.getFuncId(ctor)}#` } - return name; + return `${this.recordName}.${name}`; } writeBinaryFile(pandaGen: PandaGen) { diff --git a/ts2panda/src/expression/arrayLiteralExpression.ts b/ts2panda/src/expression/arrayLiteralExpression.ts index 1d16860880ccd9101984bc037477ab55d1e0d2f8..3f8b3152a3a64e8ec66813d3775a34e9caf8a9a0 100644 --- a/ts2panda/src/expression/arrayLiteralExpression.ts +++ b/ts2panda/src/expression/arrayLiteralExpression.ts @@ -162,9 +162,8 @@ function emitCreateArrayWithBuffer(pandaGen: PandaGen, literalBuffer: LiteralBuf if (literalBuffer.isEmpty()) { pandaGen.createEmptyArray(element); } else { - let literalArrayBuffer = PandaGen.getLiteralArrayBuffer(); - let bufferIdx = literalArrayBuffer.length; - literalArrayBuffer.push(literalBuffer); + let bufferIdx = PandaGen.getLiteralArrayBuffer().length; + PandaGen.appendLiteralArrayBuffer(literalBuffer); pandaGen.createArrayWithBuffer(element, bufferIdx); } } diff --git a/ts2panda/src/expression/objectLiteralExpression.ts b/ts2panda/src/expression/objectLiteralExpression.ts index b7f34f646dd7f74a5c15af4082a60ae79b34520a..4b4adffe436d20433796a09efb6ac7cdbc605ad0 100644 --- a/ts2panda/src/expression/objectLiteralExpression.ts +++ b/ts2panda/src/expression/objectLiteralExpression.ts @@ -112,9 +112,8 @@ function createObject(expr: ts.ObjectLiteralExpression, pandaGen: PandaGen, objR if (literalBuffer.isEmpty()) { pandaGen.createEmptyObject(expr); } else { - let literalArrayBuffer = PandaGen.getLiteralArrayBuffer(); - let bufferIdx = literalArrayBuffer.length; - literalArrayBuffer.push(literalBuffer); + let bufferIdx = PandaGen.getLiteralArrayBuffer().length; + PandaGen.appendLiteralArrayBuffer(literalBuffer); if (hasMethod) { let env = compiler.getCurrentEnv(); pandaGen.createObjectHavingMethod(expr, bufferIdx, env); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index e243c78b0d2e4615206e0435985e8a5be7ff6307..9e6b80bdd22373f723a80b034f6ee3d531ee70f7 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -45,7 +45,7 @@ function checkIsGlobalDeclaration(sourceFile: ts.SourceFile) { function generateDTs(node: ts.SourceFile, options: ts.CompilerOptions) { let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); @@ -90,7 +90,7 @@ function main(fileNames: string[], options: ts.CompilerOptions) { (ctx: ts.TransformationContext) => { return (node: ts.SourceFile) => { let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); compilerDriver.compileForSyntaxCheck(node); return node; } @@ -118,7 +118,8 @@ function main(fileNames: string[], options: ts.CompilerOptions) { node = transformCommonjsModule(node); } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, getRecordName(node)); + CompilerDriver.srcNode = node; setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); compilerDriver.showStatistics(); @@ -176,6 +177,21 @@ function getDtsFiles(libDir: string): string[] { return dtsFiles; } +export function getRecordName(node: ts.SourceFile): string { + let recordName = CmdOptions.getRecordName(); + + if (recordName == "") { + let outputBinName = getOutputBinName(node); + recordName = path.basename(outputBinName, path.extname(outputBinName)); + } + + return recordName; +} + +export function getLiteralKey(node: ts.SourceFile, idx:number): string { + return `${getRecordName(node)}_${idx}`; +} + function specifyCustomLib(customLib) { Compiler.Options.Default["lib"] = customLib; let curFiles = fs.readdirSync(__dirname); @@ -202,12 +218,14 @@ function specifyCustomLib(customLib) { const stopWatchingStr = "####"; const watchAbcFileDefaultTimeOut = 10; const watchFileName = "watch_expressions"; +const watchOutputFileName = "Base64Output"; // this path is only available in sdk const es2abcBinaryPath = path["join"](__dirname, "..", "bin", path.sep); const es2abcBinaryName = /^win/.test(require('os').platform()) ? "es2abc.exe" : "es2abc"; const es2abcBase64Input = "--base64Input"; const es2abcDebuggerEvaluateFlag = "--debugger-evaluate-expression"; const es2abcBase64Output = "--base64Output"; +// need to specify the record name as 'Base64Output' in es2abc's commandline; cancel the opMergeAbc option function callEs2pandaToolChain(ideIputStr: string) { let commandLine = es2abcBinaryPath + es2abcBinaryName + " " + es2abcBase64Input + " \"" + ideIputStr + "\" " + @@ -305,7 +323,7 @@ function compileWatchExpression(jsFileName: string, errorMsgFileName: string, op return (node: ts.SourceFile) => { if (path.basename(node.fileName) == fileName) { node = sourceFile; } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, watchOutputFileName); compilerDriver.compileForSyntaxCheck(node); return node; } @@ -330,7 +348,7 @@ function compileWatchExpression(jsFileName: string, errorMsgFileName: string, op node = ts.factory.updateSourceFile(node, newStatements); } let outputBinName = getOutputBinName(node); - let compilerDriver = new CompilerDriver(outputBinName); + let compilerDriver = new CompilerDriver(outputBinName, watchOutputFileName); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); compilerDriver.compile(node); return node; @@ -346,10 +364,11 @@ function launchWatchEvaluateDeamon(parsed: ts.ParsedCommandLine | undefined) { console.log("startWatchingSuccess supportTimeout"); return; } - let deamonFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchFileName; - let jsFileName = deamonFilePrefix + ".js"; - let abcFileName = deamonFilePrefix + ".abc"; - let errorMsgFileName = deamonFilePrefix + ".err"; + let deamonJSFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchFileName; + let deamonABCFilePrefix = CmdOptions.getEvaluateDeamonPath() + path.sep + watchOutputFileName; + let jsFileName = deamonJSFilePrefix + ".js"; + let abcFileName = deamonABCFilePrefix + ".abc"; + let errorMsgFileName = deamonJSFilePrefix + ".err"; if (fs.existsSync(jsFileName)) { console.log("watchFileServer has been initialized supportTimeout"); diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index a89c41a6c23d8ead45b73271b1a6d22aa9028a66..2d166995044568ef73f530fd0bb1dc9b3940fb7c 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -192,6 +192,8 @@ import { import { CatchTable } from "./statement/tryStatement"; import { TypeRecorder } from "./typeRecorder"; import { Variable } from "./variable"; +import { CompilerDriver } from "./compilerDriver"; +import { getLiteralKey } from "./index"; export class PandaGen { // @ts-ignore @@ -306,14 +308,24 @@ export class PandaGen { } } + static appendLiteralArrayBuffer(litBuf: LiteralBuffer) { + let index = PandaGen.literalArrayBuffer.length; + litBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer.push(litBuf); + } + static appendTypeArrayBuffer(type: BaseType): number { let index = PandaGen.literalArrayBuffer.length; - PandaGen.literalArrayBuffer.push(type.transfer2LiteralBuffer()); + let typeBuf = type.transfer2LiteralBuffer(); + typeBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer.push(typeBuf); return index; } static setTypeArrayBuffer(type: BaseType, index: number) { - PandaGen.literalArrayBuffer[index] = type.transfer2LiteralBuffer(); + let typeBuf = type.transfer2LiteralBuffer(); + typeBuf.setKey(getLiteralKey(CompilerDriver.srcNode, index)); + PandaGen.literalArrayBuffer[index] = typeBuf; } getFirstStmt() { diff --git a/ts2panda/src/pandasm.ts b/ts2panda/src/pandasm.ts index da58e906a6141e48d42f58ae162b93538929529f..549c9a6a3d5ef3cbfdafa48ece5097d14718821c 100644 --- a/ts2panda/src/pandasm.ts +++ b/ts2panda/src/pandasm.ts @@ -109,24 +109,10 @@ export class Function { export class Record { public name: string; - public whole_line: string; - public bound_left: number; - public bound_right: number; - public line_number: number; public metadata: Metadata; - constructor( - name: string, - whole_line: string, - bound_left: number, - bound_right: number, - line_number: number - ) { + constructor(name: string) { this.name = name; - this.whole_line = whole_line; - this.bound_left = bound_left; - this.bound_right = bound_right; - this.line_number = line_number; this.metadata = new Metadata(); } } diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index ace78ccba5dac0c932900b0dabb51245a86a631c..c135ac813266dad92f6edae635be1133f1fae1ef 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -252,15 +252,14 @@ function compileUnCompiledVariable(compiler: Compiler, prop: Property, classReg: function createClassLiteralBuf(compiler: Compiler, classBuffer: LiteralBuffer, stmt: ts.ClassLikeDeclaration, vregs: VReg[]) { - let classLiteralBuf = PandaGen.getLiteralArrayBuffer(); - classLiteralBuf.push(classBuffer); + PandaGen.appendLiteralArrayBuffer(classBuffer); let ctorNode = compiler.getRecorder().getCtorOfClass(stmt); let internalName = compiler.getCompilerDriver().getInternalNameForCtor(stmt, ctorNode); let pandaGen = compiler.getPandaGen(); let parameterLength = getParameterLength4Ctor(stmt); - let buffIdx = classLiteralBuf.length - 1; + let buffIdx = PandaGen.getLiteralArrayBuffer().length - 1; pandaGen.defineClassWithBuffer(stmt, internalName, buffIdx, parameterLength, vregs[0]); pandaGen.storeAccumulator(stmt, vregs[1]); } diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index 190871169d9fdc1d8dde509ca1f0a5c30bc3ec73..5e8184f161873256e2c6444c729781ddcd893d3e 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -37,7 +37,8 @@ import { ModuleRecord, NamespaceImportEntry, RegularImportEntry, - Signature + Signature, + Record } from "./pandasm"; import { generateCatchTables } from "./statement/tryStatement"; import { @@ -54,6 +55,7 @@ import { ModuleScope } from "./scope"; import { TypeRecorder } from "./typeRecorder"; import { isGlobalDeclare } from "./strictMode"; import { isFunctionLikeDeclaration } from "./syntaxCheckHelper"; +import { getLiteralKey } from "./index"; const dollarSign: RegExp = /\$/g; @@ -64,7 +66,8 @@ const JsonType = { "literal_arr": 3, "module": 4, "options": 5, - 'type_info': 6 + 'type_info': 6, + 'record_name': 7 }; export class Ts2Panda { static strings: Set = new Set(); @@ -191,6 +194,20 @@ export class Ts2Panda { }); } + static dumpRecordName(ts2abc: any, recordName: string) { + let recordNameObject = { + "t": JsonType.record_name, + "rn": recordName + } + + let jsonRecordName = escapeUnicode(JSON.stringify(recordNameObject, null, 2)); + jsonRecordName = "$" + jsonRecordName.replace(dollarSign, '#$') + "$"; + if (CmdOptions.isEnableDebugLog()) { + Ts2Panda.jsonString += jsonRecordName; + } + ts2abc.stdio[3].write(jsonRecordName + '\n'); + } + static dumpCmdOptions(ts2abc: any): void { let options = { "t": JsonType.options, @@ -201,7 +218,9 @@ export class Ts2Panda { "opt_level": CmdOptions.getOptLevel(), "opt_log_level": CmdOptions.getOptLogLevel(), "display_typeinfo": CmdOptions.getDisplayTypeinfo(), - "is_dts_file": isGlobalDeclare() + "is_dts_file": isGlobalDeclare(), + "output-proto": CmdOptions.getOutputproto(), + "record_type": CmdOptions.needRecordType() }; let jsonOpt = JSON.stringify(options, null, 2); jsonOpt = "$" + jsonOpt.replace(dollarSign, '#$') + "$"; @@ -211,6 +230,19 @@ export class Ts2Panda { ts2abc.stdio[3].write(jsonOpt + '\n'); } + static dumpRecord(ts2abc: any, recordName: string): void { + let record = { + "t": JsonType.record, + "rb": new Record(recordName) + } + let jsonRecord = escapeUnicode(JSON.stringify(record, null, 2)); + jsonRecord = "$" + jsonRecord.replace(dollarSign, '#$') + "$"; + if (CmdOptions.isEnableDebugLog()) { + Ts2Panda.jsonString += jsonRecord; + } + ts2abc.stdio[3].write(jsonRecord + '\n'); + } + // @ts-ignore static dumpInstTypeMap(pg: PandaGen): any { let insts = pg.getInsns(); @@ -320,7 +352,7 @@ export class Ts2Panda { } typeInfo = Ts2Panda.dumpInstTypeMap(pg); - if (funcName == "func_main_0") { + if (funcName.endsWith("func_main_0")) { let exportedTypes = PandaGen.getExportedTypes(); let declareddTypes = PandaGen.getDeclaredTypes(); if (exportedTypes.size != 0) { @@ -422,9 +454,9 @@ export class Ts2Panda { static dumpTypeInfoRecord(ts2abc: any, recordType: boolean): void { let enableTypeRecord = getRecordTypeFlag(recordType); - let typeSummaryIndex = 0; + let typeSummaryIndex = getLiteralKey(CompilerDriver.srcNode, 0); if (enableTypeRecord) { - typeSummaryIndex = TypeRecorder.getInstance().getTypeSummaryIndex(); + typeSummaryIndex = getLiteralKey(CompilerDriver.srcNode, TypeRecorder.getInstance().getTypeSummaryIndex()); } let typeInfo = { 'tf': enableTypeRecord, diff --git a/ts2panda/tests/builtIns.test.ts b/ts2panda/tests/builtIns.test.ts index ace7fc1c21db2e0dc2a952c00e3363df2d1b622a..590a3131b225805548f66cf10cfa8ce550ce5a98 100644 --- a/ts2panda/tests/builtIns.test.ts +++ b/ts2panda/tests/builtIns.test.ts @@ -45,7 +45,7 @@ describe("FunctionToStringTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileAfter(`function foo() {return 123;}\nfunction bar() {return 321;}\n`, 'toStringTest.js'); CmdOptions.needRecordSourceCode = () => {return false}; - let pandaGen = snippetCompiler.getPandaGenByName('foo'); + let pandaGen = snippetCompiler.getPandaGenByName('UnitTest.foo'); let expected = "function foo() {return 123;}"; expect(pandaGen.getSourceCode() == expected).to.be.true; }) diff --git a/ts2panda/tests/commonjs.test.ts b/ts2panda/tests/commonjs.test.ts index f0148848a4a5ffc5818d88be96e4f84509f2c406..119cce756fa8f9e405fb4d2f936ed39872e9bc0a 100644 --- a/ts2panda/tests/commonjs.test.ts +++ b/ts2panda/tests/commonjs.test.ts @@ -44,7 +44,7 @@ describe("CommonJsTest", function () { CmdOptions.isCommonJs = () => {return false}; let funcMainInsns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn('#1#', new Imm(5), new VReg()), + new EcmaDefinefuncdyn('UnitTest.#1#', new Imm(5), new VReg()), new StaDyn(new VReg()), new LdaDyn(new VReg()), new StaDyn(new VReg()), @@ -67,7 +67,7 @@ describe("CommonJsTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileCommonjs(`let a = require('a.js')`, 'cjs.js'); CmdOptions.isCommonJs = () => {return false}; - let execInsns = snippetCompiler.getPandaGenByName('#1#')!.getInsns(); + let execInsns = snippetCompiler.getPandaGenByName('UnitTest.#1#')!.getInsns(); let requirePara = new VReg(); let requireReg = new VReg(); let moduleRequest = new VReg(); @@ -88,7 +88,7 @@ describe("CommonJsTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compileCommonjs(`let a = 1; exports.a = a;`, 'cjs.js'); CmdOptions.isCommonJs = () => {return false}; - let execInsns = snippetCompiler.getPandaGenByName('#1#')!.getInsns(); + let execInsns = snippetCompiler.getPandaGenByName('UnitTest.#1#')!.getInsns(); let exportsPara = new VReg(); let exportsReg = new VReg(); let tmpReg = new VReg(); diff --git a/ts2panda/tests/esmodule.test.ts b/ts2panda/tests/esmodule.test.ts index e28b9b6db430b7b7535d080121fb5d35d76fe466..f88272059a29c09f4de21561f435532931ed4b0d 100644 --- a/ts2panda/tests/esmodule.test.ts +++ b/ts2panda/tests/esmodule.test.ts @@ -45,7 +45,7 @@ describe("ExportDeclaration", function () { let classReg = new VReg(); let expected = [ new MovDyn(new VReg(), new VReg()), - new EcmaDefineclasswithbuffer("#1#C", new Imm(0), new Imm(0), new VReg(), new VReg()), + new EcmaDefineclasswithbuffer("UnitTest.#1#C", new Imm(0), new Imm(0), new VReg(), new VReg()), new StaDyn(classReg), new LdaDyn(classReg), new EcmaStmodulevar('C'), diff --git a/ts2panda/tests/expression/arguments.test.ts b/ts2panda/tests/expression/arguments.test.ts index 3dd2fbd706b16156e10009d51262ed3e4564493e..350836037e894a7fc1fcd68eed38b36759a740c3 100644 --- a/ts2panda/tests/expression/arguments.test.ts +++ b/ts2panda/tests/expression/arguments.test.ts @@ -43,7 +43,7 @@ describe("arguments Keyword", function () { new EcmaLdobjbyindex(temp1, new Imm(0)), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("foo"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected)).to.be.true; @@ -62,7 +62,7 @@ describe("arguments Keyword", function () { new EcmaLdobjbyindex(temp1, new Imm(0)), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("foo"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.foo"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected)).to.be.true; diff --git a/ts2panda/tests/expression/commalist.test.ts b/ts2panda/tests/expression/commalist.test.ts index 5163fded64bb57bf427a5643c90c899cc5b69504..0681472412192c4085b4c0a896f269cb4d84a69b 100644 --- a/ts2panda/tests/expression/commalist.test.ts +++ b/ts2panda/tests/expression/commalist.test.ts @@ -54,7 +54,7 @@ describe("CommaListExpression", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ new MovDyn(new VReg(), new VReg()), - new EcmaDefineclasswithbuffer("#1#Test", new Imm(0), new Imm(0), new VReg(), new VReg()), + new EcmaDefineclasswithbuffer("UnitTest.#1#Test", new Imm(0), new Imm(0), new VReg(), new VReg()), new StaDyn(new VReg()), new LdaDyn(new VReg()), new EcmaStclasstoglobalrecord("Test"), diff --git a/ts2panda/tests/expression/functionExpression.test.ts b/ts2panda/tests/expression/functionExpression.test.ts index e04bb8ea3ed2cae536798ee94c35d6a2d1de935d..f1b0624fd1f4154671acfc453472df027f860755 100644 --- a/ts2panda/tests/expression/functionExpression.test.ts +++ b/ts2panda/tests/expression/functionExpression.test.ts @@ -72,7 +72,7 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "test") { + if (pg.internalName == "UnitTest.test") { expect(checkInstructions(pg.getInsns(), expected_func), "check func insns").to.be.true; checkCount++; } @@ -90,15 +90,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinefuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -118,15 +118,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinefuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -146,15 +146,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -182,16 +182,16 @@ describe("compileFunctionExpression", function () { ]; pandaGens.forEach((pg) => { - if (pg.internalName == "p") { + if (pg.internalName == "UnitTest.p") { expect(checkInstructions(pg.getInsns(), expected_func), "check arrow func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('p'); + expect(insns.operands[0]).to.equal('UnitTest.p'); checkCount++; } }); @@ -283,15 +283,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { expect(checkInstructions(pg.getInsns(), expected_func), "check generator func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinegeneratorfunc) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); @@ -343,15 +343,15 @@ describe("compileFunctionExpression", function () { let checkCount = 0; pandaGens.forEach((pg) => { - if (pg.internalName == "a") { + if (pg.internalName == "UnitTest.a") { expect(checkInstructions(pg.getInsns(), expected_func), "check async func insns").to.be.true; checkCount++; } - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { pg.getInsns().forEach((insns) => { if (insns instanceof EcmaDefinencfuncdyn) { - expect(insns.operands[0]).to.equal('a'); + expect(insns.operands[0]).to.equal('UnitTest.a'); checkCount++; } }); diff --git a/ts2panda/tests/expression/thisKeyWord.test.ts b/ts2panda/tests/expression/thisKeyWord.test.ts index e88f39146dfb25e0de7fda3e1120a23536077968..486902b4581f6bf300d6d256e54a74ed8ea8d176 100644 --- a/ts2panda/tests/expression/thisKeyWord.test.ts +++ b/ts2panda/tests/expression/thisKeyWord.test.ts @@ -50,7 +50,7 @@ describe("ThisKeyword", function () { it("this in function scope", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {this}"); - let functionPg = snippetCompiler.getPandaGenByName("a"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = functionPg!.getScope(); let insns = compileMainSnippet("this;", pandaGen, functionScope); let expected = [ diff --git a/ts2panda/tests/hoist.test.ts b/ts2panda/tests/hoist.test.ts index ffd5a7fe4d822ceccdab35b81498a5b26f4d626d..e048a0eca7feb52103d4951ad1e44f75282c1cb8 100644 --- a/ts2panda/tests/hoist.test.ts +++ b/ts2panda/tests/hoist.test.ts @@ -29,7 +29,6 @@ import { LdaDyn, LdaiDyn, LdaStr, - ResultType, StaDyn, VReg } from "../src/irnodes"; @@ -79,7 +78,7 @@ describe("HoistTest", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new EcmaReturnundefined() ] @@ -93,7 +92,7 @@ describe("HoistTest", function () { let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("#2#a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#2#a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new EcmaReturnundefined() ] @@ -107,7 +106,7 @@ describe("HoistTest", function () { snippetCompiler.compile(`var a = 1; function a() {}`); let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("a", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.a", new Imm(0), new VReg()), new EcmaStglobalvar("a"), new LdaiDyn(new Imm(1)), new EcmaStglobalvar("a"), @@ -121,7 +120,7 @@ describe("HoistTest", function () { it('case 6', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`function a() {var a = 1;}`); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let insns = funcPg!.getInsns(); let a = new VReg(); @@ -140,11 +139,11 @@ describe("HoistTest", function () { it('case 7', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`function a() {function b() {}};`); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let insns = funcPg!.getInsns(); let a = new VReg(); let expected = [ - new EcmaDefinefuncdyn("b", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.b", new Imm(0), new VReg()), new StaDyn(a), new EcmaReturnundefined() @@ -158,7 +157,7 @@ describe("HoistTest", function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile(`a = 1; let a;`); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let insns = funcPg!.getInsns(); let idReg = new VReg(); let expected = [ @@ -177,7 +176,7 @@ describe("HoistTest", function () { a = 1; let a; }`); - let funcPg = snippetCompiler.getPandaGenByName("b"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.b"); let insns = funcPg!.getInsns(); let idReg = new VReg(); @@ -197,7 +196,7 @@ describe("HoistTest", function () { a = 1; let a; }`); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let insns = funcPg!.getInsns(); let idReg = new VReg(); diff --git a/ts2panda/tests/lexenv.test.ts b/ts2panda/tests/lexenv.test.ts index a57433b157103c953a6fb68014569a1c92f53c64..d6752672b91ed5372672d0b799335010fa4a38f7 100644 --- a/ts2panda/tests/lexenv.test.ts +++ b/ts2panda/tests/lexenv.test.ts @@ -39,7 +39,6 @@ import { LdaDyn, LdaiDyn, LdaStr, - ResultType, ReturnDyn, StaDyn, VReg @@ -116,7 +115,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { it("test CompilerDriver.scanFunctions-with-empty", function () { let source: string = ``; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); recorder.record(); @@ -144,7 +143,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { var funcExpression = function() { } `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); recorder.record(); @@ -191,7 +190,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let source: string = ` `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -215,7 +214,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { var funcExt = function() { } `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -269,7 +268,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { }))) `; let sourceFile = creatAstFromSnippet(source); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); let globalScope = new GlobalScope(sourceFile); let recorder = new Recorder(sourceFile, globalScope, compilerDriver, false, false, true); @@ -412,7 +411,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let expected_main = [ new LdaDyn(new VReg()), new EcmaStglobalvar("outer"), - new EcmaDefinefuncdyn("func", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.func", new Imm(0), new VReg()), new EcmaStglobalvar("func"), new LdaiDyn(new Imm(1)), new EcmaStglobalvar("outer"), @@ -425,9 +424,9 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { ]; pandaGens.forEach((pg) => { - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { expect(checkInstructions(pg.getInsns(), expected_main)).to.be.true; - } else if (pg.internalName == "func") { + } else if (pg.internalName == "UnitTest.func") { expect(checkInstructions(pg.getInsns(), expected_func)).to.be.true; } }) @@ -447,7 +446,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { let pandaGens = compileAllSnippet(source, passes); let expected_main = [ ...insnsCreateLexEnv_main, - new EcmaDefinefuncdyn("func", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.func", new Imm(0), new VReg()), new EcmaStglobalvar("func"), // global.func = func_func_1 new LdaiDyn(new Imm(1)), // value = 1 // ...insnsStoreLexVar_main, @@ -465,11 +464,11 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { pandaGens.forEach((pg) => { let scope = pg.getScope(); - if (pg.internalName == "func_main_0") { + if (pg.internalName == "UnitTest.func_main_0") { expect(checkInstructions(pg.getInsns(), expected_main), "check main insns").to.be.true; expect(scope.getNumLexEnv(), "main scope has 0 lexvar").to.be.equal(0); // expect(scope.hasLexEnv(), "main scope has lexenv").to.be.true; - } else if (pg.internalName == "func") { + } else if (pg.internalName == "UnitTest.func") { expect(checkInstructions(pg.getInsns(), expected_func), "check func insns").to.be.true; expect(scope.getNumLexEnv(), "func scope has 1 lexvar").to.be.equal(0); @@ -505,7 +504,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { new LdaDyn(new VReg()), new StaDyn(new VReg()), ...insnsStoreLexVar_outer_2, - new EcmaDefinefuncdyn("#1#", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#1#", new Imm(0), new VReg()), // returnStatement new StaDyn(new VReg()), new LdaDyn(new VReg()), @@ -536,13 +535,13 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { snippetCompiler.compile(source, passes); // check compile result! - let outerPg = snippetCompiler.getPandaGenByName("outer"); + let outerPg = snippetCompiler.getPandaGenByName("UnitTest.outer"); let outerScope = outerPg!.getScope(); let outerA = outerScope!.findLocal("a"); expect(outerA instanceof LocalVariable, "a in outer is local variable").to.be.true; // expect((outerScope).hasLexEnv(), "outer scope need to create lex env").to.be.true; expect((outerScope).getNumLexEnv(), "number of lexvar at outer scope").to.be.equal(2); - let anonymousPg = snippetCompiler.getPandaGenByName("#1#"); + let anonymousPg = snippetCompiler.getPandaGenByName("UnitTest.#1#"); let anonymousScope = anonymousPg!.getScope(); let anonymousA = anonymousScope!.findLocal("a"); let searchRlt = anonymousScope!.find("a"); @@ -551,7 +550,7 @@ describe("lexenv-compile-testcase in lexenv.test.ts", function () { expect(anonymousA, "no a in anonymous function").to.be.undefined; // expect((anonymousScope).hasLexEnv(), "anonymous scope had lex env").to.be.true; expect((anonymousScope).getNumLexEnv()).to.be.equal(0); - let globalPg = snippetCompiler.getPandaGenByName("func_main_0"); + let globalPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let globalScope = globalPg!.getScope(); let globalA = globalScope!.findLocal("a"); expect(globalA instanceof GlobalVariable, "globalA is GlobalVariable").to.be.true; diff --git a/ts2panda/tests/statements/functionDeclaration.test.ts b/ts2panda/tests/statements/functionDeclaration.test.ts index 3c9b990385ec31f6ff1adf5ed9a18856fb5ea513..079b05e8e453d47311f0f1302df9e8ed0de5e7f6 100644 --- a/ts2panda/tests/statements/functionDeclaration.test.ts +++ b/ts2panda/tests/statements/functionDeclaration.test.ts @@ -29,7 +29,6 @@ import { Label, LdaDyn, LdaiDyn, - ResultType, StaDyn, VReg } from "../../src/irnodes"; @@ -43,9 +42,10 @@ describe("FunctionDeclarationTest", function () { it('function definition in the global scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function foo() {}"); + let funcInternalName = "UnitTest.foo"; let funcName = "foo"; let expected = [ - new EcmaDefinefuncdyn(funcName, new Imm(0), new VReg()), + new EcmaDefinefuncdyn(funcInternalName, new Imm(0), new VReg()), new EcmaStglobalvar(funcName), new EcmaReturnundefined() ]; @@ -64,7 +64,7 @@ describe("FunctionDeclarationTest", function () { function foo() {} `); let expected = [ - new EcmaDefinefuncdyn("#2#foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.#2#foo", new Imm(0), new VReg()), new EcmaStglobalvar("foo"), new EcmaReturnundefined() ]; @@ -81,12 +81,12 @@ describe("FunctionDeclarationTest", function () { snippetCompiler.compile(`function out() {function foo() {}}`); let funcReg = new VReg(); let expected = [ - new EcmaDefinefuncdyn("foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.foo", new Imm(0), new VReg()), new StaDyn(funcReg), new EcmaReturnundefined() ]; - let functionPg = snippetCompiler.getPandaGenByName("out"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.out"); let insns = functionPg!.getInsns(); let functionScope = functionPg!.getScope(); @@ -103,7 +103,7 @@ describe("FunctionDeclarationTest", function () { snippetCompiler.compile("let foo = function() {}"); let insns = snippetCompiler.getGlobalInsns(); let expected = [ - new EcmaDefinefuncdyn("foo", new Imm(0), new VReg()), + new EcmaDefinefuncdyn("UnitTest.foo", new Imm(0), new VReg()), new EcmaStlettoglobalrecord("foo"), new EcmaReturnundefined() ]; @@ -117,7 +117,7 @@ describe("FunctionDeclarationTest", function () { let endLabel = new Label(); let expected_main = [ - new EcmaDefinefuncdyn("test", new Imm(1), new VReg()), + new EcmaDefinefuncdyn("UnitTest.test", new Imm(1), new VReg()), new EcmaStglobalvar("test"), new EcmaReturnundefined() ]; @@ -133,10 +133,10 @@ describe("FunctionDeclarationTest", function () { ]; compilerunit.forEach(element => { - if (element.internalName == "func_main_0") { + if (element.internalName == "UnitTest.func_main_0") { let insns = element.getInsns(); expect(checkInstructions(insns, expected_main)).to.be.true; - } else if (element.internalName == "test") { + } else if (element.internalName == "UnitTest.test") { let insns = element.getInsns(); expect(checkInstructions(insns, expected_func)).to.be.true; let parameterLength = element.getParameterLength(); @@ -158,7 +158,7 @@ describe("FunctionDeclarationTest", function () { new EcmaReturnundefined(), ]; - let functionPg = snippetCompiler.getPandaGenByName("test"); + let functionPg = snippetCompiler.getPandaGenByName("UnitTest.test"); let insns = functionPg!.getInsns(); expect(checkInstructions(insns, expected_func)).to.be.true; diff --git a/ts2panda/tests/statements/variableDeclaration.test.ts b/ts2panda/tests/statements/variableDeclaration.test.ts index 1ce753c2d0ea1ca9bec4aeab8046ad6fecf61a43..2e64168b4d8b383c2c17362c0003f0f3a9112116 100644 --- a/ts2panda/tests/statements/variableDeclaration.test.ts +++ b/ts2panda/tests/statements/variableDeclaration.test.ts @@ -154,7 +154,7 @@ describe("VariableDeclarationTest", function () { it('var i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {var i;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -172,7 +172,7 @@ describe("VariableDeclarationTest", function () { it('let i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {let i;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -188,7 +188,7 @@ describe("VariableDeclarationTest", function () { it('const i in a function scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("function a() {const i = 5;}"); - let funcPg = snippetCompiler.getPandaGenByName("a"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.a"); let functionScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); let expected = [ @@ -204,7 +204,7 @@ describe("VariableDeclarationTest", function () { it('let i in a local scope', function () { let snippetCompiler = new SnippetCompiler(); snippetCompiler.compile("{let i;}"); - let funcPg = snippetCompiler.getPandaGenByName("func_main_0"); + let funcPg = snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let localScope = funcPg!.getScope(); let insns = funcPg!.getInsns(); diff --git a/ts2panda/tests/types/array.test.ts b/ts2panda/tests/types/array.test.ts index edaf90fdfa928ace5bac50f93c05acbf4c9932ed..9742c7faeddbb1e1335cba2c70071c79a2250144 100644 --- a/ts2panda/tests/types/array.test.ts +++ b/ts2panda/tests/types/array.test.ts @@ -30,7 +30,7 @@ describe("array tests in array.test.ts", function() { it("test array with primitives", function() { let fileNames = 'tests/types/array/array_primitives.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -75,7 +75,7 @@ describe("array tests in array.test.ts", function() { it("test array with class", function() { let fileNames = 'tests/types/array/array_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -108,7 +108,7 @@ describe("array tests in array.test.ts", function() { it("test array with multi same primitive", function() { let fileNames = 'tests/types/array/array_multi_same_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -159,7 +159,7 @@ describe("array tests in array.test.ts", function() { it("test array with multi same class", function() { let fileNames = 'tests/types/array/array_multi_same_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -193,7 +193,7 @@ describe("array tests in array.test.ts", function() { it("test array with union", function() { let fileNames = 'tests/types/array/array_union.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -222,7 +222,7 @@ describe("array tests in array.test.ts", function() { it("test array with object", function() { let fileNames = 'tests/types/array/array_object.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/class.test.ts b/ts2panda/tests/types/class.test.ts index 6f076b68e6efdfa6bc276e402851135cb06acbfc..7fa9db6c9b3032e90a00d9bef9022ff2b9425a9c 100644 --- a/ts2panda/tests/types/class.test.ts +++ b/ts2panda/tests/types/class.test.ts @@ -30,7 +30,7 @@ describe("class tests in class.test.ts", function () { it("test class with no parameter in block", function () { let fileNames = 'tests/types/class/class_constr_no_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -66,7 +66,7 @@ describe("class tests in class.test.ts", function () { it("test class with parameter in block", function () { let fileNames = 'tests/types/class/class_constr_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -102,7 +102,7 @@ describe("class tests in class.test.ts", function () { it("test class fields type", function () { let fileNames = 'tests/types/class/class_fields.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -136,7 +136,7 @@ describe("class tests in class.test.ts", function () { it("test class methods type", function () { let fileNames = 'tests/types/class/class_methods.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -176,7 +176,7 @@ describe("class tests in class.test.ts", function () { it("test class static fields type", function () { let fileNames = 'tests/types/class/class_static_fields.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -210,7 +210,7 @@ describe("class tests in class.test.ts", function () { it("test class static methods type", function () { let fileNames = 'tests/types/class/class_static_methods.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -252,7 +252,7 @@ describe("class tests in class.test.ts", function () { it("test abstract class type", function () { let fileNames = 'tests/types/class/class_abstract.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -297,7 +297,7 @@ describe("class tests in class.test.ts", function () { it("test class implements type", function () { let fileNames = 'tests/types/class/class_implements.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/function.test.ts b/ts2panda/tests/types/function.test.ts index 563e2b4c0061209aea968ebd536cca51cda5655d..a9ebb11ad8cb90d7984eaa04b2c97b7570478fea 100644 --- a/ts2panda/tests/types/function.test.ts +++ b/ts2panda/tests/types/function.test.ts @@ -30,7 +30,7 @@ describe("function tests in function.test.ts", function () { it("test function with no parameter", function () { let fileNames = 'tests/types/function/function_no_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -60,7 +60,7 @@ describe("function tests in function.test.ts", function () { it("test function with muti parameter", function () { let fileNames = 'tests/types/function/function_multi_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -92,7 +92,7 @@ describe("function tests in function.test.ts", function () { it("test function with same type of paras and return", function () { let fileNames = 'tests/types/function/function_same_para_and_return.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -131,7 +131,7 @@ describe("function tests in function.test.ts", function () { it("test function with class as parameter", function () { let fileNames = 'tests/types/function/function_class_para.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -172,7 +172,7 @@ describe("function tests in function.test.ts", function () { it("test function with class as return", function () { let fileNames = 'tests/types/function/function_class_return.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/object.test.ts b/ts2panda/tests/types/object.test.ts index eae94789e7537a7db90d785a20d536c614dada98..9d10d530621e9982b1217c165da81b4792cda7de 100644 --- a/ts2panda/tests/types/object.test.ts +++ b/ts2panda/tests/types/object.test.ts @@ -30,7 +30,7 @@ describe("object tests in object.test.ts", function() { it("test object with primitives", function() { let fileNames = 'tests/types/object/object_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -56,7 +56,7 @@ describe("object tests in object.test.ts", function() { it("test object with user defined type", function() { let fileNames = 'tests/types/object/object_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/primitives.test.ts b/ts2panda/tests/types/primitives.test.ts index d05b7c1ba2b25665ec77173c11029d61673b0af0..9fab6e6a776273ff1b256cea40afea29a3dde932 100644 --- a/ts2panda/tests/types/primitives.test.ts +++ b/ts2panda/tests/types/primitives.test.ts @@ -30,7 +30,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in block", function() { let fileNames = 'tests/types/primitives/primitives_in_block.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -60,7 +60,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test number in function", function() { let fileNames = 'tests/types/primitives/primitives_in_function.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("numberFunc"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.numberFunc"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -100,7 +100,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in for", function() { let fileNames = 'tests/types/primitives/primitives_in_for.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -131,7 +131,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in if", function() { let fileNames = 'tests/types/primitives/primitives_in_if.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -161,7 +161,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives in class", function() { let fileNames = 'tests/types/primitives/primitives_in_class.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -198,7 +198,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives with only type annotations", function() { let fileNames = 'tests/types/primitives/primitives_only_type_annotation.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -226,7 +226,7 @@ describe("primitives tests in primitives.test.ts", function() { it("test primitives without type annotations", function() { let fileNames = 'tests/types/primitives/primitives_no_type_annotation.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/types/union.test.ts b/ts2panda/tests/types/union.test.ts index a1dc513747e159af529ef78b482d171433a9eefd..ec680273563a76b2b8ecf5eaea441d52bec6689d 100644 --- a/ts2panda/tests/types/union.test.ts +++ b/ts2panda/tests/types/union.test.ts @@ -30,7 +30,7 @@ describe("union tests in union.test.ts", function () { it("test union with primitives", function () { let fileNames = 'tests/types/union/union_primitives.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -68,7 +68,7 @@ describe("union tests in union.test.ts", function () { it("test union with user defined type", function () { let fileNames = 'tests/types/union/union_userDefinedType.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -104,7 +104,7 @@ describe("union tests in union.test.ts", function () { it("test union with multi same primitives", function () { let fileNames = 'tests/types/union/union_multi_same_primi.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ @@ -130,7 +130,7 @@ describe("union tests in union.test.ts", function () { it("test union with multi same user defined type", function () { let fileNames = 'tests/types/union/union_multi_userDefinedType.ts'; let result = compileTsWithType(fileNames); - let functionPg = result.snippetCompiler.getPandaGenByName("func_main_0"); + let functionPg = result.snippetCompiler.getPandaGenByName("UnitTest.func_main_0"); let locals = functionPg!.getLocals(); // check vreg let extectedVRegTypePair = [ diff --git a/ts2panda/tests/utils/base.ts b/ts2panda/tests/utils/base.ts index 2d9f7ed31846e8178766f3856c858f318b605516..49c7c5b52dc2c68d17b0349dabab3e19c80d2a1c 100644 --- a/ts2panda/tests/utils/base.ts +++ b/ts2panda/tests/utils/base.ts @@ -146,7 +146,7 @@ export function compileAllSnippet(snippet: string, passes?: Pass[], literalBuffe jshelpers.bindSourceFile(sourceFile, {}); CmdOptions.isWatchEvaluateExpressionMode() ? setGlobalStrict(true) : setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(sourceFile, compileOptions)); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); if (!passes) { passes = []; @@ -166,7 +166,7 @@ export function compileMainSnippet(snippet: string, pandaGen?: PandaGen, scope?: // only return main function if (compileFunc) { compileUnits.filter((pg) => { - return (pg.internalName == "func_main_0"); + return (pg.internalName == "UnitTest.func_main_0"); }) } @@ -191,7 +191,7 @@ export function compileAfterSnippet(snippet: string, name:string, isCommonJs: bo } jshelpers.bindSourceFile(sourceFile, {}); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(sourceFile, compileOptions)); - let compilerDriver = new CompilerDriver('UnitTest'); + let compilerDriver = new CompilerDriver('UnitTest', 'UnitTest'); compilerDriver.setCustomPasses([]); compilerDriver.compileUnitTest(sourceFile, []); compileUnits = compilerDriver.getCompilationUnits(); @@ -228,7 +228,7 @@ export class SnippetCompiler { } getGlobalInsns(): IRNode[] { - let root = this.getPandaGenByName("func_main_0"); + let root = this.getPandaGenByName("UnitTest.func_main_0"); if (root) { return root.getInsns(); } else { @@ -237,7 +237,7 @@ export class SnippetCompiler { } getGlobalScope(): Scope | undefined { - let globalPandaGen = this.getPandaGenByName("func_main_0"); + let globalPandaGen = this.getPandaGenByName("UnitTest.func_main_0"); return globalPandaGen ? globalPandaGen.getScope()!.getNearestVariableScope() : undefined; } diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index ebd96d2f48ed0c7b34cb6b6d9a3aa652fe951d03..1a27f4636bd20ab6e704b3d2b3eacf1356ba2b0a 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -45,9 +45,12 @@ bool g_displayTypeinfo = false; bool g_isDtsFile = false; std::string g_optLogLevel = "error"; uint32_t g_literalArrayCount = 0; +int32_t g_newLiteralArrayIndex = -1; static constexpr const char* TSTYPE_ANNO_RECORD_NAME = "_ESTypeAnnotation"; static constexpr const char* TSTYPE_ANNO_ELEMENT_NAME = "_TypeOfInstruction"; std::string g_compilerOutputProto = ""; +std::string g_recordName = ""; +constexpr uint32_t LITERALBUFFERINDEXOFFSET = 100; constexpr std::size_t BOUND_LEFT = 0; constexpr std::size_t BOUND_RIGHT = 0; @@ -65,7 +68,6 @@ std::unordered_map g_opcodeMap = { static panda::pandasm::Record MakeRecordDefinition(const std::string &name) { auto record = panda::pandasm::Record(name, LANG_EXT); - return record; } @@ -168,6 +170,11 @@ static std::string ConvertUtf8ToMUtf8(const std::string &data) return ConvertUtf16ToMUtf8(u16Data, u16DataSize); } +static std::string GetLiteralId(int64_t index) +{ + return g_recordName + "_" + std::to_string(index); +} + static std::string ParseUnicodeEscapeString(const std::string &data) { const int unicodeEscapeSymbolLen = 2; @@ -265,8 +272,16 @@ static void ParseLiteral(const Json::Value &literal, std::vector(panda::panda_file::LiteralTag::LITERALBUFFERINDEX): { - valueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALBUFFERINDEX; - valueLiteral.value_ = static_cast(literal["v"].asInt()); + UNREACHABLE(); + } + case static_cast(panda::panda_file::LiteralTag::LITERALARRAY): { + valueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + valueLiteral.value_ = ParseString(literal["v"].asString()); + break; + } + case static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX): { + valueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + valueLiteral.value_ = static_cast(literal["v"].asInt()); break; } case static_cast(panda::panda_file::LiteralTag::NULLVALUE): { @@ -649,27 +664,108 @@ static std::vector> GetInstTypeMap(const Json::Valu return instTypeMap; } -static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static void ParseFunctionTypeInfo(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { auto instTypeMap = GetInstTypeMap(function, pandaFunc); if (!instTypeMap.empty()) { - std::vector elements; + std::vector literalArray; for (auto &it : instTypeMap) { - elements.emplace_back(panda::pandasm::ScalarValue::Create(it.first)); - elements.emplace_back(panda::pandasm::ScalarValue::Create(it.second)); + panda::pandasm::LiteralArray::Literal instTagLiteral; + panda::pandasm::LiteralArray::Literal instValueLiteral; + instTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + instTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::INTEGER); + literalArray.emplace_back(instTagLiteral); + instValueLiteral.tag_ = panda::panda_file::LiteralTag::INTEGER; + instValueLiteral.value_ = static_cast(it.first); + literalArray.emplace_back(instValueLiteral); + + panda::pandasm::LiteralArray::Literal typeTagLiteral; + panda::pandasm::LiteralArray::Literal typeValueLiteral; + typeTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + auto type = it.second; + if (type < LITERALBUFFERINDEXOFFSET) { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::INTEGER); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + typeValueLiteral.value_ = static_cast(type); + } else { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string typeId = g_recordName + "_" + std::to_string(type - LITERALBUFFERINDEXOFFSET); + typeValueLiteral.value_ = typeId; + } + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); } + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------function typeinfo litId------------------" << litId << std::endl; + auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); + panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); panda::pandasm::AnnotationElement typeOfVregElement( - TSTYPE_ANNO_ELEMENT_NAME, std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, elements))); + TSTYPE_ANNO_ELEMENT_NAME, std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(typeOfVregElement)); const_cast&>(pandaFunc.metadata->GetAnnotations()).push_back( std::move(funcAnnotation)); } } -static void ParseFunctionExportedType(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static std::string CreateLiteralArrayForType(const Json::Value &types, panda::pandasm::Program &prog) +{ + std::vector literalArray; + for (Json::ArrayIndex i = 0; i < types.size(); i++) { + auto type = types[i]; + if (!type.isObject()) { + continue; + } + + panda::pandasm::LiteralArray::Literal symbolTagLiteral; + panda::pandasm::LiteralArray::Literal symbolValueLiteral; + std::string symbol = ""; + if (type.isMember("symbol") && type["symbol"].isString()) { + symbol = type["symbol"].asString(); + } + symbolTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + symbolTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::STRING); + symbolValueLiteral.tag_ = panda::panda_file::LiteralTag::STRING; + symbolValueLiteral.value_ = symbol; + literalArray.emplace_back(symbolTagLiteral); + literalArray.emplace_back(symbolValueLiteral); + + panda::pandasm::LiteralArray::Literal typeTagLiteral; + panda::pandasm::LiteralArray::Literal typeValueLiteral; + uint32_t typeIndex = 0; + if (type.isMember("type") && type["type"].isInt()) { + typeIndex = type["type"].asUInt(); + } + typeTagLiteral.tag_ = panda::panda_file::LiteralTag::TAGVALUE; + if (typeIndex < LITERALBUFFERINDEXOFFSET) { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::BUILTINTYPEINDEX); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::BUILTINTYPEINDEX; + typeValueLiteral.value_ = static_cast(typeIndex); + } else { + typeTagLiteral.value_ = static_cast(panda::panda_file::LiteralTag::LITERALARRAY); + typeValueLiteral.tag_ = panda::panda_file::LiteralTag::LITERALARRAY; + std::string litId = GetLiteralId(typeIndex - LITERALBUFFERINDEXOFFSET); + typeValueLiteral.value_ = litId; + } + + literalArray.emplace_back(typeTagLiteral); + literalArray.emplace_back(typeValueLiteral); + } + + std::string litId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------function literalArray of type litId------------------" << litId << std::endl; + auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); + return litId; +} + +static void ParseFunctionExportedType(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { std::string funcName = ""; if (function.isMember("n") && function["n"].isString()) { @@ -682,42 +778,12 @@ static void ParseFunctionExportedType(const Json::Value &function, panda::pandas if (function.isMember("es2t") && function["es2t"].isArray()) { auto exportedTypes = function["es2t"]; panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); - std::vector symbolElements; - std::vector symbolTypeElements; - for (Json::ArrayIndex i = 0; i < exportedTypes.size(); i++) { - auto exportedType = exportedTypes[i]; - if (!exportedType.isObject()) { - continue; - } - - std::string exportedSymbol = ""; - if (exportedType.isMember("symbol") && exportedType["symbol"].isString()) { - exportedSymbol = exportedType["symbol"].asString(); - } - - uint32_t typeIndex = 0; - if (exportedType.isMember("type") && exportedType["type"].isInt()) { - typeIndex = exportedType["type"].asUInt(); - } - - panda::pandasm::ScalarValue symbol( - panda::pandasm::ScalarValue::Create(exportedSymbol)); - symbolElements.emplace_back(std::move(symbol)); - panda::pandasm::ScalarValue tIndex( - panda::pandasm::ScalarValue::Create(typeIndex)); - symbolTypeElements.emplace_back(std::move(tIndex)); - } - - std::string symbolAnnotationName = "exportedSymbols"; - panda::pandasm::AnnotationElement exportedSymbolsElement(symbolAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::STRING, symbolElements))); - funcAnnotation.AddElement(std::move(exportedSymbolsElement)); + std::string litId = CreateLiteralArrayForType(exportedTypes, prog); std::string symbolTypeAnnotationName = "exportedSymbolTypes"; panda::pandasm::AnnotationElement exportedSymbolTypesElement(symbolTypeAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, symbolTypeElements))); + std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(exportedSymbolTypesElement)); const_cast&>( @@ -725,7 +791,8 @@ static void ParseFunctionExportedType(const Json::Value &function, panda::pandas } } -static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandasm::Function &pandaFunc) +static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandasm::Function &pandaFunc, + panda::pandasm::Program &prog) { std::string funcName = ""; if (function.isMember("n") && function["n"].isString()) { @@ -738,42 +805,12 @@ static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandas if (function.isMember("ds2t") && function["ds2t"].isArray()) { auto declaredTypes = function["ds2t"]; panda::pandasm::AnnotationData funcAnnotation(TSTYPE_ANNO_RECORD_NAME); - std::vector symbolElements; - std::vector symbolTypeElements; - for (Json::ArrayIndex i = 0; i < declaredTypes.size(); i++) { - auto declaredType = declaredTypes[i]; - if (!declaredType.isObject()) { - continue; - } - - std::string declaredSymbol = ""; - if (declaredType.isMember("symbol") && declaredType["symbol"].isString()) { - declaredSymbol = declaredType["symbol"].asString(); - } - - uint32_t typeIndex = 0; - if (declaredType.isMember("type") && declaredType["type"].isInt()) { - typeIndex = declaredType["type"].asUInt(); - } - - panda::pandasm::ScalarValue symbol( - panda::pandasm::ScalarValue::Create(declaredSymbol)); - symbolElements.emplace_back(std::move(symbol)); - panda::pandasm::ScalarValue tIndex( - panda::pandasm::ScalarValue::Create(typeIndex)); - symbolTypeElements.emplace_back(std::move(tIndex)); - } - - std::string symbolAnnotationName = "declaredSymbols"; - panda::pandasm::AnnotationElement declaredSymbolsElement(symbolAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::STRING, symbolElements))); - funcAnnotation.AddElement(std::move(declaredSymbolsElement)); + std::string litId = CreateLiteralArrayForType(declaredTypes, prog); std::string symbolTypeAnnotationName = "declaredSymbolTypes"; panda::pandasm::AnnotationElement declaredSymbolTypesElement(symbolTypeAnnotationName, - std::make_unique(panda::pandasm::ArrayValue( - panda::pandasm::Value::Type::U32, symbolTypeElements))); + std::make_unique( + panda::pandasm::ScalarValue::Create(litId))); funcAnnotation.AddElement(std::move(declaredSymbolTypesElement)); const_cast&>(pandaFunc.metadata->GetAnnotations()).push_back( @@ -781,7 +818,7 @@ static void ParseFunctionDeclaredType(const Json::Value &function, panda::pandas } } -static panda::pandasm::Function ParseFunction(const Json::Value &function) +static panda::pandasm::Function ParseFunction(const Json::Value &function, panda::pandasm::Program &prog) { auto pandaFunc = GetFunctionDefintion(function); ParseFunctionInstructions(function, pandaFunc); @@ -791,9 +828,9 @@ static panda::pandasm::Function ParseFunction(const Json::Value &function) ParseFunctionCatchTables(function, pandaFunc); // parsing call opt type ParseFunctionCallType(function, pandaFunc); - ParseFunctionTypeInfo(function, pandaFunc); - ParseFunctionExportedType(function, pandaFunc); - ParseFunctionDeclaredType(function, pandaFunc); + ParseFunctionTypeInfo(function, pandaFunc, prog); + ParseFunctionExportedType(function, pandaFunc, prog); + ParseFunctionDeclaredType(function, pandaFunc, prog); if (g_isDtsFile && pandaFunc.name != "func_main_0") { pandaFunc.metadata->SetAttribute("external"); @@ -817,38 +854,31 @@ static void GenerateESTypeAnnotationRecord(panda::pandasm::Program &prog) prog.record_table.emplace(tsTypeAnnotationRecord.name, std::move(tsTypeAnnotationRecord)); } -static void GenerateCommonJsRecord(panda::pandasm::Program &prog, bool isCommonJs) +static void SetCommonjsField(panda::pandasm::Program &prog, bool isCommonjs) { - // when multi-abc file get merged, field should be inserted in abc's own record - auto commonjsRecord = panda::pandasm::Record("_CommonJsRecord", LANG_EXT); - commonjsRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - auto isCommonJsField = panda::pandasm::Field(LANG_EXT); - isCommonJsField.name = "isCommonJs"; - isCommonJsField.type = panda::pandasm::Type("u8", 0); - isCommonJsField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(isCommonJs))); - commonjsRecord.field_list.emplace_back(std::move(isCommonJsField)); - - prog.record_table.emplace(commonjsRecord.name, std::move(commonjsRecord)); -} - -static void GenerateESModuleRecord(panda::pandasm::Program &prog) -{ - auto ecmaModuleRecord = panda::pandasm::Record("_ESModuleRecord", LANG_EXT); - ecmaModuleRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - prog.record_table.emplace(ecmaModuleRecord.name, std::move(ecmaModuleRecord)); + auto iter = prog.record_table.find(g_recordName); + if (iter != prog.record_table.end()) { + auto &rec = iter->second; + auto isCommonJsField = panda::pandasm::Field(LANG_EXT); + isCommonJsField.name = "isCommonjs"; + isCommonJsField.type = panda::pandasm::Type("u8", 0); + isCommonJsField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(static_cast(isCommonjs))); + rec.field_list.emplace_back(std::move(isCommonJsField)); + } } -static void AddModuleRecord(panda::pandasm::Program &prog, const std::string &moduleName, uint32_t moduleIdx) +static void SetModuleRecordIdx(panda::pandasm::Program &prog) { - auto iter = prog.record_table.find("_ESModuleRecord"); + auto iter = prog.record_table.find(g_recordName); if (iter != prog.record_table.end()) { auto &rec = iter->second; auto moduleIdxField = panda::pandasm::Field(LANG_EXT); - moduleIdxField.name = moduleName; + moduleIdxField.name = "moduleRecordIdx"; moduleIdxField.type = panda::pandasm::Type("u32", 0); - moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(moduleIdx))); + std::string moduleId = GetLiteralId(g_newLiteralArrayIndex); + moduleIdxField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + moduleId)); rec.field_list.emplace_back(std::move(moduleIdxField)); } @@ -874,23 +904,11 @@ int ParseJson(const std::string &data, Json::Value &rootValue) return RETURN_SUCCESS; } -static void ParseModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) -{ - Logd("----------------parse module_mode-----------------"); - if (rootValue.isMember("module_mode") && rootValue["module_mode"].isBool()) { - if (rootValue["module_mode"].asBool()) { - GenerateESModuleRecord(prog); - } - } -} - -static void ParseCommonJsModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) +static void SetCommonJsModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) { Logd("------------parse commonjs_module_mode-------------"); if (rootValue.isMember("commonjs_module") && rootValue["commonjs_module"].isBool()) { - if (rootValue["commonjs_module"].asBool()) { - GenerateCommonJsRecord(prog, true); - } + SetCommonjsField(prog, rootValue["commonjs_module"].asBool()); } } @@ -944,6 +962,14 @@ static void ParseIsDtsFile(const Json::Value &rootValue) } } +static void ParseEnableTypeInfo(const Json::Value &rootValue) +{ + Logd("-----------------parse enable type info-----------------"); + if (rootValue.isMember("record_type") && rootValue["record_type"].isBool()) { + g_enableTypeinfo = rootValue["record_type"].asBool(); + } +} + static void ParseCompilerOutputProto(const Json::Value &rootValue) { Logd("-----------------parse compiler output proto-----------------"); @@ -966,26 +992,27 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & { GenerateESCallTypeAnnotationRecord(prog); GenerateESTypeAnnotationRecord(prog); - ParseModuleMode(rootValue, prog); - ParseCommonJsModuleMode(rootValue, prog); + SetCommonJsModuleMode(rootValue, prog); ParseLogEnable(rootValue); ParseDebugMode(rootValue); ParseOptLevel(rootValue); ParseDisplayTypeinfo(rootValue); ParseOptLogLevel(rootValue); ParseIsDtsFile(rootValue); + ParseEnableTypeInfo(rootValue); ParseCompilerOutputProto(rootValue); } static void ParseSingleFunc(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto function = ParseFunction(rootValue["fb"]); + auto function = ParseFunction(rootValue["fb"], prog); prog.function_table.emplace(function.name.c_str(), std::move(function)); } -static void ParseSingleRec(const Json::Value &rootValue, panda::pandasm::Program &prog) +static void ParseRec(const Json::Value &rootValue, panda::pandasm::Program &prog) { auto record = ParseRecord(rootValue["rb"]); + g_recordName = record.name; prog.record_table.emplace(record.name.c_str(), std::move(record)); } @@ -1007,7 +1034,9 @@ static void ParseSingleLiteralBuf(const Json::Value &rootValue, panda::pandasm:: } auto literalarrayInstance = panda::pandasm::LiteralArray(literalArray); - prog.literalarray_table.emplace(std::to_string(g_literalArrayCount++), std::move(literalarrayInstance)); + auto litId = literalBuffer["k"].asString(); + std::cerr << "---------literal id----------" << litId << std::endl; + prog.literalarray_table.emplace(litId, std::move(literalarrayInstance)); } static void ParseModuleRequests(const Json::Value &moduleRequests, @@ -1127,35 +1156,42 @@ static void ParseSingleModule(const Json::Value &rootValue, panda::pandasm::Prog ParseIndirectExportEntries(moduleRecord["indirectExportEntries"], moduleLiteralArray); ParseStarExportEntries(moduleRecord["starExportEntries"], moduleLiteralArray); - auto moduleName = ParseString(moduleRecord["moduleName"].asString()); - AddModuleRecord(prog, moduleName, g_literalArrayCount); + SetModuleRecordIdx(prog); + std::string moduleId = GetLiteralId(g_newLiteralArrayIndex--); + std::cerr << "--------------moduleId------------------" << moduleId << std::endl; auto moduleLiteralarrayInstance = panda::pandasm::LiteralArray(moduleLiteralArray); - prog.literalarray_table.emplace(std::to_string(g_literalArrayCount++), std::move(moduleLiteralarrayInstance)); + prog.literalarray_table.emplace(moduleId, std::move(moduleLiteralarrayInstance)); } static void ParseSingleTypeInfo(const Json::Value &rootValue, panda::pandasm::Program &prog) { - auto typeInfoRecord = rootValue["ti"]; - auto typeFlag = typeInfoRecord["tf"].asBool(); - auto typeSummaryIndex = typeInfoRecord["tsi"].asUInt(); - auto ecmaTypeInfoRecord = panda::pandasm::Record("_ESTypeInfoRecord", LANG_EXT); - ecmaTypeInfoRecord.metadata->SetAccessFlags(panda::ACC_PUBLIC); - - auto typeFlagField = panda::pandasm::Field(LANG_EXT); - typeFlagField.name = "typeFlag"; - typeFlagField.type = panda::pandasm::Type("u8", 0); - typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeFlag))); - ecmaTypeInfoRecord.field_list.emplace_back(std::move(typeFlagField)); - auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); - typeSummaryIndexField.name = "typeSummaryIndex"; - typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); - typeSummaryIndexField.metadata->SetValue(panda::pandasm::ScalarValue::Create( - static_cast(typeSummaryIndex))); - ecmaTypeInfoRecord.field_list.emplace_back(std::move(typeSummaryIndexField)); + auto iter = prog.record_table.find(g_recordName); + if (iter != prog.record_table.end()) { + auto &rec = iter->second; - prog.record_table.emplace(ecmaTypeInfoRecord.name, std::move(ecmaTypeInfoRecord)); + auto typeInfoRecord = rootValue["ti"]; + auto typeFlag = typeInfoRecord["tf"].asBool(); + auto typeSummaryIndex = typeInfoRecord["tsi"].asString(); + + auto typeFlagField = panda::pandasm::Field(LANG_EXT); + typeFlagField.name = "typeFlag"; + typeFlagField.type = panda::pandasm::Type("u8", 0); + typeFlagField.metadata->SetValue(panda::pandasm::ScalarValue::Create( + static_cast(typeFlag))); + rec.field_list.emplace_back(std::move(typeFlagField)); + + + if (g_enableTypeinfo) { + std::cerr << "--------type summary index---------" << typeSummaryIndex << std::endl; + auto typeSummaryIndexField = panda::pandasm::Field(LANG_EXT); + typeSummaryIndexField.name = "typeSummaryIndex"; + typeSummaryIndexField.type = panda::pandasm::Type("u32", 0); + typeSummaryIndexField.metadata->SetValue( + panda::pandasm::ScalarValue::Create(typeSummaryIndex)); + rec.field_list.emplace_back(std::move(typeSummaryIndexField)); + } + } } static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Program &prog) @@ -1178,7 +1214,7 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr } case static_cast(JsonType::RECORD): { if (rootValue.isMember("rb") && rootValue["rb"].isObject()) { - ParseSingleRec(rootValue, prog); + ParseRec(rootValue, prog); } break; } @@ -1210,6 +1246,12 @@ static int ParseSmallPieceJson(const std::string &subJson, panda::pandasm::Progr } break; } + case static_cast(JsonType::RECORDNAME): { + if (rootValue.isMember("rn") && rootValue["rn"].isString()) { + g_recordName = rootValue["rn"].asString(); + } + break; + } default: { std::cerr << "Unreachable json type: " << type << std::endl; return RETURN_FAILED; @@ -1349,6 +1391,8 @@ bool GenerateProgram([[maybe_unused]] const std::string &data, const std::string Logd("parsing done, calling pandasm\n"); + std::cerr << "---------num of literal array------------" << prog.literalarray_table.size() << std::endl; + std::string compilerOutputProto = g_compilerOutputProto; if (options.GetCompilerOutputProto().size() > 0) { compilerOutputProto = options.GetCompilerOutputProto(); diff --git a/ts2panda/ts2abc/ts2abc.h b/ts2panda/ts2abc/ts2abc.h index 421205bb35905ff34455f39acfdbcb1e7b5dbb8b..4b4658dfdb431b8337ffa9a6a5be827e7d8af10d 100644 --- a/ts2panda/ts2abc/ts2abc.h +++ b/ts2panda/ts2abc/ts2abc.h @@ -36,7 +36,8 @@ enum class JsonType { LITERALBUFFER, MODULE, OPTIONS, - TYPEINFO + TYPEINFO, + RECORDNAME }; constexpr int RETURN_SUCCESS = 0; diff --git a/ts2panda/ts2abc/ts2abc_options.h b/ts2panda/ts2abc/ts2abc_options.h index 76f326fe257cdf811b77e5da75b9b759e2d76843..03d56e78e92bcb5d519fc4a2aa8306aee6a30bb4 100755 --- a/ts2panda/ts2abc/ts2abc_options.h +++ b/ts2panda/ts2abc/ts2abc_options.h @@ -224,7 +224,7 @@ namespace panda::ts2abc { panda::PandArg compile_by_pipe_arg_{ "compile-by-pipe", false, R"(Compile a json file that is passed by pipe)"}; panda::PandArg compiler_output_proto_{ "output-proto", "", - R"(compiler proto serialize binary output (.proto))"}; + R"(Specify the output name for serializd protobuf file (.protoBin))"}; panda::PandArg Tail_Arg1_arg_{ "ARG_1", "", R"(Path to input(json file) or path to output(ark bytecode)" " when 'compile-by-pipe' enabled)"};