• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_FUNCTION_JS_FUNCTION_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FUNCTION_H
18 
19 #include <functional>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/macros.h"
23 #include "bridge/declarative_frontend/engine/js_types.h"
24 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
25 #include "frameworks/bridge/declarative_frontend/engine/js_execution_scope_defines.h"
26 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
27 
28 namespace OHOS::Ace::Framework {
29 
30 class ACE_EXPORT JsFunctionBase : public virtual AceType {
31     DECLARE_ACE_TYPE(JsFunctionBase, AceType);
32 
33 public:
JsFunctionBase(const JSRef<JSObject> & jsObject)34     JsFunctionBase(const JSRef<JSObject>& jsObject)
35         : jsThis_(jsObject) {}
36 
Execute()37     void Execute()
38     {
39         ExecuteJS();
40     }
41 
42     void Execute(const std::vector<std::string>& keys, const std::string& param);
43 
ExecuteJS()44     JSRef<JSVal> ExecuteJS()
45     {
46         return ExecuteJS(0, nullptr);
47     }
48 
49     JSRef<JSVal> ExecuteJS(int argc, JSRef<JSVal> argv[]);
50 
51 protected:
This()52     JSRef<JSVal> This()
53     {
54         return jsThis_.Lock();
55     }
56     virtual JSRef<JSFunc> GetFunction() = 0;
57 
58 private:
59     JSWeak<JSVal> jsThis_;
60 };
61 
62 template<template<typename> class Ref = JSRef>
63 class ACE_EXPORT JsFunctionT : public JsFunctionBase {
64     DECLARE_ACE_TYPE(JsFunctionT, JsFunctionBase);
65 
66 public:
JsFunctionT(const JSRef<JSFunc> & jsFunction)67     explicit JsFunctionT(const JSRef<JSFunc>& jsFunction)
68         : JsFunctionT({}, jsFunction) {}
JsFunctionT(const JSRef<JSObject> & jsObject,const JSRef<JSFunc> & jsFunction)69     JsFunctionT(const JSRef<JSObject>& jsObject, const JSRef<JSFunc>& jsFunction)
70         : JsFunctionBase(jsObject), jsFunction_(jsFunction) {}
71     ~JsFunctionT() override = default;
72 
73 protected:
74     JSRef<JSFunc> GetFunction() override;
75     Ref<JSFunc> jsFunction_;
76 };
77 
78 using JsFunction = JsFunctionT<JSRef>;
79 using JsWeakFunction = JsFunctionT<JSWeak>;
80 
81 template<>
GetFunction()82 inline JSRef<JSFunc> JsWeakFunction::GetFunction()
83 {
84     return jsFunction_.Lock();
85 }
86 
87 template<>
GetFunction()88 inline JSRef<JSFunc> JsFunction::GetFunction()
89 {
90     return jsFunction_;
91 }
92 
93 } // namespace OHOS::Ace::Framework
94 
95 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FUNCTION_H
96