idlize

Форк
0
/
common-interop.cc 
162 строки · 4.8 Кб
1
/*
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
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 <string>
17
#include <vector>
18
#include "common-interop.h"
19
#include "ArgDeserializerBase.h"
20

21
CustomDeserializer* ArgDeserializerBase::customDeserializers = nullptr;
22

23
#if KOALA_INTEROP_PROFILER
24
#include "profiler.h"
25

26
InteropProfiler* InteropProfiler::_instance = nullptr;
27

28
#endif
29

30
using std::string;
31

32
#ifdef KOALA_NAPI
33
static Napi::Reference<Napi::Function> g_koalaNapiCallbackDispatcher;
34

35
void impl_SetCallbackDispatcher(Napi::Object dispatcher) {
36
    g_koalaNapiCallbackDispatcher = Napi::Reference<Napi::Function>::New(dispatcher.As<Napi::Function>(), 1);
37
}
38
KOALA_INTEROP_V1(SetCallbackDispatcher, Napi::Object)
39

40
void impl_CleanCallbackDispatcher() {
41
    if (!g_koalaNapiCallbackDispatcher.IsEmpty()) {
42
        g_koalaNapiCallbackDispatcher.Reset();
43
    }
44
}
45
KOALA_INTEROP_V0(CleanCallbackDispatcher)
46

47
napi_value getKoalaNapiCallbackDispatcher() {
48
    if (g_koalaNapiCallbackDispatcher.IsEmpty()) {
49
        abort();
50
    }
51
    return (napi_value)g_koalaNapiCallbackDispatcher.Value();
52
}
53
#endif
54

55
KInt impl_StringLength(KNativePointer ptr) {
56
    string* s = reinterpret_cast<string*>(ptr);
57
    return s->length();
58
}
59
KOALA_INTEROP_1(StringLength, KInt, KNativePointer)
60

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);
64
}
65
KOALA_INTEROP_V3(StringData, KNativePointer, KByte*, KUInt)
66

67
KNativePointer impl_StringMake(const KStringPtr& str) {
68
    return new string(str.c_str());
69
}
70
KOALA_INTEROP_1(StringMake, KNativePointer, KStringPtr)
71

72
void stringFinalizer(string* ptr) {
73
    delete ptr;
74
}
75
KNativePointer impl_GetStringFinalizer() {
76
    return fnPtr<string>(stringFinalizer);
77
}
78
KOALA_INTEROP_0(GetStringFinalizer, KNativePointer)
79

80
void impl_InvokeFinalizer(KNativePointer obj, KNativePointer finalizer) {
81
    auto finalizer_f = reinterpret_cast<void (*)(KNativePointer)>(finalizer);
82
    finalizer_f(obj);
83
}
84
KOALA_INTEROP_V2(InvokeFinalizer, KNativePointer, KNativePointer)
85

86
KInt impl_GetPtrVectorSize(KNativePointer ptr) {
87
    return reinterpret_cast<std::vector<void*>*>(ptr)->size();
88
}
89
KOALA_INTEROP_1(GetPtrVectorSize, KInt, KNativePointer)
90

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);
95
}
96
KOALA_INTEROP_2(GetPtrVectorElement, KNativePointer, KNativePointer, KInt)
97

98
inline KUInt unpackUInt(const KByte* bytes) {
99
    return (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
100
}
101

102
std::vector<KStringPtr> makeStringVector(KStringArray strArray) {
103
    if (strArray == nullptr) {
104
        return std::vector<KStringPtr>(0);
105
    }
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);
113
    }
114
    return res;
115
}
116

117
std::vector<KStringPtr> makeStringVector(KNativePointerArray arr, KInt length) {
118
    if (arr == nullptr) {
119
        return std::vector<KStringPtr>(0);
120
    } else {
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]);
125
            res[i].assign(str);
126
        }
127
        return res;
128
    }
129
}
130

131
std::vector<std::string> groupedLogs;
132

133
void clearGroupedLog(KInt index) {
134
    if (index >=0 && index < groupedLogs.size()) {
135
        groupedLogs[index] = std::string();
136
    }
137
}
138

139
void appendGroupedLog(KInt index, const std::string& str) {
140
    if (index < 0) return;
141
    if (index >= groupedLogs.size()) {
142
        groupedLogs.resize(index + 1);
143
    }
144
    groupedLogs[index].append(str);
145
}
146

147
std::string getGroupedLog(int32_t index) {
148
    if (index >=0 && index < groupedLogs.size()) {
149
        return groupedLogs[index];
150
    }
151
    return std::string();
152
}
153

154
KNativePointer impl_GetGroupedLog(KInt index) {
155
    return new std::string(getGroupedLog(index));
156
}
157
KOALA_INTEROP_1(GetGroupedLog, KNativePointer, KInt)
158

159
void impl_ClearGroupedLog(KInt index) {
160
    clearGroupedLog(index);
161
}
162
KOALA_INTEROP_V1(ClearGroupedLog, KInt)
163

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.