Skip to content

Commit c9506a8

Browse files
committed
n-api: add more property defaults
Add a default value for class method and js like property in enum napi_property_attributes. n-api currently offers only one default which is non configurable, non writable, non enumerable - like Object.defineProperty(). While this is formal correct the usual way to create properties in JS is either by defining a class or use obj.prop = value. The defaults from these variants are now backed into enum values. PR-URL: #35214 Refs: nodejs/node-addon-api#811 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
1 parent 59ca56e commit c9506a8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

doc/api/n-api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,6 +3731,12 @@ if (status != napi_ok) return status;
37313731

37323732
### Structures
37333733
#### napi_property_attributes
3734+
<!-- YAML
3735+
changes:
3736+
- version: REPLACEME
3737+
pr-url: https://github.com/nodejs/node/pull/35214
3738+
description: added `napi_default_method` and `napi_default_property`
3739+
-->
37343740

37353741
```c
37363742
typedef enum {
@@ -3742,6 +3748,14 @@ typedef enum {
37423748
// Used with napi_define_class to distinguish static properties
37433749
// from instance properties. Ignored by napi_define_properties.
37443750
napi_static = 1 << 10,
3751+
3752+
// Default for class methods.
3753+
napi_default_method = napi_writable | napi_configurable,
3754+
3755+
// Default for object properties, like in JS obj[prop].
3756+
napi_default_property = napi_writable |
3757+
napi_enumerable |
3758+
napi_configurable,
37453759
} napi_property_attributes;
37463760
```
37473761

@@ -3760,6 +3774,10 @@ They can be one or more of the following bitflags:
37603774
* `napi_static`: The property will be defined as a static property on a class as
37613775
opposed to an instance property, which is the default. This is used only by
37623776
[`napi_define_class`][]. It is ignored by `napi_define_properties`.
3777+
* `napi_default_method`: The property is configureable, writeable but not
3778+
enumerable like a method in a JS class.
3779+
* `napi_default_property`: The property is writable, enumerable and configurable
3780+
like a property set via JS code `obj.key = value`.
37633781

37643782
#### napi_property_descriptor
37653783

src/js_native_api_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ typedef enum {
3030
// Used with napi_define_class to distinguish static properties
3131
// from instance properties. Ignored by napi_define_properties.
3232
napi_static = 1 << 10,
33+
34+
#ifdef NAPI_EXPERIMENTAL
35+
// Default for class methods.
36+
napi_default_method = napi_writable | napi_configurable,
37+
38+
// Default for object properties, like in JS obj[prop].
39+
napi_default_jsproperty = napi_writable |
40+
napi_enumerable |
41+
napi_configurable,
42+
#endif // NAPI_EXPERIMENTAL
3343
} napi_property_attributes;
3444

3545
typedef enum {

0 commit comments

Comments
 (0)