Skip to content

Commit 1f18f28

Browse files
committed
fix interop compat issues
1 parent e429bbf commit 1f18f28

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

NativeScript/ffi/Interop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ napi_value interop_sizeof(napi_env env, napi_callback_info info);
1717
napi_value interop_alloc(napi_env env, napi_callback_info info);
1818
napi_value interop_handleof(napi_env env, napi_callback_info info);
1919
napi_value interop_bufferFromData(napi_env env, napi_callback_info info);
20+
napi_value interop_stringFromCString(napi_env env, napi_callback_info info);
2021

2122
class Pointer {
2223
public:

NativeScript/ffi/Interop.mm

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,18 @@ void registerInterop(napi_env env, napi_value global) {
222222
.attributes = napi_enumerable,
223223
.data = nullptr,
224224
},
225+
{
226+
.utf8name = "stringFromCString",
227+
.method = interop_stringFromCString,
228+
.getter = nullptr,
229+
.setter = nullptr,
230+
.value = nullptr,
231+
.attributes = napi_enumerable,
232+
.data = nullptr,
233+
},
225234
};
226235

227-
napi_define_properties(env, interop, 12, properties);
236+
napi_define_properties(env, interop, 13, properties);
228237

229238
napi_set_named_property(env, global, "interop", interop);
230239
}
@@ -325,6 +334,42 @@ napi_value interop_free(napi_env env, napi_callback_info info) {
325334
return nullptr;
326335
}
327336

337+
napi_value interop_stringFromCString(napi_env env, napi_callback_info info) {
338+
napi_value arg;
339+
size_t argc = 1;
340+
napi_get_cb_info(env, info, &argc, &arg, nullptr, nullptr);
341+
342+
napi_valuetype type;
343+
napi_typeof(env, arg, &type);
344+
345+
if (type != napi_object) {
346+
napi_throw_type_error(env, "TypeError", "Expected an object");
347+
return nullptr;
348+
}
349+
350+
void *data = nullptr;
351+
352+
if (Pointer::isInstance(env, arg)) {
353+
Pointer *ptr = Pointer::unwrap(env, arg);
354+
data = ptr->data;
355+
}
356+
357+
if (Reference::isInstance(env, arg)) {
358+
Reference *ref = Reference::unwrap(env, arg);
359+
data = ref->data;
360+
}
361+
362+
napi_value result;
363+
364+
if (data == nullptr) {
365+
napi_get_null(env, &result);
366+
} else {
367+
napi_create_string_utf8(env, (const char *)data, NAPI_AUTO_LENGTH, &result);
368+
}
369+
370+
return result;
371+
}
372+
328373
size_t jsSizeof(napi_env env, napi_value arg) {
329374
napi_valuetype type;
330375
napi_typeof(env, arg, &type);

NativeScript/ffi/TypeConv.mm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,39 @@ void toNative(napi_env env, napi_value value, void* result, bool* shouldFree,
957957
bool* shouldFreeAny) override {
958958
NAPI_PREAMBLE
959959

960+
napi_valuetype valuetype;
961+
napi_typeof(env, value, &valuetype);
962+
963+
if (valuetype == napi_null || valuetype == napi_undefined) {
964+
*(char**)result = nullptr;
965+
*shouldFree = false;
966+
*shouldFreeAny = false;
967+
return;
968+
}
969+
970+
if (valuetype == napi_object) {
971+
if (Pointer::isInstance(env, value)) {
972+
Pointer* ptr = Pointer::unwrap(env, value);
973+
*(char**)result = (char*)ptr->data;
974+
*shouldFree = false;
975+
*shouldFreeAny = false;
976+
return;
977+
}
978+
979+
if (Reference::isInstance(env, value)) {
980+
Reference* ref = Reference::unwrap(env, value);
981+
*(char**)result = (char*)ref->data;
982+
*shouldFree = false;
983+
*shouldFreeAny = false;
984+
return;
985+
}
986+
987+
*(char**)result = nullptr;
988+
*shouldFree = false;
989+
*shouldFreeAny = false;
990+
return;
991+
}
992+
960993
char** res = (char**)result;
961994

962995
*res = nullptr;

0 commit comments

Comments
 (0)