1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "abc_field_processor.h"
17 #include "abc2program_log.h"
18
19 namespace panda::abc2program {
20
AbcFieldProcessor(panda_file::File::EntityId entity_id,Abc2ProgramEntityContainer & entity_container,pandasm::Record & record)21 AbcFieldProcessor::AbcFieldProcessor(panda_file::File::EntityId entity_id, Abc2ProgramEntityContainer &entity_container,
22 pandasm::Record &record)
23 : AbcFileEntityProcessor(entity_id, entity_container), record_(record),
24 type_converter_(entity_container),
25 field_(pandasm::Field(LANG_ECMA))
26 {
27 field_data_accessor_ = std::make_unique<panda_file::FieldDataAccessor>(*file_, entity_id_);
28 }
29
FillProgramData()30 void AbcFieldProcessor::FillProgramData()
31 {
32 FillFieldData();
33 record_.field_list.emplace_back(std::move(field_));
34 }
35
FillFieldData()36 void AbcFieldProcessor::FillFieldData()
37 {
38 FillFieldName();
39 FillFieldType();
40 FillFieldMetaData();
41 }
42
FillFieldName()43 void AbcFieldProcessor::FillFieldName()
44 {
45 panda_file::File::EntityId field_name_id = field_data_accessor_->GetNameId();
46 field_.name = GetStringById(field_name_id);
47 }
48
FillFieldType()49 void AbcFieldProcessor::FillFieldType()
50 {
51 uint32_t field_type = field_data_accessor_->GetType();
52 field_.type = type_converter_.FieldTypeToPandasmType(field_type);
53 }
54
FillFieldMetaData()55 void AbcFieldProcessor::FillFieldMetaData()
56 {
57 field_.metadata->SetFieldType(field_.type);
58 FillFieldAttributes();
59 FillMetaDataValue();
60 }
61
FillFieldAttributes()62 void AbcFieldProcessor::FillFieldAttributes()
63 {
64 if (field_data_accessor_->IsExternal()) {
65 field_.metadata->SetAttribute(ABC_ATTR_EXTERNAL);
66 }
67 if (field_data_accessor_->IsStatic()) {
68 field_.metadata->SetAttribute(ABC_ATTR_STATIC);
69 }
70 }
71
FillMetaDataValue()72 void AbcFieldProcessor::FillMetaDataValue()
73 {
74 switch (field_.type.GetId()) {
75 case panda_file::Type::TypeId::U32: {
76 uint32_t val = field_data_accessor_->GetValue<uint32_t>().value();
77 if (record_.name == ES_MODULE_RECORD || field_.name == MODULE_RECORD_IDX) {
78 entity_container_.AddModuleLiteralArrayId(val);
79 auto module_literal_array_id_name = entity_container_.GetLiteralArrayIdName(val);
80 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::LITERALARRAY>(
81 module_literal_array_id_name));
82 } else if (record_.name == ES_SCOPE_NAMES_RECORD || field_.name == SCOPE_NAMES) {
83 entity_container_.AddUnnestedLiteralArrayId(val);
84 auto literal_array_id_name = entity_container_.GetLiteralArrayIdName(val);
85 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::LITERALARRAY>(
86 literal_array_id_name));
87 } else if (field_.name == MODULE_REQUEST_PAHSE_IDX) {
88 entity_container_.AddModuleRequestPhaseId(val);
89 auto module_literal_array_id_name = entity_container_.GetLiteralArrayIdName(val);
90 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::LITERALARRAY>(
91 module_literal_array_id_name));
92 } else if (field_.name == JSON_FILE_CONTENT) {
93 auto json_string_id = panda_file::File::EntityId(val);
94 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::STRING>(
95 entity_container_.GetStringById(json_string_id)));
96 } else {
97 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(val));
98 }
99 break;
100 }
101 case panda_file::Type::TypeId::U8: {
102 const uint8_t val = field_data_accessor_->GetValue<uint8_t>().value();
103 field_.metadata->SetValue(pandasm::ScalarValue::Create<pandasm::Value::Type::U8>(val));
104 break;
105 }
106 default:
107 UNREACHABLE();
108 }
109 }
110
FillFieldAnnotations()111 void AbcFieldProcessor::FillFieldAnnotations()
112 {
113 log::Unimplemented(__PRETTY_FUNCTION__);
114 }
115
116 } // namespace panda::abc2program
117