Skip to content
Closed
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
8 changes: 8 additions & 0 deletions crates/core/proptest-regressions/host/v8/ser.txt
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 b4eaf1389ae0810b57684a9ca8315fb70e1bd59227a979fb90ac2885d9887684 # shrinks to (ty, val) = (Product(ProductType {"field_0": Bool}), Product(ProductValue { elements: [Bool(false)] }))
cc 191c44593d03c0c9d47fed4ccc34d87b824f3964cf11ba87cc324cf67fea475c # shrinks to (ty, val) = (Sum(SumType {"variant_0": Bool}), Sum(SumValue { tag: 0, value: Bool(false) }))
21 changes: 13 additions & 8 deletions crates/core/src/host/v8/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use spacetimedb_sats::de::{
};
use spacetimedb_sats::{i256, u256};
use std::borrow::{Borrow, Cow};
use v8::{Array, Global, HandleScope, Local, Object, Uint8Array, Value};
use v8::{Array, Global, HandleScope, Local, Name, Object, Uint8Array, Value};

/// Deserializes from V8 values.
pub(super) struct Deserializer<'a, 's> {
Expand Down Expand Up @@ -48,7 +48,7 @@ impl<'s> DeserializerCommon<'_, 's> {
}

/// The possible errors that [`Deserializer`] can produce.
#[derive(From)]
#[derive(Debug, From)]
pub(super) enum Error<'s> {
Value(Local<'s, Value>),
Exception(ExceptionThrown),
Expand Down Expand Up @@ -104,7 +104,7 @@ impl KeyCache {
}

// Creates an interned [`v8::String`].
fn v8_interned_string<'s>(scope: &mut HandleScope<'s>, field: &str) -> Local<'s, v8::String> {
pub(super) fn v8_interned_string<'s>(scope: &mut HandleScope<'s>, field: &str) -> Local<'s, v8::String> {
// Internalized v8 strings are significantly faster than "normal" v8 strings
// since v8 deduplicates re-used strings minimizing new allocations
// see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
Expand Down Expand Up @@ -268,6 +268,15 @@ struct ProductAccess<'a, 's> {
index: usize,
}

/// Normalizes `field` into an interned `v8::String`.
pub(super) fn intern_field_name<'s>(scope: &mut HandleScope<'s>, field: Option<&str>, index: usize) -> Local<'s, Name> {
let field = match field {
Some(field) => Cow::Borrowed(field),
None => Cow::Owned(format!("{index}")),
};
v8_interned_string(scope, &field).into()
}

