1 /*
2  * Copyright (c) 2023-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 #ifndef ECMASCRIPT_COMPILER_BUILTINS_COLLECTION_STUB_BUILDER_H
17 #define ECMASCRIPT_COMPILER_BUILTINS_COLLECTION_STUB_BUILDER_H
18 #include "ecmascript/compiler/builtins/builtins_stubs.h"
19 #include "ecmascript/compiler/stub_builder-inl.h"
20 #include "ecmascript/compiler/builtins/linked_hashtable_stub_builder.h"
21 
22 namespace panda::ecmascript::kungfu {
23 template <typename CollectionType>
24 class BuiltinsCollectionStubBuilder : public BuiltinsStubBuilder {
25 public:
BuiltinsCollectionStubBuilder(BuiltinsStubBuilder *parent, GateRef glue, GateRef thisValue, GateRef numArgs)26     explicit BuiltinsCollectionStubBuilder(BuiltinsStubBuilder *parent, GateRef glue, GateRef thisValue,
27         GateRef numArgs) : BuiltinsStubBuilder(parent), glue_(glue), thisValue_(thisValue), numArgs_(numArgs) {}
28     ~BuiltinsCollectionStubBuilder() override = default;
29     NO_MOVE_SEMANTIC(BuiltinsCollectionStubBuilder);
30     NO_COPY_SEMANTIC(BuiltinsCollectionStubBuilder);
31     void GenerateCircuit() override {}
32 
33     void Clear(Variable *result, Label *exit, Label *slowPath);
34     void Values(Variable *result, Label *exit, Label *slowPath);
35     void Entries(Variable *result, Label *exit, Label *slowPath);
36     void Keys(Variable *result, Label *exit, Label *slowPath);
37     void ForEach(Variable *result, Label *exit, Label *slowPath);
38     void Set(Variable *result, Label *exit, Label *slowPath);
39     void Add(Variable *result, Label *exit, Label *slowPath);
40     void Delete(Variable *result, Label *exit, Label *slowPath);
41     void Has(Variable *result, Label *exit, Label *slowPath);
42     void Get(Variable *result, Label *exit, Label *slowPath);
43 
44 private:
45     // check target obj
46     void CheckCollectionObj(Label *exit, Label *slowPath);
47     void CreateIterator(Variable *result, Label *exit, Label *slowPath, GateRef iterationKind);
48     void MapSetOrSetAdd(Variable *result, Label *exit, Label *slowPath, bool isJsMapSet);
49 
GetLinkedOffset()50     GateRef GetLinkedOffset()
51     {
52         int32_t linkedTableOffset = 0;
53         if constexpr (std::is_same_v<CollectionType, JSMap>) {
54             linkedTableOffset = CollectionType::LINKED_MAP_OFFSET;
55         } else {
56             linkedTableOffset = CollectionType::LINKED_SET_OFFSET;
57         }
58         return IntPtr(linkedTableOffset);
59     }
60 
GetLinked()61     GateRef GetLinked()
62     {
63         GateRef linkedTableOffset = GetLinkedOffset();
64         return Load(VariableType::JS_ANY(), thisValue_, linkedTableOffset);
65     }
66 
SetLinked(GateRef newTable)67     void SetLinked(GateRef newTable)
68     {
69         GateRef linkedTableOffset = GetLinkedOffset();
70         Store(VariableType::JS_ANY(), glue_, thisValue_, linkedTableOffset, newTable);
71     }
72 
73     GateRef glue_;
74     GateRef thisValue_;
75     GateRef numArgs_;
76 };
77 }  // namespace panda::ecmascript::kungfu
78 #endif  // ECMASCRIPT_COMPILER_BUILTINS_COLLECTION_STUB_BUILDER_H