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