impl<'de, 's: 'de> de::NamedProductAccess<'de> for ProductAccess<'_, 's> {
type Error = Error<'s>;

Expand All @@ -282,13 +291,9 @@ impl<'de, 's: 'de> de::NamedProductAccess<'de> for ProductAccess<'_, 's> {
// Normalize the field name.
// Integer keys are converted to strings,
// as that is supported on JS objects.
let field = match field {
Some(field) => Cow::Borrowed(field),
None => Cow::Owned(format!("{index}")),
};
let key = intern_field_name(scope, field, index);

// Check that such a field/key exists.
let key = v8_interned_string(scope, &field).into();
if !self
.object
.has_own_property(scope, key)
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/host/v8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::sync::{Arc, LazyLock};
mod de;
mod error;
mod from_value;
mod ser;
mod to_value;

/// The V8 runtime, for modules written in e.g., JS or TypeScript.
Expand Down
312 changes: 312 additions & 0 deletions crates/core/src/host/v8/ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
#![allow(dead_code)]

use super::de::{intern_field_name, KeyCache};
use super::error::{exception_already_thrown, ExceptionThrown};
use super::to_value::ToValue;
use core::num::TryFromIntError;
use derive_more::From;
use spacetimedb_sats::{
i256,
ser::{self, Serialize},
u256,
};
use v8::{Array, ArrayBuffer, HandleScope, IntegrityLevel, Local, Object, Uint8Array, Value};

/// Deserializes to V8 values.
pub(super) struct Serializer<'a, 's> {
/// The scope to serialize values into.
scope: &'a mut HandleScope<'s>,
/// A cache for frequently used strings.
key_cache: &'a mut KeyCache,
}

impl<'a, 's> Serializer<'a, 's> {
/// Creates a new serializer into `scope`.
pub fn new(scope: &'a mut HandleScope<'s>, key_cache: &'a mut KeyCache) -> Self {
Self { scope, key_cache }
}

fn reborrow(&mut self) -> Serializer<'_, 's> {
Serializer {
scope: self.scope,
key_cache: self.key_cache,
}
}
}

/// The possible errors that [`Serializer`] can produce.
#[derive(Debug, From)]
pub(super) enum Error {
Custom(String),
Exception(ExceptionThrown),
StringTooLarge(usize),
ArrayLengthTooLarge(TryFromIntError),
}

impl ser::Error for Error {
fn custom<T: core::fmt::Display>(msg: T) -> Self {
Self::Custom(msg.to_string())
}
}

/// Serializes a primitive via [`ToValue`].
macro_rules! serialize_primitive {
($method:ident, $ty:ty) => {
fn $method(self, val: $ty) -> Result<Self::Ok, Self::Error> {
Ok(ToValue::to_value(&val, self.scope))
}
};
}

/// Seal the object, so that e.g., new properties cannot be added.
///
/// However, the values of existing properties may be modified,
/// which can be useful if the module wants to modify a property
/// and then send the object back.
fn seal_object(scope: &mut HandleScope, object: &Object) -> Result<(), ExceptionThrown> {
let _ = object
.set_integrity_level(scope, IntegrityLevel::Sealed)
.ok_or_else(exception_already_thrown)?;
Ok(())
}

impl<'a, 's> ser::Serializer for Serializer<'a, 's> {
type Ok = Local<'s, Value>;
type Error = Error;

type SerializeArray = SerializeArray<'a, 's>;
type SerializeSeqProduct = Self::SerializeNamedProduct;
type SerializeNamedProduct = SerializeNamedProduct<'a, 's>;

// Serialization of primitive types defers to `ToValue`.
serialize_primitive!(serialize_bool, bool);
serialize_primitive!(serialize_u8, u8);
serialize_primitive!(serialize_u16, u16);
serialize_primitive!(serialize_u32, u32);
serialize_primitive!(serialize_u64, u64);
serialize_primitive!(serialize_u128, u128);
serialize_primitive!(serialize_u256, u256);
serialize_primitive!(serialize_i8, i8);
serialize_primitive!(serialize_i16, i16);
serialize_primitive!(serialize_i32, i32);
serialize_primitive!(serialize_i64, i64);
serialize_primitive!(serialize_i128, i128);
serialize_primitive!(serialize_i256, i256);
serialize_primitive!(serialize_f64, f64);
serialize_primitive!(serialize_f32, f32);

fn serialize_str(self, string: &str) -> Result<Self::Ok, Self::Error> {
v8::String::new(self.scope, string)
.map(Into::into)
.ok_or(Error::StringTooLarge(string.len()))
}

fn serialize_bytes(self, bytes: &[u8]) -> Result<Self::Ok, Self::Error> {
let store = ArrayBuffer::new_backing_store_from_boxed_slice(bytes.into()).make_shared();
let buf = ArrayBuffer::with_backing_store(self.scope, &store);
Ok(Uint8Array::new(self.scope, buf, 0, bytes.len()).unwrap().into())
}

fn serialize_array(self, len: usize) -> Result<Self::SerializeArray, Self::Error> {
let len = len.try_into()?;
Ok(SerializeArray {
array: Array::new(self.scope, len),
inner: self,
next_index: 0,
})
}

fn serialize_seq_product(self, len: usize) -> Result<Self::SerializeSeqProduct, Self::Error> {
self.serialize_named_product(len)
}

fn serialize_named_product(self, _len: usize) -> Result<Self::SerializeNamedProduct, Self::Error> {
// TODO(noa): this can be more efficient if we tell it the names ahead of time
let object = Object::new(self.scope);
Ok(SerializeNamedProduct {
inner: self,
object,
next_index: 0,
})
}

fn serialize_variant<T: Serialize + ?Sized>(
mut self,
tag: u8,
var_name: Option<&str>,
value: &T,
) -> Result<Self::Ok, Self::Error> {
// Serialize the payload.
let value_value: Local<'s, Value> = value.serialize(self.reborrow())?;
// Figure out the tag.
let tag_value: Local<'s, Value> = intern_field_name(self.scope, var_name, tag as usize).into();
let values = [tag_value, value_value];

// The property keys are always `"tag"` an `"value"`.
let names = [
self.key_cache.tag(self.scope).into(),
self.key_cache.value(self.scope).into(),
];

// Stitch together the object.
let prototype = v8::null(self.scope).into();
let object = Object::with_prototype_and_properties(self.scope, prototype, &names, &values);
seal_object(self.scope, &object)?;
Ok(object.into())
}
Comment on lines +133 to +156
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut says this should have special handling for options, the same way that the deserialize path does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but a problem here is that we actually do not have enough information to tell that the type is option here because you don't know about the other variant. For example, for .serialize_variant(0, Some("none"), payload), it could be option, but you could also have 3 variants instead of 2. Threading this information through SATS is currently not trivial. We might want to consider e.g., adding AlgebraicTypeLayout::Optional(_), which could actually simplify things down the road. But for now, I'd rather remove the special case from the deserialization end instead.

(See also #2985 (comment).)

}

/// Serializes array elements and finalizes the JS array.
pub(super) struct SerializeArray<'a, 's> {
inner: Serializer<'a, 's>,
array: Local<'s, Array>,
next_index: u32,
}

impl<'s> ser::SerializeArray for SerializeArray<'_, 's> {
type Ok = Local<'s, Value>;
type Error = Error;

fn serialize_element<T: Serialize + ?Sized>(&mut self, elem: &T) -> Result<(), Self::Error> {
// Serialize the current `elem`ent.
let value = elem.serialize(self.inner.reborrow())?;

// Set the value to the array slot at `index`.
let index = self.next_index;
self.next_index += 1;
self.array
.set_index(self.inner.scope, index, value)
.ok_or_else(exception_already_thrown)?;

Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(self.array.into())
}
}

/// Serializes into JS objects where field names are turned into property names.
pub(super) struct SerializeNamedProduct<'a, 's> {
inner: Serializer<'a, 's>,
object: Local<'s, Object>,
next_index: usize,
}

impl<'s> ser::SerializeSeqProduct for SerializeNamedProduct<'_, 's> {
type Ok = Local<'s, Value>;
type Error = Error;

fn serialize_element<T: Serialize + ?Sized>(&mut self, elem: &T) -> Result<(), Self::Error> {
ser::SerializeNamedProduct::serialize_element(self, None, elem)
}

fn end(self) -> Result<Self::Ok, Self::Error> {
ser::SerializeNamedProduct::end(self)
}
}

impl<'s> ser::SerializeNamedProduct for SerializeNamedProduct<'_, 's> {
type Ok = Local<'s, Value>;
type Error = Error;

fn serialize_element<T: Serialize + ?Sized>(
&mut self,
field_name: Option<&str>,
elem: &T,
) -> Result<(), Self::Error> {
// Serialize the field value.
let value = elem.serialize(self.inner.reborrow())?;

// Figure out the object property to use.
let scope = &mut *self.inner.scope;
let index = self.next_index;
self.next_index += 1;
let key = intern_field_name(scope, field_name, index).into();

// Set the value to the property.
self.object
.set(scope, key, value)
.ok_or_else(exception_already_thrown)?;

Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
seal_object(self.inner.scope, &self.object)?;
Ok(self.object.into())
}
}

#[cfg(test)]
mod test {
use super::super::de::Deserializer;
use super::super::to_value::test::with_scope;
use super::*;
use core::fmt::Debug;
use proptest::prelude::*;
use spacetimedb_lib::{AlgebraicType, AlgebraicValue};
use spacetimedb_sats::de::DeserializeSeed;
use spacetimedb_sats::proptest::generate_typed_value;
use spacetimedb_sats::{product, SumValue, ValueWithType, WithTypespace};
use AlgebraicType::Bool;

/// Roundtrips `rust_val` via [`Serialize`] to the V8 representation
/// and then back via [`DeserializeSeed`],
/// asserting that it's the same as the passed value.
fn assert_roundtrips<B: Debug>(
rust_val: impl Serialize + PartialEq<B> + Debug,
seed: impl for<'de> DeserializeSeed<'de, Output = B>,
) {
with_scope(|scope| {
let key_cache = &mut KeyCache::default();

// Convert to JS...
let ser = Serializer::new(scope, key_cache);
let js_val = rust_val.serialize(ser).unwrap();

// ...and then back to Rust.
let de = Deserializer::new(scope, js_val, key_cache);
let rust_val_prime = seed.deserialize(de).unwrap();

// We should end up where we started.
assert_eq!(rust_val, rust_val_prime);
})
}

fn assert_roundtrips_with_ty(ty: AlgebraicType, val: AlgebraicValue) {
let ctx = WithTypespace::empty(&ty);
let value = ValueWithType::new(ctx, &val);
let seed = value.ty_s();
assert_roundtrips(value, seed);
}

proptest! {
#[test]
fn test_random_typed_value_roundtrips((ty, val) in generate_typed_value()) {
assert_roundtrips_with_ty(ty, val);
}
}

#[test]
fn anonymized_product_works() {
let ty = AlgebraicType::product([Bool]);
let val = product![false].into();
assert_roundtrips_with_ty(ty, val);
}

/// This test demonstrates that serialization misbehaves without using [`ValueWithType`].
#[test]
fn regression_test_product_serialization_needs_value_with_type() {
let ty = AlgebraicType::product([("field_0", Bool)]);
let val = product![false].into();
assert_roundtrips_with_ty(ty, val);
}

#[test]
fn regression_test_variant() {
let ty = AlgebraicType::sum([("variant_0", Bool)]);
let val = SumValue::new(0, false).into();
assert_roundtrips_with_ty(ty, val);
}
}
Loading
Loading