Skip to content

Commit

Permalink
Strengthen fast call API test (denoland#1093)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bnoordhuis committed Oct 9, 2022
1 parent b2a09e2 commit 213305b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7595,15 +7595,16 @@ 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<v8::Object>, a: u32, b: u32) -> u32 {
unsafe { WHO = "fast" };
a + b
}

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 {
Expand All @@ -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();
Expand All @@ -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 });
Expand Down

0 comments on commit 213305b

Please sign in to comment.