• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_FUNCTION_V8_FUNCTION_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_FUNCTION_V8_FUNCTION_H
18 
19 #include <functional>
20 
21 #include "third_party/v8/include/v8.h"
22 
23 #include "base/memory/ace_type.h"
24 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
25 #include "frameworks/bridge/declarative_frontend/engine/v8/v8_bindings.h"
26 #include "frameworks/bridge/declarative_frontend/engine/v8/v8_utils.h"
27 
28 namespace OHOS::Ace::Framework {
29 
30 class ACE_EXPORT V8Function : public virtual AceType {
31     DECLARE_ACE_TYPE(V8Function, AceType);
32 
33 public:
34     void execute();
35     void execute(std::vector<std::string> keys, const std::string& param);
36 
37 protected:
38     v8::Persistent<v8::Function> jsFunction_;
39     v8::Persistent<v8::Value> jsThis_;
40     v8::Persistent<v8::Context> ctx_;
41     v8::Isolate* isolate_;
42 
43     v8::Local<v8::Value> executeJS(int argc = 0, v8::Local<v8::Value>* argv = nullptr);
44 
45 public:
46     V8Function(v8::Local<v8::Value> jsObject, v8::Local<v8::Function> jsRenderFunction);
47     ~V8Function();
48 };
49 
50 template<class T, int32_t ARGC = 0>
51 class ACE_EXPORT V8EventFunction : public V8Function {
52     DECLARE_ACE_TYPE(V8EventFunction, V8Function);
53 
54 public:
55     using ParseFunc = std::function<v8::Local<v8::Value>(const T&, v8::Isolate*)>;
56     V8EventFunction() = delete;
V8EventFunction(v8::Local<v8::Function> jsFunction,ParseFunc parser)57     V8EventFunction(v8::Local<v8::Function> jsFunction, ParseFunc parser)
58         : V8Function(v8::Undefined(v8::Isolate::GetCurrent()), jsFunction), parser_(parser)
59     {}
60     ~V8EventFunction() = default;
61 
execute()62     void execute()
63     {
64         ACE_DCHECK(isolate_);
65         v8::Isolate::Scope isolateScope(isolate_);
66         v8::HandleScope handleScope(isolate_);
67         auto context = ctx_.Get(isolate_);
68         v8::Context::Scope contextScope(context);
69         V8Function::executeJS();
70     }
71 
execute(const T & eventInfo)72     void execute(const T& eventInfo)
73     {
74         ACE_DCHECK(isolate_);
75         v8::Isolate::Scope isolateScope(isolate_);
76         v8::HandleScope handleScope(isolate_);
77         auto context = ctx_.Get(isolate_);
78         v8::Context::Scope contextScope(context);
79         v8::Local<v8::Value> v8Param;
80         if (parser_) {
81             v8Param = parser_(eventInfo, isolate_);
82         }
83         V8Function::executeJS(ARGC, &v8Param);
84     }
85 
86 private:
87     ParseFunc parser_;
88 };
89 
90 } // namespace OHOS::Ace::Framework
91 
92 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_FUNCTION_V8_FUNCTION_H