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>
impl<'rt> Context<'rt>
Sourcepub fn set_module_loader(&mut self, loader: Box<dyn ModuleLoader>)
pub fn set_module_loader(&mut self, loader: Box<dyn ModuleLoader>)
Install how module specifiers are resolved and read.
Without one, importing is a clear error rather than a silent failure: the engine
cannot guess whether ./x.js means a file, and the host is what knows.
Sourcepub fn set_script_name(&mut self, name: impl Into<String>)
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.
Sourcepub fn set_script_args(&mut self, args: Vec<String>)
pub fn set_script_args(&mut self, args: Vec<String>)
Set the arguments scriptArgs should hold.
Element 0 is the script itself, matching qjs: qjs f.js a b gives
["f.js", "a", "b"]. With -e there is no script, so the array is just the
trailing arguments and -e’s own text never appears in it.
This was parsed by the CLI and then dropped on the floor: opts.script_args was
assigned in two places and read in none, so scriptArgs was simply undeclared.
An ordinary script’s if (typeof scriptArgs != "undefined") branch therefore never
ran, and examples/pi_bigint.js printed a thousand digits of pi where the
reference printed usage: pi n_digits.
Sourcepub fn eval_module(&mut self, specifier: &str) -> Result<Value>
pub fn eval_module(&mut self, specifier: &str) -> Result<Value>
Evaluate specifier as the entry module, the way qjs -m does.
The microtask queue is drained afterwards, so a module whose top level launched an async task has finished it by the time this returns.
Sourcepub fn eval(&mut self, source: &str) -> Result<Value>
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.
Sourcepub fn last_value(&self) -> &Value
pub fn last_value(&self) -> &Value
The value of the script that ran last.