Skip to content

Commit 0a00e7c

Browse files
Philipp Renothmhdawson
authored andcommitted
src: implement missing descriptor defs for symbols
Implements descriptor definitions with symbols for `StaticMethod`, `StaticAccessor`, `InstanceAccessor`, `StaticValue`, `InstanceValue`. Ref: #279 PR-URL: #280 Refs: #279 Reviewed-By: Michael Dawson <[email protected]>
1 parent 38e01b7 commit 0a00e7c

File tree

5 files changed

+530
-77
lines changed

5 files changed

+530
-77
lines changed

doc/object_wrap.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,50 @@ One or more of `napi_property_attributes`.
234234
Returns `Napi::PropertyDescriptor` object that represents a static method of a
235235
JavaScript class.
236236

237+
### StaticMethod
238+
239+
Creates property descriptor that represents a static method of a JavaScript class.
240+
241+
```cpp
242+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
243+
StaticVoidMethodCallback method,
244+
napi_property_attributes attributes = napi_default,
245+
void* data = nullptr);
246+
```
247+
248+
- `[in] name`: Napi:Symbol that represents the name of a static
249+
method for the class.
250+
- `[in] method`: The native function that represents a static method of a
251+
JavaScript class.
252+
- `[in] attributes`: The attributes associated with a particular property.
253+
One or more of `napi_property_attributes`.
254+
- `[in] data`: User-provided data passed into method when it is invoked.
255+
256+
Returns `Napi::PropertyDescriptor` object that represents the static method of a
257+
JavaScript class.
258+
259+
### StaticMethod
260+
261+
Creates property descriptor that represents a static method of a JavaScript class.
262+
263+
```cpp
264+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticMethod(Symbol name,
265+
StaticMethodCallback method,
266+
napi_property_attributes attributes = napi_default,
267+
void* data = nullptr);
268+
```
269+
270+
method for the class.
271+
- `[in] name`: Napi:Symbol that represents the name of a static.
272+
- `[in] method`: The native function that represents a static method of a
273+
JavaScript class.
274+
- `[in] attributes`: The attributes associated with a particular property.
275+
One or more of `napi_property_attributes`.
276+
- `[in] data`: User-provided data passed into method when it is invoked.
277+
278+
Returns `Napi::PropertyDescriptor` object that represents a static method of a
279+
JavaScript class.
280+
237281
### StaticAccessor
238282

239283
Creates property descriptor that represents a static accessor property of a
@@ -261,6 +305,32 @@ is invoked.
261305
Returns `Napi::PropertyDescriptor` object that represents a static accessor
262306
property of a JavaScript class.
263307
308+
### StaticAccessor
309+
310+
Creates property descriptor that represents a static accessor property of a
311+
JavaScript class.
312+
313+
```cpp
314+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticAccessor(Symbol name,
315+
StaticGetterCallback getter,
316+
StaticSetterCallback setter,
317+
napi_property_attributes attributes = napi_default,
318+
void* data = nullptr);
319+
```
320+
321+
- `[in] name`: Napi:Symbol that represents the name of a static accessor.
322+
- `[in] getter`: The native function to call when a get access to the property of
323+
a JavaScript class is performed.
324+
- `[in] setter`: The native function to call when a set access to the property of
325+
a JavaScript class is performed.
326+
- `[in] attributes`: The attributes associated with a particular property.
327+
One or more of `napi_property_attributes`.
328+
- `[in] data`: User-provided data passed into getter or setter when
329+
is invoked.
330+
331+
Returns `Napi::PropertyDescriptor` object that represents a static accessor
332+
property of a JavaScript class.
333+
264334
### InstanceMethod
265335

266336
Creates property descriptor that represents an instance method of a JavaScript class.
@@ -375,6 +445,32 @@ One or more of `napi_property_attributes`.
375445
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
376446
property of a JavaScript class.
377447
448+
### InstanceAccessor
449+
450+
Creates property descriptor that represents an instance accessor property of a
451+
JavaScript class.
452+
453+
```cpp
454+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceAccessor(Symbol name,
455+
InstanceGetterCallback getter,
456+
InstanceSetterCallback setter,
457+
napi_property_attributes attributes = napi_default,
458+
void* data = nullptr);
459+
```
460+
461+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
462+
instance accessor.
463+
- `[in] getter`: The native function to call when a get access to the property of
464+
a JavaScript class is performed.
465+
- `[in] setter`: The native function to call when a set access to the property of
466+
a JavaScript class is performed.
467+
- `[in] attributes`: The attributes associated with the particular property.
468+
One or more of `napi_property_attributes`.
469+
- `[in] data`: User-provided data passed into getter or setter when this is invoked.
470+
471+
Returns `Napi::PropertyDescriptor` object that represents an instance accessor
472+
property of a JavaScript class.
473+
378474
### StaticValue
379475

