Call the user call Init in Constructor

This commit is contained in:
Cheng Zhao 2016-08-02 15:14:48 +09:00
parent d9bfe6a49d
commit bd20bde1a2
2 changed files with 9 additions and 9 deletions

View file

@ -123,7 +123,7 @@ inline WrappableBase* InvokeFactory(
} // namespace internal
template<typename Sig>
template<typename T, typename Sig>
class Constructor {
public:
typedef base::Callback<Sig> 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<v8::Function> CreateConstructor(
const base::StringPiece& name,
const base::Callback<Sig>& callback) {
v8::Local<v8::FunctionTemplate> constructor =
Constructor<Sig>(name).GetFunctionTemplate(isolate, callback);
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
Constructor<T, Sig>(name).GetFunctionTemplate(isolate, callback);
return constructor->GetFunction();
}

View file

@ -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<v8::Object> 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<v8::Object> 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.