summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/ffi/jit_trampoline.rs26
-rw-r--r--ext/ffi/lib.rs5
-rw-r--r--ext/ffi/prelude.h6
3 files changed, 30 insertions, 7 deletions
diff --git a/ext/ffi/jit_trampoline.rs b/ext/ffi/jit_trampoline.rs
index 6a3efa876..92e63348d 100644
--- a/ext/ffi/jit_trampoline.rs
+++ b/ext/ffi/jit_trampoline.rs
@@ -32,7 +32,8 @@ fn native_arg_to_c(ty: &NativeType) -> &'static str {
NativeType::I64 => "int64_t",
NativeType::ISize => "intptr_t",
NativeType::USize => "uintptr_t",
- NativeType::Pointer | NativeType::Function => "void*",
+ NativeType::Pointer => "struct FastApiTypedArray*",
+ NativeType::Function => "void*",
}
}
@@ -85,11 +86,15 @@ pub(crate) fn codegen(sym: &crate::Symbol) -> String {
c += ") {\n";
// return func(p0, p1, ...);
c += " return func(";
- for (i, _) in sym.parameter_types.iter().enumerate() {
+ for (i, ty) in sym.parameter_types.iter().enumerate() {
if i > 0 {
c += ", ";
}
- let _ = write!(c, "p{i}");
+ if matches!(ty, NativeType::Pointer) {
+ let _ = write!(c, "p{i}->data");
+ } else {
+ let _ = write!(c, "p{i}");
+ }
}
c += ");\n}\n\n";
c
@@ -103,7 +108,6 @@ pub(crate) fn gen_trampoline(
// SAFETY: symbol satisfies ABI requirement.
unsafe { ctx.add_symbol(cstr!("func"), sym.ptr.0 as *const c_void) };
let c = codegen(&sym);
-
ctx.compile_string(cstr!(c))?;
let alloc = Allocation {
addr: ctx.relocate_and_get_symbol(cstr!("func_trampoline"))?,
@@ -172,6 +176,20 @@ mod tests {
\n return func(p0, p1);\n\
}\n\n",
);
+ assert_codegen(
+ codegen(vec![NativeType::Pointer, NativeType::U32], NativeType::U32),
+ "extern uint32_t func(void* p0, uint32_t p1);\n\n\
+ uint32_t func_trampoline(void* recv, struct FastApiTypedArray* p0, uint32_t p1) {\
+ \n return func(p0->data, p1);\n\
+ }\n\n",
+ );
+ assert_codegen(
+ codegen(vec![NativeType::Pointer, NativeType::Pointer], NativeType::U32),
+ "extern uint32_t func(void* p0, void* p1);\n\n\
+ uint32_t func_trampoline(void* recv, struct FastApiTypedArray* p0, struct FastApiTypedArray* p1) {\
+ \n return func(p0->data, p1->data);\n\
+ }\n\n",
+ );
}
#[test]
diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs
index 4746a5203..05015a45a 100644
--- a/ext/ffi/lib.rs
+++ b/ext/ffi/lib.rs
@@ -746,9 +746,8 @@ impl From<&NativeType> for fast_api::Type {
NativeType::I64 => fast_api::Type::Int64,
NativeType::U64 => fast_api::Type::Uint64,
NativeType::ISize => fast_api::Type::Int64,
- NativeType::USize | NativeType::Function | NativeType::Pointer => {
- fast_api::Type::Uint64
- }
+ NativeType::USize | NativeType::Function => fast_api::Type::Uint64,
+ NativeType::Pointer => fast_api::Type::TypedArray(fast_api::CType::Uint8),
}
}
}
diff --git a/ext/ffi/prelude.h b/ext/ffi/prelude.h
index ed3d14e1a..89ae162c1 100644
--- a/ext/ffi/prelude.h
+++ b/ext/ffi/prelude.h
@@ -17,3 +17,9 @@ typedef unsigned long int uint64_t;
/* Types for `void *' pointers. */
typedef long int intptr_t;
typedef unsigned long int uintptr_t;
+
+// https://source.chromium.org/chromium/chromium/src/+/main:v8/include/v8-fast-api-calls.h;l=336
+struct FastApiTypedArray {
+ uintptr_t length_;
+ void* data;
+};