0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

fix(fastcall): Fix options.data representation (#1074)

This commit is contained in:
Aapo Alasuutari 2022-09-20 07:27:23 +03:00 committed by GitHub
parent 0d1ada4387
commit 2ba52ed276
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 5 deletions

View file

@ -1,4 +1,6 @@
use crate::support::Opaque;
use crate::Local;
use crate::Value;
use libc::c_void;
use std::{
mem::align_of,
@ -159,11 +161,11 @@ 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
@ -171,7 +173,7 @@ pub union FastApiCallbackData {
/// 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
@ -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>,
}

View file

@ -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>,
@ -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 });
}