pub struct JsRuntime { /* private fields */ }Expand description
This is the main entry point for the library. It manages the QuickJS runtime, as well as the registered handlers and host modules.
Implementations§
Source§impl JsRuntime
impl JsRuntime
Sourcepub fn new<H: Host + 'static>(host: H) -> Result<Self>
pub fn new<H: Host + 'static>(host: H) -> Result<Self>
Create a new JsRuntime with the given host.
The resulting runtime will have global objects registered.
Sourcepub fn register_json_host_function(
&mut self,
module_name: impl Into<String>,
function_name: impl Into<String>,
function: impl Fn(String) -> Result<String> + 'static,
) -> Result<()>
pub fn register_json_host_function( &mut self, module_name: impl Into<String>, function_name: impl Into<String>, function: impl Fn(String) -> Result<String> + 'static, ) -> Result<()>
Register a host function in the specified module. The function takes and returns a JSON string, which is deserialized and serialized by the runtime. The arguments are serialized as a JSON array containing all the arguments passed to the function.
Sourcepub fn register_host_function<Args, Output>(
&mut self,
module_name: impl Into<String>,
function_name: impl Into<String>,
function: impl Fn<Args, Output = Result<Output>> + 'static,
) -> Result<()>where
Args: DeserializeOwned,
Output: Serialize,
pub fn register_host_function<Args, Output>(
&mut self,
module_name: impl Into<String>,
function_name: impl Into<String>,
function: impl Fn<Args, Output = Result<Output>> + 'static,
) -> Result<()>where
Args: DeserializeOwned,
Output: Serialize,
Register a host function in the specified module.
The function takes and returns any type that can be (de)serialized by serde.
Sourcepub fn register_handler(
&mut self,
function_name: impl Into<String>,
handler_script: impl Into<String>,
handler_pwd: impl Into<String>,
) -> Result<()>
pub fn register_handler( &mut self, function_name: impl Into<String>, handler_script: impl Into<String>, handler_pwd: impl Into<String>, ) -> Result<()>
Register a handler function with the runtime.
The handler script is a JavaScript module that exports a function named handler.
The handler function takes a single argument, which is the event data deserialized from a JSON string.
Sourcepub fn run_handler(
&mut self,
function_name: String,
event: String,
run_gc: bool,
) -> Result<String>
pub fn run_handler( &mut self, function_name: String, event: String, run_gc: bool, ) -> Result<String>
Run a registered handler function with the given event data.
The event data is passed as a JSON string, and the handler function is expected to return a value that can be serialized to JSON.
The result is returned as a JSON string.
If run_gc is true, the runtime will run a garbage collection cycle after running the handler.