Skip to main content

Context

Struct Context 

Source
pub struct Context<'rt> { /* private fields */ }
Expand description

An execution context: a global object plus the builtins installed on it.

Mirrors the role of JSContext in the QuickJS C API.

Implementations§

Source§

impl<'rt> Context<'rt>

Source

pub fn new(runtime: &'rt Runtime) -> Context<'rt>

Create a context on runtime.

Examples found in repository?
examples/embed.rs (line 6)
4fn main() -> Result<(), quickjs::Error> {
5    let runtime = Runtime::new();
6    let mut context = Context::new(&runtime);
7    context.eval("print([1, 2, 3].map(x => x * 2).join('-'));")?;
8    assert_eq!(context.output(), "2-4-6\n");
9    print!("{}", context.output());
10    Ok(())
11}
Source

pub fn set_script_name(&mut self, name: impl Into<String>)

Name the scripts this context evaluates, for stack traces.

Mirrors what qjs passes to JS_Eval: -e code is <cmdline>, a script file is its own path. Without a name, frames print <input>, which is what the reference uses for code with no file behind it.

Source

pub fn runtime(&self) -> &'rt Runtime

The runtime this context belongs to.

Source

pub fn output(&self) -> &str

Everything print has written.

Examples found in repository?
examples/embed.rs (line 8)
4fn main() -> Result<(), quickjs::Error> {
5    let runtime = Runtime::new();
6    let mut context = Context::new(&runtime);
7    context.eval("print([1, 2, 3].map(x => x * 2).join('-'));")?;
8    assert_eq!(context.output(), "2-4-6\n");
9    print!("{}", context.output());
10    Ok(())
11}
Source

pub fn eval(&mut self, source: &str) -> Result<Value>

Evaluate source as a global script and return its completion value.

The value is reported the way qjs -e reports it: a string for anything the script leaves on the stack, and nothing at all for undefined.

Examples found in repository?
examples/embed.rs (line 7)
4fn main() -> Result<(), quickjs::Error> {
5    let runtime = Runtime::new();
6    let mut context = Context::new(&runtime);
7    context.eval("print([1, 2, 3].map(x => x * 2).join('-'));")?;
8    assert_eq!(context.output(), "2-4-6\n");
9    print!("{}", context.output());
10    Ok(())
11}
Source

pub fn last_value(&self) -> &Value

The value of the script that ran last.

Auto Trait Implementations§

§

impl<'rt> !RefUnwindSafe for Context<'rt>

§

impl<'rt> !Send for Context<'rt>

§

impl<'rt> !Sync for Context<'rt>

§

impl<'rt> !UnwindSafe for Context<'rt>

§

impl<'rt> Forget for Context<'rt>

§

impl<'rt> Freeze for Context<'rt>

§

impl<'rt> Move for Context<'rt>

§

impl<'rt> Unpin for Context<'rt>

§

impl<'rt> UnsafeUnpin for Context<'rt>

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