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.
This commit is contained in:
Bartek Iwańczuk 2021-07-08 18:56:53 +02:00 committed by GitHub
parent 91fe137d7d
commit 27e1b4cb5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 6 deletions

View file

@ -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)]

View file

@ -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();
@ -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);

View file

@ -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.

View file

@ -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.