From c6ccd8aacaa6396a8361ed8787588a9e68f29a3d Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Tue, 15 Sep 2020 23:04:48 +0200 Subject: [PATCH 1/4] 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. --- doc/api/n-api.md | 20 ++++++++++++++++---- src/js_native_api_types.h | 8 ++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index db43ee18affaf4..7a115c96247482 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -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; @@ -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), }; @@ -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, @@ -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, } napi_property_attributes; ``` @@ -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 diff --git a/src/js_native_api_types.h b/src/js_native_api_types.h index 115ccebf26132e..0f7f3b2d9e131d 100644 --- a/src/js_native_api_types.h +++ b/src/js_native_api_types.h @@ -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, } napi_property_attributes; typedef enum { From 0d3f7c25726bc3c6706be3c80efb65ce2e2cde0c Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Tue, 15 Sep 2020 23:36:26 +0200 Subject: [PATCH 2/4] add changes section --- doc/api/n-api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 7a115c96247482..86a6ad0b369f1d 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3731,6 +3731,12 @@ if (status != napi_ok) return status; ### Structures #### napi_property_attributes + ```c typedef enum { From 6728bf1b55c304d5e52d69a7ed6cb4f4ba1c9959 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Wed, 16 Sep 2020 07:52:40 +0200 Subject: [PATCH 3/4] remove some doc changes, add experimental --- doc/api/n-api.md | 8 ++++---- src/js_native_api_types.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 86a6ad0b369f1d..22fe991142f850 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -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_property, NULL}; + {"hello", NULL, Method, NULL, NULL, NULL, napi_default, NULL}; status = napi_define_properties(env, exports, 1, &desc); if (status != napi_ok) return NULL; return exports; @@ -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_method, NULL }, + { "value", NULL, NULL, GetValue, SetValue, NULL, napi_default, NULL }, DECLARE_NAPI_METHOD("plusOne", PlusOne), DECLARE_NAPI_METHOD("multiply", Multiply), }; @@ -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_method, NULL }, - { "bar", NULL, NULL, NULL, NULL, barValue, napi_default_method, NULL } + { "foo", NULL, NULL, NULL, NULL, fooValue, napi_default, NULL }, + { "bar", NULL, NULL, NULL, NULL, barValue, napi_default, NULL } } status = napi_define_properties(env, obj, diff --git a/src/js_native_api_types.h b/src/js_native_api_types.h index 0f7f3b2d9e131d..b871ba2d6dc745 100644 --- a/src/js_native_api_types.h +++ b/src/js_native_api_types.h @@ -31,6 +31,7 @@ typedef enum { // 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, @@ -38,6 +39,7 @@ typedef enum { napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable, +#endif } napi_property_attributes; typedef enum { From caac675817a9aeca57641e27255258805ab71f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20St=C3=B6bich?= Date: Wed, 16 Sep 2020 08:42:06 +0200 Subject: [PATCH 4/4] Update src/js_native_api_types.h Co-authored-by: Gabriel Schulhof --- src/js_native_api_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_types.h b/src/js_native_api_types.h index b871ba2d6dc745..7011c80e671a15 100644 --- a/src/js_native_api_types.h +++ b/src/js_native_api_types.h @@ -39,7 +39,7 @@ typedef enum { napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable, -#endif +#endif // NAPI_EXPERIMENTAL } napi_property_attributes; typedef enum {