pub struct Context { /* private fields */ }Expand description
One VM + global object. Not Send: use from one thread (or hand-manage
the API lock).
Implementations§
Source§impl Context
impl Context
Sourcepub fn new() -> Context
pub fn new() -> Context
Initializes JSC (idempotent) and creates a context. Contexts should live for the process lifetime (teardown crashes in this prebuilt).
pub fn with_options(options: &str) -> Context
pub fn raw(&self) -> *mut c_void
pub fn global(&self) -> Value
pub fn eval(&self, src: &str) -> Result<Value, Error>
pub fn eval_named(&self, src: &str, url: &str) -> Result<Value, Error>
pub fn kind(&self, v: Value) -> u32
pub fn to_number(&self, v: Value) -> Result<f64, Error>
Sourcepub fn to_rust_string(&self, v: Value) -> Result<String, Error>
pub fn to_rust_string(&self, v: Value) -> Result<String, Error>
ToString + copy out to a Rust String.
Sourcepub fn read_string(&self, v: Value) -> Result<String, Error>
pub fn read_string(&self, v: Value) -> Result<String, Error>
Reads a JS string value’s characters (must already be a string).
pub fn string(&self, s: &str) -> Value
pub fn protect(&self, v: Value)
pub fn unprotect(&self, v: Value)
pub fn atom(&self, name: &str) -> Atom
pub fn get(&self, base: Value, atom: &Atom) -> Result<Value, Error>
pub fn set(&self, obj: Value, atom: &Atom, v: Value) -> Result<(), Error>
pub fn define( &self, obj: Value, atom: &Atom, v: Value, attrs: u32, ) -> Result<(), Error>
pub fn get_index(&self, base: Value, i: u32) -> Result<Value, Error>
pub fn set_index(&self, obj: Value, i: u32, v: Value) -> Result<(), Error>
Sourcepub fn global_get(&self, name: &str) -> Result<Value, Error>
pub fn global_get(&self, name: &str) -> Result<Value, Error>
Convenience: get by name on the global object.
Sourcepub fn global_set(&self, name: &str, v: Value) -> Result<(), Error>
pub fn global_set(&self, name: &str, v: Value) -> Result<(), Error>
Convenience: define on the global object.
pub fn object(&self) -> Value
pub fn array(&self, len: u32) -> Result<Value, Error>
pub fn array_length(&self, v: Value) -> Result<u32, Error>
Sourcepub fn function<F>(&self, name: &str, arity: u32, f: F) -> Result<Value, Error>
pub fn function<F>(&self, name: &str, arity: u32, f: F) -> Result<Value, Error>
Registers a Rust closure as a JS host function. The closure receives
(global, callframe); use HostArgs to decode arguments.
Recursive entry into the same mutable Rust closure throws a TypeError;
calling a different host function remains supported.
Sourcepub unsafe fn function_sysv(
&self,
name: &str,
arity: u32,
f: *const c_void,
) -> Result<Value, Error>
pub unsafe fn function_sysv( &self, name: &str, arity: u32, f: *const c_void, ) -> Result<Value, Error>
Registers a raw JSC host function (zero-overhead path).
§Safety
f must follow the JSC host convention for this target. Declare it
with #[redjs::host_fn]: SysV on Windows x64, the C ABI on Unix.
The historical method name is retained for existing embedders.
pub fn call( &self, f: Value, this: Value, args: &[Value], ) -> Result<Value, Error>
pub fn construct(&self, ctor: Value, args: &[Value]) -> Result<Value, Error>
pub fn error(&self, kind: u32, msg: &str) -> Value
pub fn throw(&self, err: Value)
Sourcepub fn error_info(&self, err: Value) -> (String, String)
pub fn error_info(&self, err: Value) -> (String, String)
(message, stack) of an exception value.
Sourcepub unsafe fn arraybuffer_external(
&self,
bytes: *mut c_void,
len: usize,
dealloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
dealloc_ctx: *mut c_void,
) -> Result<Value, Error>
pub unsafe fn arraybuffer_external( &self, bytes: *mut c_void, len: usize, dealloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>, dealloc_ctx: *mut c_void, ) -> Result<Value, Error>
Zero-copy ArrayBuffer over host memory. Caller keeps ownership when
dealloc is None — detach before freeing the memory.
§Safety
bytes..bytes+len must stay valid until GC-with-deallocator or detach.
pub fn arraybuffer_info(&self, v: Value) -> Result<RjsAbInfo, Error>
pub fn arraybuffer_detach(&self, v: Value) -> Result<(), Error>
pub fn typedarray(&self, kind: TaKind, len: usize) -> Result<Value, Error>
Sourcepub unsafe fn typedarray_external(
&self,
kind: TaKind,
bytes: *mut c_void,
byte_len: usize,
dealloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>,
dealloc_ctx: *mut c_void,
) -> Result<Value, Error>
pub unsafe fn typedarray_external( &self, kind: TaKind, bytes: *mut c_void, byte_len: usize, dealloc: Option<unsafe extern "C" fn(*mut c_void, *mut c_void)>, dealloc_ctx: *mut c_void, ) -> Result<Value, Error>
§Safety — see Context::arraybuffer_external.
pub fn typedarray_info(&self, v: Value) -> Result<RjsTaInfo, Error>
Sourcepub fn promise(&self) -> Result<(Value, Value, Value), Error>
pub fn promise(&self) -> Result<(Value, Value, Value), Error>
Returns (promise, resolveFn, rejectFn).
Sourcepub fn promise_info(&self, v: Value) -> Result<(u32, Value), Error>
pub fn promise_info(&self, v: Value) -> Result<(u32, Value), Error>
(status, result): 0 pending, 1 fulfilled, 2 rejected.
Sourcepub fn drain_microtasks(&self)
pub fn drain_microtasks(&self)
A microtask checkpoint (crate::web::checkpoint): drains the queue,
then runs the post-checkpoint hooks when no JS is on the stack.
Sourcepub fn id(&self) -> ContextId
pub fn id(&self) -> ContextId
This context’s process-unique id (crate::context::ContextId) —
unlike Context::raw, never shared with a later context.
pub fn gc(&self)
pub fn heap_stats(&self) -> RjsHeapStats
Source§impl Context
impl Context
Sourcepub fn bytecode_cache_key(&self, source: &str, url: &str) -> [u8; 32]
pub fn bytecode_cache_key(&self, source: &str, url: &str) -> [u8; 32]
Exact source/URL, pinned engine/build, architecture, bytecode version and effective JSC options. Compilation mode is a classic script; modules and native AOT images require separate formats and entry points.
Sourcepub fn compile_bytecode(
&self,
source: &str,
url: &str,
) -> Result<Bytecode, Error>
pub fn compile_bytecode( &self, source: &str, url: &str, ) -> Result<Bytecode, Error>
Compiles the program’s top-level code without running any JavaScript. Nested functions keep normal lazy compilation, bounding cold-cache cost.
Sourcepub fn compile_bytecode_recursive(
&self,
source: &str,
url: &str,
) -> Result<Bytecode, Error>
pub fn compile_bytecode_recursive( &self, source: &str, url: &str, ) -> Result<Bytecode, Error>
Compiles the program and all nested functions. Intended for build-time generation, or explicit measurements; large bundles cost more to compile and store than a program-only cache.
Sourcepub fn compile_bytecode_with_depth(
&self,
source: &str,
url: &str,
depth: u32,
) -> Result<Bytecode, Error>
pub fn compile_bytecode_with_depth( &self, source: &str, url: &str, depth: u32, ) -> Result<Bytecode, Error>
Compiles at most depth levels of nested function bodies. Zero caches
the program only; one also caches an outer bundle IIFE. Functions beyond
the bound remain lazy. u32::MAX requests recursive compilation.
Sourcepub fn eval_named_with_bytecode(
&self,
source: &str,
url: &str,
bytecode: Option<&Bytecode>,
) -> Result<(Value, bool), Error>
pub fn eval_named_with_bytecode( &self, source: &str, url: &str, bytecode: Option<&Bytecode>, ) -> Result<(Value, bool), Error>
Evaluates exactly once. An absent/mismatched cache falls back to source; a JS exception from evaluation is returned without executing it again. The boolean reports whether JSC accepted the cache’s version/source key. JSC receives its own owned copy, so the cache can be dropped immediately after this call even when nested functions execute much later.
Auto Trait Implementations§
impl !Send for Context
impl !Sync for Context
impl Forget for Context
impl Freeze for Context
impl Move for Context
impl RefUnwindSafe for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl UnwindSafe for Context
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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