pub struct Ctx<'js> { /* private fields */ }
Expand description
Context in use, passed to Context::with
.
Implementations§
Source§impl<'js> Ctx<'js>
impl<'js> Ctx<'js>
Sourcepub fn eval<V: FromJs<'js>, S: Into<Vec<u8>>>(&self, source: S) -> Result<V>
pub fn eval<V: FromJs<'js>, S: Into<Vec<u8>>>(&self, source: S) -> Result<V>
Evaluate a script in global context.
Sourcepub fn eval_promise<S: Into<Vec<u8>>>(&self, source: S) -> Result<Promise<'js>>
pub fn eval_promise<S: Into<Vec<u8>>>(&self, source: S) -> Result<Promise<'js>>
Evaluate a script in global context with top level await support.
This function always returns a promise which resolves to the result of the evaluated expression.
Sourcepub fn eval_with_options<V: FromJs<'js>, S: Into<Vec<u8>>>(
&self,
source: S,
options: EvalOptions,
) -> Result<V>
pub fn eval_with_options<V: FromJs<'js>, S: Into<Vec<u8>>>( &self, source: S, options: EvalOptions, ) -> Result<V>
Evaluate a script with the given options.
Sourcepub fn eval_file<V: FromJs<'js>, P: AsRef<Path>>(&self, path: P) -> Result<V>
pub fn eval_file<V: FromJs<'js>, P: AsRef<Path>>(&self, path: P) -> Result<V>
Evaluate a script directly from a file.
pub fn eval_file_with_options<V: FromJs<'js>, P: AsRef<Path>>( &self, path: P, options: EvalOptions, ) -> Result<V>
Sourcepub fn catch(&self) -> Value<'js>
pub fn catch(&self) -> Value<'js>
Returns the last raised JavaScript exception, if there is no exception the JavaScript value null
is returned.
§Usage
if let Err(Error::Exception) = ctx.eval::<(),_>("throw 3"){
assert_eq!(ctx.catch().as_int(),Some(3));
}
Sourcepub fn throw(&self, value: Value<'js>) -> Error
pub fn throw(&self, value: Value<'js>) -> Error
Throws a JavaScript value as a new exception.
Always returns Error::Exception
;
Sourcepub fn json_parse<S>(&self, json: S) -> Result<Value<'js>>
pub fn json_parse<S>(&self, json: S) -> Result<Value<'js>>
Parse json into a JavaScript value.
Sourcepub fn json_stringify<V>(&self, value: V) -> Result<Option<String<'js>>>where
V: IntoJs<'js>,
pub fn json_stringify<V>(&self, value: V) -> Result<Option<String<'js>>>where
V: IntoJs<'js>,
Stringify a JavaScript value into its JSON representation
Sourcepub fn json_stringify_replacer<V, R>(
&self,
value: V,
replacer: R,
) -> Result<Option<String<'js>>>
pub fn json_stringify_replacer<V, R>( &self, value: V, replacer: R, ) -> Result<Option<String<'js>>>
Stringify a JavaScript value into its JSON representation with a possible replacer.
The replacer is the same as the replacer argument for JSON.stringify
.
It is is a function that alters the behavior of the stringification process.
Sourcepub fn json_stringify_replacer_space<V, R, S>(
&self,
value: V,
replacer: R,
space: S,
) -> Result<Option<String<'js>>>
pub fn json_stringify_replacer_space<V, R, S>( &self, value: V, replacer: R, space: S, ) -> Result<Option<String<'js>>>
Stringify a JavaScript value into its JSON representation with a possible replacer and spaces
The replacer is the same as the replacer argument for JSON.stringify
.
It is is a function that alters the behavior of the stringification process.
Space is either a number or a string which is used to insert whitespace into the output
string for readability purposes. This behaves the same as the space argument for
JSON.stringify
.
Sourcepub fn promise(&self) -> Result<(Promise<'js>, Function<'js>, Function<'js>)>
pub fn promise(&self) -> Result<(Promise<'js>, Function<'js>, Function<'js>)>
Creates javascipt promise along with its reject and resolve functions.
Sourcepub fn execute_pending_job(&self) -> bool
pub fn execute_pending_job(&self) -> bool
Executes a quickjs job.
Returns wether a job was actually executed. If this function returned false, no job was pending.
Sourcepub fn spawn<F>(&self, future: F)
Available on crate feature futures
only.
pub fn spawn<F>(&self, future: F)
futures
only.Spawn future using configured async runtime
Sourcepub unsafe fn from_raw_invariant(
ctx: NonNull<JSContext>,
inv: Invariant<'js>,
) -> Self
pub unsafe fn from_raw_invariant( ctx: NonNull<JSContext>, inv: Invariant<'js>, ) -> Self
Create a new Ctx
from a pointer to the context and a invariant lifetime.
§Safety
User must ensure that a lock was acquired over the runtime and that invariant is a unique lifetime which can’t be coerced to a lifetime outside the scope of the lock of to the lifetime of another runtime.
Sourcepub unsafe fn from_raw(ctx: NonNull<JSContext>) -> Self
pub unsafe fn from_raw(ctx: NonNull<JSContext>) -> Self
Create a new Ctx
from a pointer to the context and a invariant lifetime.
§Safety
User must ensure that a lock was acquired over the runtime and that invariant is a unique lifetime which can’t be coerced to a lifetime outside the scope of the lock of to the lifetime of another runtime.
Sourcepub fn script_or_module_name(&self, stack_level: isize) -> Option<Atom<'js>>
pub fn script_or_module_name(&self, stack_level: isize) -> Option<Atom<'js>>
Returns the name of the current module or script that is running.
It called from a javascript callback it will return the current running javascript script name. Otherwise it will return none.
Sourcepub fn run_gc(&self)
pub fn run_gc(&self)
Runs the quickjs garbage collector for a cycle.
Quickjs uses reference counting with a collection cycle for cyclic references. This runs the cyclic reference collector cycle, types which are not part of a reference cycle will be freed the momement their reference count becomes zero.
Sourcepub fn store_userdata<U>(
&self,
data: U,
) -> StdResult<Option<Box<U>>, UserDataError<U>>
pub fn store_userdata<U>( &self, data: U, ) -> StdResult<Option<Box<U>>, UserDataError<U>>
Store a type in the runtime which can be retrieved later with Ctx::userdata
.
Returns the value from the argument if the userdata is currently being accessed and insertion is not possible. Otherwise returns the exising value for this type if it existed.
Sourcepub fn remove_userdata<U>(&self) -> StdResult<Option<Box<U>>, UserDataError<()>>
pub fn remove_userdata<U>(&self) -> StdResult<Option<Box<U>>, UserDataError<()>>
Remove the userdata of the given type from the userdata storage.
Returns Err(()) if the userdata is currently being accessed and removing isn’t possible. Returns Ok(None) if userdata of the given type wasn’t inserted.
Sourcepub fn userdata<U>(&self) -> Option<UserDataGuard<'_, U>>
pub fn userdata<U>(&self) -> Option<UserDataGuard<'_, U>>
Retrieves a borrow to the userdata of the given type from the userdata storage.
Returns None if userdata of the given type wasn’t inserted.
Trait Implementations§
Source§impl<'js> FromParam<'js> for Ctx<'js>
impl<'js> FromParam<'js> for Ctx<'js>
Source§fn param_requirement() -> ParamRequirement
fn param_requirement() -> ParamRequirement
Source§fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<Self>
fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<Self>
impl Send for Ctx<'_>
Auto Trait Implementations§
impl<'js> Freeze for Ctx<'js>
impl<'js> RefUnwindSafe for Ctx<'js>
impl<'js> !Sync for Ctx<'js>
impl<'js> Unpin for Ctx<'js>
impl<'js> !UnwindSafe for Ctx<'js>
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 more