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     if (instance == nullptr) {
33         instance = new CJFFIFnInvoker();
34     }
35     return instance;
36 }
37 
GetCJFuncs()38 const FFIAtCPackage& CJFFIFnInvoker::GetCJFuncs()
39 {
40     if (!fnLoaded_) {
41         HiLog::Fatal(LABEL, "CJ-FFI functions not registered yet!");
42     }
43     return cjFFIFn_;
44 }
45 
FFIDataExist(int64_t id) const46 bool CJFFIFnInvoker::FFIDataExist(int64_t id) const
47 {
48     auto cjFunc = cjFFIFn_.atCOHOSFFIExistFFIDataExist;
49     if (!cjFunc) {
50         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIExistFFIDataExist is unregistered!");
51         return false;
52     }
53     return cjFunc(id);
54 }
55 
ReleaseFFIData(int64_t id) const56 bool CJFFIFnInvoker::ReleaseFFIData(int64_t id) const
57 {
58     auto cjFunc = cjFFIFn_.atCOHOSFFIReleaseFFIData;
59     if (!cjFunc) {
60         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIReleaseFFIData is unregistered!");
61         return false;
62     }
63     return cjFunc(id);
64 }
65 
ReleaseRemoteData(int64_t id) const66 bool CJFFIFnInvoker::ReleaseRemoteData(int64_t id) const
67 {
68     auto cjFunc = cjFFIFn_.atCOHOSFFIReleaseRemoteData;
69     if (!cjFunc) {
70         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFIReleaseRemoteData is unregistered!");
71         return false;
72     }
73     return cjFunc(id);
74 }
75 
ThrowCJError(const std::string & msg) const76 void CJFFIFnInvoker::ThrowCJError(const std::string& msg) const
77 {
78     auto cjFunc = cjFFIFn_.atCOHOSFFICJThrow;
79     if (!cjFunc) {
80         HiLog::Fatal(LABEL, "CJ-FFI atCOHOSFFICJThrow is unregistered!");
81         return;
82     }
83     cjFunc(msg.c_str());
84 }
85 
InvokeLambda(int64_t lambdaId,int32_t argc,void ** argv,void * result) const86 void CJFFIFnInvoker::InvokeLambda(int64_t lambdaId, int32_t argc, void** argv, void* result) const
87 {
88     auto cjFunc = cjFFIFn_.atCOHOSFFICallbackInvoker;
89     if (!cjFunc) {
90         HiLog::Fatal(LABEL, "CJ-FFI callback invoker atCOHOSFFICallbackInvoker is unregistered!");
91         return;
92     }
93     cjFunc(lambdaId, argc, argv, result);
94 }
95