Skip to content
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
2 changes: 1 addition & 1 deletion src/rust/jsg-test/tests/coercion_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ impl Conversion {
.set(lock, 0, undefined)
}
Self::DictKeys | Self::DictValue | Self::DictValueCoercion => {
jsg::Dict::<Number>::from_js(lock, value).map(drop)
jsg::Dict::<f64>::from_js(lock, value).map(drop)
}
}
}
Expand Down
49 changes: 24 additions & 25 deletions src/rust/jsg-test/tests/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

use jsg::Dict;
use jsg::FromJS;
use jsg::Number;
use jsg::v8;
use jsg_macros::jsg_method;
use jsg_macros::jsg_property;
Expand All @@ -22,9 +21,9 @@ struct Records {
impl Records {
/// Describes the entries, in order, as `key=value` pairs.
#[jsg_method]
pub fn describe(&self, dict: Dict<Number>) -> String {
pub fn describe(&self, dict: Dict<f64>) -> String {
dict.into_iter()
.map(|(key, value)| format!("{key}={}", value.value()))
.map(|(key, value)| format!("{key}={value}"))
.collect::<Vec<_>>()
.join(",")
}
Expand All @@ -38,29 +37,25 @@ impl Records {
}

#[jsg_method]
pub fn count_present(&self, dict: Dict<Option<Number>>) -> Number {
Number::from(u32::try_from(dict.into_iter().filter(|(_, v)| v.is_some()).count()).unwrap())
pub fn count_present(&self, dict: Dict<Option<f64>>) -> f64 {
f64::from(u32::try_from(dict.into_iter().filter(|(_, v)| v.is_some()).count()).unwrap())
}

#[jsg_method]
pub fn total_lengths(&self, dict: Dict<Vec<Number>>) -> Number {
Number::from(u32::try_from(dict.into_iter().map(|(_, v)| v.len()).sum::<usize>()).unwrap())
pub fn total_lengths(&self, dict: Dict<Vec<f64>>) -> f64 {
f64::from(u32::try_from(dict.into_iter().map(|(_, v)| v.len()).sum::<usize>()).unwrap())
}

#[jsg_method]
pub fn sum_nested(&self, dict: Dict<Dict<Number>>) -> Number {
Number::new(
dict.into_iter()
.flat_map(|(_, inner)| inner.into_iter().map(|(_, v)| v.value()))
.sum(),
)
pub fn sum_nested(&self, dict: Dict<Dict<f64>>) -> f64 {
dict.into_iter()
.flat_map(|(_, inner)| inner.into_iter().map(|(_, v)| v))
.sum()
}

#[jsg_method]
pub fn count_functions<'s>(&self, dict: Dict<v8::Local<'s, v8::Value>>) -> Number {
Number::from(
u32::try_from(dict.into_iter().filter(|(_, v)| v.is_function()).count()).unwrap(),
)
pub fn count_functions<'s>(&self, dict: Dict<v8::Local<'s, v8::Value>>) -> f64 {
f64::from(u32::try_from(dict.into_iter().filter(|(_, v)| v.is_function()).count()).unwrap())
}

#[jsg_method]
Expand All @@ -69,16 +64,16 @@ impl Records {
}

#[jsg_method]
pub fn make(&self, keys: Vec<String>) -> Dict<Number> {
pub fn make(&self, keys: Vec<String>) -> Dict<f64> {
keys.into_iter()
.zip(1..)
.map(|(key, n)| (key, Number::from(n)))
.zip(1u32..)
.map(|(key, n)| (key, f64::from(n)))
.collect()
}

/// Is terminated and then returns a `Dict`, which is converted after the termination.
#[jsg_method]
pub fn terminate_then_make(&self, lock: &mut jsg::Lock<'_>) -> Dict<Number> {
pub fn terminate_then_make(&self, lock: &mut jsg::Lock<'_>) -> Dict<f64> {
crate::Harness::terminate_and_deliver(lock);
self.make(vec!["a".to_owned()])
}
Expand Down Expand Up @@ -188,10 +183,10 @@ fn values_other_than_objects_are_not_dicts() {
);
}
let value = ctx.eval_raw(lock, "[1]").unwrap();
let err = Dict::<Number>::from_js(lock, value).unwrap_err();
let err = Dict::<f64>::from_js(lock, value).unwrap_err();
assert_eq!(err.message, "Expected object but got array");
let value = ctx.eval_raw(lock, "1").unwrap();
let err = Dict::<Number>::from_js(lock, value).unwrap_err();
let err = Dict::<f64>::from_js(lock, value).unwrap_err();
assert_eq!(err.message, "Expected object but got number");
});
}
Expand Down Expand Up @@ -436,6 +431,10 @@ fn dicts_round_trip() {
}

#[test]
#[expect(
clippy::float_cmp,
reason = "the values are small integers, exactly representable"
)]
fn get_by_name_and_keys() {
with_records(|lock, ctx| {
let value = ctx
Expand All @@ -449,11 +448,11 @@ fn get_by_name_and_keys() {
.collect();
assert_eq!(names, ["b", "a"]);
let value = object.get_by_name(lock, keys[1].into()).unwrap();
assert_eq!(Number::from_js(lock, value).unwrap(), Number::new(3.0));
assert_eq!(f64::from_js(lock, value).unwrap(), 3.0);

let symbol = ctx.eval_raw(lock, "Symbol.for('s')").unwrap();
let symbol = v8::Local::<v8::Symbol>::try_from(symbol).unwrap();
let value = object.get_by_name(lock, symbol.into()).unwrap();
assert_eq!(Number::from_js(lock, value).unwrap(), Number::new(2.0));
assert_eq!(f64::from_js(lock, value).unwrap(), 2.0);
});
}
4 changes: 2 additions & 2 deletions src/rust/jsg/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
/// }
///
/// #[jsg_method]
/// pub fn get_counts(&self) -> Dict<jsg::Number> {
/// Dict::from(vec![("hits".to_owned(), jsg::Number::new(3.0))])
/// pub fn get_counts(&self) -> Dict<f64> {
/// Dict::from(vec![("hits".to_owned(), 3.0)])
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
Loading