Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(canvas): declare image and offscreen canvas receivers

ldm0b676d09811b8
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 f6bfafb26..872da8b0a 100644
--- a/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs
+++ b/moli-renderer-v8/src/context_bootstrap/canvas/image_bitmap.rs
@@ -1,5 +1,4 @@
use super::backing_store::{canvas_like_has_context, canvas_like_pixels_copy};
-use super::offscreen::offscreen_canvas_receiver_branded;
use super::*;
use crate::context_bootstrap::new_dom_exception_value;
use crate::util::{
@@ -23,7 +22,7 @@ struct ImageBitmapObjectDeclaration {
}
#[derive(Default, WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::ImageBitmap, enumerable)]
+#[webapi(interface = web_api_interfaces::ImageBitmap, enumerable, receiver)]
struct ImageBitmapPrototypeDeclaration {
#[webapi(
accessor_property,
@@ -74,7 +73,7 @@ pub(crate) fn window_create_image_bitmap_callback<'s>(
);
return;
};
- if !offscreen_canvas_receiver_branded(scope, source) {
+ if !web_api_interfaces::OffscreenCanvas::is_instance(scope, source) {
throw_type_error(
scope,
"Failed to execute 'createImageBitmap' on 'Window': The provided value is not a valid image source.",
@@ -143,10 +142,6 @@ fn image_bitmap_dimension_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
- if !image_bitmap_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let Some(slot) = callback_data_item(
scope,
&args,
@@ -167,19 +162,8 @@ fn image_bitmap_close_callback<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
- if !image_bitmap_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
for slot in IMAGE_BITMAP_DIMENSION_SLOTS {
set_private_value(scope, args.this(), slot, v8::Number::new(scope, 0.0).into());
}
rv.set_undefined();
}
-
-fn image_bitmap_receiver_branded<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- receiver: v8::Local<'s, v8::Object>,
-) -> bool {
- web_api_interfaces::ImageBitmap::is_instance(scope, receiver)
-}
diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/offscreen.rs b/moli-renderer-v8/src/context_bootstrap/canvas/offscreen.rs
index 6cbad8d03..a0b370700 100644
--- a/moli-renderer-v8/src/context_bootstrap/canvas/offscreen.rs
+++ b/moli-renderer-v8/src/context_bootstrap/canvas/offscreen.rs
@@ -23,7 +23,7 @@ struct OffscreenCanvasObjectDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::OffscreenCanvas)]
+#[webapi(interface = web_api_interfaces::OffscreenCanvas, receiver)]
struct OffscreenCanvasPrototypeAccessorsDeclaration {
#[webapi(
accessor_property,
@@ -72,10 +72,6 @@ fn offscreen_canvas_attribute_getter_callback<'s>(
rv.set_undefined();
return;
};
- if !offscreen_canvas_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let value = get_private_value(scope, args.this(), slot)
.and_then(|value| value.number_value(scope))
.unwrap_or(0.0) as u32;
@@ -96,10 +92,6 @@ fn offscreen_canvas_attribute_setter_callback<'s>(
rv.set_undefined();
return;
};
- if !offscreen_canvas_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let Some(name) = offscreen_canvas_attribute_name_for_slot(slot) else {
rv.set_undefined();
return;
@@ -139,10 +131,6 @@ pub(crate) fn offscreen_canvas_get_context_callback<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
- if !offscreen_canvas_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let Some(kind) = webidl::try_parse_args::<OffscreenCanvasGetContextArgs>(scope, &args)
.ok()
.map(|parsed| parsed.kind)
@@ -191,13 +179,9 @@ pub(crate) fn offscreen_canvas_get_context_callback<'s>(
pub(crate) fn offscreen_canvas_convert_to_blob_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
+ _args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
- if !offscreen_canvas_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let Some(resolver) = v8::PromiseResolver::new(scope) else {
rv.set(v8::undefined(scope).into());
return;
@@ -224,10 +208,3 @@ pub(super) fn init_offscreen_canvas_object<'s>(
.expect("OffscreenCanvas declaration should initialize object");
reset_canvas_like_backing_store(scope, object);
}
-
-pub(super) fn offscreen_canvas_receiver_branded<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- receiver: v8::Local<'s, v8::Object>,
-) -> bool {
- web_api_interfaces::OffscreenCanvas::is_instance(scope, receiver)
-}
diff --git a/moli-renderer-v8/src/context_bootstrap/image_data.rs b/moli-renderer-v8/src/context_bootstrap/image_data.rs
index 7295275a1..96cf19eb1 100644
--- a/moli-renderer-v8/src/context_bootstrap/image_data.rs
+++ b/moli-renderer-v8/src/context_bootstrap/image_data.rs
@@ -28,7 +28,7 @@ struct ImageDataObjectDeclaration<'s> {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::ImageData)]
+#[webapi(interface = web_api_interfaces::ImageData, receiver)]
struct ImageDataPrototypeDeclaration {
#[webapi(
accessor_property,
@@ -146,10 +146,6 @@ fn image_data_getter_callback<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
- if !image_data_receiver_branded(scope, args.this()) {
- throw_type_error(scope, "Illegal invocation");
- return;
- }
let Some(slot) = callback_data_item(
scope,
&args,
@@ -164,13 +160,6 @@ fn image_data_getter_callback<'s>(
rv.set(value);
}
-fn image_data_receiver_branded<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- receiver: v8::Local<'s, v8::Object>,
-) -> bool {
- web_api_interfaces::ImageData::is_instance(scope, receiver)
-}
-
fn image_data_constructor_parts<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: &v8::FunctionCallbackArguments<'s>,
diff --git a/moli-renderer-v8/src/context_bootstrap/media_file_template.rs b/moli-renderer-v8/src/context_bootstrap/media_file_template.rs
index f411cd3af..dc3be189a 100644
--- a/moli-renderer-v8/src/context_bootstrap/media_file_template.rs
+++ b/moli-renderer-v8/src/context_bootstrap/media_file_template.rs
@@ -76,7 +76,7 @@ struct BlobTemplateMethodsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::OffscreenCanvas, enumerable)]
+#[webapi(interface = web_api_interfaces::OffscreenCanvas, enumerable, receiver)]
struct OffscreenCanvasTemplateMethodsDeclaration {
#[webapi(method, length = 1, callback = offscreen_canvas_get_context_callback)]
get_context: (),
diff --git a/moli-renderer-v8/src/script_vm/tests/declarative_receivers.rs b/moli-renderer-v8/src/script_vm/tests/declarative_receivers.rs
index b8aaf72a4..67421ddec 100644
--- a/moli-renderer-v8/src/script_vm/tests/declarative_receivers.rs
+++ b/moli-renderer-v8/src/script_vm/tests/declarative_receivers.rs
@@ -100,3 +100,37 @@ fn navigator_declarations_preserve_receiver_checks_and_promise_identity_across_r
assert_eq!(vm.eval("receiverSetup").unwrap(), "ok");
finish_receiver_checks(&mut vm);
}
+
+#[test]
+fn canvas_declarations_check_setters_before_conversion_and_keep_foreign_backing_state() {
+ let mut vm = receiver_test_vm();
+ vm.eval(r#"(async () => {
+ const {child, assert, check, getters} = receiverChecks;
+ for (const realm of [window, child]) {
+ const other = realm === window ? child : window;
+ const canvas = new other.OffscreenCanvas(2, 3);
+ getters(realm, 'OffscreenCanvas', canvas, ['width', 'height']);
+ for (const name of ['width', 'height']) {
+ const setter = Object.getOwnPropertyDescriptor(realm.OffscreenCanvas.prototype, name).set;
+ check(realm, canvas, setter);
+ setter.call(canvas, 4);
+ assert(canvas[name] === 4, 'foreign canvas setter updates receiver');
+ }
+ for (const name of ['getContext', 'convertToBlob']) check(realm, canvas, realm.OffscreenCanvas.prototype[name]);
+ assert(realm.OffscreenCanvas.prototype.getContext.call(canvas, '2d') === canvas.getContext('2d'), 'foreign canvas context identity');
+ const bitmap = await other.createImageBitmap(canvas);
+ getters(realm, 'ImageBitmap', bitmap, ['width', 'height']);
+ check(realm, bitmap, realm.ImageBitmap.prototype.close);
+ realm.ImageBitmap.prototype.close.call(bitmap);
+ assert(bitmap.width === 0 && bitmap.height === 0, 'foreign bitmap close updates receiver');
+ const data = new other.ImageData(2, 3);
+ getters(realm, 'ImageData', data, ['width', 'height', 'colorSpace', 'pixelFormat', 'data']);
+ let error;
+ try {realm.createImageBitmap(new Proxy(canvas, {}));} catch (caught) {error = caught;}
+ assert(Object.getPrototypeOf(error) === realm.TypeError.prototype, 'image source parameter keeps native interface check');
+ }
+ return 'ok';
+ })().then(result => {globalThis.receiverSetup = result;}, error => {globalThis.receiverSetup = String(error);})"#).unwrap();
+ assert_eq!(vm.eval("receiverSetup").unwrap(), "ok");
+ finish_receiver_checks(&mut vm);
+}