diff --git a/native_mate/constructor.h b/native_mate/constructor.h index 65c16c33598..278db078beb 100644 --- a/native_mate/constructor.h +++ b/native_mate/constructor.h @@ -123,7 +123,7 @@ inline WrappableBase* InvokeFactory( } // namespace internal -template +template class Constructor { public: typedef base::Callback WrappableFactoryFunction; @@ -140,6 +140,7 @@ class Constructor { isolate, base::Bind(&Constructor::New, factory)); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(StringToV8(isolate, name_)); + T::BuildPrototype(isolate, constructor->PrototypeTemplate()); MATE_PERSISTENT_ASSIGN(v8::FunctionTemplate, isolate, constructor_, constructor); } @@ -167,9 +168,7 @@ class Constructor { } } - if (object) - object->InitWith(isolate, args->GetThis()); - else + if (!object) args->ThrowError(); MATE_METHOD_RETURN_UNDEFINED(); @@ -193,8 +192,7 @@ v8::Local CreateConstructor( const base::StringPiece& name, const base::Callback& callback) { v8::Local constructor = - Constructor(name).GetFunctionTemplate(isolate, callback); - T::BuildPrototype(isolate, constructor->PrototypeTemplate()); + Constructor(name).GetFunctionTemplate(isolate, callback); return constructor->GetFunction(); } diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index 6ec6420e117..934cff7ee05 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -47,14 +47,15 @@ class WrappableBase { // Returns the Isolate this object is created in. v8::Isolate* isolate() const { return isolate_; } - // Bind the C++ class to the JS wrapper. - void InitWith(v8::Isolate* isolate, v8::Local wrapper); - protected: // Called after the "_init" method gets called in JavaScript. // FIXME(zcbenz): Should remove this. virtual void AfterInit(v8::Isolate* isolate) {} + // Bind the C++ class to the JS wrapper. + // This method should only be called by classes using Constructor. + void InitWith(v8::Isolate* isolate, v8::Local wrapper); + private: friend struct internal::Destroyable; @@ -74,6 +75,7 @@ class Wrappable : public WrappableBase { public: Wrappable() {} + protected: // Init the class with T::BuildPrototype. void Init(v8::Isolate* isolate) { // Fill the object template.