Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ deno_unsync.workspace = true
futures.workspace = true
indexmap.workspace = true
libc.workspace = true
num-bigint.workspace = true
parking_lot.workspace = true
percent-encoding.workspace = true
pin-project.workspace = true
Expand Down
43 changes: 43 additions & 0 deletions core/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ impl_number_types!(
u8, i8, u16, i16, u32, i32, u64, i64, usize, isize, f32, f64
);

#[derive(Debug)]
pub struct BigInt {
pub sign_bit: bool,
pub words: Vec<u64>,
Expand Down Expand Up @@ -360,6 +361,37 @@ impl<'s> FromV8<'s> for BigInt {
}
}

impl From<num_bigint::BigInt> for BigInt {
fn from(big_int: num_bigint::BigInt) -> Self {
let (sign, words) = big_int.to_u64_digits();
Self {
sign_bit: sign == num_bigint::Sign::Minus,
words,
}
}
}

impl From<BigInt> for num_bigint::BigInt {
fn from(big_int: BigInt) -> Self {
// SAFETY: Because the alignment of u64 is 8, the alignment of u32 is 4, and
// the size of u64 is 8, the size of u32 is 4, the alignment of u32 is a
// factor of the alignment of u64, and the size of u32 is a factor of the
// size of u64, we can safely transmute the slice of u64 to a slice of u32.
let (prefix, slice, suffix) = unsafe { big_int.words.align_to::<u32>() };
assert!(prefix.is_empty());
assert!(suffix.is_empty());
assert_eq!(slice.len(), big_int.words.len() * 2);
Self::from_slice(
if big_int.sign_bit {
num_bigint::Sign::Minus
} else {
num_bigint::Sign::Plus
},
slice,
)
}
}

impl<'s> ToV8<'s> for bool {
type Error = Infallible;
#[inline]
Expand Down Expand Up @@ -424,6 +456,17 @@ impl<'s> ToV8<'s> for &'static str {
}
}

impl<'s> ToV8<'s> for Box<str> {
type Error = Infallible;
#[inline]
fn to_v8<'i>(
self,
scope: &mut v8::PinScope<'s, 'i>,
) -> Result<v8::Local<'s, v8::Value>, Self::Error> {
Ok(v8::String::new(scope, &self).unwrap().into()) // TODO
}
}

const USIZE2X: usize = std::mem::size_of::<usize>() * 2;
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub struct ByteString(SmallVec<[u8; USIZE2X]>);
Expand Down
3 changes: 3 additions & 0 deletions ops/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub mod to_v8;
mod kw {
syn::custom_keyword!(rename);
syn::custom_keyword!(serde);
syn::custom_keyword!(tag);
syn::custom_keyword!(untagged);
syn::custom_keyword!(content);
}

#[allow(dead_code)]
Expand Down
Loading
Loading