-
Notifications
You must be signed in to change notification settings - Fork 1k
V8: add FromValue, error traits, roundtrip tests
#2971
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 @@ | ||
| # Seeds for failure cases proptest has generated in the past. It is | ||
| # automatically read and these particular cases re-run before any | ||
| # novel cases are generated. | ||
| # | ||
| # It is recommended to check this file in to source control so that | ||
| # everyone who runs the test benefits from these saved cases. | ||
| cc b9cebcbfdb32c25a2093f9e860502a771b4fbd22f92cc7a6a72032aed46dd476 # shrinks to x = -9223372036854775809 | ||
| cc 2480a43bc9d76592946409dbe626d75ddaa94c6f1506e6508e9e4c688ef36cec # shrinks to x = -340282366920938463463374607431768211456 |
This file contains hidden or 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,35 @@ | ||
| //! Utilities for error handling when dealing with V8. | ||
|
|
||
| use v8::{Exception, HandleScope, Local, Value}; | ||
|
|
||
| /// The result of trying to convert a [`Value`] in scope `'s` to some type `T`. | ||
| pub(super) type ValueResult<'s, T> = Result<T, Local<'s, Value>>; | ||
|
|
||
| /// Types that can convert into a JS string type. | ||
| pub(super) trait IntoJsString { | ||
| /// Converts `self` into a JS string. | ||
| fn into_string<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String>; | ||
| } | ||
|
|
||
| impl IntoJsString for String { | ||
| fn into_string<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String> { | ||
| v8::String::new(scope, &self).unwrap() | ||
| } | ||
| } | ||
|
|
||
| /// Error types that can convert into JS exception values. | ||
| pub(super) trait IntoException { | ||
| /// Converts `self` into a JS exception value. | ||
| fn into_exception<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, Value>; | ||
| } | ||
|
|
||
| /// A type converting into a JS `TypeError` exception. | ||
| #[derive(Copy, Clone)] | ||
| pub struct TypeError<M>(pub M); | ||
|
|
||
| impl<M: IntoJsString> IntoException for TypeError<M> { | ||
| fn into_exception<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, Value> { | ||
| let msg = self.0.into_string(scope); | ||
| Exception::type_error(scope, msg) | ||
| } | ||
| } |
This file contains hidden or 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,189 @@ | ||
| #![allow(dead_code)] | ||
|
|
||
| use super::error::{IntoException as _, TypeError, ValueResult}; | ||
| use bytemuck::{AnyBitPattern, NoUninit}; | ||
| use spacetimedb_sats::{i256, u256}; | ||
| use v8::{BigInt, Boolean, HandleScope, Int32, Local, Number, Uint32, Value}; | ||
|
|
||
| /// Types that a v8 [`Value`] can be converted into. | ||
| pub(super) trait FromValue: Sized { | ||
| /// Converts `val` in `scope` to `Self` if possible. | ||
| fn from_value<'s>(val: Local<'_, Value>, scope: &mut HandleScope<'s>) -> ValueResult<'s, Self>; | ||
| } | ||
|
|
||
| /// Provides a [`FromValue`] implementation. | ||
| macro_rules! impl_from_value { | ||
| ($ty:ty, ($val:ident, $scope:ident) => $logic:expr) => { | ||
| impl FromValue for $ty { | ||
| fn from_value<'s>($val: Local<'_, Value>, $scope: &mut HandleScope<'s>) -> ValueResult<'s, Self> { | ||
| $logic | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// Tries to cast `Value` into `T` or raises a JS exception as a returned `Err` value. | ||
| fn try_cast<'a, 'b, T>( | ||
| scope: &mut HandleScope<'a>, | ||
| val: Local<'b, Value>, | ||
| on_err: impl FnOnce(&str) -> String, | ||
| ) -> ValueResult<'a, Local<'b, T>> | ||
| where | ||
| Local<'b, T>: TryFrom<Local<'b, Value>>, | ||
| { | ||
| val.try_cast::<T>() | ||
| .map_err(|_| TypeError(on_err(val.type_repr())).into_exception(scope)) | ||
| } | ||
|
|
||
| /// Tries to cast `Value` into `T` or raises a JS exception as a returned `Err` value. | ||
| macro_rules! cast { | ||
| ($scope:expr, $val:expr, $js_ty:ty, $expected:literal $(, $args:expr)* $(,)?) => {{ | ||
| try_cast::<$js_ty>($scope, $val, |got| format!(concat!("Expected ", $expected, ", got {__got}"), $($args,)* __got = got)) | ||
| }}; | ||
| } | ||
| pub(super) use cast; | ||
|
|
||
| /// Returns a JS exception value indicating that a value overflowed | ||
| /// when converting to the type `rust_ty`. | ||
| fn value_overflowed<'s>(rust_ty: &str, scope: &mut HandleScope<'s>) -> Local<'s, Value> { | ||
| TypeError(format!("Value overflowed `{rust_ty}`")).into_exception(scope) | ||
| } | ||
|
|
||
| /// Returns a JS exception value indicating that a value underflowed | ||
| /// when converting to the type `rust_ty`. | ||
| fn value_underflowed<'s>(rust_ty: &str, scope: &mut HandleScope<'s>) -> Local<'s, Value> { | ||
| TypeError(format!("Value underflowed `{rust_ty}`")).into_exception(scope) | ||
| } | ||
|
|
||
| // `FromValue for bool`. | ||
| impl_from_value!(bool, (val, scope) => cast!(scope, val, Boolean, "boolean").map(|b| b.is_true())); | ||
|
|
||
| // `FromValue for u8, u16, u32, i8, i16, i32`. | ||
| macro_rules! int32_from_value { | ||
| ($js_ty:ty, $rust_ty:ty) => { | ||
| impl_from_value!($rust_ty, (val, scope) => { | ||
| let num = cast!(scope, val, $js_ty, "number for `{}`", stringify!($rust_ty))?; | ||
| num.value().try_into().map_err(|_| value_overflowed(stringify!($rust_ty), scope)) | ||
| }); | ||
| } | ||
| } | ||
| int32_from_value!(Uint32, u8); | ||
| int32_from_value!(Uint32, u16); | ||
| int32_from_value!(Uint32, u32); | ||
| int32_from_value!(Int32, i8); | ||
| int32_from_value!(Int32, i16); | ||
| int32_from_value!(Int32, i32); | ||
|
|
||
| // `FromValue for f32, f64`. | ||
| // | ||
| // Note that, as per the rust-reference, | ||
| // - "Casting from an f64 to an f32 will produce the closest possible f32" | ||
| // https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric.float-narrowing | ||
| macro_rules! float_from_value { | ||
| ($rust_ty:ty) => { | ||
| impl_from_value!($rust_ty, (val, scope) => { | ||
| cast!(scope, val, Number, "number for `{}`", stringify!($rust_ty)).map(|n| n.value() as _) | ||
| }); | ||
| } | ||
| } | ||
| float_from_value!(f32); | ||
| float_from_value!(f64); | ||
|
|
||
| // `FromValue for u64, i64`. | ||
| macro_rules! int64_from_value { | ||
| ($rust_ty:ty, $conv_method: ident) => { | ||
| impl_from_value!($rust_ty, (val, scope) => { | ||
| let rust_ty = stringify!($rust_ty); | ||
| let bigint = cast!(scope, val, BigInt, "bigint for `{}`", rust_ty)?; | ||
| let (val, ok) = bigint.$conv_method(); | ||
| ok.then_some(val).ok_or_else(|| value_overflowed(rust_ty, scope)) | ||
| }); | ||
| } | ||
| } | ||
| int64_from_value!(u64, u64_value); | ||
| int64_from_value!(i64, i64_value); | ||
|
|
||
| /// Converts `bigint` into its signnedness and its list of bytes in little-endian, | ||
| /// or errors on overflow or unwanted signedness. | ||
| /// | ||
| /// Parameters: | ||
| /// - `N` are the number of bytes to accept at most. | ||
| /// - `W = N / 8` are the number of words to accept at most. | ||
| /// - `UNSIGNED` is `true` if only unsigned integers are accepted. | ||
| /// - `rust_ty` is the target type as a string, for errors. | ||
| /// - `scope` for any JS exceptions that need to be raised. | ||
| /// - `bigint` is the integer to convert. | ||
| fn bigint_to_bytes<'s, const N: usize, const W: usize, const UNSIGNED: bool>( | ||
| rust_ty: &str, | ||
| scope: &mut HandleScope<'s>, | ||
| bigint: &BigInt, | ||
| ) -> ValueResult<'s, (bool, [u8; N])> | ||
| where | ||
| [[u8; 8]; W]: NoUninit, | ||
| [u8; N]: AnyBitPattern, | ||
| { | ||
| // Read the words. | ||
| let mut words = [0u64; W]; | ||
| let (sign, _) = bigint.to_words_array(&mut words); | ||
|
|
||
| if bigint.word_count() > W { | ||
| // There's an under-/over-flow if the caller cannot handle that many words. | ||
| return Err(if sign { | ||
| value_underflowed(rust_ty, scope) | ||
| } else { | ||
| value_overflowed(rust_ty, scope) | ||
| }); | ||
| } | ||
|
|
||
| if sign && UNSIGNED { | ||
| // There's an overflow if the caller cannot accept negative numbers. | ||
| return Err(value_overflowed(rust_ty, scope)); | ||
| } | ||
|
|
||
| // convert the words to little-endian bytes. | ||
| let bytes = bytemuck::must_cast(words.map(|w| w.to_le_bytes())); | ||
| Ok((sign, bytes)) | ||
| } | ||
|
|
||
| // `FromValue for u128, u256`. | ||
| macro_rules! unsigned_bigint_from_value { | ||
| ($rust_ty:ty, $bytes:literal, $words:literal) => { | ||
| impl_from_value!($rust_ty, (val, scope) => { | ||
| let rust_ty = stringify!($rust_ty); | ||
| let bigint = cast!(scope, val, v8::BigInt, "bigint for `{}`", rust_ty)?; | ||
| if let (val, true) = bigint.u64_value() { | ||
| // Fast path. | ||
| return Ok(val.into()); | ||
| } | ||
| let (_, bytes) = bigint_to_bytes::<$bytes, $words, true>(rust_ty, scope, &bigint)?; | ||
| Ok(Self::from_le_bytes(bytes)) | ||
| }); | ||
| }; | ||
| } | ||
| unsigned_bigint_from_value!(u128, 16, 2); | ||
| unsigned_bigint_from_value!(u256, 32, 4); | ||
|
|
||
| // `FromValue for i128, i256`. | ||
| macro_rules! signed_bigint_from_value { | ||
| ($rust_ty:ty, $bytes:literal, $words:literal) => { | ||
| impl_from_value!($rust_ty, (val, scope) => { | ||
| let rust_ty = stringify!($rust_ty); | ||
| let bigint = cast!(scope, val, v8::BigInt, "bigint for `{}`", rust_ty)?; | ||
| if let (val, true) = bigint.i64_value() { | ||
| // Fast path. | ||
| return Ok(val.into()); | ||
| } | ||
| let (sign, bytes) = bigint_to_bytes::<$bytes, $words, false>(rust_ty, scope, &bigint)?; | ||
| let x = Self::from_le_bytes(bytes); | ||
| Ok(if sign { | ||
| // A negative number, but we have a positive number `x`, so we want `-x`. | ||
| // If that's not possible, and as we know there's no underflow, we have `MIN`. | ||
| x.checked_neg().unwrap_or(Self::MIN) | ||
| } else { | ||
| x | ||
| }) | ||
| }); | ||
| }; | ||
| } | ||
| signed_bigint_from_value!(i128, 16, 2); | ||
| signed_bigint_from_value!(i256, 32, 4); | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.