Skip to content

Commit

Permalink
Add async stuff to module wrapper and bump core ver
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Jul 9, 2024
1 parent 29256d6 commit bc85ee1
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 31 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ thiserror = "1.0.61"
serde = "1.0.204"

# The deno runtime itself, and the webidl extension for the web APIs
deno_core = "0.292.0"
deno_core = "0.293.0"
deno_webidl = "0.158.0"

# For transpiling typescript
Expand Down
12 changes: 7 additions & 5 deletions src/inner_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub struct InnerRuntimeOptions {
/// as when the snapshot was created
/// If provided, user-supplied extensions must be instantiated with `init_ops` instead of `init_ops_and_esm`
pub startup_snapshot: Option<&'static [u8]>,

/// Optional configuration parameters for building the underlying v8 isolate
/// This can be used to alter the behavior of the runtime.
/// See the rusty_v8 documentation for more information
pub isolate_params: Option<v8::CreateParams>,
}

impl Default for InnerRuntimeOptions {
Expand All @@ -70,6 +75,7 @@ impl Default for InnerRuntimeOptions {
timeout: Duration::MAX,
module_cache: None,
startup_snapshot: None,
isolate_params: None,

extension_options: Default::default(),
}
Expand Down Expand Up @@ -106,6 +112,7 @@ impl InnerRuntime {
})),

source_map_getter: Some(loader),
create_params: options.isolate_params,

startup_snapshot: options.startup_snapshot,
extensions,
Expand Down Expand Up @@ -154,11 +161,6 @@ impl InnerRuntime {
Ok(())
}

// Registers an op2 function with the runtime without the need for an extension
pub fn register_op(&mut self, op: deno_core::_ops::OpDecl) -> Result<(), Error> {
self.put(op)
}

/// Register an async rust function
/// The function must return a Future that resolves to a serde_json::Value
/// and accept a vec of serde_json::Value as arguments
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
//! Use with caution.
//!
//! Please note that the 'web' feature will also enable fs_import and url_import, allowing arbitrary filesystem and network access for import statements
//! - This is because the deno_web crate allows both fetch and FS reads already
//!
//! | Feature | Description | Preserves Sandbox | Dependencies |
//! |----------------|---------------------------------------------------------------------------------------------------|------------------|---------------------------------------------------------------------------------|
Expand Down
128 changes: 118 additions & 10 deletions src/module_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use deno_core::{serde_json, v8::GetPropertyNamesArgs};

use crate::{js_value::Function, Error, Module, ModuleHandle, Runtime, RuntimeOptions};
use deno_core::{serde_json, v8::GetPropertyNamesArgs};

