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>
impl<R: CallbackRet + 'static> Callback<(), R>
Sourcepub fn settling(value: JsIn<'_>) -> Option<Self>
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>
impl<A: CallbackArgs + 'static, R: CallbackRet + 'static> Callback<A, R>
Sourcepub fn call(&self, args: A) -> Result<R, String>
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.
Sourcepub fn call_traced(&self, args: A) -> Result<R, Exception>
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.
Sourcepub fn post(&self, args: A)
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.
Sourcepub fn call_async(&self, args: A) -> Answer<Result<R, String>> ⓘ
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.
Sourcepub fn call_async_traced(&self, args: A) -> Answer<Result<R, Exception>> ⓘ
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).