• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "cj_fn_invoker.h"
17 
18 #include <iostream>
19 
20 #include "hilog/log_cpp.h"
21 
22 using namespace OHOS;
23 using namespace OHOS::HiviewDFX;
24 
25 namespace {
26 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD003901, "CJ-FFIFuncs" };
27 } // namespace
28 
GetInstance()29 CJFFIFnInvoker* CJFFIFnInvoker::GetInstance()
30 {
31     static CJFFIFnInvoker instance;
32     return &instance;
33 }
34 
GetCJFuncs()35 const FFIAtCPackage& CJFFIFnInvoker::GetCJFuncs()
36 {
37     if (!fnLoaded_) {
38         HiLog::Fatal(LABEL, "CJ-FFI functions not registered yet!");
39     }
40     return cjFFIFn_;
41 }
42 
FFIDataExist(int64_t id) const43 bool CJFFIFnInvoker::FFIDataExist(int64_t id) const
44 {
45     auto cjFunc = cjFFIFn_.atCOHOSFFIExistFFIDataExist;
46     if (!cjFunc) {
47         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIExistFFIDataExist is unregistered!");
48         return false;
49     }
50     return cjFunc(id);
51 }
52 
ReleaseFFIData(int64_t id) const53 bool CJFFIFnInvoker::ReleaseFFIData(int64_t id) const
54 {
55     auto cjFunc = cjFFIFn_.atCOHOSFFIReleaseFFIData;
56     if (!cjFunc) {
57         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIReleaseFFIData is unregistered!");
58         return false;
59     }
60     return cjFunc(id);
61 }
62 
ReleaseRemoteData(int64_t id) const63 bool CJFFIFnInvoker::ReleaseRemoteData(int64_t id) const
64 {
65     auto cjFunc = cjFFIFn_.atCOHOSFFIReleaseRemoteData;
66     if (!cjFunc) {
67         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIReleaseRemoteData is unregistered!");
68         return false;
69     }
70     return cjFunc(id);
71 }
72 
ThrowCJError(const std::string & msg) const73 void CJFFIFnInvoker::ThrowCJError(const std::string& msg) const
74 {
75     auto cjFunc = cjFFIFn_.atCOHOSFFICJThrow;
76     if (!cjFunc) {
77         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFICJThrow is unregistered!");
78         return;
79     }
80     cjFunc(msg.c_str());
81 }
82 
InvokeLambda(int64_t lambdaId,int32_t argc,void ** argv,void * result) const83 void CJFFIFnInvoker::InvokeLambda(int64_t lambdaId, int32_t argc, void** argv, void* result) const
84 {
85     auto cjFunc = cjFFIFn_.atCOHOSFFICallbackInvoker;
86     if (!cjFunc) {
87         HiLog::Fatal(LABEL, "CJ-FFI callback invoker atCOHOSFFICallbackInvoker is unregistered!");
88         return;
89     }
90     cjFunc(lambdaId, argc, argv, result);
91 }
92