3.3 KiB
Class property and descriptor
Property descriptor for use with
Napi::ObjectWrap::DefineClass()
. This is different from the
standalone Napi::PropertyDescriptor
because it is specific
to each Napi::ObjectWrap<T>
subclass. This prevents
using descriptors from a different class when defining a new class
(preventing the callbacks from having incorrect this
pointers).
Example
#include <napi.h>
class Example : public Napi::ObjectWrap<Example> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
(const Napi::CallbackInfo &info);
Example
private:
static Napi::FunctionReference constructor;
double _value;
::Value GetValue(const Napi::CallbackInfo &info);
Napivoid SetValue(const Napi::CallbackInfo &info, const Napi::Value &value);
};
::Object Example::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "Example", {
Napi// Register a class instance accessor with getter and setter functions.
("value", &Example::GetValue, &Example::SetValue),
InstanceAccessor// We can also register a readonly accessor by passing nullptr as the setter.
("readOnlyProp", &Example::GetValue, nullptr)
InstanceAccessor});
= Napi::Persistent(func);
constructor .SuppressDestruct();
constructor.Set("Example", func);
exports
return exports;
}
::Example(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Example>(info) {
Example::Env env = info.Env();
Napi// ...
::Number value = info[0].As<Napi::Number>();
Napithis->_value = value.DoubleValue();
}
::FunctionReference Example::constructor;
Napi
::Value Example::GetValue(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napireturn Napi::Number::New(env, this->_value);
}
void Example::SetValue(const Napi::CallbackInfo &info, const Napi::Value &value) {
::Env env = info.Env();
Napi// ...
::Number arg = value.As<Napi::Number>();
Napithis->_value = arg.DoubleValue();
}
// Initialize native add-on
::Object Init (Napi::Env env, Napi::Object exports) {
Napi::Init(env, exports);
Examplereturn exports;
}
// Register and initialize native add-on
(NODE_GYP_MODULE_NAME, Init) NODE_API_MODULE
The above code can be used from JavaScript as follows:
'use strict';
const { Example } = require('bindings')('addon');
const example = new Example(11);
console.log(example.value);
// It prints 11
.value = 19;
exampleconsole.log(example.value);
// It prints 19
.readOnlyProp = 500;
exampleconsole.log(example.readOnlyProp);
// Unchanged. It prints 19
Methods
Constructor
Creates new instance of Napi::ClassPropertyDescriptor
descriptor object.
::ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {} Napi
[in] desc
: Thenapi_property_descriptor
Returns new instance of Napi::ClassPropertyDescriptor
that is used as property descriptor inside the
Napi::ObjectWrap<T>
class.
Operator
operator napi_property_descriptor&() { return _desc; }
Returns the original N-API napi_property_descriptor
wrapped inside the Napi::ClassPropertyDescriptor
operator const napi_property_descriptor&() const { return _desc; }
Returns the original N-API napi_property_descriptor
wrapped inside the Napi::ClassPropertyDescriptor