Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   array - JS::Array before luce-js
#
#   DESIGN.md §3.6's phase-1 stand-ins: CSSFontFeatureValuesMap keeps each feature value's list
#   as a JS::Array in its JS::Map (CSSFontFeatureValuesMap::set, to_ordered_hash_map), with
#   scripting disabled too, so before phase 3 an Array is a cell of the engine.
#
#   DESCRIPTION:
#       Port of Libraries/LibJS/Runtime/Array.cpp lines 26-64 (ArrayCreate, create_from) over
#       dense indexed storage, with the two property reads LibWeb makes of the arrays it made:
#       Object::get of "length" and Object::get_without_side_effects of an index. Other
#       objects' properties, the length property's attributes and holes are phase 3.
#
#       Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## JS_OBJECT(Array, Object)'s class_name.
func js_array_class_name_impl(this: gc.Cell*) -> ak.StringView:
    _ = this
    return ak.sv("Array")

## Ported from Libraries/LibJS/Runtime/Array.cpp:26 JS::Array::create: 10.4.2.2 ArrayCreate ( length [ , proto ] ),
## https://tc39.es/ecma262/#sec-arraycreate. Its prototype, %Array.prototype%, is a JS object (phase 3).
pub func js_array_create(realm: Realm*, length: u64) -> JsArray*!:
    # 1. If length > 2^32 - 1, throw a RangeError exception.
    if length > 4294967295:
        trap("unported (P3): JS::Array::create of a length over 2^32 - 1 (a RangeError)")

    # 2. If proto is not present, set proto to %Array.prototype%.
    # 3. Let A be MakeBasicObject(« [[Prototype]], [[Extensible]] »).
    # 4. Set A.[[Prototype]] to proto.
    # 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
    let this = (JsArray*)web_allocate_cell(realm, &js_array_class)
    this.m_realm = realm
    web_initialize_cell(this.cell(), realm)

    # 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
    for _ in 0..<length:
        ak.vector_append_t(&this.m_indexed_properties, js_undefined())

    # 7. Return A.
    return this

## Ported from Libraries/LibJS/Runtime/Array.cpp:49 JS::Array::create_from: 7.3.18 CreateArrayFromList ( elements ),
## https://tc39.es/ecma262/#sec-createarrayfromlist
pub func js_array_create_from(realm: Realm*, values: const JsValue[]) -> JsArray*:
    # 1. Let array be ! ArrayCreate(0).
    let array = js_array_create(realm, 0) else trap("MUST: Array::create")

    # 2. Let n be 0.
    # 3. For each element e of elements, do
    for n in 0..<values.length:
        # a. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(n)), e).
        js_array_create_data_property_or_throw(array, (u32)n, values[n])

        # b. Set n to n + 1.

    # 4. Return array.
    return array

## `array->create_data_property_or_throw(index, value)` (Object.cpp:271, CreateDataPropertyOrThrow) on the
## dense storage: the index is the next one or an existing one.
pub func js_array_create_data_property_or_throw(this: JsArray*, index: u32, value: JsValue):
    let size = ak.vector_size(&this.m_indexed_properties)
    if (usize)index < size:
        *ak.vector_at(&this.m_indexed_properties, (usize)index) = value
        return
    assert((usize)index == size, "the stand-in Array is dense")
    ak.vector_append_t(&this.m_indexed_properties, value)

## JS::Array's visit_edges (Object::visit_edges): its shape's realm and its indexed properties.
pub func js_array_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (JsArray*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_realm)
    for value in ak.vector_span(&this.m_indexed_properties):
        js_visit_value(visitor, value)

## Ported from Libraries/LibJS/Runtime/Object.cpp JS::Object::get, for `vm().names.length`: an Array's length.
pub func js_object_get_length(this: JsObject*) -> JsValue!:
    let array = as_if_js_array_object(this) else trap("unported (P3): JS::Object::get(vm.names.length) of an object that is not an Array")
    return js_value_from_f64((f64)ak.vector_size(&array.m_indexed_properties))

## Ported from Libraries/LibJS/Runtime/Object.cpp JS::Object::get_without_side_effects, for an index of an Array.
pub func js_object_get_without_side_effects_index(this: JsObject*, index: usize) -> JsValue:
    let array = as_if_js_array_object(this) else trap("unported (P3): JS::Object::get_without_side_effects of an object that is not an Array")
    if index < ak.vector_size(&array.m_indexed_properties):
        return *ak.vector_at(&array.m_indexed_properties, index)
    return js_undefined()

## `as_if<JS::Array>(object)`.
func as_if_js_array_object(object: JsObject*) -> JsArray*?:
    let cell = (gc.Cell*)object
    if cell.vtable.class_info == &js_array_class:
        return (JsArray*)object
    return none