From a181ceb0e3791c842db6e8e6f528cf9ce320642a Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Sat, 24 Jun 2023 23:30:04 +0200 Subject: refactor(ops): op2 supports Result in slow call path (#19602) --- core/runtime/ops.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'core/runtime/ops.rs') 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> { + run_test( + "op_test_result_void_err", + "op_test_result_void_err()", + |value, _scope| { + let js_error = value.err().unwrap().downcast::().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 { + Err(generic_error("failed!!!")) + } + + #[op2(core)] + pub fn op_test_result_primitive_ok() -> Result { + Ok(123) + } + + #[tokio::test] + pub async fn test_op_result_primitive( + ) -> Result<(), Box> { + run_test( + "op_test_result_primitive_err", + "op_test_result_primitive_err()", + |value, _scope| { + let js_error = value.err().unwrap().downcast::().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(()) + } } -- cgit v1.2.3