Turn Wrappable into using FunctionTemplate

This commit is contained in:
Cheng Zhao 2016-08-02 15:56:47 +09:00
parent ab62755a88
commit d55c3c5030

View file

@ -49,7 +49,6 @@ class WrappableBase {
protected: protected:
// Called after the "_init" method gets called in JavaScript. // Called after the "_init" method gets called in JavaScript.
// FIXME(zcbenz): Should remove this.
virtual void AfterInit(v8::Isolate* isolate) {} virtual void AfterInit(v8::Isolate* isolate) {}
// Bind the C++ class to the JS wrapper. // Bind the C++ class to the JS wrapper.
@ -80,17 +79,18 @@ class Wrappable : public WrappableBase {
void Init(v8::Isolate* isolate) { void Init(v8::Isolate* isolate) {
// Fill the object template. // Fill the object template.
if (!templ_) { if (!templ_) {
v8::Local<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate); v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
T::BuildPrototype(isolate, templ); T::BuildPrototype(isolate, templ->PrototypeTemplate());
templ_ = new v8::Global<v8::ObjectTemplate>(isolate, templ); templ_ = new v8::Global<v8::FunctionTemplate>(isolate, templ);
} }
v8::Local<v8::Object> wrapper; v8::Local<v8::Object> wrapper;
v8::Local<v8::ObjectTemplate> templ = v8::Local<v8::ObjectTemplate>::New( v8::Local<v8::FunctionTemplate> templ =
isolate, *templ_); v8::Local<v8::FunctionTemplate>::New(isolate, *templ_);
// |wrapper| may be empty in some extreme cases, e.g., when // |wrapper| may be empty in some extreme cases, e.g., when
// Object.prototype.constructor is overwritten. // Object.prototype.constructor is overwritten.
if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) { if (!templ->PrototypeTemplate()->NewInstance(
isolate->GetCurrentContext()).ToLocal(&wrapper)) {
// The current wrappable object will be no longer managed by V8. Delete // The current wrappable object will be no longer managed by V8. Delete
// this now. // this now.
delete this; delete this;
@ -100,14 +100,14 @@ class Wrappable : public WrappableBase {
} }
private: private:
static v8::Global<v8::ObjectTemplate>* templ_; // Leaked on purpose static v8::Global<v8::FunctionTemplate>* templ_; // Leaked on purpose
DISALLOW_COPY_AND_ASSIGN(Wrappable); DISALLOW_COPY_AND_ASSIGN(Wrappable);
}; };
// static // static
template<typename T> template<typename T>
v8::Global<v8::ObjectTemplate>* Wrappable<T>::templ_ = nullptr; v8::Global<v8::FunctionTemplate>* Wrappable<T>::templ_ = nullptr;
// This converter handles any subclass of Wrappable. // This converter handles any subclass of Wrappable.
template <typename T> template <typename T>