Struct JsRuntime
pub struct JsRuntime { /* private fields */ }js and std only.Expand description
A javascript runtime, executing scripts on the current thread.
Create one via JsRuntime::builder, or use JsRuntime::eval_once
for a one-off script evaluation with the default configuration.
A runtime is single-threaded (!Send); services which need to
evaluate scripts from async (Send) contexts use a
JsWorker instead.
Implementations§
§impl JsRuntime
impl JsRuntime
pub fn builder() -> JsRuntimeBuilder
pub fn builder() -> JsRuntimeBuilder
Create a JsRuntimeBuilder to configure a new runtime.
pub fn warm_up() -> Result<(), JsError>
pub fn warm_up() -> Result<(), JsError>
Compile and validate the private engine component, if it has not been initialized in this process yet.
Applications may call this during startup to keep the one-time Wasm compilation cost out of a latency-sensitive first evaluation.
pub fn warm_up_with_disk_cache(
cache_root: impl AsRef<Path>,
cache_dir: impl AsRef<Path>,
) -> Result<(), JsError>
Available on crate feature disk-cache only.
pub fn warm_up_with_disk_cache( cache_root: impl AsRef<Path>, cache_dir: impl AsRef<Path>, ) -> Result<(), JsError>
disk-cache only.Compile and validate the private engine component using a persistent compilation cache, if it has not been initialized in this process yet.
cache_root must be an absolute path to an existing private directory
trusted by the application. cache_dir is resolved relative to that
root and created if necessary. Absolute paths, parent traversal, and
symbolic links escaping the root are rejected. Cached artifacts contain
executable native code and must not be writable by an untrusted user.
Wasmtime keys entries by the component and compatible compiler
configuration, so stale or incompatible entries are not reused.
This must be called before constructing any runtime or worker. Once the process-wide engine is initialized, selecting a different cache directory returns an error. A failed initialization may be retried.
pub fn eval_once(src: impl AsRef<str>) -> Result<JsValue, JsError>
pub fn eval_once(src: impl AsRef<str>) -> Result<JsValue, JsError>
Evaluate a single script with the default runtime configuration, returning the value of its final expression.
pub fn eval(&mut self, src: impl AsRef<str>) -> Result<JsValue, JsError>
pub fn eval(&mut self, src: impl AsRef<str>) -> Result<JsValue, JsError>
Evaluate a script, returning the value of its final expression.
Each source is evaluated as a classic Script. State, including top-level lexical declarations and global function definitions, persists across evaluations within the same runtime.
Scheduled promise jobs and other microtasks are drained before the operation returns. Their work consumes the same fuel and wall-clock budget as the script which scheduled them.
pub fn exec(&mut self, src: impl AsRef<str>) -> Result<(), JsError>
pub fn exec(&mut self, src: impl AsRef<str>) -> Result<(), JsError>
Execute a script while discarding its final expression value.
This avoids materializing a value snapshot when only script side effects are relevant.
pub fn call<I, V>(
&mut self,
name: impl AsRef<str>,
args: I,
) -> Result<JsValue, JsError>
pub fn call<I, V>( &mut self, name: impl AsRef<str>, args: I, ) -> Result<JsValue, JsError>
Call a global function with the given arguments.
Reachable functions are function-declared globals, var-assigned
function values, and registered host functions — anything that lands
as an own property of the global object. Top-level let/const/
class bindings live in the script’s declarative scope, not on the
global object, so const f = () => … is not callable by name
(it resolves to JsErrorKind::NotFound);
use a function declaration or assign to a var/global.
pub fn has_global_fn(&mut self, name: impl AsRef<str>) -> bool
pub fn has_global_fn(&mut self, name: impl AsRef<str>) -> bool
Returns true if a global function with the given name exists.
pub fn is_poisoned(&self) -> bool
pub fn is_poisoned(&self) -> bool
Returns true once an operation trapped inside the WebAssembly engine,
for example after exhausting fuel, time, stack, or memory. The script
was aborted mid-execution, so every later operation
fails with JsErrorKind::Setup.
pub fn set_host_global<T>(
&mut self,
name: impl Into<JsStr>,
object: JsHostObject<T>,
) -> Result<(), JsError>where
T: Send + 'static,
pub fn set_host_global<T>(
&mut self,
name: impl Into<JsStr>,
object: JsHostObject<T>,
) -> Result<(), JsError>where
T: Send + 'static,
Install a Rust-owned native object as a global in this runtime.
This operation is deliberately runtime-local: native objects are not
accepted by JsRuntimeBuilder, because a reusable builder may back
multiple runtimes.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JsRuntime
impl !Send for JsRuntime
impl !Sync for JsRuntime
impl !UnwindSafe for JsRuntime
impl Freeze for JsRuntime
impl Unpin for JsRuntime
impl UnsafeUnpin for JsRuntime
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more