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(realm): remove duplicate interface prototype caches
ldm0614aa1fb33cc
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rsindex c69b7c460..e7c907295 100644--- a/moli-renderer-v8/src/context_bootstrap.rs+++ b/moli-renderer-v8/src/context_bootstrap.rs@@ -222,8 +222,6 @@ pub(crate) use self::canvas::{build_offscreen_canvas_object, build_webgl_context_object, build_webgl2_context_object,canvas_like_to_data_url, reset_html_canvas_backing_store_for_dimension_assignment,};-#[cfg(test)]-pub(crate) use self::constructors::finalize_dom_exception_realm_bindings;use self::constructors::illegal_constructor_callback;pub(crate) use self::constructors::{dom_exception_clone_fields, ensure_dom_implementation_singleton, new_dom_error_value,diff --git a/moli-renderer-v8/src/context_bootstrap/constructors.rs b/moli-renderer-v8/src/context_bootstrap/constructors.rsindex a903aba96..d5e3fb731 100644--- a/moli-renderer-v8/src/context_bootstrap/constructors.rs+++ b/moli-renderer-v8/src/context_bootstrap/constructors.rs@@ -34,9 +34,8 @@ pub(super) use document_nodes::{};pub(crate) use dom_exception::{dom_error_constructor_callback, dom_exception_clone_fields, dom_exception_constructor_callback,- finalize_dom_exception_realm_bindings, initialize_websocket_error,- install_dom_exception_template_bindings, new_dom_error_value, new_dom_exception_value,- new_most_derived_dom_exception_value, new_quota_exceeded_error_value,+ initialize_websocket_error, install_dom_exception_template_bindings, new_dom_error_value,+ new_dom_exception_value, new_most_derived_dom_exception_value, new_quota_exceeded_error_value,new_websocket_error_value, quota_exceeded_error_clone_fields,quota_exceeded_error_constructor_callback, throw_dom_exception_value,websocket_error_close_info,diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs b/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rsindex 9e6f4252f..dda15594d 100644--- a/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs+++ b/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs@@ -14,11 +14,6 @@ const QUOTA_EXCEEDED_ERROR_REQUESTED_SLOT: &str = "__lmQuotaExceededErrorRequestconst WEBSOCKET_ERROR_CLOSE_CODE_SLOT: &str = "__lmWebSocketErrorCloseCode";const WEBSOCKET_ERROR_REASON_SLOT: &str = "__lmWebSocketErrorReason";-#[derive(Debug)]-struct DomExceptionPrototypeSlot {- prototype: v8::Global<v8::Object>,-}-#[derive(webidl::WebIdlArgs)]#[webidl(prefix = "DOMException")]struct DomExceptionConstructorArgs {@@ -196,17 +191,6 @@ pub(crate) fn install_dom_exception_template_bindings<'s>(}}-pub(crate) fn finalize_dom_exception_realm_bindings(- scope: &mut v8::PinScope<'_, '_>,- prototype: v8::Local<'_, v8::Object>,-) {- let _ = scope- .get_current_context()- .set_slot(std::rc::Rc::new(DomExceptionPrototypeSlot {- prototype: v8::Global::new(scope, prototype),- }));-}-fn dom_exception_receiver<'s>(scope: &mut v8::PinScope<'s, '_>,receiver: v8::Local<'s, v8::Object>,@@ -541,13 +525,14 @@ pub(crate) fn new_dom_exception_value<'s>(message: &str,name: &str,) -> v8::Local<'s, v8::Value> {- if let Some(prototype) = scope- .get_current_context()- .get_slot::<DomExceptionPrototypeSlot>()+ if let Some(prototype) =+ crate::context_bootstrap::exposed_interfaces::materialized_intrinsic_interface_prototype(+ scope,+ "DOMException",+ ){let exception = v8::Object::new(scope);initialize_dom_exception(scope, exception, message, name);- let prototype = v8::Local::new(scope, &prototype.prototype);let _ = exception.set_prototype(scope, prototype.into());return exception.into();}diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rsindex ab7d6dac5..71389fde6 100644--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs@@ -7,7 +7,6 @@ use super::materialize::{#[derive(Clone, Copy, Debug, Eq, PartialEq)]enum RealmDependentFinalizer {NodeMixinUnscopables,- DomExceptionPrototypeCache,CryptoSecureContextSurface,XmlHttpRequestEventTargetState,NotificationPermission,@@ -34,10 +33,6 @@ const REALM_DEPENDENT_FINALIZER_ALLOWLIST: &[(&str, RealmDependentFinalizer)] =),// These entries install private realm state, context-conditional surface,// or constructor data whose JavaScript identity must be realm-local.- (- "DOMException",- RealmDependentFinalizer::DomExceptionPrototypeCache,- ),("Crypto",RealmDependentFinalizer::CryptoSecureContextSurface,@@ -85,11 +80,6 @@ pub(super) fn finalize_materialized_interface(RealmDependentFinalizer::NodeMixinUnscopables => {finalize_node_mixin_unscopables(scope, prototype);}- RealmDependentFinalizer::DomExceptionPrototypeCache => {- crate::context_bootstrap::constructors::finalize_dom_exception_realm_bindings(- scope, prototype,- );- }RealmDependentFinalizer::CryptoSecureContextSurface => {crate::context_bootstrap::crypto::finalize_crypto_realm_bindings(scope, prototype)?;}@@ -150,7 +140,6 @@ mod tests {"Element","DocumentType","CharacterData",- "DOMException","Crypto","XMLHttpRequestEventTarget","Notification",diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/install.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/install.rsindex c434b9dfa..64350bb01 100644--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/install.rs+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/install.rs@@ -11,9 +11,8 @@ use super::template_registry::ExposedInterfaceTemplateRegistry;use crate::context_bootstrap::specs::ConstructorSpec;use crate::util::{constructor_object, constructor_prototype_object, initialize_intrinsic_interface_registry,- register_intrinsic_interface, register_public_interface_object,- registered_intrinsic_constructor, registered_intrinsic_prototype,- registered_public_interface_object, v8str,+ register_intrinsic_interface, registered_intrinsic_constructor, registered_intrinsic_prototype,+ v8str,};const LEGACY_WINDOW_INTERFACE_ALIASES: &[(&str, &str)] = &[@@ -191,18 +190,9 @@ pub(crate) fn capture_eager_intrinsic_interfaces<'s>(}if let Some(constructor) = captured_constructor {let prototype = captured_prototype.expect("captured intrinsic pair was validated");- if registered_public_interface_object(scope, global, metadata.name).is_none()- && !register_public_interface_object(scope, global, metadata.name, constructor)- {- return Err(anyhow!(- "failed to capture eager public interface `{}`",- metadata.name- ));- }- let public_interface = registered_public_interface_object(scope, global, metadata.name)- .ok_or_else(|| {- anyhow!("captured public interface `{}` is missing", metadata.name)- })?;+ let public_interface = realm+ .public_interface(scope, metadata.id)+ .unwrap_or(constructor);realm.register_objects(scope, metadata.id, constructor, prototype, public_interface)?;realm.set_state(metadata.id, RealmInterfaceState::Ready)?;continue;@@ -232,12 +222,6 @@ pub(crate) fn capture_eager_intrinsic_interfaces<'s>(metadata.name));}- if !register_public_interface_object(scope, global, metadata.name, constructor) {- return Err(anyhow!(- "failed to capture eager public interface `{}`",- metadata.name- ));- }realm.register_objects(scope, metadata.id, constructor, prototype, constructor)?;realm.set_state(metadata.id, RealmInterfaceState::Ready)?;}diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rsindex 1d7f37dd3..eba493a6a 100644--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs@@ -11,9 +11,8 @@ use crate::context_bootstrap::runtime_state::set_interface_prototype_constructoruse crate::context_bootstrap::shared::throw_error;use crate::context_bootstrap::specs::ConstructorKind;use crate::util::{- constructor_prototype_object, register_intrinsic_interface, register_public_interface_object,- registered_intrinsic_constructor, registered_intrinsic_prototype,- registered_public_interface_object, v8str,+ constructor_prototype_object, register_intrinsic_interface, registered_intrinsic_constructor,+ registered_intrinsic_prototype, v8str,};pub(super) fn exposed_interface_lazy_getter<'s>(@@ -98,7 +97,6 @@ pub(super) fn materialize_interface<'s>(if registered_intrinsic_constructor(scope, global, metadata.name).is_some()|| registered_intrinsic_prototype(scope, global, metadata.name).is_some()- || registered_public_interface_object(scope, global, metadata.name).is_some(){return Err(anyhow!("uninitialized interface `{}` has partial registry state",@@ -176,6 +174,19 @@ pub(crate) fn ensure_intrinsic_interface_prototype<'s>(.ok_or_else(|| anyhow!("intrinsic prototype `{name}` is missing after materialization"))}+/// Reads an already registered prototype without materializing an interface.+/// This is useful while creating an exception for a bootstrap failure.+pub(crate) fn materialized_intrinsic_interface_prototype<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ name: &str,+) -> Option<v8::Local<'s, v8::Object>> {+ let registry = ExposedInterfaceTemplateRegistry::current(scope)?;+ let id = registry.id_by_name(name)?;+ IntrinsicInterfaceRegistry::for_current_context(scope, registry.len())+ .ok()?+ .prototype(scope, id)+}+fn materialize_uninitialized_interface<'s>(scope: &mut v8::PinScope<'s, '_>,registry: &Rc<ExposedInterfaceTemplateRegistry>,@@ -273,12 +284,6 @@ fn materialize_uninitialized_interface<'s>(}_ => constructor.into(),};- if !register_public_interface_object(scope, global, metadata.name, public_interface) {- return Err(anyhow!(- "failed to register public interface object `{}`",- metadata.name- ));- }realm.register_objects(scope,id,diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rsindex eb9e78fc3..d97999d5a 100644--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs@@ -22,7 +22,7 @@ pub(crate) use install::{};pub(crate) use materialize::{build_intrinsic_interface_instance, ensure_intrinsic_interface_constructor,- ensure_intrinsic_interface_prototype,+ ensure_intrinsic_interface_prototype, materialized_intrinsic_interface_prototype,};pub(crate) use metadata::RealmKind;pub(in crate::context_bootstrap) use metadata::TemplateBuildProfile;diff --git a/moli-renderer-v8/src/context_bootstrap/worker_host/tests.rs b/moli-renderer-v8/src/context_bootstrap/worker_host/tests.rsindex 92226bc07..f19b71f15 100644--- a/moli-renderer-v8/src/context_bootstrap/worker_host/tests.rs+++ b/moli-renderer-v8/src/context_bootstrap/worker_host/tests.rs@@ -100,18 +100,12 @@ fn install_dom_exception<'s>(scope: &mut v8::PinScope<'s, '_>, global: v8::Locallet constructor = template.get_function(scope).expect("DOMException constructor");- let prototype_key = v8::String::new(scope, "prototype").unwrap();- let prototype = constructor- .get(scope, prototype_key.into())- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())- .expect("DOMException prototype");let _ = global.define_own_property(scope,v8::String::new(scope, "DOMException").unwrap().into(),constructor.into(),v8::PropertyAttribute::DONT_ENUM,);- crate::context_bootstrap::finalize_dom_exception_realm_bindings(scope, prototype);}/// Helper callback: __drainWorkerMessages(workerObj) → booldiff --git a/moli-renderer-v8/src/script_vm/tests/lazy_storage.rs b/moli-renderer-v8/src/script_vm/tests/lazy_storage.rsindex b76aa6b25..c51c32c51 100644--- a/moli-renderer-v8/src/script_vm/tests/lazy_storage.rs+++ b/moli-renderer-v8/src/script_vm/tests/lazy_storage.rs@@ -585,6 +585,34 @@ fn lazy_dom_exception_uses_original_error_prototype_after_public_override() {);}+#[test]+fn native_dom_exception_uses_registered_prototype_after_public_override() {+ let mut vm = new_storage_test_vm("https://native-dom-exception-intrinsic.test/");+ vm.eval("void DOMException; globalThis.DOMException = function FakeDOMException() {}")+ .expect("DOMException override should evaluate");++ vm.with_default_context_scope_and_checkpoint_for_test(|scope, _host_ptr| {+ let context = scope.get_current_context();+ let intrinsic =+ crate::context_bootstrap::ensure_intrinsic_interface_prototype(scope, "DOMException")+ .expect("DOMException intrinsic should be registered");+ let exception = crate::context_bootstrap::new_dom_exception_value(+ scope,+ "native exception",+ "DataError",+ );+ let exception = v8::Local::<v8::Object>::try_from(exception)+ .expect("native DOMException should be an object");+ let prototype = exception+ .get_prototype(scope)+ .expect("native DOMException should have a prototype");+ assert!(prototype.strict_equals(intrinsic.into()));+ assert_eq!(exception.get_creation_context(scope), Some(context));+ Ok(())+ })+ .expect("native exception should use the realm registry");+}+#[test]fn illegal_storage_receivers_do_not_materialize_wrappers_or_opfs_state() {let mut vm = new_storage_test_vm("https://lazy-illegal-storage-receivers.test/");diff --git a/moli-renderer-v8/src/util.rs b/moli-renderer-v8/src/util.rsindex 8d5d8e05d..62b4a301f 100644--- a/moli-renderer-v8/src/util.rs+++ b/moli-renderer-v8/src/util.rs@@ -12,8 +12,7 @@ pub use moli_v8_util::{object_number_property, object_own_static_bool_property, object_own_static_property_as_array,object_own_static_string_property, object_property_as_array, object_property_as_object,object_string_property, private_key, register_intrinsic_interface,- register_public_interface_object, registered_intrinsic_constructor,- registered_intrinsic_prototype, registered_public_interface_object, set_null_prototype,+ registered_intrinsic_constructor, registered_intrinsic_prototype, set_null_prototype,set_private_value, set_symbol_to_string_tag, throw_range_error, throw_type_error,v8_json_parse, v8_string, v8str, walk_object_chain,};diff --git a/moli-v8-util/src/constructors.rs b/moli-v8-util/src/constructors.rsindex 55a2f4960..7184d86f2 100644--- a/moli-v8-util/src/constructors.rs+++ b/moli-v8-util/src/constructors.rs@@ -6,7 +6,6 @@ use crate::symbols::{get_private_object, set_private_value};const INTRINSIC_CONSTRUCTORS_SLOT: &str = "__moliIntrinsicConstructors";const INTRINSIC_PROTOTYPES_SLOT: &str = "__moliIntrinsicPrototypes";-const PUBLIC_INTERFACE_OBJECTS_SLOT: &str = "__moliPublicInterfaceObjects";fn intrinsic_registry_object<'s>(scope: &mut PinScope<'s, '_>,@@ -34,7 +33,6 @@ pub fn initialize_intrinsic_interface_registry<'s>() {let _ = intrinsic_registry_object(scope, global, INTRINSIC_CONSTRUCTORS_SLOT);let _ = intrinsic_registry_object(scope, global, INTRINSIC_PROTOTYPES_SLOT);- let _ = intrinsic_registry_object(scope, global, PUBLIC_INTERFACE_OBJECTS_SLOT);}fn define_intrinsic<'s>(@@ -96,29 +94,6 @@ pub fn register_intrinsic_interface<'s>(true}-/// Records the value returned by the exposed-interface lazy property.-///-/// Most interfaces expose their intrinsic constructor directly. HTML element-/// interfaces use a callable Proxy around that constructor to enforce the-/// custom-element early-sanity check, so the public value is tracked-/// separately from the trusted constructor used for inheritance and wrapper-/// creation.-pub fn register_public_interface_object<'s>(- scope: &mut PinScope<'s, '_>,- global: Local<'s, Object>,- name: &str,- value: Local<'s, Object>,-) -> bool {- let objects = intrinsic_registry_object(scope, global, PUBLIC_INTERFACE_OBJECTS_SLOT);- let Some(key) = v8_string(scope, name) else {- return false;- };- if objects.has_own_property(scope, key.into()) != Some(false) {- return false;- }- define_intrinsic(scope, objects, name, value)-}-fn registered_intrinsic<'s>(scope: &mut PinScope<'s, '_>,global: Local<'s, Object>,@@ -148,14 +123,6 @@ pub fn registered_intrinsic_prototype<'s>(registered_intrinsic(scope, global, INTRINSIC_PROTOTYPES_SLOT, name)}-pub fn registered_public_interface_object<'s>(- scope: &mut PinScope<'s, '_>,- global: Local<'s, Object>,- name: &str,-) -> Option<Local<'s, Object>> {- registered_intrinsic(scope, global, PUBLIC_INTERFACE_OBJECTS_SLOT, name)-}-pub fn constructor_object<'s>(scope: &mut PinScope<'s, '_>,global: Local<'s, Object>,