From de8543f121b53c70c192d12bf0a796a4ae931b9d Mon Sep 17 00:00:00 2001 From: chenyiyuan Date: Fri, 26 Apr 2024 15:01:59 +0800 Subject: [PATCH] Fix class access flag issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I9JX6W Signed-off-by: chenyiyuan Change-Id: I9914e04efa317e02f0952992f5530aa4a37dfebe --- abc2program/abc_class_processor.cpp | 2 +- assembler/assembly-emitter.cpp | 4 +- assembler/assembly-record.h | 2 + assembler/meta.h | 5 ++ assembler/tests/BUILD.gn | 26 +++++++++ .../tests/assembler_access_flag_test.cpp | 58 +++++++++++++++++++ assembler/tests/assembler_emitter_test.cpp | 2 +- assembler/tests/js/test_class_access_flags.js | 29 ++++++++++ 8 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 assembler/tests/assembler_access_flag_test.cpp create mode 100755 assembler/tests/js/test_class_access_flags.js diff --git a/abc2program/abc_class_processor.cpp b/abc2program/abc_class_processor.cpp index 3d51b0a742..ec7473f90d 100644 --- a/abc2program/abc_class_processor.cpp +++ b/abc2program/abc_class_processor.cpp @@ -44,7 +44,7 @@ void AbcClassProcessor::FillRecord() pandasm::Record record("", panda_file::SourceLang::ECMASCRIPT); record.name = entity_container_.GetFullRecordNameById(entity_id_); if (record.name == SLOT_NUMBER_RECORD_NAME || record.name == CONCURRENT_MODULE_REQUEST_RECORD_NAME) { - record.metadata->SetAccessFlags(panda::ACC_ANNOTATION); + record.metadata->SetAccessFlags(class_data_accessor_->GetAccessFlags()); } ASSERT(program_->record_table.find(record.name) == program_->record_table.end()); FillRecordData(record); diff --git a/assembler/assembly-emitter.cpp b/assembler/assembly-emitter.cpp index 8a9127eeb7..d6413ab0cf 100644 --- a/assembler/assembly-emitter.cpp +++ b/assembler/assembly-emitter.cpp @@ -1329,7 +1329,7 @@ static void MakeSlotNumberRecord(Program *prog) { static const std::string SLOT_NUMBER = "_ESSlotNumberAnnotation"; pandasm::Record record(SLOT_NUMBER, pandasm::extensions::Language::ECMASCRIPT); - record.metadata->SetAccessFlags(panda::ACC_ANNOTATION); + record.metadata->AddAccessFlags(panda::ACC_ANNOTATION); prog->record_table.emplace(SLOT_NUMBER, std::move(record)); } @@ -1358,7 +1358,7 @@ static void MakeConcurrentModuleRequestsRecord(Program *prog) { static const std::string CONCURRENT_MODULE_REQUESTS = "_ESConcurrentModuleRequestsAnnotation"; pandasm::Record record(CONCURRENT_MODULE_REQUESTS, pandasm::extensions::Language::ECMASCRIPT); - record.metadata->SetAccessFlags(panda::ACC_ANNOTATION); + record.metadata->AddAccessFlags(panda::ACC_ANNOTATION); prog->record_table.emplace(CONCURRENT_MODULE_REQUESTS, std::move(record)); } diff --git a/assembler/assembly-record.h b/assembler/assembly-record.h index 7ef8a3b1d5..205d3ee96c 100644 --- a/assembler/assembly-record.h +++ b/assembler/assembly-record.h @@ -46,11 +46,13 @@ struct Record { metadata(extensions::MetadataExtension::CreateRecordMetadata(lang)), file_location({f_c, b_l, b_r, l_n, d}) { + metadata->SetAccessFlags(panda::ACC_PUBLIC); } Record(std::string s, panda::panda_file::SourceLang lang) : name(std::move(s)), language(lang), metadata(extensions::MetadataExtension::CreateRecordMetadata(lang)) { + metadata->SetAccessFlags(panda::ACC_PUBLIC); } bool HasImplementation() const diff --git a/assembler/meta.h b/assembler/meta.h index fd21d188c6..5c6ced6818 100644 --- a/assembler/meta.h +++ b/assembler/meta.h @@ -496,6 +496,11 @@ public: access_flags_ = access_flags; } + void AddAccessFlags(uint32_t access_flags) + { + access_flags_ = access_flags_ | access_flags; + } + bool IsForeign() const; private: diff --git a/assembler/tests/BUILD.gn b/assembler/tests/BUILD.gn index c34695ab17..6a4c578b9d 100644 --- a/assembler/tests/BUILD.gn +++ b/assembler/tests/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") import("//arkcompiler/runtime_core/ark_config.gni") import("$ark_root/tests/test_helper.gni") @@ -30,11 +31,31 @@ config("module_private_config") { defines = [ "PANDA_WITH_ECMASCRIPT" ] } +assembler_test_js_files = [ "test_class_access_flags" ] + +test_js_path = "//arkcompiler/runtime_core/assembler/tests/js/" + +foreach(file, assembler_test_js_files) { + es2abc_gen_abc("gen_${file}_abc") { + test_js = "${test_js_path}${file}.js" + test_abc = "$target_out_dir/${file}.abc" + + src_js = rebase_path(test_js) + dst_file = rebase_path(test_abc) + + extra_args = [ "--merge-abc" ] + + in_puts = [ test_js ] + out_puts = [ test_abc ] + } +} + host_unittest_action("AssemblerTest") { module_out_path = module_output_path sources = [ "annotation_test.cpp", + "assembler_access_flag_test.cpp", "assembler_emitter_test.cpp", "assembler_ins_test.cpp", "assembler_lexer_test.cpp", @@ -65,6 +86,11 @@ host_unittest_action("AssemblerTest") { ] external_deps = [ sdk_libc_secshared_dep ] + test_abc_dir = rebase_path(target_out_dir) + defines = [ "GRAPH_TEST_ABC_DIR=\"${test_abc_dir}/\"" ] + foreach(file, assembler_test_js_files) { + deps += [ ":gen_${file}_abc" ] + } } group("host_unittest") { diff --git a/assembler/tests/assembler_access_flag_test.cpp b/assembler/tests/assembler_access_flag_test.cpp new file mode 100644 index 0000000000..e842eecfaf --- /dev/null +++ b/assembler/tests/assembler_access_flag_test.cpp @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file EXPECT 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. + */ + +#include + +#include "class_data_accessor-inl.h" + +using namespace testing::ext; + +namespace panda::pandasm { +class AssemblyEmitterTest : public testing::Test { +}; + +static const std::string SLOT_NUMBER = "L_ESSlotNumberAnnotation;"; +static const std::string CONCURRENT_MODULE_REQUESTS = "L_ESConcurrentModuleRequestsAnnotation;"; + +/** + * @tc.name: assembly_access_flag_test_001 + * @tc.desc: Verify class access flag. + * @tc.type: FUNC + * @tc.require: file path and name + */ +HWTEST_F(AssemblyEmitterTest, assembly_access_flag_test_001, TestSize.Level1) +{ + const std::string file_name = GRAPH_TEST_ABC_DIR "test_class_access_flags.abc"; + auto file_to_verify = panda_file::File::Open(file_name); + std::unique_ptr file_; + file_.swap(file_to_verify); + + const auto class_idx = file_->GetClasses(); + for (size_t i = 0; i < class_idx.size(); i++) { + uint32_t class_id = class_idx[i]; + ASSERT(class_id < file_->GetHeader()->file_size); + const panda_file::File::EntityId record_id {class_id}; + panda_file::ClassDataAccessor class_accessor {*file_, record_id}; + auto class_name = class_accessor.GetName(); + auto access_flag = class_accessor.GetAccessFlags(); + auto name = std::string(utf::Mutf8AsCString(class_name.data)); + if (name == SLOT_NUMBER || name == CONCURRENT_MODULE_REQUESTS) { + EXPECT_EQ(access_flag, ACC_PUBLIC | ACC_ANNOTATION); + } else { + EXPECT_EQ(access_flag, ACC_PUBLIC); + } + } +} +} \ No newline at end of file diff --git a/assembler/tests/assembler_emitter_test.cpp b/assembler/tests/assembler_emitter_test.cpp index a0856a36f8..a970958383 100644 --- a/assembler/tests/assembler_emitter_test.cpp +++ b/assembler/tests/assembler_emitter_test.cpp @@ -168,7 +168,7 @@ HWTEST_F(AssemblyEmitterTest, assembly_emitter_test_002, TestSize.Level1) panda_file::ClassDataAccessor cda(*pf, class_id); EXPECT_EQ(cda.GetSuperClassId().GetOffset(), 0U); - EXPECT_EQ(cda.GetAccessFlags(), 0U); + EXPECT_EQ(cda.GetAccessFlags(), ACC_PUBLIC); EXPECT_EQ(cda.GetFieldsNumber(), 2U); EXPECT_EQ(cda.GetMethodsNumber(), 0U); EXPECT_EQ(cda.GetIfacesNumber(), 0U); diff --git a/assembler/tests/js/test_class_access_flags.js b/assembler/tests/js/test_class_access_flags.js new file mode 100755 index 0000000000..4dc9b23a5f --- /dev/null +++ b/assembler/tests/js/test_class_access_flags.js @@ -0,0 +1,29 @@ +/* + 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. + */ + +let val = 'hello'; +function setValue() { + let value = val; + return value; +} + +class Student { + constructor(name, age) { + this.name = name; + this.age = age; + } +} + +let arr = [1, 2, 9, 0]; \ No newline at end of file -- Gitee