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

Remove libc dependency. #1117

Merged
merged 1 commit into from
Nov 24, 2022
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
Remove libc dependency.
Use `std` functionality where possible. Only size_t needs
to be defined like was done with the `intptr_t` type before.

Also unifies the usage of `std::os::raw` types that where
already defined in the support module.
  • Loading branch information
hasenbanck committed Nov 8, 2022
commit 224726ce67e2e5cafe6b923f85c6cfa0042fd3bb
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ use_custom_libcxx = []
[dependencies]
bitflags = "1.3.2"
lazy_static = "1.4.0"
libc = "0.2.126"

[build-dependencies]
fslock = "0.1.8"
Expand Down
20 changes: 10 additions & 10 deletions src/V8.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
use libc::c_char;
use libc::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::sync::Mutex;
use std::vec::Vec;

use crate::platform::Platform;
use crate::support::char;
use crate::support::int;
use crate::support::SharedRef;
use crate::support::UnitType;

extern "C" {
fn v8__V8__SetFlagsFromCommandLine(
argc: *mut c_int,
argv: *mut *mut c_char,
usage: *const c_char,
argc: *mut int,
argv: *mut *mut char,
usage: *const char,
);
fn v8__V8__SetFlagsFromString(flags: *const u8, length: usize);
fn v8__V8__SetEntropySource(callback: EntropySource);
fn v8__V8__GetVersion() -> *const c_char;
fn v8__V8__GetVersion() -> *const char;
fn v8__V8__InitializePlatform(platform: *mut Platform);
fn v8__V8__Initialize();
fn v8__V8__Dispose() -> bool;
Expand Down Expand Up @@ -120,16 +120,16 @@ pub fn set_flags_from_command_line_with_usage(
.collect::<Vec<_>>();
let mut c_argv = raw_argv
.iter_mut()
.map(|arg| arg.as_mut_ptr() as *mut c_char)
.map(|arg| arg.as_mut_ptr() as *mut char)
.collect::<Vec<_>>();

// Store the length of the c_argv array in a local variable. We'll pass
// a pointer to this local variable to deno_set_v8_flags(), which then
// updates its value.
let mut c_argv_len = c_argv.len() as c_int;
let mut c_argv_len = c_argv.len() as int;
// Let v8 parse the arguments it recognizes and remove them from c_argv.
let c_usage = match usage {
Some(str) => CString::new(str).unwrap().into_raw() as *const c_char,
Some(str) => CString::new(str).unwrap().into_raw() as *const char,
None => std::ptr::null(),
};
unsafe {
Expand All @@ -145,7 +145,7 @@ pub fn set_flags_from_command_line_with_usage(
c_argv
.iter()
.map(|ptr| unsafe {
let cstr = CStr::from_ptr(*ptr as *const c_char);
let cstr = CStr::from_ptr(*ptr as *const char);
let slice = cstr.to_str().unwrap();
slice.to_string()
})
Expand Down
14 changes: 7 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::handle::UnsafeRefHandle;
use crate::isolate::BuildTypeIdHasher;
use crate::isolate::Isolate;
use crate::isolate::RawSlot;
use crate::support::int;
use crate::Context;
use crate::Function;
use crate::HandleScope;
Expand All @@ -11,7 +12,6 @@ use crate::Object;
use crate::ObjectTemplate;
use crate::Value;
use crate::Weak;
use libc::c_int;
use std::any::TypeId;
use std::collections::HashMap;
use std::ffi::c_void;
Expand All @@ -37,11 +37,11 @@ extern "C" {
fn v8__Context__GetNumberOfEmbedderDataFields(this: *const Context) -> u32;
fn v8__Context__GetAlignedPointerFromEmbedderData(
this: *const Context,
index: c_int,
index: int,
) -> *mut c_void;
fn v8__Context__SetAlignedPointerInEmbedderData(
this: *const Context,
index: c_int,
index: int,
value: *mut c_void,
);
fn v8__Context__FromSnapshot(
Expand All @@ -51,8 +51,8 @@ extern "C" {
}

impl Context {
const ANNEX_SLOT: c_int = 1;
const INTERNAL_SLOT_COUNT: c_int = 1;
const ANNEX_SLOT: int = 1;
const INTERNAL_SLOT_COUNT: int = 1;

/// Creates a new context.
#[inline(always)]
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Context {
);

let num_data_fields =
unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as c_int;
unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as int;
if num_data_fields > Self::ANNEX_SLOT {
let annex_ptr = unsafe {
v8__Context__GetAlignedPointerFromEmbedderData(self, Self::ANNEX_SLOT)
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Context {
)
};
assert!(
unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as c_int
unsafe { v8__Context__GetNumberOfEmbedderDataFields(self) } as int
> Self::ANNEX_SLOT
);

Expand Down
2 changes: 1 addition & 1 deletion src/external_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::support::intptr_t;
use crate::AccessorNameGetterCallback;
use crate::FunctionCallback;
use crate::MessageCallback;
use std::os::raw::c_void;
use std::ffi::c_void;

#[derive(Clone, Copy)]
pub union ExternalReference<'s> {
Expand Down
2 changes: 1 addition & 1 deletion src/fast_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::support::Opaque;
use crate::Local;
use crate::Value;
use libc::c_void;
use std::{
ffi::c_void,
mem::align_of,
ptr::{self, NonNull},
};
Expand Down
3 changes: 1 addition & 2 deletions src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Borrow;
use std::cell::Cell;
use std::ffi::c_void;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
Expand All @@ -8,8 +9,6 @@ use std::mem::transmute;
use std::ops::Deref;
use std::ptr::NonNull;

use libc::c_void;

use crate::support::Opaque;
use crate::Data;
use crate::HandleScope;
Expand Down
10 changes: 6 additions & 4 deletions src/icu.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{ffi::CString, os::raw::c_char};
use crate::support::char;

use std::ffi::CString;

extern "C" {
fn icu_get_default_locale(output: *mut c_char, output_len: usize) -> usize;
fn icu_set_default_locale(locale: *const c_char);
fn icu_get_default_locale(output: *mut char, output_len: usize) -> usize;
fn icu_set_default_locale(locale: *const char);
fn udata_setCommonData_71(this: *const u8, error_code: *mut i32);
}

Expand Down Expand Up @@ -56,7 +58,7 @@ pub fn set_common_data_71(data: &'static [u8]) -> Result<(), i32> {
pub fn get_language_tag() -> String {
let mut output = [0u8; 1024];
let len = unsafe {
icu_get_default_locale(output.as_mut_ptr() as *mut c_char, output.len())
icu_get_default_locale(output.as_mut_ptr() as *mut char, output.len())
};
std::str::from_utf8(&output[..len]).unwrap().to_owned()
}
Expand Down
6 changes: 3 additions & 3 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::isolate_create_params::CreateParams;
use crate::promise::PromiseRejectMessage;
use crate::scope::data::ScopeData;
use crate::snapshot::SnapshotCreator;
use crate::support::char;
use crate::support::int;
use crate::support::Allocated;
use crate::support::MapFnFrom;
Expand Down Expand Up @@ -49,7 +50,6 @@ use std::mem::size_of;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::DerefMut;
use std::os::raw::c_char;
use std::ptr;
use std::ptr::drop_in_place;
use std::ptr::null_mut;
Expand Down Expand Up @@ -302,11 +302,11 @@ pub type NearHeapLimitCallback = extern "C" fn(
#[repr(C)]
pub struct OomDetails {
pub is_heap_oom: bool,
pub detail: *const c_char,
pub detail: *const char,
}

pub type OomErrorCallback =
extern "C" fn(location: *const c_char, details: &OomDetails);
extern "C" fn(location: *const char, details: &OomDetails);

/// Collection of V8 heap information.
///
Expand Down
3 changes: 1 addition & 2 deletions src/isolate_create_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ use std::convert::TryFrom;
use std::iter::once;
use std::mem::size_of;
use std::mem::MaybeUninit;
use std::os::raw::c_char;
use std::ptr::null;

/// Should return a pointer to memory that persists for the lifetime of the
/// isolate.
pub type CounterLookupCallback = extern "C" fn(name: *const c_char) -> *mut i32;
pub type CounterLookupCallback = extern "C" fn(name: *const char) -> *mut i32;

/// Initial configuration parameters for a new Isolate.
#[must_use]
Expand Down
3 changes: 1 addition & 2 deletions src/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use libc::c_void;

use crate::isolate::Isolate;
use crate::support::int;
use crate::support::MapFnTo;
Expand All @@ -22,6 +20,7 @@ use crate::PropertyAttribute;
use crate::PropertyFilter;
use crate::Value;
use std::convert::TryFrom;
use std::ffi::c_void;
use std::num::NonZeroI32;

extern "C" {
Expand Down
8 changes: 4 additions & 4 deletions src/script_compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
use std::os::raw::c_int;
use std::{marker::PhantomData, mem::MaybeUninit};

use crate::support::int;
use crate::Function;
use crate::Local;
use crate::Module;
Expand Down Expand Up @@ -65,9 +65,9 @@ extern "C" {
pub struct Source {
_source_string: usize,
_resource_name: usize,
_resource_line_offset: c_int,
_resource_column_offset: c_int,
_resource_options: c_int,
_resource_line_offset: int,
_resource_column_offset: int,
_resource_options: int,
_source_map_url: usize,
_host_defined_options: usize,
_cached_data: usize,
Expand Down
3 changes: 2 additions & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use std::slice;

use crate::support::char;
use crate::support::int;
use crate::support::size_t;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::String;

extern "C" {
fn v8__String__kMaxLength() -> libc::size_t;
fn v8__String__kMaxLength() -> size_t;

fn v8__String__Empty(isolate: *mut Isolate) -> *const String;

Expand Down
5 changes: 5 additions & 0 deletions src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ use std::time::Instant;
#[allow(non_camel_case_types)]
pub type intptr_t = isize;

// TODO use libc::size_t when stable.
// https://doc.rust-lang.org/1.7.0/libc/type.size_t.html
#[allow(non_camel_case_types)]
pub type size_t = usize;

pub use std::os::raw::c_char as char;
pub use std::os::raw::c_int as int;
pub use std::os::raw::c_long as long;
Expand Down
3 changes: 1 addition & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use libc::c_void;

use crate::data::Data;
use crate::data::FunctionTemplate;
use crate::data::Name;
Expand Down Expand Up @@ -31,6 +29,7 @@ use crate::String;
use crate::Value;
use crate::NONE;
use std::convert::TryFrom;
use std::ffi::c_void;
use std::ptr::null;

extern "C" {
Expand Down
3 changes: 2 additions & 1 deletion src/typed_array.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
use crate::support::size_t;
use crate::ArrayBuffer;
use crate::HandleScope;
use crate::Local;
use crate::TypedArray;

extern "C" {
fn v8__TypedArray__kMaxLength() -> libc::size_t;
fn v8__TypedArray__kMaxLength() -> size_t;
}

impl TypedArray {
Expand Down