feat(ext/ffi): Replace pointer integers with v8::External objects (#16889)

This commit is contained in:
Aapo Alasuutari 2023-02-22 19:32:38 +02:00 committed by GitHub
parent 2bd7482295
commit b56b8c8a75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 834 additions and 373 deletions

View file

@ -45,6 +45,7 @@ enum TransformKind {
SliceU8(bool),
SliceF64(bool),
PtrU8,
PtrVoid,
WasmMemory,
}
@ -90,6 +91,13 @@ impl Transform {
index,
}
}
fn void_ptr(index: usize) -> Self {
Transform {
kind: TransformKind::PtrVoid,
index,
}
}
}
#[derive(Debug, PartialEq)]
@ -195,19 +203,25 @@ impl Transform {
.as_ptr();
})
}
TransformKind::PtrVoid => {
*ty = parse_quote! { *mut ::std::ffi::c_void };
q!(Vars {}, {})
}
}
}
}
fn get_fast_scalar(s: &str) -> Option<FastValue> {
match s {
"bool" => Some(FastValue::Bool),
"u32" => Some(FastValue::U32),
"i32" => Some(FastValue::I32),
"u64" => Some(FastValue::U64),
"i64" => Some(FastValue::I64),
"f32" => Some(FastValue::F32),
"f64" => Some(FastValue::F64),
"bool" => Some(FastValue::Bool),
"* const c_void" | "* mut c_void" => Some(FastValue::Pointer),
"ResourceId" => Some(FastValue::U32),
_ => None,
}
@ -226,13 +240,14 @@ fn can_return_fast(v: &FastValue) -> bool {
#[derive(Debug, PartialEq, Clone)]
pub(crate) enum FastValue {
Void,
Bool,
U32,
I32,
U64,
I64,
F32,
F64,
Bool,
Pointer,
V8Value,
Uint8Array,
Uint32Array,
@ -414,6 +429,31 @@ impl Optimizer {
{
self.fast_result = Some(FastValue::Void);
}
Some(GenericArgument::Type(Type::Ptr(TypePtr {
mutability: Some(_),
elem,
..
}))) => {
match &**elem {
Type::Path(TypePath {
path: Path { segments, .. },
..
}) => {
// Is `T` a c_void?
let segment = single_segment(segments)?;
match segment {
PathSegment { ident, .. } if ident == "c_void" => {
self.fast_result = Some(FastValue::Pointer);
return Ok(());
}
_ => {
return Err(BailoutReason::FastUnsupportedParamType)
}
}
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
@ -430,6 +470,29 @@ impl Optimizer {
}
};
}
Type::Ptr(TypePtr {
mutability: Some(_),
elem,
..
}) => {
match &**elem {
Type::Path(TypePath {
path: Path { segments, .. },
..
}) => {
// Is `T` a c_void?
let segment = single_segment(segments)?;
match segment {
PathSegment { ident, .. } if ident == "c_void" => {
self.fast_result = Some(FastValue::Pointer);
return Ok(());
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
};
@ -684,6 +747,31 @@ impl Optimizer {
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
},
// *const T
Type::Ptr(TypePtr {
elem,
mutability: Some(_),
..
}) => match &**elem {
Type::Path(TypePath {
path: Path { segments, .. },
..
}) => {
let segment = single_segment(segments)?;
match segment {
// Is `T` a c_void?
PathSegment { ident, .. } if ident == "c_void" => {
self.fast_parameters.push(FastValue::Pointer);
assert!(self
.transforms
.insert(index, Transform::void_ptr(index))
.is_none());
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
}
}
_ => return Err(BailoutReason::FastUnsupportedParamType),
},
_ => return Err(BailoutReason::FastUnsupportedParamType),
},
_ => return Err(BailoutReason::FastUnsupportedParamType),