| 13 | // ref: https://nodejs.org/api/addons.html#addons_factory_of_wrapped_objects |
| 14 | |
| 15 | class InnerObject : public ObjectWrap { |
| 16 | public: |
| 17 | static NAN_MODULE_INIT(Init) { |
| 18 | v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New); |
| 19 | tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 20 | |
| 21 | SetPrototypeMethod(tpl, "getValue", GetValue); |
| 22 | |
| 23 | constructor().Reset(GetFunction(tpl).ToLocalChecked()); |
| 24 | } |
| 25 | |
| 26 | static |
| 27 | v8::Local<v8::Object> NewInstance(int argc, v8::Local<v8::Value> argv[]) { |
| 28 | v8::Local<v8::Function> cons = Nan::New(constructor()); |
| 29 | return Nan::NewInstance(cons, argc, argv).ToLocalChecked(); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | explicit InnerObject(double value = 0) : value_(value) {} |
| 34 | ~InnerObject() {} |
| 35 | |
| 36 | static NAN_METHOD(New) { |
| 37 | if (info.IsConstructCall()) { |
| 38 | double value = info[0]->IsNumber() ? To<double>(info[0]).FromJust() : 0; |
| 39 | InnerObject * obj = new InnerObject(value); |
| 40 | obj->Wrap(info.This()); |
| 41 | info.GetReturnValue().Set(info.This()); |
| 42 | } else { |
| 43 | const int argc = 1; |
| 44 | v8::Local<v8::Value> argv[argc] = {info[0]}; |
| 45 | v8::Local<v8::Function> cons = Nan::New(constructor()); |
| 46 | info.GetReturnValue().Set( |
| 47 | Nan::NewInstance(cons, argc, argv).ToLocalChecked()); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static NAN_METHOD(GetValue) { |
| 52 | InnerObject* obj = ObjectWrap::Unwrap<InnerObject>(info.Holder()); |
| 53 | info.GetReturnValue().Set(obj->value_); |
| 54 | } |
| 55 | |
| 56 | static inline Persistent<v8::Function> & constructor() { |
| 57 | static Persistent<v8::Function> my_constructor; |
| 58 | return my_constructor; |
| 59 | } |
| 60 | |
| 61 | double value_; |
| 62 | }; |
| 63 | |
| 64 | class MyObject : public ObjectWrap { |
| 65 | public: |
nothing calls this directly
no outgoing calls
no test coverage detected