diff --git a/native_mate/constructor.cc b/native_mate/constructor.cc new file mode 100644 index 00000000000..fca79212776 --- /dev/null +++ b/native_mate/constructor.cc @@ -0,0 +1,42 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE.chromium file. + +#include "native_mate/wrappable_class.h" + +#include "native_mate/function_template.h" +#include "native_mate/object_template_builder.h" + +namespace mate { + +Constructor::Constructor(const base::StringPiece& name) : name_(name) { +} + +Constructor::~Constructor() { + constructor_.Reset(); +} + +v8::Handle Constructor::GetFunction(v8::Isolate* isolate) { + if (constructor_.IsEmpty()) { + v8::Local constructor = CreateFunctionTemplate( + isolate, + base::Bind(&Constructor::New, base::Unretained(this))); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(StringToV8(isolate, name_)); + SetPrototype(isolate, constructor->PrototypeTemplate()); + + constructor_.Reset(isolate, constructor); + } + + return MATE_PERSISTENT_TO_LOCAL( + v8::FunctionTemplate, isolate, constructor_)->GetFunction(); +} + +void Constructor::New() { +} + +void Constructor::SetPrototype(v8::Isolate* isolate, + v8::Handle prototype) { +} + +} // namespace mate diff --git a/native_mate/constructor.h b/native_mate/constructor.h new file mode 100644 index 00000000000..45786fdb341 --- /dev/null +++ b/native_mate/constructor.h @@ -0,0 +1,34 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE.chromium file. + +#ifndef NATIVE_MATE_WRAPPABLE_CLASS_H_ +#define NATIVE_MATE_WRAPPABLE_CLASS_H_ + +#include "native_mate/wrappable.h" + +namespace mate { + +class Constructor { + public: + v8::Handle GetFunction(v8::Isolate* isolate); + + protected: + Constructor(const base::StringPiece& name); + virtual ~Constructor(); + + virtual void New() = 0; + + virtual void SetPrototype(v8::Isolate* isolate, + v8::Handle prototype); + + private: + base::StringPiece name_; + v8::Persistent constructor_; + + DISALLOW_COPY_AND_ASSIGN(Constructor); +}; + +} // namespace mate + +#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_ diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index e4037c0bb5e..b6b80789887 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -56,6 +56,10 @@ class Wrappable { virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); + // Override to change how the v8 wrapper is built (by default it was built + // from the ObjectTemplateBuilder. + // virtual v8::Handle BuildObject(); + private: static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, Wrappable); diff --git a/native_mate_files.gypi b/native_mate_files.gypi index 8d3952bdd8d..934c91f2323 100644 --- a/native_mate_files.gypi +++ b/native_mate_files.gypi @@ -4,6 +4,8 @@ 'native_mate/arguments.cc', 'native_mate/arguments.h', 'native_mate/compat.h', + 'native_mate/constructor.cc', + 'native_mate/constructor.h', 'native_mate/converter.cc', 'native_mate/converter.h', 'native_mate/dictionary.cc',