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 lossless DOMString conversion

ldm0e6e9a3309cd6
diff --git a/moli-webidl/src/convert.rs b/moli-webidl/src/convert.rs
index caec06c57..5cd61d15a 100644
--- a/moli-webidl/src/convert.rs
+++ b/moli-webidl/src/convert.rs
@@ -1,7 +1,7 @@
use crate::{
- Boolean, BufferSource, ByteString, ClampedUnsignedShort, Context, DomString, Double,
- EnforceRangeLong, EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue, Long,
- Record, Sequence, StringOptions, UnrestrictedDouble, UnsignedLong, UnsignedLongLong,
+ Boolean, BufferSource, ByteString, ClampedUnsignedShort, Context, DomString, DomString16,
+ Double, EnforceRangeLong, EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue,
+ Long, Record, Sequence, StringOptions, UnrestrictedDouble, UnsignedLong, UnsignedLongLong,
UnsignedShort, UsvString, WebIdlArguments, WebIdlConverter, WebIdlDictionary, WebIdlEnum,
WebIdlError, WebIdlErrorKind, dictionary_value, is_nullish, property_result,
symbol_property_result, throw_error, throw_type_error,
@@ -30,6 +30,28 @@ impl<'s> WebIdlConverter<'s> for DomString {
}
}
+impl<'s> WebIdlConverter<'s> for DomString16 {
+ type Options = StringOptions;
+
+ fn convert(
+ scope: &mut v8::PinScope<'s, '_>,
+ value: v8::Local<'s, v8::Value>,
+ context: Context,
+ options: &Self::Options,
+ ) -> Result<Self, WebIdlError> {
+ if value.is_null() && options.treat_null_as_empty_string {
+ return Ok(Self(Vec::new()));
+ }
+ if value.is_symbol() {
+ return Err(WebIdlError::new(
+ context,
+ WebIdlErrorKind::CannotConvert("DOMString"),
+ ));
+ }
+ string_value_utf16(scope, value, context, "DOMString").map(Self)
+ }
+}
+
// String conversion first captures V8's UTF-16 result so `DOMString`,
// `USVString`, and `ByteString` can apply their different WebIDL post-processing
// rules. This keeps getter/toString side effects in one V8-facing helper while
diff --git a/moli-webidl/src/lib.rs b/moli-webidl/src/lib.rs
index dcf51022d..2f0251120 100644
--- a/moli-webidl/src/lib.rs
+++ b/moli-webidl/src/lib.rs
@@ -62,10 +62,10 @@ pub use moli_webidl_callback::{
pub use moli_webidl_derive::{WebIdlArgs, WebIdlDictionary, WebIdlEnum};
pub use traits::{ParseOutcome, WebIdlArguments, WebIdlConverter, WebIdlDictionary, WebIdlEnum};
pub use types::{
- Boolean, BufferSource, ByteString, ClampedUnsignedShort, DomString, Double, EnforceRangeLong,
- EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue, EventListenerOptions, Long,
- Record, Sequence, StringOptions, UnrestrictedDouble, UnsignedLong, UnsignedLongLong,
- UnsignedShort, UsvString,
+ Boolean, BufferSource, ByteString, ClampedUnsignedShort, DomString, DomString16, Double,
+ EnforceRangeLong, EnforceRangeUnsignedLong, EnforceRangeUnsignedLongLong, EnumValue,
+ EventListenerOptions, Long, Record, Sequence, StringOptions, UnrestrictedDouble, 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 08c784406..dd23eb8c2 100644
--- a/moli-webidl/src/types.rs
+++ b/moli-webidl/src/types.rs
@@ -12,6 +12,11 @@ pub struct EnumValue<T>(pub T);
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DomString(pub String);
+/// Lossless WebIDL DOMString storage for APIs whose identity depends on the
+/// original UTF-16 units, including unpaired surrogates.
+#[derive(Debug, Clone, Eq, PartialEq)]
+pub struct DomString16(pub Vec<u16>);
+
impl From<DomString> for String {
fn from(value: DomString) -> Self {
value.0
diff --git a/moli-webidl/tests/dom_string16.rs b/moli-webidl/tests/dom_string16.rs
new file mode 100644
index 000000000..2efa4f907
--- /dev/null
+++ b/moli-webidl/tests/dom_string16.rs
@@ -0,0 +1,110 @@
+use moli_webidl::{Context, DomString, DomString16, StringOptions, UsvString};
+
+fn lossless<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ let options = StringOptions {
+ treat_null_as_empty_string: args.get(1).is_true(),
+ };
+ match moli_webidl::convert_with_options::<DomString16>(
+ scope,
+ args.get(0),
+ Context::argument("lossless", 1),
+ &options,
+ ) {
+ Ok(value) => rv.set(
+ v8::String::new_from_two_byte(scope, &value.0, v8::NewStringType::Normal)
+ .unwrap()
+ .into(),
+ ),
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+fn utf8_boundary<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s>,
+) {
+ let context = Context::argument("utf8Boundary", 1);
+ let value = if args.get(1).is_true() {
+ moli_webidl::convert::<UsvString>(scope, args.get(0), context).map(|value| value.0)
+ } else {
+ moli_webidl::convert::<DomString>(scope, args.get(0), context).map(|value| value.0)
+ };
+ match value {
+ Ok(value) => rv.set(v8::String::new(scope, &value).unwrap().into()),
+ Err(error) => moli_webidl::throw_error(scope, &error),
+ }
+}
+
+#[test]
+fn dom_string16_preserves_code_units_and_webidl_conversion_semantics() {
+ 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 (lossless_function, utf8_function, type_error) = {
+ let scope = &mut v8::ContextScope::new(scope, callee);
+ let lossless_function = v8::Function::new(scope, lossless).unwrap();
+ let utf8_function = v8::Function::new(scope, utf8_boundary).unwrap();
+ let key = v8::String::new(scope, "TypeError").unwrap();
+ let type_error = callee.global(scope).get(scope, key.into()).unwrap();
+ (lossless_function, utf8_function, type_error)
+ };
+ let caller = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, caller);
+ let global = caller.global(scope);
+ for (name, value) in [
+ ("lossless", lossless_function.into()),
+ ("utf8Boundary", utf8_function.into()),
+ ("CalleeTypeError", type_error),
+ ] {
+ let key = v8::String::new(scope, name).unwrap();
+ assert_eq!(
+ global.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); };
+ const thrown = callback => { try { callback(); } catch (error) { return error; } throw Error('expected exception'); };
+ for (const value of ['', 'ascii', '\0a\0', '\ud800', '\udfff', '\ud800x\udfff', '\ud83d\ude00', '\ud800\ud800\udfff\udfff']) {
+ const result = lossless(value);
+ assert(result === value && result.length === value.length, 'exact UTF-16 units');
+ for (let i = 0; i < value.length; i++) assert(result.charCodeAt(i) === value.charCodeAt(i), 'unit ' + i);
+ }
+ for (const value of [null, undefined, true, false, 0, -0, NaN, Infinity, 123n]) {
+ assert(lossless(value) === String(value), 'ToString scalar');
+ }
+ assert(lossless(null, true) === '' && lossless(undefined, true) === 'undefined', 'null-only empty-string option');
+ let conversions = 0;
+ const value = {toString() { conversions++; return '\ud800\0\udfff'; }, valueOf() { throw Error('wrong hint'); }};
+ assert(lossless(value) === '\ud800\0\udfff' && conversions === 1, 'single ToString with string hint');
+ const order = [];
+ const exotic = {[Symbol.toPrimitive](hint) { order.push(hint); return '\udfff'; }};
+ assert(lossless(exotic) === '\udfff' && order.join() === 'string', 'ToPrimitive string hint');
+ const sentinel = {};
+ assert(thrown(() => lossless({toString() { throw sentinel; }})) === sentinel, 'exception identity');
+ for (const value of [Symbol(), Object(Symbol()), {toString() { return Symbol(); }}]) {
+ const error = thrown(() => lossless(value));
+ assert(error instanceof CalleeTypeError && !(error instanceof TypeError), 'callee realm conversion error');
+ }
+ assert(utf8Boundary('\ud800x\udfff') === '\ufffdx\ufffd', 'existing Rust String boundary stays lossy');
+ assert(utf8Boundary('\ud800x\udfff', true) === '\ufffdx\ufffd', 'USVString applies scalar replacement');
+ assert(utf8Boundary('\ud83d\ude00') === '\ud83d\ude00', 'UTF-8 boundary preserves valid pairs');
+ return true;
+ })()
+ "#).unwrap();
+ let script = v8::Script::compile(scope, source, None).unwrap();
+ assert!(
+ script
+ .run(scope)
+ .expect("lossless DOMString checks")
+ .is_true()
+ );
+}