Skip to content

Commit 7f3b2b2

Browse files
Flarnaruyadorno
authored andcommitted
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 64ac5c6 commit 7f3b2b2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

doc/api/n-api.md

+18
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,12 @@ if (status != napi_ok) return status;
37263726

37273727
### Structures
37283728
#### napi_property_attributes
3729+
<!-- YAML
3730+
changes:
3731+
- version: REPLACEME
3732+
pr-url: https://github.com/nodejs/node/pull/35214
3733+
description: added `napi_default_method` and `napi_default_property`
3734+
-->
37293735

37303736
```c
37313737
typedef enum {
@@ -3737,6 +3743,14 @@ typedef enum {
37373743
// Used with napi_define_class to distinguish static properties
37383744
// from instance properties. Ignored by napi_define_properties.
37393745
napi_static = 1 << 10,
3746+
3747+
// Default for class methods.
3748+
napi_default_method = napi_writable | napi_configurable,
3749+
3750+
// Default for object properties, like in JS obj[prop].
3751+
napi_default_property = napi_writable |
3752+
napi_enumerable |
3753+
napi_configurable,
37403754
} napi_property_attributes;
37413755
```
37423756

@@ -3755,6 +3769,10 @@ They can be one or more of the following bitflags:
37553769
* `napi_static`: The property will be defined as a static property on a class as
37563770
opposed to an instance property, which is the default. This is used only by
37573771
[`napi_define_class`][]. It is ignored by `napi_define_properties`.
3772+
* `napi_default_method`: The property is configureable, writeable but not
3773+
enumerable like a method in a JS class.
3774+
* `napi_default_property`: The property is writable, enumerable and configurable
3775+
like a property set via JS code `obj.key = value`.
37583776

37593777
#### napi_property_descriptor
37603778

src/js_native_api_types.h

+10
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)