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_with_options<V, S>(
&self,
source: S,
options: EvalOptions
) -> Result<V, Error>
pub fn eval_with_options<V, S>( &self, source: S, options: EvalOptions ) -> Result<V, Error>
Evaluate a script with the given options.
sourcepub fn eval_file<V, P>(&self, path: P) -> Result<V, Error>
pub fn eval_file<V, P>(&self, path: P) -> Result<V, Error>
Evaluate a script directly from a file.
pub fn eval_file_with_options<V, P>( &self, path: P, options: EvalOptions ) -> Result<V, Error>
sourcepub fn compile<N, S>(self, name: N, source: S) -> Result<Module<'js>, Error>
pub fn compile<N, S>(self, name: N, source: S) -> Result<Module<'js>, Error>
Compile a module for later use.
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>, Error>
pub fn json_parse<S>(&self, json: S) -> Result<Value<'js>, Error>
Parse json into a JavaScript value.
sourcepub fn json_parse_ext<S>(
&self,
json: S,
allow_extensions: bool
) -> Result<Value<'js>, Error>
pub fn json_parse_ext<S>( &self, json: S, allow_extensions: bool ) -> Result<Value<'js>, Error>
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.
sourcepub fn json_stringify<V>(&self, value: V) -> Result<Option<String<'js>>, Error>where
V: IntoJs<'js>,
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
sourcepub fn json_stringify_replacer<V, R>(
&self,
value: V,
replacer: R
) -> Result<Option<String<'js>>, Error>
pub fn json_stringify_replacer<V, R>( &self, value: V, replacer: R ) -> Result<Option<String<'js>>, Error>
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>>, Error>
pub fn json_stringify_replacer_space<V, R, S>( &self, value: V, replacer: R, space: S ) -> Result<Option<String<'js>>, Error>
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
.
pub fn promise( &self ) -> Result<(Object<'js>, Function<'js>, Function<'js>), Error>
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>
) -> Ctx<'js>
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.
sourcepub unsafe fn from_raw(ctx: NonNull<JSContext>) -> Ctx<'js>
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.
sourcepub unsafe fn free_unevaluated_modules(&self)
pub unsafe fn free_unevaluated_modules(&self)
Frees modules which aren’t evaluated.
When a module is compiled and the compilation results in an error the module can already have resolved several modules. Originally QuickJS freed all these module when compiling (but not when a it was dynamically imported), this library patched that behavior out because it proved to be hard to make safe. This function will free those modules.
§Safety
Caller must ensure that this method is not called from a module being evaluated.