Skip to content

Commit 15f0b60

Browse files
committed
feat: handling pointer types wip
1 parent 0cd6c4d commit 15f0b60

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

NativeScript/ffi/ClassMember.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ inline id assertSelf(napi_env env, napi_value jsThis) {
318318
}
319319
}
320320

321-
// NSLog(@"objcNativeCall: %p, %@", self, NSStringFromSelector(method->methodOrGetter.selector));
321+
NSLog(@"objcNativeCall: %p, %@", self, NSStringFromSelector(method->methodOrGetter.selector));
322322

323323
if (!objcNativeCall(env, cif, self, avalues, rvalue)) {
324324
return nullptr;

NativeScript/ffi/Interop.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,17 @@ napi_value interop_bufferFromData(napi_env env, napi_callback_info info) {
540540
}
541541

542542
napi_value Pointer::create(napi_env env, void* data) {
543+
if (data == nullptr) {
544+
printf("Pointer::create called with null data\n");
545+
return nullptr;
546+
}
543547
ObjCBridgeState* bridgeState = ObjCBridgeState::InstanceData(env);
544548
bool isInstance = false;
545549
napi_value jsPointer = get_ref_value(env, bridgeState->pointerClass);
546550
napi_value result;
547551
napi_new_instance(env, jsPointer, 0, nullptr, &result);
548552
Pointer* ptr = Pointer::unwrap(env, result);
553+
549554
ptr->data = data;
550555
return result;
551556
}

NativeScript/ffi/TypeConv.mm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "node_api_util.h"
1414

1515
#import <Foundation/Foundation.h>
16+
#import <CoreFoundation/CoreFoundation.h>
1617
#include <objc/runtime.h>
1718
#include <stdbool.h>
1819
#include <memory>
@@ -531,6 +532,40 @@ void toNative(napi_env env, napi_value value, void* result, bool* shouldFree,
531532
return;
532533
}
533534

535+
case napi_string: {
536+
size_t len = 0;
537+
NAPI_GUARD(napi_get_value_string_utf8(env, value, nullptr, len, &len)) {
538+
NAPI_THROW_LAST_ERROR
539+
return;
540+
}
541+
542+
char* str = (char*)malloc(len + 1);
543+
544+
NAPI_GUARD(napi_get_value_string_utf8(env, value, str, len + 1, &len)) {
545+
NAPI_THROW_LAST_ERROR
546+
::free(str);
547+
return;
548+
}
549+
550+
str[len] = '\0';
551+
552+
// Check if we need to create a CFStringRef instead of a C string
553+
// if (pointeeType && pointeeType->kind == mdTypeNSStringObject) {
554+
// Create CFStringRef for CF/NS string pointers
555+
CFStringRef cfStr = CFStringCreateWithCString(kCFAllocatorDefault, str, kCFStringEncodingUTF8);
556+
::free(str); // Free the temporary C string
557+
*res = (void*)cfStr;
558+
*shouldFree = false; // CFStringRef is reference counted, don't use free()
559+
*shouldFreeAny = false;
560+
// } else {
561+
// // Default behavior: return C string
562+
// *res = (void*)str;
563+
// *shouldFree = true;
564+
// *shouldFreeAny = true;
565+
// }
566+
return;
567+
}
568+
534569
case napi_external: {
535570
NAPI_GUARD(napi_get_value_external(env, value, res)) {
536571
NAPI_THROW_LAST_ERROR
@@ -590,12 +625,26 @@ void toNative(napi_env env, napi_value value, void* result, bool* shouldFree,
590625
}
591626

592627
default:
628+
NSLog(@"value %d", type);
593629
napi_throw_error(env, nullptr, "Invalid pointer type");
594630
*res = nullptr;
595631
return;
596632
}
597633
}
598634

635+
void free(napi_env env, void* value) override {
636+
if (pointeeType && pointeeType->kind == mdTypeNSStringObject) {
637+
// CFStringRef needs CFRelease, not free()
638+
CFStringRef cfStr = (CFStringRef)value;
639+
if (cfStr != nullptr) {
640+
CFRelease(cfStr);
641+
}
642+
} else {
643+
// Default behavior for C strings
644+
::free(value);
645+
}
646+
}
647+
599648
void encode(std::string* encoding) override { *encoding += "^v"; }
600649
};
601650

@@ -941,6 +990,20 @@ void toNative(napi_env env, napi_value value, void* result, bool* shouldFree,
941990
status = napi_unwrap(env, value, (void**)res);
942991

943992
if (status != napi_ok) {
993+
// Check if this is a Pointer instance and use its underlying data
994+
if (Pointer::isInstance(env, value)) {
995+
Pointer* ptr = Pointer::unwrap(env, value);
996+
*res = (id)ptr->data;
997+
return;
998+
}
999+
1000+
// Check if this is a Reference instance and use its underlying data
1001+
if (Reference::isInstance(env, value)) {
1002+
Reference* ref = Reference::unwrap(env, value);
1003+
*res = (id)ref->data;
1004+
return;
1005+
}
1006+
9441007
bool isArray = false;
9451008
napi_is_array(env, value, &isArray);
9461009
if (isArray) {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Getting Started
22

3+
**Prerequisites**:
4+
- Download v8 static libraries from here:
5+
- Create a `Frameworks` folder at the root. Unzip and move the `v8_ios` folder inside so they are at `Frameworks/v8_ios`.
6+
37
To start diving into the v8 iOS runtime make sure you have Xcode and [Homebrew](https://brew.sh/) installed, and then run the following
48
```bash
59
# Install CMake

0 commit comments

Comments
 (0)