Skip to content

Commit f5efd54

Browse files
Gabriel SchulhofTrott
authored andcommittedDec 15, 2020
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 d49e0ca commit f5efd54

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
@@ -4720,14 +4720,15 @@ napi_status napi_define_class(napi_env env,
47204720
```
47214721

47224722
* `[in] env`: The environment that the API is invoked under.
4723-
* `[in] utf8name`: Name of the JavaScript constructor function; this is
4724-
not required to be the same as the C++ class name, though it is recommended
4725-
for clarity.
4723+
* `[in] utf8name`: Name of the JavaScript constructor function; When wrapping a
4724+
C++ class, we recommend for clarity that this name be the same as that of
4725+
the C++ class.
47264726
* `[in] length`: The length of the `utf8name` in bytes, or `NAPI_AUTO_LENGTH`
47274727
if it is null-terminated.
47284728
* `[in] constructor`: Callback function that handles constructing instances
4729-
of the class. This should be a static method on the class, not an actual
4730-
C++ constructor function. [`napi_callback`][] provides more details.
4729+
of the class. When wrapping a C++ class, this method must be a static member
4730+
with the [`napi_callback`][] signature. A C++ class constructor cannot be
4731+
used. [`napi_callback`][] provides more details.
47314732
* `[in] data`: Optional data to be passed to the constructor callback as
47324733
the `data` property of the callback info.
47334734
* `[in] property_count`: Number of items in the `properties` array argument.
@@ -4739,27 +4740,33 @@ napi_status napi_define_class(napi_env env,
47394740

47404741
Returns `napi_ok` if the API succeeded.
47414742

4742-
Defines a JavaScript class that corresponds to a C++ class, including:
4743-
4744-
* A JavaScript constructor function that has the class name and invokes the
4745-
provided C++ constructor callback.
4746-
* Properties on the constructor function corresponding to _static_ data
4747-
properties, accessors, and methods of the C++ class (defined by
4748-
property descriptors with the `napi_static` attribute).
4749-
* Properties on the constructor function's `prototype` object corresponding to
4750-
_non-static_ data properties, accessors, and methods of the C++ class
4751-
(defined by property descriptors without the `napi_static` attribute).
4752-
4753-
The C++ constructor callback should be a static method on the class that calls
4754-
the actual class constructor, then wraps the new C++ instance in a JavaScript
4755-
object, and returns the wrapper object. See `napi_wrap()` for details.
4743+
Defines a JavaScript class, including:
4744+
4745+
* A JavaScript constructor function that has the class name. When wrapping a
4746+
corresponding C++ class, the callback passed via `constructor` can be used to
4747+
instantiate a new C++ class instance, which can then be placed inside the
4748+
JavaScript object instance being constructed using [`napi_wrap`][].
4749+
* Properties on the constructor function whose implementation can call
4750+
corresponding _static_ data properties, accessors, and methods of the C++
4751+
class (defined by property descriptors with the `napi_static` attribute).
4752+
* Properties on the constructor function's `prototype` object. When wrapping a
4753+
C++ class, _non-static_ data properties, accessors, and methods of the C++
4754+
class can be called from the static functions given in the property
4755+
descriptors without the `napi_static` attribute after retrieving the C++ class
4756+
instance placed inside the JavaScript object instance by using
4757+
[`napi_unwrap`][].
4758+
4759+
When wrapping a C++ class, the C++ constructor callback passed via `constructor`
4760+
should be a static method on the class that calls the actual class constructor,
4761+
then wraps the new C++ instance in a JavaScript object, and returns the wrapper
4762+
object. See [`napi_wrap`][] for details.
47564763

47574764
The JavaScript constructor function returned from [`napi_define_class`][] is
4758-
often saved and used later, to construct new instances of the class from native
4759-
code, and/or check whether provided values are instances of the class. In that
4760-
case, to prevent the function value from being garbage-collected, create a
4761-
persistent reference to it using [`napi_create_reference`][] and ensure the
4762-
reference count is kept >= 1.
4765+
often saved and used later to construct new instances of the class from native
4766+
code, and/or to check whether provided values are instances of the class. In
4767+
that case, to prevent the function value from being garbage-collected, a
4768+
strong persistent reference to it can be created using
4769+
[`napi_create_reference`][], ensuring that the reference count is kept >= 1.
47634770

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

0 commit comments

Comments
 (0)
Please sign in to comment.