Struct rquickjs::Ctx

source ·
pub struct Ctx<'js> { /* private fields */ }
Expand description

Context in use, passed to Context::with.

Implementations§

source§

impl<'js> Ctx<'js>

source

pub fn eval<V, S>(&self, source: S) -> Result<V, Error>
where V: FromJs<'js>, S: Into<Vec<u8>>,

Evaluate a script in global context.

source

pub fn eval_promise<S>(&self, source: S) -> Result<Promise<'js>, Error>
where S: Into<Vec<u8>>,

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.

source

pub fn eval_with_options<V, S>( &self, source: S, options: EvalOptions ) -> Result<V, Error>
where V: FromJs<'js>, S: Into<Vec<u8>>,

Evaluate a script with the given options.

source

pub fn eval_file<V, P>(&self, path: P) -> Result<V, Error>
where V: FromJs<'js>, P: AsRef<Path>,

Evaluate a script directly from a file.

source

pub fn eval_file_with_options<V, P>( &self, path: P, options: EvalOptions ) -> Result<V, Error>
where V: FromJs<'js>, P: AsRef<Path>,

source

pub fn globals(&self) -> Object<'js>

Returns the global object of this context.

source

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));
}
source

pub fn throw(&self, value: Value<'js>) -> Error

Throws a JavaScript value as a new exception. Always returns Error::Exception;

source

pub fn json_parse<S>(&self, json: S) -> Result<Value<'js>, Error>
where S: Into<Vec<u8>>,

Parse json into a JavaScript value.

source

pub fn json_parse_ext<S>( &self, json: S, allow_extensions: bool ) -> Result<Value<'js>, Error>
where S: Into<Vec<u8>>,

Parse json into a JavaScript value, possibly allowing extended syntax support.

If allow_extensions is true, this function will allow extended json syntax. Extended syntax allows comments, single quoted strings, non string property names, trailing comma’s and hex, oct and binary numbers.

source

pub fn json_stringify<V>(&self, value: V) -> Result<Option<String<'js>>, Error>
where V: IntoJs<'js>,

Stringify a JavaScript value into its JSON representation

source

pub fn json_stringify_replacer<V, R>( &self, value: V, replacer: R ) -> Result<Option<String<'js>>, Error>
where V: IntoJs<'js>, R: IntoJs<'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.

source

pub fn json_stringify_replacer_space<V, R, S>( &self, value: V, replacer: R, space: S ) -> Result<Option<String<'js>>, Error>
where V: IntoJs<'js>, R: IntoJs<'js>, S: IntoJs<'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.

source

pub fn promise( &self ) -> Result<(Promise<'js>, Function<'js>, Function<'js>), Error>

Creates javascipt promise along with its reject and resolve functions.

source

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.

source

pub fn spawn<F>(&self, future: F)
where F: Future<Output = ()> + 'js,

Available on crate feature futures only.

Spawn future using configured async runtime

source

pub unsafe fn from_raw_invariant( ctx: NonNull<JSContext>, inv: Invariant<'js> ) -> Ctx<'js>

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.

source

pub unsafe fn from_raw(ctx: NonNull<JSContext>) -> Ctx<'js>

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.

source

pub fn script_or_module_name(&self, stack_level: isize) -> Option<Atom<'js>>

source

pub fn run_gc(&self)

source

pub fn as_raw(&self) -> NonNull<JSContext>

Returns the pointer to the C library context.

Trait Implementations§

source§

impl<'js> Clone for Ctx<'js>

source§

fn clone(&self) -> Ctx<'js>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'js> Debug for Ctx<'js>

source§

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

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

impl<'js> Drop for Ctx<'js>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'js> FromParam<'js> for Ctx<'js>

source§

fn param_requirement() -> ParamRequirement

The parameters requirements this value requires.
source§

fn from_param<'a>( params: &mut ParamsAccessor<'a, 'js> ) -> Result<Ctx<'js>, Error>

Convert from a parameter value.
source§

impl<'js> Trace<'js> for Ctx<'js>

source§

fn trace<'a>(&self, tracer: Tracer<'a, 'js>)

source§

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> 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> 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

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

§

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.
source§

impl<T> ParallelSend for T
where T: Send,