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_OBJECT_FAST_OPERATOR_H 17 #define ECMASCRIPT_OBJECT_FAST_OPERATOR_H 18 19 #include "ecmascript/js_hclass.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/property_attributes.h" 22 23 namespace panda::ecmascript { 24 using SCheckMode = JSShared::SCheckMode; 25 26 class ObjectFastOperator final { 27 public: 28 enum class Status: uint8_t { 29 None = 0x00UL, 30 UseOwn = 0x01UL, 31 GetInternal = 0x1UL << 1, 32 }; 33 UseOwn(Status status)34 static inline bool UseOwn(Status status) 35 { 36 return (static_cast<int32_t>(status) & static_cast<int32_t>(Status::UseOwn)) > 0; 37 } 38 GetInternal(Status status)39 static inline bool GetInternal(Status status) 40 { 41 return (static_cast<int32_t>(status) & static_cast<int32_t>(Status::GetInternal)) > 0; 42 } 43 44 static inline std::pair<JSTaggedValue, bool> HasOwnProperty(JSThread *thread, 45 JSTaggedValue receiver, JSTaggedValue key); 46 template<Status status = Status::None> 47 static inline JSTaggedValue GetPropertyByName(JSThread *thread, JSTaggedValue receiver, 48 JSTaggedValue key); 49 50 template <Status status = Status::None> 51 static inline JSTaggedValue SetPropertyByName(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key, 52 JSTaggedValue value, SCheckMode sCheckMode = SCheckMode::CHECK); 53 54 template<Status status = Status::None> 55 static inline JSTaggedValue GetPropertyByIndex(JSThread *thread, JSTaggedValue receiver, uint32_t index); 56 57 template<Status status = Status::None> 58 static inline JSTaggedValue SetPropertyByIndex(JSThread *thread, JSTaggedValue receiver, uint32_t index, 59 JSTaggedValue value); 60 61 template<Status status = Status::None> 62 static inline JSTaggedValue GetPropertyByValue(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key); 63 64 template <Status status = Status::None> 65 static inline JSTaggedValue SetPropertyByValue(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key, 66 JSTaggedValue value, SCheckMode sCheckMode = SCheckMode::CHECK); 67 68 static inline bool FastSetPropertyByValue(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key, 69 JSTaggedValue value); 70 71 static inline bool FastSetPropertyByIndex(JSThread *thread, JSTaggedValue receiver, uint32_t index, 72 JSTaggedValue value); 73 74 static inline JSTaggedValue FastGetPropertyByName(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key); 75 76 static inline JSTaggedValue FastGetPropertyByValue(JSThread *thread, JSTaggedValue receiver, JSTaggedValue key); 77 78 static inline JSTaggedValue FastGetPropertyByIndex(JSThread *thread, JSTaggedValue receiver, uint32_t index); 79 80 static inline JSTaggedValue FastParseDate(const EcmaString *str); 81 82 static inline PropertyAttributes AddPropertyByName(JSThread *thread, JSHandle<JSObject> objHandle, 83 JSHandle<JSTaggedValue> keyHandle, 84 JSHandle<JSTaggedValue> valueHandle, 85 PropertyAttributes attr); 86 87 static inline JSTaggedValue CallGetter(JSThread *thread, JSTaggedValue receiver, JSTaggedValue holder, 88 JSTaggedValue value); 89 private: 90 static inline JSTaggedValue CallSetter(JSThread *thread, JSTaggedValue receiver, JSTaggedValue value, 91 JSTaggedValue accessorValue); 92 93 static inline bool ShouldCallSetter(JSTaggedValue receiver, JSTaggedValue holder, JSTaggedValue accessorValue, 94 PropertyAttributes attr); 95 96 static inline bool IsSpecialIndexedObj(JSType jsType); 97 98 static inline bool IsFastTypeArray(JSType jsType); 99 100 static inline bool TryStringOrSymbolToIndex(JSTaggedValue key, uint32_t *output); 101 102 static inline JSTaggedValue FastGetTypeArrayProperty(JSThread *thread, JSTaggedValue receiver, JSTaggedValue holder, 103 JSTaggedValue key, JSType jsType); 104 105 static inline JSTaggedValue FastSetTypeArrayProperty(JSThread *thread, JSTaggedValue receiver, JSTaggedValue holder, 106 JSTaggedValue key, JSTaggedValue value, JSType jsType); 107 108 // non ECMA standard jsapi container 109 static inline bool IsSpecialContainer(JSType jsType); 110 111 static inline JSTaggedValue GetContainerProperty(JSThread *thread, JSTaggedValue receiver, uint32_t index, 112 JSType jsType); 113 114 static inline JSTaggedValue SetContainerProperty(JSThread *thread, JSTaggedValue receiver, uint32_t index, 115 JSTaggedValue value, JSType jsType); 116 117 static inline JSTaggedValue AddPropertyByIndex(JSThread *thread, JSTaggedValue receiver, uint32_t index, 118 JSTaggedValue value); 119 120 static inline int64_t TryToElementsIndex(JSTaggedValue key); 121 122 static inline bool GetNumFromString(const char *str, int len, int *index, int *num); 123 124 friend class FastRuntimeStub; 125 }; 126 } // namespace panda::ecmascript 127 #endif // ECMASCRIPT_OBJECT_FAST_OPERATOR_H 128