pub struct Function<'scope, Args: JsValuesTupleIntoVec = Unknown<'scope>, Return = Unknown<'scope>> { /* private fields */ }Expand description
A JavaScript function.
It can only live in the scope of a function call.
If you want to use it outside the scope of a function call, you can turn it into a reference.
By calling the create_ref method.
Implementations§
Source§impl<Args: JsValuesTupleIntoVec, Return> Function<'_, Args, Return>
impl<Args: JsValuesTupleIntoVec, Return> Function<'_, Args, Return>
Sourcepub fn create_ref(&self) -> Result<FunctionRef<Args, Return>>
pub fn create_ref(&self) -> Result<FunctionRef<Args, Return>>
Create a reference to the JavaScript function.
Sourcepub fn new_instance<'a>(&self, args: Args) -> Result<Unknown<'a>>
pub fn new_instance<'a>(&self, args: Args) -> Result<Unknown<'a>>
Create a new instance of the JavaScript Class.
Sourcepub fn build_threadsafe_function<T: 'static>(
&self,
) -> ThreadsafeFunctionBuilder<'_, T, Args, Return>
pub fn build_threadsafe_function<T: 'static>( &self, ) -> ThreadsafeFunctionBuilder<'_, T, Args, Return>
Create a threadsafe function from the JavaScript function.
Source§impl<Args: JsValuesTupleIntoVec, Return: FromNapiValue> Function<'_, Args, Return>
impl<Args: JsValuesTupleIntoVec, Return: FromNapiValue> Function<'_, Args, Return>
Sourcepub fn call(&self, args: Args) -> Result<Return>
pub fn call(&self, args: Args) -> Result<Return>
Call the JavaScript function.
this in the JavaScript function will be undefined.
If you want to specify this, you can use the apply method.
Sourcepub fn apply<Context: ToNapiValue>(
&self,
this: Context,
args: Args,
) -> Result<Return>
pub fn apply<Context: ToNapiValue>( &self, this: Context, args: Args, ) -> Result<Return>
Call the JavaScript function.
this in the JavaScript function will be the provided this.
Sourcepub fn bind<T: ToNapiValue>(
&self,
this: T,
) -> Result<Function<'_, Args, Return>>
pub fn bind<T: ToNapiValue>( &self, this: T, ) -> Result<Function<'_, Args, Return>>
Call Function.bind
Trait Implementations§
Source§impl<'scope, Args: Clone + JsValuesTupleIntoVec, Return: Clone> Clone for Function<'scope, Args, Return>
impl<'scope, Args: Clone + JsValuesTupleIntoVec, Return: Clone> Clone for Function<'scope, Args, Return>
Source§impl<Args: JsValuesTupleIntoVec, Return> FromNapiValue for Function<'_, Args, Return>
impl<Args: JsValuesTupleIntoVec, Return> FromNapiValue for Function<'_, Args, Return>
Source§unsafe fn from_napi_value(env: napi_env, value: napi_value) -> Result<Self>
unsafe fn from_napi_value(env: napi_env, value: napi_value) -> Result<Self>
This function called to convert napi values to native rust values Read more
fn from_unknown(value: Unknown<'_>) -> Result<Self>
Source§impl<'env, Args: JsValuesTupleIntoVec, Return> JsObjectValue<'env> for Function<'env, Args, Return>
impl<'env, Args: JsValuesTupleIntoVec, Return> JsObjectValue<'env> for Function<'env, Args, Return>
Source§fn set_property<'k, 'v, K, V>(&mut self, key: K, value: V) -> Result<()>
fn set_property<'k, 'v, K, V>(&mut self, key: K, value: V) -> Result<()>
Set the property value to the
ObjectSource§fn get_property<'k, K, T>(&self, key: K) -> Result<T>
fn get_property<'k, K, T>(&self, key: K) -> Result<T>
Get the property value from the
Object Read moreSource§fn get_property_unchecked<'k, K, T>(&self, key: K) -> Result<T>where
K: JsValue<'k>,
T: FromNapiValue,
fn get_property_unchecked<'k, K, T>(&self, key: K) -> Result<T>where
K: JsValue<'k>,
T: FromNapiValue,
Get the property value from the
Object without validationSource§fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>where
T: ToNapiValue,
fn set_named_property<T>(&mut self, name: &str, value: T) -> Result<()>where
T: ToNapiValue,
Set the property value to the
ObjectSource§fn set_c_named_property<T>(&mut self, name: &CStr, value: T) -> Result<()>where
T: ToNapiValue,
fn set_c_named_property<T>(&mut self, name: &CStr, value: T) -> Result<()>where
T: ToNapiValue,
Set the property value to the
Object, the property name is a CStr
This is useful when the property name comes from a C librarySource§fn create_named_method<K>(&mut self, name: K, function: Callback) -> Result<()>
fn create_named_method<K>(&mut self, name: K, function: Callback) -> Result<()>
Create a named method on the
ObjectSource§fn create_c_named_method(
&mut self,
name: &CStr,
function: Callback,
) -> Result<()>
fn create_c_named_method( &mut self, name: &CStr, function: Callback, ) -> Result<()>
Create a named method on the
Object, the name is a CStr
This is useful when the method name comes from a C librarySource§fn get_named_property<T>(&self, name: &str) -> Result<T>where
T: FromNapiValue + ValidateNapiValue,
fn get_named_property<T>(&self, name: &str) -> Result<T>where
T: FromNapiValue + ValidateNapiValue,
Get the property value from the
Object Read moreSource§fn get_c_named_property<T>(&self, name: &CStr) -> Result<T>where
T: FromNapiValue + ValidateNapiValue,
fn get_c_named_property<T>(&self, name: &CStr) -> Result<T>where
T: FromNapiValue + ValidateNapiValue,
Get the property value from the
Object Read moreSource§fn get_named_property_unchecked<T>(&self, name: &str) -> Result<T>where
T: FromNapiValue,
fn get_named_property_unchecked<T>(&self, name: &str) -> Result<T>where
T: FromNapiValue,
Get the property value from the
Object without validationSource§fn get_c_named_property_unchecked<T>(&self, name: &CStr) -> Result<T>where
T: FromNapiValue,
fn get_c_named_property_unchecked<T>(&self, name: &CStr) -> Result<T>where
T: FromNapiValue,
Get the property value from the
Object without validation Read moreSource§fn has_named_property<N: AsRef<str>>(&self, name: N) -> Result<bool>
fn has_named_property<N: AsRef<str>>(&self, name: N) -> Result<bool>
Check if the
Object has the named propertySource§fn has_c_named_property(&self, name: &CStr) -> Result<bool>
fn has_c_named_property(&self, name: &CStr) -> Result<bool>
Check if the
Object has the named property Read moreSource§fn delete_property<'s, S>(&mut self, name: S) -> Result<bool>where
S: JsValue<'s>,
fn delete_property<'s, S>(&mut self, name: S) -> Result<bool>where
S: JsValue<'s>,
Delete the property from the
Object, the property name can be a JsValueSource§fn delete_named_property<K: AsRef<str>>(&mut self, name: K) -> Result<bool>
fn delete_named_property<K: AsRef<str>>(&mut self, name: K) -> Result<bool>
Delete the property from the
ObjectSource§fn delete_c_named_property(&mut self, name: &CStr) -> Result<bool>
fn delete_c_named_property(&mut self, name: &CStr) -> Result<bool>
Delete the property from the
Object Read moreSource§fn has_own_property(&self, key: &str) -> Result<bool>
fn has_own_property(&self, key: &str) -> Result<bool>
Check if the
Object has the own propertySource§fn has_c_own_property(&self, key: &CStr) -> Result<bool>
fn has_c_own_property(&self, key: &CStr) -> Result<bool>
Check if the
Object has the own property Read moreSource§fn has_own_property_js<'k, K>(&self, key: K) -> Result<bool>where
K: JsValue<'k>,
fn has_own_property_js<'k, K>(&self, key: K) -> Result<bool>where
K: JsValue<'k>,
The same as
has_own_property, but accepts a JsValue as the property name.Source§fn has_property(&self, name: &str) -> Result<bool>
fn has_property(&self, name: &str) -> Result<bool>
This API checks if the Object passed in has the named property.
Source§fn has_property_js<'k, K>(&self, name: K) -> Result<bool>where
K: JsValue<'k>,
fn has_property_js<'k, K>(&self, name: K) -> Result<bool>where
K: JsValue<'k>,
This API is the same as
has_property, but accepts a JsValue as the property name.
So you can pass the JsNumber or JsSymbol as the property name.Source§fn get_property_names(&self) -> Result<Object<'env>>
fn get_property_names(&self) -> Result<Object<'env>>
This API returns the names of the enumerable properties of object as an array of strings.
The properties of object whose key is a symbol will not be included.
Source§fn get_all_property_names(
&self,
mode: KeyCollectionMode,
filter: KeyFilter,
conversion: KeyConversion,
) -> Result<Object<'env>>
fn get_all_property_names( &self, mode: KeyCollectionMode, filter: KeyFilter, conversion: KeyConversion, ) -> Result<Object<'env>>
https://nodejs.org/api/n-api.html#n_api_napi_get_all_property_names
This API returns an array containing the names of the available properties of this object.
Source§fn get_prototype(&self) -> Result<Unknown<'env>>
fn get_prototype(&self) -> Result<Unknown<'env>>
This returns the equivalent of
Object.getPrototypeOf (which is not the same as the function’s prototype property).Source§fn get_prototype_unchecked<T>(&self) -> Result<T>where
T: FromNapiValue,
fn get_prototype_unchecked<T>(&self) -> Result<T>where
T: FromNapiValue,
Get the prototype of the
ObjectSource§fn set_element<'t, T>(&mut self, index: u32, value: T) -> Result<()>where
T: JsValue<'t>,
fn set_element<'t, T>(&mut self, index: u32, value: T) -> Result<()>where
T: JsValue<'t>,
Set the element at the given index
Source§fn has_element(&self, index: u32) -> Result<bool>
fn has_element(&self, index: u32) -> Result<bool>
Check if the
Array has the element at the given indexSource§fn delete_element(&mut self, index: u32) -> Result<bool>
fn delete_element(&mut self, index: u32) -> Result<bool>
Delete the element at the given index
Source§fn get_element<T>(&self, index: u32) -> Result<T>where
T: FromNapiValue,
fn get_element<T>(&self, index: u32) -> Result<T>where
T: FromNapiValue,
Get the element at the given index Read more
Source§fn define_properties(&mut self, properties: &[Property]) -> Result<()>
fn define_properties(&mut self, properties: &[Property]) -> Result<()>
This method allows the efficient definition of multiple properties on a given object.
Source§fn get_array_length(&self) -> Result<u32>
fn get_array_length(&self) -> Result<u32>
Perform
is_array check before get the length Read moreSource§fn get_array_length_unchecked(&self) -> Result<u32>
fn get_array_length_unchecked(&self) -> Result<u32>
use this API if you can ensure this
Object is ArraySource§fn wrap<T: 'static>(
&mut self,
native_object: T,
size_hint: Option<usize>,
) -> Result<()>
fn wrap<T: 'static>( &mut self, native_object: T, size_hint: Option<usize>, ) -> Result<()>
Wrap the native value
T to this Object
the T will be dropped when this Object is finalizedSource§fn unwrap<T: 'static>(&self) -> Result<&mut T>
fn unwrap<T: 'static>(&self) -> Result<&mut T>
Get the wrapped native value from the
Object Read moreSource§fn remove_wrapped<T: 'static>(&mut self) -> Result<()>
fn remove_wrapped<T: 'static>(&mut self) -> Result<()>
Remove the wrapped native value from the
Object Read moreSource§fn add_finalizer<T, Hint, F>(
&mut self,
native: T,
finalize_hint: Hint,
finalize_cb: F,
) -> Result<()>where
T: 'static,
Hint: 'static,
F: FnOnce(FinalizeContext<T, Hint>) + 'static,
fn add_finalizer<T, Hint, F>(
&mut self,
native: T,
finalize_hint: Hint,
finalize_cb: F,
) -> Result<()>where
T: 'static,
Hint: 'static,
F: FnOnce(FinalizeContext<T, Hint>) + 'static,
Adds a
finalize_cb callback which will be called when the JavaScript object in js_object has been garbage-collected. Read moreSource§fn freeze(&mut self) -> Result<()>
fn freeze(&mut self) -> Result<()>
This method freezes a given object.
This prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed.
It also prevents the object’s prototype from being changed. This is described in Section 19.1.2.6 of the ECMA-262 specification.
Source§impl<'env, Args: JsValuesTupleIntoVec, Return> JsValue<'env> for Function<'env, Args, Return>
impl<'env, Args: JsValuesTupleIntoVec, Return> JsValue<'env> for Function<'env, Args, Return>
fn value(&self) -> Value
fn raw(&self) -> napi_value
Source§fn to_unknown(&self) -> Unknown<'env>
fn to_unknown(&self) -> Unknown<'env>
Convert the value to an unknown
Source§fn coerce_to_bool(&self) -> Result<bool>
fn coerce_to_bool(&self) -> Result<bool>
Coerce the value to a boolean
fn coerce_to_number(&self) -> Result<JsNumber<'_>>
fn coerce_to_string(&self) -> Result<JsString<'_>>
fn coerce_to_object(&self) -> Result<Object<'env>>
fn is_date(&self) -> Result<bool>
fn is_promise(&self) -> Result<bool>
fn is_error(&self) -> Result<bool>
fn is_typedarray(&self) -> Result<bool>
fn is_dataview(&self) -> Result<bool>
fn is_array(&self) -> Result<bool>
fn is_buffer(&self) -> Result<bool>
fn is_arraybuffer(&self) -> Result<bool>
fn instanceof<'c, Constructor>(&self, constructor: Constructor) -> Result<bool>where
Constructor: JsValue<'c>,
fn escape<'scope, E: JsValue<'scope> + FromNapiValue>( &self, escapable_handle_scope: EscapableHandleScope<'scope>, ) -> Result<E>
Source§impl<Args: JsValuesTupleIntoVec, Return> TypeName for Function<'_, Args, Return>
impl<Args: JsValuesTupleIntoVec, Return> TypeName for Function<'_, Args, Return>
Source§impl<Args: JsValuesTupleIntoVec, Return> ValidateNapiValue for Function<'_, Args, Return>
impl<Args: JsValuesTupleIntoVec, Return> ValidateNapiValue for Function<'_, Args, Return>
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
This function called to validate whether napi value passed to rust is valid type. Read more
impl<'scope, Args: Copy + JsValuesTupleIntoVec, Return: Copy> Copy for Function<'scope, Args, Return>
Auto Trait Implementations§
impl<'scope, Args, Return> Freeze for Function<'scope, Args, Return>
impl<'scope, Args, Return> RefUnwindSafe for Function<'scope, Args, Return>where
Args: RefUnwindSafe,
Return: RefUnwindSafe,
impl<'scope, Args = Unknown<'scope>, Return = Unknown<'scope>> !Send for Function<'scope, Args, Return>
impl<'scope, Args = Unknown<'scope>, Return = Unknown<'scope>> !Sync for Function<'scope, Args, Return>
impl<'scope, Args, Return> Unpin for Function<'scope, Args, Return>
impl<'scope, Args, Return> UnsafeUnpin for Function<'scope, Args, Return>
impl<'scope, Args, Return> UnwindSafe for Function<'scope, Args, Return>where
Args: UnwindSafe,
Return: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<'env, T> JsValuesTuple for Twhere
T: JsValue<'env>,
impl<'env, T> JsValuesTuple for Twhere
T: JsValue<'env>,
fn env(&self) -> *mut napi_env__
Source§impl<T> JsValuesTupleIntoVec for Twhere
T: ToNapiValue,
impl<T> JsValuesTupleIntoVec for Twhere
T: ToNapiValue,
fn into_vec(self, env: *mut napi_env__) -> Result<Vec<*mut napi_value__>, Error>
Source§impl<'env, T> ToNapiValue for Twhere
T: JsValue<'env>,
impl<'env, T> ToNapiValue for Twhere
T: JsValue<'env>,
Source§unsafe fn to_napi_value(
_env: *mut napi_env__,
val: T,
) -> Result<*mut napi_value__, Error>
unsafe fn to_napi_value( _env: *mut napi_env__, val: T, ) -> Result<*mut napi_value__, Error>
This function called to convert rust values to napi values Read more