-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serde_v8): support BigInt serialization
This commit enables serializing `v8::BigInt` to `num_bigint::BigInt` in Rust.
- Loading branch information
1 parent
33b85a2
commit 7951026
Showing
12 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use super::transl8::FromV8; | ||
use super::transl8::ToV8; | ||
use crate::magic::transl8::impl_magic; | ||
use crate::Error; | ||
|
||
#[derive( | ||
PartialEq, | ||
Eq, | ||
Clone, | ||
Debug, | ||
Default, | ||
derive_more::Deref, | ||
derive_more::DerefMut, | ||
derive_more::AsRef, | ||
derive_more::AsMut, | ||
)] | ||
#[as_mut(forward)] | ||
#[as_ref(forward)] | ||
pub struct BigInt(num_bigint::BigInt); | ||
impl_magic!(BigInt); | ||
|
||
impl ToV8 for BigInt { | ||
fn to_v8<'a>( | ||
&mut self, | ||
scope: &mut v8::HandleScope<'a>, | ||
) -> Result<v8::Local<'a, v8::Value>, crate::Error> { | ||
let (sign, words) = self.0.to_u64_digits(); | ||
let sign_bit = sign == num_bigint::Sign::Minus; | ||
let v = v8::BigInt::new_from_words(scope, sign_bit, &words).unwrap(); | ||
Ok(v.into()) | ||
} | ||
} | ||
|
||
impl FromV8 for BigInt { | ||
fn from_v8( | ||
_scope: &mut v8::HandleScope, | ||
value: v8::Local<v8::Value>, | ||
) -> Result<Self, crate::Error> { | ||
let v8bigint = v8::Local::<v8::BigInt>::try_from(value) | ||
.map_err(|_| Error::ExpectedBigInt)?; | ||
let word_count = v8bigint.word_count(); | ||
let mut words = vec![0; word_count]; | ||
let (sign_bit, _words) = v8bigint.to_words_array(&mut words); | ||
let sign = match sign_bit { | ||
true => num_bigint::Sign::Minus, | ||
false => num_bigint::Sign::Plus, | ||
}; | ||
// 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 { words.align_to::<u32>() }; | ||
assert!(prefix.is_empty()); | ||
assert!(suffix.is_empty()); | ||
assert_eq!(slice.len(), words.len() * 2); | ||
let big_int = num_bigint::BigInt::from_slice(sign, slice); | ||
Ok(Self(big_int)) | ||
} | ||
} | ||
|
||
impl From<num_bigint::BigInt> for BigInt { | ||
fn from(big_int: num_bigint::BigInt) -> Self { | ||
Self(big_int) | ||
} | ||
} | ||
|
||
impl From<BigInt> for num_bigint::BigInt { | ||
fn from(big_int: BigInt) -> Self { | ||
big_int.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters