k2hash 1.1.33 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/k2h_queue.cc CHANGED
@@ -22,7 +22,6 @@
22
22
  #include "k2h_queue.h"
23
23
  #include "k2h_queue_async.h"
24
24
 
25
- using namespace v8;
26
25
  using namespace std;
27
26
 
28
27
  //---------------------------------------------------------
@@ -44,37 +43,60 @@ const char* stc_k2hq_emitters[] = {
44
43
  };
45
44
 
46
45
  //---------------------------------------------------------
47
- // Utility macros
46
+ // Utility (using StackEmitCB Class)
48
47
  //---------------------------------------------------------
49
- #define SetK2hqEmitterCallback(info, pos, pemitter) \
50
- { \
51
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This()); \
52
- if(info.Length() <= pos){ \
53
- Nan::ThrowSyntaxError("No callback is specified."); \
54
- return; \
55
- } \
56
- Nan::Callback* cb = new Nan::Callback(); \
57
- cb->SetFunction(info[pos].As<v8::Function>()); \
58
- bool result = obj->_cbs.Set(pemitter, cb); \
59
- info.GetReturnValue().Set(Nan::New(result)); \
60
- }
48
+ static Napi::Value SetK2hqEmitterCallback(const Napi::CallbackInfo& info, size_t pos, const char* pemitter)
49
+ {
50
+ Napi::Env env = info.Env();
61
51
 
62
- #define UnsetK2hqEmitterCallback(info, pemitter) \
63
- { \
64
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This()); \
65
- bool result = obj->_cbs.Unset(pemitter); \
66
- info.GetReturnValue().Set(Nan::New(result)); \
67
- }
52
+ // Unwrap
53
+ if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
54
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
55
+ return env.Undefined();
56
+ }
57
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
58
+
59
+ // check parameter
60
+ if(info.Length() <= pos){
61
+ Napi::TypeError::New(env, "No callback is specified.").ThrowAsJavaScriptException();
62
+ return env.Undefined();
63
+ }
64
+ if(!info[pos].IsFunction()){
65
+ Napi::TypeError::New(env, "The parameter is not callback function.").ThrowAsJavaScriptException();
66
+ return env.Undefined();
67
+ }
68
+ Napi::Function cb = info[pos].As<Napi::Function>();
69
+
70
+ // set
71
+ bool result = obj->_cbs.Set(std::string(pemitter), cb);
72
+ return Napi::Boolean::New(env, result);
73
+ }
74
+
75
+ static Napi::Value UnsetK2hqEmitterCallback(const Napi::CallbackInfo& info, const char* pemitter)
76
+ {
77
+ Napi::Env env = info.Env();
78
+
79
+ // Unwrap
80
+ if(!info.This().IsObject() || !info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
81
+ Napi::TypeError::New(env, "Invalid this object").ThrowAsJavaScriptException();
82
+ return env.Undefined();
83
+ }
84
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
85
+
86
+ // unset
87
+ bool result = obj->_cbs.Unset(std::string(pemitter));
88
+ return Napi::Boolean::New(env, result);
89
+ }
68
90
 
69
91
  //---------------------------------------------------------
70
92
  // K2hQueue Class
71
93
  //---------------------------------------------------------
72
- Nan::Persistent<Function> K2hQueue::constructor;
94
+ Napi::FunctionReference K2hQueue::constructor;
73
95
 
74
96
  //---------------------------------------------------------
75
97
  // K2hQueue Methods
76
98
  //---------------------------------------------------------
77
- K2hQueue::K2hQueue() : _k2hqueue(), _cbs()
99
+ K2hQueue::K2hQueue(const Napi::CallbackInfo& info) : Napi::ObjectWrap<K2hQueue>(info), _cbs(), _k2hqueue()
78
100
  {
79
101
  }
80
102
 
@@ -82,75 +104,90 @@ K2hQueue::~K2hQueue()
82
104
  {
83
105
  }
84
106
 
