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(renderer): return anyhow errors from bridge identity lookups
ldm003c8e143e6c3
diff --git a/moli-renderer-v8/src/native_bridge/document/hit_test.rs b/moli-renderer-v8/src/native_bridge/document/hit_test.rsindex cf0a1c18b..6e47628bf 100644--- a/moli-renderer-v8/src/native_bridge/document/hit_test.rs+++ b/moli-renderer-v8/src/native_bridge/document/hit_test.rs@@ -442,7 +442,7 @@ fn build_caret_position_object<'s>(fn document_runtime_and_handle_from_args<'s>(scope: &mut v8::PinScope<'s, '_>,args: &v8::FunctionCallbackArguments<'s>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {node_runtime_and_handle_from_args(scope, args).or_else(|error| {let Some(runtime_ptr) = context_host_ptr_from_global_bridge(scope) else {return Err(error);diff --git a/moli-renderer-v8/src/native_bridge/element/class_list/identity.rs b/moli-renderer-v8/src/native_bridge/element/class_list/identity.rsindex b6d029586..a61d052bd 100644--- a/moli-renderer-v8/src/native_bridge/element/class_list/identity.rs+++ b/moli-renderer-v8/src/native_bridge/element/class_list/identity.rs@@ -1,10 +1,12 @@use super::*;+use anyhow::{Context, bail};pub(super) fn class_list_runtime_handle_and_kind_from_object(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle, DomTokenListKind), String> {- let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)?;+) -> anyhow::Result<(*mut JsContextHost, DomHandle, DomTokenListKind)> {+ let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)+ .context("failed to resolve DOMTokenList wrapper")?;match handle {BridgeHandle::ClassList(handle, kind) => Ok((runtime_ptr, handle, kind)),BridgeHandle::Window@@ -12,7 +14,7 @@ pub(super) fn class_list_runtime_handle_and_kind_from_object(| BridgeHandle::Dataset(_)| BridgeHandle::Style(_)| BridgeHandle::ComputedStyle(_, _) => {- Err("wrapper did not contain a DOMTokenList identity".to_owned())+ bail!("wrapper did not contain a DOMTokenList identity")}}}diff --git a/moli-renderer-v8/src/native_bridge/element/dataset.rs b/moli-renderer-v8/src/native_bridge/element/dataset.rsindex 3177f5168..20152a34f 100644--- a/moli-renderer-v8/src/native_bridge/element/dataset.rs+++ b/moli-renderer-v8/src/native_bridge/element/dataset.rs@@ -9,6 +9,7 @@ use super::{set_live_element_attribute_ns_appending_to_current_reaction_queue,};use crate::{custom_elements, document_runtime::DomHandle, util::v8_string};+use anyhow::{Context, bail};use moli_webapi_declare::DataPropertyDescriptorDeclaration;pub(in crate::native_bridge) fn build_dom_string_map_wrapper_template<'s, 'i>(@@ -43,8 +44,9 @@ pub(in crate::native_bridge) fn build_dom_string_map_wrapper_template<'s, 'i>(fn dataset_runtime_and_handle_from_object(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {- let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)?;+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {+ let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)+ .context("failed to resolve DOMStringMap wrapper")?;match handle {BridgeHandle::Dataset(handle) => Ok((runtime_ptr, handle)),BridgeHandle::Window@@ -52,7 +54,7 @@ fn dataset_runtime_and_handle_from_object(| BridgeHandle::ClassList(_, _)| BridgeHandle::Style(_)| BridgeHandle::ComputedStyle(_, _) => {- Err("wrapper did not contain a DOMStringMap identity".to_owned())+ bail!("wrapper did not contain a DOMStringMap identity")}}}diff --git a/moli-renderer-v8/src/native_bridge/element/details_dialog.rs b/moli-renderer-v8/src/native_bridge/element/details_dialog.rsindex fabb7a902..45a400067 100644--- a/moli-renderer-v8/src/native_bridge/element/details_dialog.rs+++ b/moli-renderer-v8/src/native_bridge/element/details_dialog.rs@@ -193,7 +193,7 @@ fn dialog_close_return_value_arg<'s>(fn dialog_runtime_and_handle_from_object<'s>(scope: &mut v8::PinScope<'s, '_>,object: v8::Local<'s, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {node_runtime_and_handle_from_object_or_detached(scope, object)}diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/identity.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/identity.rsindex 6c9fc4da8..b8ec7e6b0 100644--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/identity.rs+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/identity.rs@@ -1,4 +1,5 @@use crate::document_runtime::DomHandle;+use anyhow::{Context, bail};use super::super::super::super::{BridgeHandle, JsContextHost, bridge_handle_from_object};@@ -11,8 +12,9 @@ pub(crate) enum StyleMode {pub(in crate::native_bridge::element::styles) fn style_runtime_and_handle_from_object(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle, StyleMode), String> {- let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)?;+) -> anyhow::Result<(*mut JsContextHost, DomHandle, StyleMode)> {+ let (runtime_ptr, handle) = bridge_handle_from_object(scope, object)+ .context("failed to resolve CSSStyleDeclaration wrapper")?;match handle {BridgeHandle::Style(handle) => Ok((runtime_ptr, handle, StyleMode::Inline)),BridgeHandle::ComputedStyle(handle, _) => Ok((runtime_ptr, handle, StyleMode::Computed)),@@ -20,7 +22,7 @@ pub(in crate::native_bridge::element::styles) fn style_runtime_and_handle_from_o| BridgeHandle::Node(_)| BridgeHandle::ClassList(_, _)| BridgeHandle::Dataset(_) => {- Err("wrapper did not contain a CSSStyleDeclaration identity".to_owned())+ bail!("wrapper did not contain a CSSStyleDeclaration identity")}}}diff --git a/moli-renderer-v8/src/native_bridge/helpers/identity.rs b/moli-renderer-v8/src/native_bridge/helpers/identity.rsindex 6e9a7b1de..1f160120b 100644--- a/moli-renderer-v8/src/native_bridge/helpers/identity.rs+++ b/moli-renderer-v8/src/native_bridge/helpers/identity.rs@@ -1,41 +1,43 @@use super::super::{BridgeHandle, JsContextHost, ReflectorId};use crate::util::context_host_ptr_from_global_bridge;+use anyhow::{Context, bail, ensure};fn object_reflector_id(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<ReflectorId, String> {+) -> anyhow::Result<ReflectorId> {let field_index = match object.internal_field_count() {1 => 0,// Window wrappers retain their host pointer in field 0 because Window// brand checks can cross realm boundaries.2 => 1,- _ => return Err("wrapper had an invalid reflector field count".to_owned()),+ count => bail!("wrapper has {count} internal fields, expected 1 or 2"),};let value = object.get_internal_field(scope, field_index)- .ok_or_else(|| "wrapper missing reflector field".to_owned())?;+ .with_context(|| format!("wrapper is missing reflector field {field_index}"))?;let value = v8::Local::<v8::Value>::try_from(value)- .map_err(|_| "wrapper reflector field had invalid type".to_owned())?;+ .context("wrapper reflector field is not a JS value")?;let number = value.number_value(scope)- .ok_or_else(|| "wrapper reflector field was not numeric".to_owned())?;- if !number.is_finite() || number.fract() != 0.0 || number <= 0.0 {- return Err("wrapper reflector field was invalid".to_owned());- }+ .context("wrapper reflector field is not numeric")?;+ ensure!(+ number.is_finite() && number.fract() == 0.0 && number > 0.0,+ "wrapper reflector field {number} is not a positive integer"+ );Ok(ReflectorId::from_raw(number as u64))}pub(in crate::native_bridge) fn bridge_handle_from_object(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, BridgeHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, BridgeHandle)> {let runtime_ptr = context_host_ptr_from_global_bridge(scope)- .ok_or_else(|| "wrapper context was missing its native host".to_owned())?;+ .context("current context has no JsContextHost")?;let reflector_id = object_reflector_id(scope, object)?;let handle = unsafe { &*runtime_ptr }.native_bridge().bridge_handle(reflector_id)- .ok_or_else(|| format!("missing bridge identity `{}`", reflector_id.raw()))?;+ .with_context(|| format!("no bridge identity for reflector id {}", reflector_id.raw()))?;Ok((runtime_ptr, handle))}diff --git a/moli-renderer-v8/src/native_bridge/node.rs b/moli-renderer-v8/src/native_bridge/node.rsindex 8b107a7f4..000134c17 100644--- a/moli-renderer-v8/src/native_bridge/node.rs+++ b/moli-renderer-v8/src/native_bridge/node.rs@@ -4,6 +4,7 @@ use crate::{dom::native::{DocumentType, Node, NodeType},webidl,};+use anyhow::{Context, bail};use moli_webapi_declare::WebApiFunctionTemplate;use super::super::{@@ -1264,8 +1265,9 @@ pub(crate) fn install_node_template_bindings<'s>(pub(crate) fn node_runtime_and_handle_from_object(scope: &mut v8::PinScope<'_, '_>,object: v8::Local<'_, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {- let (runtime_ptr, handle) = super::bridge_handle_from_object(scope, object)?;+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {+ let (runtime_ptr, handle) = super::bridge_handle_from_object(scope, object)+ .context("failed to resolve Node wrapper")?;match handle {super::BridgeHandle::Node(handle) => Ok((runtime_ptr, handle)),super::BridgeHandle::Window@@ -1273,7 +1275,7 @@ pub(crate) fn node_runtime_and_handle_from_object(| super::BridgeHandle::Dataset(_)| super::BridgeHandle::Style(_)| super::BridgeHandle::ComputedStyle(_, _) => {- Err("wrapper did not contain a node identity".to_owned())+ bail!("wrapper did not contain a Node identity")}}}@@ -1289,14 +1291,14 @@ pub(crate) fn object_is_node_wrapper_or_detached<'s>(pub(crate) fn node_runtime_and_handle_from_object_or_detached<'s>(scope: &mut v8::PinScope<'s, '_>,object: v8::Local<'s, v8::Object>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {if let Ok(node) = node_runtime_and_handle_from_object(scope, object) {return Ok(node);}let runtime_ptr = context_host_ptr_from_global_bridge(scope)- .ok_or_else(|| "missing native bridge host".to_owned())?;+ .context("current context has no JsContextHost")?;let handle = super::document::detached_native_handle_for_runtime(scope, runtime_ptr, object)- .ok_or_else(|| "object did not contain a node identity".to_owned())?;+ .context("object is neither a Node wrapper nor a detached node")?;Ok((runtime_ptr, handle))}@@ -1316,14 +1318,14 @@ pub(crate) fn current_or_live_delegate_node_arg_handle(pub(super) fn node_runtime_and_handle_from_args(scope: &mut v8::PinScope<'_, '_>,args: &v8::FunctionCallbackArguments<'_>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {node_runtime_and_handle_from_object(scope, args.this())}pub(super) fn node_runtime_and_handle_from_args_or_detached(scope: &mut v8::PinScope<'_, '_>,args: &v8::FunctionCallbackArguments<'_>,-) -> std::result::Result<(*mut JsContextHost, DomHandle), String> {+) -> anyhow::Result<(*mut JsContextHost, DomHandle)> {let this = v8::Global::new(scope, args.this());let this = v8::Local::new(scope, this);node_runtime_and_handle_from_object_or_detached(scope, this)