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(blob): use receiver realm intrinsic for slice results
ldm00faec8278044
diff --git a/moli-renderer-v8/src/blob.rs b/moli-renderer-v8/src/blob.rsindex 85f66fc5f..ff84c1842 100644--- a/moli-renderer-v8/src/blob.rs+++ b/moli-renderer-v8/src/blob.rs@@ -9,13 +9,11 @@ use std::sync::{Arc, OnceLock};use super::{resource_owner::{ResourceOwnerId, current_resource_owner_id},runtime::RendererStoragePartitionIdentity,- util::{get_private_value, set_private_value, throw_type_error, v8_string},+ util::{get_private_value, throw_type_error, v8_string},webidl,};const BLOB_ID_SLOT: &str = "__lmBlobId";-const BLOB_PROTOTYPE_SLOT: &str = "__lmBlobPrototype";-const BLOB_REALM_PROTOTYPE_SLOT: &str = "__lmBlobRealmPrototype";fn native_blob_line_ending() -> &'static str {if moli_browser_profile::DEFAULT_WINDOW_SURFACE_PROFILE@@ -107,14 +105,6 @@ pub(super) fn install_blob_template_bindings<'s>(}}-pub(super) fn finalize_blob_realm_bindings<'s>(- scope: &mut v8::PinScope<'s, '_>,- prototype: v8::Local<'s, v8::Object>,-) {- let global = scope.get_current_context().global(scope);- set_private_value(scope, global, BLOB_PROTOTYPE_SLOT, prototype.into());-}-fn blob_size_attribute_getter_callback<'s>(scope: &mut v8::PinScope<'s, '_>,args: v8::FunctionCallbackArguments<'s>,@@ -226,11 +216,13 @@ pub(super) fn blob_slice_callback<'s>(.map(ToOwned::to_owned).unwrap_or_default();- let prototype = blob_realm_prototype_from_object(scope, args.this())- .or_else(|| current_realm_blob_prototype(scope));- if let Some(blob) = prototype- .and_then(|prototype| build_blob_object_with_prototype(scope, sliced, mime_type, prototype))- {+ // Convert arguments in the method's realm, then allocate in the receiver's realm.+ let Some(receiver_context) = args.this().get_creation_context(scope) else {+ rv.set(v8::undefined(scope).into());+ return;+ };+ let scope = &mut v8::ContextScope::new(scope, receiver_context);+ if let Some(blob) = build_blob_object(scope, sliced, mime_type) {rv.set(blob.into());} else {rv.set(v8::undefined(scope).into());@@ -249,9 +241,6 @@ pub(super) fn init_blob_object<'s>(BlobInstanceDeclaration::new(v8::BigInt::new_from_u64(scope, blob_id)).initialize(scope, object).expect("Blob instance declaration should initialize");- if let Some(prototype) = current_realm_blob_prototype(scope) {- set_private_value(scope, object, BLOB_REALM_PROTOTYPE_SLOT, prototype.into());- }track_blob_ref_lifetime(scope, object, move || release_blob_wrapper_ref(blob_id));blob_id}@@ -261,39 +250,8 @@ pub(super) fn build_blob_object<'s>(bytes: Vec<u8>,mime_type: String,) -> Option<v8::Local<'s, v8::Object>> {- let prototype = current_realm_blob_prototype(scope)?;- build_blob_object_with_prototype(scope, bytes, mime_type, prototype)-}--fn current_realm_blob_prototype<'s>(- scope: &mut v8::PinScope<'s, '_>,-) -> Option<v8::Local<'s, v8::Object>> {- let global = scope.get_current_context().global(scope);- get_private_value(scope, global, BLOB_PROTOTYPE_SLOT)- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())- .or_else(|| {- let prototype =- crate::context_bootstrap::ensure_intrinsic_interface_prototype(scope, "Blob")- .ok()?;- finalize_blob_realm_bindings(scope, prototype);- Some(prototype)- })-}--fn blob_realm_prototype_from_object<'s>(- scope: &mut v8::PinScope<'s, '_>,- object: v8::Local<'s, v8::Object>,-) -> Option<v8::Local<'s, v8::Object>> {- get_private_value(scope, object, BLOB_REALM_PROTOTYPE_SLOT)- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())-}--fn build_blob_object_with_prototype<'s>(- scope: &mut v8::PinScope<'s, '_>,- bytes: Vec<u8>,- mime_type: String,- prototype: v8::Local<'s, v8::Object>,-) -> Option<v8::Local<'s, v8::Object>> {+ let prototype =+ crate::context_bootstrap::ensure_intrinsic_interface_prototype(scope, "Blob").ok()?;let owner_id = current_resource_owner_id(scope);let partition_id = current_blob_storage_partition_identity(scope);let blob_id = blob_store().create_blob(owner_id, partition_id, bytes, mime_type);@@ -305,7 +263,6 @@ fn build_blob_object_with_prototype<'s>(None})?;let _ = object.set_prototype(scope, prototype.into());- set_private_value(scope, object, BLOB_REALM_PROTOTYPE_SLOT, prototype.into());track_blob_ref_lifetime(scope, object, move || release_blob_wrapper_ref(blob_id));Some(object)}diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rsindex f869a6b60..ab7d6dac5 100644--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/finalize.rs@@ -9,7 +9,6 @@ enum RealmDependentFinalizer {NodeMixinUnscopables,DomExceptionPrototypeCache,CryptoSecureContextSurface,- BlobPrototypeCache,XmlHttpRequestEventTargetState,NotificationPermission,PointerEventSecureContextSurface,@@ -43,7 +42,6 @@ const REALM_DEPENDENT_FINALIZER_ALLOWLIST: &[(&str, RealmDependentFinalizer)] ="Crypto",RealmDependentFinalizer::CryptoSecureContextSurface,),- ("Blob", RealmDependentFinalizer::BlobPrototypeCache),("XMLHttpRequestEventTarget",RealmDependentFinalizer::XmlHttpRequestEventTargetState,@@ -95,9 +93,6 @@ pub(super) fn finalize_materialized_interface(RealmDependentFinalizer::CryptoSecureContextSurface => {crate::context_bootstrap::crypto::finalize_crypto_realm_bindings(scope, prototype)?;}- RealmDependentFinalizer::BlobPrototypeCache => {- crate::blob::finalize_blob_realm_bindings(scope, prototype);- }RealmDependentFinalizer::XmlHttpRequestEventTargetState => {crate::network_host::finalize_xml_http_request_event_target_realm_bindings(scope, prototype,@@ -157,7 +152,6 @@ mod tests {"CharacterData","DOMException","Crypto",- "Blob","XMLHttpRequestEventTarget","Notification","PointerEvent",diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rsindex c93029c76..998181fe7 100644--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs@@ -3158,6 +3158,8 @@ fn blob_slice_uses_receiver_realm_after_method_realm_is_detached() {const blobSlice = detachedSlice.call(new Blob(["abcdef"]), 1, 4, "Text/Custom");const fileSlice = detachedSlice.call(new File(["uvwxyz"], "sample.txt"), 2, 5);+ globalThis.__blobSliceRealmBlobResult = blobSlice;+ globalThis.__blobSliceRealmFileResult = fileSlice;globalThis.__blobSliceRealmProbe = {childDetached: iframe.contentWindow === null,blobIsMainRealmBlob: blobSlice instanceof Blob,@@ -3191,6 +3193,107 @@ fn blob_slice_uses_receiver_realm_after_method_realm_is_detached() {result,r#"{"childDetached":true,"blobIsMainRealmBlob":true,"blobPrototypeIsMainRealmBlob":true,"fileSliceIsMainRealmBlob":true,"fileSliceIsFile":false,"fileSlicePrototypeIsMainRealmBlob":true,"type":"text/custom","blobText":"bcd","fileText":"wxy"}"#);++ vm.with_default_context_scope_and_checkpoint_for_test(|scope, _runtime_ptr| {+ let context = scope.get_current_context();+ let global = context.global(scope);+ for name in ["__blobSliceRealmBlobResult", "__blobSliceRealmFileResult"] {+ let key = v8::String::new(scope, name).expect("result property name should allocate");+ let result = global+ .get(scope, key.into())+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ .expect("slice result should be an object");+ assert_eq!(result.get_creation_context(scope), Some(context));+ }+ Ok(())+ })+ .expect("slice results should be allocated in the receiver's context");+}++#[test]+fn blob_slice_allocates_in_foreign_receiver_realm() {+ let mut vm = new_parsed_test_vm(+ "https://blob-slice-foreign-receiver.test/",+ "<!doctype html><html><body></body></html>",+ );++ vm.eval(+ r#"+(() => {+ const iframe = document.createElement("iframe");+ iframe.srcdoc = "<!doctype html><html><body></body></html>";+ document.body.appendChild(iframe);+ globalThis.__blobSliceForeignFrame = iframe;+})()+"#,+ )+ .expect("foreign-realm Blob slice setup should evaluate");+ vm.drain_pending_child_frame_work_for_test();++ let result = vm+ .eval(+ r#"+(() => {+ const iframe = __blobSliceForeignFrame;+ const childBlob = iframe.contentWindow.Blob;+ const receiver = new childBlob(["abcdef"]);+ const sliced = Blob.prototype.slice.call(receiver, 1, 4);+ iframe.remove();+ const detachedSlice = Blob.prototype.slice.call(receiver, 2, 5);+ globalThis.__blobSliceForeignReceiver = receiver;+ globalThis.__blobSliceForeignResult = sliced;+ globalThis.__blobSliceForeignDetachedResult = detachedSlice;+ return JSON.stringify({+ prototypeIsChild: Object.getPrototypeOf(sliced) === childBlob.prototype,+ prototypeIsParent: Object.getPrototypeOf(sliced) === Blob.prototype,+ size: sliced.size,+ type: sliced.type,+ childDetached: iframe.contentWindow === null,+ detachedPrototypeIsChild: Object.getPrototypeOf(detachedSlice) === childBlob.prototype,+ detachedSize: detachedSlice.size+ });+})()+"#,+ )+ .expect("foreign-realm Blob slice should evaluate");+ assert_eq!(+ result,+ r#"{"prototypeIsChild":true,"prototypeIsParent":false,"size":3,"type":"","childDetached":true,"detachedPrototypeIsChild":true,"detachedSize":3}"#+ );++ vm.with_default_context_scope_and_checkpoint_for_test(|scope, _runtime_ptr| {+ let context = scope.get_current_context();+ let global = context.global(scope);+ let receiver_key = v8::String::new(scope, "__blobSliceForeignReceiver")+ .expect("receiver property name should allocate");+ let receiver = global+ .get(scope, receiver_key.into())+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ .expect("receiver should be an object");+ let result_key = v8::String::new(scope, "__blobSliceForeignResult")+ .expect("result property name should allocate");+ let sliced = global+ .get(scope, result_key.into())+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ .expect("slice result should be an object");+ let detached_result_key = v8::String::new(scope, "__blobSliceForeignDetachedResult")+ .expect("detached result property name should allocate");+ let detached_slice = global+ .get(scope, detached_result_key.into())+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())+ .expect("detached slice result should be an object");+ let receiver_context = receiver+ .get_creation_context(scope)+ .expect("receiver should have a creation context");+ assert_ne!(receiver_context, context);+ assert_eq!(sliced.get_creation_context(scope), Some(receiver_context));+ assert_eq!(+ detached_slice.get_creation_context(scope),+ Some(receiver_context)+ );+ Ok(())+ })+ .expect("slice result should be allocated in the foreign receiver's context");}#[test]