diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 130e025ae7..aed1dda9de 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -2265,7 +2265,8 @@ fn start( let init_config = json!({ "debug": debug, "rootUri": root_uri }); let init_src = format!("globalThis.serverInit({});", init_config); - runtime.execute_script(&located_script_name!(), &init_src) + runtime.execute_script(&located_script_name!(), &init_src)?; + Ok(()) } #[derive(Debug, Serialize)] diff --git a/core/runtime.rs b/core/runtime.rs index cf43c2adce..fc7a7228c6 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -482,7 +482,7 @@ impl JsRuntime { pub fn sync_ops_cache(&mut self) { self .execute_script("", "Deno.core.syncOpsCache()") - .unwrap() + .unwrap(); } /// Returns the runtime's op state, which can be used to maintain ops @@ -513,7 +513,7 @@ impl JsRuntime { &mut self, name: &str, source_code: &str, - ) -> Result<(), AnyError> { + ) -> Result, AnyError> { let scope = &mut self.handle_scope(); let source = v8::String::new(scope, source_code).unwrap(); @@ -531,7 +531,10 @@ impl JsRuntime { }; match script.run(tc_scope) { - Some(_) => Ok(()), + Some(value) => { + let value_handle = v8::Global::new(tc_scope, value); + Ok(value_handle) + } None => { assert!(tc_scope.has_caught()); let exception = tc_scope.exception().unwrap(); @@ -1606,6 +1609,27 @@ pub mod tests { assert_eq!(dispatch_count.load(Ordering::Relaxed), 1); } + #[test] + fn test_execute_script_return_value() { + let mut runtime = JsRuntime::new(Default::default()); + let value_global = runtime.execute_script("a.js", "a = 1 + 2").unwrap(); + { + let scope = &mut runtime.handle_scope(); + let value = value_global.get(scope); + assert_eq!(value.integer_value(scope).unwrap(), 3); + } + let value_global = runtime.execute_script("b.js", "b = 'foobar'").unwrap(); + { + let scope = &mut runtime.handle_scope(); + let value = value_global.get(scope); + assert!(value.is_string()); + assert_eq!( + value.to_string(scope).unwrap().to_rust_string_lossy(scope), + "foobar" + ); + } + } + #[test] fn terminate_execution() { let (mut isolate, _dispatch_count) = setup(Mode::Async); diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index f8aadf4c2c..1fcd57dc22 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -427,7 +427,8 @@ impl WebWorker { name: &str, source_code: &str, ) -> Result<(), AnyError> { - self.js_runtime.execute_script(name, source_code) + self.js_runtime.execute_script(name, source_code)?; + Ok(()) } /// Loads and instantiates specified JavaScript module. diff --git a/runtime/worker.rs b/runtime/worker.rs index 04c2941461..543eae6f69 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -191,7 +191,8 @@ impl MainWorker { name: &str, source_code: &str, ) -> Result<(), AnyError> { - self.js_runtime.execute_script(name, source_code) + self.js_runtime.execute_script(name, source_code)?; + Ok(()) } /// Loads and instantiates specified JavaScript module.