Skip to content

Commit

Permalink
Add Nan::To<v8::Function>() overload.
Browse files Browse the repository at this point in the history
Converting from v8::Value to v8::Function is a laborious and somewhat
unobvious process.  Add a convenience function to make it easier.
  • Loading branch information
bnoordhuis authored and kkoopa committed Aug 16, 2017
1 parent 79a26f7 commit b932806
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions nan_converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ template<typename T> struct ValueFactoryBase { typedef Maybe<T> return_t; };

template<typename T> struct ToFactory;

template<>
struct ToFactory<v8::Function> : ToFactoryBase<v8::Function> {
static inline return_t convert(v8::Local<v8::Value> val);
};

#define X(TYPE) \
template<> \
struct ToFactory<v8::TYPE> : ToFactoryBase<v8::TYPE> { \
Expand Down
6 changes: 6 additions & 0 deletions nan_converters_43_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#ifndef NAN_CONVERTERS_43_INL_H_
#define NAN_CONVERTERS_43_INL_H_

imp::ToFactory<v8::Function>::return_t
imp::ToFactory<v8::Function>::convert(v8::Local<v8::Value> val) {
if (val.IsEmpty() || !val->IsFunction()) return MaybeLocal<v8::Function>();
return MaybeLocal<v8::Function>(val.As<v8::Function>());
}

#define X(TYPE) \
imp::ToFactory<v8::TYPE>::return_t \
imp::ToFactory<v8::TYPE>::convert(v8::Local<v8::Value> val) { \
Expand Down
6 changes: 6 additions & 0 deletions nan_converters_pre_43_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#ifndef NAN_CONVERTERS_PRE_43_INL_H_
#define NAN_CONVERTERS_PRE_43_INL_H_

imp::ToFactory<v8::Function>::return_t
imp::ToFactory<v8::Function>::convert(v8::Local<v8::Value> val) {
if (val.IsEmpty() || !val->IsFunction()) return MaybeLocal<v8::Function>();
return MaybeLocal<v8::Function>(val.As<v8::Function>());
}

#define X(TYPE) \
imp::ToFactory<v8::TYPE>::return_t \
imp::ToFactory<v8::TYPE>::convert(v8::Local<v8::Value> val) { \
Expand Down
8 changes: 8 additions & 0 deletions test/cpp/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ NAN_METHOD(ToDetailString) {
info.GetReturnValue().Set(ToDetailString(info[0]).ToLocalChecked());
}

NAN_METHOD(ToFunction) {
info.GetReturnValue().Set(To<v8::Function>(info[0]).FromMaybe(info[1]));
}

NAN_METHOD(ToObject) {
info.GetReturnValue().Set(To<v8::Object>(info[0]).ToLocalChecked());
}
Expand Down Expand Up @@ -85,6 +89,10 @@ NAN_MODULE_INIT(Init) {
, New<v8::String>("toDetailString").ToLocalChecked()
, New<v8::FunctionTemplate>(ToDetailString)->GetFunction()
);
Set(target
, New<v8::String>("toFunction").ToLocalChecked()
, New<v8::FunctionTemplate>(ToFunction)->GetFunction()
);
Set(target
, New<v8::String>("toObject").ToLocalChecked()
, New<v8::FunctionTemplate>(ToObject)->GetFunction()
Expand Down
8 changes: 7 additions & 1 deletion test/js/converters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const test = require('tap').test
, bindings = require('bindings')({ module_root: testRoot, bindings: 'converters' });

test('converters', function (t) {
t.plan(28);
t.plan(32);

var converters = bindings;
t.type(converters.toBoolean, 'function');
t.type(converters.toNumber, 'function');
t.type(converters.toString, 'function');
t.type(converters.toDetailString, 'function');
t.type(converters.toFunction, 'function');
t.type(converters.toObject, 'function');
t.type(converters.toInteger, 'function');
t.type(converters.toUint32, 'function');
Expand All @@ -32,6 +33,7 @@ test('converters', function (t) {
t.equal(converters.toNumber(15.3), 15.3);
t.equal(converters.toString('sol'), 'sol');
t.equal(converters.toDetailString('sol'), 'sol');
t.equal(converters.toFunction(test), test);
t.strictDeepEqual(converters.toObject({prop : 'x'}), {prop : 'x'});
t.equal(converters.toInteger(12), 12);
t.equal(converters.toUint32(12), 12);
Expand All @@ -42,4 +44,8 @@ test('converters', function (t) {
t.equal(converters.integerValue(12), 12);
t.equal(converters.uint32Value(12), 12);
t.equal(converters.int32Value(-12), -12);

var conversionFailed = {};
t.equal(converters.toFunction(null, conversionFailed), conversionFailed);
t.equal(converters.toFunction({}, conversionFailed), conversionFailed);
});

0 comments on commit b932806

Please sign in to comment.