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 closuresin order to trace their captured variables. This means that onlyCopy closures are 100% safeto use. All other closures can also be stored in aNativeFunction, albeit by using anunsafeAPI, but note that passing closures implicitly capturing traceable types could causeUndefined Behaviour.
Implementations§
Source§implNativeFunction
implNativeFunction
Sourcepub fnfrom_fn_ptr(function:NativeFunctionPointer) -> Self
pub fnfrom_fn_ptr(function:NativeFunctionPointer) -> Self
Creates aNativeFunction from a function pointer.
Sourcepub fnfrom_async_fn<F>(f: F) -> Self
pub fnfrom_async_fn<F>(f: F) -> Self
Creates aNativeFunction from a function returning aFuture-like.
The returnedNativeFunction will return an ECMAScriptPromise that will be fulfilledor rejected when the returnedFuture completes.
If you only need to convert aFuture-like into aJsPromise, seeJsPromise::from_async_fn.
§Examples
async fntest( _this:&JsValue, args:&[JsValue], context:&RefCell<&mutContext>,) -> JsResult<JsValue> {letarg = args.get_or_undefined(0).clone(); std::future::ready(()).await;letvalue = arg.to_u32(&mutcontext.borrow_mut())?;Ok(JsValue::from(value *2))}NativeFunction::from_async_fn(test);Sourcepub fnfrom_copy_closure<F>(closure: F) -> Self
pub fnfrom_copy_closure<F>(closure: F) -> Self
Creates aNativeFunction from aCopy closure.
Sourcepub fnfrom_copy_closure_with_captures<F, T>(closure: F, captures: T) -> Self
pub fnfrom_copy_closure_with_captures<F, T>(closure: F, captures: T) -> Self
Creates aNativeFunction from aCopy closure and a list of traceable captures.
Sourcepub unsafe fnfrom_closure<F>(closure: F) -> Self
pub unsafe fnfrom_closure<F>(closure: F) -> Self
Creates a newNativeFunction from a closure.
§Safety
Passing a closure that contains a captured variable that needs to be traced by the garbagecollector could cause an use after free, memory corruption or other kinds ofUndefinedBehaviour. Seehttps://github.com/Manishearth/rust-gc/issues/50 for a technical explanationon why that is the case.
Sourcepub unsafe fnfrom_closure_with_captures<F, T>(closure: F, captures: T) -> Self
pub unsafe fnfrom_closure_with_captures<F, T>(closure: F, captures: T) -> Self
Create a newNativeFunction 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 garbagecollector could cause an use after free, memory corruption or other kinds ofUndefinedBehaviour. Seehttps://github.com/Manishearth/rust-gc/issues/50 for a technical explanationon why that is the case.
Sourcepub fncall( &self, this: &JsValue, args: &[JsValue], context: &mutContext,) ->JsResult<JsValue>
pub fncall( &self, this: &JsValue, args: &[JsValue], context: &mutContext,) ->JsResult<JsValue>
Calls thisNativeFunction, forwarding the arguments to the corresponding function.
Sourcepub fnto_js_function(self, realm: &Realm) ->JsFunction
pub fnto_js_function(self, realm: &Realm) ->JsFunction
Converts thisNativeFunction into aJsFunction without setting its name or length.
Useful to create functions that will only be used once, such as callbacks.
Trait Implementations§
Source§implClone forNativeFunction
implClone forNativeFunction
Source§fnclone(&self) ->NativeFunction
fnclone(&self) ->NativeFunction
1.0.0 ·Source§fnclone_from(&mut self, source: &Self)
fnclone_from(&mut self, source: &Self)
source.Read moreSource§implDebug forNativeFunction
implDebug forNativeFunction
Source§implTrace forNativeFunction
implTrace forNativeFunction
Source§unsafe fntrace_non_roots(&self)
unsafe fntrace_non_roots(&self)
Source§fnrun_finalizer(&self)
fnrun_finalizer(&self)
Finalize::finalize on this object and allcontained subobjects.Auto Trait Implementations§
implFreeze forNativeFunction
impl !RefUnwindSafe forNativeFunction
impl !Send forNativeFunction
impl !Sync forNativeFunction
implUnpin forNativeFunction
impl !UnwindSafe forNativeFunction
Blanket Implementations§
Source§impl<T>BorrowMut<T> for Twhere T: ?Sized,
impl<T>BorrowMut<T> for Twhere T: ?Sized,
Source§fnborrow_mut(&mut self) ->&mut T
fnborrow_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§fninto_either(self, into_left:bool) ->Either<Self, Self>
fninto_either(self, into_left:bool) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left istrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left(&self) returnstrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§impl<T>Pipe for Twhere T: ?Sized,
impl<T>Pipe for Twhere T: ?Sized,
Source§fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
Source§fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
Source§fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
Source§fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
self, then passesself.as_ref() into the pipe function.Source§fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
self, then passesself.as_mut() into the pipefunction.Source§fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
self, then passesself.deref() into the pipe function.Source§impl<T>Tap for T
impl<T>Tap for T
Source§fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
Borrow<B> of a value.Read moreSource§fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
BorrowMut<B> of a value.Read moreSource§fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
AsRef<R> view of a value.Read moreSource§fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
AsMut<R> view of a value.Read moreSource§fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
Deref::Target of a value.Read moreSource§fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
Deref::Target of a value.Read moreSource§fntap_dbg(self, func: implFnOnce(&Self)) -> Self
fntap_dbg(self, func: implFnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in releasebuilds.