--- /dev/null
+#include <node.h>
+#include <node_object_wrap.h>
+
+namespace zetjs {
+
+using v8::Context;
+using v8::Function;
+using v8::FunctionCallbackInfo;
+using v8::FunctionTemplate;
+using v8::Isolate;
+using v8::Local;
+using v8::Number;
+using v8::Object;
+using v8::Persistent;
+using v8::String;
+using v8::Value;
+
+class MyObject : public node::ObjectWrap {
+ public:
+ static void Init(v8::Local<v8::Object> exports);
+
+ private:
+ explicit MyObject(double value = 0);
+ ~MyObject();
+
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static v8::Persistent<v8::Function> constructor;
+ double value_;
+};
+
+Persistent<Function> MyObject::constructor;
+
+MyObject::MyObject(double value) : value_(value) {
+}
+
+MyObject::~MyObject() {
+}
+
+void MyObject::Init(Local<Object> exports) {
+ Isolate* isolate = exports->GetIsolate();
+
+ // Prepare constructor template
+ Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
+ tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
+ tpl->InstanceTemplate()->SetInternalFieldCount(1);
+
+ // Prototype
+ NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
+
+ constructor.Reset(isolate, tpl->GetFunction());
+ exports->Set(String::NewFromUtf8(isolate, "MyObject"),
+ tpl->GetFunction());
+}
+
+void MyObject::New(const FunctionCallbackInfo<Value>& args) {
+ Isolate* isolate = args.GetIsolate();
+
+ if (args.IsConstructCall()) {
+ // Invoked as constructor: `new MyObject(...)`
+ double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
+ MyObject* obj = new MyObject(value);
+ obj->Wrap(args.This());
+ args.GetReturnValue().Set(args.This());
+ } else {
+ // Invoked as plain function `MyObject(...)`, turn into construct call.
+ const int argc = 1;
+ Local<Value> argv[argc] = { args[0] };
+ Local<Context> context = isolate->GetCurrentContext();
+ Local<Function> cons = Local<Function>::New(isolate, constructor);
+ Local<Object> result =
+ cons->NewInstance(context, argc, argv).ToLocalChecked();
+ args.GetReturnValue().Set(result);
+ }
+}
+
+void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
+ Isolate* isolate = args.GetIsolate();
+
+ MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
+ obj->value_ += 1;
+
+ args.GetReturnValue().Set(Number::New(isolate, obj->value_));
+}
+
+void InitAll(Local<Object> exports) {
+ MyObject::Init(exports);
+}
+
+NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
+
+}