/// A wrapper type representing a runtime instance loaded with a single module
/// Exactly equivalent to [Runtime::new] followed by [Runtime::load_module]
///
/// Can also be created using the [[crate::import] function
pub struct ModuleWrapper {
module_context: ModuleHandle,
runtime: Runtime,
Expand All @@ -12,7 +14,6 @@ impl ModuleWrapper {
/// Creates a new `ModuleWrapper` from a given module and runtime options.
///
/// # Arguments
///
/// * `module` - A reference to the module to load.
/// * `options` - The runtime options for the module.
///
Expand All @@ -31,7 +32,6 @@ impl ModuleWrapper {
/// Creates a new `ModuleWrapper` from a file path and runtime options.
///
/// # Arguments
///
/// * `path` - The path to the module file.
/// * `options` - The runtime options for the module.
///
Expand All @@ -54,13 +54,12 @@ impl ModuleWrapper {
}

/// Retrieves a value from the module by name and deserializes it.
/// See [Runtime::get_value]
///
/// # Arguments
///
/// * `name` - The name of the value to retrieve.
///
/// # Returns
///
/// A `Result` containing the deserialized value of type `T` on success or an `Error` on failure.
pub fn get<T>(&mut self, name: &str) -> Result<T, Error>
where
Expand All @@ -69,6 +68,40 @@ impl ModuleWrapper {
self.runtime.get_value(Some(&self.module_context), name)
}

/// Retrieves a future resolving to a value from the module by name and deserializes it.
/// See [Runtime::get_value_async]
///
/// # Arguments
/// * `name` - The name of the value to retrieve.
///
/// # Returns
/// A `Result` containing the deserialized value of type `T` on success or an `Error` on failure.
pub async fn get_async<T>(&mut self, name: &str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
self.runtime
.get_value_async(Some(&self.module_context), name)
.await
}

/// Retrieves a value from the module by name and deserializes it.
/// Does not await promises or the event loop.
/// See [Runtime::get_value_immediate]
///
/// # Arguments
/// * `name` - The name of the value to retrieve.
///
/// # Returns
/// A `Result` containing the deserialized value of type `T` on success or an `Error` on failure.
pub fn get_immediate<T>(&mut self, name: &str) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
self.runtime
.get_value_immediate(Some(&self.module_context), name)
}

/// Checks if a value in the module with the given name is callable as a JavaScript function.
///
/// # Arguments
Expand All @@ -84,14 +117,13 @@ impl ModuleWrapper {
}

/// Calls a function in the module with the given name and arguments and deserializes the result.
/// See [Runtime::call_function]
///
/// # Arguments
///
/// * `name` - The name of the function to call.
/// * `args` - The arguments to pass to the function.
///
/// # Returns
///
/// A `Result` containing the deserialized result of type `T` on success or an `Error` on failure.
pub fn call<T>(&mut self, name: &str, args: &[serde_json::Value]) -> Result<T, Error>
where
Expand All @@ -101,8 +133,46 @@ impl ModuleWrapper {
.call_function(Some(&self.module_context), name, args)
}

/// Calls a function using the module's runtime that was previously stored
/// as a Function object
/// Calls a function in the module with the given name and arguments and deserializes the result.
/// See [Runtime::call_function_async]
///
/// # Arguments
/// * `name` - The name of the function to call.
/// * `args` - The arguments to pass to the function.
///
/// # Returns
/// A `Result` containing the deserialized result of type `T` on success or an `Error` on failure.
pub async fn call_async(
&mut self,
name: &str,
args: &[serde_json::Value],
) -> Result<serde_json::Value, Error> {
self.runtime
.call_function_async(Some(&self.module_context), name, args)
.await
}

/// Calls a function in the module with the given name and arguments and deserializes the result.
/// Does not await promises or the event loop.
/// See [Runtime::call_function_immediate]
///
/// # Arguments
/// * `name` - The name of the function to call.
/// * `args` - The arguments to pass to the function.
///
/// # Returns
/// A `Result` containing the deserialized result of type `T` on success or an `Error` on failure.
pub fn call_immediate(
&mut self,
name: &str,
args: &[serde_json::Value],
) -> Result<serde_json::Value, Error> {
self.runtime
.call_function_immediate(Some(&self.module_context), name, args)
}

/// Calls a function using the module's runtime that was previously stored as a Function object
/// See [Runtime::call_stored_function]
///
/// # Arguments
/// * `function` - The Function to call.
Expand All @@ -122,6 +192,44 @@ impl ModuleWrapper {
.call_stored_function(Some(&self.module_context), function, args)
}

/// Calls a function using the module's runtime that was previously stored as a Function object
/// See [Runtime::call_stored_function_async]
///
/// # Arguments
/// * `function` - The Function to call.
/// * `args` - The arguments to pass to the function.
///
/// # Returns
/// A `Result` containing the deserialized result of type `T` on success or an `Error` on failure.
pub async fn call_stored_async(
&mut self,
function: &Function,
args: &[serde_json::Value],
) -> Result<serde_json::Value, Error> {
self.runtime
.call_stored_function_async(Some(&self.module_context), function, args)
.await
}

/// Calls a function using the module's runtime that was previously stored as a Function object
/// Does not await promises or the event loop.
/// See [Runtime::call_stored_function_immediate]
///
/// # Arguments
/// * `function` - The Function to call.
/// * `args` - The arguments to pass to the function.
///
/// # Returns
/// A `Result` containing the deserialized result of type `T` on success or an `Error` on failure.
pub fn call_stored_immediate(
&mut self,
function: &Function,
args: &[serde_json::Value],
) -> Result<serde_json::Value, Error> {
self.runtime
.call_stored_function_immediate(Some(&self.module_context), function, args)
}

/// Retrieves the names of the module's exports.
///
/// # Returns
Expand Down
Loading

0 comments on commit bc85ee1

Please sign in to comment.