Skip to content

Commit 35791b2

Browse files
committed
Updated to support NodeJS 14
1 parent 89cdaa7 commit 35791b2

File tree

2 files changed

+113
-41
lines changed

2 files changed

+113
-41
lines changed

src/i2c.cc

+78-41
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void SetAddress(const Nan::FunctionCallbackInfo<v8::Value>& info) {
3535
Nan::ThrowTypeError("addr must be an int");
3636
return;
3737
}
38-
addr = info[0]->Int32Value();
38+
addr = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
3939
setAddress(addr);
4040
}
4141

@@ -57,7 +57,11 @@ void Scan(const Nan::FunctionCallbackInfo<v8::Value>& info) {
5757
if (res >= 0) {
5858
res = i;
5959
}
60-
results->Set(i, Nan::New<Integer>(res));
60+
results->Set(
61+
Nan::GetCurrentContext(),
62+
i,
63+
Nan::New<Integer>(res)
64+
);
6165
}
6266

6367
setAddress(addr);
@@ -81,7 +85,7 @@ void Close(const Nan::FunctionCallbackInfo<v8::Value>& info) {
8185
void Open(const Nan::FunctionCallbackInfo<v8::Value>& info) {
8286
Nan::HandleScope scope;
8387

84-
String::Utf8Value device(info[0]);
88+
Nan::Utf8String device(info[0]);
8589
Local<Value> err = Nan::New<Value>(Nan::Null());
8690

8791
fd = open(*device, O_RDWR);
@@ -100,7 +104,7 @@ void Open(const Nan::FunctionCallbackInfo<v8::Value>& info) {
100104
void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
101105
Nan::HandleScope scope;
102106

103-
int len = info[0]->Int32Value();
107+
int len = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
104108

105109
Local<Array> data = Nan::New<Array>();
106110

@@ -111,7 +115,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
111115
err = Nan::Error(Nan::New("Cannot read from device").ToLocalChecked());
112116
} else {
113117
for (int i = 0; i < len; ++i) {
114-
data->Set(i, Nan::New<Integer>(buf[i]));
118+
data->Set(Nan::GetCurrentContext(), i, Nan::New<Integer>(buf[i]));
115119
}
116120
}
117121
delete[] buf;
@@ -151,8 +155,8 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
151155
void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
152156
Nan::HandleScope scope;
153157

154-
int8_t cmd = info[0]->Int32Value();
155-
int32_t len = info[1]->Int32Value();
158+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
159+
int32_t len = info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
156160
uint8_t data[len];
157161
Local<Value> err = Nan::New<Value>(Nan::Null());
158162
// Local<Object> buffer = node::Buffer::New(len);
@@ -175,7 +179,7 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
175179
}
176180

177181
if (info[2]->IsNumber()) {
178-
int32_t delay = info[2]->Int32Value();
182+
int32_t delay = info[2]->Int32Value(Nan::GetCurrentContext()).FromJust();
179183
usleep(delay * 1000);
180184
} else {
181185
break;
@@ -188,10 +192,11 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
188192
void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
189193
Nan::HandleScope scope;
190194

191-
Local<Value> buffer = info[0];
195+
// v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
196+
auto bufferObj = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
192197

193-
int len = node::Buffer::Length(buffer->ToObject());
194-
char* data = node::Buffer::Data(buffer->ToObject());
198+
int len = node::Buffer::Length(bufferObj);
199+
char* data = node::Buffer::Data(bufferObj);
195200

196201
Local<Value> err = Nan::New<Value>(Nan::Null());
197202

@@ -210,7 +215,7 @@ void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
210215
void WriteByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
211216
Nan::HandleScope scope;
212217

213-
int8_t byte = info[0]->Int32Value();
218+
int8_t byte = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
214219
Local<Value> err = Nan::New<Value>(Nan::Null());
215220

216221
if (i2c_smbus_write_byte(fd, byte) == -1) {
@@ -228,10 +233,12 @@ void WriteByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
228233
void WriteBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
229234
Nan::HandleScope scope;
230235

231-
Local<Value> buffer = info[1];
232-
int8_t cmd = info[0]->Int32Value();
233-
int len = node::Buffer::Length(buffer->ToObject());
234-
char* data = node::Buffer::Data(buffer->ToObject());
236+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
237+
auto bufferObj = info[1]->ToObject(context).ToLocalChecked();
238+
239+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
240+
int len = node::Buffer::Length(bufferObj);
241+
char* data = node::Buffer::Data(bufferObj);
235242

236243
Local<Value> err = Nan::New<Value>(Nan::Null());
237244

@@ -250,8 +257,8 @@ void WriteBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
250257
void WriteWord(const Nan::FunctionCallbackInfo<v8::Value>& info) {
251258
Nan::HandleScope scope;
252259

253-
int8_t cmd = info[0]->Int32Value();
254-
int16_t word = info[1]->Int32Value();
260+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
261+
int16_t word = info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
255262

256263
Local<Value> err = Nan::New<Value>(Nan::Null());
257264

@@ -267,29 +274,59 @@ void WriteWord(const Nan::FunctionCallbackInfo<v8::Value>& info) {
267274
}
268275
}
269276

270-
void Init(Handle<Object> exports) {
271-
272-
exports->Set(Nan::New("setAddress").ToLocalChecked(),
273-
Nan::New<v8::FunctionTemplate>(SetAddress)->GetFunction());
274-
exports->Set(Nan::New("scan").ToLocalChecked(),
275-
Nan::New<v8::FunctionTemplate>(Scan)->GetFunction());
276-
exports->Set(Nan::New("open").ToLocalChecked(),
277-
Nan::New<v8::FunctionTemplate>(Open)->GetFunction());
278-
exports->Set(Nan::New("close").ToLocalChecked(),
279-
Nan::New<v8::FunctionTemplate>(Close)->GetFunction());
280-
exports->Set(Nan::New("write").ToLocalChecked(),
281-
Nan::New<v8::FunctionTemplate>(Write)->GetFunction());
282-
exports->Set(Nan::New("writeByte").ToLocalChecked(),
283-
Nan::New<v8::FunctionTemplate>(WriteByte)->GetFunction());
284-
exports->Set(Nan::New("writeBlock").ToLocalChecked(),
285-
Nan::New<v8::FunctionTemplate>(WriteBlock)->GetFunction());
286-
exports->Set(Nan::New("read").ToLocalChecked(),
287-
Nan::New<v8::FunctionTemplate>(Read)->GetFunction());
288-
exports->Set(Nan::New("readByte").ToLocalChecked(),
289-
Nan::New<v8::FunctionTemplate>(ReadByte)->GetFunction());
290-
exports->Set(Nan::New("readBlock").ToLocalChecked(),
291-
Nan::New<v8::FunctionTemplate>(ReadBlock)->GetFunction());
277+
void Init(Local<Object> exports) {
278+
279+
exports->Set(
280+
Nan::GetCurrentContext(),
281+
Nan::New("setAddress").ToLocalChecked(),
282+
Nan::New<v8::FunctionTemplate>(SetAddress)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
283+
);
284+
exports->Set(
285+
Nan::GetCurrentContext(),
286+
Nan::New("scan").ToLocalChecked(),
287+
Nan::New<v8::FunctionTemplate>(Scan)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
288+
);
289+
exports->Set(
290+
Nan::GetCurrentContext(),
291+
Nan::New("open").ToLocalChecked(),
292+
Nan::New<v8::FunctionTemplate>(Open)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
293+
);
294+
exports->Set(
295+
Nan::GetCurrentContext(),
296+
Nan::New("close").ToLocalChecked(),
297+
Nan::New<v8::FunctionTemplate>(Close)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
298+
);
299+
exports->Set(
300+
Nan::GetCurrentContext(),
301+
Nan::New("write").ToLocalChecked(),
302+
Nan::New<v8::FunctionTemplate>(Write)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
303+
);
304+
exports->Set(
305+
Nan::GetCurrentContext(),
306+
Nan::New("writeByte").ToLocalChecked(),
307+
Nan::New<v8::FunctionTemplate>(WriteByte)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
308+
);
309+
exports->Set(
310+
Nan::GetCurrentContext(),
311+
Nan::New("writeBlock").ToLocalChecked(),
312+
Nan::New<v8::FunctionTemplate>(WriteBlock)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
313+
);
314+
exports->Set(
315+
Nan::GetCurrentContext(),
316+
Nan::New("read").ToLocalChecked(),
317+
Nan::New<v8::FunctionTemplate>(Read)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
318+
);
319+
exports->Set(
320+
Nan::GetCurrentContext(),
321+
Nan::New("readByte").ToLocalChecked(),
322+
Nan::New<v8::FunctionTemplate>(ReadByte)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
323+
);
324+
exports->Set(
325+
Nan::GetCurrentContext(),
326+
Nan::New("readBlock").ToLocalChecked(),
327+
Nan::New<v8::FunctionTemplate>(ReadBlock)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
328+
);
292329

293330
}
294331

295-
NODE_MODULE(i2c, Init)
332+
NODE_MODULE(i2c, Init)

yarn.lock

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
bindings@1.5.0:
6+
version "1.5.0"
7+
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
8+
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
9+
dependencies:
10+
file-uri-to-path "1.0.0"
11+
12+
coffeescript@2.5.1:
13+
version "2.5.1"
14+
resolved "https://registry.yarnpkg.com/coffeescript/-/coffeescript-2.5.1.tgz#b2442a1f2c806139669534a54adc35010559d16a"
15+
integrity sha512-J2jRPX0eeFh5VKyVnoLrfVFgLZtnnmp96WQSLAS8OrLm2wtQLcnikYKe1gViJKDH7vucjuhHvBKKBP3rKcD1tQ==
16+
17+
file-uri-to-path@1.0.0:
18+
version "1.0.0"
19+
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
20+
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
21+
22+
nan@~2.14.0:
23+
version "2.14.2"
24+
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.2.tgz#f5376400695168f4cc694ac9393d0c9585eeea19"
25+
integrity sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==
26+
27+
repl@0.1.3:
28+
version "0.1.3"
29+
resolved "https://registry.yarnpkg.com/repl/-/repl-0.1.3.tgz#2f05d42b0c88b43d05ccbda10ed14aeff5699b60"
30+
integrity sha1-LwXUKwyItD0FzL2hDtFK7/Vpm2A=
31+
32+
underscore@1.11.0:
33+
version "1.11.0"
34+
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.11.0.tgz#dd7c23a195db34267186044649870ff1bab5929e"
35+
integrity sha512-xY96SsN3NA461qIRKZ/+qox37YXPtSBswMGfiNptr+wrt6ds4HaMw23TP612fEyGekRE6LNRiLYr/aqbHXNedw==

0 commit comments

Comments
 (0)