Skip to content

Commit

Permalink
Update to "nan" v2, adds io.js v3 support
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 9418dd7
Author: unbornchikken <[email protected]>
Date:   Sat Aug 29 21:13:45 2015 +0200

    license extended to current year

commit 72eed31
Author: unbornchikken <[email protected]>
Date:   Thu Aug 27 09:04:18 2015 +0200

    references fixed

commit a4b74e3
Author: unbornchikken <[email protected]>
Date:   Thu Aug 6 21:49:33 2015 +0200

    done

commit 6f024fa
Author: unbornchikken <[email protected]>
Date:   Mon Aug 3 12:26:22 2015 +0200

    whitespace fixes

commit a6d494e
Author: unbornchikken <[email protected]>
Date:   Mon Aug 3 10:26:54 2015 +0200

    ffi_test.cc wrap pointer fixd

commit 01ea563
Author: Benjamin Byholm <[email protected]>
Date:   Sun Aug 2 15:25:10 2015 +0300

    Fix errors

commit 99b4983
Author: unbornchikken <[email protected]>
Date:   Thu Aug 27 08:49:14 2015 +0200

    Merge master
    Fix errors
    Merge pull request #1 from kkoopa/nan2

    Fix errors
    ffi_test.cc wrap pointer fixd
    whitespace fixes
    done

commit dc0e555
Author: unbornchikken <[email protected]>
Date:   Sat Aug 1 12:48:51 2015 +0200

    handle scope crash

commit 66f877d
Author: unbornchikken <[email protected]>
Date:   Fri Jul 31 21:50:15 2015 +0200

    nan2 ok

commit 8ae22da
Author: unbornchikken <[email protected]>
Date:   Thu Aug 27 09:14:17 2015 +0200

    nan2 support impl begin

Fixes node-ffi#219.
Refs node-ffi#221.
Closes node-ffi#231.
  • Loading branch information
unbornchikken authored and TooTallNate committed Aug 31, 2015
1 parent f954bb7 commit 34879e6
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 190 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"dependencies": {
"bindings": "~1.2.0",
"debug": "2",
"ref": "*",
"ref-struct": "*",
"nan": "1"
"ref": "1",
"ref-struct": "1",
"nan": "2"
},
"devDependencies": {
"fs-extra": "^0.23.1",
"mocha": "*",
"ref-array": "*"
"ref-array": "1"
},
"scripts": {
"test": "node-gyp rebuild --directory test && mocha -gc --reporter spec"
Expand Down
63 changes: 31 additions & 32 deletions src/callback_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,34 @@ void closure_pointer_cb(char *data, void *hint) {
*/

void CallbackInfo::DispatchToV8(callback_info *info, void *retval, void **parameters, bool dispatched) {
NanScope();
Nan::HandleScope scope;

const char* errorMessage = "ffi fatal: callback has been garbage collected!";
static const char* errorMessage = "ffi fatal: callback has been garbage collected!";

if (info->function == NULL) {
// throw an error instead of segfaulting.
// see: https://github.com/rbranson/node-ffi/issues/72
if (dispatched) {
Handle<Value> errorFunctionArgv[1];
errorFunctionArgv[0] = NanNew<String>(errorMessage);
Local<Value> errorFunctionArgv[1];
errorFunctionArgv[0] = Nan::New<String>(errorMessage).ToLocalChecked();
info->errorFunction->Call(1, errorFunctionArgv);
}
else {
NanThrowError(errorMessage);
Nan::ThrowError(errorMessage);
}
} else {
// invoke the registered callback function
Handle<Value> functionArgv[2];
Local<Value> functionArgv[2];
functionArgv[0] = WrapPointer((char *)retval, info->resultSize);
functionArgv[1] = WrapPointer((char *)parameters, sizeof(char *) * info->argc);
Handle<Value> e = info->function->Call(2, functionArgv);
Local<Value> e = info->function->Call(2, functionArgv);
if (!e->IsUndefined()) {
if (dispatched) {
Handle<Value> errorFunctionArgv[1];
Local<Value> errorFunctionArgv[1];
errorFunctionArgv[0] = e;
info->errorFunction->Call(1, errorFunctionArgv);
} else {
NanThrowError(e);
Nan::ThrowError(e);
}
}
}
Expand All @@ -103,56 +103,54 @@ void CallbackInfo::WatcherCallback(uv_async_t *w, int revents) {
*/

NAN_METHOD(CallbackInfo::Callback) {
NanScope();

if (args.Length() != 5) {
if (info.Length() != 5) {
return THROW_ERROR_EXCEPTION("Not enough arguments.");
}

// Args: cif pointer, JS function
// TODO: Check args
ffi_cif *cif = (ffi_cif *)Buffer::Data(args[0]->ToObject());
size_t resultSize = args[1]->Int32Value();
int argc = args[2]->Int32Value();
Local<Function> errorReportCallback = Local<Function>::Cast(args[3]);
Local<Function> callback = Local<Function>::Cast(args[4]);
ffi_cif *cif = (ffi_cif *)Buffer::Data(info[0]->ToObject());
size_t resultSize = info[1]->Int32Value();
int argc = info[2]->Int32Value();
Local<Function> errorReportCallback = Local<Function>::Cast(info[3]);
Local<Function> callback = Local<Function>::Cast(info[4]);

callback_info *info;
callback_info *cbInfo;
ffi_status status;
void *code;

info = reinterpret_cast<callback_info *>(ffi_closure_alloc(sizeof(callback_info), &code));
cbInfo = reinterpret_cast<callback_info *>(ffi_closure_alloc(sizeof(callback_info), &code));

if (!info) {
if (!cbInfo) {
return THROW_ERROR_EXCEPTION("ffi_closure_alloc() Returned Error");
}

info->resultSize = resultSize;
info->argc = argc;
info->errorFunction = new NanCallback(errorReportCallback);
info->function = new NanCallback(callback);
cbInfo->resultSize = resultSize;
cbInfo->argc = argc;
cbInfo->errorFunction = new Nan::Callback(errorReportCallback);
cbInfo->function = new Nan::Callback(callback);

// store a reference to the callback function pointer
// (not sure if this is actually needed...)
info->code = code;
cbInfo->code = code;

//CallbackInfo *self = new CallbackInfo(callback, closure, code, argc);

status = ffi_prep_closure_loc(
(ffi_closure *)info,
(ffi_closure *)cbInfo,
cif,
Invoke,
(void *)info,
(void *)cbInfo,
code
);

if (status != FFI_OK) {
ffi_closure_free(info);
ffi_closure_free(cbInfo);
return THROW_ERROR_EXCEPTION_WITH_STATUS_CODE("ffi_prep_closure() Returned Error", status);
}

NanReturnValue(
NanNewBufferHandle((char *)code, sizeof(void *), closure_pointer_cb, info)
info.GetReturnValue().Set(
Nan::NewBuffer((char *)code, sizeof(void*), closure_pointer_cb, cbInfo).ToLocalChecked()
);
}

Expand Down Expand Up @@ -208,9 +206,10 @@ void CallbackInfo::Invoke(ffi_cif *cif, void *retval, void **parameters, void *u
*/

void CallbackInfo::Initialize(Handle<Object> target) {
NanScope();
Nan::HandleScope scope;

NODE_SET_METHOD(target, "Callback", Callback);
Nan::Set(target, Nan::New<String>("Callback").ToLocalChecked(),
Nan::New<FunctionTemplate>(Callback)->GetFunction());

// initialize our threaded invokation stuff
#ifdef WIN32
Expand Down
Loading

0 comments on commit 34879e6

Please sign in to comment.