1 /*
2 * Copyright (c) 2021-2022 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
18
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 #include "frameworks/bridge/declarative_frontend/engine/bindings_implementation.h"
22 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h"
23
24 namespace __detail__ {
25
26 template<typename... Types>
ToTuple(panda::JsiRuntimeCallInfo * runtimeCallInfo)27 std::tuple<Types...> ToTuple(panda::JsiRuntimeCallInfo *runtimeCallInfo)
28 {
29 int index = 0;
30 return {
31 OHOS::Ace::Framework::JsiValueConvertor::fromJsiValue<Types>(
32 runtimeCallInfo->GetVM(), runtimeCallInfo->GetCallArgRef(index++))...,
33 };
34 }
35
36 }; // namespace __detail__
37
38 namespace OHOS::Ace::Framework {
39
40 template<typename C>
41 class JsiClass {
42 public:
43 using ThisJSClass = JSClassImpl<C, JsiClass>;
44
45 JsiClass() = delete;
46
47 static void Declare(const char* name);
48
49 template<typename Base, typename R, typename... Args>
50 static void Method(const char* name, R (Base::*func)(Args...), int id);
51
52 template<typename T>
53 static void CustomMethod(const char* name, MemberFunctionCallback<T> callback, int id);
54
55 static void CustomMethod(const char* name, FunctionCallback callback);
56
57 template<typename T>
58 static void CustomMethod(const char* name, JSMemberFunctionCallback<T> callback, int id);
59
60 template<typename T>
61 static void CustomProperty(const char* name, MemberFunctionGetCallback<T> callback, int getterId, int setterId);
62
63 static void CustomProperty(const char* name, FunctionGetCallback getter, FunctionSetCallback setter);
64
65 template<typename T>
66 static void CustomProperty(const char* name, JSMemberFunctionCallback<T> callback, int getterId, int setterId);
67
68 template<typename R, typename... Args>
69 static void StaticMethod(const char* name, StaticFunctionBinding<R, Args...>* staticFunctionBinding);
70
71 static void StaticMethod(
72 const char* name, StaticFunctionBinding<void, const JSCallbackInfo&>* staticFunctionBinding);
73
74 static void CustomStaticMethod(const char* name, FunctionCallback callback);
75
76 template<typename T>
77 static void StaticConstant(const char* name, T val);
78
79 static void Bind(BindingTarget t, FunctionCallback ctor);
80
81 static void Bind(BindingTarget t, JSFunctionCallback ctor, JSDestructorCallback<C> dtor = nullptr,
82 JSGCMarkCallback<C> gcMark = nullptr);
83
84 template<typename... Args>
85 static void Bind(BindingTarget t, JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
86
87 template<typename Base>
88 static void InheritAndBind(
89 BindingTarget t, JSFunctionCallback ctor = nullptr,
90 JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
91
92 template<typename Base>
93 static void Inherit();
94
95 static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetCustomFunctions();
96
97 static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetStaticFunctions();
98
99 static panda::Local<panda::JSValueRef> NewInstance();
100
101 private:
102 template<typename T, typename... Args>
103 static panda::Local<panda::JSValueRef> InternalMemberFunctionCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
104
105 template<typename T>
106 static panda::Local<panda::JSValueRef> InternalJSMemberFunctionCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
107
108 template<typename Class, typename R, typename... Args>
109 static panda::Local<panda::JSValueRef> MethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
110
111 template<typename Class, typename R, typename... Args>
112 static panda::Local<panda::JSValueRef> JSMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
113
114 template<typename R, typename... Args>
115 static panda::Local<panda::JSValueRef> StaticMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
116
117 static panda::Local<panda::JSValueRef> JSStaticMethodCallback(panda::JsiRuntimeCallInfo *runtimeCallInfo);
118
119 template<typename... Args>
120 static panda::Local<panda::JSValueRef> InternalConstructor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
121
122 static panda::Local<panda::JSValueRef> ConstructorInterceptor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
123
124 static void DestructorInterceptor(void* nativePtr, void* data);
125
126 static panda::Local<panda::JSValueRef> JSConstructorInterceptor(panda::JsiRuntimeCallInfo *runtimeCallInfo);
127
128 static bool CheckIfConstructCall(panda::JsiRuntimeCallInfo *runtimeCallInfo);
129
130 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> staticFunctions_;
131 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customFunctions_;
132 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customGetFunctions_;
133 static thread_local std::unordered_map<std::string, panda::Global<panda::FunctionRef>> customSetFunctions_;
134 static thread_local FunctionCallback constructor_;
135 static thread_local JSFunctionCallback jsConstructor_;
136 static thread_local JSDestructorCallback<C> jsDestructor_;
137 static thread_local JSGCMarkCallback<C> jsGcMark_;
138 static thread_local std::string className_;
139 static thread_local panda::Global<panda::FunctionRef> classFunction_;
140 static thread_local std::vector<shared_ptr<int32_t>> functionIds_;
141 };
142
143 template<typename T>
144 using JSClass = JSClassImpl<T, JsiClass>;
145
146 }; // namespace OHOS::Ace::Framework
147
148 #include "jsi_bindings.inl"
149
150 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
151