Skip to content

Commit 4ab4a99

Browse files
Gabriel SchulhofTrott
authored andcommitted
doc: de-emphasize wrapping in napi_define_class
Change the documentation for `napi_define_class` in such a way that it mentions wrapping C++ class instances as a possible use for the API, rather than making the assumption that it is the use case for the API. Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> Co-authored-by: Rich Trott <rtrott@gmail.com> Fixes: #36150 PR-URL: #36159 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 3ee556a commit 4ab4a99

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

doc/api/n-api.md

+31-24
Original file line numberDiff line numberDiff line change
@@ -4738,14 +4738,15 @@ napi_status napi_define_class(napi_env env,
47384738
```
47394739

47404740
* `[in] env`: The environment that the API is invoked under.
4741-
* `[in] utf8name`: Name of the JavaScript constructor function; this is
4742-
not required to be the same as the C++ class name, though it is recommended
4743-
for clarity.
4741+
* `[in] utf8name`: Name of the JavaScript constructor function; When wrapping a
4742+
C++ class, we recommend for clarity that this name be the same as that of
4743+
the C++ class.
47444744
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
47454745
if it is null-terminated.
47464746
* `[in] constructor`: Callback function that handles constructing instances
4747-
of the class. This should be a static method on the class, not an actual
4748-
C++ constructor function. [`napi_callback`][] provides more details.
4747+
of the class. When wrapping a C++ class, this method must be a static member
4748+
with the [`napi_callback`][] signature. A C++ class constructor cannot be
4749+
used. [`napi_callback`][] provides more details.
47494750
* `[in] data`: Optional data to be passed to the constructor callback as
47504751
the `data` property of the callback info.
47514752
* `[in] property_count`: Number of items in the `properties` array argument.
@@ -4757,27 +4758,33 @@ napi_status napi_define_class(napi_env env,
47574758

47584759
Returns `napi_ok` if the API succeeded.
47594760

4760-
Defines a JavaScript class that corresponds to a C++ class, including:
4761-
4762-
* A JavaScript constructor function that has the class name and invokes the
4763-
provided C++ constructor callback.
4764-
* Properties on the constructor function corresponding to _static_ data
4765-
properties, accessors, and methods of the C++ class (defined by
4766-
property descriptors with the `napi_static` attribute).
4767-
* Properties on the constructor function's `prototype` object corresponding to
4768-
_non-static_ data properties, accessors, and methods of the C++ class
4769-
(defined by property descriptors without the `napi_static` attribute).
4770-
4771-
The C++ constructor callback should be a static method on the class that calls
4772-
the actual class constructor, then wraps the new C++ instance in a JavaScript
4773-
object, and returns the wrapper object. See `napi_wrap()` for details.
4761+
Defines a JavaScript class, including:
4762+
4763+
* A JavaScript constructor function that has the class name. When wrapping a
4764+
corresponding C++ class, the callback passed via `constructor` can be used to
4765+
instantiate a new C++ class instance, which can then be placed inside the
4766+
JavaScript object instance being constructed using [`napi_wrap`][].
4767+
* Properties on the constructor function whose implementation can call
4768+
corresponding _static_ data properties, accessors, and methods of the C++
4769+
class (defined by property descriptors with the `napi_static` attribute).
4770+
* Properties on the constructor function's `prototype` object. When wrapping a
4771+
C++ class, _non-static_ data properties, accessors, and methods of the C++
4772+
class can be called from the static functions given in the property
4773+
descriptors without the `napi_static` attribute after retrieving the C++ class
4774+
instance placed inside the JavaScript object instance by using
4775+
[`napi_unwrap`][].
4776+
4777+
When wrapping a C++ class, the C++ constructor callback passed via `constructor`
4778+
should be a static method on the class that calls the actual class constructor,
4779+
then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4780+
object. See [`napi_wrap`][] for details.
47744781

47754782
The JavaScript constructor function returned from [`napi_define_class`][] is
4776-
often saved and used later, to construct new instances of the class from native
4777-
code, and/or check whether provided values are instances of the class. In that
4778-
case, to prevent the function value from being garbage-collected, create a
4779-
persistent reference to it using [`napi_create_reference`][] and ensure the
4780-
reference count is kept >= 1.
4783+
often saved and used later to construct new instances of the class from native
4784+
code, and/or to check whether provided values are instances of the class. In
4785+
that case, to prevent the function value from being garbage-collected, a
4786+
strong persistent reference to it can be created using
4787+
[`napi_create_reference`][], ensuring that the reference count is kept >= 1.
47814788

47824789
Any non-`NULL` data which is passed to this API via the `data` parameter or via
47834790
the `data` field of the `napi_property_descriptor` array items can be associated

0 commit comments

Comments
 (0)