Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(webidl): add unrestricted binary32 float conversion

ldm04c13737bc839
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rs
index d053a1d55..4bcf25230 100644
--- a/moli-webidl-derive/src/converter.rs
+++ b/moli-webidl-derive/src/converter.rs
@@ -23,6 +23,7 @@ pub(crate) enum ConverterKind {
UnsignedLongLong,
EnforceRangeUnsignedLongLong,
Float,
+ UnrestrictedFloat,
Double,
UnrestrictedDouble,
Boolean,
@@ -56,6 +57,7 @@ impl ConverterKind {
"unsigned_long_long" => Ok(Self::UnsignedLongLong),
"enforce_range_unsigned_long_long" => Ok(Self::EnforceRangeUnsignedLongLong),
"float" => Ok(Self::Float),
+ "unrestricted_float" => Ok(Self::UnrestrictedFloat),
"double" => Ok(Self::Double),
"unrestricted_double" => Ok(Self::UnrestrictedDouble),
"boolean" => Ok(Self::Boolean),
@@ -90,6 +92,7 @@ impl ConverterKind {
quote!(::moli_webidl::EnforceRangeUnsignedLongLong)
}
Self::Float => quote!(::moli_webidl::Float),
+ Self::UnrestrictedFloat => quote!(::moli_webidl::UnrestrictedFloat),
Self::Double => quote!(::moli_webidl::Double),
Self::UnrestrictedDouble => quote!(::moli_webidl::UnrestrictedDouble),
Self::Boolean => quote!(::moli_webidl::Boolean),
@@ -131,6 +134,7 @@ impl ConverterKind {
quote!(::moli_webidl::EnforceRangeUnsignedLongLong(#expr))
}
Self::Float => quote!(::moli_webidl::Float(#expr)),
+ Self::UnrestrictedFloat => quote!(::moli_webidl::UnrestrictedFloat(#expr)),
Self::Double => quote!(::moli_webidl::Double(#expr)),
Self::UnrestrictedDouble => quote!(::moli_webidl::UnrestrictedDouble(#expr)),
Self::Boolean => quote!(::moli_webidl::Boolean(#expr)),
@@ -159,6 +163,7 @@ impl ConverterKind {
| Self::UnsignedLongLong
| Self::EnforceRangeUnsignedLongLong
| Self::Float
+ | Self::UnrestrictedFloat
| Self::Double
| Self::UnrestrictedDouble
| Self::Boolean
diff --git a/moli-webidl/src/convert.rs b/moli-webidl/src/convert.rs
index a34f9edff..4978fb0f6 100644
--- a/moli-webidl/src/convert.rs
+++ b/moli-webidl/src/convert.rs
@@ -168,6 +168,19 @@ impl<'s> WebIdlConverter<'s> for UnrestrictedDouble {
}
}
+impl<'s> WebIdlConverter<'s> for crate::UnrestrictedFloat {
+ type Options = ();
+
+ fn convert(
+ scope: &mut v8::PinScope<'s, '_>,
+ value: v8::Local<'s, v8::Value>,
+ context: Context,
+ _options: &Self::Options,
+ ) -> Result<Self, WebIdlError> {
+ number_value(scope, value, context, "unrestricted float").map(|value| Self(value as f32))
+ }
+}
+
impl<'s> WebIdlConverter<'s> for crate::Float {
type Options = ();
diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rs
index 6d99ca44b..11324ece9 100644
--- a/moli-webidl/src/lib.rs
+++ b/moli-webidl/src/lib.rs
@@ -94,7 +94,8 @@ pub use types::{
Boolean, BufferSource, ByteString, ClampedUnsignedShort, Dictionary, DomString, DomString16,
Double, EnforceRangeLong, EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue,
EventListenerOptions, InterfaceObject, InterfaceOptions, Long, Record, Sequence, Short,
- StringOptions, UnrestrictedDouble, UnsignedLong, UnsignedLongLong, UnsignedShort, UsvString,
+ StringOptions, UnrestrictedDouble, UnrestrictedFloat, UnsignedLong, UnsignedLongLong,
+ UnsignedShort, UsvString,
};
/// Restricted WebIDL float represented as a finite binary32 value.
diff --git a/moli-webidl/src/types.rs b/moli-webidl/src/types.rs
index 60c32c6b5..a155e87a1 100644
--- a/moli-webidl/src/types.rs
+++ b/moli-webidl/src/types.rs
@@ -207,6 +207,16 @@ impl From<UnrestrictedDouble> for f64 {
}
}
+/// A binary32 WebIDL number that permits infinities and NaN after rounding.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct UnrestrictedFloat(pub f32);
+
+impl From<UnrestrictedFloat> for f32 {
+ fn from(value: UnrestrictedFloat) -> Self {
+ value.0
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Float(pub f32);
diff --git a/moli-webidl/tests/float.rs b/moli-webidl/tests/float.rs
index d50521630..76dbd9668 100644
--- a/moli-webidl/tests/float.rs
+++ b/moli-webidl/tests/float.rs
@@ -14,6 +14,77 @@ struct ExplicitFloatArgs {
value: f32,
}
+#[derive(WebIdlArgs)]
+#[webidl(prefix = "Test.unrestrictedFloat")]
+struct UnrestrictedFloatArgs {
+ #[webidl(required, converter = "unrestricted_float")]
+ value: f32,
+}
+
+fn unrestricted_float<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(parsed) = moli_webidl::parse_args::<UnrestrictedFloatArgs>(scope, &args) {
+ rv.set(v8::Number::new(scope, f64::from(parsed.value)).into());
+ }
+}
+
+#[test]
+fn unrestricted_float_accepts_nonfinite_values_but_preserves_number_conversion_errors() {
+ moli_v8_test_util::ensure_v8();
+ let mut isolate = v8::Isolate::new(v8::CreateParams::default());
+ let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let callee = v8::Context::new(scope, Default::default());
+ let (function, type_error) = {
+ let scope = &mut v8::ContextScope::new(scope, callee);
+ let function = v8::Function::new(scope, unrestricted_float).unwrap();
+ let key = v8::String::new(scope, "TypeError").unwrap();
+ (
+ function,
+ callee.global(scope).get(scope, key.into()).unwrap(),
+ )
+ };
+ let caller = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, caller);
+ for (name, value) in [
+ ("convert", function.into()),
+ ("CalleeTypeError", type_error),
+ ] {
+ let key = v8::String::new(scope, name).unwrap();
+ assert_eq!(
+ caller.global(scope).set(scope, key.into(), value),
+ Some(true)
+ );
+ }
+ let source = v8::String::new(scope, r#"(() => {
+ const assert=(ok,message)=>{if(!ok)throw Error(message);};
+ for(const value of [0,-0,1/3,16777217,2**-149,2**-150,-(2**-150),
+ NaN,Infinity,-Infinity,3.5e38,1e300,undefined,'1.25',true,null]) {
+ assert(Object.is(convert(value),Math.fround(Number(value))),'binary32 rounding');
+ }
+ for(const value of [Symbol(),1n]) {
+ let error;try{convert(value);}catch(e){error=e;}
+ assert(error instanceof CalleeTypeError && !(error instanceof TypeError),'callee TypeError');
+ }
+ let error;try{convert();}catch(e){error=e;}
+ assert(error instanceof CalleeTypeError,'required argument');
+ let reads=0;assert(convert({valueOf(){reads++;return 1/3;}})===Math.fround(1/3)&&reads===1,'one conversion');
+ const sentinel={};try{convert({valueOf(){throw sentinel;}});}catch(e){error=e;}
+ assert(error===sentinel,'original exception');
+ return true;
+ })()"#).unwrap();
+ let script = v8::Script::compile(scope, source, None).unwrap();
+ assert!(
+ script
+ .run(scope)
+ .expect("unrestricted float conversion matrix")
+ .is_true()
+ );
+}
+
fn inferred_float<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,