85
- void K2hQueue::Init(void)
107
+ void K2hQueue::Init(Napi::Env env, Napi::Object exports)
86
108
  {
87
- // Prepare constructor template
88
- Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
89
- tpl->SetClassName(Nan::New("K2hQueue").ToLocalChecked());
90
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
91
-
92
- // Prototype for event emitter
93
- Nan::SetPrototypeMethod(tpl, "on", On);
94
- Nan::SetPrototypeMethod(tpl, "onPush", OnPush);
95
- Nan::SetPrototypeMethod(tpl, "onPop", OnPop);
96
- Nan::SetPrototypeMethod(tpl, "onCount", OnCount);
97
- Nan::SetPrototypeMethod(tpl, "onRead", OnRead);
98
- Nan::SetPrototypeMethod(tpl, "onRemove", OnRemove);
99
- Nan::SetPrototypeMethod(tpl, "off", Off);
100
- Nan::SetPrototypeMethod(tpl, "offPush", OffPush);
101
- Nan::SetPrototypeMethod(tpl, "offPop", OffPop);
102
- Nan::SetPrototypeMethod(tpl, "offCount", OffCount);
103
- Nan::SetPrototypeMethod(tpl, "offRead", OffRead);
104
- Nan::SetPrototypeMethod(tpl, "offRemove", OffRemove);
105
-
106
- // Prototype
107
- Nan::SetPrototypeMethod(tpl, "init", Init);
108
- Nan::SetPrototypeMethod(tpl, "push", Push);
109
- Nan::SetPrototypeMethod(tpl, "count", Count);
110
- Nan::SetPrototypeMethod(tpl, "isEmpty", IsEmpty);
111
- Nan::SetPrototypeMethod(tpl, "read", Read);
112
- Nan::SetPrototypeMethod(tpl, "pop", Pop);
113
- Nan::SetPrototypeMethod(tpl, "remove", Remove);
114
- Nan::SetPrototypeMethod(tpl, "dump", Dump);
115
-
116
- // Reset
117
- constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
109
+ Napi::Function funcs = DefineClass(env, "K2hQueue", {
110
+
111
+ // DefineClass normally handles the constructor internally. Therefore, there is no need
112
+ // to include a static wrapper New() in the class prototype, which works the same way as
113
+ // when using NAN.
114
+ // For reference, the following example shows how to declare New as a static method.
115
+ // (Registration is not normally required.)
116
+ //
117
+ // K2hQueue::InstanceMethod("new", &K2hQueue::New),
118
+
119
+ // Prototype for event emitter
120
+ K2hQueue::InstanceMethod("on", &K2hQueue::On),
121
+ K2hQueue::InstanceMethod("onPush", &K2hQueue::OnPush),
122
+ K2hQueue::InstanceMethod("onPop", &K2hQueue::OnPop),
123
+ K2hQueue::InstanceMethod("onCount", &K2hQueue::OnCount),
124
+ K2hQueue::InstanceMethod("onRead", &K2hQueue::OnRead),
125
+ K2hQueue::InstanceMethod("onRemove", &K2hQueue::OnRemove),
126
+ K2hQueue::InstanceMethod("off", &K2hQueue::Off),
127
+ K2hQueue::InstanceMethod("offPush", &K2hQueue::OffPush),
128
+ K2hQueue::InstanceMethod("offPop", &K2hQueue::OffPop),
129
+ K2hQueue::InstanceMethod("offCount", &K2hQueue::OffCount),
130
+ K2hQueue::InstanceMethod("offRead", &K2hQueue::OffRead),
131
+ K2hQueue::InstanceMethod("offRemove", &K2hQueue::OffRemove),
132
+
133
+ // Prototype
134
+ K2hQueue::InstanceMethod("init", &K2hQueue::Initialize),
135
+ K2hQueue::InstanceMethod("push", &K2hQueue::Push),
136
+ K2hQueue::InstanceMethod("count", &K2hQueue::Count),
137
+ K2hQueue::InstanceMethod("isEmpty", &K2hQueue::IsEmpty),
138
+ K2hQueue::InstanceMethod("read", &K2hQueue::Read),
139
+ K2hQueue::InstanceMethod("pop", &K2hQueue::Pop),
140
+ K2hQueue::InstanceMethod("remove", &K2hQueue::Remove),
141
+ K2hQueue::InstanceMethod("dump", &K2hQueue::Dump)
142
+ });
143
+
144
+ constructor = Napi::Persistent(funcs);
145
+ constructor.SuppressDestruct();
146
+
147
+ // [NOTE]
148
+ // do NOT do exports.Set("K2hQueue", func) here if InitAll will return createFn.
149
+ //
118
150
  }
119
151
 
120
- NAN_METHOD(K2hQueue::New)
152
+ Napi::Value K2hQueue::New(const Napi::CallbackInfo& info)
121
153
  {
122
- if(info.IsConstructCall()){
154
+ if(info.IsConstructCall()){
123
155
  // Invoked as constructor: new K2hQueue()
124
- K2hQueue* obj = new K2hQueue();
125
- obj->Wrap(info.This());
126
- info.GetReturnValue().Set(info.This());
127
- }else{
128
- // Invoked as plain function K2hQueue(), turn into construct call.
129
- const unsigned argc = 1;
130
- Local<Value> argv[argc] = {info[0]};
131
- Local<Function> cons = Nan::New<Function>(constructor);
132
- info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
133
- }
156
+ // DefineClass 経由のコンストラクタが呼ばれているはずなので、ここでは this を返すだけで良い
157
+ return info.This();
158
+ }else{
159
+ // Invoked as plain function K2hQueue(), turn into construct call.
160
+ if(0 < info.Length()){
161
+ return constructor.New({info[0]});
162
+ }else{
163
+ return constructor.New({});
164
+ }
165
+ }
134
166
  }
135
167
 
136
- NAN_METHOD(K2hQueue::NewInstance)
168
+ // NewInstance( no parameter )
169
+ Napi::Object K2hQueue::NewInstance(Napi::Env env)
137
170
  {
138
- const unsigned argc = 1;
139
- Local<Value> argv[argc] = {info[0]};
140
- Local<Function> cons = Nan::New<Function>(constructor);
141
- info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
171
+ Napi::EscapableHandleScope scope(env);
172
+ Napi::Object obj = constructor.New({}).As<Napi::Object>();
173
+ return scope.Escape(napi_value(obj)).ToObject();
142
174
  }
143
175
 
144
- Local<Object> K2hQueue::GetInstance(Nan::NAN_METHOD_ARGS_TYPE info)
176
+ // NewInstance( 1 parameter )
177
+ Napi::Object K2hQueue::NewInstance(Napi::Env env, const Napi::Value& arg)
145
178
  {
146
- Nan::EscapableHandleScope scope;
147
-
148
- const unsigned argc = 1;
149
- Local<Value> argv[argc] = {info[0]};
150
- Local<Function> cons = Nan::New<Function>(constructor);
151
- Local<Object> instance = Nan::NewInstance(cons, argc, argv).ToLocalChecked();
179
+ Napi::EscapableHandleScope scope(env);
180
+ Napi::Object obj = constructor.New({ arg }).As<Napi::Object>();
181
+ return scope.Escape(napi_value(obj)).ToObject();
182
+ }
152
183
 
153
- return scope.Escape(instance);
184
+ Napi::Object K2hQueue::GetInstance(const Napi::CallbackInfo& info)
185
+ {
186
+ if(0 < info.Length()){
187
+ return K2hQueue::constructor.New({info[0]});
188
+ }else{
189
+ return K2hQueue::constructor.New({});
190
+ }
154
191
  }
155
192
 
156
193
  /// \defgroup nodejs_methods
@@ -171,28 +208,36 @@ Local<Object> K2hQueue::GetInstance(Nan::NAN_METHOD_ARGS_TYPE info)
171
208
  * @return return true for success, false for failure
172
209
  */
173
210
 
174
- NAN_METHOD(K2hQueue::On)
211
+ Napi::Value K2hQueue::On(const Napi::CallbackInfo& info)
175
212
  {
213
+ Napi::Env env = info.Env();
214
+
215
+ // check
216
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
217
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
218
+ return env.Undefined();
219
+ }
176
220
  if(info.Length() < 1){
177
- Nan::ThrowSyntaxError("No handle emitter name is specified.");
178
- return;
221
+ Napi::TypeError::New(env, "No handle emitter name is specified.").ThrowAsJavaScriptException();
222
+ return env.Undefined();
179
223
  }else if(info.Length() < 2){
180
- Nan::ThrowSyntaxError("No callback is specified.");
181
- return;
224
+ Napi::TypeError::New(env, "No callback is specified.").ThrowAsJavaScriptException();
225
+ return env.Undefined();
182
226
  }
183
227
 
184
228
  // check emitter name
185
- Nan::Utf8String emitter(info[0]);
186
- const char* pemitter;
187
- if(NULL == (pemitter = GetNormalizationEmitter(*emitter, stc_k2hq_emitters))){
188
- string msg = "Unknown ";
189
- msg += *emitter;
190
- msg += " emitter";
191
- Nan::ThrowSyntaxError(msg.c_str());
192
- return;
229
+ std::string emitter = info[0].ToString().Utf8Value();
230
+ const char* pemitter = GetNormalizationEmitter(emitter.c_str(), stc_k2hq_emitters);
231
+ if(!pemitter){
232
+ std::string msg = "Unknown ";
233
+ msg += emitter;
234
+ msg += " emitter";
235
+ Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
236
+ return env.Undefined();
193
237
  }
238
+
194
239
  // add callback
195
- SetK2hqEmitterCallback(info, 1, pemitter);
240
+ return SetK2hqEmitterCallback(info, 1, pemitter);
196
241
  }
197
242
 
198
243
  /**
@@ -208,9 +253,9 @@ NAN_METHOD(K2hQueue::On)
208
253
  * @return return true for success, false for failure
209
254
  */
210
255
 
211
- NAN_METHOD(K2hQueue::OnPush)
256
+ Napi::Value K2hQueue::OnPush(const Napi::CallbackInfo& info)
212
257
  {
213
- SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
258
+ return SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
214
259
  }
215
260
 
216
261
  /**
@@ -226,9 +271,9 @@ NAN_METHOD(K2hQueue::OnPush)
226
271
  * @return return true for success, false for failure
227
272
  */
228
273
 
229
- NAN_METHOD(K2hQueue::OnPop)
274
+ Napi::Value K2hQueue::OnPop(const Napi::CallbackInfo& info)
230
275
  {
231
- SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
276
+ return SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
232
277
  }
233
278
 
234
279
  /**
@@ -244,9 +289,9 @@ NAN_METHOD(K2hQueue::OnPop)
244
289
  * @return return true for success, false for failure
245
290
  */
246
291
 
247
- NAN_METHOD(K2hQueue::OnCount)
292
+ Napi::Value K2hQueue::OnCount(const Napi::CallbackInfo& info)
248
293
  {
249
- SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
294
+ return SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
250
295
  }
251
296
 
252
297
  /**
@@ -262,9 +307,9 @@ NAN_METHOD(K2hQueue::OnCount)
262
307
  * @return return true for success, false for failure
263
308
  */
264
309
 
265
- NAN_METHOD(K2hQueue::OnRead)
310
+ Napi::Value K2hQueue::OnRead(const Napi::CallbackInfo& info)
266
311
  {
267
- SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
312
+ return SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
268
313
  }
269
314
 
270
315
  /**
@@ -280,9 +325,9 @@ NAN_METHOD(K2hQueue::OnRead)
280
325
  * @return return true for success, false for failure
281
326
  */
282
327
 
