Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
refactor(observers): deliver callbacks through shared execution
ldm0e25966f16e46
diff --git a/moli-renderer-v8/src/context_bootstrap/performance_observer_runtime/registration.rs b/moli-renderer-v8/src/context_bootstrap/performance_observer_runtime/registration.rsindex bddddcea5..c7d5092da 100644--- a/moli-renderer-v8/src/context_bootstrap/performance_observer_runtime/registration.rs+++ b/moli-renderer-v8/src/context_bootstrap/performance_observer_runtime/registration.rs@@ -179,8 +179,7 @@ impl<'s> PerformanceObserverRegistration<'s> {relevant,incumbent,);- crate::observer_runtime::prepare_callback(scope, host_ptr, residence)?- .into_execution(scope, host_ptr)+ crate::observer_runtime::prepare_callback(scope, host_ptr, residence)}RegistrationEnvironment::Worker => {let context = self.observer.get_creation_context(scope)?;diff --git a/moli-renderer-v8/src/context_bootstrap/resize_observer_runtime.rs b/moli-renderer-v8/src/context_bootstrap/resize_observer_runtime.rsindex 6210f2bf6..197758ab3 100644--- a/moli-renderer-v8/src/context_bootstrap/resize_observer_runtime.rs+++ b/moli-renderer-v8/src/context_bootstrap/resize_observer_runtime.rs@@ -1,11 +1,9 @@mod entry;use super::*;-use crate::host::report_event_callback_exception;use crate::observer_runtime::ObserverCallbackId;use crate::util::{get_private_value, serialize_v8_iter_array, set_private_value};use crate::web_api_interfaces;use crate::webidl;-use crate::window_webidl_callback::WindowWebIdlCallbackFunctionOutcome;use entry::ResizeObserverEntryData;pub(super) use entry::install_resize_observer_entry_template_bindings;use moli_webapi_declare::WebApiObject;@@ -276,9 +274,7 @@ fn resize_observer_flush_callback<'s>(set_resize_observer_pending_targets(scope, observer, pending_targets);return;};- let Some(callback_context) = callback.relevant_context(scope) else {- return;- };+ let callback_context = callback.relevant_context(scope);let result = {let scope = &mut v8::ContextScope::new(scope, callback_context);build_resize_observer_entries(scope, pending_targets)@@ -298,26 +294,13 @@ fn resize_observer_flush_callback<'s>(return;}let observer_value: v8::Local<'_, v8::Value> = observer.into();- match callback.invoke(+ callback.invoke_and_report(scope,- host_ptr,+ "resizeobserver","ResizeObserver callback",observer_value,&[entries.into(), observer_value],- ) {- WindowWebIdlCallbackFunctionOutcome::Threw(report) => {- report_event_callback_exception(- scope,- host_ptr,- "resizeobserver",- callback.relevant_identity(),- None,- &report,- );- }- WindowWebIdlCallbackFunctionOutcome::Returned- | WindowWebIdlCallbackFunctionOutcome::Retired => {}- }+ );}fn is_resize_observer_target<'s>(diff --git a/moli-renderer-v8/src/observer_runtime/callback.rs b/moli-renderer-v8/src/observer_runtime/callback.rsindex b360383ac..80cb3751b 100644--- a/moli-renderer-v8/src/observer_runtime/callback.rs+++ b/moli-renderer-v8/src/observer_runtime/callback.rs@@ -19,10 +19,7 @@ use crate::{WindowExecutionContextOwner,},webidl_callback_execution::PreparedWebIdlCallbackExecution,- window_webidl_callback::{- PreparedWindowWebIdlCallbackFunction, WindowWebIdlCallbackFunction,- WindowWebIdlCallbackFunctionOutcome, invoke_window_webidl_callback_function,- },+ window_webidl_callback::WindowWebIdlCallbackFunction,};pub(super) struct ObserverCallback {@@ -46,11 +43,13 @@ impl ObserverCallback {}}- pub(super) fn prepare(&self, scope: &mut v8::PinScope<'_, '_>) -> PreparedObserverCallback {- PreparedObserverCallback {- callback: self.callback.prepare(scope),- observer_identity: self.observer_identity,- }+ pub(super) fn prepare(+ &self,+ scope: &mut v8::PinScope<'_, '_>,+ host_ptr: *mut JsContextHost,+ ) -> Option<PreparedWebIdlCallbackExecution> {+ self.callback+ .prepare_execution(scope, host_ptr, Some(self.observer_identity?))}pub(super) fn is_owned_by(&self, owner: WindowExecutionContextOwner) -> bool {@@ -194,7 +193,7 @@ impl ObserverCallbackRegistry {callback: v8::Local<'s, v8::Object>,relevant_global: v8::Local<'s, v8::Object>,incumbent_global: v8::Local<'s, v8::Object>,- ) -> Option<PreparedObserverCallback> {+ ) -> Option<PreparedWebIdlCallbackExecution> {let (observer_identity, callback_identity) = {let state = self.state.borrow();let binding = state.bindings.get(&id)?;@@ -211,13 +210,13 @@ impl ObserverCallbackRegistry {relevant_context,incumbent_context,)?;- Some(PreparedObserverCallback {- callback: PreparedWindowWebIdlCallbackFunction::Live {- callback,- relevant_identity: callback_identity?,- },- observer_identity,- })+ PreparedWebIdlCallbackExecution::window(+ scope,+ callback,+ std::ptr::from_ref(host).cast_mut(),+ Some(observer_identity?),+ callback_identity?,+ )}pub(super) fn is_current(&self, host: &JsContextHost, id: ObserverCallbackId) -> bool {@@ -318,73 +317,3 @@ impl ObserverCallbackRegistry {}}}--pub(crate) struct PreparedObserverCallback {- callback: PreparedWindowWebIdlCallbackFunction,- observer_identity: Option<WindowExecutionContextIdentity>,-}--impl PreparedObserverCallback {- /// Retain both registration-time identities while using the common- /// Window/Worker invocation and exception-reporting adapter.- pub(crate) fn into_execution(- self,- scope: &mut v8::PinScope<'_, '_>,- host_ptr: *mut JsContextHost,- ) -> Option<PreparedWebIdlCallbackExecution> {- let PreparedWindowWebIdlCallbackFunction::Live {- callback,- relevant_identity,- } = self.callback- else {- return None;- };- PreparedWebIdlCallbackExecution::window(- scope,- callback,- host_ptr,- Some(self.observer_identity?),- relevant_identity,- )- }-- pub(crate) fn relevant_context<'s>(- &self,- scope: &mut v8::PinScope<'s, '_>,- ) -> Option<v8::Local<'s, v8::Context>> {- self.callback.relevant_context(scope)- }-- pub(crate) fn is_current(&self, host: &JsContextHost) -> bool {- self.observer_identity- .is_some_and(|identity| host.window_execution_context_identity_is_current(identity))- && self.callback.is_current(host)- }-- pub(crate) const fn relevant_identity(&self) -> Option<WindowExecutionContextIdentity> {- self.callback.relevant_identity()- }-- pub(crate) fn invoke<'s>(- &self,- scope: &mut v8::PinScope<'s, '_>,- host_ptr: *mut JsContextHost,- callback_name: &str,- receiver: v8::Local<'s, v8::Value>,- arguments: &[v8::Local<'s, v8::Value>],- ) -> WindowWebIdlCallbackFunctionOutcome {- if !self.is_current(unsafe { &*host_ptr }) {- return WindowWebIdlCallbackFunctionOutcome::Retired;- }- invoke_window_webidl_callback_function(- scope,- host_ptr,- "callback",- "observer callback threw",- callback_name,- &self.callback,- receiver,- arguments,- )- }-}diff --git a/moli-renderer-v8/src/observer_runtime/dom_access.rs b/moli-renderer-v8/src/observer_runtime/dom_access.rsindex fa40e7eec..5d8276be9 100644--- a/moli-renderer-v8/src/observer_runtime/dom_access.rs+++ b/moli-renderer-v8/src/observer_runtime/dom_access.rs@@ -3,6 +3,7 @@ use std::ptr::NonNull;use crate::{dom::native::{DomHost, DomMutationEffects, NativeNodeId},native_bridge::{JsContextHost, RuntimeObservableContextToken, WindowExecutionContextOwner},+ webidl_callback_execution::PreparedWebIdlCallbackExecution,};use moli_webidl_callback::WebIdlCallbackFunction;@@ -10,9 +11,7 @@ use super::{IntersectionMutationPlan, IntersectionObserverOptions, MutationObserverOptions,ObserverMutationPlan, ObserverStore, build_intersection_entries_array,build_mutation_records_array,- callback::{- ObserverCallback, ObserverCallbackBinding, ObserverCallbackId, PreparedObserverCallback,- },+ callback::{ObserverCallback, ObserverCallbackBinding, ObserverCallbackId},intersection::{self, IntersectionCheckBatch},invoke_intersection_deliveries, invoke_mutation_deliveries, node_is_intersection_root,node_is_intersection_target,@@ -99,7 +98,7 @@ pub(crate) fn prepare_callback<'s>(scope: &mut v8::PinScope<'s, '_>,host_ptr: *mut JsContextHost,residence: ObserverCallbackResidence<'s>,-) -> Option<PreparedObserverCallback> {+) -> Option<PreparedWebIdlCallbackExecution> {let ObserverCallbackResidence {id,callback,@@ -316,7 +315,7 @@ pub(super) fn flush_mutation_observers(host.begin_mutation_observer_delivery();let store = host.observers_mut(&OBSERVER_STORE_ACCESS);store.begin_task(ObserverTask::MutationDelivery);- store.collect_mutation_deliveries(scope)+ store.collect_mutation_deliveries(scope, host_ptr)});// `access` holds no host reference here. The callback can safely call@@ -525,7 +524,7 @@ pub(super) fn flush_intersection_observers() {let deliveries = ObserverHostAccess::new(host_ptr).store(|store| {store.begin_task(ObserverTask::IntersectionDelivery);- store.collect_intersection_deliveries(scope)+ store.collect_intersection_deliveries(scope, host_ptr)});// Blink likewise removes observers from its controller pending set beforediff --git a/moli-renderer-v8/src/observer_runtime/mod.rs b/moli-renderer-v8/src/observer_runtime/mod.rsindex 90f21f3bf..c334bb92c 100644--- a/moli-renderer-v8/src/observer_runtime/mod.rs+++ b/moli-renderer-v8/src/observer_runtime/mod.rs@@ -37,7 +37,6 @@ use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};use super::{context_bootstrap::{build_dom_rect_object, throw_dom_exception_value},- host::report_event_callback_exception,native_bridge::document::{DETACHED_STATE_SLOT, detached_native_handle_for_runtime},native_bridge::{JsContextHost, build_static_handle_node_list_wrapper, callback_value_dom_handle,@@ -51,7 +50,7 @@ use super::{get_private_object, get_private_value, serialize_v8_array, serialize_v8_iter_array,set_private_value, throw_range_error, throw_type_error, v8_string, v8str,},- window_webidl_callback::WindowWebIdlCallbackFunctionOutcome,+ webidl_callback_execution::PreparedWebIdlCallbackExecution,};const MUTATION_OBSERVER_ID_SLOT: &str = "__lmMutationObserverId";@@ -758,6 +757,7 @@ impl ObserverStore {fn collect_mutation_deliveries<'s>(&mut self,scope: &mut v8::PinScope<'s, '_>,+ host_ptr: *mut JsContextHost,) -> Vec<MutationObserverDelivery<'s>> {let mut deliveries = Vec::new();let mut ids: Vec<_> = self.mutation_observers.keys().copied().collect();@@ -770,10 +770,13 @@ impl ObserverStore {continue;}let records = mem::take(&mut state.queued_records);+ let Some(callback) = state.callback.prepare(scope, host_ptr) else {+ continue;+ };let observer = v8::Local::new(scope, &state.observer);deliveries.push(MutationObserverDelivery {observer,- callback: state.callback.prepare(scope),+ callback,records,});}@@ -783,6 +786,7 @@ impl ObserverStore {fn collect_intersection_deliveries<'s>(&mut self,scope: &mut v8::PinScope<'s, '_>,+ host_ptr: *mut JsContextHost,) -> Vec<IntersectionObserverDelivery<'s>> {let mut deliveries = Vec::new();for state in self.intersection_observers.values_mut() {@@ -790,10 +794,13 @@ impl ObserverStore {continue;}let entries = mem::take(&mut state.queued_entries);+ let Some(callback) = state.callback.prepare(scope, host_ptr) else {+ continue;+ };let observer = v8::Local::new(scope, &state.observer);deliveries.push(IntersectionObserverDelivery {observer,- callback: state.callback.prepare(scope),+ callback,entries,options: state.options.clone(),});@@ -837,13 +844,13 @@ impl ObserverStore {struct MutationObserverDelivery<'s> {observer: v8::Local<'s, v8::Object>,- callback: callback::PreparedObserverCallback,+ callback: PreparedWebIdlCallbackExecution,records: Vec<QueuedMutationRecord>,}struct IntersectionObserverDelivery<'s> {observer: v8::Local<'s, v8::Object>,- callback: callback::PreparedObserverCallback,+ callback: PreparedWebIdlCallbackExecution,entries: Vec<QueuedIntersectionEntry>,options: IntersectionObserverOptions,}@@ -854,31 +861,18 @@ fn invoke_mutation_deliveries<'s>(deliveries: Vec<MutationObserverDelivery<'s>>,) {for delivery in deliveries {- if !delivery.callback.is_current(unsafe { &*host_ptr }) {+ if !delivery.callback.is_current(scope) {continue;}let records_array = build_mutation_records_array(scope, host_ptr, &delivery.records);let observer_value: v8::Local<'_, v8::Value> = delivery.observer.into();- match delivery.callback.invoke(+ delivery.callback.invoke_and_report(scope,- host_ptr,+ "mutationobserver","MutationObserver callback",observer_value,&[records_array.into(), observer_value],- ) {- WindowWebIdlCallbackFunctionOutcome::Threw(report) => {- report_event_callback_exception(- scope,- host_ptr,- "mutationobserver",- delivery.callback.relevant_identity(),- None,- &report,- );- }- WindowWebIdlCallbackFunctionOutcome::Returned- | WindowWebIdlCallbackFunctionOutcome::Retired => {}- }+ );}}@@ -888,32 +882,19 @@ fn invoke_intersection_deliveries<'s>(deliveries: Vec<IntersectionObserverDelivery<'s>>,) {for delivery in deliveries {- if !delivery.callback.is_current(unsafe { &*host_ptr }) {+ if !delivery.callback.is_current(scope) {continue;}let entries =build_intersection_entries_array(scope, host_ptr, &delivery.options, &delivery.entries);let observer_value: v8::Local<'_, v8::Value> = delivery.observer.into();- match delivery.callback.invoke(+ delivery.callback.invoke_and_report(scope,- host_ptr,+ "intersectionobserver","IntersectionObserver callback",observer_value,&[entries.into(), observer_value],- ) {- WindowWebIdlCallbackFunctionOutcome::Threw(report) => {- report_event_callback_exception(- scope,- host_ptr,- "intersectionobserver",- delivery.callback.relevant_identity(),- None,- &report,- );- }- WindowWebIdlCallbackFunctionOutcome::Returned- | WindowWebIdlCallbackFunctionOutcome::Retired => {}- }+ );}}diff --git a/moli-renderer-v8/src/webidl_callback_execution.rs b/moli-renderer-v8/src/webidl_callback_execution.rsindex 41d1a4f16..4fd33d6cf 100644--- a/moli-renderer-v8/src/webidl_callback_execution.rs+++ b/moli-renderer-v8/src/webidl_callback_execution.rs@@ -116,6 +116,17 @@ impl PreparedWebIdlCallbackExecution {})}+ pub(crate) fn relevant_context<'s>(+ &self,+ scope: &mut v8::PinScope<'s, '_>,+ ) -> v8::Local<'s, v8::Context> {+ self.callback.relevant_context(scope)+ }++ pub(crate) fn is_current(&self, scope: &mut v8::PinScope<'_, '_>) -> bool {+ self.environment.can_invoke(scope, &self.binding)+ }+/// Execute the caller's invocation and return conversion while the Web/// IDL relevant/incumbent contexts are entered. This entry point neither/// captures nor reports exceptions on behalf of the API algorithm.@@ -131,7 +142,7 @@ impl PreparedWebIdlCallbackExecution {&'a [v8::Local<'s, v8::Value>],) -> Result<R, E>,) -> WebIdlCallbackExecutionOutcome<R, E> {- if !self.environment.can_invoke(scope, &self.binding) {+ if !self.is_current(scope) {return WebIdlCallbackExecutionOutcome::Unavailable;}match invoke_webidl_callback_function(diff --git a/moli-renderer-v8/src/window_webidl_callback.rs b/moli-renderer-v8/src/window_webidl_callback.rsindex 0434f7579..f51e1f78e 100644--- a/moli-renderer-v8/src/window_webidl_callback.rs+++ b/moli-renderer-v8/src/window_webidl_callback.rs@@ -19,6 +19,7 @@ use crate::{WindowExecutionContextOwner,},v8_traced_webidl_callback::V8TracedWebIdlCallbackFunction,+ webidl_callback_execution::PreparedWebIdlCallbackExecution,};/// A callback-function residence traced through an owning V8 object.@@ -115,6 +116,31 @@ impl WindowWebIdlCallbackFunction {}}+ /// Prepare a single invocation while keeping the captured callback Realm+ /// and any API-specific owner identity authoritative.+ pub(crate) fn prepare_execution(+ &self,+ scope: &mut v8::PinScope<'_, '_>,+ host_ptr: *mut JsContextHost,+ owner_identity: Option<WindowExecutionContextIdentity>,+ ) -> Option<PreparedWebIdlCallbackExecution> {+ let Self::Live {+ callback,+ relevant_identity,+ } = self+ else {+ return None;+ };+ let callback = callback.prepare(scope);+ PreparedWebIdlCallbackExecution::window(+ scope,+ callback,+ host_ptr,+ owner_identity,+ *relevant_identity,+ )+ }+pub(crate) const fn relevant_identity(&self) -> Option<WindowExecutionContextIdentity> {match self {Self::Live {@@ -180,16 +206,6 @@ pub(crate) enum PreparedWindowWebIdlCallbackFunctionOutcome<R, E> {}impl PreparedWindowWebIdlCallbackFunction {- pub(crate) fn relevant_context<'s>(- &self,- scope: &mut v8::PinScope<'s, '_>,- ) -> Option<v8::Local<'s, v8::Context>> {- match self {- Self::Live { callback, .. } => Some(callback.relevant_context(scope)),- Self::Retired => None,- }- }-pub(crate) const fn relevant_identity(&self) -> Option<WindowExecutionContextIdentity> {match self {Self::Live {