Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New const evaluated FastFunction API #1201

Merged
merged 5 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions benches/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::os::raw::c_void;

fn main() {
// Skip running benchmarks in debug or CI.
if cfg!(debug_assertions) || std::env::var("CI").is_ok() {
Expand Down Expand Up @@ -67,27 +65,20 @@ fn main() {
fn fast_fn() -> i32 {
42
}
pub struct FastCall;
impl v8::fast_api::FastFunction for FastCall {
fn args(&self) -> &'static [v8::fast_api::Type] {
&[v8::fast_api::Type::V8Value]
}
fn return_type(&self) -> v8::fast_api::CType {
v8::fast_api::CType::Int32
}

fn function(&self) -> *const c_void {
fast_fn as _
}
}
const FAST_CALL: v8::fast_api::FastFunction =
v8::fast_api::FastFunction::new(
&[v8::fast_api::Type::V8Value],
v8::fast_api::CType::Int32,
fast_fn as _,
);
let template = v8::FunctionTemplate::builder(
|scope: &mut v8::HandleScope,
_: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue| {
rv.set(v8::Integer::new(scope, 42).into());
},
)
.build_fast(scope, &FastCall, None, None, None);
.build_fast(scope, &FAST_CALL, None, None, None);
let name = v8::String::new(scope, "new_fast").unwrap();
let value = template.get_function(scope).unwrap();

Expand Down
25 changes: 17 additions & 8 deletions src/fast_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,23 @@ impl<T: Default> FastApiTypedArray<T> {
}
}

pub trait FastFunction {
#[inline(always)]
fn args(&self) -> &'static [Type] {
&[]
}
pub struct FastFunction {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it.

pub args: &'static [Type],
pub return_type: CType,
pub function: *const c_void,
}

impl FastFunction {
#[inline(always)]
fn return_type(&self) -> CType {
CType::Void
pub const fn new(
args: &'static [Type],
return_type: CType,
function: *const c_void,
) -> Self {
Self {
args,
return_type,
function,
}
}
fn function(&self) -> *const c_void;
}
24 changes: 10 additions & 14 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,18 @@ impl<'s> FunctionBuilder<'s, FunctionTemplate> {
pub fn build_fast(
self,
scope: &mut HandleScope<'s, ()>,
overload1: &dyn FastFunction,
overload1: &FastFunction,
c_fn_info1: Option<*const CFunctionInfo>,
overload2: Option<&dyn FastFunction>,
overload2: Option<&FastFunction>,
c_fn_info2: Option<*const CFunctionInfo>,
) -> Local<'s, FunctionTemplate> {
let c_fn1 = if let Some(fn_info) = c_fn_info1 {
fn_info
} else {
let args = CTypeInfo::new_from_slice(overload1.args());
let ret = CTypeInfo::new(overload1.return_type());
let args = CTypeInfo::new_from_slice(overload1.args);
let ret = CTypeInfo::new(overload1.return_type);
let fn_info = unsafe {
CFunctionInfo::new(args.as_ptr(), overload1.args().len(), ret.as_ptr())
CFunctionInfo::new(args.as_ptr(), overload1.args.len(), ret.as_ptr())
};
fn_info.as_ptr()
};
Expand All @@ -400,14 +400,10 @@ impl<'s> FunctionBuilder<'s, FunctionTemplate> {
if let Some(fn_info) = c_fn_info2 {
fn_info
} else {
let args = CTypeInfo::new_from_slice(overload2.args());
let ret = CTypeInfo::new(overload2.return_type());
let args = CTypeInfo::new_from_slice(overload2.args);
let ret = CTypeInfo::new(overload2.return_type);
let fn_info = unsafe {
CFunctionInfo::new(
args.as_ptr(),
overload2.args().len(),
ret.as_ptr(),
)
CFunctionInfo::new(args.as_ptr(), overload2.args.len(), ret.as_ptr())
};
fn_info.as_ptr()
}
Expand All @@ -425,9 +421,9 @@ impl<'s> FunctionBuilder<'s, FunctionTemplate> {
self.length,
ConstructorBehavior::Throw,
self.side_effect_type,
overload1.function(),
overload1.function,
c_fn1,
overload2.map_or(null(), |f| f.function()),
overload2.map_or(null(), |f| f.function),
c_fn2,
)
})
Expand Down
Loading