|
| 1 | +#include "myobject.h" |
| 2 | + |
| 3 | +using namespace v8; |
| 4 | + |
| 5 | +Persistent<Function> MyObject::constructor; |
| 6 | + |
| 7 | +MyObject::MyObject(double value) : value_(value) { |
| 8 | +} |
| 9 | + |
| 10 | +MyObject::~MyObject() { |
| 11 | +} |
| 12 | + |
| 13 | +void MyObject::Init(Handle<Object> exports) { |
| 14 | + Isolate* isolate = exports->GetIsolate(); |
| 15 | + |
| 16 | + // Prepare constructor template |
| 17 | + Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); |
| 18 | + tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); |
| 19 | + tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 20 | + |
| 21 | + // Prototype |
| 22 | + NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); |
| 23 | + |
| 24 | + constructor.Reset(isolate, tpl->GetFunction()); |
| 25 | + exports->Set(String::NewFromUtf8(isolate, "MyObject"), |
| 26 | + tpl->GetFunction()); |
| 27 | +} |
| 28 | + |
| 29 | +void MyObject::New(const FunctionCallbackInfo<Value>& args) { |
| 30 | + Isolate* isolate = args.GetIsolate(); |
| 31 | + |
| 32 | + if (args.IsConstructCall()) { |
| 33 | + // Invoked as constructor: `new MyObject(...)` |
| 34 | + double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); |
| 35 | + MyObject* obj = new MyObject(value); |
| 36 | + obj->Wrap(args.This()); |
| 37 | + args.GetReturnValue().Set(args.This()); |
| 38 | + } else { |
| 39 | + // Invoked as plain function `MyObject(...)`, turn into construct call. |
| 40 | + const int argc = 1; |
| 41 | + Local<Value> argv[argc] = { args[0] }; |
| 42 | + Local<Function> cons = Local<Function>::New(isolate, constructor); |
| 43 | + args.GetReturnValue().Set(cons->NewInstance(argc, argv)); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void MyObject::GetValue(const FunctionCallbackInfo<Value>& args) { |
| 48 | + Isolate* isolate = args.GetIsolate(); |
| 49 | + MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); |
| 50 | + args.GetReturnValue().Set(Number::New(isolate, obj->value_)); |
| 51 | +} |
| 52 | + |
| 53 | +void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { |
| 54 | + Isolate* isolate = args.GetIsolate(); |
| 55 | + |
| 56 | + MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); |
| 57 | + obj->value_ += 1; |
| 58 | + |
| 59 | + args.GetReturnValue().Set(Number::New(isolate, obj->value_)); |
| 60 | +} |
| 61 | + |
| 62 | +void MyObject::Multiply(const FunctionCallbackInfo<Value>& args) { |
| 63 | + Isolate* isolate = args.GetIsolate(); |
| 64 | + |
| 65 | + MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); |
| 66 | + double multiple = args[0]->IsUndefined() ? 1 : args[0]->NumberValue(); |
| 67 | + |
| 68 | + const int argc = 1; |
| 69 | + Local<Value> argv[argc] = { Number::New(isolate, obj->value_ * multiple) }; |
| 70 | + |
| 71 | + Local<Function> cons = Local<Function>::New(isolate, constructor); |
| 72 | + args.GetReturnValue().Set(cons->NewInstance(argc, argv)); |
| 73 | +} |
0 commit comments