Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / external / lib_js / error.lucb

#==============================================================================================
#
#   error - the JS::Error stand-in: Error and TypeError objects with a message
#
#   DESCRIPTION:
#       The parts of Libraries/LibJS/Runtime/Error.cpp (lines 16-31, 68-102) LibWeb reaches
#       before luce-js, as a stand-in (DESIGN.md ยง3.6; types in types_error): Error::create and
#       TypeError::create in a realm, with set_message's `message` kept on the cell. Written by
#       region p2c for the TypeError that errors a response body's stream when a load fails
#       (Fetching.cpp:2224). The prototype ([[Prototype]], the realm's %Error.prototype% or
#       %TypeError.prototype%) is a JS object of phase 3: the class names it. Stack traces
#       (ErrorData), `cause` (install_error_cause) and the other native errors are phase 3.
#
#       Copyright (c) 2020-2023, the SerenityOS and Ladybird developers.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Allocates an error of `class_info` in `realm` (`realm.create<ClassName>(realm.intrinsics().X_prototype())`).
func js_error_allocate(realm: Realm*, class_info: const ak.ClassInfo*) -> JsError*:
    let this = (JsError*)web_allocate_cell(realm, class_info)
    this.m_realm = realm
    web_initialize_cell(this.cell(), realm)
    return this

## JS_OBJECT(Error, Object)'s class_name.
func js_error_class_name_impl(this: gc.Cell*) -> ak.StringView:
    return ak.sv("Error")

## JS_OBJECT(TypeError, Error)'s class_name.
func js_type_error_class_name_impl(this: gc.Cell*) -> ak.StringView:
    return ak.sv("TypeError")

## Error::visit_edges (override): the realm (Object's shape); the message is managed memory the cell's scan keeps.
func js_error_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (JsError*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_realm)

## Ported from Libraries/LibJS/Runtime/Error.cpp:16 JS::Error::create.
pub func js_error_create(realm: Realm*) -> JsError*:
    return js_error_allocate(realm, &js_error_class)

## Ported from Libraries/LibJS/Runtime/Error.cpp:21 JS::Error::create (with a message).
pub func js_error_create_message(realm: Realm*, message: ak.Utf16String) -> JsError*:
    let error_ = js_error_create(realm)
    js_error_set_message(error_, message)
    return error_

## Ported from Libraries/LibJS/Runtime/Error.cpp:68 JS::Error::set_message: the `message` own property (writable,
## configurable).
pub func js_error_set_message(this: JsError*, message: ak.Utf16String):
    this.m_message = message

## The error's `message` own property, if set_message set one (`Get(error, "message")` before phase 3).
pub func js_error_message(this: JsError*) -> ak.Utf16String?:
    return this.m_message

## The name of the error's constructor ("Error", "TypeError"): what `Get(error, "name")` answers through its prototype.
pub func js_error_name(this: JsError*) -> ak.StringView:
    return ak.sv(this.cell().vtable.class_info.name)

## Ported from Libraries/LibJS/Runtime/Error.cpp:82 JS::TypeError::create (with a message, JS_ENUMERATE_NATIVE_ERRORS),
## as the JS::Value LibWeb rejects a promise or errors a stream with.
pub func js_type_error_create(realm: Realm*, message: ak.String) -> JsValue:
    let error_ = js_error_allocate(realm, &js_type_error_class)
    js_error_set_message(error_, ak.utf16_string_from_utf8_string(&message))
    return js_value_from_object((JsObject*)error_)