Skip to content

Commit

Permalink
Remove flawed Runtime::options
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Jul 25, 2024
1 parent d751c94 commit f1a732a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
22 changes: 9 additions & 13 deletions src/inner_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ impl Default for RuntimeOptions {
pub struct InnerRuntime {
pub module_loader: Rc<RustyLoader>,
pub deno_runtime: JsRuntime,
pub options: RuntimeOptions,

pub default_entrypoint: Option<String>,
}
impl InnerRuntime {
pub fn new(options: RuntimeOptions) -> Result<Self, Error> {
Expand All @@ -156,7 +157,7 @@ impl InnerRuntime {

source_map_getter: Some(module_loader.clone()),
create_params: options.isolate_params,
shared_array_buffer_store: options.shared_array_buffer_store,
shared_array_buffer_store: options.shared_array_buffer_store.clone(),

startup_snapshot: options.startup_snapshot,
extensions,
Expand All @@ -167,11 +168,8 @@ impl InnerRuntime {
Ok(Self {
deno_runtime,
module_loader,
options: RuntimeOptions {
timeout: options.timeout,
default_entrypoint: options.default_entrypoint,
..Default::default()
},

default_entrypoint: options.default_entrypoint,
})
}

Expand Down Expand Up @@ -470,8 +468,9 @@ impl InnerRuntime {
pub fn get_module_entrypoint(
&mut self,
module_context: &mut ModuleHandle,
default: Option<&str>,
) -> Result<Option<v8::Global<v8::Function>>, Error> {
let default = self.default_entrypoint.clone();

// Try to get an entrypoint from a call to `rustyscript.register_entrypoint` first
let state = self.deno_runtime.op_state();
let mut deep_state = state.try_borrow_mut()?;
Expand All @@ -492,7 +491,7 @@ impl InnerRuntime {
}

// Try to get an entrypoint from the default entrypoint
if let Some(default) = default {
if let Some(default) = default.as_deref() {
if let Ok(f) = self.get_function_by_name(Some(module_context), default) {
return Ok(Some(f));
}
Expand All @@ -512,8 +511,6 @@ impl InnerRuntime {
main_module: Option<&Module>,
side_modules: Vec<&Module>,
) -> Result<ModuleHandle, Error> {
let default_entrypoint = self.options.default_entrypoint.clone();

if main_module.is_none() && side_modules.is_empty() {
return Err(Error::Runtime(
"Internal error: attempt to load no modules".to_string(),
Expand Down Expand Up @@ -579,8 +576,7 @@ impl InnerRuntime {
}

// Try to get the default entrypoint
let entrypoint =
self.get_module_entrypoint(&mut module_handle_stub, default_entrypoint.as_deref())?;
let entrypoint = self.get_module_entrypoint(&mut module_handle_stub)?;

Ok(ModuleHandle::new(
module_handle_stub.module(),
Expand Down
13 changes: 6 additions & 7 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub type Undefined = crate::js_value::Value;
pub struct Runtime {
inner: InnerRuntime,
tokio: Rc<tokio::runtime::Runtime>,
timeout: std::time::Duration,
}

impl Runtime {
Expand Down Expand Up @@ -92,6 +93,7 @@ impl Runtime {
tokio: Rc<tokio::runtime::Runtime>,
) -> Result<Self, Error> {
Ok(Self {
timeout: options.timeout,
inner: InnerRuntime::new(options)?,
tokio,
})
Expand All @@ -108,13 +110,10 @@ impl Runtime {
self.tokio.clone()
}

/// Access the options used to create this runtime
///
/// Warning: Not all options can be accessed in this way
/// Extensions, for example, are consumed during runtime creation
/// Returns the timeout for the runtime
#[must_use]
pub fn options(&self) -> &RuntimeOptions {
&self.inner.options
pub fn timeout(&self) -> std::time::Duration {
self.timeout
}

/// Destroy the v8 runtime, releasing all resources
Expand Down Expand Up @@ -1053,7 +1052,7 @@ impl Runtime {
U: std::future::Future<Output = Result<T, Error>>,
F: FnOnce(&'a mut Runtime) -> U,
{
let timeout = self.options().timeout;
let timeout = self.timeout();
let rt = self.tokio_runtime();
rt.block_on(async move { tokio::time::timeout(timeout, f(self)).await })?
}
Expand Down

0 comments on commit f1a732a

Please sign in to comment.