diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-07-08 18:56:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-08 18:56:53 +0200 |
commit | 27e1b4cb5ac81c5ac2ca5adf78c91fdbef9409e8 (patch) | |
tree | e5dc9610d551663926eceb34a0e4a31b8624b487 | |
parent | 91fe137d7da3cbd1ab1c2cfd963962d964571526 (diff) |
feat(core): return v8::Value from JsRuntime::execute_script (#11129)
This commit changes return type of JsRuntime::execute_script to include
v8::Value returned from evaluation.
When embedding deno_core it is sometimes useful to be able to inspect
script evaluation value without the hoops of adding ops to store the
value on the OpState.
v8::Global<v8::Value> is used so consumers don't have to pass
scope themselves.
-rw-r--r-- | cli/lsp/tsc.rs | 3 | ||||
-rw-r--r-- | core/runtime.rs | 30 | ||||
-rw-r--r-- | runtime/web_worker.rs | 3 | ||||
-rw-r--r-- | runtime/worker.rs | 3 |
4 files changed, 33 insertions, 6 deletions
diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 130e025ae..aed1dda9d 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 cf43c2adc..fc7a7228c 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("<anon>", "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<v8::Global<v8::Value>, 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(); @@ -1607,6 +1610,27 @@ pub mod tests { } #[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); // TODO(piscisaureus): in rusty_v8, the `thread_safe_handle()` method diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index f8aadf4c2..1fcd57dc2 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 04c294146..543eae6f6 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. |