1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-03 17:08:35 -05:00

chore(ext/ffi): remove dependency on stdint.h (#15294)

This commit is contained in:
Divy Srivastava 2022-07-24 18:38:13 +05:30 committed by crowlkats
parent 3893ce848c
commit c416553a6b
No known key found for this signature in database
GPG key ID: A82C9D461FC483E8
3 changed files with 63 additions and 36 deletions

View file

@ -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 <stdint.h>\n");
let mut c = String::from(include_str!("prelude.h"));
let ret = native_to_c(&sym.result_type);
// extern <return_type> 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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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 <stdint.h>\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",
);
}
}

View file

@ -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::<c_char>() == 1);
assert!(size_of::<c_short>() == 2);
assert!(size_of::<*const ()>() == 8);
};
thread_local! {
static LOCAL_ISOLATE_POINTER: RefCell<*const v8::Isolate> = RefCell::new(ptr::null());
}

19
ext/ffi/prelude.h Normal file
View file

@ -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;