X Tutup
Skip to content

Commit 3cca72b

Browse files
committed
Ported signals to use NAN
1 parent 5fc4620 commit 3cca72b

File tree

5 files changed

+62
-53
lines changed

5 files changed

+62
-53
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ There are two options for installing node-can:
5555
1. Clone / download node-can from [GitHub](https://github.com/sebi2k1/node-can),
5656
then:
5757
58-
node-gyp configure && node-gyp build
58+
npm i
59+
npm run configure
60+
npm run build
61+
5962
6063
2. Install via npm:
6164

binding.gyp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"include_dirs": [
77
"<!(node -e \"require('nan')\")"
88
]
9+
},
10+
{
11+
"target_name": "can_signals",
12+
"sources": [ "src/signals.cc" ],
13+
"include_dirs": [
14+
"<!(node -e \"require('nan')\")"
15+
]
916
}
1017
]
1118
}

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
"email": "sebastian@sebastianhaas.info"
66
},
77
"description": "A SocketCAN abstraction layer for NodeJS.",
8-
"version": "2.0.1",
8+
"version": "2.1.0",
9+
"license": "MIT",
910
"repository": {
1011
"type": "git",
1112
"url": "git://github.com/sebi2k1/node-can.git"
1213
},
1314
"scripts": {
14-
"preinstall": "node-gyp configure && node-gyp build",
15-
"preuninstall": "rm -rf build/*"
15+
"configure": "node-gyp configure",
16+
"build": "node-gyp build"
1617
},
17-
"dependencies": {
18+
"devDependencies": {
1819
"nan": "^2.0.9",
20+
"node-gyp": ">=1.0.0"
21+
},
22+
"dependencies": {
1923
"xml2js": ">=0.2.0"
2024
},
2125
"main": "socketcan.js",

src/rawchannel.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright Sebastian Haas <sebastian@sebastianhaas.info>. All rights reserved.
2-
* Updated for NodeJs 4.X using NAN by Daniel Gross <dgross@intronik.de>
2+
* Updated for NodeJS 4.X using NAN by Daniel Gross <dgross@intronik.de>
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to

src/signals.cc

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
*/
1616
#define __STDC_LIMIT_MACROS
1717

18-
#include <v8.h>
19-
#include <node.h>
18+
#include <nan.h>
2019
#include <node_buffer.h>
2120

2221
#include <algorithm>
2322

2423
#include <stdint.h>
2524
#include <string.h>
2625

27-
using namespace node;
2826
using namespace v8;
27+
using namespace node;
2928

30-
#define CHECK_CONDITION(expr, str) if(! (expr) ) return ThrowException(Exception::Error(String::New(str)));
29+
#define CHECK_CONDITION(expr, str) if(!(expr)) return Nan::ThrowError(str);
3130

3231
typedef enum ENDIANESS
3332
{
@@ -81,30 +80,28 @@ static u_int64_t _getvalue(u_int8_t * data,
8180
// arg[1] - offset zero indexed
8281
// arg[3] - bitLength one indexed
8382
// arg[4] - endianess
84-
Handle<Value> DecodeSignal(const Arguments& args)
83+
NAN_METHOD(DecodeSignal)
8584
{
86-
HandleScope scope;
87-
8885
u_int32_t offset, bitLength;
8986
ENDIANESS endianess;
9087
bool isSigned = false;
9188
u_int8_t data[8];
9289

93-
CHECK_CONDITION(args.Length() == 5, "Too few arguments");
94-
CHECK_CONDITION(args[0]->IsObject(), "Invalid argument");
90+
CHECK_CONDITION(info.Length() == 5, "Too few arguments");
91+
CHECK_CONDITION(info[0]->IsObject(), "Invalid argument");
9592

96-
Local<Object> jsData = args[0]->ToObject();
93+
Local<Object> jsData = info[0]->ToObject();
9794

9895
CHECK_CONDITION(Buffer::HasInstance(jsData), "Invalid argument");
99-
CHECK_CONDITION(args[1]->IsUint32(), "Invalid offset");
100-
CHECK_CONDITION(args[2]->IsUint32(), "Invalid bit length");
101-
CHECK_CONDITION(args[3]->IsBoolean(), "Invalid endianess");
102-
CHECK_CONDITION(args[4]->IsBoolean(), "Invalid signed flag");
96+
CHECK_CONDITION(info[1]->IsUint32(), "Invalid offset");
97+
CHECK_CONDITION(info[2]->IsUint32(), "Invalid bit length");
98+
CHECK_CONDITION(info[3]->IsBoolean(), "Invalid endianess");
99+
CHECK_CONDITION(info[4]->IsBoolean(), "Invalid signed flag");
103100

104-
offset = args[1]->ToUint32()->Uint32Value();
105-
bitLength = args[2]->ToUint32()->Uint32Value();
106-
endianess = args[3]->IsTrue() ? ENDIANESS_INTEL : ENDIANESS_MOTOROLA;
107-
isSigned = args[4]->IsTrue() ? true : false;
101+
offset = info[1]->ToUint32()->Uint32Value();
102+
bitLength = info[2]->ToUint32()->Uint32Value();
103+
endianess = info[3]->IsTrue() ? ENDIANESS_INTEL : ENDIANESS_MOTOROLA;
104+
isSigned = info[4]->IsTrue() ? true : false;
108105

109106
size_t maxBytes = std::min<u_int32_t>(Buffer::Length(jsData), sizeof(data));
110107

@@ -117,12 +114,12 @@ Handle<Value> DecodeSignal(const Arguments& args)
117114
// Value shall be interpreted as signed (2's complement)
118115
if (isSigned && val & (1 << (bitLength - 1))) {
119116
int32_t tmp = -1 * (~((UINT64_MAX << bitLength) | val) + 1);
120-
retval = Integer::New(tmp);
117+
retval = Nan::New(tmp);
121118
} else {
122-
retval = Integer::NewFromUnsigned((u_int32_t)val);
119+
retval = Nan::New((u_int32_t)val);
123120
}
124121

125-
return scope.Close(retval);
122+
info.GetReturnValue().Set(retval);
126123
}
127124

128125
void _setvalue(u_int32_t offset, u_int32_t bitLength, ENDIANESS endianess, u_int8_t data[8], u_int64_t raw_value)
@@ -171,35 +168,33 @@ void _setvalue(u_int32_t offset, u_int32_t bitLength, ENDIANESS endianess, u_int
171168
// arg[4] - endianess
172169
// arg[5] - sign flag
173170
// arg[6] - value to encode
174-
Handle<Value> EncodeSignal(const Arguments& args)
171+
NAN_METHOD(EncodeSignal)
175172
{
176-
HandleScope scope;
177-
178173
u_int32_t offset, bitLength;
179174
ENDIANESS endianess;
180175
bool sign = false;
181176
u_int8_t data[8];
182177
u_int64_t raw_value;
183178

184-
CHECK_CONDITION(args.Length() == 6, "Too few arguments");
185-
CHECK_CONDITION(args[0]->IsObject(), "Invalid argument");
179+
CHECK_CONDITION(info.Length() == 6, "Too few arguments");
180+
CHECK_CONDITION(info[0]->IsObject(), "Invalid argument");
186181

187-
Local<Object> jsData = args[0]->ToObject();
182+
Local<Object> jsData = info[0]->ToObject();
188183

189184
CHECK_CONDITION(Buffer::HasInstance(jsData), "Invalid argument");
190-
CHECK_CONDITION(args[1]->IsUint32(), "Invalid offset");
191-
CHECK_CONDITION(args[2]->IsUint32(), "Invalid bit length");
192-
CHECK_CONDITION(args[3]->IsBoolean(), "Invalid endianess");
193-
CHECK_CONDITION(args[4]->IsBoolean(), "Invalid sign flag");
194-
CHECK_CONDITION(args[5]->IsNumber() || args[5]->IsBoolean(), "Invalid value");
185+
CHECK_CONDITION(info[1]->IsUint32(), "Invalid offset");
186+
CHECK_CONDITION(info[2]->IsUint32(), "Invalid bit length");
187+
CHECK_CONDITION(info[3]->IsBoolean(), "Invalid endianess");
188+
CHECK_CONDITION(info[4]->IsBoolean(), "Invalid sign flag");
189+
CHECK_CONDITION(info[5]->IsNumber() || info[5]->IsBoolean(), "Invalid value");
195190

196-
offset = args[1]->ToUint32()->Uint32Value();
197-
bitLength = args[2]->ToUint32()->Uint32Value();
198-
endianess = args[3]->IsTrue() ? ENDIANESS_INTEL : ENDIANESS_MOTOROLA;
199-
sign = args[4]->IsTrue() ? true : false;
191+
offset = info[1]->ToUint32()->Uint32Value();
192+
bitLength = info[2]->ToUint32()->Uint32Value();
193+
endianess = info[3]->IsTrue() ? ENDIANESS_INTEL : ENDIANESS_MOTOROLA;
194+
sign = info[4]->IsTrue() ? true : false;
200195

201196
if (sign) {
202-
int32_t in_val = args[5]->ToNumber()->Int32Value();
197+
int32_t in_val = info[5]->ToNumber()->Int32Value();
203198

204199
if (in_val < 0) {
205200
in_val *= -1; // Make it a positive number
@@ -209,7 +204,7 @@ Handle<Value> EncodeSignal(const Arguments& args)
209204
}
210205
}
211206

212-
raw_value = args[5]->ToNumber()->Uint32Value();
207+
raw_value = info[5]->ToNumber()->Uint32Value();
213208

214209
size_t maxBytes = std::min<u_int32_t>(Buffer::Length(jsData), sizeof(data));
215210

@@ -220,17 +215,17 @@ Handle<Value> EncodeSignal(const Arguments& args)
220215

221216
memcpy(Buffer::Data(jsData), data, maxBytes);
222217

223-
return scope.Close(Undefined());
218+
info.GetReturnValue().Set(Nan::Undefined());
224219
}
225220

226221
//-----------------------------------------------------------------------------------------
227222

228-
extern "C" {
229-
static void init (Handle<Object> target)
230-
{
231-
NODE_SET_METHOD(target, "decode_signal", DecodeSignal);
232-
NODE_SET_METHOD(target, "encode_signal", EncodeSignal);
233-
}
234-
235-
NODE_MODULE(can_signals, init);
223+
NAN_MODULE_INIT(InitAll)
224+
{
225+
Nan::Set(target, Nan::New<String>("decode_signal").ToLocalChecked(),
226+
Nan::GetFunction(Nan::New<FunctionTemplate>(DecodeSignal)).ToLocalChecked());
227+
Nan::Set(target, Nan::New<String>("encode_signal").ToLocalChecked(),
228+
Nan::GetFunction(Nan::New<FunctionTemplate>(EncodeSignal)).ToLocalChecked());
236229
}
230+
231+
NODE_MODULE(can_signals, InitAll);

0 commit comments

Comments
 (0)
X Tutup