From a806bfd713f2a3887a889a39fdfbad1237347326 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 13 Oct 2026 03:36:00 +0800 Subject: [PATCH] fix(canvas): preserve native ImageData construction and clone graphs --- .../tests/fixtures/runtime/image_data.html | 2 +- moli-renderer-v8/src/context_bootstrap.rs | 2 +- .../canvas/context2d/pixel_manipulation.rs | 4 +- .../context_bootstrap/canvas/image_bitmap.rs | 8 +- .../src/context_bootstrap/image_data.rs | 441 +++++++++--------- .../context_bootstrap/image_data/pixels.rs | 170 +++++++ .../indexed_db/core/clone.rs | 8 +- .../src/script_vm/tests/canvas_webgl.rs | 2 +- .../src/script_vm/tests/image_data_native.js | 194 ++++++++ .../src/script_vm/tests/image_data_native.rs | 172 +++++++ moli-renderer-v8/src/script_vm/tests/mod.rs | 1 + moli-renderer-v8/src/structured_clone.rs | 54 ++- 12 files changed, 815 insertions(+), 243 deletions(-) create mode 100644 moli-renderer-v8/src/context_bootstrap/image_data/pixels.rs create mode 100644 moli-renderer-v8/src/script_vm/tests/image_data_native.js create mode 100644 moli-renderer-v8/src/script_vm/tests/image_data_native.rs diff --git a/moli-core/tests/fixtures/runtime/image_data.html b/moli-core/tests/fixtures/runtime/image_data.html index 4143cf7ef62..b2946c40f5b 100644 --- a/moli-core/tests/fixtures/runtime/image_data.html +++ b/moli-core/tests/fixtures/runtime/image_data.html @@ -26,7 +26,7 @@ new ImageData(2147483648, 2147483648); body.setAttribute("data-too-large", "no-error"); } catch (error) { - body.setAttribute("data-too-large", String(error).includes("IndexSizeError")); + body.setAttribute("data-too-large", error instanceof RangeError); } diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs index fc84e9dc417..22df9173f57 100644 --- a/moli-renderer-v8/src/context_bootstrap.rs +++ b/moli-renderer-v8/src/context_bootstrap.rs @@ -420,7 +420,7 @@ pub(crate) use self::geometry_clone::{ use self::geometry_runtime::{build_dom_point_object, optional_dom_point_init_arg}; pub(crate) use self::image_data::{ ImageDataClonePayload, build_image_data_object_from_clone_payload, - image_data_clone_payload_from_object, + build_image_data_object_from_legacy_bytes, image_data_clone_payload_from_object, }; pub(crate) use self::indexed_db::{ IndexedDbTaskId, discard_indexed_db_task_by_id, flush_indexed_db_task_by_id, diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/context2d/pixel_manipulation.rs b/moli-renderer-v8/src/context_bootstrap/canvas/context2d/pixel_manipulation.rs index b8e3fe2fedf..8c5b3087fc9 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/context2d/pixel_manipulation.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/context2d/pixel_manipulation.rs @@ -7,7 +7,7 @@ use crate::context_bootstrap::{ }, image_data::{ build_image_data_object, build_image_data_object_with_bytes, image_data_buffer_is_detached, - image_data_bytes_from_object, image_data_dimensions_from_object, + image_data_dimensions_from_object, image_data_rgba8_bytes_from_object, }, throw_dom_exception_value, }; @@ -224,7 +224,7 @@ fn put_image_data<'s>( let Some((width, height)) = image_data_dimensions_from_object(scope, source) else { return; }; - let Some(bytes) = image_data_bytes_from_object(scope, source) else { + let Some(bytes) = image_data_rgba8_bytes_from_object(scope, source) else { return; }; let dirty = dirty.unwrap_or_else(|| ImageDataRect::new(0, 0, width as i32, height as i32)); diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs b/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs index b875a0d2de5..6619a7fd84b 100644 --- a/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs +++ b/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs @@ -269,7 +269,13 @@ fn snapshot_bitmap_source<'s>( let data = crate::context_bootstrap::image_data::image_data_clone_payload_from_object( scope, source, )?; - (data.bytes, data.width, data.height) + ( + crate::context_bootstrap::image_data::image_data_rgba8_bytes_from_object( + scope, source, + )?, + data.width, + data.height, + ) } else { image_source_pixels(scope, source)? }; diff --git a/moli-renderer-v8/src/context_bootstrap/image_data.rs b/moli-renderer-v8/src/context_bootstrap/image_data.rs index ab76128d371..3ec811b5384 100644 --- a/moli-renderer-v8/src/context_bootstrap/image_data.rs +++ b/moli-renderer-v8/src/context_bootstrap/image_data.rs @@ -1,11 +1,11 @@ use super::*; use crate::util::{callback_data_index_value, get_private_value}; -use crate::web_api_interfaces; -use crate::webidl; -use moli_canvas::byte_len as rgba8_byte_len; -use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject}; +use crate::{web_api_interfaces, webidl}; +use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject, web_api_object_target}; use std::str::FromStr; -use webidl::{SnapshotPrototype, WebIdlDictionarySerialize}; + +mod pixels; +use pixels::{ImageDataArray, ImageDataPixelFormat, new_pixel_array, rgba8_bytes}; const IMAGE_DATA_WIDTH_SLOT: &str = "__moliImageDataWidth"; const IMAGE_DATA_HEIGHT_SLOT: &str = "__moliImageDataHeight"; @@ -20,58 +20,37 @@ struct ImageDataObjectDeclaration<'s> { width: u32, #[webapi(slot = IMAGE_DATA_HEIGHT_SLOT)] height: u32, + #[webapi(slot = IMAGE_DATA_COLOR_SPACE_SLOT)] + color_space: String, #[webapi(slot = IMAGE_DATA_PIXEL_FORMAT_SLOT)] pixel_format: &'static str, #[webapi(slot = IMAGE_DATA_DATA_SLOT)] - data: v8::Local<'s, v8::Uint8ClampedArray>, + data: v8::Local<'s, v8::ArrayBufferView>, } #[derive(WebApiFunctionTemplate)] -#[webapi(interface = web_api_interfaces::ImageData, receiver)] +#[webapi(interface = web_api_interfaces::ImageData, enumerable, receiver)] struct ImageDataPrototypeDeclaration { - #[webapi( - accessor_property, - getter = image_data_getter_callback, - data = callback_data_index_value(scope, 0), - enumerable - )] + #[webapi(accessor_property, getter = image_data_getter_callback, data = callback_data_index_value(scope, 0))] width: (), - #[webapi( - accessor_property, - getter = image_data_getter_callback, - data = callback_data_index_value(scope, 1), - enumerable - )] + #[webapi(accessor_property, getter = image_data_getter_callback, data = callback_data_index_value(scope, 1))] height: (), - #[webapi( - accessor_property, - getter = image_data_getter_callback, - data = callback_data_index_value(scope, 2), - enumerable - )] + #[webapi(accessor_property, getter = image_data_getter_callback, data = callback_data_index_value(scope, 2))] data: (), - #[webapi( - accessor_property, - getter = image_data_getter_callback, - data = callback_data_index_value(scope, 3), - enumerable - )] + #[webapi(accessor_property, getter = image_data_getter_callback, data = callback_data_index_value(scope, 3))] color_space: (), - #[webapi( - accessor_property, - getter = image_data_getter_callback, - data = callback_data_index_value(scope, 4), - enumerable - )] + #[webapi(accessor_property, getter = image_data_getter_callback, data = callback_data_index_value(scope, 4))] pixel_format: (), } -#[derive(Clone, Debug)] -pub(crate) struct ImageDataClonePayload { +/// Retain the native view while V8 sub-serializes it in the enclosing object graph. +/// Copying bytes here would lose view identity, offsets and shared buffer references. +pub(crate) struct ImageDataClonePayload<'s> { pub(crate) width: u32, pub(crate) height: u32, pub(crate) color_space: String, - pub(crate) bytes: Vec, + pub(crate) pixel_format: ImageDataPixelFormat, + pub(crate) data: v8::Local<'s, v8::ArrayBufferView>, } #[derive( @@ -89,26 +68,42 @@ impl ImageDataColorSpace { fn parse(value: &str) -> Option { Self::from_str(value).ok() } - fn label(self) -> &'static str { self.into() } } -#[derive(webidl::WebIdlDictionary)] -#[webidl(prefix = "ImageDataSettings", snapshot)] +#[derive(Default, webidl::WebIdlDictionary)] +#[webidl(prefix = "ImageDataSettings")] struct ImageDataSettingsMembers { - #[webidl(converter = "enum", default = ImageDataColorSpace::Srgb)] - #[webidl(snapshot_slot = IMAGE_DATA_COLOR_SPACE_SLOT, snapshot_with = color_space_snapshot)] - color_space: ImageDataColorSpace, + #[webidl(converter = "enum")] + color_space: Option, + #[webidl(converter = "enum", default = ImageDataPixelFormat::RgbaUnorm8)] + pixel_format: ImageDataPixelFormat, } -fn color_space_snapshot<'s>( - scope: &mut v8::PinScope<'s, '_>, - value: &ImageDataColorSpace, - _: SnapshotPrototype, -) -> Option> { - v8_string(scope, value.label()).map(Into::into) +#[derive(webidl::WebIdlArgs)] +#[webidl(prefix = "ImageData")] +struct ImageDataSizeArgs { + #[webidl(required, converter = "unsigned_long")] + width: u32, + #[webidl(required, converter = "unsigned_long")] + height: u32, + #[webidl(dictionary)] + settings: ImageDataSettingsMembers, +} + +#[derive(webidl::WebIdlArgs)] +#[webidl(prefix = "ImageData")] +struct ImageDataSourceArgs<'s> { + #[webidl(required, converter = "raw")] + data: ImageDataArray<'s>, + #[webidl(required, converter = "unsigned_long")] + width: u32, + #[webidl(converter = "unsigned_long")] + height: Option, + #[webidl(dictionary)] + settings: ImageDataSettingsMembers, } pub(super) fn image_data_constructor_callback<'s>( @@ -123,16 +118,10 @@ pub(super) fn image_data_constructor_callback<'s>( ); return; } - - let Some((data, width, height, color_space)) = image_data_constructor_parts(scope, &args) - else { + let Some(parts) = image_data_constructor_parts(scope, &args) else { return; }; - - color_space - .initialize_snapshot(scope, args.this()) - .expect("ImageData settings"); - ImageDataObjectDeclaration::new(width, height, "rgba-unorm8", data) + declaration(parts) .initialize(scope, args.this()) .expect("ImageData declaration should initialize object"); rv.set(args.this().into()); @@ -151,98 +140,111 @@ fn image_data_getter_callback<'s>( args: v8::FunctionCallbackArguments<'s>, mut rv: v8::ReturnValue<'_, v8::Value>, ) { + let receiver = web_api_object_target(scope, args.this()).expect("validated ImageData receiver"); let Some(slot) = callback_data_item( scope, &args, IMAGE_DATA_ATTRIBUTE_SLOTS, "ImageData attribute slots", ) else { - rv.set_undefined(); return; }; - let value = - get_private_value(scope, args.this(), slot).unwrap_or_else(|| v8::undefined(scope).into()); - rv.set(value); + if let Some(value) = get_private_value(scope, receiver, slot) { + rv.set(value); + } } fn image_data_constructor_parts<'s>( scope: &mut v8::PinScope<'s, '_>, args: &v8::FunctionCallbackArguments<'s>, -) -> Option<( - v8::Local<'s, v8::Uint8ClampedArray>, - u32, - u32, - ImageDataSettingsMembers, -)> { - if let Ok(data) = v8::Local::::try_from(args.get(0)) { - let width = image_data_dimension_arg(scope, args.get(1))?; - let height = if args.length() > 2 && !args.get(2).is_undefined() { - image_data_dimension_arg(scope, args.get(2))? - } else { - let pixels = data.byte_length() / 4; - if width == 0 || pixels % width as usize != 0 { - throw_image_data_index_size_error(scope); +) -> Option> { + // Four arguments leave only the data overload. With two or three, the + // distinguishing argument selects the native typed-array union or unsigned long. + let (width, height, settings, data) = + if args.length() >= 4 || ImageDataArray::matches(args.get(0)) { + let parsed = webidl::parse_args::(scope, args)?; + let bytes_per_pixel = parsed.settings.pixel_format.bytes_per_pixel(); + let length = parsed.data.view.byte_length(); + if length == 0 || !length.is_multiple_of(bytes_per_pixel) { + throw_invalid_image_data(scope); return None; } - (pixels / width as usize) as u32 + let pixels = length / bytes_per_pixel; + if parsed.width == 0 || !pixels.is_multiple_of(parsed.width as usize) { + webidl::throw_index_size_error(scope); + return None; + } + let Ok(height) = u32::try_from(pixels / parsed.width as usize) else { + webidl::throw_index_size_error(scope); + return None; + }; + if parsed.height.is_some_and(|given| given != height) { + webidl::throw_index_size_error(scope); + return None; + } + if parsed.data.format != parsed.settings.pixel_format { + throw_invalid_image_data(scope); + return None; + } + (parsed.width, height, parsed.settings, parsed.data.view) + } else { + let parsed = webidl::parse_args::(scope, args)?; + if parsed.width == 0 || parsed.height == 0 { + webidl::throw_index_size_error(scope); + return None; + } + let data = allocate_pixel_array( + scope, + parsed.width, + parsed.height, + parsed.settings.pixel_format, + )?; + (parsed.width, parsed.height, parsed.settings, data) }; - let expected_len = image_data_expected_len(scope, width, height)?; - if data.byte_length() != expected_len { - throw_image_data_index_size_error(scope); - return None; - } - let color_space = image_data_color_space_arg(scope, args.get(3))?; - return Some((data, width, height, color_space)); - } - - let width = image_data_dimension_arg(scope, args.get(0))?; - let height = image_data_dimension_arg(scope, args.get(1))?; - let color_space = image_data_color_space_arg(scope, args.get(2))?; - let len = image_data_expected_len(scope, width, height)?; - let data = new_uint8_clamped_array_from_bytes(scope, vec![0; len])?; - Some((data, width, height, color_space)) + Some(ImageDataClonePayload { + width, + height, + data, + color_space: settings + .color_space + .unwrap_or(ImageDataColorSpace::Srgb) + .label() + .to_owned(), + pixel_format: settings.pixel_format, + }) } -fn image_data_dimension_arg( - scope: &mut v8::PinScope<'_, '_>, - value: v8::Local<'_, v8::Value>, -) -> Option { - let number = value.number_value(scope)?; - if !number.is_finite() || number <= 0.0 || number > i32::MAX as f64 { - throw_image_data_index_size_error(scope); +fn allocate_pixel_array<'s>( + scope: &mut v8::PinScope<'s, '_>, + width: u32, + height: u32, + format: ImageDataPixelFormat, +) -> Option> { + let Some(length) = format.byte_len(width, height) else { + throw_image_data_allocation_error(scope); + return None; + }; + let mut bytes = Vec::new(); + if bytes.try_reserve_exact(length).is_err() { + throw_image_data_allocation_error(scope); return None; } - Some(number as u32) + bytes.resize(length, 0); + new_pixel_array(scope, bytes, format) } -fn image_data_color_space_arg<'s>( - scope: &mut v8::PinScope<'s, '_>, - value: v8::Local<'s, v8::Value>, -) -> Option { - if webidl::is_nullish(value) { - return Some(ImageDataSettingsMembers { - color_space: ImageDataColorSpace::Srgb, - }); - } - let object = match v8::Local::::try_from(value) { - Ok(object) => object, - Err(_) => { - throw_type_error( - scope, - "Failed to construct 'ImageData': settings must be an object", - ); - return None; - } - }; - let color_space = - match webidl::parse_dictionary_object::(scope, object) { - Ok(value) => value, - Err(error) => { - throw_type_error(scope, &error.to_string()); - return None; - } - }; - Some(color_space) +fn throw_image_data_allocation_error(scope: &mut v8::PinScope<'_, '_>) { + let message = crate::util::v8str(scope, "ImageData pixel storage could not be allocated."); + let error = v8::Exception::range_error(scope, message); + scope.throw_exception(error); +} + +fn throw_invalid_image_data(scope: &mut v8::PinScope<'_, '_>) { + throw_dom_exception_value( + scope, + "The pixel array does not match the ImageData format.", + "InvalidStateError", + ); } const IMAGE_DATA_ATTRIBUTE_SLOTS: &[&str] = &[ @@ -253,17 +255,6 @@ const IMAGE_DATA_ATTRIBUTE_SLOTS: &[&str] = &[ IMAGE_DATA_PIXEL_FORMAT_SLOT, ]; -fn image_data_expected_len( - scope: &mut v8::PinScope<'_, '_>, - width: u32, - height: u32, -) -> Option { - rgba8_byte_len(width, height).or_else(|| { - throw_image_data_index_size_error(scope); - None - }) -} - pub(in crate::context_bootstrap) fn new_uint8_clamped_array_from_bytes<'s>( scope: &mut v8::PinScope<'s, '_>, bytes: Vec, @@ -274,24 +265,33 @@ pub(in crate::context_bootstrap) fn new_uint8_clamped_array_from_bytes<'s>( v8::Uint8ClampedArray::new(scope, buffer, 0, len) } +fn declaration(parts: ImageDataClonePayload<'_>) -> ImageDataObjectDeclaration<'_> { + ImageDataObjectDeclaration::new( + parts.width, + parts.height, + parts.color_space, + parts.pixel_format.label(), + parts.data, + ) +} + pub(super) fn build_image_data_object<'s>( scope: &mut v8::PinScope<'s, '_>, width: u32, height: u32, ) -> Option> { - let ctor = v8::Local::::try_from(super::shared::global_constructor_object( + let format = ImageDataPixelFormat::RgbaUnorm8; + let data = allocate_pixel_array(scope, width, height, format)?; + build_image_data_object_from_clone_payload( scope, - "ImageData", - )?) - .ok()?; - let object = ctor.new_instance( - scope, - &[ - v8::Integer::new(scope, width as i32).into(), - v8::Integer::new(scope, height as i32).into(), - ], - )?; - Some(object) + ImageDataClonePayload { + width, + height, + color_space: "srgb".to_owned(), + pixel_format: format, + data, + }, + ) } pub(super) fn build_image_data_object_with_bytes<'s>( @@ -301,63 +301,78 @@ pub(super) fn build_image_data_object_with_bytes<'s>( bytes: Vec, ) -> Option> { let data = new_uint8_clamped_array_from_bytes(scope, bytes)?; - let ctor = v8::Local::::try_from(super::shared::global_constructor_object( + build_image_data_object_from_clone_payload( scope, - "ImageData", - )?) - .ok()?; - let object = ctor.new_instance( - scope, - &[ - data.into(), - v8::Integer::new(scope, width as i32).into(), - v8::Integer::new(scope, height as i32).into(), - ], - )?; - Some(object) + ImageDataClonePayload { + width, + height, + color_space: "srgb".to_owned(), + pixel_format: ImageDataPixelFormat::RgbaUnorm8, + data: data.into(), + }, + ) } pub(crate) fn build_image_data_object_from_clone_payload<'s>( scope: &mut v8::PinScope<'s, '_>, - payload: ImageDataClonePayload, + payload: ImageDataClonePayload<'s>, ) -> Option> { - let data = new_uint8_clamped_array_from_bytes(scope, payload.bytes)?; - let settings = ImageDataSettingsMembers { - color_space: ImageDataColorSpace::parse(&payload.color_space)?, + if payload.width == 0 + || payload.height == 0 + || payload + .pixel_format + .byte_len(payload.width, payload.height)? + != payload.data.byte_length() + || ImageDataArray::format(payload.data.into())? != payload.pixel_format + || ImageDataColorSpace::parse(&payload.color_space).is_none() + { + return None; } - .to_js_snapshot(scope, SnapshotPrototype::Ordinary)?; - let ctor = v8::Local::::try_from(super::shared::global_constructor_object( + declaration(payload).bind(scope).ok() +} + +pub(crate) fn build_image_data_object_from_legacy_bytes<'s>( + scope: &mut v8::PinScope<'s, '_>, + width: u32, + height: u32, + color_space: String, + bytes: Vec, +) -> Option> { + let data = new_uint8_clamped_array_from_bytes(scope, bytes)?; + build_image_data_object_from_clone_payload( scope, - "ImageData", - )?) - .ok()?; - ctor.new_instance( - scope, - &[ - data.into(), - v8::Integer::new(scope, payload.width as i32).into(), - v8::Integer::new(scope, payload.height as i32).into(), - settings.into(), - ], + ImageDataClonePayload { + width, + height, + color_space, + pixel_format: ImageDataPixelFormat::RgbaUnorm8, + data: data.into(), + }, ) } pub(crate) fn image_data_clone_payload_from_object<'s>( scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, -) -> Option { +) -> Option> { + let object = web_api_object_target(scope, object)?; let (width, height) = image_data_dimensions_from_object(scope, object)?; - let bytes = image_data_bytes_from_object(scope, object)?; - if bytes.len() != rgba8_byte_len(width, height)? { + let data = image_data_view(scope, object)?; + if data.buffer(scope)?.was_detached() { return None; } let color_space = image_data_string_from_object_slot(scope, object, IMAGE_DATA_COLOR_SPACE_SLOT)?; + let pixel_format = + image_data_string_from_object_slot(scope, object, IMAGE_DATA_PIXEL_FORMAT_SLOT)? + .parse() + .ok()?; Some(ImageDataClonePayload { width, height, color_space, - bytes, + pixel_format, + data, }) } @@ -376,11 +391,7 @@ fn image_data_dimension_from_object_slot<'s>( object: v8::Local<'s, v8::Object>, slot: &'static str, ) -> Option { - let value = get_private_value(scope, object, slot)?.number_value(scope)?; - if !value.is_finite() || value <= 0.0 || value > u32::MAX as f64 { - return None; - } - Some(value as u32) + get_private_value(scope, object, slot)?.uint32_value(scope) } fn image_data_string_from_object_slot<'s>( @@ -388,40 +399,40 @@ fn image_data_string_from_object_slot<'s>( object: v8::Local<'s, v8::Object>, slot: &'static str, ) -> Option { - get_private_value(scope, object, slot).and_then(|value| { - value - .to_string(scope) - .map(|value| value.to_rust_string_lossy(scope)) - }) + get_private_value(scope, object, slot)? + .to_string(scope) + .map(|value| value.to_rust_string_lossy(scope)) +} + +fn image_data_view<'s>( + scope: &mut v8::PinScope<'s, '_>, + object: v8::Local<'s, v8::Object>, +) -> Option> { + v8::Local::try_from(get_private_value(scope, object, IMAGE_DATA_DATA_SLOT)?).ok() } pub(super) fn image_data_buffer_is_detached<'s>( scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, ) -> Option { - let view = get_private_value(scope, object, IMAGE_DATA_DATA_SLOT) - .and_then(|value| v8::Local::::try_from(value).ok())?; - Some(view.buffer(scope)?.was_detached()) + Some( + image_data_view(scope, object)? + .buffer(scope)? + .was_detached(), + ) } -pub(super) fn image_data_bytes_from_object<'s>( +/// Convert only at the boundary to the existing RGBA8 canvas/bitmap backend. +/// Native ImageData storage and structured clone keep the original pixel format. +pub(super) fn image_data_rgba8_bytes_from_object<'s>( scope: &mut v8::PinScope<'s, '_>, object: v8::Local<'s, v8::Object>, ) -> Option> { - let view = get_private_value(scope, object, IMAGE_DATA_DATA_SLOT) - .and_then(|value| v8::Local::::try_from(value).ok())?; - let mut bytes = vec![0; view.byte_length()]; - let written = view.copy_contents(&mut bytes); - bytes.truncate(written); - Some(bytes) -} - -fn throw_image_data_index_size_error(scope: &mut v8::PinScope<'_, '_>) { - crate::context_bootstrap::throw_dom_exception_value( - scope, - "The source width is zero or not a finite integer.", - "IndexSizeError", - ); + let view = image_data_view(scope, object)?; + if view.buffer(scope)?.was_detached() { + return None; + } + rgba8_bytes(view) } #[cfg(test)] diff --git a/moli-renderer-v8/src/context_bootstrap/image_data/pixels.rs b/moli-renderer-v8/src/context_bootstrap/image_data/pixels.rs new file mode 100644 index 00000000000..68ce3e9de15 --- /dev/null +++ b/moli-renderer-v8/src/context_bootstrap/image_data/pixels.rs @@ -0,0 +1,170 @@ +use crate::webidl; +use std::str::FromStr; + +#[derive( + Clone, + Copy, + Debug, + Default, + Eq, + PartialEq, + strum::EnumString, + strum::IntoStaticStr, + webidl::WebIdlEnum, +)] +#[webidl(name = "ImageDataPixelFormat", parse_with = Self::parse)] +pub(crate) enum ImageDataPixelFormat { + #[default] + #[strum(serialize = "rgba-unorm8")] + RgbaUnorm8, + #[strum(serialize = "rgba-float16")] + RgbaFloat16, +} + +impl ImageDataPixelFormat { + fn parse(value: &str) -> Option { + Self::from_str(value).ok() + } + + pub(crate) fn label(self) -> &'static str { + self.into() + } + + pub(super) fn bytes_per_pixel(self) -> usize { + match self { + Self::RgbaUnorm8 => 4, + Self::RgbaFloat16 => 8, + } + } + + pub(super) fn byte_len(self, width: u32, height: u32) -> Option { + (width as usize) + .checked_mul(height as usize)? + .checked_mul(self.bytes_per_pixel()) + .filter(|length| *length <= moli_canvas::MAX_RGBA8_BYTE_LENGTH) + } +} + +/// The ImageDataArray union retains the view through later argument conversion. +pub(super) struct ImageDataArray<'s> { + pub(super) view: v8::Local<'s, v8::ArrayBufferView>, + pub(super) format: ImageDataPixelFormat, +} + +impl ImageDataArray<'_> { + pub(super) fn matches(value: v8::Local<'_, v8::Value>) -> bool { + Self::format(value).is_some() + } + + pub(super) fn format(value: v8::Local<'_, v8::Value>) -> Option { + if value.is_uint8_clamped_array() { + Some(ImageDataPixelFormat::RgbaUnorm8) + } else if value.is_float16_array() { + Some(ImageDataPixelFormat::RgbaFloat16) + } else { + None + } + } +} + +impl<'s> webidl::WebIdlConverter<'s> for ImageDataArray<'s> { + type Options = (); + + fn convert( + scope: &mut v8::PinScope<'s, '_>, + value: v8::Local<'s, v8::Value>, + context: webidl::Context, + _options: &(), + ) -> Result { + let invalid = || webidl::WebIdlError::cannot_convert(context, "ImageDataArray"); + let format = Self::format(value).ok_or_else(invalid)?; + let view = v8::Local::::try_from(value).map_err(|_| invalid())?; + let buffer = view.buffer(scope).ok_or_else(invalid)?; + // Use native buffer flags, which survive detachment. No author getters + // or Proxy traps participate in typed-array union conversion. + if buffer.is_shared_array_buffer() || buffer.is_resizable_by_user_javascript() { + return Err(webidl::WebIdlError::cannot_convert( + context, + "non-shared, non-resizable ImageDataArray", + )); + } + Ok(Self { view, format }) + } +} + +pub(super) fn new_pixel_array<'s>( + scope: &mut v8::PinScope<'s, '_>, + bytes: Vec, + format: ImageDataPixelFormat, +) -> Option> { + match format { + ImageDataPixelFormat::RgbaUnorm8 => { + super::new_uint8_clamped_array_from_bytes(scope, bytes).map(Into::into) + } + ImageDataPixelFormat::RgbaFloat16 => { + let length = bytes.len() / 2; + let backing = v8::ArrayBuffer::new_backing_store_from_vec(bytes).make_shared(); + let buffer = v8::ArrayBuffer::with_backing_store(scope, &backing); + v8::Float16Array::new(scope, buffer, 0, length).map(|view| { + // SAFETY: Float16Array inherits ArrayBufferView in V8. rusty_v8 + // exposes construction/downcasts but omits this mechanical upcast. + unsafe { v8::Local::::cast_unchecked(view) } + }) + } + } +} + +pub(super) fn rgba8_bytes(view: v8::Local<'_, v8::ArrayBufferView>) -> Option> { + let format = ImageDataArray::format(view.into())?; + let mut bytes = vec![0; view.byte_length()]; + let written = view.copy_contents(&mut bytes); + bytes.truncate(written); + Some(match format { + ImageDataPixelFormat::RgbaUnorm8 => bytes, + ImageDataPixelFormat::RgbaFloat16 => bytes + .chunks_exact(2) + .map(|pair| half_to_unorm8(u16::from_ne_bytes([pair[0], pair[1]]))) + .collect(), + }) +} + +fn half_to_unorm8(bits: u16) -> u8 { + let sign = u32::from(bits & 0x8000) << 16; + let exponent = u32::from((bits >> 10) & 0x1f); + let fraction = u32::from(bits & 0x3ff); + let value = match exponent { + 0 => (fraction as f32 * 2.0_f32.powi(-24)).copysign(f32::from_bits(sign)), + 31 => f32::from_bits(sign | 0x7f80_0000 | (fraction << 13)), + _ => f32::from_bits(sign | ((exponent + 112) << 23) | (fraction << 13)), + }; + // Casting also maps NaN to zero. Half precision expands exactly to f32. + (value * 255.0).round_ties_even().clamp(0.0, 255.0) as u8 +} + +#[cfg(test)] +mod tests { + use super::half_to_unorm8; + + #[test] + fn half_pixels_quantize_only_at_the_rgba8_backend_boundary() { + for (bits, expected) in [ + (0x0000, 0), + (0x8000, 0), + (0x0001, 0), + (0x03ff, 0), + (0xbc00, 0), + (0x3400, 64), + (0x3800, 128), + (0x3a00, 191), + (0x3c00, 255), + (0x4000, 255), + (0x7bff, 255), + (0x7c00, 255), + (0xfc00, 0), + (0x7e00, 0), + (0xfe00, 0), + ] { + assert_eq!(half_to_unorm8(bits), expected, "half bits {bits:04x}"); + } + } +} diff --git a/moli-renderer-v8/src/context_bootstrap/indexed_db/core/clone.rs b/moli-renderer-v8/src/context_bootstrap/indexed_db/core/clone.rs index 1dfd291797a..238c661357f 100644 --- a/moli-renderer-v8/src/context_bootstrap/indexed_db/core/clone.rs +++ b/moli-renderer-v8/src/context_bootstrap/indexed_db/core/clone.rs @@ -15,8 +15,8 @@ use crate::{ }, structured_clone::{ BlobClonePayload, HOST_OBJECT_TAG_BLOB, HOST_OBJECT_TAG_CRYPTO_KEY, - HOST_OBJECT_TAG_FILE_LIST, HOST_OBJECT_TAG_FILE_SYSTEM_HANDLE, HOST_OBJECT_TAG_IMAGE_DATA, - blob_clone_payload_from_object, read_crypto_key_payload, read_image_data_payload, + HOST_OBJECT_TAG_FILE_LIST, HOST_OBJECT_TAG_FILE_SYSTEM_HANDLE, HOST_OBJECT_TAG_IMAGE_DATA, HOST_OBJECT_TAG_IMAGE_DATA_GRAPH, + blob_clone_payload_from_object, read_crypto_key_payload, read_image_data_payload, read_image_data_graph_payload, write_crypto_key_payload, write_image_data_payload, }, }; @@ -199,8 +199,7 @@ impl v8::ValueSerializerImpl for IndexedDbStructuredCloneSerializer { } Some("ImageData") => { if let Some(payload) = image_data_clone_payload_from_object(scope, object) { - write_image_data_payload(serializer, payload); - return Some(true); + return write_image_data_payload(scope, serializer, payload); } } Some("FileSystemFileHandle" | "FileSystemDirectoryHandle") @@ -435,6 +434,7 @@ impl v8::ValueDeserializerImpl for IndexedDbStructuredCloneDeserializer<'_> { build_web_transport_error_from_clone_payload(scope, &payload) } HOST_OBJECT_TAG_IMAGE_DATA => read_image_data_payload(scope, deserializer), + HOST_OBJECT_TAG_IMAGE_DATA_GRAPH => read_image_data_graph_payload(scope, deserializer), HOST_OBJECT_TAG_CRYPTO_KEY => { read_crypto_key_payload(scope, deserializer).or_else(|| { let exception = dom_exception_value( diff --git a/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs b/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs index 13443434ec9..090e437b578 100644 --- a/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs +++ b/moli-renderer-v8/src/script_vm/tests/canvas_webgl.rs @@ -1793,7 +1793,7 @@ fn html_canvas_image_data_methods_parse_webidl_enforce_range_long() { assert_eq!( result, - r#"{"created":"2:3","createZero":"IndexSizeError","createSymbol":"TypeError","getNegative":"2|2|255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255","getMissing":"TypeError","getNan":"TypeError","getZero":"IndexSizeError","constructorOversized":"IndexSizeError","getOversized":"IndexSizeError","putCoerced":"0,0,0,0,255,0,0,255,0,255,0,255","putSymbol":"TypeError","afterBadPut":true,"dirtyNegative":"255,0,0,255,0,0,0,0","putPlainObject":"TypeError"}"# + r#"{"created":"2:3","createZero":"IndexSizeError","createSymbol":"TypeError","getNegative":"2|2|255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255","getMissing":"TypeError","getNan":"TypeError","getZero":"IndexSizeError","constructorOversized":"RangeError","getOversized":"IndexSizeError","putCoerced":"0,0,0,0,255,0,0,255,0,255,0,255","putSymbol":"TypeError","afterBadPut":true,"dirtyNegative":"255,0,0,255,0,0,0,0","putPlainObject":"TypeError"}"# ); } diff --git a/moli-renderer-v8/src/script_vm/tests/image_data_native.js b/moli-renderer-v8/src/script_vm/tests/image_data_native.js new file mode 100644 index 00000000000..1c8124a2326 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/image_data_native.js @@ -0,0 +1,194 @@ +(sourceRealm, realm, label) => { + const checks = []; + const C = realm.ImageData; + const check = (name, run) => { + try { checks.push({name: label + ':' + name, passed: run() === true}); } + catch (error) { checks.push({name: label + ':' + name, passed: false, error: String(error)}); } + }; + const construct = (...args) => Reflect.construct(C, args); + const throws = (kind, run) => { + try { run(); } catch (error) { + return kind === 'TypeError' || kind === 'RangeError' + ? error instanceof realm[kind] + : error.name === kind && error instanceof realm.DOMException; + } + return false; + }; + const same = (a, b) => a.length === b.length && a.every((v, i) => Object.is(v, b[i])); + check('constructor length', () => C.length === 2); + check('constructor requires new', () => throws('TypeError', () => C(1, 1))); + for (const args of [[], [1], [new sourceRealm.Uint8ClampedArray(4)]]) { + check('required arity ' + args.length + '-' + typeof args[0], () => throws('TypeError', () => Reflect.construct(C, args))); + } + check('required arity precedes conversion', () => { + let calls = 0; + const rejected = throws('TypeError', () => construct({valueOf() { calls++; return 1; }})); + return rejected && calls === 0; + }); + for (const [width, height, expectedWidth, expectedHeight] of [ + [1, 2, 1, 2], [2.9, 3.9, 2, 3], [4294967298, 4294967299, 2, 3], + [-4294967295, -4294967294, 1, 2], ['2', true, 2, 1] + ]) check('unsigned dimensions ' + String(width), () => { + const image = construct(width, height); + return image.width === expectedWidth && image.height === expectedHeight && + image.data instanceof realm.Uint8ClampedArray && image.data.length === expectedWidth * expectedHeight * 4 && + image.data.every(v => v === 0); + }); + for (const [index, value] of [0, -0, 0.9, -0.9, NaN, Infinity, -Infinity, 4294967296, undefined, null, 'bad'].entries()) { + check('zero converted width ' + index, () => throws('IndexSizeError', () => construct(value, 2))); + check('zero converted height ' + index, () => throws('IndexSizeError', () => construct(2, value))); + } + for (const value of [Symbol('size'), 1n]) check('invalid numeric ' + typeof value, () => throws('TypeError', () => construct(1, value))); + check('dimension and settings conversion order before size check', () => { + const order = []; + const rejected = throws('IndexSizeError', () => construct( + {valueOf() { order.push('width'); return 0; }}, + {valueOf() { order.push('height'); return 1; }}, + {get colorSpace() { order.push('colorSpace'); return 'srgb'; }, get pixelFormat() { order.push('pixelFormat'); return 'rgba-unorm8'; }})); + return rejected && order.join(',') === 'width,height,colorSpace,pixelFormat'; + }); + check('dictionary exception identity before size check', () => { + const marker = {}; + try { construct(0, 0, {get colorSpace() { throw marker; }}); } + catch (error) { return error === marker; } + return false; + }); + check('pixelFormat exception identity', () => { + const marker = {}; + try { construct(1, 1, {get pixelFormat() { throw marker; }}); } + catch (error) { return error === marker; } + return false; + }); + for (const settings of [null, undefined, {}]) check('default settings ' + String(settings), () => { + const image = construct(1, 1, settings); + return image.colorSpace === 'srgb' && image.pixelFormat === 'rgba-unorm8'; + }); + for (const field of ['colorSpace', 'pixelFormat']) { + for (const value of ['invalid', null, Symbol(field)]) check('invalid ' + field + ' ' + String(value), () => + throws('TypeError', () => construct(1, 1, {[field]: value}))); + } + check('four arguments select data overload before numeric conversion', () => { + let calls = 0; + const rejected = throws('TypeError', () => construct({valueOf() { calls++; return 1; }}, 1, {}, {})); + return rejected && calls === 0; + }); + for (const [type, format, bytesPerElement] of [ + ['Uint8ClampedArray', 'rgba-unorm8', 1], ['Float16Array', 'rgba-float16', 2] + ]) { + const settings = {colorSpace: 'display-p3', pixelFormat: format}; + check(type + ' allocated storage', () => { + const image = construct(3, 2, settings); + return image.pixelFormat === format && image.colorSpace === 'display-p3' && image.data instanceof realm[type] && + image.data.length === 24 && image.data.byteOffset === 0 && image.data.buffer.byteLength === 24 * bytesPerElement && image.data.every(v => v === 0); + }); + for (const explicitHeight of [false, true]) check(type + ' native subview identity ' + explicitHeight, () => { + const buffer = new sourceRealm.ArrayBuffer(40 * bytesPerElement); + const data = new sourceRealm[type](buffer, 4 * bytesPerElement, 24); + const image = construct(data, 3, explicitHeight ? 2 : undefined, settings); + data[1] = type === 'Float16Array' ? -0.5 : 7; + return image.width === 3 && image.height === 2 && image.data === data && image.data[1] === data[1] && + image.data.buffer === buffer && image.data.byteOffset === 4 * bytesPerElement && image.pixelFormat === format; + }); + for (const length of [0, 1, 3, 5, 7]) check(type + ' invalid element count ' + length, () => + throws('InvalidStateError', () => construct(new sourceRealm[type](length), 0, undefined, settings))); + for (const [width, height] of [[0, undefined], [3, undefined], [1, 0], [1, 1], [2, 2]]) { + check(type + ' inconsistent dimensions ' + width + '-' + height, () => + throws('IndexSizeError', () => construct(new sourceRealm[type](8), width, height, settings))); + } + check(type + ' mismatched format', () => throws('InvalidStateError', () => + construct(new sourceRealm[type](8), 1, undefined, {pixelFormat: type === 'Float16Array' ? 'rgba-unorm8' : 'rgba-float16'}))); + check(type + ' settings precede invalid length', () => { + let calls = 0; + const rejected = throws('InvalidStateError', () => construct(new sourceRealm[type](0), 0, undefined, + {get pixelFormat() { calls++; return format; }})); + return rejected && calls === 1; + }); + check(type + ' dimension conversion can detach data', () => { + const data = new sourceRealm[type](4), order = []; + const rejected = throws('InvalidStateError', () => construct(data, {valueOf() { + order.push('width'); sourceRealm.structuredClone(data.buffer, {transfer: [data.buffer]}); return 1; + }}, undefined, {get pixelFormat() { order.push('pixelFormat'); return format; }})); + return rejected && order.join(',') === 'width,pixelFormat'; + }); + check(type + ' dictionary conversion can detach data', () => { + const data = new sourceRealm[type](4); + return throws('InvalidStateError', () => construct(data, 1, 1, {get pixelFormat() { + sourceRealm.structuredClone(data.buffer, {transfer: [data.buffer]}); return format; + }})); + }); + for (const detach of [false, true]) check(type + ' rejects resizable buffer before conversion ' + detach, () => { + const buffer = new sourceRealm.ArrayBuffer(8 * bytesPerElement, {maxByteLength: 16 * bytesPerElement}); + const data = new sourceRealm[type](buffer); + if (detach) sourceRealm.structuredClone(buffer, {transfer: [buffer]}); + let calls = 0; + const rejected = throws('TypeError', () => construct(data, {valueOf() { calls++; return 1; }}, undefined, settings)); + return rejected && calls === 0; + }); + check(type + ' rejects shared buffer before conversion', () => { + const buffer = new sourceRealm.WebAssembly.Memory({initial: 1, maximum: 1, shared: true}).buffer; + const data = new sourceRealm[type](buffer, 0, 4); + let calls = 0; + const rejected = throws('TypeError', () => construct(data, {valueOf() { calls++; return 1; }}, undefined, settings)); + return rejected && calls === 0; + }); + check(type + ' clone preserves format pixels and independence', () => { + const data = new sourceRealm[type](type === 'Float16Array' ? [-1, 0.5, 1024, 1] : [1, 127, 255, 17]); + const image = construct(data, 1, 1, settings); + const copy = realm.structuredClone(image); + return copy instanceof C && copy.width === 1 && copy.height === 1 && copy.colorSpace === 'display-p3' && + copy.pixelFormat === format && copy.data instanceof realm[type] && copy.data !== data && copy.data.buffer !== data.buffer && + same(Array.from(copy.data), Array.from(data)); + }); + for (const first of ['image', 'data']) check(type + ' clone graph identity ' + first, () => { + const data = new sourceRealm[type](new sourceRealm.ArrayBuffer(12 * bytesPerElement), 4 * bytesPerElement, 4); + const image = construct(data, 1, 1, settings); + const object = first === 'image' ? {image, data, buffer: data.buffer} : {data, buffer: data.buffer, image}; + object.again = image; + const copy = realm.structuredClone(object); + return copy.image === copy.again && copy.image.data === copy.data && copy.data.buffer === copy.buffer && + copy.data.byteOffset === 4 * bytesPerElement && copy.buffer.byteLength === 12 * bytesPerElement; + }); + check(type + ' clone transferred backing buffer graph', () => { + const data = new sourceRealm[type]([1, 0, 1, 1]); + const image = construct(data, 1, 1, settings); + const copy = realm.structuredClone({image, data, buffer: data.buffer}, {transfer: [data.buffer]}); + return data.byteLength === 0 && image.width === 1 && copy.image.data === copy.data && copy.data.buffer === copy.buffer && + same(Array.from(copy.data), [1, 0, 1, 1]); + }); + check(type + ' detached data cannot clone', () => { + const data = new sourceRealm[type](4), image = construct(data, 1, 1, settings); + sourceRealm.structuredClone(data.buffer, {transfer: [data.buffer]}); + return throws('DataCloneError', () => realm.structuredClone(image)); + }); + } + const image = construct(1, 1); + for (const attribute of ['width', 'height', 'data', 'colorSpace', 'pixelFormat']) { + const getter = Object.getOwnPropertyDescriptor(C.prototype, attribute).get; + const revoked = sourceRealm.Proxy.revocable(image, {}); revoked.revoke(); + for (const [index, receiver] of [null, undefined, {}, Object.create(C.prototype), Object.create(image), new sourceRealm.Proxy(image, {}), revoked.proxy].entries()) { + check(attribute + ' rejects receiver ' + index, () => throws('TypeError', () => Reflect.apply(getter, receiver, []))); + } + } + check('native dimensions ignore author properties', () => { + const data = new sourceRealm.Uint8ClampedArray(8); + for (const key of ['byteLength', 'length', 'buffer']) Object.defineProperty(data, key, {get() { throw Error('author ' + key); }}); + const result = construct(data, 2); + return result.width === 2 && result.height === 1 && result.data === data; + }); + check('subclass initializes native ImageData state', () => { + class Child extends C {} + const result = new Child(1, 2); + return result instanceof Child && result instanceof C && result.width === 1 && result.height === 2; + }); + check('clone ignores poisoned globals and instance accessors', () => { + const image = construct(new sourceRealm.Uint8ClampedArray([1, 2, 3, 4]), 1, 1); + for (const key of ['width', 'height', 'data', 'colorSpace', 'pixelFormat']) Object.defineProperty(image, key, {get() { throw Error('author ' + key); }}); + const original = realm.ImageData; + try { + realm.ImageData = function() { throw Error('author constructor'); }; + const copy = realm.structuredClone(image); + return copy instanceof C && copy.width === 1 && same(Array.from(copy.data), [1, 2, 3, 4]); + } finally { realm.ImageData = original; } + }); + return checks; +} diff --git a/moli-renderer-v8/src/script_vm/tests/image_data_native.rs b/moli-renderer-v8/src/script_vm/tests/image_data_native.rs new file mode 100644 index 00000000000..1a8e2cb1dc8 --- /dev/null +++ b/moli-renderer-v8/src/script_vm/tests/image_data_native.rs @@ -0,0 +1,172 @@ +use super::*; +use v8::ValueSerializerHelper; + +#[test] +fn image_data_constructors_and_clone_graphs_preserve_native_state_across_realms() { + let mut vm = new_storage_page_task_executor_test_vm("https://image-data-native.test/"); + vm.eval("document.body.innerHTML=''") + .unwrap(); + vm.eval(&format!( + "const imageDataProbe={};const imageDataRealms=[globalThis,document.getElementById('child').contentWindow];\ + globalThis.imageDataChecks=[];\ + for(let source=0;source<2;source++)for(let callee=0;callee<2;callee++)\ + imageDataChecks.push(...imageDataProbe(imageDataRealms[source],imageDataRealms[callee],source+'-'+callee));", + include_str!("image_data_native.js"), + )).unwrap(); + assert_eq!(vm.eval("imageDataChecks.length").unwrap(), "544"); + assert_eq!( + vm.eval("JSON.stringify(imageDataChecks.filter(row=>!row.passed))") + .unwrap(), + "[]" + ); +} + +#[test] +fn image_data_accessors_accept_registered_native_proxy_identity() { + let mut vm = new_storage_page_task_executor_test_vm("https://image-data-proxy.test/"); + vm.eval("globalThis.imageDataNative=new ImageData(new Float16Array([-1,0.5,2,1]),1,1,{colorSpace:'display-p3',pixelFormat:'rgba-float16'});").unwrap(); + let context_ptr = &vm.page_default_runtime.context as *const _; + vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| { + let global = scope.get_current_context().global(scope); + let key = crate::util::v8str(scope, "imageDataNative"); + let target = + v8::Local::::try_from(global.get(scope, key.into()).unwrap()).unwrap(); + let handler = crate::util::new_null_prototype_object(scope); + let proxy = v8::Proxy::new(scope, target, handler).unwrap(); + moli_webapi_declare::register_web_api_proxy(scope, proxy)?; + let key = crate::util::v8str(scope, "imageDataProxy"); + assert_eq!( + global.create_data_property(scope, key.into(), proxy.into()), + Some(true) + ); + Ok(()) + }) + .unwrap(); + assert_eq!(vm.eval(r#"(() => { + for(const name of ['width','height','data','colorSpace','pixelFormat']) { + const get=Object.getOwnPropertyDescriptor(ImageData.prototype,name).get; + if(get.call(imageDataProxy)!==get.call(imageDataNative))throw Error(name+' native identity'); + let rejected=false; + try{get.call(new Proxy(imageDataProxy,{}));}catch(error){rejected=error instanceof TypeError;} + if(!rejected)throw Error(name+' author proxy'); + } + const context=new OffscreenCanvas(1,1).getContext('2d'); + context.putImageData(imageDataProxy,0,0); + return Array.from(context.getImageData(0,0,1,1).data).join(); + })()"#).unwrap(), "0,128,255,255"); +} + +#[test] +fn image_data_indexeddb_preserves_typed_arrays_and_shared_graph_references() { + let mut vm = new_storage_page_task_executor_test_vm("https://image-data-storage.test/"); + vm.eval(r#"(() => { + globalThis.imageDataStorage='pending'; + const rows=[]; + for(const [ArrayType,pixelFormat] of [[Uint8ClampedArray,'rgba-unorm8'],[Float16Array,'rgba-float16']]) { + const buffer=new ArrayBuffer(ArrayType.BYTES_PER_ELEMENT*12); + const data=new ArrayType(buffer,ArrayType.BYTES_PER_ELEMENT*4,4); + data.set([1,0,1,1]); + const image=new ImageData(data,1,1,{pixelFormat,colorSpace:'display-p3'}); + rows.push({image,again:image,data,buffer}); + } + const request=indexedDB.open('image-data-graph-'+Math.random(),1); + const fail=event=>{imageDataStorage='error:'+event.target.error?.name;}; + request.onerror=fail; + request.onupgradeneeded=()=>request.result.createObjectStore('values'); + request.onsuccess=()=>{ + const db=request.result,write=db.transaction('values','readwrite'); + write.onerror=fail;write.objectStore('values').put(rows,'pixels'); + rows[0].data[0]=99;rows[1].data[0]=-99; + write.oncomplete=()=>{ + const read=db.transaction('values').objectStore('values').get('pixels');read.onerror=fail; + read.onsuccess=()=>{ + try { + imageDataStorage=read.result.length===2&&read.result.every((row,index)=> + row.image instanceof ImageData&&row.image===row.again&&row.image.data===row.data&&row.data.buffer===row.buffer&& + row.data.byteOffset===4*(index?2:1)&&row.buffer.byteLength===12*(index?2:1)&& + row.image.pixelFormat===(index?'rgba-float16':'rgba-unorm8')&&row.image.colorSpace==='display-p3'&& + Array.from(row.data).join()==='1,0,1,1'&&row.data instanceof (index?Float16Array:Uint8ClampedArray)); + }finally{db.close();} + }; + }; + }; + })()"#).unwrap(); + assert_eq!( + vm.eval_after_selected_page_tasks("String(imageDataStorage)") + .unwrap(), + "true" + ); +} + +struct LegacyImageDataWriter; + +impl v8::ValueSerializerImpl for LegacyImageDataWriter { + fn throw_data_clone_error<'s>( + &self, + scope: &mut v8::PinScope<'s, '_>, + message: v8::Local<'s, v8::String>, + ) { + let error = v8::Exception::error(scope, message); + scope.throw_exception(error); + } + + fn has_custom_host_object(&self, _isolate: &v8::Isolate) -> bool { + true + } + + fn is_host_object<'s>( + &self, + scope: &mut v8::PinScope<'s, '_>, + object: v8::Local<'s, v8::Object>, + ) -> Option { + Some(crate::web_api_interfaces::ImageData::is_instance( + scope, object, + )) + } + + fn write_host_object<'s>( + &self, + _scope: &mut v8::PinScope<'s, '_>, + _object: v8::Local<'s, v8::Object>, + serializer: &dyn v8::ValueSerializerHelper, + ) -> Option { + // The pre-graph record consists of tag, dimensions, color-space bytes + // and RGBA8 bytes. Keep this independent of the current writer. + for value in [2, 1, 2, 10] { + serializer.write_uint32(value); + } + serializer.write_raw_bytes(b"display-p3"); + serializer.write_uint32(8); + serializer.write_raw_bytes(&[1, 2, 3, 4, 5, 6, 7, 8]); + Some(true) + } +} + +#[test] +fn image_data_legacy_serialized_records_remain_readable() { + let mut vm = new_storage_page_task_executor_test_vm("https://image-data-legacy.test/"); + vm.eval("globalThis.legacyImageDataSource=new ImageData(1,2)") + .unwrap(); + let context_ptr = &vm.page_default_runtime.context as *const _; + vm.with_context_scope_by_ptr_and_checkpoint_for_test(context_ptr, |scope, _| { + let context = scope.get_current_context(); + let global = context.global(scope); + let key = crate::util::v8str(scope, "legacyImageDataSource"); + let source = global.get(scope, key.into()).unwrap(); + let serializer = v8::ValueSerializer::new(scope, Box::new(LegacyImageDataWriter)); + serializer.write_header(); + assert_eq!(serializer.write_value(context, source), Some(true)); + let mut payload = crate::structured_clone::V8StructuredClonePayload::default(); + payload.base.wire_bytes = serializer.release(); + drop(serializer); + let result = crate::structured_clone::deserialize_from_wire(scope, &payload).unwrap(); + let key = crate::util::v8str(scope, "legacyImageDataResult"); + assert_eq!( + global.create_data_property(scope, key.into(), result), + Some(true) + ); + Ok(()) + }) + .unwrap(); + assert_eq!(vm.eval("[legacyImageDataResult instanceof ImageData,legacyImageDataResult.width,legacyImageDataResult.height,legacyImageDataResult.colorSpace,legacyImageDataResult.pixelFormat,Array.from(legacyImageDataResult.data)].join('|')").unwrap(), "true|1|2|display-p3|rgba-unorm8|1,2,3,4,5,6,7,8"); +} diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs index eb464cdce58..6904e38f00a 100644 --- a/moli-renderer-v8/src/script_vm/tests/mod.rs +++ b/moli-renderer-v8/src/script_vm/tests/mod.rs @@ -2305,6 +2305,7 @@ mod device_events; mod document_shadow_handler_receivers; mod document_svg_root; mod image_bitmap_tasks; +mod image_data_native; mod payment_events; mod payment_request; mod reporting_observer; diff --git a/moli-renderer-v8/src/structured_clone.rs b/moli-renderer-v8/src/structured_clone.rs index 590cebdc16c..fe9cc438d3c 100644 --- a/moli-renderer-v8/src/structured_clone.rs +++ b/moli-renderer-v8/src/structured_clone.rs @@ -19,7 +19,7 @@ use crate::{ attach_file_system_file_snapshot_clone_payload, build_dom_exception_object, build_encoded_video_chunk_from_clone_payload, build_file_list_object, build_file_system_handle_from_clone_payload, build_geometry_object_from_clone_payload, build_geometry_quad_from_cloned_points, - build_image_data_object_from_clone_payload, build_quota_exceeded_error_object, + build_image_data_object_from_clone_payload, build_image_data_object_from_legacy_bytes, build_quota_exceeded_error_object, build_readable_stream_clone_shell, build_transform_stream_clone_shell, build_writable_stream_clone_shell, crypto_key_clone_payload_from_object, crypto_key_object_from_clone_payload, detach_message_port_owner_for_transfer, @@ -60,6 +60,7 @@ const HOST_OBJECT_TAG_VIDEO_FRAME: u32 = 15; const HOST_OBJECT_TAG_RTC_ENCODED_AUDIO_FRAME: u32 = 16; const HOST_OBJECT_TAG_RTC_ENCODED_VIDEO_FRAME: u32 = 17; const HOST_OBJECT_TAG_RTC_CERTIFICATE: u32 = 18; +pub(crate) const HOST_OBJECT_TAG_IMAGE_DATA_GRAPH: u32 = 19; const HOST_OBJECT_TAG_TRANSFERRED_MESSAGE_PORT: u32 = 22; const HOST_OBJECT_TAG_RTC_ENCODED_AUDIO_FRAME: u32 = 16; const HOST_OBJECT_TAG_RTC_ENCODED_VIDEO_FRAME: u32 = 17; @@ -582,8 +583,7 @@ impl v8::ValueSerializerImpl for WireSerializer { } Some("ImageData") => { if let Some(payload) = image_data_clone_payload_from_object(scope, object) { - write_image_data_payload(serializer, payload); - return Some(true); + return write_image_data_payload(scope, serializer, payload); } } Some("VideoFrame") => { @@ -916,6 +916,7 @@ impl v8::ValueDeserializerImpl for WireDeserializer { ensure_message_port_wrapper_for_id(scope, port_id) } HOST_OBJECT_TAG_IMAGE_DATA => read_image_data_payload(scope, deserializer), + HOST_OBJECT_TAG_IMAGE_DATA_GRAPH => read_image_data_graph_payload(scope, deserializer), HOST_OBJECT_TAG_VIDEO_FRAME => { let info = read_raw_vec(deserializer) .and_then(|bytes| serde_json::from_slice(&bytes).ok()); @@ -1216,17 +1217,42 @@ pub(crate) fn build_blob_object_from_clone_payload<'s>( } } -pub(crate) fn write_image_data_payload( +pub(crate) fn write_image_data_payload<'s>( + scope: &mut v8::PinScope<'s, '_>, serializer: &dyn v8::ValueSerializerHelper, - payload: ImageDataClonePayload, -) { - serializer.write_uint32(HOST_OBJECT_TAG_IMAGE_DATA); + payload: ImageDataClonePayload<'s>, +) -> Option { + serializer.write_uint32(HOST_OBJECT_TAG_IMAGE_DATA_GRAPH); serializer.write_uint32(payload.width); serializer.write_uint32(payload.height); - write_raw_vec(serializer, payload.color_space.as_bytes()); - write_raw_vec(serializer, &payload.bytes); + write_string(serializer, &payload.color_space); + write_string(serializer, payload.pixel_format.label()); + serializer.write_value(scope.get_current_context(), payload.data.into()) } +pub(crate) fn read_image_data_graph_payload<'s>( + scope: &mut v8::PinScope<'s, '_>, + deserializer: &dyn v8::ValueDeserializerHelper, +) -> Option> { + let width = read_u32(deserializer)?; + let height = read_u32(deserializer)?; + let color_space = read_string(deserializer)?; + let pixel_format = read_string(deserializer)?.parse().ok()?; + let data = deserializer.read_value(scope.get_current_context())?; + let data = v8::Local::::try_from(data).ok()?; + build_image_data_object_from_clone_payload( + scope, + ImageDataClonePayload { + width, + height, + color_space, + pixel_format, + data, + }, + ) +} + +// Keep the original byte-only record readable for existing IndexedDB/history data. pub(crate) fn read_image_data_payload<'s>( scope: &mut v8::PinScope<'s, '_>, deserializer: &dyn v8::ValueDeserializerHelper, @@ -1244,15 +1270,7 @@ pub(crate) fn read_image_data_payload<'s>( throw_data_clone_exception(scope, "Failed to deserialize ImageData pixel data."); return None; }; - build_image_data_object_from_clone_payload( - scope, - ImageDataClonePayload { - width, - height, - color_space, - bytes, - }, - ) + build_image_data_object_from_legacy_bytes(scope, width, height, color_space, bytes) } fn write_geometry_clone_payload(