Move more bindings types to script_bindings (#35620)

* Move weak references implementation to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Move maplike/setlike definitions to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Move base error types to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Formatting.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-02-23 09:25:46 -05:00 committed by GitHub
parent 0383ba9a5b
commit 381e168877
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 435 additions and 391 deletions

View file

@ -16,6 +16,7 @@ use js::jsval::UndefinedValue;
use js::rust::wrappers::{JS_ErrorFromException, JS_GetPendingException, JS_SetPendingException};
use js::rust::{HandleObject, HandleValue, MutableHandleValue};
use libc::c_uint;
pub(crate) use script_bindings::error::*;
#[cfg(feature = "js_backtrace")]
use crate::dom::bindings::cell::DomRefCell;
@ -36,74 +37,6 @@ thread_local! {
static LAST_EXCEPTION_BACKTRACE: DomRefCell<Option<(Option<String>, String)>> = DomRefCell::new(None);
}
/// DOM exceptions that can be thrown by a native DOM method.
#[derive(Clone, Debug, MallocSizeOf)]
pub(crate) enum Error {
/// IndexSizeError DOMException
IndexSize,
/// NotFoundError DOMException
NotFound,
/// HierarchyRequestError DOMException
HierarchyRequest,
/// WrongDocumentError DOMException
WrongDocument,
/// InvalidCharacterError DOMException
InvalidCharacter,
/// NotSupportedError DOMException
NotSupported,
/// InUseAttributeError DOMException
InUseAttribute,
/// InvalidStateError DOMException
InvalidState,
/// SyntaxError DOMException
Syntax,
/// NamespaceError DOMException
Namespace,
/// InvalidAccessError DOMException
InvalidAccess,
/// SecurityError DOMException
Security,
/// NetworkError DOMException
Network,
/// AbortError DOMException
Abort,
/// TimeoutError DOMException
Timeout,
/// InvalidNodeTypeError DOMException
InvalidNodeType,
/// DataCloneError DOMException
DataClone,
/// NoModificationAllowedError DOMException
NoModificationAllowed,
/// QuotaExceededError DOMException
QuotaExceeded,
/// TypeMismatchError DOMException
TypeMismatch,
/// InvalidModificationError DOMException
InvalidModification,
/// NotReadableError DOMException
NotReadable,
/// DataError DOMException
Data,
/// OperationError DOMException
Operation,
/// TypeError JavaScript Error
Type(String),
/// RangeError JavaScript Error
Range(String),
/// A JavaScript exception is already pending.
JSFailed,
}
/// The return type for IDL operations that can throw DOM exceptions.
pub(crate) type Fallible<T> = Result<T, Error>;
/// The return type for IDL operations that can throw DOM exceptions and
/// return `()`.
pub(crate) type ErrorResult = Fallible<()>;
/// Set a pending exception for the given `result` on `cx`.
pub(crate) fn throw_dom_exception(
cx: SafeJSContext,
@ -341,15 +274,14 @@ pub(crate) fn throw_constructor_without_new(cx: SafeJSContext, name: &str) {
unsafe { throw_type_error(*cx, &error) };
}
impl Error {
pub(crate) trait ErrorToJsval {
fn to_jsval(self, cx: SafeJSContext, global: &GlobalScope, rval: MutableHandleValue);
}
impl ErrorToJsval for Error {
/// Convert this error value to a JS value, consuming it in the process.
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_jsval(
self,
cx: SafeJSContext,
global: &GlobalScope,
rval: MutableHandleValue,
) {
fn to_jsval(self, cx: SafeJSContext, global: &GlobalScope, rval: MutableHandleValue) {
match self {
Error::JSFailed => (),
_ => unsafe { assert!(!JS_IsExceptionPending(*cx)) },