pub struct JsClass(/* private fields */);
Implementations§
Source§impl JsClass
impl JsClass
Sourcepub fn new<F, P, T, R>(
env: NapiEnv,
name: impl AsRef<str>,
func: F,
properties: P,
) -> NapiResult<JsClass>where
T: FromJsArgs,
R: NapiValueT,
F: FnMut(JsObject, T) -> NapiResult<R>,
P: AsRef<[NapiPropertyDescriptor]>,
pub fn new<F, P, T, R>(
env: NapiEnv,
name: impl AsRef<str>,
func: F,
properties: P,
) -> NapiResult<JsClass>where
T: FromJsArgs,
R: NapiValueT,
F: FnMut(JsObject, T) -> NapiResult<R>,
P: AsRef<[NapiPropertyDescriptor]>,
Defines a JavaScript class, including:
- A JavaScript constructor function that has the class name. When wrapping a corresponding C++ class, the callback passed via constructor can be used to instantiate a new C++ class instance, which can then be placed inside the JavaScript object instance being constructed using napi_wrap.
- Properties on the constructor function whose implementation can call corresponding static data properties, accessors, and methods of the C++ class (defined by property descriptors with the napi_static attribute).
- Properties on the constructor function’s prototype object. When wrapping a C++ class, non-static data properties, accessors, and methods of the C++ class can be called from the static functions given in the property descriptors without the napi_static attribute after retrieving the C++ class instance placed inside the JavaScript object instance by using napi_unwrap.
When wrapping a C++ class, the C++ constructor callback passed via constructor should be a static method on the class that calls the actual class constructor, then wraps the new C++ instance in a JavaScript object, and returns the wrapper object. See napi_wrap for details.
The JavaScript constructor function returned from napi_define_class is often saved and used later to construct new instances of the class from native code, and/or to check whether provided values are instances of the class. In that case, to prevent the function value from being garbage-collected, a strong persistent reference to it can be created using napi_create_reference, ensuring that the reference count is kept >= 1.
Any non-NULL data which is passed to this API via the data parameter or via the data field of the napi_property_descriptor array items can be associated with the resulting JavaScript constructor (which is returned in the result parameter) and freed whenever the class is garbage-collected by passing both the JavaScript function and the data to napi_add_finalizer.
Sourcepub fn new_instance<T>(&self, args: T) -> NapiResult<JsObject>where
T: ToJsArgs,
pub fn new_instance<T>(&self, args: T) -> NapiResult<JsObject>where
T: ToJsArgs,
This method is used to instantiate a new JavaScript value using a given napi_value that represents the constructor for the object.