Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / bindings / exception_or_utils.lucb

#==============================================================================================
#
#   exception_or_utils - Bindings::ExceptionOrUtils: WebIDL exceptions become JS completions
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Bindings/ExceptionOrUtils.h (lines 1-114). The generated
#       bindings call an implementation through throw_dom_exception_if_needed, which turns a
#       WebIDL exception into a JS throw completion. With ExceptionOr<T> as `T!` (DESIGN.md
#       ยง2.10) the call site handles the failure and calls bind_throw_dom_exception_if_needed:
#       `let value = implementation(...) catch: try bind_throw_dom_exception_if_needed(vm)`.
#       The IsExceptionOr / ExtractExceptionOrValueType templates choose C++ return types and
#       have no counterpart. The JS completions themselves come with luce-js (phase 3).
#
#       Copyright (c) 2021-2023, the SerenityOS developers.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Bindings/ExceptionOrUtils.h:61 Web::Bindings::exception_to_throw_completion:
## a SimpleException becomes `vm.throw_completion<JS::<type>>(message)`, a DOMException the
## throw completion of its wrapper, and a JS completion stays itself.
pub func bind_exception_to_throw_completion(vm: Vm*, exception: IdlException) -> JsCompletion:
    match exception:
        .idl_simple_exception(simple_exception):
            # switch (exception.type): E(EvalError) E(RangeError) E(ReferenceError) E(TypeError) E(URIError)
            _ = simple_exception
            trap("unported (P3): Web::Bindings::exception_to_throw_completion of a SimpleException (a JS Error object)")
        .idl_dom_exception(dom_exception):
            return throw_completion(dom_exception)
        .js_completion(completion):
            return completion

## Ported from Libraries/LibWeb/Bindings/ExceptionOrUtils.h:94 Web::Bindings::throw_dom_exception_if_needed:
## the failure branch, for a call that failed with webidl_exception: its pending WebIDL
## exception becomes the pending JS completion and this fails with js_exception. (A failure that
## is already a JS completion, js_exception, is propagated unchanged by the caller's `try`.)
pub func bind_throw_dom_exception_if_needed(vm: Vm*) -> never!:
    let exception = vm_take_pending_exception(vm)
    vm_set_pending_exception(vm, .js_completion(bind_exception_to_throw_completion(vm, exception)))
    error(js_exception, "")