Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"node-pre-gyp": "git+https://github.com/mapbox/node-pre-gyp.git#c88d6a04",
"nan": "^1.6.0"
"nan": "^2.0.9"
},
"bundledDependencies": [
"node-pre-gyp"
Expand Down
82 changes: 37 additions & 45 deletions src/device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string.h>

#define STRUCT_TO_V8(TARGET, STR, NAME) \
TARGET->ForceSet(V8STR(#NAME), NanNew<Uint32>((uint32_t) (STR).NAME), CONST_PROP);
TARGET->ForceSet(V8STR(#NAME), Nan::New<Uint32>((uint32_t) (STR).NAME).ToLocalChecked(), CONST_PROP);

#define CHECK_OPEN() \
if (!self->device_handle){THROW_ERROR("Device is not opened");}
Expand All @@ -12,7 +12,7 @@
static v8::Persistent<v8::FunctionTemplate> device_constructor;

Handle<Object> makeBuffer(const unsigned char* ptr, unsigned length) {
return NanNewBufferHandle((char*) ptr, (uint32_t) length);
return Nan::NewBuffer((char*) ptr, (uint32_t) length).ToLocalChecked();
}

Device::Device(libusb_device* d): device(d), device_handle(0) {
Expand All @@ -28,24 +28,24 @@ Device::~Device(){
}

// Map pinning each libusb_device to a particular V8 instance
std::map<libusb_device*, _NanWeakCallbackInfo<Value, libusb_device>*> Device::byPtr;
std::map<libusb_device*, Nan::WeakCallbackInfo<std::pair<v8::Value, libusb_device*>>*> Device::byPtr;

NAN_WEAK_CALLBACK(DeviceWeakCallback) {
Device::unpin(data.GetParameter());
void DeviceWeakCallback (const Nan::WeakCallbackInfo<std::pair<v8::Value, libusb_device*>> &data) {
Device::unpin(data.GetParameter()->second);
}

// Get a V8 instance for a libusb_device: either the existing one from the map,
// or create a new one and add it to the map.
Handle<Value> Device::get(libusb_device* dev){
Local<Value> Device::get(libusb_device* dev){
auto it = byPtr.find(dev);
if (it != byPtr.end()){
return NanNew(it->second->persistent);
return Nan::New<v8::Value>(it->second->GetParameter()->first);
}else{
Local<FunctionTemplate> constructorHandle = NanNew<v8::FunctionTemplate>(device_constructor);
v8::Handle<v8::Value> argv[1] = { EXTERNAL_NEW(new Device(dev)) };
Handle<Value> v = constructorHandle->GetFunction()->NewInstance(1, argv);
auto p = NanMakeWeakPersistent(v, dev, DeviceWeakCallback);
byPtr.insert(std::make_pair(dev, p));
Local<FunctionTemplate> constructorHandle = Nan::New<v8::FunctionTemplate>(device_constructor).ToLocalChecked();
v8::Local<v8::Value> argv[1] = { EXTERNAL_NEW(new Device(dev)) };
v8::Local<v8::Value> v = constructorHandle->GetFunction()->NewInstance(1, argv);
auto p = Nan::MakeWeakPersistent(v, dev, DeviceWeakCallback);
byPtr.insert(std::make_pair(dev, std::make_pair(p, dev)));
return v;
}
}
Expand All @@ -58,13 +58,13 @@ void Device::unpin(libusb_device* device) {
static NAN_METHOD(deviceConstructor) {
ENTER_CONSTRUCTOR_POINTER(Device, 1);

args.This()->ForceSet(V8SYM("busNumber"),
NanNew<Uint32>((uint32_t) libusb_get_bus_number(self->device)), CONST_PROP);
args.This()->ForceSet(V8SYM("deviceAddress"),
NanNew<Uint32>((uint32_t) libusb_get_device_address(self->device)), CONST_PROP);
info.This()->ForceSet(V8SYM("busNumber"),
Nan::New<Uint32>((uint32_t) libusb_get_bus_number(self->device)).ToLocalChecked(), CONST_PROP);
info.This()->ForceSet(V8SYM("deviceAddress"),
Nan::New<Uint32>((uint32_t) libusb_get_device_address(self->device)).ToLocalChecked(), CONST_PROP);

Local<Object> v8dd = NanNew<Object>();
args.This()->ForceSet(V8SYM("deviceDescriptor"), v8dd, CONST_PROP);
Local<Object> v8dd = Nan::New<Object>().ToLocalChecked();
info.This()->ForceSet(V8SYM("deviceDescriptor"), v8dd, CONST_PROP);

struct libusb_device_descriptor dd;
CHECK_USB(libusb_get_device_descriptor(self->device, &dd));
Expand All @@ -87,14 +87,14 @@ static NAN_METHOD(deviceConstructor) {
uint8_t port_numbers[MAX_PORTS];
int ret = libusb_get_port_numbers(self->device, &port_numbers[0], MAX_PORTS);
CHECK_USB(ret);
Local<Array> array = NanNew<Array>(ret);
Local<Array> array = Nan::New<Array>(ret);
for (int i = 0; i < ret; ++ i) {
array->Set(i, NanNew(port_numbers[i]));
array->Set(i, Nan::New(port_numbers[i]).ToLocalChecked());
}

args.This()->ForceSet(V8SYM("portNumbers"), array, CONST_PROP);
info.This()->ForceSet(V8SYM("portNumbers"), array, CONST_PROP);

NanReturnValue(args.This());
info.GetReturnValue().Set(info.This());
}

NAN_METHOD(Device_GetConfigDescriptor) {
Expand All @@ -103,7 +103,7 @@ NAN_METHOD(Device_GetConfigDescriptor) {
libusb_config_descriptor* cdesc;
CHECK_USB(libusb_get_active_config_descriptor(self->device, &cdesc));

Local<Object> v8cdesc = NanNew<Object>();
Local<Object> v8cdesc = Nan::New<Object>().ToLocalChecked();

STRUCT_TO_V8(v8cdesc, *cdesc, bLength)
STRUCT_TO_V8(v8cdesc, *cdesc, bDescriptorType)
Expand All @@ -113,24 +113,24 @@ NAN_METHOD(Device_GetConfigDescriptor) {
STRUCT_TO_V8(v8cdesc, *cdesc, iConfiguration)
STRUCT_TO_V8(v8cdesc, *cdesc, bmAttributes)
// Libusb 1.0 typo'd bMaxPower as MaxPower
v8cdesc->ForceSet(V8STR("bMaxPower"), NanNew<Uint32>((uint32_t) cdesc->MaxPower), CONST_PROP);
v8cdesc->ForceSet(V8STR("bMaxPower"), Nan::New<Uint32>((uint32_t) cdesc->MaxPower).ToLocalChecked(), CONST_PROP);

v8cdesc->ForceSet(V8SYM("extra"), makeBuffer(cdesc->extra, cdesc->extra_length), CONST_PROP);

Local<Array> v8interfaces = NanNew<Array>(cdesc->bNumInterfaces);
Local<Array> v8interfaces = Nan::New<Array>(cdesc->bNumInterfaces);
v8cdesc->ForceSet(V8SYM("interfaces"), v8interfaces);

for (int idxInterface = 0; idxInterface < cdesc->bNumInterfaces; idxInterface++) {
int numAltSettings = cdesc->interface[idxInterface].num_altsetting;

Local<Array> v8altsettings = NanNew<Array>(numAltSettings);
Local<Array> v8altsettings = Nan::New<Array>(numAltSettings);
v8interfaces->Set(idxInterface, v8altsettings);

for (int idxAltSetting = 0; idxAltSetting < numAltSettings; idxAltSetting++) {
const libusb_interface_descriptor& idesc =
cdesc->interface[idxInterface].altsetting[idxAltSetting];

Local<Object> v8idesc = NanNew<Object>();
Local<Object> v8idesc = Nan::New<Object>().ToLocalChecked();
v8altsettings->Set(idxAltSetting, v8idesc);

STRUCT_TO_V8(v8idesc, idesc, bLength)
Expand All @@ -145,12 +145,12 @@ NAN_METHOD(Device_GetConfigDescriptor) {

v8idesc->ForceSet(V8SYM("extra"), makeBuffer(idesc.extra, idesc.extra_length), CONST_PROP);

Local<Array> v8endpoints = NanNew<Array>(idesc.bNumEndpoints);
Local<Array> v8endpoints = Nan::New<Array>(idesc.bNumEndpoints);
v8idesc->ForceSet(V8SYM("endpoints"), v8endpoints, CONST_PROP);
for (int idxEndpoint = 0; idxEndpoint < idesc.bNumEndpoints; idxEndpoint++){
const libusb_endpoint_descriptor& edesc = idesc.endpoint[idxEndpoint];

Local<Object> v8edesc = NanNew<Object>();
Local<Object> v8edesc = Nan::New<Object>().ToLocalChecked();
v8endpoints->Set(idxEndpoint, v8edesc);

STRUCT_TO_V8(v8edesc, edesc, bLength)
Expand All @@ -168,15 +168,14 @@ NAN_METHOD(Device_GetConfigDescriptor) {
}

libusb_free_config_descriptor(cdesc);
NanReturnValue(v8cdesc);
info.GetReturnValue().Set(v8cdesc);
}

NAN_METHOD(Device_Open) {
ENTER_METHOD(Device, 0);
if (!self->device_handle){
CHECK_USB(libusb_open(self->device, &self->device_handle));
}
NanReturnValue(NanUndefined());
}

NAN_METHOD(Device_Close) {
Expand All @@ -187,7 +186,6 @@ NAN_METHOD(Device_Close) {
}else{
THROW_ERROR("Can't close device with a pending request");
}
NanReturnValue(NanUndefined());
}

struct Req{
Expand All @@ -205,20 +203,20 @@ struct Req{
}

static void default_after(uv_work_t *req){
NanScope();
Nan::HandleScope scope;
auto baton = (Req*) req->data;

auto device = NanObjectWrapHandle(baton->device);
baton->device->unref();

if (!NanNew(baton->callback).IsEmpty()) {
if (!Nan::New(baton->callback).ToLocalChecked().IsEmpty()) {
Handle<Value> error = NanUndefined();
if (baton->errcode < 0){
error = libusbException(baton->errcode);
}
Handle<Value> argv[1] = {error};
TryCatch try_catch;
NanMakeCallback(device, NanNew(baton->callback), 1, argv);
Nan::MakeCallback(device, Nan::New(baton->callback).ToLocalChecked(), 1, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
Expand All @@ -235,7 +233,6 @@ struct Device_Reset: Req{
CALLBACK_ARG(0);
auto baton = new Device_Reset;
baton->submit(self, callback, &backend, &default_after);
NanReturnValue(NanUndefined());
}

static void backend(uv_work_t *req){
Expand All @@ -251,7 +248,7 @@ NAN_METHOD(IsKernelDriverActive) {
INT_ARG(interface, 0);
int r = libusb_kernel_driver_active(self->device_handle, interface);
CHECK_USB(r);
NanReturnValue(NanNew<Boolean>(r));
info.GetReturnValue().Set(Nan::New<Boolean>(r));
}

NAN_METHOD(DetachKernelDriver) {
Expand All @@ -260,7 +257,6 @@ NAN_METHOD(DetachKernelDriver) {
int interface;
INT_ARG(interface, 0);
CHECK_USB(libusb_detach_kernel_driver(self->device_handle, interface));
NanReturnValue(NanUndefined());
}

NAN_METHOD(AttachKernelDriver) {
Expand All @@ -269,7 +265,6 @@ NAN_METHOD(AttachKernelDriver) {
int interface;
INT_ARG(interface, 0);
CHECK_USB(libusb_attach_kernel_driver(self->device_handle, interface));
NanReturnValue(NanUndefined());
}

NAN_METHOD(Device_ClaimInterface) {
Expand All @@ -278,7 +273,6 @@ NAN_METHOD(Device_ClaimInterface) {
int interface;
INT_ARG(interface, 0);
CHECK_USB(libusb_claim_interface(self->device_handle, interface));
NanReturnValue(NanUndefined());
}

struct Device_ReleaseInterface: Req{
Expand All @@ -294,7 +288,6 @@ struct Device_ReleaseInterface: Req{
baton->interface = interface;
baton->submit(self, callback, &backend, &default_after);

NanReturnValue(NanUndefined());
}

static void backend(uv_work_t *req){
Expand All @@ -318,7 +311,6 @@ struct Device_SetInterface: Req{
baton->interface = interface;
baton->altsetting = altsetting;
baton->submit(self, callback, &backend, &default_after);
NanReturnValue(NanUndefined());
}

static void backend(uv_work_t *req){
Expand All @@ -329,8 +321,8 @@ struct Device_SetInterface: Req{
};

void Device::Init(Handle<Object> target){
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(deviceConstructor);
tpl->SetClassName(NanNew("Device"));
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(deviceConstructor).ToLocalChecked();
tpl->SetClassName(Nan::New("Device").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(tpl, "__getConfigDescriptor", Device_GetConfigDescriptor);
Expand All @@ -347,5 +339,5 @@ void Device::Init(Handle<Object> target){
NODE_SET_PROTOTYPE_METHOD(tpl, "__attachKernelDriver", AttachKernelDriver);

NanAssignPersistent(device_constructor, tpl);
target->Set(NanNew("Device"), tpl->GetFunction());
target->Set(Nan::New("Device").ToLocalChecked(), tpl->GetFunction());
}
20 changes: 7 additions & 13 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,36 @@

using namespace v8;

#define V8STR(str) NanNew<String>(str)
#define V8SYM(str) NanNew<String>(str)
#define V8STR(str) Nan::New<String>(str).ToLocalChecked()
#define V8SYM(str) Nan::New<String>(str).ToLocalChecked()

#define THROW_BAD_ARGS(FAIL_MSG) return NanThrowTypeError(FAIL_MSG);
#define THROW_ERROR(FAIL_MSG) return NanThrowError(FAIL_MSG);
#define THROW_BAD_ARGS(FAIL_MSG) return Nan::ThrowTypeError(FAIL_MSG);
#define THROW_ERROR(FAIL_MSG) return Nan::ThrowError(FAIL_MSG);
#define CHECK_N_ARGS(MIN_ARGS) if (args.Length() < MIN_ARGS) { THROW_BAD_ARGS("Expected " #MIN_ARGS " arguments") }

const PropertyAttribute CONST_PROP = static_cast<PropertyAttribute>(ReadOnly|DontDelete);

inline static void setConst(Handle<Object> obj, const char* const name, Handle<Value> value){
obj->ForceSet(NanNew<String>(name), value, CONST_PROP);
obj->ForceSet(Nan::New<String>(name).ToLocalChecked(), value, CONST_PROP);
}

#define ENTER_CONSTRUCTOR(MIN_ARGS) \
NanScope(); \
if (!args.IsConstructCall()) return NanThrowError("Must be called with `new`!"); \
if (!args.IsConstructCall()) return Nan::ThrowError("Must be called with `new`!"); \
CHECK_N_ARGS(MIN_ARGS);

#define ENTER_CONSTRUCTOR_POINTER(CLASS, MIN_ARGS) \
ENTER_CONSTRUCTOR(MIN_ARGS) \
if (!args.Length() || !args[0]->IsExternal()){ \
return NanThrowError("This type cannot be created directly!"); \
return Nan::ThrowError("This type cannot be created directly!"); \
} \
auto self = static_cast<CLASS*>(External::Cast(*args[0])->Value()); \
self->attach(args.This())

#define ENTER_METHOD(CLASS, MIN_ARGS) \
NanScope(); \
CHECK_N_ARGS(MIN_ARGS); \
auto self = node::ObjectWrap::Unwrap<CLASS>(args.This()); \
if (self == NULL) { THROW_BAD_ARGS(#CLASS " method called on invalid object") }

#define ENTER_ACCESSOR(CLASS) \
NanScope(); \
auto self = node::ObjectWrap::Unwrap<CLASS>(info.Holder());

#define UNWRAP_ARG(CLASS, NAME, ARGNO) \
if (!args[ARGNO]->IsObject()) \
THROW_BAD_ARGS("Parameter " #NAME " is not an object"); \
Expand Down
Loading