mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
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:
parent
91fe137d7d
commit
27e1b4cb5a
4 changed files with 33 additions and 6 deletions
|
@ -2265,7 +2265,8 @@ fn start(
|
||||||
let init_config = json!({ "debug": debug, "rootUri": root_uri });
|
let init_config = json!({ "debug": debug, "rootUri": root_uri });
|
||||||
let init_src = format!("globalThis.serverInit({});", init_config);
|
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)]
|
#[derive(Debug, Serialize)]
|
||||||
|
|
|
@ -482,7 +482,7 @@ impl JsRuntime {
|
||||||
pub fn sync_ops_cache(&mut self) {
|
pub fn sync_ops_cache(&mut self) {
|
||||||
self
|
self
|
||||||
.execute_script("<anon>", "Deno.core.syncOpsCache()")
|
.execute_script("<anon>", "Deno.core.syncOpsCache()")
|
||||||
.unwrap()
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the runtime's op state, which can be used to maintain ops
|
/// Returns the runtime's op state, which can be used to maintain ops
|
||||||
|
@ -513,7 +513,7 @@ impl JsRuntime {
|
||||||
&mut self,
|
&mut self,
|
||||||
name: &str,
|
name: &str,
|
||||||
source_code: &str,
|
source_code: &str,
|
||||||
) -> Result<(), AnyError> {
|
) -> Result<v8::Global<v8::Value>, AnyError> {
|
||||||
let scope = &mut self.handle_scope();
|
let scope = &mut self.handle_scope();
|
||||||
|
|
||||||
let source = v8::String::new(scope, source_code).unwrap();
|
let source = v8::String::new(scope, source_code).unwrap();
|
||||||
|
@ -531,7 +531,10 @@ impl JsRuntime {
|
||||||
};
|
};
|
||||||
|
|
||||||
match script.run(tc_scope) {
|
match script.run(tc_scope) {
|
||||||
Some(_) => Ok(()),
|
Some(value) => {
|
||||||
|
let value_handle = v8::Global::new(tc_scope, value);
|
||||||
|
Ok(value_handle)
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
assert!(tc_scope.has_caught());
|
assert!(tc_scope.has_caught());
|
||||||
let exception = tc_scope.exception().unwrap();
|
let exception = tc_scope.exception().unwrap();
|
||||||
|
@ -1606,6 +1609,27 @@ pub mod tests {
|
||||||
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
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]
|
#[test]
|
||||||
fn terminate_execution() {
|
fn terminate_execution() {
|
||||||
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
||||||
|
|
|
@ -427,7 +427,8 @@ impl WebWorker {
|
||||||
name: &str,
|
name: &str,
|
||||||
source_code: &str,
|
source_code: &str,
|
||||||
) -> Result<(), AnyError> {
|
) -> 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.
|
/// Loads and instantiates specified JavaScript module.
|
||||||
|
|
|
@ -191,7 +191,8 @@ impl MainWorker {
|
||||||
name: &str,
|
name: &str,
|
||||||
source_code: &str,
|
source_code: &str,
|
||||||
) -> Result<(), AnyError> {
|
) -> 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.
|
/// Loads and instantiates specified JavaScript module.
|
||||||
|
|
Loading…
Reference in a new issue