e46fee
commit 1b3d14846e245a541bd7132c96dfeb56cc64b1b1
e46fee
Author: Tom Hughes <tom@compton.nu>
e46fee
Date:   Thu Jun 20 14:01:15 2019 +0100
e46fee
e46fee
    Update for Node.js 12.x support
e46fee
e46fee
diff --git a/src/i2c.cc b/src/i2c.cc
e46fee
index c7d4bee..968d5fb 100644
e46fee
--- a/src/i2c.cc
e46fee
+++ b/src/i2c.cc
e46fee
@@ -35,7 +35,7 @@ void SetAddress(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     Nan::ThrowTypeError("addr must be an int");
e46fee
     return;
e46fee
   }
e46fee
-  addr = info[0]->Int32Value();
e46fee
+  addr = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
   setAddress(addr);
e46fee
 }
e46fee
 
e46fee
@@ -57,15 +57,16 @@ void Scan(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     if (res >= 0) {
e46fee
       res = i;
e46fee
     }
e46fee
-    results->Set(i, Nan::New<integer>(res));
e46fee
+    Nan::Set(results, i, Nan::New<integer>(res));
e46fee
   }
e46fee
 
e46fee
   setAddress(addr);
e46fee
 
e46fee
   const unsigned argc = 2;
e46fee
   Local<value> argv[argc] = { err, results };
e46fee
+  Nan::AsyncResource ar("Scan");
e46fee
 
e46fee
-  Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+  ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
 
e46fee
   info.GetReturnValue().Set(results);
e46fee
 }
e46fee
@@ -81,7 +82,7 @@ void Close(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
 void Open(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
 
e46fee
-  String::Utf8Value device(info[0]);
e46fee
+  Nan::Utf8String device(info[0]);
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
 
e46fee
   fd = open(*device, O_RDWR);
e46fee
@@ -93,14 +94,16 @@ void Open(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 1;
e46fee
     Local<function> callback = Local<function>::Cast(info[1]);
e46fee
     Local<value> argv[argc] = { err };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("Open");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
 void Read(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
 
e46fee
-  int len = info[0]->Int32Value();
e46fee
+  int len = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
 
e46fee
   Local<array> data = Nan::New<array>();
e46fee
 
e46fee
@@ -111,7 +114,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     err = Nan::Error(Nan::New("Cannot read from device").ToLocalChecked());
e46fee
   } else {
e46fee
     for (int i = 0; i < len; ++i) {
e46fee
-      data->Set(i, Nan::New<integer>(buf[i]));
e46fee
+      Nan::Set(data, i, Nan::New<integer>(buf[i]));
e46fee
     }
e46fee
   }
e46fee
   delete[] buf;
e46fee
@@ -120,7 +123,9 @@ void Read(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 2;
e46fee
     Local<function> callback = Local<function>::Cast(info[1]);
e46fee
     Local<value> argv[argc] = { err, data };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("Read");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
@@ -142,7 +147,9 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 2;
e46fee
     Local<function> callback = Local<function>::Cast(info[0]);
e46fee
     Local<value> argv[argc] = { err, data };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("ReadByte");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 
e46fee
   info.GetReturnValue().Set(data);
e46fee
@@ -151,8 +158,8 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
 void ReadBlock(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
 
e46fee
-  int8_t cmd = info[0]->Int32Value();
e46fee
-  int32_t len = info[1]->Int32Value();
e46fee
+  int8_t cmd = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
+  int32_t len = Nan::To<int32_t>(info[1]).ToChecked();
e46fee
   uint8_t data[len]; 
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
   // Local<object> buffer = node::Buffer::New(len);
e46fee
@@ -171,11 +178,13 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
       const unsigned argc = 2;
e46fee
       Local<function> callback = Local<function>::Cast(info[3]);
e46fee
       Local<value> argv[argc] = { err, buffer };
e46fee
-      Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+      Nan::AsyncResource ar("ReadBlock");
e46fee
+
e46fee
+      ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
     }
e46fee
  
e46fee
     if (info[2]->IsNumber()) {
e46fee
-      int32_t delay = info[2]->Int32Value();
e46fee
+      int32_t delay = Nan::To<int32_t>(info[2]).ToChecked();
e46fee
       usleep(delay * 1000);
e46fee
     } else {
e46fee
       break;
e46fee
@@ -190,8 +199,8 @@ void Write(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
 
e46fee
   Local<value> buffer = info[0];
e46fee
 
e46fee
-  int   len = node::Buffer::Length(buffer->ToObject());
e46fee
-  char* data = node::Buffer::Data(buffer->ToObject());
e46fee
+  int   len = node::Buffer::Length(Nan::To<object>(buffer).ToLocalChecked());
e46fee
+  char* data = node::Buffer::Data(Nan::To<object>(buffer).ToLocalChecked());
e46fee
 
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
 
e46fee
@@ -203,14 +212,16 @@ void Write(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 1;
e46fee
     Local<function> callback = Local<function>::Cast(info[1]);
e46fee
     Local<value> argv[argc] = { err };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("Write");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
 void WriteByte(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
 
e46fee
-  int8_t byte = info[0]->Int32Value();
e46fee
+  int8_t byte = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
 
e46fee
   if (i2c_smbus_write_byte(fd, byte) == -1) {
e46fee
@@ -221,7 +232,9 @@ void WriteByte(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 1;
e46fee
     Local<function> callback = Local<function>::Cast(info[1]);
e46fee
     Local<value> argv[argc] = { err };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("WriteByte");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
@@ -229,9 +242,9 @@ void WriteBlock(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
 
e46fee
   Local<value> buffer = info[1];
e46fee
-  int8_t cmd = info[0]->Int32Value();
e46fee
-  int   len = node::Buffer::Length(buffer->ToObject());
e46fee
-  char* data = node::Buffer::Data(buffer->ToObject());
e46fee
+  int8_t cmd = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
+  int   len = node::Buffer::Length(Nan::To<object>(buffer).ToLocalChecked());
e46fee
+  char* data = node::Buffer::Data(Nan::To<object>(buffer).ToLocalChecked());
e46fee
 
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
 
e46fee
@@ -243,15 +256,17 @@ void WriteBlock(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 1;
e46fee
     Local<function> callback = Local<function>::Cast(info[2]);
e46fee
     Local<value> argv[argc] = { err };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("WriteBlock");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
 void WriteWord(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
   Nan::HandleScope scope;
e46fee
   
e46fee
-  int8_t cmd = info[0]->Int32Value();
e46fee
-  int16_t word = info[1]->Int32Value();
e46fee
+  int8_t cmd = Nan::To<int32_t>(info[0]).ToChecked();
e46fee
+  int16_t word = Nan::To<int32_t>(info[1]).ToChecked();
e46fee
 
e46fee
   Local<value> err = Nan::New<value>(Nan::Null());
e46fee
   
e46fee
@@ -263,33 +278,25 @@ void WriteWord(const Nan::FunctionCallbackInfo<v8::value>& info) {
e46fee
     const unsigned argc = 1;
e46fee
     Local<function> callback = Local<function>::Cast(info[2]);
e46fee
     Local<value> argv[argc] = { err };
e46fee
-    Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
+    Nan::AsyncResource ar("WriteWord");
e46fee
+
e46fee
+    ar.runInAsyncScope(Nan::GetCurrentContext()->Global(), callback, argc, argv);
e46fee
   }
e46fee
 }
e46fee
 
e46fee
-void Init(Handle<object> exports) {
e46fee
-
e46fee
-  exports->Set(Nan::New("setAddress").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(SetAddress)->GetFunction());
e46fee
-  exports->Set(Nan::New("scan").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(Scan)->GetFunction());
e46fee
-  exports->Set(Nan::New("open").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(Open)->GetFunction());
e46fee
-  exports->Set(Nan::New("close").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(Close)->GetFunction());
e46fee
-  exports->Set(Nan::New("write").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(Write)->GetFunction());
e46fee
-  exports->Set(Nan::New("writeByte").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(WriteByte)->GetFunction());
e46fee
-  exports->Set(Nan::New("writeBlock").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(WriteBlock)->GetFunction());
e46fee
-  exports->Set(Nan::New("read").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(Read)->GetFunction());
e46fee
-  exports->Set(Nan::New("readByte").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(ReadByte)->GetFunction());
e46fee
-  exports->Set(Nan::New("readBlock").ToLocalChecked(),
e46fee
-               Nan::New<v8::functiontemplate>(ReadBlock)->GetFunction());
e46fee
+NAN_MODULE_INIT(Init) {
e46fee
+
e46fee
+  Nan::Export(target, "setAddress", SetAddress);
e46fee
+  Nan::Export(target, "scan", Scan);
e46fee
+  Nan::Export(target, "open", Open);
e46fee
+  Nan::Export(target, "close", Close);
e46fee
+  Nan::Export(target, "write", Write);
e46fee
+  Nan::Export(target, "writeByte", WriteByte);
e46fee
+  Nan::Export(target, "writeBlock", WriteBlock);
e46fee
+  Nan::Export(target, "read", Read);
e46fee
+  Nan::Export(target, "readByte", ReadByte);
e46fee
+  Nan::Export(target, "readBlock", ReadBlock);
e46fee
 
e46fee
 }
e46fee
 
e46fee
-NODE_MODULE(i2c, Init)
e46fee
\ No newline at end of file
e46fee
+NAN_MODULE_WORKER_ENABLED(i2c, Init)