Skip to content

Truly independent web browser

10.4 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

use core::ops::Deref;

use ak::Utf16String;
use libjs_runtime_macros::Trace;

use super::completion::{Throw, ThrowCompletionOr};
use super::error_types::ErrorType;
use crate::gc::class::{Class, ExternalMemorySize, GcCell, define_cell, downcast_ref};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::aggregate_error::AggregateError;
use crate::runtime::error_data::{CompactTraceback, ErrorData};
use crate::runtime::function_object::FunctionObject;
use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, ObjectMethods};
use crate::runtime::primitive_string::PrimitiveString;
use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::realm::Realm;
use crate::runtime::suppressed_error::SuppressedError;
use crate::utf16::Utf16Display;

/// The constructors of the errors the runtime and its embedder throw: %Error%, the NativeError constructors,
/// %AggregateError% and %SuppressedError%.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind {
    Error,
    EvalError,
    InternalError,
    RangeError,
    ReferenceError,
    SyntaxError,
    TypeError,
    URIError,
    AggregateError,
    SuppressedError,
}

impl ErrorKind {
    /// T::create(realm) for the error class T this kind names: a new error of this kind in `realm`, without a
    /// "message".
    pub fn create_without_message(self, vm: &Vm, realm: Gc<Realm>) -> Gc<Error> {
        match self {
            Self::Error => Error::create(vm, realm),
            Self::EvalError => EvalError::create(vm, realm).upcast(),
            Self::InternalError => InternalError::create(vm, realm).upcast(),
            Self::RangeError => RangeError::create(vm, realm).upcast(),
            Self::ReferenceError => ReferenceError::create(vm, realm).upcast(),
            Self::SyntaxError => SyntaxError::create(vm, realm).upcast(),
            Self::TypeError => TypeError::create(vm, realm).upcast(),
            Self::URIError => URIError::create(vm, realm).upcast(),
            Self::AggregateError => AggregateError::create(vm, realm).upcast(),
            Self::SuppressedError => SuppressedError::create(vm, realm).upcast(),
        }
    }

    /// T::create(realm, message) for the error class T this kind names: a new error of this kind in `realm`, whose
    /// "message" is `message`.
    pub fn create(self, vm: &Vm, realm: Gc<Realm>, message: Utf16String) -> Gc<Error> {
        let error = self.create_without_message(vm, realm);
        error.set_message(vm, message);
        error
    }
}

impl Vm {
    /// 5.2.3.2 Throw an Exception, https://tc39.es/ecma262/#sec-throw-an-exception
    #[cold]
    pub fn throw_completion<T>(
        &self,
        kind: ErrorKind,
        error_type: ErrorType,
        arguments: &[&dyn Utf16Display],
    ) -> ThrowCompletionOr<T> {
        self.throw_completion_with_utf16_message(kind, error_type.message(arguments))
    }

    /// Throws a new error of `kind` with `message`.
    #[cold]
    pub fn throw_completion_with_message<T>(&self, kind: ErrorKind, message: String) -> ThrowCompletionOr<T> {
        self.throw_completion_with_utf16_message(kind, Utf16String::from_utf8(&message))
    }

    /// Throws a new error of `kind` with `message`, for the messages that can hold any code unit of a string.
    #[cold]
    pub fn throw_completion_with_utf16_message<T>(
        &self,
        kind: ErrorKind,
        message: Utf16String,
    ) -> ThrowCompletionOr<T> {
        let realm = if kind == ErrorKind::TypeError {
            self.type_error_realm()
        } else {
            self.current_realm()
        };
        let realm = realm.expect("an error is thrown in an execution context with a realm");
        let completion = Value::from_object(kind.create(self, realm, message));
        crate::embedding::completion::log_exception_if_enabled(self, completion);
        Err(Throw::new(completion))
    }
}

/// The Error objects, which have an [[ErrorData]] internal slot. The NativeError objects extend it.
#[repr(C)]
#[derive(Trace)]
pub struct Error {
    base: Object,
    error_data: ErrorData,
}

static ERROR_METHODS: ObjectMethods = ObjectMethods {
    initialize: Error::initialize,
    ..ORDINARY_OBJECT_METHODS
};

define_cell!(
    Error,
    Object,
    extends: [Object],
    methods: ERROR_METHODS,
    external_memory_size: external_memory_size
);

impl ExternalMemorySize for Error {
    fn external_memory_size(&self) -> usize {
        self.base.external_memory_size() + self.error_data.external_memory_size()
    }
}

impl Deref for Error {
    type Target = Object;

    fn deref(&self) -> &Object {
        &self.base
    }
}

/// The error_data method of ordinary objects, which finds the slot of the Error objects among them.
pub fn error_data_of_error(object: &Object) -> Option<&ErrorData> {
    downcast_ref::<_, Error>(object).map(|error| &error.error_data)
}

