From 213305b3defce0ac8ec73bbc349c8e42ca305f51 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 9 Oct 2022 15:26:06 +0200 Subject: [PATCH] Strengthen fast call API test (#1093) Check that it actually performs the expected operation (adding two numbers) because it didn't - the callback's function prototype was wrong. V8 always passes the receiver object as the first parameter. --- tests/test_api.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index fb565163..65d26ac8 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -7595,7 +7595,7 @@ fn host_create_shadow_realm_context_callback() { #[test] fn test_fast_calls() { static mut WHO: &str = "none"; - fn fast_fn(a: u32, b: u32) -> u32 { + fn fast_fn(_recv: v8::Local, a: u32, b: u32) -> u32 { unsafe { WHO = "fast" }; a + b } @@ -7603,7 +7603,8 @@ fn test_fast_calls() { pub struct FastTest; impl fast_api::FastFunction for FastTest { fn args(&self) -> &'static [fast_api::Type] { - &[fast_api::Type::Uint32, fast_api::Type::Uint32] + use fast_api::Type::*; + &[V8Value, Uint32, Uint32] } fn return_type(&self) -> fast_api::CType { @@ -7617,11 +7618,13 @@ fn test_fast_calls() { fn slow_fn( scope: &mut v8::HandleScope, - _: v8::FunctionCallbackArguments, + args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, ) { unsafe { WHO = "slow" }; - rv.set(v8::Boolean::new(scope, false).into()); + let a = args.get(0).uint32_value(scope).unwrap(); + let b = args.get(1).uint32_value(scope).unwrap(); + rv.set_uint32(a + b); } let _setup_guard = setup(); @@ -7639,16 +7642,16 @@ fn test_fast_calls() { let value = template.get_function(scope).unwrap(); global.set(scope, name.into(), value.into()).unwrap(); let source = r#" - function f(x, y) { return func(x, y); } - %PrepareFunctionForOptimization(f); - f(1, 2); -"#; + function f(x, y) { return func(x, y); } + %PrepareFunctionForOptimization(f); + if (42 !== f(19, 23)) throw "unexpected"; + "#; eval(scope, source).unwrap(); assert_eq!("slow", unsafe { WHO }); let source = r#" %OptimizeFunctionOnNextCall(f); - f(1, 2); + if (42 !== f(19, 23)) throw "unexpected"; "#; eval(scope, source).unwrap(); assert_eq!("fast", unsafe { WHO });