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