Skip to content

Commit 289fde9

Browse files
committed
add support for inout id params
1 parent 4ff7617 commit 289fde9

7 files changed

Lines changed: 183 additions & 1 deletion

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ set(LIB_SOURCE_FILES
9797
src/type_conv.mm
9898
src/util.mm
9999
src/struct.mm
100+
src/object_ref.mm
100101
segappend/src/segappend.cc
101102
)
102103

‎deno.json‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
},
1919

2020
"fmt": {
21-
"exclude": ["build", "libffi", "node_modules", "metadata", ".vscode"]
21+
"exclude": [
22+
"build",
23+
"libffi",
24+
"node_modules",
25+
"metadata",
26+
".vscode",
27+
"segappend"
28+
]
2229
},
2330

2431
"imports": {

‎examples/foundation.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ console.log(device);
77

88
const queue = device.newCommandQueue();
99
console.log(queue);
10+
11+
const ref = new ObjectRef();
12+
ref.value = device;
13+
console.log(ref);

‎include/object_ref.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef OBJECT_REF_H
2+
#define OBJECT_REF_H
3+
4+
#include "js_native_api.h"
5+
6+
namespace objc_bridge {
7+
8+
napi_value defineObjectRefClass(napi_env env);
9+
10+
} // namespace objc_bridge
11+
12+
#endif /* OBJECT_REF_H */

‎src/bridge.mm‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "native_call.h"
99
#include "node_api_util.h"
1010
#include "objc_bridge_data.h"
11+
#include "object_ref.h"
1112
#include "struct.h"
1213
#include "type_conv.h"
1314
#import <Foundation/Foundation.h>
@@ -192,6 +193,18 @@ void finalize_bridge_data(napi_env env, void *data, void *hint) {
192193

193194
napi_define_properties(env, global, 1, &objc_object);
194195

196+
napi_property_descriptor objectRefClass = {
197+
.utf8name = "ObjectRef",
198+
.attributes = napi_enumerable,
199+
.getter = nullptr,
200+
.setter = nullptr,
201+
.method = nullptr,
202+
.data = nullptr,
203+
.value = defineObjectRefClass(env),
204+
};
205+
206+
napi_define_properties(env, global, 1, &objectRefClass);
207+
195208
MDSectionOffset offset = bridgeData->metadata->structsOffset;
196209
while (offset < bridgeData->metadata->size) {
197210
// Sometimes there is padding after file ends.

‎src/object_ref.mm‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "object_ref.h"
2+
#include "js_native_api.h"
3+
#include "node_api_util.h"
4+
#include "type_conv.h"
5+
#include "util.h"
6+
7+
#import <Foundation/Foundation.h>
8+
9+
namespace objc_bridge {
10+
11+
void ObjectRef_finalize(napi_env env, void *data, void *hint) { free(data); }
12+
13+
NAPI_FUNCTION(ObjectRef_constructor) {
14+
napi_value jsThis, arg;
15+
size_t argc = 1;
16+
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr);
17+
18+
auto data = malloc(sizeof(id));
19+
napi_wrap(env, jsThis, data, ObjectRef_finalize, nullptr, nullptr);
20+
21+
napi_valuetype argType;
22+
napi_typeof(env, arg, &argType);
23+
24+
if (argType != napi_undefined && argType != napi_null) {
25+
const char *argenc = "@";
26+
auto conv = TypeConv::Make(env, &argenc);
27+
bool shouldFree;
28+
conv->toNative(env, arg, data, &shouldFree, &shouldFree);
29+
} else {
30+
*(id *)data = nil;
31+
}
32+
33+
return jsThis;
34+
}
35+
36+
NAPI_FUNCTION(ObjectRef_unwrap) {
37+
napi_value jsThis;
38+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
39+
40+
void *data;
41+
napi_unwrap(env, jsThis, &data);
42+
43+
id obj = *(id *)data;
44+
if (obj == nil) {
45+
napi_throw_error(env, nullptr, "ObjectRef.unwrap returned nil");
46+
return nullptr;
47+
}
48+
49+
const char *argenc = "@";
50+
auto conv = TypeConv::Make(env, &argenc);
51+
return conv->toJS(env, &obj);
52+
}
53+
54+
NAPI_FUNCTION(ObjectRef_get_value) {
55+
napi_value jsThis;
56+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
57+
58+
void *data;
59+
napi_unwrap(env, jsThis, &data);
60+
61+
id obj = *(id *)data;
62+
const char *argenc = "@";
63+
auto conv = TypeConv::Make(env, &argenc);
64+
return conv->toJS(env, &obj);
65+
}
66+
67+
NAPI_FUNCTION(ObjectRef_set_value) {
68+
napi_value jsThis, arg;
69+
size_t argc = 1;
70+
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr);
71+
72+
void *data;
73+
napi_unwrap(env, jsThis, &data);
74+
75+
const char *argenc = "@";
76+
auto conv = TypeConv::Make(env, &argenc);
77+
bool shouldFree;
78+
conv->toNative(env, arg, data, &shouldFree, &shouldFree);
79+
80+
return nullptr;
81+
}
82+
83+
NAPI_FUNCTION(ObjectRef_customInspect) {
84+
napi_value jsThis;
85+
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
86+
87+
void *data;
88+
napi_unwrap(env, jsThis, &data);
89+
90+
id obj = *(id *)data;
91+
92+
std::string inspect = "ObjectRef(";
93+
if (obj == nil) {
94+
inspect += "nil";
95+
} else {
96+
inspect += [[obj description] UTF8String];
97+
}
98+
inspect += ")";
99+
100+
napi_value result;
101+
napi_create_string_utf8(env, inspect.c_str(), NAPI_AUTO_LENGTH, &result);
102+
return result;
103+
}
104+
105+
napi_value defineObjectRefClass(napi_env env) {
106+
napi_value result;
107+
const napi_property_descriptor properties[] = {
108+
{
109+
.utf8name = "unwrap",
110+
.name = nullptr,
111+
.method = JS_ObjectRef_unwrap,
112+
.getter = nullptr,
113+
.setter = nullptr,
114+
.value = nullptr,
115+
.attributes = napi_default,
116+
.data = nullptr,
117+
},
118+
{
119+
.utf8name = "value",
120+
.name = nullptr,
121+
.method = nullptr,
122+
.getter = JS_ObjectRef_get_value,
123+
.setter = JS_ObjectRef_set_value,
124+
.value = nullptr,
125+
.attributes = napi_default,
126+
.data = nullptr,
127+
},
128+
{
129+
.utf8name = nullptr,
130+
.name = jsSymbolFor(env, "nodejs.util.inspect.custom"),
131+
.method = JS_ObjectRef_customInspect,
132+
.getter = nullptr,
133+
.setter = nullptr,
134+
.value = nullptr,
135+
.attributes = napi_default,
136+
.data = nullptr,
137+
}};
138+
napi_define_class(env, "ObjectRef", NAPI_AUTO_LENGTH,
139+
JS_ObjectRef_constructor, nullptr, 3, properties, &result);
140+
return result;
141+
}
142+
143+
} // namespace objc_bridge

‎src/type_conv.mm‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ void toNative(napi_env env, napi_value value, void *result, bool *shouldFree,
423423
case napi_undefined:
424424
*res = nullptr;
425425
return;
426+
426427
case napi_bigint: {
427428
uint64_t val = 0;
428429
bool lossless = false;
@@ -434,6 +435,7 @@ void toNative(napi_env env, napi_value value, void *result, bool *shouldFree,
434435
*res = (void *)val;
435436
return;
436437
}
438+
437439
case napi_external: {
438440
NAPI_GUARD(napi_get_value_external(env, value, res)) {
439441
NAPI_THROW_LAST_ERROR

0 commit comments

Comments
 (0)