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, FunctionBinding<Base, R, Args...>*);
51 
52     template<typename T>
53     static void CustomMethod(
54         const char* name, FunctionBinding<T, panda::Local<panda::JSValueRef>, panda::JsiRuntimeCallInfo*>* binding);
55 
56     static void CustomMethod(const char* name, FunctionCallback callback);
57 
58     template<typename T>
59     static void CustomMethod(const char* name, FunctionBinding<T, void, const JSCallbackInfo&>* binding);
60 
61     template<typename T>
62     static void CustomProperty(const char* name,
63         FunctionBinding<T, panda::Local<panda::JSValueRef>, panda::JsiRuntimeCallInfo*>* getter,
64         FunctionBinding<T, panda::Local<panda::JSValueRef>, panda::JsiRuntimeCallInfo*>* setter);
65 
66     static void CustomProperty(const char* name, FunctionGetCallback getter, FunctionSetCallback setter);
67 
68     template<typename T>
69     static void CustomProperty(const char* name, FunctionBinding<T, void, const JSCallbackInfo&>* getter,
70         FunctionBinding<T, void, const JSCallbackInfo&>* setter);
71 
72     template<typename R, typename... Args>
73     static void StaticMethod(const char* name, StaticFunctionBinding<R, Args...>* staticFunctionBinding);
74 
75     static void StaticMethod(
76         const char* name, StaticFunctionBinding<void, const JSCallbackInfo&>* staticFunctionBinding);
77 
78     static void CustomStaticMethod(const char* name, FunctionCallback callback);
79 
80     template<typename T>
81     static void StaticConstant(const char* name, T val);
82 
83     static void Bind(BindingTarget t, FunctionCallback ctor);
84 
85     static void Bind(BindingTarget t, JSFunctionCallback ctor, JSDestructorCallback<C> dtor = nullptr,
86         JSGCMarkCallback<C> gcMark = nullptr);
87 
88     template<typename... Args>
89     static void Bind(BindingTarget t, JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
90 
91     template<typename Base>
92     static void InheritAndBind(BindingTarget t, JSFunctionCallback ctor = nullptr,
93         JSDestructorCallback<C> dtor = nullptr, JSGCMarkCallback<C> gcMark = nullptr);
94 
95     template<typename Base>
96     static void Inherit();
97 
98     static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetCustomFunctions();
99 
100     static std::unordered_map<std::string, panda::Global<panda::FunctionRef>>& GetStaticFunctions();
101 
102     static panda::Local<panda::JSValueRef> NewInstance();
103 
104 private:
105     template<typename T, typename... Args>
106     static panda::Local<panda::JSValueRef> InternalMemberFunctionCallback(panda::JsiRuntimeCallInfo* runtimeCallInfo);
107 
108     template<typename T>
109     static panda::Local<panda::JSValueRef> InternalJSMemberFunctionCallback(panda::JsiRuntimeCallInfo* runtimeCallInfo);
110 
111     template<typename Class, typename R, typename... Args>
112     static panda::Local<panda::JSValueRef> MethodCallback(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* env, 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 };
141 
142 template<typename T>
143 using JSClass = JSClassImpl<T, JsiClass>;
144 
145 }; // namespace OHOS::Ace::Framework
146 
147 #include "jsi_bindings.inl"
148 
149 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_JSI_BINDINGS_H
150