2
* Copyright (c) 2022-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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "common-interop.h"
19
#include "ArgDeserializerBase.h"
21
CustomDeserializer* ArgDeserializerBase::customDeserializers = nullptr;
23
#if KOALA_INTEROP_PROFILER
26
InteropProfiler* InteropProfiler::_instance = nullptr;
33
static Napi::Reference<Napi::Function> g_koalaNapiCallbackDispatcher;
35
void impl_SetCallbackDispatcher(Napi::Object dispatcher) {
36
g_koalaNapiCallbackDispatcher = Napi::Reference<Napi::Function>::New(dispatcher.As<Napi::Function>(), 1);
38
KOALA_INTEROP_V1(SetCallbackDispatcher, Napi::Object)
40
void impl_CleanCallbackDispatcher() {
41
if (!g_koalaNapiCallbackDispatcher.IsEmpty()) {
42
g_koalaNapiCallbackDispatcher.Reset();
45
KOALA_INTEROP_V0(CleanCallbackDispatcher)
47
napi_value getKoalaNapiCallbackDispatcher() {
48
if (g_koalaNapiCallbackDispatcher.IsEmpty()) {
51
return (napi_value)g_koalaNapiCallbackDispatcher.Value();
55
KInt impl_StringLength(KNativePointer ptr) {
56
string* s = reinterpret_cast<string*>(ptr);
59
KOALA_INTEROP_1(StringLength, KInt, KNativePointer)
61
void impl_StringData(KNativePointer ptr, KByte* bytes, KUInt size) {
62
string* s = reinterpret_cast<string*>(ptr);
63
if (s) memcpy(bytes, s->c_str(), size);
65
KOALA_INTEROP_V3(StringData, KNativePointer, KByte*, KUInt)
67
KNativePointer impl_StringMake(const KStringPtr& str) {
68
return new string(str.c_str());
70
KOALA_INTEROP_1(StringMake, KNativePointer, KStringPtr)
72
void stringFinalizer(string* ptr) {
75
KNativePointer impl_GetStringFinalizer() {
76
return fnPtr<string>(stringFinalizer);
78
KOALA_INTEROP_0(GetStringFinalizer, KNativePointer)
80
void impl_InvokeFinalizer(KNativePointer obj, KNativePointer finalizer) {
81
auto finalizer_f = reinterpret_cast<void (*)(KNativePointer)>(finalizer);
84
KOALA_INTEROP_V2(InvokeFinalizer, KNativePointer, KNativePointer)
86
KInt impl_GetPtrVectorSize(KNativePointer ptr) {
87
return reinterpret_cast<std::vector<void*>*>(ptr)->size();
89
KOALA_INTEROP_1(GetPtrVectorSize, KInt, KNativePointer)
91
KNativePointer impl_GetPtrVectorElement(KNativePointer ptr, KInt index) {
92
auto vector = reinterpret_cast<std::vector<void*>*>(ptr);
93
auto element = vector->at(index);
94
return nativePtr(element);
96
KOALA_INTEROP_2(GetPtrVectorElement, KNativePointer, KNativePointer, KInt)
98
inline KUInt unpackUInt(const KByte* bytes) {
99
return (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
102
std::vector<KStringPtr> makeStringVector(KStringArray strArray) {
103
if (strArray == nullptr) {
104
return std::vector<KStringPtr>(0);
106
KUInt arraySize = unpackUInt(strArray);
107
std::vector<KStringPtr> res(arraySize);
108
size_t offset = sizeof(KUInt);
109
for (KUInt i = 0; i < arraySize; ++i) {
110
int len = unpackUInt(strArray + offset);
111
res[i].assign((const char*)(strArray + offset + sizeof(KUInt)), len);
112
offset += len + sizeof(KUInt);
117
std::vector<KStringPtr> makeStringVector(KNativePointerArray arr, KInt length) {
118
if (arr == nullptr) {
119
return std::vector<KStringPtr>(0);
121
std::vector<KStringPtr> res(length);
122
char** strings = reinterpret_cast<char**>(arr);
123
for (KInt i = 0; i < length; ++i) {
124
const char* str = reinterpret_cast<const char*>(strings[i]);
131
std::vector<std::string> groupedLogs;
133
void clearGroupedLog(KInt index) {
134
if (index >=0 && index < groupedLogs.size()) {
135
groupedLogs[index] = std::string();
139
void appendGroupedLog(KInt index, const std::string& str) {
140
if (index < 0) return;
141
if (index >= groupedLogs.size()) {
142
groupedLogs.resize(index + 1);
144
groupedLogs[index].append(str);
147
std::string getGroupedLog(int32_t index) {
148
if (index >=0 && index < groupedLogs.size()) {
149
return groupedLogs[index];
151
return std::string();
154
KNativePointer impl_GetGroupedLog(KInt index) {
155
return new std::string(getGroupedLog(index));
157
KOALA_INTEROP_1(GetGroupedLog, KNativePointer, KInt)
159
void impl_ClearGroupedLog(KInt index) {
160
clearGroupedLog(index);
162
KOALA_INTEROP_V1(ClearGroupedLog, KInt)