Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
feat(webidl): support restricted float conversion
ldm0e03f6a71c5c9
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rsindex aeb5aecd3..0db14acd5 100644--- a/moli-webidl-derive/src/converter.rs+++ b/moli-webidl-derive/src/converter.rs@@ -21,6 +21,7 @@ pub(crate) enum ConverterKind {EnforceRangeUnsignedLong,UnsignedLongLong,EnforceRangeUnsignedLongLong,+ Float,Double,UnrestrictedDouble,Boolean,@@ -50,6 +51,7 @@ impl ConverterKind {"enforce_range_unsigned_long" => Ok(Self::EnforceRangeUnsignedLong),"unsigned_long_long" => Ok(Self::UnsignedLongLong),"enforce_range_unsigned_long_long" => Ok(Self::EnforceRangeUnsignedLongLong),+ "float" => Ok(Self::Float),"double" => Ok(Self::Double),"unrestricted_double" => Ok(Self::UnrestrictedDouble),"boolean" => Ok(Self::Boolean),@@ -82,6 +84,7 @@ impl ConverterKind {Self::EnforceRangeUnsignedLongLong => {quote!(::moli_webidl::EnforceRangeUnsignedLongLong)}+ Self::Float => quote!(::moli_webidl::Float),Self::Double => quote!(::moli_webidl::Double),Self::UnrestrictedDouble => quote!(::moli_webidl::UnrestrictedDouble),Self::Boolean => quote!(::moli_webidl::Boolean),@@ -119,6 +122,7 @@ impl ConverterKind {Self::EnforceRangeUnsignedLongLong => {quote!(::moli_webidl::EnforceRangeUnsignedLongLong(#expr))}+ Self::Float => quote!(::moli_webidl::Float(#expr)),Self::Double => quote!(::moli_webidl::Double(#expr)),Self::UnrestrictedDouble => quote!(::moli_webidl::UnrestrictedDouble(#expr)),Self::Boolean => quote!(::moli_webidl::Boolean(#expr)),@@ -143,6 +147,7 @@ impl ConverterKind {| Self::EnforceRangeUnsignedLong| Self::UnsignedLongLong| Self::EnforceRangeUnsignedLongLong+ | Self::Float| Self::Double| Self::UnrestrictedDouble| Self::Boolean@@ -240,6 +245,9 @@ fn infer_converter_kind(ty: &Type) -> Option<ConverterKind> {if is_type_ident(ty, "u64") {return Some(ConverterKind::UnsignedLongLong);}+ if is_type_ident(ty, "f32") {+ return Some(ConverterKind::Float);+ }if is_type_ident(ty, "f64") {return Some(ConverterKind::UnrestrictedDouble);}diff --git a/moli-webidl/src/convert.rs b/moli-webidl/src/convert.rsindex 1bc7620c6..caec06c57 100644--- a/moli-webidl/src/convert.rs+++ b/moli-webidl/src/convert.rs@@ -155,6 +155,24 @@ impl<'s> WebIdlConverter<'s> for UnrestrictedDouble {}}+impl<'s> WebIdlConverter<'s> for crate::Float {+ type Options = ();++ fn convert(+ scope: &mut v8::PinScope<'s, '_>,+ value: v8::Local<'s, v8::Value>,+ context: Context,+ _options: &Self::Options,+ ) -> Result<Self, WebIdlError> {+ let value = number_value(scope, value, context, "float")? as f32;+ if value.is_finite() {+ Ok(Self(value))+ } else {+ Err(WebIdlError::cannot_convert(context, "float"))+ }+ }+}+impl<'s> WebIdlConverter<'s> for Double {type Options = ();diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rsindex b3848b5fd..dcf51022d 100644--- a/moli-webidl/src/lib.rs+++ b/moli-webidl/src/lib.rs@@ -67,3 +67,6 @@ pub use types::{Record, Sequence, StringOptions, UnrestrictedDouble, UnsignedLong, UnsignedLongLong,UnsignedShort, UsvString,};++/// Restricted WebIDL float represented as a finite binary32 value.+pub use types::Float;diff --git a/moli-webidl/src/types.rs b/moli-webidl/src/types.rsindex c3f318e5b..08c784406 100644--- a/moli-webidl/src/types.rs+++ b/moli-webidl/src/types.rs@@ -168,6 +168,15 @@ impl From<UnrestrictedDouble> for f64 {}}+#[derive(Debug, Clone, Copy, PartialEq)]+pub struct Float(pub f32);++impl From<Float> for f32 {+ fn from(value: Float) -> Self {+ value.0+ }+}+#[derive(Debug, Clone, Copy, PartialEq)]pub struct Double(pub f64);diff --git a/moli-webidl/tests/float.rs b/moli-webidl/tests/float.rsnew file mode 100644index 000000000..d50521630--- /dev/null+++ b/moli-webidl/tests/float.rs@@ -0,0 +1,100 @@+use moli_webidl::WebIdlArgs;++#[derive(WebIdlArgs)]+#[webidl(prefix = "Test.float")]+struct InferredFloatArgs {+ #[webidl(required)]+ value: f32,+}++#[derive(WebIdlArgs)]+#[webidl(prefix = "Test.explicitFloat")]+struct ExplicitFloatArgs {+ #[webidl(required, converter = "float")]+ value: f32,+}++fn inferred_float<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ args: v8::FunctionCallbackArguments<'s>,+ mut rv: v8::ReturnValue<'s>,+) {+ if let Some(parsed) = moli_webidl::parse_args::<InferredFloatArgs>(scope, &args) {+ rv.set(v8::Number::new(scope, f64::from(parsed.value)).into());+ }+}++fn explicit_float<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ args: v8::FunctionCallbackArguments<'s>,+ mut rv: v8::ReturnValue<'s>,+) {+ if let Some(parsed) = moli_webidl::parse_args::<ExplicitFloatArgs>(scope, &args) {+ rv.set(v8::Number::new(scope, f64::from(parsed.value)).into());+ }+}++#[test]+fn restricted_float_rounds_and_preserves_conversion_errors_in_the_callee_realm() {+ 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 values = {+ let scope = &mut v8::ContextScope::new(scope, callee);+ let inferred = v8::Function::new(scope, inferred_float).unwrap();+ let explicit = v8::Function::new(scope, explicit_float).unwrap();+ let key = v8::String::new(scope, "TypeError").unwrap();+ let error = callee.global(scope).get(scope, key.into()).unwrap();+ [+ ("inferred", inferred.into()),+ ("explicit", explicit.into()),+ ("CalleeTypeError", error),+ ]+ };+ let caller = v8::Context::new(scope, Default::default());+ let scope = &mut v8::ContextScope::new(scope, caller);+ for (name, value) in values {+ 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 convert of [inferred, explicit]) {+ for (const value of [0, -0, 1 / 3, 16777217, 2 ** -149, 2 ** -150,+ -(2 ** -150), 3.4028234663852886e38, '1.25', true, null]) {+ assert(Object.is(convert(value), Math.fround(Number(value))), 'rounding: ' + value);+ }+ for (const value of [NaN, Infinity, -Infinity, 3.5e38, 1e300, undefined,+ Symbol('number'), 1n]) {+ let error;+ try { convert(value); } catch (caught) { error = caught; }+ assert(error instanceof CalleeTypeError && !(error instanceof TypeError),+ 'restricted float rejects nonfinite, overflow and invalid ToNumber in callee realm');+ }+ let error;+ try { convert(); } catch (caught) { error = caught; }+ assert(error instanceof CalleeTypeError, 'required argument');+ let reads = 0;+ const value = { valueOf() { reads++; return 1 / 3; } };+ assert(convert(value) === Math.fround(1 / 3) && reads === 1, 'one numeric conversion');+ const sentinel = {};+ try { convert({ valueOf() { throw sentinel; } }); } catch (caught) { error = caught; }+ assert(error === sentinel, 'original ToNumber exception is preserved');+ }+ return true;+ })()+ "#).unwrap();+ let script = v8::Script::compile(scope, source, None).unwrap();+ assert!(+ script+ .run(scope)+ .expect("restricted float conversion matrix")+ .is_true()+ );+}