diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-06-24 23:30:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-24 15:30:04 -0600 |
commit | a181ceb0e3791c842db6e8e6f528cf9ce320642a (patch) | |
tree | 0714537d8b91013f9253e65202e2dc596e977d01 /core | |
parent | 4a18c761351dccb146973793cf22e6efffff18bf (diff) |
refactor(ops): op2 supports Result in slow call path (#19602)
Diffstat (limited to 'core')
-rw-r--r-- | core/lib.rs | 5 | ||||
-rw-r--r-- | core/runtime/ops.rs | 72 |
2 files changed, 71 insertions, 6 deletions
diff --git a/core/lib.rs b/core/lib.rs index 9a960e93a..1042bf55c 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -147,11 +147,6 @@ pub mod _ops { pub use super::runtime::V8_WRAPPER_TYPE_INDEX; } -pub(crate) mod deno_core { - pub(crate) use crate::_ops; - pub(crate) use crate::v8; -} - // TODO(mmastrac): Temporary while we move code around pub mod snapshot_util { pub use crate::runtime::create_snapshot; diff --git a/core/runtime/ops.rs b/core/runtime/ops.rs index fb71de8cb..85708b24b 100644 --- a/core/runtime/ops.rs +++ b/core/runtime/ops.rs @@ -208,12 +208,25 @@ pub fn to_i64(number: &v8::Value) -> i32 { #[cfg(test)] mod tests { + use crate::error::generic_error; + use crate::error::AnyError; + use crate::error::JsError; use crate::FastString; use crate::JsRuntime; use crate::RuntimeOptions; use deno_ops::op2; - crate::extension!(testing, ops = [op_test_add, op_test_add_option]); + crate::extension!( + testing, + ops = [ + op_test_add, + op_test_add_option, + op_test_result_void_ok, + op_test_result_void_err, + op_test_result_primitive_ok, + op_test_result_primitive_err + ] + ); /// Run a test for a single op. fn run_test( @@ -281,4 +294,61 @@ mod tests { ); Ok(()) } + + #[op2(core)] + pub fn op_test_result_void_err() -> Result<(), AnyError> { + Err(generic_error("failed!!!")) + } + + #[op2(core)] + pub fn op_test_result_void_ok() -> Result<(), AnyError> { + Ok(()) + } + + #[tokio::test] + pub async fn test_op_result_void() -> Result<(), Box<dyn std::error::Error>> { + run_test( + "op_test_result_void_err", + "op_test_result_void_err()", + |value, _scope| { + let js_error = value.err().unwrap().downcast::<JsError>().unwrap(); + assert_eq!(js_error.message, Some("failed!!!".to_owned())); + }, + ); + run_test( + "op_test_result_void_ok", + "op_test_result_void_ok()", + |value, _scope| assert!(value.unwrap().is_null_or_undefined()), + ); + Ok(()) + } + + #[op2(core)] + pub fn op_test_result_primitive_err() -> Result<u32, AnyError> { + Err(generic_error("failed!!!")) + } + + #[op2(core)] + pub fn op_test_result_primitive_ok() -> Result<u32, AnyError> { + Ok(123) + } + + #[tokio::test] + pub async fn test_op_result_primitive( + ) -> Result<(), Box<dyn std::error::Error>> { + run_test( + "op_test_result_primitive_err", + "op_test_result_primitive_err()", + |value, _scope| { + let js_error = value.err().unwrap().downcast::<JsError>().unwrap(); + assert_eq!(js_error.message, Some("failed!!!".to_owned())); + }, + ); + run_test( + "op_test_result_primitive_ok", + "op_test_result_primitive_ok()", + |value, scope| assert_eq!(value.unwrap().int32_value(scope), Some(123)), + ); + Ok(()) + } } |