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 octet arguments with modulo-256 conversion

ldm046eb3fe8895b
diff --git a/moli-webidl-derive/src/converter.rs b/moli-webidl-derive/src/converter.rs
index 3498440c0..af76f0d90 100644
--- a/moli-webidl-derive/src/converter.rs
+++ b/moli-webidl-derive/src/converter.rs
@@ -13,6 +13,7 @@ pub(crate) enum ConverterKind {
BufferSource,
CallbackFunction,
CallbackInterface,
+ Octet,
Long,
LongLong,
EnforceRangeLong,
@@ -49,6 +50,7 @@ impl ConverterKind {
"buffer_source" => Ok(Self::BufferSource),
"callback_function" => Ok(Self::CallbackFunction),
"callback_interface" => Ok(Self::CallbackInterface),
+ "octet" => Ok(Self::Octet),
"long" => Ok(Self::Long),
"long_long" => Ok(Self::LongLong),
"enforce_range_long" => Ok(Self::EnforceRangeLong),
@@ -83,6 +85,7 @@ impl ConverterKind {
Self::BufferSource => quote!(::moli_webidl::BufferSource),
Self::CallbackFunction => quote!(::moli_webidl::WebIdlCallbackFunction),
Self::CallbackInterface => quote!(::moli_webidl::WebIdlCallbackInterface),
+ Self::Octet => quote!(::moli_webidl::Octet),
Self::Long => quote!(::moli_webidl::Long),
Self::LongLong => quote!(::moli_webidl::LongLong),
Self::EnforceRangeLong => quote!(::moli_webidl::EnforceRangeLong),
@@ -125,6 +128,7 @@ impl ConverterKind {
}
Self::BufferSource => quote!(::moli_webidl::BufferSource::from(#expr)),
Self::CallbackFunction | Self::CallbackInterface => quote!(#expr),
+ Self::Octet => quote!(::moli_webidl::Octet(#expr)),
Self::Long => quote!(::moli_webidl::Long(#expr)),
Self::LongLong => quote!(::moli_webidl::LongLong(#expr)),
Self::EnforceRangeLong => quote!(::moli_webidl::EnforceRangeLong(#expr)),
@@ -162,6 +166,7 @@ impl ConverterKind {
Self::CallbackFunction | Self::CallbackInterface => value,
Self::DomString
| Self::UsvString
+ | Self::Octet
| Self::Long
| Self::LongLong
| Self::EnforceRangeLong
@@ -366,6 +371,9 @@ fn infer_converter_kind(ty: &Type) -> Option<ConverterKind> {
// domain enums must opt into `converter = "enum"` so invalid-token errors
// get the correct WebIDL enum name.
let ty = option_inner_type(ty).unwrap_or(ty);
+ if is_type_ident(ty, "u8") {
+ return Some(ConverterKind::Octet);
+ }
if is_type_ident(ty, "u32") {
return Some(ConverterKind::UnsignedLong);
}
diff --git a/moli-webidl/src/convert.rs b/moli-webidl/src/convert.rs
index 0d94186df..2e011d8ec 100644
--- a/moli-webidl/src/convert.rs
+++ b/moli-webidl/src/convert.rs
@@ -233,6 +233,21 @@ impl<'s> WebIdlConverter<'s> for Long {
}
}
+impl<'s> WebIdlConverter<'s> for crate::Octet {
+ type Options = ();
+
+ fn convert(
+ scope: &mut v8::PinScope<'s, '_>,
+ value: v8::Local<'s, v8::Value>,
+ context: Context,
+ _options: &Self::Options,
+ ) -> Result<Self, WebIdlError> {
+ // Reducing modulo 2^32 first preserves the low eight bits, including
+ // negative values and integers too large for a saturating Rust cast.
+ number_value(scope, value, context, "octet").map(|value| Self(unsigned_long(value) as u8))
+ }
+}
+
impl<'s> WebIdlConverter<'s> for EnforceRangeLong {
type Options = ();
diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rs
index 2e03bee00..193be8161 100644
--- a/moli-webidl/src/lib.rs
+++ b/moli-webidl/src/lib.rs
@@ -95,9 +95,9 @@ pub use traits::{ParseOutcome, WebIdlArguments, WebIdlConverter, WebIdlDictionar
pub use types::{
Boolean, BufferSource, ByteString, ClampedUnsignedShort, Dictionary, DomString, DomString16,
Double, EnforceRangeLong, EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue,
- EventListenerOptions, InterfaceObject, InterfaceOptions, Long, LongLong, Record, Sequence,
- Short, StringOptions, UnrestrictedDouble, UnrestrictedFloat, UnsignedLong, UnsignedLongLong,
- UnsignedShort, UsvString,
+ EventListenerOptions, InterfaceObject, InterfaceOptions, Long, LongLong, Octet, Record,
+ Sequence, Short, StringOptions, UnrestrictedDouble, UnrestrictedFloat, UnsignedLong,
+ UnsignedLongLong, UnsignedShort, UsvString,
};
pub use types::EnforceRangeLongLong;
diff --git a/moli-webidl/src/types.rs b/moli-webidl/src/types.rs
index fd4a9af9e..5949f9e91 100644
--- a/moli-webidl/src/types.rs
+++ b/moli-webidl/src/types.rs
@@ -168,6 +168,10 @@ impl From<&[u8]> for BufferSource {
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Boolean(pub bool);
+/// WebIDL octet with the default modulo-256 integer conversion.
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub struct Octet(pub u8);
+
impl From<Boolean> for bool {
fn from(value: Boolean) -> Self {
value.0
diff --git a/moli-webidl/tests/octet.rs b/moli-webidl/tests/octet.rs
new file mode 100644
index 000000000..7f780b60c
--- /dev/null
+++ b/moli-webidl/tests/octet.rs
@@ -0,0 +1,122 @@
+use moli_webidl::{WebIdlArgs, WebIdlDictionary};
+
+#[derive(WebIdlArgs)]
+#[webidl(prefix = "Test.octets")]
+struct OctetArgs {
+ #[webidl(required)]
+ value: u8,
+ #[webidl(sequence, default = Vec::new())]
+ bytes: Vec<u8>,
+ #[webidl(converter = "octet", default = 7)]
+ explicit: u8,
+}
+
+// This dictionary is declared in IDL member order; argument order is separate.
+#[derive(WebIdlDictionary)]
+#[webidl(prefix = "Test.octets")]
+struct OctetDictionary {
+ #[webidl(sequence, default = Vec::new())]
+ bytes: Vec<u8>,
+ #[webidl(converter = "octet", default = 7)]
+ explicit: u8,
+ #[webidl(required)]
+ value: u8,
+}
+
+fn octets<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ if let Some(parsed) = moli_webidl::parse_args::<OctetArgs>(scope, &args) {
+ let values: Vec<v8::Local<v8::Value>> = std::iter::once(parsed.value)
+ .chain(parsed.bytes)
+ .chain(std::iter::once(parsed.explicit))
+ .map(|value| v8::Integer::new(scope, i32::from(value)).into())
+ .collect();
+ rv.set(v8::Array::new_with_elements(scope, &values).into());
+ }
+}
+
+fn dictionary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ match moli_webidl::convert::<moli_webidl::Dictionary<OctetDictionary>>(
+ scope,
+ args.get(0),
+ moli_webidl::Context::argument("Test.octets", 1),
+ ) {
+ Ok(parsed) => {
+ assert!(parsed.0.bytes.is_empty());
+ rv.set_int32(i32::from(parsed.0.value) + i32::from(parsed.0.explicit));
+ }
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+#[test]
+fn octet_conversion_wraps_and_preserves_sequence_order_and_callee_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 values = {
+ let scope = &mut v8::ContextScope::new(scope, callee);
+ let scalar = v8::Function::new(scope, octets).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();
+ [
+ ("octets", scalar.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).set(scope, key.into(), value),
+ Some(true)
+ );
+ }
+ let source = v8::String::new(scope, r#"(() => {
+ const assert = (ok, name) => {if (!ok) throw Error(name)};
+ for (const [value,expected] of [[0,0],[-0,0],[3.9,3],[-3.9,253],[255,255],[256,0],[257,1],[-257,255],[65535,255],[4294967297,1],[Number.MAX_SAFE_INTEGER,255],[NaN,0],[Infinity,0],[-Infinity,0],[undefined,0],[null,0],[true,1],['511',255]]) {
+ const result = octets(value,[value],value);
+ assert(result.join() === [expected,expected,value === undefined ? 7 : expected].join(), 'modulo-256 conversion and optional parameter default');
+ }
+ for (const value of [Symbol(),1n]) {
+ let error; try {octets(value)} catch(caught) {error=caught}
+ assert(error instanceof CalleeTypeError && !(error instanceof TypeError), 'callee numeric error');
+ }
+ const log=[], marker={};
+ const first={valueOf(){log.push('first'); return -1}}, last={valueOf(){log.push('last'); return 256}};
+ const iterable={get [Symbol.iterator](){log.push('iterator'); return function*(){log.push('next'); yield {valueOf(){log.push('item'); return 257}}; log.push('done')}}};
+ assert(octets(first,iterable,last).join()==='255,1,0' && log.join()==='first,iterator,next,item,done,last', 'sequence converts each item before next argument');
+ log.length=0; let error;
+ try {octets(0,[{valueOf(){throw marker}}],last)} catch(caught){error=caught}
+ assert(error===marker && log.length===0,'conversion exception stops later arguments');
+ log.length=0;
+ const input={get bytes(){log.push('bytes'); return []},get explicit(){log.push('explicit'); return -1},get value(){log.push('value'); return 257}};
+ assert(dictionary(input)===256 && log.join()==='bytes,explicit,value','dictionary order and octet conversion');
+ try {octets()} catch(caught){error=caught}
+ assert(error instanceof CalleeTypeError, 'required scalar');
+ assert(octets(1).join()==='1,7', 'optional sequence and octet default');
+ return true;
+ })()"#).unwrap();
+ let tc = std::pin::pin!(v8::TryCatch::new(scope));
+ let scope = &mut tc.init();
+ let result = v8::Script::compile(scope, source, None).unwrap().run(scope);
+ if let Some(exception) = scope.exception() {
+ panic!(
+ "octet conversion matrix: {}",
+ exception.to_rust_string_lossy(scope)
+ );
+ }
+ assert!(result.is_some_and(|result| result.is_true()));
+}