expose the node class constructor

This commit is contained in:
Yavor Georgiev 2016-10-11 00:38:32 +02:00
parent 3a8ed6cbe6
commit 245328009a
4 changed files with 41 additions and 39 deletions

View File

@ -5,6 +5,7 @@
"-Wno-missing-field-initializers",
"-Wno-return-type",
"-Wno-unused-result",
"-Wno-deprecated-declarations",
"-Wundef"
]
},

View File

@ -49,6 +49,8 @@ class ObjectWrap : public Nan::ObjectWrap {
return Nan::New(js_template);
}
static void construct(const v8::FunctionCallbackInfo<v8::Value>&);
static bool has_instance(v8::Isolate* isolate, const v8::Local<v8::Value> &value) {
return get_template()->HasInstance(value);
}
@ -73,27 +75,26 @@ class ObjectWrap : public Nan::ObjectWrap {
static v8::Local<v8::FunctionTemplate> create_template();
static void setup_method(v8::Local<v8::FunctionTemplate>, const std::string &, Nan::FunctionCallback);
static void setup_static_method(v8::Local<v8::FunctionTemplate>, const std::string &, Nan::FunctionCallback);
static void setup_method(v8::Local<v8::FunctionTemplate>, const std::string &, v8::FunctionCallback);
static void setup_static_method(v8::Local<v8::FunctionTemplate>, const std::string &, v8::FunctionCallback);
template<typename TargetType>
static void setup_property(v8::Local<TargetType>, const std::string &, const PropertyType &);
static void construct(Nan::NAN_METHOD_ARGS_TYPE);
static void get_indexes(Nan::NAN_INDEX_ENUMERATOR_ARGS_TYPE);
static void set_property(v8::Local<v8::String>, v8::Local<v8::Value>, Nan::NAN_PROPERTY_SETTER_ARGS_TYPE);
static void get_indexes(const v8::PropertyCallbackInfo<v8::Array>&);
static void set_property(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::PropertyCallbackInfo<v8::Value>&);
static void set_readonly_property(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_SETTER_ARGS_TYPE info) {
static void set_readonly_property(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) {
std::string message = std::string("Cannot assign to read only property '") + std::string(String(property)) + "'";
Nan::ThrowError(message.c_str());
}
static void set_readonly_index(uint32_t index, v8::Local<v8::Value> value, Nan::NAN_INDEX_SETTER_ARGS_TYPE info) {
static void set_readonly_index(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) {
std::string message = std::string("Cannot assign to read only index ") + util::to_string(index);
Nan::ThrowError(message.c_str());
}
static void get_nonexistent_property(v8::Local<v8::String>, Nan::NAN_PROPERTY_GETTER_ARGS_TYPE) {
static void get_nonexistent_property(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>&) {
// Do nothing. This function exists only to prevent a crash where it is used.
}
};
@ -109,7 +110,7 @@ class ObjectWrap<void> {
};
// This helper function is needed outside the scope of the ObjectWrap class as well.
static inline std::vector<v8::Local<v8::Value>> get_arguments(const Nan::FunctionCallbackInfo<v8::Value> &info) {
static inline std::vector<v8::Local<v8::Value>> get_arguments(const v8::FunctionCallbackInfo<v8::Value> &info) {
int count = info.Length();
std::vector<v8::Local<v8::Value>> arguments;
arguments.reserve(count);
@ -156,7 +157,7 @@ template<typename ClassType>
inline v8::Local<v8::FunctionTemplate> ObjectWrap<ClassType>::create_template() {
Nan::EscapableHandleScope scope;
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(construct);
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), construct);
v8::Local<v8::ObjectTemplate> instance_tpl = tpl->InstanceTemplate();
v8::Local<v8::String> name = Nan::New(s_class.name).ToLocalChecked();
@ -181,21 +182,21 @@ inline v8::Local<v8::FunctionTemplate> ObjectWrap<ClassType>::create_template()
if (s_class.index_accessor.getter) {
auto &index_accessor = s_class.index_accessor;
Nan::SetIndexedPropertyHandler(instance_tpl, index_accessor.getter, index_accessor.setter ?: set_readonly_index, 0, 0, get_indexes);
instance_tpl->SetIndexedPropertyHandler(index_accessor.getter, index_accessor.setter ?: set_readonly_index, 0, 0, get_indexes);
}
if (s_class.string_accessor.getter || s_class.index_accessor.getter || s_class.index_accessor.setter) {
// Use our own wrapper for the setter since we want to throw for negative indices.
auto &string_accessor = s_class.string_accessor;
Nan::SetNamedPropertyHandler(instance_tpl, string_accessor.getter ?: get_nonexistent_property, set_property, 0, 0, string_accessor.enumerator);
instance_tpl->SetNamedPropertyHandler(string_accessor.getter ?: get_nonexistent_property, set_property, 0, 0, string_accessor.enumerator);
}
return scope.Escape(tpl);
}
template<typename ClassType>
inline void ObjectWrap<ClassType>::setup_method(v8::Local<v8::FunctionTemplate> tpl, const std::string &name, Nan::FunctionCallback callback) {
inline void ObjectWrap<ClassType>::setup_method(v8::Local<v8::FunctionTemplate> tpl, const std::string &name, v8::FunctionCallback callback) {
v8::Local<v8::Signature> signature = Nan::New<v8::Signature>(tpl);
v8::Local<v8::FunctionTemplate> fn_tpl = Nan::New<v8::FunctionTemplate>(callback, v8::Local<v8::Value>(), signature);
v8::Local<v8::FunctionTemplate> fn_tpl = v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), callback, v8::Local<v8::Value>(), signature);
v8::Local<v8::String> fn_name = Nan::New(name).ToLocalChecked();
// The reason we use this rather than Nan::SetPrototypeMethod is DontEnum.
@ -204,8 +205,8 @@ inline void ObjectWrap<ClassType>::setup_method(v8::Local<v8::FunctionTemplate>
}
template<typename ClassType>
inline void ObjectWrap<ClassType>::setup_static_method(v8::Local<v8::FunctionTemplate> tpl, const std::string &name, Nan::FunctionCallback callback) {
v8::Local<v8::FunctionTemplate> fn_tpl = Nan::New<v8::FunctionTemplate>(callback);
inline void ObjectWrap<ClassType>::setup_static_method(v8::Local<v8::FunctionTemplate> tpl, const std::string &name, v8::FunctionCallback callback) {
v8::Local<v8::FunctionTemplate> fn_tpl = v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), callback);
v8::Local<v8::String> fn_name = Nan::New(name).ToLocalChecked();
tpl->Set(fn_name, fn_tpl, v8::PropertyAttribute::DontEnum);
@ -218,11 +219,11 @@ inline void ObjectWrap<ClassType>::setup_property(v8::Local<TargetType> target,
v8::Local<v8::String> prop_name = Nan::New(name).ToLocalChecked();
v8::PropertyAttribute attributes = v8::PropertyAttribute(v8::DontEnum | v8::DontDelete);
Nan::SetAccessor(target, prop_name, property.getter, property.setter ?: set_readonly_property, v8::Local<v8::Value>(), v8::DEFAULT, attributes);
target->SetAccessor(prop_name, property.getter, property.setter ?: set_readonly_property, v8::Local<v8::Value>(), v8::DEFAULT, attributes);
}
template<typename ClassType>
inline void ObjectWrap<ClassType>::construct(Nan::NAN_METHOD_ARGS_TYPE info) {
inline void ObjectWrap<ClassType>::construct(const v8::FunctionCallbackInfo<v8::Value>& info) {
if (!info.IsConstructCall()) {
Nan::ThrowError("Constructor must be called with new");
}
@ -248,7 +249,7 @@ inline void ObjectWrap<ClassType>::construct(Nan::NAN_METHOD_ARGS_TYPE info) {
}
template<typename ClassType>
inline void ObjectWrap<ClassType>::get_indexes(Nan::NAN_INDEX_ENUMERATOR_ARGS_TYPE info) {
inline void ObjectWrap<ClassType>::get_indexes(const v8::PropertyCallbackInfo<v8::Array>& info) {
uint32_t length;
try {
length = Object::validated_get_length(info.GetIsolate(), info.This());
@ -267,7 +268,7 @@ inline void ObjectWrap<ClassType>::get_indexes(Nan::NAN_INDEX_ENUMERATOR_ARGS_TY
}
template<typename ClassType>
inline void ObjectWrap<ClassType>::set_property(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_PROPERTY_SETTER_ARGS_TYPE info) {
inline void ObjectWrap<ClassType>::set_property(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) {
if (s_class.index_accessor.getter || s_class.index_accessor.setter) {
try {
// Negative indices are passed into this string property interceptor, so check for them here.
@ -294,7 +295,7 @@ template<typename ClassType>
class ObjectWrap<node::Types, ClassType> : public node::ObjectWrap<ClassType> {};
template<node::MethodType F>
void wrap(Nan::NAN_METHOD_ARGS_TYPE info) {
void wrap(const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
node::ReturnValue return_value(info.GetReturnValue());
auto arguments = node::get_arguments(info);
@ -308,7 +309,7 @@ void wrap(Nan::NAN_METHOD_ARGS_TYPE info) {
}
template<node::PropertyType::GetterType F>
void wrap(v8::Local<v8::String> property, Nan::NAN_GETTER_ARGS_TYPE info) {
void wrap(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
node::ReturnValue return_value(info.GetReturnValue());
try {
@ -320,7 +321,7 @@ void wrap(v8::Local<v8::String> property, Nan::NAN_GETTER_ARGS_TYPE info) {
}
template<node::PropertyType::SetterType F>
void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_SETTER_ARGS_TYPE info) {
void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) {
v8::Isolate* isolate = info.GetIsolate();
try {
F(isolate, info.This(), value);
@ -331,7 +332,7 @@ void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_S
}
template<node::IndexPropertyType::GetterType F>
void wrap(uint32_t index, Nan::NAN_INDEX_GETTER_ARGS_TYPE info) {
void wrap(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
node::ReturnValue return_value(info.GetReturnValue());
try {
@ -347,7 +348,7 @@ void wrap(uint32_t index, Nan::NAN_INDEX_GETTER_ARGS_TYPE info) {
}
template<node::IndexPropertyType::SetterType F>
void wrap(uint32_t index, v8::Local<v8::Value> value, Nan::NAN_INDEX_SETTER_ARGS_TYPE info) {
void wrap(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
try {
if (F(isolate, info.This(), index, value)) {
@ -361,7 +362,7 @@ void wrap(uint32_t index, v8::Local<v8::Value> value, Nan::NAN_INDEX_SETTER_ARGS
}
template<node::StringPropertyType::GetterType F>
void wrap(v8::Local<v8::String> property, Nan::NAN_PROPERTY_GETTER_ARGS_TYPE info) {
void wrap(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
node::ReturnValue return_value(info.GetReturnValue());
try {
@ -373,7 +374,7 @@ void wrap(v8::Local<v8::String> property, Nan::NAN_PROPERTY_GETTER_ARGS_TYPE inf
}
template<node::StringPropertyType::SetterType F>
void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_PROPERTY_SETTER_ARGS_TYPE info) {
void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
try {
if (F(isolate, info.This(), property, value)) {
@ -387,7 +388,7 @@ void wrap(v8::Local<v8::String> property, v8::Local<v8::Value> value, Nan::NAN_P
}
template<node::StringPropertyType::EnumeratorType F>
void wrap(Nan::NAN_PROPERTY_ENUMERATOR_ARGS_TYPE info) {
void wrap(const v8::PropertyCallbackInfo<v8::Array>& info) {
auto names = F(info.GetIsolate(), info.This());
int count = (int)names.size();
v8::Local<v8::Array> array = Nan::New<v8::Array>(count);

View File

@ -25,10 +25,10 @@ namespace js {
template<>
class ReturnValue<node::Types> {
Nan::ReturnValue<v8::Value> m_value;
v8::ReturnValue<v8::Value> m_value;
public:
ReturnValue(Nan::ReturnValue<v8::Value> value) : m_value(value) {}
ReturnValue(v8::ReturnValue<v8::Value> value) : m_value(value) {}
void set(const v8::Local<v8::Value> &value) {
m_value.Set(value);

View File

@ -46,15 +46,15 @@ struct Types {
using Function = v8::Local<v8::Function>;
using HandleScope = Nan::HandleScope;
using ConstructorCallback = Nan::FunctionCallback;
using FunctionCallback = Nan::FunctionCallback;
using PropertyGetterCallback = Nan::GetterCallback;
using PropertySetterCallback = Nan::SetterCallback;
using IndexPropertyGetterCallback = Nan::IndexGetterCallback;
using IndexPropertySetterCallback = Nan::IndexSetterCallback;
using StringPropertyGetterCallback = Nan::PropertyGetterCallback;
using StringPropertySetterCallback = Nan::PropertySetterCallback;
using StringPropertyEnumeratorCallback = Nan::PropertyEnumeratorCallback;
using ConstructorCallback = v8::FunctionCallback;
using FunctionCallback = v8::FunctionCallback;
using PropertyGetterCallback = v8::AccessorGetterCallback;
using PropertySetterCallback = v8::AccessorSetterCallback;
using IndexPropertyGetterCallback = v8::IndexedPropertyGetterCallback;
using IndexPropertySetterCallback = v8::IndexedPropertySetterCallback;
using StringPropertyGetterCallback = v8::NamedPropertyGetterCallback;
using StringPropertySetterCallback = v8::NamedPropertySetterCallback;
using StringPropertyEnumeratorCallback = v8::NamedPropertyEnumeratorCallback;
};
template<typename ClassType>