Skip to content

Commit 21d2f11

Browse files
committed
Fix compilation flags and add new features
1 parent 983ed38 commit 21d2f11

File tree

11 files changed

+87
-38
lines changed

11 files changed

+87
-38
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(ObjCBridge)
88
set(NAME ObjCBridge)
99
set(VERSION 0.1.0)
1010

11-
set(CMAKE_C_FLAGS "-fobjective-c")
11+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fobjective-c")
1212
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-shorten-64-to-32")
1313
set(CMAKE_CXX_FLAGS_DEBUG "-g")
1414
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

examples/class.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,12 @@ class MyClass extends NSObject {
1717
const myClass = MyClass.new();
1818
console.log(myClass);
1919

20+
console.log(NSObject.class() === NSObject, MyClass.class() === MyClass);
21+
2022
MyClass.prototype.init = function () {
21-
console.log("MyClass.prototype.init()", this);
23+
console.log("MyClass.init() (patched)", this);
2224
return this;
2325
};
2426

2527
const myClass2 = MyClass.new();
2628
console.log(myClass2);
27-
28-
// deno-lint-ignore ban-ts-comment
29-
// @ts-expect-error
30-
MyClass.prototype.init = 0;
31-
32-
try {
33-
MyClass.new();
34-
} catch (e) {
35-
console.log("Expected Error:", e);
36-
}

include/MethodCif.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MethodCif {
1616
unsigned int argc;
1717
size_t frameLength;
1818
size_t rvalueLength;
19+
bool isVariadic = false;
1920

2021
void *rvalue;
2122
void **avalues;

lib/inline_functions.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export {};
22

33
declare global {
4-
function CGMakePoint(x: number, y: number): { x: number; y: number };
5-
let NSMakePoint: typeof CGMakePoint;
4+
function CGPointMake(x: number, y: number): { x: number; y: number };
5+
let NSMakePoint: typeof CGPointMake;
66

7-
function CGMakeSize(
7+
function CGSizeMake(
88
width: number,
99
height: number,
1010
): { width: number; height: number };
11-
let NSMakeSize: typeof CGMakeSize;
11+
let NSMakeSize: typeof CGSizeMake;
1212

13-
function CGMakeRect(
13+
function CGRectMake(
1414
x: number,
1515
y: number,
1616
width: number,
@@ -19,7 +19,7 @@ declare global {
1919
origin: { x: number; y: number };
2020
size: { width: number; height: number };
2121
};
22-
let NSMakeRect: typeof CGMakeRect;
22+
let NSMakeRect: typeof CGRectMake;
2323

2424
function UIMakeEdgeInsets(
2525
top: number,

src/Closure.mm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ void JSIMP(ffi_cif *cif, void *ret, void *args[], void *data) {
4949

5050
napi_value argv[cif->nargs - 2];
5151
for (int i = 2; i < cif->nargs; i++) {
52-
argv[i - 2] = closure->argTypes[i]->toJS(env, args[i], kBlockParam);
52+
argv[i - 2] = closure->argTypes[i]->toJS(env, args[i], 0);
5353
}
5454

5555
// Clear any pending exceptions before calling the function.
5656
napi_get_and_clear_last_exception(env, &result);
5757

58-
auto pool = objc_autoreleasePoolPush();
5958
napi_status status =
6059
napi_call_function(env, thisArg, func, cif->nargs - 2, argv, &result);
61-
objc_autoreleasePoolPop(pool);
6260

6361
bool shouldFree;
6462
closure->returnType->toNative(env, result, ret, &shouldFree, &shouldFree);
@@ -100,16 +98,14 @@ void callJSBlockFromMainThread(napi_env env, napi_value js_cb, void *context,
10098

10199
napi_value argv[ctx->cif->nargs - 1];
102100
for (int i = 0; i < ctx->cif->nargs - 1; i++) {
103-
argv[i] = closure->argTypes[i]->toJS(env, ctx->args[i + 1], kBlockParam);
101+
argv[i] = closure->argTypes[i]->toJS(env, ctx->args[i + 1], 0);
104102
}
105103

106104
// Clear any pending exceptions before calling the function.
107105
napi_get_and_clear_last_exception(env, &result);
108106

109-
auto pool = objc_autoreleasePoolPush();
110107
napi_status status = napi_call_function(env, thisArg, func,
111108
ctx->cif->nargs - 1, argv, &result);
112-
objc_autoreleasePoolPop(pool);
113109

114110
bool shouldFree;
115111
closure->returnType->toNative(env, result, ctx->ret, &shouldFree,

src/CustomClass.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ void addProtocol(napi_env env, ObjCBridgeData *bridgeData, Class cls,
145145
napi_get_named_property(env, constructor, "name", &className);
146146
napi_get_value_string_utf8(env, className, name_buf, 512, nullptr);
147147
std::string name = name_buf;
148+
name += "_";
149+
name += std::to_string(rand());
148150

149151
Class cls = objc_allocateClassPair(superClassNative, name.c_str(), 0);
150152

src/InlineFunctions.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
static const char *inlineFunctionsSource = R"(
77
8-
globalThis.CGMakePoint = globalThis.NSMakePoint = function CGMakePoint(x, y) {
8+
globalThis.CGPointMake = globalThis.NSMakePoint = function CGMakePoint(x, y) {
99
return { x, y };
1010
};
1111
12-
globalThis.CGMakeSize = globalThis.NSMakeSize = function CGMakeSize(width, height) {
12+
globalThis.CGSizeMake = globalThis.NSMakeSize = function CGMakeSize(width, height) {
1313
return { width, height };
1414
};
1515
16-
globalThis.CGMakeRect = globalThis.NSMakeRect = function CGMakeRect(x, y, width, height) {
16+
globalThis.CGRectMake = globalThis.NSMakeRect = function CGMakeRect(x, y, width, height) {
1717
return { origin: { x, y }, size: { width, height } };
1818
};
1919

src/MethodCif.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
MDSectionOffset offset, bool isMethod) {
9494
auto returnTypeKind = reader->getTypeKind(offset);
9595
bool next = (returnTypeKind & mdTypeFlagNext) != 0;
96+
isVariadic = (returnTypeKind & mdTypeFlagVariadic) != 0;
9697

9798
returnType = TypeConv::Make(env, reader, &offset);
9899

src/NativeCall.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ inline void objcNativeCall(napi_env env, napi_value jsThis, MethodCif *cif,
5858
id self, void **avalues, void *rvalue) {
5959
// TODO: This seems bad for performance. Is there a better way?
6060
bool supercall = false;
61-
// napi_has_named_property(env, jsThis, "__objc_msgSendSuper__", &supercall);
61+
napi_has_named_property(env, jsThis, "__objc_msgSendSuper__", &supercall);
6262

6363
#if defined(__x86_64__)
6464
bool isStret = cif->returnType->type->size > 16 &&
@@ -132,6 +132,8 @@ inline void objcNativeCall(napi_env env, napi_value jsThis, MethodCif *cif,
132132
}
133133
}
134134

135+
// NSLog(@"[call] %s", sel_getName(method->selector));
136+
135137
objcNativeCall(env, jsThis, cif, self, avalues, rvalue);
136138

137139
for (unsigned int i = 0; i < cif->argc; i++) {

src/Object.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ObjCBridge.h"
2+
#include "node_api_util.h"
23

34
#import <Foundation/Foundation.h>
45

@@ -180,16 +181,29 @@ napi_value findConstructorForObject(napi_env env, ObjCBridgeData *bridgeData,
180181
return value;
181182
}
182183

184+
// It was collected, but not unregistered yet.
183185
unregisterObject(obj);
184186
}
185187

188+
auto findClass = classesByPointer.find(obj);
189+
if (findClass != classesByPointer.end()) {
190+
return get_ref_value(env, findClass->second->constructor);
191+
}
192+
186193
auto cls = object_getClass(obj);
187194

188195
auto mdFindByPointer = mdClassesByPointer.find(cls);
189196
if (mdFindByPointer != mdClassesByPointer.end()) {
190197
classOffset = mdFindByPointer->second;
191198
}
192199

200+
auto findByPointer = classesByPointer.find(cls);
201+
if (findByPointer != classesByPointer.end()) {
202+
return getObject(env, obj,
203+
get_ref_value(env, findByPointer->second->constructor),
204+
ownership);
205+
}
206+
193207
napi_value constructor = nullptr;
194208
if (classOffset != 0) {
195209
auto bridgedCls = getClass(env, classOffset);
@@ -205,7 +219,7 @@ napi_value findConstructorForObject(napi_env env, ObjCBridgeData *bridgeData,
205219
if (proto == nullptr) {
206220
return nullptr;
207221
}
208-
222+
209223
constructor = get_ref_value(env, proto->constructor);
210224
} else {
211225
constructor = findConstructorForObject(env, this, obj, cls);

0 commit comments

Comments
 (0)