diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index efa30fbaa27ebddaad8832782b0cf160f1ae7a04..b6883a3f27fac9e1841fa9c248d42d93186410dc 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -206,6 +207,49 @@ static bool GenerateProgram(std::map programsInfo) +{ + std::unordered_map> recordNameMap; + + // Names of these program records generated from abc input all follow such format: abcName|recordName + for (auto &[name, _] : programsInfo) { + if (util::RecordNotGeneratedFromBytecode(name)) { + continue; + } + + auto nameVec = util::Split(name, util::CHAR_VERTICAL_LINE); + auto abcFileName = nameVec[0]; + auto recordName = nameVec.back(); + + if (mergeAbc) { + if (recordName == util::GLOBAL_TYPE_NAME) { + std::cerr << "Current compile mode is merge-abc, all input abc files must be merged mode. " + << "But file '" << abcFileName << "' is not a merged abc." << std::endl; + return false; + } + } else { + if (recordNameMap.find(abcFileName) != recordNameMap.end()) { + recordNameMap.find(abcFileName)->second.insert(recordName); + } else { + recordNameMap.insert({abcFileName, {recordName}}); + } + } + } + + if (!mergeAbc) { + for (auto &[abcFileName, recordNameSet] : recordNameMap) { + if (!recordNameSet.count(util::GLOBAL_TYPE_NAME)) { + std::cerr << "Current compile mode is non merge-abc, all input abc files must be unmerged mode. " + << "But file '" << abcFileName << "' is a merged abc." << std::endl; + return false; + } + } + } + + return true; +} + static bool GenerateAbcFiles(std::map &programsInfo, const std::unique_ptr &options, size_t expectedProgsCount, const std::map> &resolvedDepsRelation) @@ -259,6 +303,11 @@ int Run(int argc, const char **argv) Compiler::SetExpectedProgsCount(options->CompilerOptions().sourceFiles.size()); int ret = Compiler::CompileFiles(options->CompilerOptions(), programsInfo, &allocator); + + if (!CheckMergeModeConsistency(options->CompilerOptions().mergeAbc, programsInfo)) { + return 1; + } + if (options->ParseOnly()) { return ret; } diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index 2a05358b0d6b0e91f1514ac49f00f281a9a84640..a52e64154977d7cb5e9c134ab71503ab0c4b865e 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -167,7 +167,7 @@ void CompileAbcClassJob::Run() std::unique_lock lock(CompileFileJob::globalMutex_); ASSERT(compiler_.GetAbcFile().GetFilename().find(util::CHAR_VERTICAL_LINE) == std::string::npos); ASSERT(program->record_table.size() == 1); - ASSERT(program->record_table.begin()->first.find(util::CHAR_VERTICAL_LINE) == std::string::npos); + ASSERT(util::RecordNotGeneratedFromBytecode(program->record_table.begin()->first)); auto name = compiler_.GetAbcFile().GetFilename(); name += util::CHAR_VERTICAL_LINE + program->record_table.begin()->first; auto *cache = allocator_->New(std::move(*program)); diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/abcinputs/bytecodehar.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/abcinputs/bytecodehar.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0ca3c85d9e85d005522ff5a92aaae9cd3d42fc2 --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/abcinputs/bytecodehar.txt @@ -0,0 +1 @@ +bytecodehar-file1.js diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/bytecodehar-file1.js b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/bytecodehar-file1.js new file mode 100644 index 0000000000000000000000000000000000000000..ef74c4a5dd8a5cd9e3743794c182ff0a1237c7a0 --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/bytecodehar-file1.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function bbb(){} \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec-expected.pa.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec-expected.pa.txt new file mode 100644 index 0000000000000000000000000000000000000000..e10ab4b150d9f8e2661b02342152b409a6624168 --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec-expected.pa.txt @@ -0,0 +1 @@ +Current compile mode is merge-abc, all input abc files must be merged mode \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec.js b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec.js new file mode 100644 index 0000000000000000000000000000000000000000..3bbc146ba3accc8468654c25edeaadaad2f17ccc --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/hap-file-exec.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function ccc(){} \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/recordnames.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/recordnames.txt new file mode 100644 index 0000000000000000000000000000000000000000..7be992a8aff71546fb690c4f331e3742538f4e4d --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/merged_compilation_process_with_unmerged_abc_input/recordnames.txt @@ -0,0 +1 @@ +bytecodehar-file1:&myapp/bytecodehar-file1& diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/abcinputs/bytecodehar.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/abcinputs/bytecodehar.txt new file mode 100644 index 0000000000000000000000000000000000000000..60401f4d5c70427483a2264d4156e792f580a6fa --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/abcinputs/bytecodehar.txt @@ -0,0 +1,2 @@ +bytecodehar-file1.js +bytecodehar-file2.js diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file1.js b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file1.js new file mode 100644 index 0000000000000000000000000000000000000000..ef74c4a5dd8a5cd9e3743794c182ff0a1237c7a0 --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file1.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function bbb(){} \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file2.js b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file2.js new file mode 100644 index 0000000000000000000000000000000000000000..88d2a0fcd72738f2aeab94e98f272a54f757761b --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/bytecodehar-file2.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function aaa(){} \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec-expected.pa.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec-expected.pa.txt new file mode 100644 index 0000000000000000000000000000000000000000..107104644d7ca55ddc01d71ae6437f62d9194d00 --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec-expected.pa.txt @@ -0,0 +1 @@ +Current compile mode is non merge-abc, all input abc files must be unmerged mode \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec.js b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec.js new file mode 100644 index 0000000000000000000000000000000000000000..3bbc146ba3accc8468654c25edeaadaad2f17ccc --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/hap-file-exec.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function ccc(){} \ No newline at end of file diff --git a/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/recordnames.txt b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/recordnames.txt new file mode 100644 index 0000000000000000000000000000000000000000..49577de77f2cddb15a9c0dd803c78d5e49ab638e --- /dev/null +++ b/es2panda/test/compiler/bytecodehar/merge_abc_consistence_check/projects/unmerged_compilation_process_with_merged_abc_input/recordnames.txt @@ -0,0 +1,2 @@ +bytecodehar-file1:&myapp/bytecodehar-file1& +bytecodehar-file2:&myapp/bytecodehar-file2& diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index f8adfc959d90ef91862d31cd37a0791431456a0a..e9a32a2721dd9fff8f97b88703e0f7256f941536 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -811,7 +811,7 @@ class CompilerProjectTest(Test): es2abc_cmd = runner.cmd_prefix + [runner.es2panda] es2abc_cmd.extend(self.flags) es2abc_cmd.extend(['%s%s' % ("--output=", output_file)]) - es2abc_cmd.append('@' + input_file) + es2abc_cmd.append(input_file) return es2abc_cmd def gen_merged_abc_for_abc_input(self, runner, files_info_name): @@ -821,7 +821,14 @@ class CompilerProjectTest(Test): abc_input_files_info_path = path.join(self.generated_abc_inputs_path, files_info_name) abc_input_merged_abc_path = path.join(self.generated_abc_inputs_path, '%s-abcinput.abc' % (files_info_name[:-len('-filesInfo.txt')])) - es2abc_cmd = self.gen_es2abc_cmd(runner, abc_input_files_info_path, abc_input_merged_abc_path) + + abc_input_file_path = '@' + abc_input_files_info_path + if "unmerged_abc_input" in self.generated_abc_inputs_path: + self.flags.remove("--merge-abc") + with open(abc_input_files_info_path, 'r') as fp: + abc_input_file_path = fp.read().split(';')[0] + + es2abc_cmd = self.gen_es2abc_cmd(runner, abc_input_file_path, abc_input_merged_abc_path) self.log_cmd(es2abc_cmd) process = subprocess.Popen(es2abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() @@ -849,7 +856,14 @@ class CompilerProjectTest(Test): test_abc_name = ("%s.abc" % (path.splitext(file_name)[0])) output_abc_name = path.join(file_absolute_path, test_abc_name) - es2abc_cmd = self.gen_es2abc_cmd(runner, self.files_info_path, output_abc_name) + # reverse merge-abc flag + if "merge_abc_consistence_check" in self.path: + if "--merge-abc" in self.flags: + self.flags.remove("--merge-abc") + else: + self.flags.append("--merge-abc") + + es2abc_cmd = self.gen_es2abc_cmd(runner, '@' + self.files_info_path, output_abc_name) compile_context_info_path = path.join(path.join(self.projects_path, self.project), "compileContextInfo.json") if path.exists(compile_context_info_path): es2abc_cmd.append("%s%s" % ("--compile-context-info=", compile_context_info_path)) @@ -858,11 +872,17 @@ class CompilerProjectTest(Test): process = subprocess.Popen(es2abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() + # restore merge-abc flag + if "merge_abc_consistence_check" in self.path and "--merge-abc" not in self.flags: + self.flags.append("--merge-abc") + # Check dump-assembly outputs when required if "--dump-assembly" in self.flags: pa_expected_path = "".join([self.get_path_to_expected()[:self.get_path_to_expected().rfind(".txt")], ".pa.txt"]) self.output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + if "merge_abc_consistence_check" in self.path: + self.output = self.output.split('.')[0] try: with open(pa_expected_path, 'r') as fp: expected = fp.read() @@ -873,6 +893,8 @@ class CompilerProjectTest(Test): self.error = err.decode("utf-8", errors="ignore") self.remove_project(runner) return self + else: + return self if err: self.passed = False @@ -1492,6 +1514,9 @@ def add_directory_for_compiler(runners, args): ["--merge-abc", "--dump-assembly", "--enable-abc-input", "--dump-deps-info", "--remove-redundant-file", "--dump-literal-buffer", "--dump-string", "--abc-class-threads=4"])) + compiler_test_infos.append(CompilerTestInfo("compiler/bytecodehar/merge_abc_consistence_check/projects", "js", + ["--merge-abc", "--dump-assembly", "--enable-abc-input", + "--abc-class-threads=4"])) if args.enable_arkguard: prepare_for_obfuscation(compiler_test_infos, runner.test_root) diff --git a/es2panda/util/commonUtil.cpp b/es2panda/util/commonUtil.cpp index 7197cab16f6656d04418f39eab701439abdac973..d81db93a8e335d4ac7a98ab13c12152a6c19ee59 100644 --- a/es2panda/util/commonUtil.cpp +++ b/es2panda/util/commonUtil.cpp @@ -112,5 +112,9 @@ std::string UpdatePackageVersionIfNeeded(const std::string &ohmurl, const panda: return ohmurl.substr(0, versionStart + 1) + iter->second.version; } +bool RecordNotGeneratedFromBytecode(std::string recordName) +{ + return recordName.find(util::CHAR_VERTICAL_LINE) == std::string::npos; +} } // namespace panda::es2panda::util \ No newline at end of file diff --git a/es2panda/util/commonUtil.h b/es2panda/util/commonUtil.h index 8bb3e357b1ed720b924e3ba036f67b7a2680ebea..779c6f17b1cf0c3e337ddc97005d7c6ef68a311d 100644 --- a/es2panda/util/commonUtil.h +++ b/es2panda/util/commonUtil.h @@ -38,6 +38,7 @@ const std::string IS_COMMONJS = "isCommonjs"; // The format of ohmurl for non-SO files are start with '@normalized:N'. const std::string NORMALIZED_OHMURL_NOT_SO = "@normalized:N"; const std::string MODULE_RECORD_IDX = "moduleRecordIdx"; +const std::string GLOBAL_TYPE_NAME = "_GLOBAL"; constexpr char NORMALIZED_OHMURL_SEPARATOR = '&'; constexpr char NORMALIZED_OHMURL_PREFIX = '@'; @@ -53,6 +54,7 @@ bool IsExternalPkgNames(const std::string &ohmurl, const std::set & std::string GetRecordNameFromNormalizedOhmurl(const std::string &ohmurl); std::string GetPkgNameFromNormalizedOhmurl(const std::string &ohmurl); std::string UpdatePackageVersionIfNeeded(const std::string &ohmurl, const CompileContextInfo &info); +bool RecordNotGeneratedFromBytecode(std::string recordName); template using ConstReferenceIf = typename std::conditional::type;