GetConstructor should init default constructor

This commit is contained in:
Cheng Zhao 2016-08-02 17:20:41 +09:00
parent 2d26eebca8
commit 3a7a6efffb
3 changed files with 16 additions and 10 deletions

View file

@ -148,6 +148,11 @@ bool Converter<std::string>::FromV8(Isolate* isolate, Local<Value> val,
return true;
}
Local<Value> Converter<Local<Function>>::ToV8(Isolate* isolate,
Local<Function> val) {
return val;
}
bool Converter<Local<Function> >::FromV8(Isolate* isolate, Local<Value> val,
Local<Function>* out) {
if (!val->IsFunction())

View file

@ -144,6 +144,8 @@ std::string V8ToString(v8::Local<v8::Value> value);
template<>
struct Converter<v8::Local<v8::Function> > {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Function> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Function>* out);

View file

@ -36,13 +36,7 @@ class Wrappable : public WrappableBase {
templ_ = new v8::Global<v8::FunctionTemplate>(isolate, constructor);
}
static v8::Local<v8::Object> GetConstructor(v8::Isolate* isolate) {
return v8::Local<v8::FunctionTemplate>::New(isolate, *templ_)->GetFunction();
}
protected:
// Init the class with T::BuildPrototype.
void Init(v8::Isolate* isolate) {
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate) {
// Fill the object template.
if (!templ_) {
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
@ -50,12 +44,17 @@ class Wrappable : public WrappableBase {
T::BuildPrototype(isolate, templ->PrototypeTemplate());
templ_ = new v8::Global<v8::FunctionTemplate>(isolate, templ);
}
return v8::Local<v8::FunctionTemplate>::New(isolate, *templ_);
}
protected:
// Init the class with T::BuildPrototype.
void Init(v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> templ = GetConstructor(isolate);
v8::Local<v8::Object> wrapper;
v8::Local<v8::FunctionTemplate> templ =
v8::Local<v8::FunctionTemplate>::New(isolate, *templ_);
// |wrapper| may be empty in some extreme cases, e.g., when
// Object.prototype.constructor is overwritten.
v8::Local<v8::Object> wrapper;
if (!templ->InstanceTemplate()->NewInstance(
isolate->GetCurrentContext()).ToLocal(&wrapper)) {
// The current wrappable object will be no longer managed by V8. Delete