Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / css_font_feature_values_map.lucb

#==============================================================================================
#
#   css_font_feature_values_map - CSS::CSSFontFeatureValuesMap: the named values of one
#                                 feature type in an @font-feature-values rule
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSSFontFeatureValuesMap.cpp (lines 1-106) and the inline members of
#       CSSFontFeatureValuesMap.h (lines 17-39). Ladybird keeps the entries in a JS::Map (the
#       maplike of the IDL interface); the JS::Map and JS::Array operations are LibJS's, which
#       phase 3 provides (their stubs are in stubs/stub_closure).
#
#       Copyright (c) 2026, Callum Law <callumlaw1709@outlook.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.h:23 Web::CSS::CSSFontFeatureValuesMap::map_entries.
pub func css_font_feature_values_map_map_entries(this: CssFontFeatureValuesMap*) -> JsMap*:
    return this.m_map_entries

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:18 Web::CSS::CSSFontFeatureValuesMap::create.
pub func css_font_feature_values_map_create(realm: Realm*, max_value_count: usize, parent_rule: CssFontFeatureValuesRule*) -> CssFontFeatureValuesMap*:
    return realm_create_css_font_feature_values_map(realm, realm, max_value_count, parent_rule)

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:22 Web::CSS::CSSFontFeatureValuesMap::CSSFontFeatureValuesMap.
pub func css_font_feature_values_map_construct(this: CssFontFeatureValuesMap*, realm: Realm*, max_value_count: usize, parent_rule: CssFontFeatureValuesRule*):
    bind_platform_object_construct_realm(this.bind_platform_object(), realm)
    this.m_map_entries = js_map_create(realm)
    this.m_max_value_count = max_value_count
    this.m_parent_rule = parent_rule

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:30 Web::CSS::CSSFontFeatureValuesMap::set.
pub func css_font_feature_values_map_set(this: CssFontFeatureValuesMap*, feature_value_name: ak.String, values: ak.U32OrVectorU32) -> !:
    let realm = bind_platform_object_realm(this.bind_platform_object())

    # https://drafts.csswg.org/css-fonts-4/#cssfontfeaturevaluesmap
    # The CSSFontFeatureValuesMap interface uses the default map class methods but the set method has different
    # behavior. It takes a sequence of unsigned integers and associates it with a given featureValueName. The method
    # behaves the same as the default map class method except that

    # a single unsigned long value is treated as a sequence of a single value.
    var value_vector: ak.Vector[u32]
    match values:
        .u32_(single_value):
            ak.vector_append_t(&value_vector, single_value)
        .vector_u32(vector):
            value_vector = vector

    # The method throws an exception if an invalid number of values is passed in.
    if ak.vector_is_empty(&value_vector):
        try idl_throw(.idl_dom_exception(idl_invalid_access_error_create(realm, ak.utf16_string_from_str("CSSFontFeatureValuesMap.set requires at least one value."))))

    # If the associated feature value block only allows a limited number of values, the set method throws an
    # InvalidAccessError exception when the input sequence to set contains more than the limited number of values. See
    # the description of multi-valued feature value definitions for details on the maximum number of values allowed for
    # a given type of feature value block.
    if ak.vector_size(&value_vector) > this.m_max_value_count:
        let message = css_string_formatted("CSSFontFeatureValuesMap.set only allows a maximum of {} values for the associated feature", [ak.arg_u64((u64)this.m_max_value_count)])
        try idl_throw(.idl_dom_exception(idl_invalid_access_error_create(realm, ak.utf16_string_from_utf8_string(&message))))

    var wrapped_values: ak.Vector[JsValue]
    ak.vector_ensure_capacity(&wrapped_values, ak.vector_size(&value_vector))

    for value in ak.vector_span(&value_vector):
        ak.vector_append_t(&wrapped_values, js_value_from_f64((f64)value))

    let array = js_array_create_from(realm, ak.vector_span_const(&wrapped_values))
    js_map_map_set(this.m_map_entries, js_primitive_string_create_string(js_cell_vm(this.js_cell()), feature_value_name), js_value_from_object((JsObject*)array))

    css_rule_clear_caches(this.m_parent_rule.css_rule())

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:66 Web::CSS::CSSFontFeatureValuesMap::on_map_modified_from_js.
pub func css_font_feature_values_map_on_map_modified_from_js(this: CssFontFeatureValuesMap*):
    css_rule_clear_caches(this.m_parent_rule.css_rule())

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:71 Web::CSS::CSSFontFeatureValuesMap::to_ordered_hash_map.
pub func css_font_feature_values_map_to_ordered_hash_map(this: CssFontFeatureValuesMap*) -> ak.OrderedHashMap[ak.FlyString, ak.Vector[u32], ak.FlyStringTraits]:
    var result: ak.OrderedHashMap[ak.FlyString, ak.Vector[u32], ak.FlyStringTraits]
    let vm = js_cell_vm(this.js_cell())

    let entries = js_map_entries(this.m_map_entries)
    for entry in ak.vector_span_const(&entries):
        let key = js_value_to_string(entry.key, vm) else trap("MUST: to_string")

        # as<JS::Array>(entry.value.as_object())
        let array = js_value_as_object(entry.value)
        let length_value = js_object_get_length(array) else trap("MUST: get(length)")
        let array_length = js_value_to_length(length_value, vm) else trap("MUST: to_length")

        var values: ak.Vector[u32]
        ak.vector_ensure_capacity(&values, (usize)array_length)

        for i in 0..<(usize)array_length:
            ak.vector_append_t(&values, js_value_to_u32(js_object_get_without_side_effects_index(array, i), vm) else trap("MUST: to_u32"))

        _ = ak.ordered_hash_map_set(&result, ak.fly_string_from_string(key), values)

    return result

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:92 Web::CSS::CSSFontFeatureValuesMap::initialize (override).
pub func css_font_feature_values_map_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.css_font_feature_values_map)
    js_cell_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/CSS/CSSFontFeatureValuesMap.cpp:98 Web::CSS::CSSFontFeatureValuesMap::visit_edges (override).
pub func css_font_feature_values_map_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (CssFontFeatureValuesMap*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_cell(visitor, (gc.Cell*?)this.m_map_entries)  # JS::Map is opaque in phase 1 (P3)
    gc.cell_visitor_visit_t_2(visitor, this.m_parent_rule)