Module static_runtime

Source
Expand description

This module contains a macro for creating a static runtime instance It creates a safe, thread-local runtime static.

Can be used with default RuntimeOptions like so:

use rustyscript::{RuntimeOptions, Error, static_runtime};
use std::time::Duration;

static_runtime!(MY_DEFAULT_RUNTIME);

fn main() -> Result<(), Error> {
    MY_DEFAULT_RUNTIME::with(|runtime| {
        runtime.eval::<()>("console.log('Hello, world!')")
    })
}

Or with custom RuntimeOptions:

use rustyscript::{Error, RuntimeOptions, static_runtime};
use std::time::Duration;

static_runtime!(MY_CUSTOM_RUNTIME, {
   RuntimeOptions {
       timeout: Duration::from_secs(5),
       ..Default::default()
   }
});

fn main() -> Result<(), Error> {
    MY_CUSTOM_RUNTIME::with(|runtime| {
        runtime.eval::<()>("console.log('Hello, world!')")
    })
}

Structsยง

StaticRuntime
A static runtime instance Uses OnceCell to ensure that the runtime is only initialized once And RefCell to ensure that the runtime is never accessed concurrently And finally, a Result to catch initialization errors.
StaticRuntimeLock
A lock for the static runtime Created using StaticRuntime::lock Should be mutable to be of any use