1 /* 2 * Copyright (c) 2021 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_INTERNAL_CALL_PARAMS_H 17 #define ECMASCRIPT_INTERNAL_CALL_PARAMS_H 18 19 #include "ecmascript/ecma_runtime_call_info.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/js_handle.h" 22 #include "ecmascript/js_function.h" 23 #include "ecmascript/mem/c_containers.h" 24 25 namespace panda::ecmascript { 26 class InternalCallParams { 27 public: 28 static constexpr uint8_t RESERVE_INTERNAL_CALL_PARAMS_FIXED_LENGTH = 128; 29 InternalCallParams()30 InternalCallParams() 31 { 32 } 33 34 ~InternalCallParams() = default; 35 GetArgv()36 inline const JSTaggedType *GetArgv() const 37 { 38 if (IsFixedMode()) { 39 return &fixed_data_.front(); 40 } 41 42 ASSERT_PRINT(variable_length_ > RESERVE_INTERNAL_CALL_PARAMS_FIXED_LENGTH, "internal call params mode error"); 43 return variable_data_.data(); 44 } 45 GetLength()46 inline uint32_t GetLength() const 47 { 48 if (IsFixedMode()) { 49 return fixed_length_; 50 } 51 52 ASSERT_PRINT(variable_length_ > RESERVE_INTERNAL_CALL_PARAMS_FIXED_LENGTH, "internal call params mode error"); 53 return variable_length_; 54 } 55 56 template<typename T> MakeArgv(const JSHandle<T> & arg)57 inline void MakeArgv(const JSHandle<T> &arg) 58 { 59 EnableFixedModeAndSetLength(1); 60 fixed_data_[0] = arg.GetTaggedType(); // 0: first index 61 } 62 63 template<typename T0, typename T1> MakeArgv(const JSHandle<T0> & arg0,const JSHandle<T1> & arg1)64 inline void MakeArgv(const JSHandle<T0> &arg0, const JSHandle<T1> &arg1) 65 { 66 EnableFixedModeAndSetLength(2); 67 fixed_data_[0] = arg0.GetTaggedType(); // 0: first index 68 fixed_data_[1] = arg1.GetTaggedType(); // 1: second index 69 } 70 71 template<typename T0, typename T1, typename T2> MakeArgv(const JSHandle<T0> & arg0,const JSHandle<T1> & arg1,const JSHandle<T2> & arg2)72 inline void MakeArgv(const JSHandle<T0> &arg0, const JSHandle<T1> &arg1, const JSHandle<T2> &arg2) 73 { 74 EnableFixedModeAndSetLength(3); 75 fixed_data_[0] = arg0.GetTaggedType(); // 0: first index 76 fixed_data_[1] = arg1.GetTaggedType(); // 1: second indexs 77 fixed_data_[2] = arg2.GetTaggedType(); // 2: third index 78 } 79 80 template<typename T0, typename T1, typename T2, typename T3> MakeArgv(const JSHandle<T0> & arg0,const JSHandle<T1> & arg1,const JSHandle<T2> & arg2,const JSHandle<T3> & arg3)81 inline void MakeArgv(const JSHandle<T0> &arg0, const JSHandle<T1> &arg1, const JSHandle<T2> &arg2, 82 const JSHandle<T3> &arg3) 83 { 84 EnableFixedModeAndSetLength(4); 85 fixed_data_[0] = arg0.GetTaggedType(); // 0: first index 86 fixed_data_[1] = arg1.GetTaggedType(); // 1: second index 87 fixed_data_[2] = arg2.GetTaggedType(); // 2: third index 88 fixed_data_[3] = arg3.GetTaggedType(); // 3: fourth index 89 } 90 MakeEmptyArgv()91 inline void MakeEmptyArgv() 92 { 93 EnableFixedModeAndSetLength(0); 94 } 95 MakeArgv(const JSTaggedValue arg)96 inline void MakeArgv(const JSTaggedValue arg) 97 { 98 EnableFixedModeAndSetLength(1); 99 fixed_data_[0] = arg.GetRawData(); // 0: first index 100 } 101 MakeArgv(const JSTaggedValue arg0,const JSTaggedValue arg1)102 inline void MakeArgv(const JSTaggedValue arg0, const JSTaggedValue arg1) 103 { 104 EnableFixedModeAndSetLength(2); 105 fixed_data_[0] = arg0.GetRawData(); // 0: first index 106 fixed_data_[1] = arg1.GetRawData(); // 1: second index 107 } 108 MakeArgv(const JSTaggedValue arg0,const JSTaggedValue arg1,const JSTaggedValue arg2)109 inline void MakeArgv(const JSTaggedValue arg0, const JSTaggedValue arg1, const JSTaggedValue arg2) 110 { 111 EnableFixedModeAndSetLength(3); 112 fixed_data_[0] = arg0.GetRawData(); // 0: first index 113 fixed_data_[1] = arg1.GetRawData(); // 1: second index 114 fixed_data_[2] = arg2.GetRawData(); // 2: third index 115 } 116 MakeArgv(const JSTaggedValue arg0,const JSTaggedValue arg1,const JSTaggedValue arg2,const JSTaggedValue arg3)117 inline void MakeArgv(const JSTaggedValue arg0, const JSTaggedValue arg1, const JSTaggedValue arg2, 118 const JSTaggedValue arg3) 119 { 120 EnableFixedModeAndSetLength(4); 121 fixed_data_[0] = arg0.GetRawData(); // 0: first index 122 fixed_data_[1] = arg1.GetRawData(); // 1: second index 123 fixed_data_[2] = arg2.GetRawData(); // 2: third index 124 fixed_data_[3] = arg3.GetRawData(); // 3: fourth index 125 } 126 127 void MakeArgv(const EcmaRuntimeCallInfo *info, uint32_t position); 128 129 void MakeArgList(const TaggedArray *argv); 130 void MakeArgListWithHole(const TaggedArray *argv, uint32_t length); 131 132 void MakeBoundArgv(const JSThread *thread, const JSHandle<JSBoundFunction> &boundFunc); 133 134 void Iterate(const RootRangeVisitor &v) const; 135 136 private: 137 DEFAULT_COPY_SEMANTIC(InternalCallParams); 138 DEFAULT_MOVE_SEMANTIC(InternalCallParams); 139 IsFixedMode()140 inline bool IsFixedMode() const 141 { 142 return !variable_mode_; 143 } 144 EnableFixedModeAndSetLength(uint32_t length)145 inline void EnableFixedModeAndSetLength(uint32_t length) 146 { 147 variable_mode_ = false; 148 variable_data_.clear(); 149 variable_length_ = 0; 150 fixed_length_ = length; 151 } 152 GetFixedLength()153 inline uint32_t GetFixedLength() const 154 { 155 return fixed_length_; 156 } 157 GetFixedDataAddress()158 inline uintptr_t GetFixedDataAddress() const 159 { 160 return ToUintPtr(&fixed_data_); 161 } 162 GetFixedBuffer(uint32_t idx)163 inline JSTaggedType GetFixedBuffer(uint32_t idx) const 164 { 165 return fixed_data_[idx]; 166 } 167 SetFixedBuffer(uint32_t idx,JSHandle<JSTaggedValue> val)168 inline void SetFixedBuffer(uint32_t idx, JSHandle<JSTaggedValue> val) 169 { 170 fixed_data_[idx] = val.GetTaggedType(); 171 } 172 SetFixedBuffer(uint32_t idx,JSTaggedValue val)173 inline void SetFixedBuffer(uint32_t idx, JSTaggedValue val) 174 { 175 fixed_data_[idx] = val.GetRawData(); 176 } 177 SetFixedBuffer(uint32_t idx,JSTaggedType val)178 inline void SetFixedBuffer(uint32_t idx, JSTaggedType val) 179 { 180 fixed_data_[idx] = val; 181 } 182 EnableVariableModeAndSetLength(uint32_t length)183 inline void EnableVariableModeAndSetLength(uint32_t length) 184 { 185 variable_mode_ = true; 186 fixed_length_ = 0; 187 variable_length_ = length; 188 variable_data_.resize(variable_length_); 189 } 190 GetVariableLength()191 inline uint32_t GetVariableLength() const 192 { 193 return variable_length_; 194 } 195 GetVariableDataAddress()196 inline uintptr_t GetVariableDataAddress() const 197 { 198 return ToUintPtr(variable_data_.data()); 199 } 200 GetVariableBuffer(uint32_t idx)201 inline JSTaggedType GetVariableBuffer(uint32_t idx) const 202 { 203 return variable_data_[idx]; 204 } 205 SetVariableBuffer(uint32_t idx,JSHandle<JSTaggedValue> val)206 inline void SetVariableBuffer(uint32_t idx, JSHandle<JSTaggedValue> val) 207 { 208 variable_data_[idx] = val.GetTaggedType(); 209 } 210 SetVariableBuffer(uint32_t idx,JSTaggedValue val)211 inline void SetVariableBuffer(uint32_t idx, JSTaggedValue val) 212 { 213 variable_data_[idx] = val.GetRawData(); 214 } 215 SetVariableBuffer(uint32_t idx,JSTaggedType val)216 inline void SetVariableBuffer(uint32_t idx, JSTaggedType val) 217 { 218 variable_data_[idx] = val; 219 } 220 InsertVariableBuffer(JSTaggedValue val)221 inline void InsertVariableBuffer(JSTaggedValue val) 222 { 223 variable_data_.insert(variable_data_.begin(), val.GetRawData()); 224 } 225 226 std::array<JSTaggedType, RESERVE_INTERNAL_CALL_PARAMS_FIXED_LENGTH> fixed_data_{}; 227 CVector<JSTaggedType> variable_data_{}; 228 uint32_t fixed_length_{0}; 229 uint32_t variable_length_{0}; 230 bool variable_mode_{false}; 231 }; 232 } // namespace panda::ecmascript 233 234 #endif // ECMASCRIPT_INTERNAL_CALL_PARAMS_H 235