MCPcopy Create free account
hub / github.com/nodejs/nan / MyObject

Class MyObject

test/cpp/objectwraphandle.cpp:13–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11using namespace Nan; // NOLINT(build/namespaces)
12
13class MyObject : public ObjectWrap {
14 public:
15 static NAN_MODULE_INIT(Init) {
16 v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
17 tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
18 tpl->InstanceTemplate()->SetInternalFieldCount(1);
19
20 SetPrototypeMethod(tpl, "getHandle", GetHandle);
21 SetPrototypeMethod(tpl, "getHandleConst", GetHandleConst);
22 SetPrototypeMethod(tpl, "getValue", GetValue);
23
24 constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
25 Set(target, Nan::New("MyObject").ToLocalChecked(),
26 Nan::GetFunction(tpl).ToLocalChecked());
27 }
28
29 private:
30 explicit MyObject(double value = 0) : value_(value) {}
31 ~MyObject() {}
32
33 static NAN_METHOD(New) {
34 if (info.IsConstructCall()) {
35 double value =
36 info[0]->IsUndefined() ? 0 : Nan::To<double>(info[0]).FromJust();
37 MyObject *obj = new MyObject(value);
38 obj->Wrap(info.This());
39 info.GetReturnValue().Set(info.This());
40 } else {
41 const int argc = 1;
42 v8::Local<v8::Value> argv[argc] = {info[0]};
43 v8::Local<v8::Function> cons = Nan::New(constructor());
44 info.GetReturnValue().Set(
45 Nan::NewInstance(cons, argc, argv).ToLocalChecked());
46 }
47 }
48
49 static NAN_METHOD(GetHandle) {
50 MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
51 info.GetReturnValue().Set(obj->handle());
52 }
53
54 static NAN_METHOD(GetHandleConst) {
55 MyObject const *obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
56 info.GetReturnValue().Set(obj->handle());
57 }
58
59 static NAN_METHOD(GetValue) {
60 MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
61 info.GetReturnValue().Set(obj->value_);
62 }
63
64 static inline Persistent<v8::Function> & constructor() {
65 static Persistent<v8::Function> my_constructor;
66 return my_constructor;
67 }
68
69 double value_;
70};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected