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