@@ -116,40 +116,40 @@ JS_METHOD(releaseProgram) { NAPI_ENV;
116116 RET_NUM (CL_SUCCESS);
117117}
118118
119- class ProgramWorker : public Napi ::AsyncWorker {
119+ class NotifyHelper {
120120public:
121- ProgramWorker (Napi::Function callback, Napi::Object userData, Napi::Object wrapper):
122- Napi::AsyncWorker (callback, " CL::ProgramWorker " ) {
123- _refProgram. Reset (wrapper, 1 );
124- _refData. Reset (userData, 1 );
121+ NotifyHelper (Napi::Function callback, Napi::Value userData) {
122+ _ref. Reset ( Napi::Object::New (callback. Env ()), 1 );
123+ _ref. Set ( " cb " , callback );
124+ _ref. Set ( " data " , userData );
125125 }
126126
127- ~ProgramWorker () {}
128-
129- // Executed inside the worker-thread.
130- void Execute () {}
127+ ~NotifyHelper () {
128+ Napi::Env env = _ref.Env ();
129+ _ref.Set (" cb" , env.Undefined ());
130+ _ref.Set (" data" , env.Undefined ());
131+ _ref.Reset ();
132+ }
131133
132- // Executed when the async work is complete
133- // this function will be run inside the main event loop
134- void OnOK () {
135- Napi::Env env = Env ();
136- NAPI_HS;
137- Callback ().Call ({
138- _refProgram.Value (), // program
139- _refData.Value () // userdata
140- });
134+ static void CL_CALLBACK callNotify (cl_program prog, void *ptr) {
135+ NotifyHelper *notifier = reinterpret_cast <NotifyHelper*>(ptr);
136+ notifier->notify (prog);
137+ delete notifier;
141138 }
142139
143140private:
144- Napi::ObjectReference _refProgram;
145- Napi::ObjectReference _refData;
141+ Napi::ObjectReference _ref;
142+
143+ void notify (cl_program prog) {
144+ Napi::Env env = _ref.Env (); NAPI_HS;
145+
146+ _ref.Get (" cb" ).As <Napi::Function>().Call ({
147+ Wrapper::fromRaw (env, prog),
148+ _ref.Get (" data" )
149+ });
150+ }
146151};
147152
148- void CL_CALLBACK notifyPCB (cl_program prog, void *user_data) {
149- ProgramWorker* asyncCB = reinterpret_cast <ProgramWorker*>(user_data);
150- asyncCB->Queue ();
151- }
152-
153153JS_METHOD (buildProgram) { NAPI_ENV;
154154 REQ_CL_ARG (0 , p, cl_program);
155155
@@ -172,19 +172,13 @@ JS_METHOD(buildProgram) { NAPI_ENV;
172172 // callback + userdata
173173 if (!IS_ARG_EMPTY (3 )) {
174174 REQ_FUN_ARG (3 , callback);
175- ProgramWorker* cb = new ProgramWorker (
176- callback,
177- info[4 ].As <Napi::Object>(),
178- info[0 ].As <Napi::Object>()
179- );
180-
181175 err = clBuildProgram (
182176 p,
183177 (cl_uint) cl_devices.size (),
184178 &cl_devices.front (),
185179 options.length () > 0 ? options.c_str () : nullptr ,
186- notifyPCB ,
187- cb
180+ NotifyHelper::callNotify ,
181+ new NotifyHelper (callback, info[ 4 ])
188182 );
189183 } else {
190184 err = clBuildProgram (
@@ -250,11 +244,6 @@ JS_METHOD(compileProgram) { NAPI_ENV;
250244
251245 if (!IS_ARG_EMPTY (5 )) {
252246 REQ_FUN_ARG (5 , callback);
253- ProgramWorker* cb = new ProgramWorker (
254- callback,
255- info[6 ].As <Napi::Object>(),
256- info[0 ].As <Napi::Object>()
257- );
258247 err = clCompileProgram (
259248 p,
260249 (cl_uint) cl_devices.size (),
@@ -263,8 +252,8 @@ JS_METHOD(compileProgram) { NAPI_ENV;
263252 (cl_uint) program_headers.size (),
264253 &program_headers.front (),
265254 &names.front (),
266- notifyPCB ,
267- cb
255+ NotifyHelper::callNotify ,
256+ new NotifyHelper (callback, info[ 6 ])
268257 );
269258 } else {
270259 err = clCompileProgram (
@@ -311,27 +300,21 @@ JS_METHOD(linkProgram) { NAPI_ENV;
311300 }
312301 }
313302
314- // OSX ISSUE: ret always equals to CL_SUCCESS
315303 cl_int ret = CL_SUCCESS;
316304
317305 cl_program prg;
318306
319307 if (!IS_ARG_EMPTY (4 )) {
320308 REQ_FUN_ARG (4 , callback);
321- ProgramWorker* cb = new ProgramWorker (
322- callback,
323- info[5 ].As <Napi::Object>(),
324- info[0 ].As <Napi::Object>()
325- );
326309 prg = clLinkProgram (
327310 ctx,
328311 (cl_uint) cl_devices.size (),
329312 &cl_devices.front (),
330313 options.length () > 0 ? options.c_str () : nullptr ,
331314 (cl_uint) cl_programs.size (),
332315 &cl_programs.front (),
333- notifyPCB ,
334- cb ,
316+ NotifyHelper::callNotify ,
317+ new NotifyHelper (callback, info[ 5 ]) ,
335318 &ret
336319 );
337320 } else {
0 commit comments