From 92e4cb9b2a542eb95c1cf581a20a33394729e82a Mon Sep 17 00:00:00 2001 From: chenqy930 Date: Fri, 17 Mar 2023 14:32:37 +0800 Subject: [PATCH] fixed 41d8acb from https://gitee.com/chenqy930/arkcompiler_ets_frontend_hotfix_dev/pulls/922 Fix compile base64 input failed Issue:I6NYP4 Signed-off-by: chenqy930 Change-Id: I56d28f48ca8ee341d7d45ac3967e380a8d5e16cd --- es2panda/BUILD.gn | 3 +- es2panda/compiler/core/compileQueue.cpp | 38 ++++++----- es2panda/test/BUILD.gn | 14 ++++ es2panda/test/base64/inputFile/expected.txt | 14 ++++ es2panda/test/base64/inputFile/input.js | 17 +++++ es2panda/test/base64/inputString/expected.txt | 14 ++++ es2panda/test/base64/inputString/input.txt | 14 ++++ es2panda/test/runner.py | 65 +++++++++++++++++++ 8 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 es2panda/test/base64/inputFile/expected.txt create mode 100644 es2panda/test/base64/inputFile/input.js create mode 100644 es2panda/test/base64/inputString/expected.txt create mode 100644 es2panda/test/base64/inputString/input.txt diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn index 2bfa6fcf70..dbc56a2f52 100644 --- a/es2panda/BUILD.gn +++ b/es2panda/BUILD.gn @@ -554,7 +554,8 @@ group("es2panda_build_win") { } group("es2abc_tests") { - deps = [ "./test:es2abc_compiler_tests" ] + deps = [ "./test:es2abc_base64_tests" ] + deps += [ "./test:es2abc_compiler_tests" ] deps += [ "./test:es2abc_parser_tests" ] deps += [ "./test:es2abc_parser_tsc_tests" ] deps += [ "./test:es2abc_patch_tests" ] diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index cdc08adfbe..6617f2a339 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -70,24 +70,30 @@ void CompileModuleRecordJob::Run() void CompileFileJob::Run() { std::stringstream ss; - if (!util::Helpers::ReadFileToBuffer(src_->fileName, ss)) { - return; + std::string buffer; + if (!src_->fileName.empty()) { + if (!util::Helpers::ReadFileToBuffer(src_->fileName, ss)) { + return; + } + buffer = ss.str(); + src_->source = buffer; } - std::string buffer = ss.str(); - src_->source = buffer; - auto cacheFileIter = options_->cacheFiles.find(src_->fileName); - if (cacheFileIter != options_->cacheFiles.end()) { - src_->hash = GetHash32String(reinterpret_cast(buffer.c_str())); - - ArenaAllocator allocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true); - auto *cacheProgramInfo = proto::ProtobufSnapshotGenerator::GetCacheContext(cacheFileIter->second, &allocator); - - if (cacheProgramInfo != nullptr && cacheProgramInfo->hashCode == src_->hash) { - std::unique_lock lock(global_m_); - auto *cache = allocator_->New(src_->hash, std::move(cacheProgramInfo->program)); - progsInfo_.insert({src_->fileName, cache}); - return; + if (!src_->fileName.empty()) { + auto cacheFileIter = options_->cacheFiles.find(src_->fileName); + if (cacheFileIter != options_->cacheFiles.end()) { + src_->hash = GetHash32String(reinterpret_cast(buffer.c_str())); + + ArenaAllocator allocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true); + auto *cacheProgramInfo = proto::ProtobufSnapshotGenerator::GetCacheContext(cacheFileIter->second, + &allocator); + + if (cacheProgramInfo != nullptr && cacheProgramInfo->hashCode == src_->hash) { + std::unique_lock lock(global_m_); + auto *cache = allocator_->New(src_->hash, std::move(cacheProgramInfo->program)); + progsInfo_.insert({src_->fileName, cache}); + return; + } } } diff --git a/es2panda/test/BUILD.gn b/es2panda/test/BUILD.gn index c310f51304..9ece93c017 100644 --- a/es2panda/test/BUILD.gn +++ b/es2panda/test/BUILD.gn @@ -97,3 +97,17 @@ action("es2abc_patch_tests") { outputs = [ "${es2abc_build_path}/keep_es2abc_patch_tests_run" ] } + +action("es2abc_base64_tests") { + script = "${es2abc_root}/test/runner.py" + + deps = es2abc_build_deps + + args = [ + "--no-progress", + "--base64", + rebase_path("${es2abc_build_path}"), + ] + + outputs = [ "${es2abc_build_path}/keep_es2abc_base64_tests_run" ] +} diff --git a/es2panda/test/base64/inputFile/expected.txt b/es2panda/test/base64/inputFile/expected.txt new file mode 100644 index 0000000000..dac0202f24 --- /dev/null +++ b/es2panda/test/base64/inputFile/expected.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +UEFOREEAAAAAAAAACQAAAPwBAACMAAAAFQAAAAQAAAA8AAAAAQAAAPgBAAAAAAAATAAAAAEAAABMAAAAuwAAAIwAAADhAAAADQEAAIwAAAD8AQAAAgAAAHQAAAADAAAAfAAAAAAAAACIAAAAAQAAAIgAAAC7AAAADQEAAKEAAACvAAAAtgAAAD4BAAAnTF9FU1R5cGVBbm5vdGF0aW9uOwAZaGVsbG8gd29ybGQhAAtwcmludAAHc3RyADNMX0VTU2xvdE51bWJlckFubm90YXRpb247AAAAAACAQAAAAgAAJ0xfRVNUeXBlSW5mb1JlY29yZDsAAAAAAAEAAAIAABdmdW5jX21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAAAAAAEAAIgCAb8BAAACAAXrAQAABrIBAAAA7u4AAMMBL21udC9jaGVucWl1eWFvL2NvZGUvb3Blbmhhcm1vbnkvYXJrY29tcGlsZXIvZXRzX2Zyb250ZW5kL2VzMnBhbmRhL3Rlc3QvYmFzZTY0L2lucHV0RmlsZS9pbnB1dC5qcwAVU2xvdE51bWJlcgAAAAEApgEAAAUAAAA3BwMhAERwRIFEkj4AAEgAAAIAPwEBAGEFPwICAGEGYAUqAwYAZQkLAQIQiQD/////DwAFwgIABhAA5AEAAA== diff --git a/es2panda/test/base64/inputFile/input.js b/es2panda/test/base64/inputFile/input.js new file mode 100644 index 0000000000..106fa1b7ad --- /dev/null +++ b/es2panda/test/base64/inputFile/input.js @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 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. + */ + +let str = "hello world!"; +print(str); \ No newline at end of file diff --git a/es2panda/test/base64/inputString/expected.txt b/es2panda/test/base64/inputString/expected.txt new file mode 100644 index 0000000000..eba80a89df --- /dev/null +++ b/es2panda/test/base64/inputString/expected.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +UEFOREEAAAAAAAAACQAAAJQBAACMAAAAFQAAAAQAAAA8AAAAAQAAAJABAAAAAAAATAAAAAEAAABMAAAAuwAAAIwAAADhAAAADQEAAIwAAACUAQAAAgAAAHQAAAADAAAAfAAAAAAAAACIAAAAAQAAAIgAAAC7AAAADQEAAKEAAACvAAAAtgAAAD4BAAAnTF9FU1R5cGVBbm5vdGF0aW9uOwAZaGVsbG8gd29ybGQhAAtwcmludAAHc3RyADNMX0VTU2xvdE51bWJlckFubm90YXRpb247AAAAAACAQAAAAgAAJ0xfRVNUeXBlSW5mb1JlY29yZDsAAAAAAAEAAAIAABdmdW5jX21haW5fMAATTF9HTE9CQUw7AAAAAAABAAECAAABAAAAAAEAAIgCAVsBAAACAAWEAQAABk4BAAAA7u4AABVTbG90TnVtYmVyAAAAAQBCAQAABQAAADcHAyEARHBEgUSSPgAASAAAAgA/AQEAYQU/AgIAYQZgBSoDBgBlC2uJAP////8PAAEAAAAAAIABAAA= diff --git a/es2panda/test/base64/inputString/input.txt b/es2panda/test/base64/inputString/input.txt new file mode 100644 index 0000000000..7fd6cf0d29 --- /dev/null +++ b/es2panda/test/base64/inputString/input.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +bGV0IHN0ciA9ICJoZWxsbyB3b3JsZCEiOwpwcmludChzdHIpOw== \ No newline at end of file diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index b06fb27e99..4f03722ea1 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -134,6 +134,8 @@ def get_args(): help='run hotfix tests') parser.add_argument('--hotreload', dest='hotreload', action='store_true', default=False, help='run hotreload tests') + parser.add_argument('--base64', dest='base64', action='store_true', default=False, + help='run base64 tests') return parser.parse_args() @@ -937,6 +939,66 @@ class HotreloadRunner(PatchRunner): self.tests += list(map(lambda t: PatchTest(t, "hotreload"), self.tests_in_dirs)) +class Base64Test(Test): + def __init__(self, test_path, input_type): + Test.__init__(self, test_path, "") + self.input_type = input_type + + def run(self, runner): + cmd = runner.cmd_prefix + [runner.es2panda, "--base64Output"] + if self.input_type == "file": + cmd.extend([os.path.join(self.path, "input.js")]) + elif self.input_type == "string": + input_file = os.path.join(self.path, "input.txt") + try: + with open(input_file, 'r') as fp: + base64_input = (''.join((fp.readlines()[12:]))).lstrip() # ignore license description lines + cmd.extend(["--base64Input", base64_input]) + except Exception: + self.passed = False + else: + self.error = "Unsupported base64 input type" + self.passed = False + return self + + self.log_cmd(cmd) + + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate(timeout=runner.args.es2panda_timeout) + if stderr: + self.passed = False + self.error = stderr.decode("utf-8", errors="ignore") + return self + + self.output = stdout.decode("utf-8", errors="ignore") + + expected_path = os.path.join(self.path, 'expected.txt') + try: + with open(expected_path, 'r') as fp: + expected = (''.join((fp.readlines()[12:]))).lstrip() + self.passed = expected == self.output + except Exception: + self.passed = False + + if not self.passed: + self.error = "expected output:" + os.linesep + expected + os.linesep + "actual output:" + os.linesep +\ + self.output + + return self + + +class Base64Runner(Runner): + def __init__(self, args): + Runner.__init__(self, args, "Base64") + self.test_directory = path.join(self.test_root, "base64") + self.add_test() + + def add_test(self): + self.tests = [] + self.tests.append(Base64Test(os.path.join(self.test_directory, "inputFile"), "file")) + self.tests.append(Base64Test(os.path.join(self.test_directory, "inputString"), "string")) + + def main(): args = get_args() @@ -974,6 +1036,9 @@ def main(): if args.hotreload: runners.append(HotreloadRunner(args)) + if args.base64: + runners.append(Base64Runner(args)) + failed_tests = 0 for runner in runners: -- Gitee