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_INVOKE_H 17 #define ECMASCRIPT_INVOKE_H 18 19 #include "ecmascript/js_function.h" 20 #include "ecmascript/js_handle.h" 21 #include "ecmascript/js_object.h" 22 #include "ecmascript/js_thread.h" 23 #include "ecmascript/mem/c_containers.h" 24 25 namespace panda::ecmascript { 26 class JsInvoker { 27 public: JsInvoker(const JSThread * thread,JSTaggedValue func,JSTaggedValue obj)28 JsInvoker(const JSThread *thread, JSTaggedValue func, JSTaggedValue obj) 29 : JsInvoker(thread, func, obj, JSTaggedValue::Undefined()) 30 { 31 } 32 JsInvoker(const JSThread * thread,JSTaggedValue func,JSTaggedValue obj,JSTaggedValue newTarget)33 JsInvoker(const JSThread *thread, JSTaggedValue func, JSTaggedValue obj, JSTaggedValue newTarget) 34 { 35 AddArgument(JSHandle<JSTaggedValue>(thread, func)); 36 AddArgument(JSHandle<JSTaggedValue>(thread, newTarget)); 37 AddArgument(JSHandle<JSTaggedValue>(thread, obj)); 38 } 39 40 ~JsInvoker() = default; 41 NO_COPY_SEMANTIC(JsInvoker); 42 NO_MOVE_SEMANTIC(JsInvoker); 43 44 template<class T> AddArgument(const JSHandle<T> & arg)45 void AddArgument(const JSHandle<T> &arg) 46 { 47 args_.emplace_back(JSHandle<JSTaggedValue>(arg)); 48 } 49 50 template<class T> AddArgument(JSHandle<T> && arg)51 void AddArgument(JSHandle<T> &&arg) 52 { 53 args_.emplace_back(std::move(arg)); 54 } 55 56 JSTaggedValue Invoke(JSThread *thread); 57 58 private: 59 CVector<JSHandle<JSTaggedValue>> args_{}; 60 }; 61 62 JSTaggedValue InvokeJsFunction(JSThread *thread, const JSHandle<JSFunction> &func, const JSHandle<JSTaggedValue> &obj, 63 const JSHandle<JSTaggedValue> &newTgt, InternalCallParams *arguments); 64 } // namespace panda::ecmascript 65 66 #endif // ECMASCRIPT_INVOKE_H 67