From a98ee07bc026d72339816f021ac346bd8b521d20 Mon Sep 17 00:00:00 2001 From: Anisha Rohra Date: Thu, 19 Jul 2018 00:21:32 -0400 Subject: [PATCH 1/3] Added documentation for PropertyDescriptor --- doc/property_descriptor.md | 113 +++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/doc/property_descriptor.md b/doc/property_descriptor.md index 65bf400ad..dfdb625e3 100644 --- a/doc/property_descriptor.md +++ b/doc/property_descriptor.md @@ -1,5 +1,110 @@ -# property descriptor +# Property Descriptor + +An [Object](object.md) can be assigned properites via its [DefineProperty](object.md#defineproperty) and [DefineProperties](object.md#defineproperties) function, which take PropertyDescrptor(s) as their parameters. The PropertyDescriptor can contain either values or functions, which are then assigned to the Object. Note that a single instance of a PropertyDescriptor class can only contain either one value, or at most two functions. PropertyDescriptors can only be created through the class methods [Accessor](#accessor), [Function](#function), or [Value](#value), each of which return a new static instance of a PropertyDescriptor. + +## Example + +```cpp + +``` + +## Methods + +### Constructor + +```cpp +Napi::PropertyDescriptor::PropertyDescriptor (napi_property_descriptor desc); +``` + +* `[in] desc`: A PropertyDescriptor that is needed in order to create another PropertyDescriptor. + +### Accessor + +```cpp +static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, + Getter getter, + napi_property_attributes attributes = napi_default, + void *data = nullptr); +``` + +* `[in] name`: The name used for the getter function. +* `[in] getter`: A getter function. +* `[in] attributes`: Potential attributes for the getter function. +* `[in] data`: A pointer to data of any type, default is a null pointer. + +Returns a PropertyDescriptor that contains a function. + +The name of the property can be any of the following types: +- `const char*` +- `const std::string &` +- `napi_value value` +- `Name` + +```cpp +static PropertyDescriptor Napi::PropertyDescriptor::Accessor (___ name, + Getter getter, + Setter setter, + napi_property_attributes attributes = napi_default, + void *data = nullptr); +``` + +* `[in] name`: The name of the getter and setter function. +* `[in] getter`: The getter function. +* `[in] setter`: The setter function. +* `[in] attributes`: Potential attributes for the getter function. +* `[in] data`: A pointer to data of any type, default is a null pointer. + +Returns a PropertyDescriptor that contains a Getter and Setter function. + +The name of the property can be any of the following types: +- `const char*` +- `const std::string &` +- `napi_value value` +- `Name` + +### Function + +```cpp +static PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, + Callable cb, + napi_property_attributes attributes = napi_default, + void *data = nullptr); +``` + +* `[in] name`: The name of the Callable function. +* `[in] cb`: The function +* `[in] attributes`: Potential attributes for the getter function. +* `[in] data`: A pointer to data of any type, default is a null pointer. + +Returns a PropertyDescriptor that contains a callable Function. + +The name of the property can be any of the following types: +- `const char*` +- `const std::string &` +- `napi_value value` +- `Name` + +### Value + +```cpp +static PropertyDescriptor Napi::PropertyDescriptor::Value (___ name, + napi_value value, + napi_property_attributes attributes = napi_default); +``` + +The name of the property can be any of the following types: +- `const char*` +- `const std::string &` +- `napi_value value` +- `Name` + +## Related Information + +### napi\_property\_attributes +`napi_property_attributes` are flags used to indicate to JavaScript certain permissions that the property is meant to have. The following are the flag options: +- napi\_default, +- napi\_writable, +- napi\_enumerable, +- napi\_configurable +For more information on the flags and on napi\_property\_attributes, please read the documentation [here](https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_property_attributes). -You are reading a draft of the next documentation and it's in continuous update so -if you don't find what you need please refer to: -[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/) From dfc11a48e9904d5ef8f1cc07240cb7b73ce22557 Mon Sep 17 00:00:00 2001 From: Anisha Rohra Date: Thu, 19 Jul 2018 00:56:00 -0400 Subject: [PATCH 2/3] Add example for PropertyDescriptor --- doc/property_descriptor.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/doc/property_descriptor.md b/doc/property_descriptor.md index dfdb625e3..3caa2d5d6 100644 --- a/doc/property_descriptor.md +++ b/doc/property_descriptor.md @@ -5,7 +5,38 @@ An [Object](object.md) can be assigned properites via its [DefineProperty](objec ## Example ```cpp - +#include + +using namespace Napi; + +Value TestGetter(const CallbackInfo& info) { + return Boolean::New(info.Env(), testValue); +} + +void TestSetter(const CallbackInfo& info) { + testValue = info[0].As(); +} + +Value TestFunction(const CallbackInfo& info) { + return Boolean::New(info.Env(), true); +} + +Void Init(Env env) { + // Accessor + PropertyDescriptor pd1 = PropertyDescriptor::Accessor("pd1", TestGetter); + PropertyDescriptor pd2 = PropertyDescriptor::Accessor("pd2", TestGetter, TestSetter); + + // Function + PropertyDescriptor pd3 = PropertyDescriptor::Function("function", TestFunction); + + // Value + Boolean true_bool = Boolean::New(env, true); + PropertyDescriptor pd4 = PropertyDescriptor::Value("boolean value", TestFunction, napi_writable); + + // Assign to an Object + Object obj = Object::New(env); + obj.DefineProperties({pd1, pd2, pd3, pd4}); +} ``` ## Methods From 35d274fb4ed4687f14b4edc13fb567ca00a9865b Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 19 Jul 2018 13:25:36 -0400 Subject: [PATCH 3/3] squash: fix spacing --- doc/property_descriptor.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/property_descriptor.md b/doc/property_descriptor.md index 3caa2d5d6..f458907b9 100644 --- a/doc/property_descriptor.md +++ b/doc/property_descriptor.md @@ -99,7 +99,7 @@ The name of the property can be any of the following types: static PropertyDescriptor Napi::PropertyDescriptor::Function (___ name, Callable cb, napi_property_attributes attributes = napi_default, - void *data = nullptr); + void *data = nullptr); ``` * `[in] name`: The name of the Callable function.