| 62 | }; |
| 63 | |
| 64 | class MyObject : public ObjectWrap { |
| 65 | public: |
| 66 | static NAN_MODULE_INIT(Init) { |
| 67 | v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New); |
| 68 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 69 | |
| 70 | SetPrototypeMethod(tpl, "getValue", GetValue); |
| 71 | SetPrototypeMethod(tpl, "newInnerObject", NewInnerObject); |
| 72 | |
| 73 | constructor().Reset(GetFunction(tpl).ToLocalChecked()); |
| 74 | } |
| 75 | |
| 76 | static NAN_METHOD(NewInstance) { |
| 77 | v8::Local<v8::Function> cons = Nan::New(constructor()); |
| 78 | double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0; |
| 79 | const int argc = 1; |
| 80 | v8::Local<v8::Value> argv[1] = {Nan::New(value)}; |
| 81 | info.GetReturnValue().Set( |
| 82 | Nan::NewInstance(cons, argc, argv).ToLocalChecked()); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | explicit MyObject(double value = 0) : value_(value) {} |
| 87 | ~MyObject() {} |
| 88 | |
| 89 | static NAN_METHOD(New) { |
| 90 | if (info.IsConstructCall()) { |
| 91 | double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0; |
| 92 | MyObject * obj = new MyObject(value); |
| 93 | obj->Wrap(info.This()); |
| 94 | info.GetReturnValue().Set(info.This()); |
| 95 | } else { |
| 96 | const int argc = 1; |
| 97 | v8::Local<v8::Value> argv[argc] = {info[0]}; |
| 98 | v8::Local<v8::Function> cons = Nan::New(constructor()); |
| 99 | info.GetReturnValue().Set( |
| 100 | Nan::NewInstance(cons, argc, argv).ToLocalChecked()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static NAN_METHOD(GetValue) { |
| 105 | MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder()); |
| 106 | info.GetReturnValue().Set(obj->value_); |
| 107 | } |
| 108 | |
| 109 | static NAN_METHOD(NewInnerObject) { |
| 110 | const int argc = 1; |
| 111 | v8::Local<v8::Value> argv[argc] = {info[0]}; |
| 112 | info.GetReturnValue().Set(InnerObject::NewInstance(argc, argv)); |
| 113 | } |
| 114 | |
| 115 | static inline Persistent<v8::Function> & constructor() { |
| 116 | static Persistent<v8::Function> my_constructor; |
| 117 | return my_constructor; |
| 118 | } |
| 119 | |
| 120 | double value_; |
| 121 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected