Skip to content

Commit 623ed8e

Browse files
committed
Pass Endpoint as 'this' to Endpoint.transfer callback.
1 parent 0528375 commit 623ed8e

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/endpoint.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ namespace NodeUsb {
188188
NodeUsb::Transfer* t = Transfer::newTransfer(
189189
self->transfer_type,
190190
self->v8device,
191+
args.This(),
191192
self->descriptor->bEndpointAddress,
192193
buf,
193194
length,

src/transfer.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ using namespace NodeUsb;
66

77
UVQueue<Transfer*> Transfer::completionQueue(Transfer::handleCompletion);
88

9-
Transfer::Transfer(Handle<Object> _device, Handle<Function> _callback):
9+
Transfer::Transfer(Handle<Object> _device, Handle<Object> _v8this, Handle<Function> _callback):
1010
v8device(Persistent<Object>::New(_device)),
11+
v8this(Persistent<Object>::New(_v8this)),
1112
v8callback(Persistent<Function>::New(_callback)),
1213
device(ObjectWrap::Unwrap<Device>(_device)){
1314
transfer = libusb_alloc_transfer(0);
1415
}
1516

1617
Transfer::~Transfer(){
18+
v8this.Dispose();
1719
v8device.Dispose();
1820
v8callback.Dispose();
1921
free(transfer->buffer);
@@ -31,7 +33,7 @@ Transfer* Transfer::newControlTransfer(Handle<Object> device,
3133
uint16_t wLength,
3234
unsigned timeout,
3335
Handle<Function> callback){
34-
Transfer *t = new Transfer(device, callback);
36+
Transfer *t = new Transfer(device, device, callback);
3537

3638
uint8_t *buffer = (uint8_t*) malloc(LIBUSB_CONTROL_SETUP_SIZE+wLength);
3739
libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex, wLength);
@@ -44,12 +46,13 @@ Transfer* Transfer::newControlTransfer(Handle<Object> device,
4446

4547
Transfer* Transfer::newTransfer(libusb_transfer_type type,
4648
Handle<Object> device,
49+
Handle<Object> v8endpoint,
4750
uint8_t endpoint,
4851
unsigned char *data,
4952
int length,
5053
unsigned int timeout,
5154
Handle<Function> callback){
52-
Transfer *t = new Transfer(device, callback);
55+
Transfer *t = new Transfer(device, v8endpoint, callback);
5356
uint8_t *buffer = (uint8_t*) malloc(length);
5457
if (data) memcpy(buffer, data, length);
5558
t->direction = data?LIBUSB_ENDPOINT_OUT:LIBUSB_ENDPOINT_IN;
@@ -80,7 +83,7 @@ void Transfer::handleCompletion(Transfer* t){
8083
}
8184
}
8285

83-
doTransferCallback(t->v8callback, Context::GetCurrent()->Global(), t->transfer->status, buffer, length);
86+
doTransferCallback(t->v8callback, t->v8this, t->transfer->status, buffer, length);
8487

8588
uv_unref(uv_default_loop());
8689
delete t;

src/transfer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace NodeUsb {
2323

2424
static Transfer* newTransfer(libusb_transfer_type type,
2525
Handle<Object> device,
26+
Handle<Object> v8endpoint,
2627
uint8_t endpoint,
2728
unsigned char *data,
2829
int length,
@@ -37,10 +38,11 @@ namespace NodeUsb {
3738
static UVQueue<Transfer*> completionQueue;
3839

3940
protected:
40-
Transfer(Handle<Object> _device, Handle<Function> _callback);
41+
Transfer(Handle<Object> _device, Handle<Object> _v8this, Handle<Function> _callback);
4142

4243
libusb_transfer* transfer;
4344
Persistent<Object> v8device;
45+
Persistent<Object> v8this;
4446
Persistent<Function> v8callback;
4547
Device* device;
4648
uint32_t direction;

0 commit comments

Comments
 (0)