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