impl Error {
    /// An error with `prototype`, for `class`, which is Error or a class that extends it.
    pub fn new(vm: &Vm, realm: Gc<Realm>, class: &'static Class, prototype: Gc<Object>) -> Error {
        Error {
            base: Object::new_with_prototype(
                vm,
                class,
                realm,
                Some(prototype),
                MayInterfereWithIndexedPropertyAccess::No,
            ),
            error_data: ErrorData::new(vm),
        }
    }

    /// An error an Error constructor is creating for `new_target`: its stack leaves out the frames of the constructors,
    /// as ErrorData::new_in_constructor() says.
    pub fn new_in_constructor(
        vm: &Vm,
        realm: Gc<Realm>,
        class: &'static Class,
        prototype: Gc<Object>,
        new_target: Gc<FunctionObject>,
    ) -> Error {
        Error {
            base: Object::new_with_prototype(
                vm,
                class,
                realm,
                Some(prototype),
                MayInterfereWithIndexedPropertyAccess::No,
            ),
            error_data: ErrorData::new_in_constructor(vm, new_target),
        }
    }

    fn initialize(object: &Object, vm: &Vm, _realm: Gc<Realm>) {
        let error_data = error_data_of_error(object).expect("an Error has error data");
        vm.heap()
            .did_allocate_external_memory(error_data.traceback_storage_size());
    }

    pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<Error> {
        realm.create_object(vm, |_| {
            Error::new(vm, realm, Error::CLASS, realm.intrinsics().error_prototype(vm))
        })
    }

    /// An error with `prototype`, as OrdinaryCreateFromConstructor creates one for an error type of the embedder,
    /// whose prototype the realm's intrinsics do not have.
    pub fn create_with_prototype(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>) -> Gc<Error> {
        realm.create_object(vm, |_| Error::new(vm, realm, Error::CLASS, prototype))
    }

    pub fn create_with_message(vm: &Vm, realm: Gc<Realm>, message: Utf16String) -> Gc<Error> {
        let error = Error::create(vm, realm);
        error.set_message(vm, message);
        error
    }

    pub fn stack_string(&self, compact: CompactTraceback) -> Utf16String {
        self.error_data.stack_string(compact)
    }

    // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/ecma262/#sec-installerrorcause
    pub fn install_error_cause(&self, vm: &Vm, options: Value) -> ThrowCompletionOr<()> {
        // 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
        if options.is_object() && options.as_object().has_property(vm, &vm.names.cause)? {
            // a. Let cause be ? Get(options, "cause").
            let cause = options.as_object().get(vm, &vm.names.cause)?;

            // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
            self.create_non_enumerable_data_property_or_throw(vm, &vm.names.cause, cause);
        }

        // 2. Return unused.
        Ok(())
    }

    pub fn set_message(&self, vm: &Vm, message: Utf16String) {
        let attributes = PropertyAttributes::new(Attribute::WRITABLE | Attribute::CONFIGURABLE);
        self.define_direct_property(
            vm,
            &vm.names.message,
            Value::from_string(PrimitiveString::create(vm, message)),
            attributes,
        );
    }
}

// NOTE: Making these inherit from Error is not required by the spec but
//       our way of implementing the [[ErrorData]] internal slot, which is
//       used in Object.prototype.toString().
macro_rules! define_native_errors {
    ($($class:ident: $prototype:ident;)*) => {
        $(
            #[repr(C)]
            #[derive(Trace)]
            pub struct $class {
                base: Error,
            }

            define_cell!($class, Object, extends: [Error, Object]);

            impl Deref for $class {
                type Target = Error;

                fn deref(&self) -> &Error {
                    &self.base
                }
            }

            impl $class {
                pub fn new(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>) -> $class {
                    $class {
                        base: Error::new(vm, realm, Self::CLASS, prototype),
                    }
                }

                pub fn new_in_constructor(
                    vm: &Vm,
                    realm: Gc<Realm>,
                    prototype: Gc<Object>,
                    new_target: Gc<FunctionObject>,
                ) -> $class {
                    $class {
                        base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype, new_target),
                    }
                }

                pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<$class> {
                    realm.create_object(vm, |_| $class::new(vm, realm, realm.intrinsics().$prototype(vm)))
                }

                pub fn create_with_message(vm: &Vm, realm: Gc<Realm>, message: Utf16String) -> Gc<$class> {
                    let error = $class::create(vm, realm);
                    error.set_message(vm, message);
                    error
                }
            }
        )*
    };
}

define_native_errors! {
    EvalError: eval_error_prototype;
    InternalError: internal_error_prototype;
    RangeError: range_error_prototype;
    ReferenceError: reference_error_prototype;
    SyntaxError: syntax_error_prototype;
    TypeError: type_error_prototype;
    URIError: uri_error_prototype;
}