Skip to content

Commit

Permalink
fix(fastcall): Fix options.data representation (denoland#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas committed Sep 20, 2022
1 parent 0d1ada4 commit 2ba52ed
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/fast_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::support::Opaque;
use crate::Local;
use crate::Value;
use libc::c_void;
use std::{
mem::align_of,
Expand Down Expand Up @@ -159,19 +161,19 @@ struct CTypeSequenceInfo {
}

#[repr(C)]
pub union FastApiCallbackData {
pub union FastApiCallbackData<'a> {
/// `data_ptr` allows for default constructing FastApiCallbackOptions.
pub data_ptr: *mut c_void,
/// The `data` passed to the FunctionTemplate constructor, or `undefined`.
pub data: crate::Value,
pub data: Local<'a, Value>,
}

/// A struct which may be passed to a fast call callback, like so
/// ```c
/// void FastMethodWithOptions(int param, FastApiCallbackOptions& options);
/// ```
#[repr(C)]
pub struct FastApiCallbackOptions {
pub struct FastApiCallbackOptions<'a> {
/// If the callback wants to signal an error condition or to perform an
/// allocation, it must set options.fallback to true and do an early return
/// from the fast method. Then V8 checks the value of options.fallback and if
Expand All @@ -182,7 +184,7 @@ pub struct FastApiCallbackOptions {
/// fallback conditions are checked, because otherwise executing the slow
/// callback might produce visible side-effects twice.
pub fallback: bool,
pub data: FastApiCallbackData,
pub data: FastApiCallbackData<'a>,
/// When called from WebAssembly, a view of the calling module's memory.
pub wasm_memory: *const FastApiTypedArray<u8>,
}
Expand Down
72 changes: 71 additions & 1 deletion tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7650,7 +7650,7 @@ fn test_fast_calls_overload() {
}

#[test]
fn test_fast_calls_callback_options() {
fn test_fast_calls_callback_options_fallback() {
static mut WHO: &str = "none";
fn fast_fn(
_recv: v8::Local<v8::Object>,
Expand Down Expand Up @@ -7718,3 +7718,73 @@ fn test_fast_calls_callback_options() {
eval(scope, source).unwrap();
assert_eq!("slow", unsafe { WHO });
}

#[test]
fn test_fast_calls_callback_options_data() {
static mut DATA: bool = false;
unsafe fn fast_fn(
_recv: v8::Local<v8::Object>,
options: *mut fast_api::FastApiCallbackOptions,
) {
let options = &mut *options;
if !options.data.data.is_external() {
options.fallback = true;
return;
}

let data = v8::Local::<v8::External>::cast(options.data.data);
let data = &mut *(data.value() as *mut bool);
*data = true;
}

pub struct FastTest;
impl fast_api::FastFunction for FastTest {
fn args(&self) -> &'static [fast_api::Type] {
&[fast_api::Type::V8Value, fast_api::Type::CallbackOptions]
}

fn function(&self) -> *const c_void {
fast_fn as _
}
}

fn slow_fn(
scope: &mut v8::HandleScope,
_: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
rv.set(v8::Boolean::new(scope, false).into());
}

let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);

let global = context.global(scope);
let external =
v8::External::new(scope, unsafe { &mut DATA as *mut bool as *mut c_void });

let template = v8::FunctionTemplate::builder(slow_fn)
.data(external.into())
.build_fast(scope, &FastTest, None);

let name = v8::String::new(scope, "func").unwrap();
let value = template.get_function(scope).unwrap();
global.set(scope, name.into(), value.into()).unwrap();
let source = r#"
function f() { return func(); }
%PrepareFunctionForOptimization(f);
f();
"#;
eval(scope, source).unwrap();
assert!(unsafe { !DATA });

let source = r#"
%OptimizeFunctionOnNextCall(f);
f();
"#;
eval(scope, source).unwrap();
assert!(unsafe { DATA });
}

0 comments on commit 2ba52ed

Please sign in to comment.