1 /*
2 * Copyright (c) 2025 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 _KOALA_TYPES_H
17 #define _KOALA_TYPES_H
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <cmath>
23
24 struct KStringPtrImpl {
KStringPtrImplKStringPtrImpl25 KStringPtrImpl(const char* str) : _value(nullptr), _owned(true) {
26 int len = str ? strlen(str) : 0;
27 assign(str, len);
28 }
KStringPtrImplKStringPtrImpl29 KStringPtrImpl(const char* str, int len, bool owned) : _value(nullptr), _owned(owned) {
30 assign(str, len);
31 }
KStringPtrImplKStringPtrImpl32 KStringPtrImpl() : _value(nullptr), _length(0), _owned(true) {}
33
34 KStringPtrImpl(const KStringPtrImpl& other) = delete;
35 KStringPtrImpl& operator=(const KStringPtrImpl& other) = delete;
36
KStringPtrImplKStringPtrImpl37 KStringPtrImpl(KStringPtrImpl&& other) {
38 this->_value = other.release();
39 this->_owned = other._owned;
40 other._owned = false;
41 this->_length = other._length;
42 }
43
~KStringPtrImplKStringPtrImpl44 ~KStringPtrImpl() { if (_value && _owned) free(_value); }
45
isNullKStringPtrImpl46 bool isNull() const { return _value == nullptr; }
c_strKStringPtrImpl47 const char* c_str() const { return _value; }
dataKStringPtrImpl48 char* data() const { return _value; }
lengthKStringPtrImpl49 int length() const { return _length; }
50
resizeKStringPtrImpl51 void resize(int size) {
52 _length = size;
53 if (!_owned) return;
54 // Ignore old content.
55 if (_value && _owned) free(_value);
56 _value = reinterpret_cast<char*>(malloc(size + 1));
57 _value[size] = 0;
58 }
59
assignKStringPtrImpl60 void assign(const char* data) {
61 assign(data, data ? strlen(data) : 0);
62 }
63
assignKStringPtrImpl64 void assign(const char* data, int len) {
65 if (_value && _owned) free(_value);
66 if (data) {
67 if (_owned) {
68 _value = reinterpret_cast<char*>(malloc(len + 1));
69 #ifdef __STDC_LIB_EXT1__
70 errno_t res = memcpy_s(_value, len, data, len);
71 if (res != EOK) {
72 return;
73 }
74 #else
75 memcpy(_value, data, len);
76 #endif
77 _value[len] = 0;
78 } else {
79 _value = const_cast<char*>(data);
80 }
81 } else {
82 _value = nullptr;
83 }
84 _length = len;
85 }
86
87 protected:
releaseKStringPtrImpl88 char* release() {
89 char* result = this->_value;
90 this->_value = nullptr;
91 return result;
92 }
93 private:
94 char* _value;
95 int _length;
96 bool _owned;
97 };
98
99 struct KInteropNumber {
100 int8_t tag;
101 union {
102 int32_t i32;
103 float f32;
104 };
fromDoubleKInteropNumber105 static inline KInteropNumber fromDouble(double value) {
106 KInteropNumber result = { 0 };
107 // TODO: boundary check
108 if (value == std::floor(value)) {
109 result.tag = 102; // ARK_TAG_INT32
110 result.i32 = static_cast<int>(value);
111 } else {
112 result.tag = 103; // ARK_TAG_FLOAT32
113 result.f32 = (float)value;
114 }
115 return result;
116 }
asDoubleKInteropNumber117 inline double asDouble() {
118 if (tag == 102) // ARK_TAG_INT32
119 return (double)i32;
120 else
121 return (double)f32;
122 }
123 };
124
125 typedef int8_t KBoolean;
126 typedef uint8_t KByte;
127 typedef int16_t KChar;
128 typedef int16_t KShort;
129 typedef uint16_t KUShort;
130 typedef int32_t KInt;
131 typedef uint32_t KUInt;
132 typedef float KFloat;
133 typedef int64_t KLong;
134 typedef uint64_t KULong;
135 typedef double KDouble;
136 typedef void* KNativePointer;
137 typedef KStringPtrImpl KStringPtr;
138 typedef float* KFloatArray;
139 typedef const uint8_t* KStringArray;
140 typedef void** KNativePointerArray;
141
142 struct KInteropBuffer {
143 KLong length;
144 KNativePointer data;
145
146 KInt resourceId;
147 void (*dispose)(KInt /* resourceId for now */);
148 };
149
150 struct KInteropReturnBuffer {
151 KInt length;
152 KNativePointer data;
153 void (*dispose)(KNativePointer data, KInt length);
154 };
155
156 struct KLength {
157 KByte type;
158 KFloat value;
159 KInt unit;
160 KInt resource;
161 };
162
parseKLength(const KStringPtrImpl & string,KLength * result)163 inline void parseKLength(const KStringPtrImpl &string, KLength *result)
164 {
165 char *suffixPtr = nullptr;
166
167 float value = std::strtof(string.c_str(), &suffixPtr);
168
169 if (!suffixPtr || suffixPtr == string.c_str())
170 {
171 // not a numeric value
172 result->unit = -1;
173 return;
174 }
175 result->value = value;
176 if (suffixPtr[0] == '\0' || (suffixPtr[0] == 'v' && suffixPtr[1] == 'p'))
177 {
178 result->unit = 1;
179 }
180 else if (suffixPtr[0] == '%')
181 {
182 result->unit = 3;
183 }
184 else if (suffixPtr[0] == 'p' && suffixPtr[1] == 'x')
185 {
186 result->unit = 0;
187 }
188 else if (suffixPtr[0] == 'l' && suffixPtr[1] == 'p' && suffixPtr[2] == 'x')
189 {
190 result->unit = 4;
191 }
192 else if (suffixPtr[0] == 'f' && suffixPtr[1] == 'p')
193 {
194 result->unit = 2;
195 }
196 else
197 {
198 result->unit = -1;
199 }
200 }
201
202 struct _KVMContext;
203 typedef _KVMContext *KVMContext;
204
205 // BEWARE: this MUST never be used in user code, only in very rare service code.
206 struct _KVMObject;
207 typedef _KVMObject *KVMObjectHandle;
208
209 typedef struct KVMDeferred {
210 void* handler;
211 void* context;
212 void (*resolve)(KVMDeferred* thiz, uint8_t* data, int32_t length);
213 void (*reject)(KVMDeferred* thiz, const char* message);
214 } KVMDeferred;
215
ptr(KNativePointer ptr)216 template <class T> T* ptr(KNativePointer ptr) {
217 return reinterpret_cast<T*>(ptr);
218 }
219
ref(KNativePointer ptr)220 template <class T> T& ref(KNativePointer ptr) {
221 return *reinterpret_cast<T*>(ptr);
222 }
223
nativePtr(void * pointer)224 inline KNativePointer nativePtr(void* pointer) {
225 return reinterpret_cast<KNativePointer>(pointer);
226 }
227
fnPtr(void (* pointer)(T *))228 template <class T> KNativePointer fnPtr(void (*pointer)(T*)) {
229 return reinterpret_cast<KNativePointer>(pointer);
230 }
231
232 #endif /* _KOALA_TYPES_H */
233