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): derive EnforceRange unsigned short conversion

ldm006866c9474a1
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rs
index b2a9f4344..05df405d7 100644
--- a/moli-webidl-derive/src/converter.rs
+++ b/moli-webidl-derive/src/converter.rs
@@ -20,6 +20,7 @@ pub(crate) enum ConverterKind {
EnforceRangeLong,
Short,
UnsignedShort,
+ EnforceRangeUnsignedShort,
ClampedUnsignedShort,
UnsignedLong,
EnforceRangeUnsignedLong,
@@ -58,6 +59,7 @@ impl ConverterKind {
"enforce_range_long" => Ok(Self::EnforceRangeLong),
"short" => Ok(Self::Short),
"unsigned_short" => Ok(Self::UnsignedShort),
+ "enforce_range_unsigned_short" => Ok(Self::EnforceRangeUnsignedShort),
"clamped_unsigned_short" => Ok(Self::ClampedUnsignedShort),
"unsigned_long" => Ok(Self::UnsignedLong),
"enforce_range_unsigned_long" => Ok(Self::EnforceRangeUnsignedLong),
@@ -94,6 +96,7 @@ impl ConverterKind {
Self::EnforceRangeLong => quote!(::moli_webidl::EnforceRangeLong),
Self::Short => quote!(::moli_webidl::Short),
Self::UnsignedShort => quote!(::moli_webidl::UnsignedShort),
+ Self::EnforceRangeUnsignedShort => quote!(::moli_webidl::EnforceRangeUnsignedShort),
Self::ClampedUnsignedShort => quote!(::moli_webidl::ClampedUnsignedShort),
Self::UnsignedLong => quote!(::moli_webidl::UnsignedLong),
Self::EnforceRangeUnsignedLong => {
@@ -140,6 +143,9 @@ impl ConverterKind {
Self::EnforceRangeLong => quote!(::moli_webidl::EnforceRangeLong(#expr)),
Self::Short => quote!(::moli_webidl::Short(#expr)),
Self::UnsignedShort => quote!(::moli_webidl::UnsignedShort(#expr)),
+ Self::EnforceRangeUnsignedShort => {
+ quote!(::moli_webidl::EnforceRangeUnsignedShort(#expr))
+ }
Self::ClampedUnsignedShort => {
quote!(::moli_webidl::ClampedUnsignedShort(#expr))
}
@@ -179,6 +185,7 @@ impl ConverterKind {
| Self::EnforceRangeLong
| Self::Short
| Self::UnsignedShort
+ | Self::EnforceRangeUnsignedShort
| Self::ClampedUnsignedShort
| Self::UnsignedLong
| Self::EnforceRangeUnsignedLong
diff --git a/moli-webidl/src/convert.rs b/moli-webidl/src/convert.rs
index 2e011d8ec..e36f93736 100644
--- a/moli-webidl/src/convert.rs
+++ b/moli-webidl/src/convert.rs
@@ -318,6 +318,26 @@ impl<'s> WebIdlConverter<'s> for UnsignedShort {
}
}
+impl<'s> WebIdlConverter<'s> for crate::EnforceRangeUnsignedShort {
+ 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, "[EnforceRange] unsigned short")?.trunc();
+ if !value.is_finite() || !(0.0..=f64::from(u16::MAX)).contains(&value) {
+ return Err(WebIdlError::cannot_convert(
+ context,
+ "[EnforceRange] unsigned short",
+ ));
+ }
+ Ok(Self(value as u16))
+ }
+}
+
impl<'s> WebIdlConverter<'s> for ClampedUnsignedShort {
type Options = ();
diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rs
index 193be8161..963f78660 100644
--- a/moli-webidl/src/lib.rs
+++ b/moli-webidl/src/lib.rs
@@ -100,6 +100,6 @@ pub use types::{
UnsignedLongLong, UnsignedShort, UsvString,
};
-pub use types::EnforceRangeLongLong;
/// Restricted WebIDL float represented as a finite binary32 value.
pub use types::Float;
+pub use types::{EnforceRangeLongLong, EnforceRangeUnsignedShort};
diff --git a/moli-webidl/src/types.rs b/moli-webidl/src/types.rs
index 5949f9e91..7ee194f97 100644
--- a/moli-webidl/src/types.rs
+++ b/moli-webidl/src/types.rs
@@ -293,6 +293,16 @@ impl From<UnsignedShort> for u16 {
}
}
+/// WebIDL `[EnforceRange] unsigned short`, truncating before the range check.
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub struct EnforceRangeUnsignedShort(pub u16);
+
+impl From<EnforceRangeUnsignedShort> for u16 {
+ fn from(value: EnforceRangeUnsignedShort) -> Self {
+ value.0
+ }
+}
+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ClampedUnsignedShort(pub u16);
diff --git a/moli-webidl/tests/enforce_range_unsigned_short.rs b/moli-webidl/tests/enforce_range_unsigned_short.rs
new file mode 100644
index 000000000..b69a60f2b
--- /dev/null
+++ b/moli-webidl/tests/enforce_range_unsigned_short.rs
@@ -0,0 +1,100 @@
+use moli_webidl::{WebIdlArgs, WebIdlDictionary};
+
+#[derive(WebIdlArgs)]
+#[webidl(prefix = "Test.enforceRangeUnsignedShort")]
+struct Args {
+ #[webidl(required, converter = "enforce_range_unsigned_short")]
+ value: u16,
+}
+
+#[derive(WebIdlDictionary)]
+#[webidl(prefix = "Options")]
+struct Options {
+ #[webidl(default = 7, converter = "enforce_range_unsigned_short")]
+ value: u16,
+}
+
+#[derive(WebIdlArgs)]
+#[webidl(prefix = "Test.dictionary")]
+struct DictionaryArgs {
+ #[webidl(dictionary)]
+ options: Options,
+}
+
+fn convert<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(Args { value }) = moli_webidl::parse_args(scope, &args) {
+ rv.set_uint32(u32::from(value));
+ }
+}
+
+fn dictionary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(DictionaryArgs { options }) = moli_webidl::parse_args(scope, &args) {
+ rv.set_uint32(u32::from(options.value));
+ }
+}
+
+#[test]
+fn enforce_range_unsigned_short_truncates_checks_bounds_and_preserves_callee_exceptions() {
+ 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 convert = v8::Function::new(scope, convert).unwrap();
+ let dictionary = v8::Function::new(scope, dictionary).unwrap();
+ let key = v8::String::new(scope, "TypeError").unwrap();
+ let error = callee.global(scope).get(scope, key.into()).unwrap();
+ [
+ ("convert", convert.into()),
+ ("dictionary", dictionary.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)
+ .create_data_property(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,expected] of [[0,0],[-0,0],[-0.9,0],[3.9,3],[65535,65535],[65535.9,65535],['100',100],[null,0],[true,1]]) {
+ assert(Object.is(convert(value),expected),'argument truncation');
+ assert(Object.is(dictionary({value}),expected),'dictionary truncation');
+ }
+ assert(dictionary()===7&&dictionary(null)===7&&dictionary({value:undefined})===7,'dictionary defaults');
+ for(const value of [-1,-1.1,65536,65536.1,NaN,Infinity,-Infinity,'65536',Symbol(),1n]) {
+ for(const body of [()=>convert(value),()=>dictionary({value})]) {
+ let error;try{body()}catch(e){error=e}
+ assert(error instanceof CalleeTypeError&&!(error instanceof TypeError),'callee range TypeError');
+ }
+ }
+ let reads=0,conversions=0;
+ assert(dictionary({get value(){reads++;return {[Symbol.toPrimitive](hint){assert(hint==='number','hint');conversions++;return 17.8}}}})===17,'converted value');
+ assert(reads===1&&conversions===1,'observable conversion once');
+ const sentinel={};
+ for(const body of [()=>convert({valueOf(){throw sentinel}}),()=>dictionary({get value(){throw sentinel}})]) {
+ let error;try{body()}catch(e){error=e}assert(error===sentinel,'exception identity');
+ }
+ return true;
+ })()
+ "#).unwrap();
+ let script = v8::Script::compile(scope, source, None).unwrap();
+ assert!(script.run(scope).unwrap().is_true());
+}