diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-07-01 18:00:14 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-02 00:00:14 +0000 |
commit | e746b6d80654ba4e4e26370fe6e4f784ce841d92 (patch) | |
tree | 153ffad92a96126b9ab8e906dcdabf7648755931 /core/examples/eval_js_value.rs | |
parent | b9c0e7cd550ab14fa7da7e33ed87cbeeeb9785a0 (diff) |
refactor(core): Extract deno_core (#19658)
`deno_core` is moving out! You'll find it at
https://github.com/denoland/deno_core/ once this PR lands.
Diffstat (limited to 'core/examples/eval_js_value.rs')
-rw-r--r-- | core/examples/eval_js_value.rs | 48 |
1 files changed, 0 insertions, 48 deletions
diff --git a/core/examples/eval_js_value.rs b/core/examples/eval_js_value.rs deleted file mode 100644 index a752d7100..000000000 --- a/core/examples/eval_js_value.rs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -//! This example shows you how to evaluate JavaScript expression and deserialize -//! return value into a Rust object. - -// NOTE: -// Here we are deserializing to `serde_json::Value` but you can -// deserialize to any other type that implements the `Deserialize` trait. - -use deno_core::v8; -use deno_core::JsRuntime; -use deno_core::RuntimeOptions; - -fn main() { - let mut runtime = JsRuntime::new(RuntimeOptions::default()); - - // Evaluate some code - let code = "let a = 1+4; a*2"; - let output: serde_json::Value = - eval(&mut runtime, code).expect("Eval failed"); - - println!("Output: {output:?}"); - - let expected_output = serde_json::json!(10); - assert_eq!(expected_output, output); -} - -fn eval( - context: &mut JsRuntime, - code: &'static str, -) -> Result<serde_json::Value, String> { - let res = context.execute_script_static("<anon>", code); - match res { - Ok(global) => { - let scope = &mut context.handle_scope(); - let local = v8::Local::new(scope, global); - // Deserialize a `v8` object into a Rust type using `serde_v8`, - // in this case deserialize to a JSON `Value`. - let deserialized_value = - serde_v8::from_v8::<serde_json::Value>(scope, local); - - match deserialized_value { - Ok(value) => Ok(value), - Err(err) => Err(format!("Cannot deserialize value: {err:?}")), - } - } - Err(err) => Err(format!("Evaling error: {err:?}")), - } -} |