380476
Creates property descriptor that represents an static value property of a
@@ -394,6 +490,25 @@ to the napi_static attribute. One or more of `napi_property_attributes`.
394490
Returns `Napi::PropertyDescriptor` object that represents an static value
395491
property of a JavaScript class
396492
493+
### StaticValue
494+
495+
Creates property descriptor that represents an static value property of a
496+
JavaScript class.
497+
```cpp
498+
static Napi::PropertyDescriptor Napi::ObjectWrap::StaticValue(Symbol name,
499+
Napi::Value value,
500+
napi_property_attributes attributes = napi_default);
501+
```
502+
503+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
504+
name of the static property.
505+
- `[in] value`: The value that's retrieved by a get access of the property.
506+
- `[in] attributes`: The attributes to be associated with the property in addition
507+
to the napi_static attribute. One or more of `napi_property_attributes`.
508+
509+
Returns `Napi::PropertyDescriptor` object that represents an static value
510+
property of a JavaScript class
511+
397512
### InstanceValue
398513

399514
Creates property descriptor that represents an instance value property of a
@@ -411,3 +526,21 @@ One or more of `napi_property_attributes`.
411526
412527
Returns `Napi::PropertyDescriptor` object that represents an instance value
413528
property of a JavaScript class.
529+
530+
### InstanceValue
531+
532+
Creates property descriptor that represents an instance value property of a
533+
JavaScript class.
534+
```cpp
535+
static Napi::PropertyDescriptor Napi::ObjectWrap::InstanceValue(Symbol name,
536+
Napi::Value value,
537+
napi_property_attributes attributes = napi_default);
538+
```
539+
540+
- `[in] name`: The `Napi::Symbol` object whose value is used to identify the
541+
name of the property.
542+
- `[in] value`: The value that's retrieved by a get access of the property.
543+
- `[in] attributes`: The attributes to be associated with the property.
544+
One or more of `napi_property_attributes`.
545+
546+
Returns `Napi::PropertyDescriptor` object that represents an instance value

napi-inl.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,40 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
28232823
return desc;
28242824
}
28252825

2826+
template <typename T>
2827+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
2828+
Symbol name,
2829+
StaticVoidMethodCallback method,
2830+
napi_property_attributes attributes,
2831+
void* data) {
2832+
// TODO: Delete when the class is destroyed
2833+
StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data });
2834+
2835+
napi_property_descriptor desc = napi_property_descriptor();
2836+
desc.name = name;
2837+
desc.method = T::StaticVoidMethodCallbackWrapper;
2838+
desc.data = callbackData;
2839+
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
2840+
return desc;
2841+
}
2842+
2843+
template <typename T>
2844+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
2845+
Symbol name,
2846+
StaticMethodCallback method,
2847+
napi_property_attributes attributes,
2848+
void* data) {
2849+
// TODO: Delete when the class is destroyed
2850+
StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data });
2851+
2852+
napi_property_descriptor desc = napi_property_descriptor();
2853+
desc.name = name;
2854+
desc.method = T::StaticMethodCallbackWrapper;
2855+
desc.data = callbackData;
2856+
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
2857+
return desc;
2858+
}
2859+
28262860
template <typename T>
28272861
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
28282862
const char* utf8name,
@@ -2843,6 +2877,26 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
28432877
return desc;
28442878
}
28452879

2880+
template <typename T>
2881+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
2882+
Symbol name,
2883+
StaticGetterCallback getter,
2884+
StaticSetterCallback setter,
2885+
napi_property_attributes attributes,
2886+
void* data) {
2887+
// TODO: Delete when the class is destroyed
2888+
StaticAccessorCallbackData* callbackData =
2889+
new StaticAccessorCallbackData({ getter, setter, data });
2890+
2891+
napi_property_descriptor desc = napi_property_descriptor();
2892+
desc.name = name;
2893+
desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
2894+
desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
2895+
desc.data = callbackData;
2896+
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
2897+
return desc;
2898+
}
2899+
28462900
template <typename T>
28472901
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
28482902
const char* utf8name,
@@ -2933,6 +2987,26 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
29332987
return desc;
29342988
}
29352989

