Skip to main content

Callback

Struct Callback 

Source
pub struct Callback<A = (), R = ()> { /* private fields */ }
Expand description

A JS function a class’s member or a function takes (TS: Callback<f64> is (value: number) => void, Callback<(String, f64), bool> is (a: string, b: number) => boolean; #[args(name, amount)] on the parameter names them, (name: string, amount: number) => boolean): A is its argument (a JsValue type, an instance, a tuple of them for several, () for none), R what it returns. Cheap to clone; the JS function is released when the last clone goes. Kept in an instance’s field, it is reported to QuickJS’s collector, so a cycle through the instance’s JS object is collected; kept elsewhere, it keeps the function alive until dropped.

Implementations§

Source§

impl<R: CallbackRet + 'static> Callback<(), R>

Source

pub fn settling(value: JsIn<'_>) -> Option<Self>

A stand-in for a function that returns value: called with Callback::call_async (or call_async_traced), it answers value, a promise what it settles to (nsh/test’s expect(promise).rejects). Called on the spot (Callback::call) it throws, as calling what is no function does. A promise is handled from then on: its rejection comes here, not reported as unhandled. None where the host keeps no values ([JsHost::retain]).

Source§

impl<A: CallbackArgs + 'static, R: CallbackRet + 'static> Callback<A, R>

Source

pub fn call(&self, args: A) -> Result<R, String>

Calls the function now, inside a synchronous call from JS (a method, a function, the constructor), as Array.prototype.sort calls its comparison: its result, or what it threw (its message). The instance the call is on stays borrowed meanwhile: the function using it again throws.

§Panics

Outside a synchronous call from JS (in a task): use Callback::post or Callback::call_async there.

Source

pub fn call_traced(&self, args: A) -> Result<R, Exception>

Callback::call, with what the function threw whole: the message, the stack and the value thrown (Exception::thrown).

§Panics

As Callback::call.

Source

pub fn post(&self, args: A)

Calls the function in the next JS turn, in order with the other posts, forgetting its result (what it throws is reported as uncaught). From anywhere on the engine thread.

Source

pub fn call_async(&self, args: A) -> Answer<Result<R, String>> ⓘ

Calls the function in the next JS turn: its result (a promise it returns, settled), or the message of what it threw.

Source

pub fn call_async_traced(&self, args: A) -> Answer<Result<R, Exception>> ⓘ

Callback::call_async, with what the function threw (or its promise rejected with) whole: the message and the stack, which says where it was thrown (a test’s failing line).

Trait Implementations§

Source§

impl<A: CallbackArgs + 'static, R: CallbackRet + 'static> Arg for Callback<A, R>

Source§

fn ts_type(types: &mut TsTypes) -> String

Its TypeScript type.
Source§

fn from_js(value: JsIn<'_>) -> Result<Self, PropError>

The value of a JS argument.
Source§

impl<A, R> Clone for Callback<A, R>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A, R> Debug for Callback<A, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, R> Trace for Callback<A, R>

A callback held only here: shared ones are kept alive by whoever else holds them (reporting those would let the collector free a function still in use).

Source§

fn trace(&self, tracer: &mut Tracer<'_>)

Reports the callbacks to tracer.

Auto Trait Implementations§

§

impl<A = (), R = ()> !RefUnwindSafe for Callback<A, R>

§

impl<A = (), R = ()> !Send for Callback<A, R>

§

impl<A = (), R = ()> !Sync for Callback<A, R>

§

impl<A = (), R = ()> !UnwindSafe for Callback<A, R>

§

impl<A, R> Forget for Callback<A, R>

§

impl<A, R> Freeze for Callback<A, R>
where *const RcInner<dyn CallbackHandle> is !null: Freeze, PhantomData<RcInner<dyn CallbackHandle>>: Freeze, PhantomData<fn(A) -> R>: Freeze,

§

impl<A, R> Move for Callback<A, R>

§

impl<A, R> Unpin for Callback<A, R>
where Rc<dyn CallbackHandle>: Unpin,

§

impl<A, R> UnsafeUnpin for Callback<A, R>
where *const RcInner<dyn CallbackHandle> is !null: UnsafeUnpin, PhantomData<RcInner<dyn CallbackHandle>>: UnsafeUnpin, PhantomData<fn(A) -> R>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.