Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: add more property defaults #35214

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ provided by the addon:
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc =
{"hello", NULL, Method, NULL, NULL, NULL, napi_default, NULL};
{"hello", NULL, Method, NULL, NULL, NULL, napi_default_property, NULL};
status = napi_define_properties(env, exports, 1, &desc);
if (status != napi_ok) return NULL;
return exports;
Expand All @@ -1805,7 +1805,7 @@ To define a class so that new instances can be created (often used with
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor properties[] = {
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_default, NULL },
{ "value", NULL, NULL, GetValue, SetValue, NULL, napi_default_method, NULL },
DECLARE_NAPI_METHOD("plusOne", PlusOne),
DECLARE_NAPI_METHOD("multiply", Multiply),
};
Expand Down Expand Up @@ -3719,8 +3719,8 @@ if (status != napi_ok) return status;

// Set the properties
napi_property_descriptor descriptors[] = {
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_default, NULL },
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_default, NULL }
{ "foo", NULL, NULL, NULL, NULL, fooValue, napi_default_method, NULL },
{ "bar", NULL, NULL, NULL, NULL, barValue, napi_default_method, NULL }
}
status = napi_define_properties(env,
obj,
Expand All @@ -3742,6 +3742,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,
Flarna marked this conversation as resolved.
Show resolved Hide resolved
} napi_property_attributes;
```

Expand All @@ -3760,6 +3768,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
8 changes: 8 additions & 0 deletions src/js_native_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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_jsproperty = napi_writable |
napi_enumerable |
napi_configurable,
Flarna marked this conversation as resolved.
Show resolved Hide resolved
} napi_property_attributes;

typedef enum {
Expand Down