2990+
template <typename T>
2991+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
2992+
Symbol name,
2993+
InstanceGetterCallback getter,
2994+
InstanceSetterCallback setter,
2995+
napi_property_attributes attributes,
2996+
void* data) {
2997+
// TODO: Delete when the class is destroyed
2998+
InstanceAccessorCallbackData* callbackData =
2999+
new InstanceAccessorCallbackData({ getter, setter, data });
3000+
3001+
napi_property_descriptor desc = napi_property_descriptor();
3002+
desc.name = name;
3003+
desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr;
3004+
desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr;
3005+
desc.data = callbackData;
3006+
desc.attributes = attributes;
3007+
return desc;
3008+
}
3009+
29363010
template <typename T>
29373011
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(const char* utf8name,
29383012
Napi::Value value, napi_property_attributes attributes) {
@@ -2943,6 +3017,16 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(const char* utf8nam
29433017
return desc;
29443018
}
29453019

3020+
template <typename T>
3021+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(Symbol name,
3022+
Napi::Value value, napi_property_attributes attributes) {
3023+
napi_property_descriptor desc = napi_property_descriptor();
3024+
desc.name = name;
3025+
desc.value = value;
3026+
desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3027+
return desc;
3028+
}
3029+
29463030
template <typename T>
29473031
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
29483032
const char* utf8name,
@@ -2955,6 +3039,18 @@ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
29553039
return desc;
29563040
}
29573041

3042+
template <typename T>
3043+
inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
3044+
Symbol name,
3045+
Napi::Value value,
3046+
napi_property_attributes attributes) {
3047+
napi_property_descriptor desc = napi_property_descriptor();
3048+
desc.name = name;
3049+
desc.value = value;
3050+
desc.attributes = attributes;
3051+
return desc;
3052+
}
3053+
29583054
template <typename T>
29593055
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
29603056
napi_env env,

napi.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,11 +1453,24 @@ namespace Napi {
14531453
StaticMethodCallback method,
14541454
napi_property_attributes attributes = napi_default,
14551455
void* data = nullptr);
1456+
static PropertyDescriptor StaticMethod(Symbol name,
1457+
StaticVoidMethodCallback method,
1458+
napi_property_attributes attributes = napi_default,
1459+
void* data = nullptr);
1460+
static PropertyDescriptor StaticMethod(Symbol name,
1461+
StaticMethodCallback method,
1462+
napi_property_attributes attributes = napi_default,
1463+
void* data = nullptr);
14561464
static PropertyDescriptor StaticAccessor(const char* utf8name,
14571465
StaticGetterCallback getter,
14581466
StaticSetterCallback setter,
14591467
napi_property_attributes attributes = napi_default,
14601468
void* data = nullptr);
1469+
static PropertyDescriptor StaticAccessor(Symbol name,
1470+
StaticGetterCallback getter,
1471+
StaticSetterCallback setter,
1472+
napi_property_attributes attributes = napi_default,
1473+
void* data = nullptr);
14611474
static PropertyDescriptor InstanceMethod(const char* utf8name,
14621475
InstanceVoidMethodCallback method,
14631476
napi_property_attributes attributes = napi_default,
@@ -1479,12 +1492,23 @@ namespace Napi {
14791492
InstanceSetterCallback setter,
14801493
napi_property_attributes attributes = napi_default,
14811494
void* data = nullptr);
1495+
static PropertyDescriptor InstanceAccessor(Symbol name,
1496+
InstanceGetterCallback getter,
1497+
InstanceSetterCallback setter,
1498+
napi_property_attributes attributes = napi_default,
1499+
void* data = nullptr);
14821500
static PropertyDescriptor StaticValue(const char* utf8name,
14831501
Napi::Value value,
14841502
napi_property_attributes attributes = napi_default);
1503+
static PropertyDescriptor StaticValue(Symbol name,
1504+
Napi::Value value,
1505+
napi_property_attributes attributes = napi_default);
14851506
static PropertyDescriptor InstanceValue(const char* utf8name,
14861507
Napi::Value value,
14871508
napi_property_attributes attributes = napi_default);
1509+
static PropertyDescriptor InstanceValue(Symbol name,
1510+
Napi::Value value,
1511+
napi_property_attributes attributes = napi_default);
14881512

14891513
private:
14901514
static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);

0 commit comments

Comments
 (0)