diff --git a/ext/ffi/jit_trampoline.rs b/ext/ffi/jit_trampoline.rs index 12c9b2a7d8..6a3efa8764 100644 --- a/ext/ffi/jit_trampoline.rs +++ b/ext/ffi/jit_trampoline.rs @@ -56,7 +56,7 @@ fn native_to_c(ty: &NativeType) -> &'static str { } pub(crate) fn codegen(sym: &crate::Symbol) -> String { - let mut c = String::from("#include \n"); + let mut c = String::from(include_str!("prelude.h")); let ret = native_to_c(&sym.result_type); // extern func( @@ -130,75 +130,72 @@ mod tests { super::codegen(&sym) } + const PRELUDE: &str = include_str!("prelude.h"); + fn assert_codegen(expected: String, actual: &str) { + assert_eq!(expected, format!("{PRELUDE}\n{}", actual)) + } + #[test] fn test_gen_trampoline() { - assert_eq!( + assert_codegen( codegen(vec![], NativeType::Void), - "#include \n\n\ - extern void func();\n\n\ + "extern void func();\n\n\ void func_trampoline(void* recv) {\ \n return func();\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::U32, NativeType::U32], NativeType::U32), - "#include \n\n\ - extern uint32_t func(uint32_t p0, uint32_t p1);\n\n\ + "extern uint32_t func(uint32_t p0, uint32_t p1);\n\n\ uint32_t func_trampoline(void* recv, uint32_t p0, uint32_t p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::I32, NativeType::I32], NativeType::I32), - "#include \n\n\ - extern int32_t func(int32_t p0, int32_t p1);\n\n\ + "extern int32_t func(int32_t p0, int32_t p1);\n\n\ int32_t func_trampoline(void* recv, int32_t p0, int32_t p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::F32, NativeType::F32], NativeType::F32), - "#include \n\n\ - extern float func(float p0, float p1);\n\n\ + "extern float func(float p0, float p1);\n\n\ float func_trampoline(void* recv, float p0, float p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::F64, NativeType::F64], NativeType::F64), - "#include \n\n\ - extern double func(double p0, double p1);\n\n\ + "extern double func(double p0, double p1);\n\n\ double func_trampoline(void* recv, double p0, double p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); } #[test] fn test_gen_trampoline_implicit_cast() { - assert_eq!( + assert_codegen( codegen(vec![NativeType::I8, NativeType::U8], NativeType::I8), - "#include \n\n\ - extern int8_t func(int8_t p0, uint8_t p1);\n\n\ + "extern int8_t func(int8_t p0, uint8_t p1);\n\n\ int8_t func_trampoline(void* recv, int32_t p0, uint32_t p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::ISize, NativeType::U64], NativeType::Void), - "#include \n\n\ - extern void func(intptr_t p0, uint64_t p1);\n\n\ + "extern void func(intptr_t p0, uint64_t p1);\n\n\ void func_trampoline(void* recv, intptr_t p0, uint64_t p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); - assert_eq!( + assert_codegen( codegen(vec![NativeType::USize, NativeType::USize], NativeType::U32), - "#include \n\n\ - extern uint32_t func(uintptr_t p0, uintptr_t p1);\n\n\ + "extern uint32_t func(uintptr_t p0, uintptr_t p1);\n\n\ uint32_t func_trampoline(void* recv, uintptr_t p0, uintptr_t p1) {\ \n return func(p0, p1);\n\ - }\n\n" + }\n\n", ); } } diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 6d03159644..a03bb41d62 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -10,13 +10,11 @@ use deno_core::futures::channel::mpsc; use deno_core::futures::Future; use deno_core::include_js_files; use deno_core::op; -use deno_core::v8::fast_api; -use std::sync::mpsc::sync_channel; - use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::serde_v8; use deno_core::v8; +use deno_core::v8::fast_api; use deno_core::Extension; use deno_core::OpState; use deno_core::Resource; @@ -33,17 +31,30 @@ use std::cell::RefCell; use std::collections::HashMap; use std::ffi::c_void; use std::ffi::CStr; +use std::mem::size_of; use std::os::raw::c_char; +use std::os::raw::c_short; use std::path::Path; use std::path::PathBuf; use std::ptr; use std::rc::Rc; +use std::sync::mpsc::sync_channel; #[cfg(not(target_os = "windows"))] mod jit_trampoline; #[cfg(not(target_os = "windows"))] mod tcc; +#[cfg(not(target_pointer_width = "64"))] +compile_error!("platform not supported"); + +// Assert assumptions made in `prelude.h` +const _: () = { + assert!(size_of::() == 1); + assert!(size_of::() == 2); + assert!(size_of::<*const ()>() == 8); +}; + thread_local! { static LOCAL_ISOLATE_POINTER: RefCell<*const v8::Isolate> = RefCell::new(ptr::null()); } diff --git a/ext/ffi/prelude.h b/ext/ffi/prelude.h new file mode 100644 index 0000000000..ed3d14e1a0 --- /dev/null +++ b/ext/ffi/prelude.h @@ -0,0 +1,19 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +/* Exact integral types. */ + +/* Signed. */ +typedef signed char int8_t; +typedef short int int16_t; +typedef int int32_t; +typedef long int int64_t; + +/* Unsigned. */ +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long int uint64_t; + +/* Types for `void *' pointers. */ +typedef long int intptr_t; +typedef unsigned long int uintptr_t;