pub struct NativeFunction { /* private fields */ }Expand description
A callable Rust function that can be invoked by the engine.
NativeFunction functions are divided in two:
- Function pointers a.k.a common functions (see
NativeFunctionPointer). - Closure functions that can capture the current environment.
§Caveats
By limitations of the Rust language, the garbage collector currently cannot inspect closures
in order to trace their captured variables. This means that only Copy closures are 100% safe
to use. All other closures can also be stored in a NativeFunction, albeit by using an unsafe
API, but note that passing closures implicitly capturing traceable types could cause
Undefined Behaviour.
Implementations§
Source§impl NativeFunction
impl NativeFunction
Sourcepub fn from_fn_ptr(function: NativeFunctionPointer) -> Self
pub fn from_fn_ptr(function: NativeFunctionPointer) -> Self
Creates a NativeFunction from a function pointer.
Sourcepub fn from_async_fn<F>(f: F) -> Self
pub fn from_async_fn<F>(f: F) -> Self
Creates a NativeFunction from a function returning a Future-like.
The returned NativeFunction will return an ECMAScript Promise that will be fulfilled
or rejected when the returned Future completes.
If you only need to convert a Future-like into a JsPromise, see
JsPromise::from_async_fn.
§Examples
async fn test(
_this: &JsValue,
args: &[JsValue],
context: &RefCell<&mut Context>,
) -> JsResult<JsValue> {
let arg = args.get_or_undefined(0).clone();
std::future::ready(()).await;
let value = arg.to_u32(&mut context.borrow_mut())?;
Ok(JsValue::from(value * 2))
}
NativeFunction::from_async_fn(test);Sourcepub fn from_copy_closure<F>(closure: F) -> Self
pub fn from_copy_closure<F>(closure: F) -> Self
Creates a NativeFunction from a Copy closure.
Sourcepub fn from_copy_closure_with_captures<F, T>(closure: F, captures: T) -> Self
pub fn from_copy_closure_with_captures<F, T>(closure: F, captures: T) -> Self
Creates a NativeFunction from a Copy closure and a list of traceable captures.
Sourcepub unsafe fn from_closure<F>(closure: F) -> Self
pub unsafe fn from_closure<F>(closure: F) -> Self
Creates a new NativeFunction from a closure.
§Safety
Passing a closure that contains a captured variable that needs to be traced by the garbage collector could cause an use after free, memory corruption or other kinds of Undefined Behaviour. See https://github.com/Manishearth/rust-gc/issues/50 for a technical explanation on why that is the case.
Sourcepub unsafe fn from_closure_with_captures<F, T>(closure: F, captures: T) -> Self
pub unsafe fn from_closure_with_captures<F, T>(closure: F, captures: T) -> Self
Create a new NativeFunction from a closure and a list of traceable captures.
§Safety
Passing a closure that contains a captured variable that needs to be traced by the garbage collector could cause an use after free, memory corruption or other kinds of Undefined Behaviour. See https://github.com/Manishearth/rust-gc/issues/50 for a technical explanation on why that is the case.
Sourcepub fn call(
&self,
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue>
pub fn call( &self, this: &JsValue, args: &[JsValue], context: &mut Context, ) -> JsResult<JsValue>
Calls this NativeFunction, forwarding the arguments to the corresponding function.
Sourcepub fn to_js_function(self, realm: &Realm) -> JsFunction
pub fn to_js_function(self, realm: &Realm) -> JsFunction
Converts this NativeFunction into a JsFunction without setting its name or length.
Useful to create functions that will only be used once, such as callbacks.
Trait Implementations§
Source§impl Clone for NativeFunction
impl Clone for NativeFunction
Source§fn clone(&self) -> NativeFunction
fn clone(&self) -> NativeFunction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NativeFunction
impl Debug for NativeFunction
Source§impl Trace for NativeFunction
impl Trace for NativeFunction
Source§unsafe fn trace_non_roots(&self)
unsafe fn trace_non_roots(&self)
Source§fn run_finalizer(&self)
fn run_finalizer(&self)
Finalize::finalize on this object and all
contained subobjects.Auto Trait Implementations§
impl Freeze for NativeFunction
impl !RefUnwindSafe for NativeFunction
impl !Send for NativeFunction
impl !Sync for NativeFunction
impl Unpin for NativeFunction
impl UnsafeUnpin for NativeFunction
impl !UnwindSafe for NativeFunction
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.