MCPcopy Index your code
hub / github.com/nodejs/node-addon-api / Test

Class Test

test/objectwrap.cc:29–284  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27}
28
29class Test : public Napi::ObjectWrap<Test> {
30 public:
31 Test(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Test>(info) {
32 if (info.Length() > 0) {
33 finalizeCb_ = Napi::Persistent(info[0].As<Napi::Function>());
34 }
35 // Create an own instance property.
36 info.This().As<Napi::Object>().DefineProperty(
37 Napi::PropertyDescriptor::Accessor(info.Env(),
38 info.This().As<Napi::Object>(),
39 "ownProperty",
40 OwnPropertyGetter,
41 napi_enumerable,
42 this));
43
44 // Create an own instance property with a templated function.
45 info.This().As<Napi::Object>().DefineProperty(
46 Napi::PropertyDescriptor::Accessor<OwnPropertyGetter>(
47 "ownPropertyT", napi_enumerable, this));
48
49 bufref_ = Napi::Persistent(Napi::Buffer<uint8_t>::New(
50 Env(),
51 static_cast<uint8_t*>(malloc(1)),
52 1,
53 [](Napi::Env, uint8_t* bufaddr) { free(bufaddr); }));
54 }
55
56 static Napi::Value OwnPropertyGetter(const Napi::CallbackInfo& info) {
57 return static_cast<Test*>(info.Data())->Getter(info);
58 }
59
60 static Napi::Value CanUnWrap(const Napi::CallbackInfo& info) {
61 Napi::Object wrappedObject = info[0].As<Napi::Object>();
62 std::string expectedString = info[1].As<Napi::String>();
63 Test* nativeObject = Test::Unwrap(wrappedObject);
64 std::string strVal = MaybeUnwrap(nativeObject->Getter(info).ToString());
65
66 return Napi::Boolean::New(info.Env(), strVal == expectedString);
67 }
68
69 void Setter(const Napi::CallbackInfo& /*info*/, const Napi::Value& value) {
70 value_ = MaybeUnwrap(value.ToString());
71 }
72
73 Napi::Value Getter(const Napi::CallbackInfo& info) {
74 return Napi::String::New(info.Env(), value_);
75 }
76
77 Napi::Value TestMethod(const Napi::CallbackInfo& info) {
78 std::string str = MaybeUnwrap(info[0].ToString());
79 return Napi::String::New(info.Env(), str + " instance");
80 }
81
82 Napi::Value TestMethodInternal(const Napi::CallbackInfo& info) {
83 std::string str = MaybeUnwrap(info[0].ToString());
84 return Napi::String::New(info.Env(), str + " instance internal");
85 }
86

Callers 1

TestCallbackInfoSetDataFunction · 0.70

Calls

no outgoing calls

Tested by 1

TestCallbackInfoSetDataFunction · 0.56