k2hash 1.1.34 → 2.0.1

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