Skip to main content

Context

Struct Context 

Source
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

Source

pub fn new() -> Context

Initializes JSC (idempotent) and creates a context. Contexts should live for the process lifetime (teardown crashes in this prebuilt).

Source

pub fn with_options(options: &str) -> Context

Source

pub fn raw(&self) -> *mut c_void

Source

pub fn global(&self) -> Value

Source

pub fn eval(&self, src: &str) -> Result<Value, Error>

Source

pub fn eval_named(&self, src: &str, url: &str) -> Result<Value, Error>

Source

pub fn kind(&self, v: Value) -> u32

Source

pub fn to_number(&self, v: Value) -> Result<f64, Error>

Source

pub fn to_rust_string(&self, v: Value) -> Result<String, Error>

ToString + copy out to a Rust String.

Source

pub fn read_string(&self, v: Value) -> Result<String, Error>

Reads a JS string value’s characters (must already be a string).

Source

pub fn string(&self, s: &str) -> Value

Source

pub fn protect(&self, v: Value)

Source

pub fn unprotect(&self, v: Value)

Source

pub fn atom(&self, name: &str) -> Atom

Source

pub fn get(&self, base: Value, atom: &Atom) -> Result<Value, Error>

Source

pub fn set(&self, obj: Value, atom: &Atom, v: Value) -> Result<(), Error>

Source

pub fn define( &self, obj: Value, atom: &Atom, v: Value, attrs: u32, ) -> Result<(), Error>

Source

pub fn get_index(&self, base: Value, i: u32) -> Result<Value, Error>

Source

pub fn set_index(&self, obj: Value, i: u32, v: Value) -> Result<(), Error>

Source

pub fn global_get(&self, name: &str) -> Result<Value, Error>

Convenience: get by name on the global object.

Source

pub fn global_set(&self, name: &str, v: Value) -> Result<(), Error>

Convenience: define on the global object.

Source

pub fn object(&self) -> Value

Source

pub fn array(&self, len: u32) -> Result<Value, Error>

Source

pub fn array_length(&self, v: Value) -> Result<u32, Error>

Source

pub fn function<F>(&self, name: &str, arity: u32, f: F) -> Result<Value, Error>
where F: FnMut(*mut c_void, *mut c_void) -> u64 + 'static,

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.

Source

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.

Source

pub fn call( &self, f: Value, this: Value, args: &[Value], ) -> Result<Value, Error>

Source

pub fn construct(&self, ctor: Value, args: &[Value]) -> Result<Value, Error>

Source

pub fn error(&self, kind: u32, msg: &str) -> Value

Source

pub fn throw(&self, err: Value)

Source

pub fn error_info(&self, err: Value) -> (String, String)

(message, stack) of an exception value.

Source

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.

Source

pub fn arraybuffer_info(&self, v: Value) -> Result<RjsAbInfo, Error>

Source

pub fn arraybuffer_detach(&self, v: Value) -> Result<(), Error>

Source

pub fn typedarray(&self, kind: TaKind, len: usize) -> Result<Value, Error>

Source

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>

Source

pub fn typedarray_info(&self, v: Value) -> Result<RjsTaInfo, Error>

Source

pub fn promise(&self) -> Result<(Value, Value, Value), Error>

Returns (promise, resolveFn, rejectFn).

Source

pub fn promise_info(&self, v: Value) -> Result<(u32, Value), Error>

(status, result): 0 pending, 1 fulfilled, 2 rejected.

Source

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.

Source

pub fn id(&self) -> ContextId

This context’s process-unique id (crate::context::ContextId) — unlike Context::raw, never shared with a later context.

Source

pub fn gc(&self)

Source

pub fn heap_stats(&self) -> RjsHeapStats

Source§

impl Context

Source

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.

Source

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.

Source

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.

Source

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.

Source

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§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

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

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more