Skip to content

Commit

Permalink
Fix for v8::Boolean from gboolean (which is an int)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Nov 2, 2014
1 parent 7eb5172 commit 9d8b270
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
34 changes: 16 additions & 18 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ NAN_INLINE uint32_t NanUInt32OptionValue(
: def;
}

template<typename T>
v8::Local<T> NanNew(v8::Handle<T>);

template<typename T>
NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Handle<T> val) {
return NanNew(val);
}

template<typename T>
NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Local<T> val) {
return val;
}

#if (NODE_MODULE_VERSION > 0x000B)
// Node 0.11+ (0.11.3 and below won't compile with these)

Expand Down Expand Up @@ -498,17 +511,7 @@ NAN_INLINE uint32_t NanUInt32OptionValue(
# define NanEscapableScope() \
v8::EscapableHandleScope scope(v8::Isolate::GetCurrent())

template<typename T>
NAN_INLINE v8::Local<T> _NanEscapeScopeHelper(v8::Handle<T> val) {
return NanNew(val);
}

template<typename T>
NAN_INLINE v8::Local<T> _NanEscapeScopeHelper(v8::Local<T> val) {
return val;
}

# define NanEscapeScope(val) scope.Escape(_NanEscapeScopeHelper(val))
# define NanEscapeScope(val) scope.Escape(_NanEnsureLocal(val))
# define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent())
# define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent())
# define NanReturnValue(value) return args.GetReturnValue().Set(value)
Expand Down Expand Up @@ -1077,12 +1080,12 @@ NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(

template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg) {
return T::New(arg);
return _NanEnsureLocal(T::New(arg));
}

template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(P arg, int length) {
return T::New(arg, length);
return _NanEnsureLocal(T::New(arg, length));
}

template<typename T>
Expand Down Expand Up @@ -1129,11 +1132,6 @@ NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
return v8::Date::New(time).As<v8::Date>();
}

template<>
NAN_INLINE v8::Local<v8::Boolean> NanNew<v8::Boolean>(bool value) {
return v8::Local<v8::Boolean>::New(v8::Boolean::New(value));
}

typedef v8::Script NanUnboundScript;
typedef v8::Script NanBoundScript;

Expand Down
10 changes: 10 additions & 0 deletions test/cpp/news.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ NAN_METHOD(NewBoolean) {
NanReturnValue(NanNew<v8::Boolean>(true));
}

// #212
NAN_METHOD(NewBoolean2) {
NanScope();
NanReturnValue(NanNew<v8::Boolean>(1));
}

void Init(v8::Handle<v8::Object> target) {
target->Set(
NanNew<v8::String>("newNumber")
Expand Down Expand Up @@ -255,6 +261,10 @@ void Init(v8::Handle<v8::Object> target) {
NanNew<v8::String>("newBoolean")
, NanNew<v8::FunctionTemplate>(NewBoolean)->GetFunction()
);
target->Set(
NanNew<v8::String>("newBoolean2")
, NanNew<v8::FunctionTemplate>(NewBoolean2)->GetFunction()
);
}

NODE_MODULE(news, Init)
4 changes: 3 additions & 1 deletion test/js/news-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const test = require('tap').test
, bindings = require('bindings')({ module_root: testRoot, bindings: 'news' });

test('news', function (t) {
t.plan(50);
t.plan(52);
t.type(bindings.newNumber, 'function');
t.type(bindings.newPositiveInteger, 'function');
t.type(bindings.newNegativeInteger, 'function');
Expand All @@ -37,6 +37,7 @@ test('news', function (t) {
t.type(bindings.newDate, 'function');
t.type(bindings.newArray, 'function');
t.type(bindings.newBoolean, 'function');
t.type(bindings.newBoolean2, 'function');

t.equal(bindings.newNumber(), 0.5);
t.equal(bindings.newPositiveInteger(), 1);
Expand All @@ -63,4 +64,5 @@ test('news', function (t) {
t.deepEquals(bindings.newDate(), new Date(1337));
t.deepEquals(bindings.newArray(), []);
t.equal(bindings.newBoolean(), true);
t.equal(bindings.newBoolean2(), true);
});

0 comments on commit 9d8b270

Please sign in to comment.