283
- NAN_METHOD(K2hQueue::OnRemove)
328
+ Napi::Value K2hQueue::OnRemove(const Napi::CallbackInfo& info)
284
329
  {
285
- SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
330
+ return SetK2hqEmitterCallback(info, 0, stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
286
331
  }
287
332
 
288
333
  /**
@@ -298,25 +343,27 @@ NAN_METHOD(K2hQueue::OnRemove)
298
343
  * @return return true for success, false for failure
299
344
  */
300
345
 
301
- NAN_METHOD(K2hQueue::Off)
346
+ Napi::Value K2hQueue::Off(const Napi::CallbackInfo& info)
302
347
  {
348
+ Napi::Env env = info.Env();
349
+
303
350
  if(info.Length() < 1){
304
- Nan::ThrowSyntaxError("No handle emitter name is specified.");
305
- return;
351
+ Napi::TypeError::New(env, "No handle emitter name is specified.").ThrowAsJavaScriptException();
352
+ return env.Undefined();
306
353
  }
307
354
 
308
355
  // check emitter name
309
- Nan::Utf8String emitter(info[0]);
310
- const char* pemitter;
311
- if(NULL == (pemitter = GetNormalizationEmitter(*emitter, stc_k2hq_emitters))){
312
- string msg = "Unknown ";
313
- msg += *emitter;
314
- msg += " emitter";
315
- Nan::ThrowSyntaxError(msg.c_str());
316
- return;
356
+ std::string emitter = info[0].ToString().Utf8Value();
357
+ const char* pemitter = GetNormalizationEmitter(emitter.c_str(), stc_k2hq_emitters);
358
+ if (nullptr == pemitter) {
359
+ std::string msg = "Unknown ";
360
+ msg += emitter;
361
+ msg += " emitter";
362
+ Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
363
+ return env.Undefined();
317
364
  }
318
365
  // unset callback
319
- UnsetK2hqEmitterCallback(info, pemitter);
366
+ return UnsetK2hqEmitterCallback(info, pemitter);
320
367
  }
321
368
 
322
369
  /**
@@ -329,9 +376,9 @@ NAN_METHOD(K2hQueue::Off)
329
376
  * @return return true for success, false for failure
330
377
  */
331
378
 
332
- NAN_METHOD(K2hQueue::OffPush)
379
+ Napi::Value K2hQueue::OffPush(const Napi::CallbackInfo& info)
333
380
  {
334
- UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
381
+ return UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
335
382
  }
336
383
 
337
384
  /**
@@ -344,9 +391,9 @@ NAN_METHOD(K2hQueue::OffPush)
344
391
  * @return return true for success, false for failure
345
392
  */
346
393
 
347
- NAN_METHOD(K2hQueue::OffPop)
394
+ Napi::Value K2hQueue::OffPop(const Napi::CallbackInfo& info)
348
395
  {
349
- UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
396
+ return UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
350
397
  }
351
398
 
352
399
  /**
@@ -359,9 +406,9 @@ NAN_METHOD(K2hQueue::OffPop)
359
406
  * @return return true for success, false for failure
360
407
  */
361
408
 
362
- NAN_METHOD(K2hQueue::OffCount)
409
+ Napi::Value K2hQueue::OffCount(const Napi::CallbackInfo& info)
363
410
  {
364
- UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
411
+ return UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
365
412
  }
366
413
 
367
414
  /**
@@ -374,9 +421,9 @@ NAN_METHOD(K2hQueue::OffCount)
374
421
  * @return return true for success, false for failure
375
422
  */
376
423
 
377
- NAN_METHOD(K2hQueue::OffRead)
424
+ Napi::Value K2hQueue::OffRead(const Napi::CallbackInfo& info)
378
425
  {
379
- UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
426
+ return UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
380
427
  }
381
428
 
382
429
  /**
@@ -389,9 +436,9 @@ NAN_METHOD(K2hQueue::OffRead)
389
436
  * @return return true for success, false for failure
390
437
  */
391
438
 
392
- NAN_METHOD(K2hQueue::OffRemove)
439
+ Napi::Value K2hQueue::OffRemove(const Napi::CallbackInfo& info)
393
440
  {
394
- UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
441
+ return UnsetK2hqEmitterCallback(info, stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
395
442
  }
396
443
 
397
444
  /**
@@ -412,25 +459,39 @@ NAN_METHOD(K2hQueue::OffRemove)
412
459
  * @return return true for success, false for failure
413
460
  */
414
461
 
415
- NAN_METHOD(K2hQueue::Init)
462
+ Napi::Value K2hQueue::Initialize(const Napi::CallbackInfo& info)
416
463
  {
417
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
464
+ Napi::Env env = info.Env();
418
465
 
466
+ // check
419
467
  if(info.Length() < 1){
420
- Nan::ThrowSyntaxError("No k2hash object is specified.");
421
- return;
468
+ Napi::TypeError::New(env, "No k2hash object is specified.").ThrowAsJavaScriptException();
469
+ return env.Undefined();
470
+ }
471
+
472
+ // Unwrap
473
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
474
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
475
+ return env.Undefined();
422
476
  }
423
- K2hNode* objNode = ObjectWrap::Unwrap<K2hNode>(info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked());
424
- bool is_fifo = (info.Length() < 2 ? true : Nan::To<bool>(info[1]).ToChecked());
477
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
478
+
479
+ // parse positional optional args
480
+ if(!info[0].IsObject()||!info[0].As<Napi::Object>().InstanceOf(K2hNode::constructor.Value())){
481
+ Napi::TypeError::New(env, "Invalid k2hash object").ThrowAsJavaScriptException();
482
+ return env.Undefined();
483
+ }
484
+ K2hNode* objNode = Napi::ObjectWrap<K2hNode>::Unwrap(info[0].As<Napi::Object>());
485
+
486
+ bool is_fifo = (info.Length() < 2 ? true : info[1].ToBoolean().Value());
425
487
  std::string prefix;
426
- if(3 <= info.Length()){
427
- Nan::Utf8String buf(info[2]);
428
- prefix = std::string(*buf);
488
+ if(2 < info.Length()){
489
+ prefix = info[2].ToString().Utf8Value();
429
490
  }
430
491
 
431
- info.GetReturnValue().Set(Nan::New(
432
- obj->_k2hqueue.Init(&objNode->_k2hshm, is_fifo, (prefix.empty() ? NULL : reinterpret_cast<const unsigned char*>(prefix.c_str())), (prefix.empty() ? 0UL : (prefix.length() + 1)))
433
- ));
492
+ // Execute
493
+ bool res = obj->_k2hqueue.Init(&objNode->_k2hshm, is_fifo, (prefix.empty() ? NULL : reinterpret_cast<const unsigned char*>(prefix.c_str())), (prefix.empty() ? 0UL : (prefix.length() + 1)));
494
+ return Napi::Boolean::New(env, res);
434
495
  }
435
496
 
436
497
  /**
@@ -455,69 +516,92 @@ NAN_METHOD(K2hQueue::Init)
455
516
  *
456
517
  */
457
518
 
458
- NAN_METHOD(K2hQueue::Push)
519
+ Napi::Value K2hQueue::Push(const Napi::CallbackInfo& info)
459
520
  {
521
+ Napi::Env env = info.Env();
522
+
460
523
  if(info.Length() < 1){
461
- Nan::ThrowSyntaxError("No data is specified.");
462
- return;
524
+ Napi::TypeError::New(env, "No data is specified.").ThrowAsJavaScriptException();
525
+ return env.Undefined();
463
526
  }
464
527
 
465
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
466
- std::string data;
467
- bool is_pass_set = false;
468
- std::string pass;
469
- time_t expire = 0;
470
- Nan::Callback* callback = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
528
+ // Unwrap
529
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
530
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
531
+ return env.Undefined();
532
+ }
533
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
534
+
535
+ std::string data;
536
+ bool is_pass_set = false;
537
+ std::string pass;
538
+ time_t expire = 0;
539
+
540
+ // initial callback comes from emitter map if set
541
+ Napi::Function maybeCallback;
542
+ bool hasCallback = false;
543
+ Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_PUSH]);
544
+ if(emitterCbRef){
545
+ maybeCallback = emitterCbRef->Value();
546
+ hasCallback = true;
547
+ }
471
548
 
472
- if(info[0]->IsNull()){
473
- Nan::ThrowSyntaxError("data is empty.");
474
- return;
549
+ // parse positional optional args
550
+ if(info[0].IsNull()){
551
+ Napi::TypeError::New(env, "data is empty.").ThrowAsJavaScriptException();
552
+ return env.Undefined();
475
553
  }else{
476
- Nan::Utf8String buf(info[0]);
477
- data = std::string(*buf);
554
+ data = info[0].ToString().Utf8Value();
478
555
  }
556
+
479
557
  if(1 < info.Length()){
480
- if(info[1]->IsFunction()){
558
+ if(info[1].IsFunction()){
481
559
  if(2 < info.Length()){
482
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
483
- return;
560
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
561
+ return env.Undefined();
484
562
  }
485
- callback = new Nan::Callback(info[1].As<v8::Function>());
486
- }else if(!info[1]->IsNull()){
487
- Nan::Utf8String buf(info[1]);
488
- pass = std::string(*buf);
489
- is_pass_set = true;
563
+ maybeCallback = info[1].As<Napi::Function>();
564
+ hasCallback = true;
565
+ }else if(!info[1].IsNull()){
566
+ pass = info[1].ToString().Utf8Value();
567
+ is_pass_set = true;
490
568
  }
491
569
  }
570
+
492
571
  if(2 < info.Length()){
493
- if(info[2]->IsFunction()){
572
+ if(info[2].IsFunction()){
494
573
  if(3 < info.Length()){
495
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
496
- return;
574
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
575
+ return env.Undefined();
497
576
  }
498
- callback = new Nan::Callback(info[2].As<v8::Function>());
499
- }else if(info[2]->IsInt32()){
500
- int nexpire = Nan::To<int32_t>(info[2]).ToChecked();
501
- expire = static_cast<time_t>(nexpire);
577
+ maybeCallback = info[2].As<Napi::Function>();
578
+ hasCallback = true;
579
+ }else if(info[2].IsNumber()){
580
+ int nexpire = info[2].ToNumber().Int32Value();
581
+ expire = static_cast<time_t>(nexpire);
502
582
  }else{
503
- Nan::ThrowSyntaxError("Expire parameter must be int32 value.");
504
- return;
583
+ Napi::TypeError::New(env, "Expire parameter must be int32 value.").ThrowAsJavaScriptException();
584
+ return env.Undefined();
505
585
  }
506
586
  }
587
+
507
588
  if(3 < info.Length()){
508
- if(4 < info.Length() || !info[3]->IsFunction()){
509
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
510
- return;
589
+ if(4 < info.Length() || !info[3].IsFunction()){
590
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
591
+ return env.Undefined();
511
592
  }
512
- callback = new Nan::Callback(info[3].As<v8::Function>());
593
+ maybeCallback = info[3].As<Napi::Function>();
594
+ hasCallback = true;
513
595
  }
514
596
 
515
- // work
516
- if(callback){
517
- Nan::AsyncQueueWorker(new K2hqPushWorker(callback, &(obj->_k2hqueue), data.c_str(), (is_pass_set ? pass.c_str() : NULL), (expire <= 0 ? NULL : &expire)));
518
- info.GetReturnValue().Set(Nan::True());
597
+ // Execute
598
+ if(hasCallback){
599
+ K2hqPushAsyncWorker* worker = new K2hqPushAsyncWorker(maybeCallback, &(obj->_k2hqueue), data.c_str(), (is_pass_set ? pass.c_str() : NULL), (expire <= 0 ? NULL : &expire));
600
+ worker->Queue();
601
+ return Napi::Boolean::New(env, true);
519
602
  }else{
520
- info.GetReturnValue().Set(Nan::New(obj->_k2hqueue.Push(reinterpret_cast<const unsigned char*>(data.c_str()), (data.length() + 1), NULL, (is_pass_set ? pass.c_str() : NULL), (expire <= 0 ? NULL : &expire))));
603
+ bool res = obj->_k2hqueue.Push(reinterpret_cast<const unsigned char*>(data.c_str()), (data.length() + 1), NULL, (is_pass_set ? pass.c_str() : NULL), (expire <= 0 ? NULL : &expire));
604
+ return Napi::Boolean::New(env, res);
521
605
  }
522
606
  }
523
607
 
@@ -541,26 +625,44 @@ NAN_METHOD(K2hQueue::Push)
541
625
  *
542
626
  */
543
627
 
544
- NAN_METHOD(K2hQueue::Count)
628
+ Napi::Value K2hQueue::Count(const Napi::CallbackInfo& info)
545
629
  {
546
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
547
- Nan::Callback* callback= obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
630
+ Napi::Env env = info.Env();
548
631
 
632
+ // Unwrap
633
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
634
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
635
+ return env.Undefined();
636
+ }
637
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
638
+
639
+ // initial callback comes from emitter map if set
640
+ Napi::Function maybeCallback;
641
+ bool hasCallback = false;
642
+ Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_COUNT]);
643
+ if(emitterCbRef){
644
+ maybeCallback = emitterCbRef->Value();
645
+ hasCallback = true;
646
+ }
647
+
648
+ // parse positional optional args
549
649
  if(0 < info.Length()){
550
- if(1 < info.Length() || !info[0]->IsFunction()){
551
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
552
- return;
650
+ if(1 < info.Length() || !info[0].IsFunction()){
651
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
652
+ return env.Undefined();
553
653
  }
554
- callback = new Nan::Callback(info[0].As<v8::Function>());
654
+ maybeCallback = info[0].As<Napi::Function>();
655
+ hasCallback = true;
555
656
  }
556
657
 
557
- // work
558
- if(callback){
559
- Nan::AsyncQueueWorker(new K2hqCountWorker(callback, &(obj->_k2hqueue)));
560
- info.GetReturnValue().Set(Nan::True());
658
+ // Execute
659
+ if(hasCallback){
660
+ K2hqCountAsyncWorker* worker = new K2hqCountAsyncWorker(maybeCallback, &(obj->_k2hqueue));
661
+ worker->Queue();
662
+ return Napi::Boolean::New(env, true);
561
663
  }else{
562
- //SetK2hDbgMode(K2HDBG_MSG);
563
- info.GetReturnValue().Set(Nan::New(obj->_k2hqueue.GetCount()));
664
+ uint64_t cnt = obj->_k2hqueue.GetCount();
665
+ return Napi::Number::New(env, static_cast<double>(cnt));
564
666
  }
565
667
  }
566
668
 
@@ -572,15 +674,20 @@ NAN_METHOD(K2hQueue::Count)
572
674
  * @return returns true if the queue is empty. false for not empty.
573
675
  */
574
676
 
575
- NAN_METHOD(K2hQueue::IsEmpty)
677
+ Napi::Value K2hQueue::IsEmpty(const Napi::CallbackInfo& info)
576
678
  {
577
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
679
+ Napi::Env env = info.Env();
578
680
 
579
- //SetK2hDbgMode(K2HDBG_MSG);
681
+ // Unwrap
682
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
683
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
684
+ return env.Undefined();
685
+ }
686
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
580
687
 
581
- info.GetReturnValue().Set(Nan::New(
582
- obj->_k2hqueue.IsEmpty()
583
- ));
688
+ // Execute
689
+ bool res = obj->_k2hqueue.IsEmpty();
690
+ return Napi::Boolean::New(env, res);
584
691
  }
585
692
 
586
693
  /**
@@ -611,70 +718,93 @@ NAN_METHOD(K2hQueue::IsEmpty)
611
718
  *
612
719
  */
613
720
 
614
- NAN_METHOD(K2hQueue::Read)
721
+ Napi::Value K2hQueue::Read(const Napi::CallbackInfo& info)
615
722
  {
616
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
617
- int pos = 0;
618
- bool is_pass_set = false;
619
- string strpass;
620
- Nan::Callback* callback = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
723
+ Napi::Env env = info.Env();
724
+
725
+ // Unwrap
726
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
727
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
728
+ return env.Undefined();
729
+ }
730
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
731
+
732
+ int pos = 0;
733
+ bool is_pass_set = false;
734
+ std::string strpass;
735
+
736
+ // initial callback comes from emitter map if set
737
+ Napi::Function maybeCallback;
738
+ bool hasCallback = false;
739
+ Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_READ]);
740
+ if(emitterCbRef){
741
+ maybeCallback = emitterCbRef->Value();
742
+ hasCallback = true;
743
+ }
621
744
 
745
+ // parse positional optional args
622
746
  if(0 < info.Length()){
623
- if(info[0]->IsNumber()){
624
- pos = Nan::To<int>(info[0]).ToChecked();
625
- }else if(info[0]->IsString()){
626
- Nan::Utf8String buf(info[0]);
627
- strpass = std::string(*buf);
628
- is_pass_set = true;
629
- }else if(info[0]->IsFunction()){
747
+ if(info[0].IsNumber()){
748
+ pos = info[0].ToNumber().Int32Value();
749
+ }else if(info[0].IsString()){
750
+ strpass = info[0].ToString().Utf8Value();
751
+ is_pass_set = true;
752
+ }else if(info[0].IsFunction()){
630
753
  if(1 < info.Length()){
631
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
632
- return;
754
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
755
+ return env.Undefined();
633
756
  }
634
- callback = new Nan::Callback(info[0].As<v8::Function>());
757
+ maybeCallback = info[0].As<Napi::Function>();
758
+ hasCallback = true;
635
759
  }else{
636
- Nan::ThrowSyntaxError("Unknown first parameter.");
637
- return;
760
+ Napi::TypeError::New(env, "Unknown first parameter.").ThrowAsJavaScriptException();
761
+ return env.Undefined();
638
762
  }
639
763
  }
764
+
640
765
  if(1 < info.Length()){
641
- if(info[1]->IsString() && !is_pass_set){
642
- Nan::Utf8String buf(info[1]);
643
- strpass = std::string(*buf);
644
- is_pass_set = true;
645
- }else if(info[1]->IsFunction()){
766
+ if(info[1].IsString() && !is_pass_set){
767
+ strpass = info[1].ToString().Utf8Value();
768
+ is_pass_set = true;
769
+ }else if(info[1].IsFunction()){
646
770
  if(2 < info.Length()){
647
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
648
- return;
771
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
772
+ return env.Undefined();
649
773
  }
650
- callback = new Nan::Callback(info[1].As<v8::Function>());
774
+ maybeCallback = info[1].As<Napi::Function>();
775
+ hasCallback = true;
651
776
  }else{
652
- Nan::ThrowSyntaxError("Unknown second parameter.");
653
- return;
777
+ Napi::TypeError::New(env, "Unknown second parameter.").ThrowAsJavaScriptException();
778
+ return env.Undefined();
654
779
  }
655
780
  }
781
+
656
782
  if(2 < info.Length()){
657
- if(3 < info.Length() || !info[2]->IsFunction()){
658
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
659
- return;
783
+ if(3 < info.Length() || !info[2].IsFunction()){
784
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
785
+ return env.Undefined();
660
786
  }
661
- callback = new Nan::Callback(info[2].As<v8::Function>());
787
+ maybeCallback = info[2].As<Napi::Function>();
788
+ hasCallback = true;
662
789
  }
663
790
 
664
- // work
665
- if(callback){
666
- Nan::AsyncQueueWorker(new K2hqReadWorker(callback, &(obj->_k2hqueue), pos, (is_pass_set ? strpass.c_str() : NULL)));
667
- info.GetReturnValue().Set(Nan::True());
791
+ // Execute
792
+ if(hasCallback){
793
+ K2hqReadAsyncWorker* worker = new K2hqReadAsyncWorker(maybeCallback, &(obj->_k2hqueue), pos, (is_pass_set ? strpass.c_str() : NULL));
794
+ worker->Queue();
795
+ return Napi::Boolean::New(env, true);
668
796
  }else{
669
797
  unsigned char* pdata = NULL;
670
798
  size_t datalen = 0;
671
799
  bool result = obj->_k2hqueue.Read(&pdata, datalen, pos, (is_pass_set ? strpass.c_str() : NULL));
672
800
  if(!result || !pdata || 0 == datalen){
673
- info.GetReturnValue().Set(Nan::Null());
801
+ K2H_Free(pdata);
802
+ return env.Null();
674
803
  }else{
675
- info.GetReturnValue().Set(Nan::New<String>(reinterpret_cast<const char*>(pdata)).ToLocalChecked());
804
+ Napi::String ret = Napi::String::New(env, reinterpret_cast<const char*>(pdata), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pdata))));
805
+ K2H_Free(pdata);
806
+ return ret;
676
807
  }
677
- K2H_Free(pdata);
678
808
  }
679
809
  }
680
810
 
@@ -699,51 +829,73 @@ NAN_METHOD(K2hQueue::Read)
699
829
  *
700
830
  */
701
831
 
702
- NAN_METHOD(K2hQueue::Pop)
832
+ Napi::Value K2hQueue::Pop(const Napi::CallbackInfo& info)
703
833
  {
704
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
705
- bool is_pass_set = false;
706
- string strpass;
707
- Nan::Callback* callback = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
834
+ Napi::Env env = info.Env();
708
835
 
836
+ // Unwrap
837
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
838
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
839
+ return env.Undefined();
840
+ }
841
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
842
+
843
+ bool is_pass_set = false;
844
+ std::string strpass;
845
+
846
+ // initial callback comes from emitter map if set
847
+ Napi::Function maybeCallback;
848
+ bool hasCallback = false;
849
+ Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_POP]);
850
+ if(emitterCbRef){
851
+ maybeCallback = emitterCbRef->Value();
852
+ hasCallback = true;
853
+ }
854
+
855
+ // parse positional optional args
709
856
  if(0 < info.Length()){
710
- if(info[0]->IsString()){
711
- Nan::Utf8String buf(info[0]);
712
- strpass = std::string(*buf);
713
- is_pass_set = true;
714
- }else if(info[0]->IsFunction()){
857
+ if(info[0].IsString()){
858
+ strpass = info[0].ToString().Utf8Value();
859
+ is_pass_set = true;
860
+ }else if(info[0].IsFunction()){
715
861
  if(1 < info.Length()){
716
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
717
- return;
862
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
863
+ return env.Undefined();
718
864
  }
719
- callback = new Nan::Callback(info[0].As<v8::Function>());
865
+ maybeCallback = info[0].As<Napi::Function>();
866
+ hasCallback = true;
720
867
  }else{
721
- Nan::ThrowSyntaxError("Unknown first parameter.");
722
- return;
868
+ Napi::TypeError::New(env, "Unknown first parameter.").ThrowAsJavaScriptException();
869
+ return env.Undefined();
723
870
  }
724
871
  }
872
+
725
873
  if(1 < info.Length()){
726
- if(2 < info.Length() || !info[1]->IsFunction()){
727
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
728
- return;
874
+ if(2 < info.Length() || !info[1].IsFunction()){
875
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
876
+ return env.Undefined();
729
877
  }
730
- callback = new Nan::Callback(info[1].As<v8::Function>());
878
+ maybeCallback = info[1].As<Napi::Function>();
879
+ hasCallback = true;
731
880
  }
732
881
 
733
- // work
734
- if(callback){
735
- Nan::AsyncQueueWorker(new K2hqPopWorker(callback, &(obj->_k2hqueue), (is_pass_set ? strpass.c_str() : NULL)));
736
- info.GetReturnValue().Set(Nan::True());
882
+ // Execute
883
+ if(hasCallback){
884
+ K2hqPopAsyncWorker* worker = new K2hqPopAsyncWorker(maybeCallback, &(obj->_k2hqueue), (is_pass_set ? strpass.c_str() : NULL));
885
+ worker->Queue();
886
+ return Napi::Boolean::New(env, true);
737
887
  }else{
738
888
  unsigned char* pdata = NULL;
739
889
  size_t datalen = 0;
740
890
  bool result = obj->_k2hqueue.Pop(&pdata, datalen, NULL, (is_pass_set ? strpass.c_str() : NULL));
741
891
  if(!result || !pdata || 0 == datalen){
742
- info.GetReturnValue().Set(Nan::Null());
892
+ K2H_Free(pdata);
893
+ return env.Null();
743
894
  }else{
744
- info.GetReturnValue().Set(Nan::New<String>(reinterpret_cast<const char*>(pdata)).ToLocalChecked());
895
+ Napi::String ret = Napi::String::New(env, reinterpret_cast<const char*>(pdata), static_cast<size_t>(strlen(reinterpret_cast<const char*>(pdata))));
896
+ K2H_Free(pdata);
897
+ return ret;
745
898
  }
746
- K2H_Free(pdata);
747
899
  }
748
900
  }
749
901
 
@@ -770,59 +922,81 @@ NAN_METHOD(K2hQueue::Pop)
770
922
  *
771
923
  */
772
924
 
773
- NAN_METHOD(K2hQueue::Remove)
925
+ Napi::Value K2hQueue::Remove(const Napi::CallbackInfo& info)
774
926
  {
927
+ Napi::Env env = info.Env();
928
+
775
929
  if(info.Length() < 1){
776
- Nan::ThrowSyntaxError("Need to specify first parameter for count of queue.");
777
- return;
930
+ Napi::TypeError::New(env, "Need to specify first parameter for count of queue.").ThrowAsJavaScriptException();
931
+ return env.Undefined();
778
932
  }
779
933
 
780
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
781
- int count = 0;
782
- bool is_pass_set = false;
783
- string strpass;
784
- Nan::Callback* callback = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
934
+ // Unwrap
935
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
936
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
937
+ return env.Undefined();
938
+ }
939
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
940
+
941
+ int count = 0;
942
+ bool is_pass_set = false;
943
+ std::string strpass;
944
+
945
+ // initial callback comes from emitter map if set
946
+ Napi::Function maybeCallback;
947
+ bool hasCallback = false;
948
+ Napi::FunctionReference* emitterCbRef = obj->_cbs.Find(stc_k2hq_emitters[K2HQ_EMITTER_POS_REMOVE]);
949
+ if(emitterCbRef){
950
+ maybeCallback = emitterCbRef->Value();
951
+ hasCallback = true;
952
+ }
785
953
 
786
- if(!info[0]->IsNumber()){
787
- Nan::ThrowSyntaxError("First parameter must be number.");
788
- return;
954
+ // parse positional optional args
955
+ if(!info[0].IsNumber()){
956
+ Napi::TypeError::New(env, "First parameter must be number.").ThrowAsJavaScriptException();
957
+ return env.Undefined();
789
958
  }else{
790
- count = Nan::To<int>(info[0]).ToChecked();
959
+ count = info[0].ToNumber().Int32Value();
791
960
  if(count <= 0){
792
- Nan::ThrowSyntaxError("Remove queue count must be over 0.");
793
- return;
961
+ Napi::TypeError::New(env, "Remove queue count must be over 0.").ThrowAsJavaScriptException();
962
+ return env.Undefined();
794
963
  }
795
964
  }
965
+
796
966
  if(1 < info.Length()){
797
- if(info[1]->IsString()){
798
- Nan::Utf8String buf(info[1]);
799
- strpass = std::string(*buf);
800
- is_pass_set = true;
801
- }else if(info[1]->IsFunction()){
967
+ if(info[1].IsString()){
968
+ strpass = info[1].ToString().Utf8Value();
969
+ is_pass_set = true;
970
+ }else if(info[1].IsFunction()){
802
971
  if(2 < info.Length()){
803
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
804
- return;
972
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
973
+ return env.Undefined();
805
974
  }
806
- callback = new Nan::Callback(info[1].As<v8::Function>());
975
+ maybeCallback = info[1].As<Napi::Function>();
976
+ hasCallback = true;
807
977
  }else{
808
- Nan::ThrowSyntaxError("Unknown second parameter.");
809
- return;
978
+ Napi::TypeError::New(env, "Unknown second parameter.").ThrowAsJavaScriptException();
979
+ return env.Undefined();
810
980
  }
811
981
  }
982
+
812
983
  if(2 < info.Length()){
813
- if(3 < info.Length() || !info[2]->IsFunction()){
814
- Nan::ThrowSyntaxError("Last parameter is not callback function.");
815
- return;
984
+ if(3 < info.Length() || !info[2].IsFunction()){
985
+ Napi::TypeError::New(env, "Last parameter is not callback function.").ThrowAsJavaScriptException();
986
+ return env.Undefined();
816
987
  }
817
- callback = new Nan::Callback(info[2].As<v8::Function>());
988
+ maybeCallback = info[2].As<Napi::Function>();
989
+ hasCallback = true;
818
990
  }
819
991
 
820
- // work
821
- if(callback){
822
- Nan::AsyncQueueWorker(new K2hqRemoveWorker(callback, &(obj->_k2hqueue), count, (is_pass_set ? strpass.c_str() : NULL)));
823
- info.GetReturnValue().Set(Nan::True());
992
+ // Execute
993
+ if(hasCallback){
994
+ K2hqRemoveAsyncWorker* worker = new K2hqRemoveAsyncWorker(maybeCallback, &(obj->_k2hqueue), count, (is_pass_set ? strpass.c_str() : NULL));
995
+ worker->Queue();
996
+ return Napi::Boolean::New(env, true);
824
997
  }else{
825
- info.GetReturnValue().Set(Nan::New(obj->_k2hqueue.Remove(count, NULL, NULL, (is_pass_set ? strpass.c_str() : NULL))));
998
+ int res = obj->_k2hqueue.Remove(count, NULL, NULL, (is_pass_set ? strpass.c_str() : NULL));
999
+ return Napi::Number::New(env, static_cast<double>(res));
826
1000
  }
827
1001
  }
828
1002
 
@@ -839,25 +1013,30 @@ NAN_METHOD(K2hQueue::Remove)
839
1013
  * @return return true for success, false for failure
840
1014
  */
841
1015
 
842
- NAN_METHOD(K2hQueue::Dump)
1016
+ Napi::Value K2hQueue::Dump(const Napi::CallbackInfo& info)
843
1017
  {
844
- K2hQueue* obj = Nan::ObjectWrap::Unwrap<K2hQueue>(info.This());
1018
+ Napi::Env env = info.Env();
1019
+
1020
+ // Unwrap
1021
+ if(!info.This().IsObject()||!info.This().As<Napi::Object>().InstanceOf(K2hQueue::constructor.Value())){
1022
+ Napi::TypeError::New(env, "Invalid this object(K2hQueue instance)").ThrowAsJavaScriptException();
1023
+ return env.Undefined();
1024
+ }
1025
+ K2hQueue* obj = Napi::ObjectWrap<K2hQueue>::Unwrap(info.This().As<Napi::Object>());
845
1026
 
846
- int fd = (0 < info.Length() && info[0]->IsInt32()) ? Nan::To<int32_t>(info[0]).ToChecked() : -1;
847
- FILE* fp = (-1 == fd ? stdout : fdopen(fd, "a"));
1027
+ int fd = (0 < info.Length() && info[0].IsNumber()) ? info[0].ToNumber().Int32Value() : -1;
1028
+ FILE* fp = (-1 == fd ? stdout : fdopen(fd, "a"));
848
1029
  if(!fp){
849
- Nan::ThrowError("could not open output stream.");
850
- return;
1030
+ Napi::Error::New(env, "could not open output stream.").ThrowAsJavaScriptException();
1031
+ return env.Undefined();
851
1032
  }
852
1033
 
853
- info.GetReturnValue().Set(Nan::New(
854
- obj->_k2hqueue.Dump(fp)
855
- ));
1034
+ obj->_k2hqueue.Dump(fp);
856
1035
 
857
1036
  // Need to flush stream here!
858
1037
  fflush(fp);
859
1038
 
860
- info.GetReturnValue().Set(Nan::True());
1039
+ return Napi::Boolean::New(env, true);
861
1040
  }
862
1041
 
863
1042
  //@}