11#include " Block.h"
2+ #import < Foundation/Foundation.h>
3+ #include " js_native_api_types.h"
24#include " Interop.h"
35#include " ObjCBridge.h"
46#include " js_native_api.h"
57#include " node_api_util.h"
68#include " objc/runtime.h"
7- #import < Foundation/Foundation.h>
89
910struct Block_descriptor_1 {
10- unsigned long int reserved; // NULL
11- unsigned long int size; // sizeof(struct Block_literal_1)
11+ unsigned long int reserved; // NULL
12+ unsigned long int size; // sizeof(struct Block_literal_1)
1213 // optional helper functions
13- void (*copy_helper)(void * dst, void * src); // IFF (1<<25)
14- void (*dispose_helper)(void * src); // IFF (1<<25)
14+ void (*copy_helper)(void * dst, void * src); // IFF (1<<25)
15+ void (*dispose_helper)(void * src); // IFF (1<<25)
1516 // required ABI.2010.3.16
16- const char * signature; // IFF (1<<30)
17+ const char * signature; // IFF (1<<30)
1718};
1819
1920struct Block_literal_1 {
20- void * isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
21+ void * isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
2122 int flags;
2223 int reserved;
23- void * invoke;
24- Block_descriptor_1 * descriptor;
24+ void * invoke;
25+ Block_descriptor_1* descriptor;
2526 // imported variables
26- objc_bridge::Closure * closure;
27+ objc_bridge::Closure* closure;
2728};
2829
29- void block_copy (void * dest, void * src) {}
30- void block_release (void * src) {}
30+ void block_copy (void * dest, void * src) {}
31+ void block_release (void * src) {}
3132
32- void block_finalize (napi_env env, void * data, void * hint) {
33- auto block = (Block_literal_1 *)data;
33+ void block_finalize (napi_env env, void * data, void * hint) {
34+ auto block = (Block_literal_1*)data;
3435 delete block->closure ;
3536 delete block;
3637}
3738
3839namespace objc_bridge {
3940
40- void * stackBlockISA = nullptr ;
41+ void * stackBlockISA = nullptr ;
4142
42- id registerBlock (napi_env env, Closure * closure, napi_value callback) {
43+ id registerBlock (napi_env env, Closure* closure, napi_value callback) {
4344 auto block = new Block_literal_1 ();
4445 if (stackBlockISA == nullptr ) {
4546 stackBlockISA = dlsym (RTLD_DEFAULT, " _NSConcreteStackBlock" );
@@ -59,22 +60,24 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
5960
6061 napi_remove_wrap (env, callback, nullptr );
6162 napi_ref ref = nullptr ;
62- napi_wrap (env, callback, block, block_finalize, nullptr , &ref);
63- if (ref == nullptr ) {
63+ // TODO: fix memory management of objc blocks here
64+ // napi_wrap(env, callback, block, block_finalize, nullptr, &ref);
65+ // if (ref == nullptr) {
6466 // Deno doesn't handle napi_wrap properly.
65- ref = make_ref (env, callback, 0 );
66- }
67+ ref = make_ref (env, callback, 1 );
68+ // } else {
69+ // uint32_t refCount;
70+ // napi_reference_ref(env, ref, &refCount);
71+ // }
6772 closure->func = ref;
6873
6974 auto bridgeState = ObjCBridgeState::InstanceData (env);
7075 if (napiSupportsThreadsafeFunctions (bridgeState->self_dl )) {
7176 napi_value workName;
7277 napi_create_string_utf8 (env, " Block" , NAPI_AUTO_LENGTH, &workName);
73- napi_create_threadsafe_function (
74- env, callback, nullptr , workName, 0 , 1 , nullptr , nullptr , closure,
75- Closure::callBlockFromMainThread, &closure->tsfn );
76- if (closure->tsfn )
77- napi_unref_threadsafe_function (env, closure->tsfn );
78+ napi_create_threadsafe_function (env, callback, nullptr , workName, 0 , 1 , nullptr , nullptr ,
79+ closure, Closure::callBlockFromMainThread, &closure->tsfn );
80+ if (closure->tsfn ) napi_unref_threadsafe_function (env, closure->tsfn );
7881 }
7982
8083 return (id )block;
@@ -98,14 +101,13 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
98101 return callback;
99102}
100103
101- napi_value FunctionPointer::wrap (napi_env env, void *function,
102- metagen::MDSectionOffset offset,
104+ napi_value FunctionPointer::wrap (napi_env env, void * function, metagen::MDSectionOffset offset,
103105 bool isBlock) {
104- FunctionPointer * ref = new FunctionPointer ();
106+ FunctionPointer* ref = new FunctionPointer ();
105107 ref->function = function;
106108 ref->offset = offset;
107109
108- ObjCBridgeState * bridgeState = ObjCBridgeState::InstanceData (env);
110+ ObjCBridgeState* bridgeState = ObjCBridgeState::InstanceData (env);
109111
110112 if (isBlock) {
111113 ref->cif = bridgeState->getBlockCif (env, offset);
@@ -114,36 +116,32 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
114116 }
115117
116118 napi_value result;
117- napi_create_function (
118- env, isBlock ? " objcBlockWrapper" : " cFunctionWrapper" , NAPI_AUTO_LENGTH,
119- isBlock ? jsCallAsBlock : jsCallAsCFunction, ref, &result);
119+ napi_create_function (env, isBlock ? " objcBlockWrapper" : " cFunctionWrapper" , NAPI_AUTO_LENGTH,
120+ isBlock ? jsCallAsBlock : jsCallAsCFunction, ref, &result);
120121
121122 napi_ref jsRef;
122- napi_add_finalizer (env, result, ref, FunctionPointer::finalize, nullptr ,
123- &jsRef);
123+ napi_add_finalizer (env, result, ref, FunctionPointer::finalize, nullptr , &jsRef);
124124
125125 return result;
126126}
127127
128- void FunctionPointer::finalize (napi_env env, void *finalize_data,
129- void *finalize_hint) {
130- auto ref = (FunctionPointer *)finalize_data;
128+ void FunctionPointer::finalize (napi_env env, void * finalize_data, void * finalize_hint) {
129+ auto ref = (FunctionPointer*)finalize_data;
131130 delete ref;
132131}
133132
134- napi_value FunctionPointer::jsCallAsCFunction (napi_env env,
135- napi_callback_info cbinfo) {
136- FunctionPointer *ref;
133+ napi_value FunctionPointer::jsCallAsCFunction (napi_env env, napi_callback_info cbinfo) {
134+ FunctionPointer* ref;
137135
138- napi_get_cb_info (env, cbinfo, nullptr , nullptr , nullptr , (void **)&ref);
136+ napi_get_cb_info (env, cbinfo, nullptr , nullptr , nullptr , (void **)&ref);
139137
140138 auto cif = ref->cif ;
141139
142140 size_t argc = cif->argc ;
143141 napi_get_cb_info (env, cbinfo, &argc, cif->argv , nullptr , nullptr );
144142
145- void * avalues[cif->argc];
146- void * rvalue = cif->rvalue ;
143+ void * avalues[cif->argc];
144+ void * rvalue = cif->rvalue ;
147145
148146 bool shouldFreeAny = false ;
149147 bool shouldFree[cif->argc];
@@ -152,8 +150,7 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
152150 for (unsigned int i = 0 ; i < cif->argc ; i++) {
153151 shouldFree[i] = false ;
154152 avalues[i] = cif->avalues [i];
155- cif->argTypes [i]->toNative (env, cif->argv [i], avalues[i], &shouldFree[i],
156- &shouldFreeAny);
153+ cif->argTypes [i]->toNative (env, cif->argv [i], avalues[i], &shouldFree[i], &shouldFreeAny);
157154 }
158155 }
159156
@@ -162,28 +159,27 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
162159 if (shouldFreeAny) {
163160 for (unsigned int i = 0 ; i < cif->argc ; i++) {
164161 if (shouldFree[i]) {
165- cif->argTypes [i]->free (env, *((void **)avalues[i]));
162+ cif->argTypes [i]->free (env, *((void **)avalues[i]));
166163 }
167164 }
168165 }
169166
170167 return cif->returnType ->toJS (env, rvalue);
171168}
172169
173- napi_value FunctionPointer::jsCallAsBlock (napi_env env,
174- napi_callback_info cbinfo) {
175- FunctionPointer *ref;
170+ napi_value FunctionPointer::jsCallAsBlock (napi_env env, napi_callback_info cbinfo) {
171+ FunctionPointer* ref;
176172
177- napi_get_cb_info (env, cbinfo, nullptr , nullptr , nullptr , (void **)&ref);
173+ napi_get_cb_info (env, cbinfo, nullptr , nullptr , nullptr , (void **)&ref);
178174
179- Block_literal_1 * block = (Block_literal_1 *)ref->function ;
175+ Block_literal_1* block = (Block_literal_1*)ref->function ;
180176 auto cif = ref->cif ;
181177
182178 size_t argc = cif->argc ;
183179 napi_get_cb_info (env, cbinfo, &argc, cif->argv , nullptr , nullptr );
184180
185- void * avalues[cif->cif.nargs];
186- void * rvalue = cif->rvalue ;
181+ void * avalues[cif->cif.nargs];
182+ void * rvalue = cif->rvalue ;
187183
188184 bool shouldFreeAny = false ;
189185 bool shouldFree[cif->argc];
@@ -194,8 +190,7 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
194190 for (unsigned int i = 0 ; i < cif->argc ; i++) {
195191 shouldFree[i] = false ;
196192 avalues[i + 1 ] = cif->avalues [i];
197- cif->argTypes [i]->toNative (env, cif->argv [i], avalues[i + 1 ],
198- &shouldFree[i], &shouldFreeAny);
193+ cif->argTypes [i]->toNative (env, cif->argv [i], avalues[i + 1 ], &shouldFree[i], &shouldFreeAny);
199194 }
200195 }
201196
@@ -204,12 +199,12 @@ id registerBlock(napi_env env, Closure *closure, napi_value callback) {
204199 if (shouldFreeAny) {
205200 for (unsigned int i = 0 ; i < cif->argc ; i++) {
206201 if (shouldFree[i]) {
207- cif->argTypes [i]->free (env, *((void **)avalues[i + 1 ]));
202+ cif->argTypes [i]->free (env, *((void **)avalues[i + 1 ]));
208203 }
209204 }
210205 }
211206
212207 return cif->returnType ->toJS (env, rvalue);
213208}
214209
215- } // namespace objc_bridge
210+ } // namespace objc_bridge
0 commit comments