Skip to content

Commit

Permalink
n-api: add more property defaults
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Flarna authored and ruyadorno committed Sep 21, 2020
1 parent 64ac5c6 commit 7f3b2b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3726,6 +3726,12 @@ if (status != napi_ok) return status;

### Structures
#### napi_property_attributes
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/35214
description: added `napi_default_method` and `napi_default_property`
-->

```c
typedef enum {
Expand All @@ -3737,6 +3743,14 @@ typedef enum {
// Used with napi_define_class to distinguish static properties
// from instance properties. Ignored by napi_define_properties.
napi_static = 1 << 10,

// Default for class methods.
napi_default_method = napi_writable | napi_configurable,

// Default for object properties, like in JS obj[prop].
napi_default_property = napi_writable |
napi_enumerable |
napi_configurable,
} napi_property_attributes;
```

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

#### napi_property_descriptor

Expand Down
10 changes: 10 additions & 0 deletions src/js_native_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ typedef enum {
// Used with napi_define_class to distinguish static properties
// from instance properties. Ignored by napi_define_properties.
napi_static = 1 << 10,

#ifdef NAPI_EXPERIMENTAL
// Default for class methods.
napi_default_method = napi_writable | napi_configurable,

// Default for object properties, like in JS obj[prop].
napi_default_jsproperty = napi_writable |
napi_enumerable |
napi_configurable,
#endif // NAPI_EXPERIMENTAL
} napi_property_attributes;

typedef enum {
Expand Down

0 comments on commit 7f3b2b2

Please sign in to comment.