1 /*
2 * Copyright (c) 2021 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 "ecmascript/ts_types/ts_type_table.h"
17
18 namespace panda::ecmascript {
GetExportValueTable(JSThread * thread,JSHandle<TSTypeTable> typeTable)19 JSHandle<JSTaggedValue> TSTypeTable::GetExportValueTable(JSThread *thread, JSHandle<TSTypeTable> typeTable)
20 {
21 int index = static_cast<int>(typeTable->GetLength()) - 1;
22 JSHandle<JSTaggedValue> exportValueTable(thread, typeTable->Get(index));
23 return exportValueTable;
24 }
25
SetExportValueTable(JSThread * thread,JSHandle<TSTypeTable> typeTable,JSHandle<TaggedArray> exportValueTable)26 void TSTypeTable::SetExportValueTable(JSThread *thread, JSHandle<TSTypeTable> typeTable,
27 JSHandle<TaggedArray> exportValueTable)
28 {
29 if (exportValueTable->GetLength() != 0) { // add exprotValueTable to tSTypeTable if isn't empty
30 typeTable->Set(thread, typeTable->GetLength() - 1, exportValueTable);
31 }
32 }
33
PushBackTypeToTable(JSThread * thread,JSHandle<TSTypeTable> & table,const JSHandle<TSType> & type)34 JSHandle<TSTypeTable> TSTypeTable::PushBackTypeToTable(JSThread *thread, JSHandle<TSTypeTable> &table,
35 const JSHandle<TSType> &type)
36 {
37 uint32_t capacity = table->GetLength(); // can't be 0 due to RESERVE_TABLE_LENGTH
38 uint32_t numberOfTypes = static_cast<uint32_t>(table->GetNumberOfTypes());
39 if (UNLIKELY(capacity <= numberOfTypes + RESERVE_TABLE_LENGTH)) {
40 table = JSHandle<TSTypeTable>(TaggedArray::SetCapacity(thread, JSHandle<TaggedArray>(table),
41 capacity * INCREASE_CAPACITY_RATE));
42 }
43
44 table->Set(thread, numberOfTypes + 1, type);
45 table->SetNumberOfTypes(thread, numberOfTypes + 1);
46
47 return table;
48 }
49 } // namespace panda::ecmascript
50