Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / web_idl / exception_or.lucb

#==============================================================================================
#
#   exception_or - WebIDL::ExceptionOr: throwing, catching and printing WebIDL exceptions
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/WebIDL/ExceptionOr.h (lines 1-192). `WebIDL::ExceptionOr<T>`
#       is `T!` (DESIGN.md ยง2.10): `return exception;` stores the WebIDL::Exception (a
#       SimpleException, a DOMException or a JS completion: IdlException) as the VM's pending
#       exception and fails with webidl_exception; `TRY(x)` is `try x`; the caller that handles
#       a failure (`if (result.is_exception())`, `result.exception()`, `release_error()`) takes
#       the exception with idl_take_pending_exception. `MUST(x)` is `x else trap(...)` and
#       `release_value_but_fixme_should_propagate_errors()` is `x else trap("FIXME: ...")`, so the
#       ExceptionOr class itself has no counterpart. The AK formatters of SimpleException and
#       Exception are the two functions at the end.
#
#       Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: ExceptionOr =============================================================================

## `return exception;` in a function returning WebIDL::ExceptionOr<T> (ExceptionOr.h:66-89, the
## constructors from a DOMException, a SimpleException, a JS::Completion or an Exception): the
## exception becomes the VM's pending exception and the function fails with webidl_exception.
## Callers write `return try idl_throw(.idl_dom_exception(idl_not_found_error_create(realm, message)))`.
pub func idl_throw(exception: IdlException) -> never!:
    vm_set_pending_exception(vm_the(), exception)
    error(webidl_exception, "")

## `return WebIDL::SimpleException { type, message };`: idl_throw of a simple exception.
pub func idl_throw_simple(type_: IdlSimpleExceptionType, message: ak.StringOrStringView) -> never!:
    try idl_throw(.idl_simple_exception(IdlSimpleException(type_ = type_, message = message)))

## `result.exception()` / `result.release_error()` of a failed ExceptionOr (ExceptionOr.h:112,
## 130): the pending exception, taken out of the VM.
pub func idl_take_pending_exception() -> IdlException:
    return vm_take_pending_exception(vm_the())

# mark: Formatters ==============================================================================

## Ported from Libraries/LibWeb/WebIDL/ExceptionOr.h:149 AK::Formatter<SimpleException>::format:
## a simple exception formats as its message (answered as a String: the view of a short String
## would point into a copy).
pub func idl_simple_exception_message(exception: const IdlSimpleException*) -> ak.String:
    match exception.message:
        .string(message):
            return message
        .string_view(message):
            return ak.string_from_utf8(message) else trap("MUST: String::from_utf8")

## Ported from Libraries/LibWeb/WebIDL/ExceptionOr.h:165 AK::Formatter<Exception>::format: the
## text `"{}"` formats an exception to.
pub func idl_exception_to_string(exception: IdlException) -> ak.String:
    match exception:
        .idl_simple_exception(simple_exception):
            return idl_simple_exception_message(&simple_exception)
        .idl_dom_exception(dom_exception):
            return idl_dom_exception_to_string(dom_exception)
        .js_completion(completion):
            # VERIFY(completion.is_error()); the message property of the thrown object, or the
            # value as a string: JS values (phase 3).
            _ = completion
            trap("unported (P3): AK::Formatter<WebIDL::Exception> of a JS completion")