Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / dom / qualified_name.lucb

#==============================================================================================
#
#   qualified_name - DOM::QualifiedName: interned (local name, prefix, namespace) triples
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/QualifiedName.cpp (lines 1-90) and the inline accessors of
#       QualifiedName.h. Every QualifiedName holds a pointer to a shared Impl found in the
#       `impls` table by its three names, so equal qualified names share one Impl and its
#       precomputed lowercased local name and "prefix:local" string.
#
#       The Impls and the table live in `memory.heap`, as the interned FlyStrings do (ak's
#       fly_string.lucb): the table is a module global outside the managed heap, so neither
#       its buckets nor the Impls it points at may be collectable, and an Impl holds only
#       FlyStrings, which are interned there too.
#
#       FIXME: Impls are never released. In C++ an Impl is reference counted and its
#       destructor removes it from the table; here nothing is counted or destroyed, so the
#       table grows by every distinct qualified name, as the FlyString table does.
#
#==============================================================================================

# mark: The Impl table ==========================================================================

## Ported from QualifiedName.cpp:12 Web::DOM::hash_impl.
func dom_hash_impl(local_name: ak.FlyString, prefix: ak.FlyString?, namespace_: ak.FlyString?) -> u32:
    var hash_ = ak.fly_string_hash(&local_name)
    if let prefix_value = prefix:
        hash_ = ak.pair_int_hash(hash_, ak.fly_string_hash(&prefix_value))
    if let namespace_value = namespace_:
        hash_ = ak.pair_int_hash(hash_, ak.fly_string_hash(&namespace_value))
    return hash_

## Ported from QualifiedName.cpp:23 Web::DOM::ImplTraits::hash.
pub func dom_impl_traits_hash(impl: DomQualifiedNameImpl*?) -> u32:
    let value = impl else trap("VERIFY: null QualifiedName::Impl")
    return dom_hash_impl(value.local_name, value.prefix, value.namespace_)

## Ported from QualifiedName.cpp:28 Web::DOM::ImplTraits::equals.
pub func dom_impl_traits_equals(a: DomQualifiedNameImpl*?, b: DomQualifiedNameImpl*?) -> bool:
    let a_value = a else trap("VERIFY: null QualifiedName::Impl")
    let b_value = b else trap("VERIFY: null QualifiedName::Impl")
    return ak.fly_string_eq_fly_string(&a_value.local_name, &b_value.local_name) and dom_node_optional_fly_strings_equal(a_value.prefix, b_value.prefix) and dom_node_optional_fly_strings_equal(a_value.namespace_, b_value.namespace_)

## The captures of ensure_impl's find predicate.
struct DomEnsureImplPredicate:
    var local_name: ak.FlyString
    var prefix: ak.FlyString?
    var namespace_: ak.FlyString?

## ensure_impl's predicate: `entry->local_name == local_name && entry->prefix == prefix && ...`.
func dom_ensure_impl_predicate_call(context: void*?, entry: const (DomQualifiedNameImpl*?)*) -> bool:
    let captures = (DomEnsureImplPredicate*)(context else trap("no captures"))
    let impl = *entry else return false
    return ak.fly_string_eq_fly_string(&impl.local_name, &captures.local_name) and dom_node_optional_fly_strings_equal(impl.prefix, captures.prefix) and dom_node_optional_fly_strings_equal(impl.namespace_, captures.namespace_)

## Ported from QualifiedName.cpp:38 Web::DOM::ensure_impl.
func dom_ensure_impl(local_name: ak.FlyString, prefix: ak.FlyString?, namespace_: ak.FlyString?) -> DomQualifiedNameImpl*:
    let hash_ = dom_hash_impl(local_name, prefix, namespace_)

    var captures = DomEnsureImplPredicate(local_name = local_name, prefix = prefix, namespace_ = namespace_)
    let predicate = ak.Function1[const (DomQualifiedNameImpl*?)*, bool](call = dom_ensure_impl_predicate_call, context = &captures)
    var it = ak.hash_table_find_u32_tt_unary_predicate(&dom_impls, hash_, predicate)
    if ak.hash_table_iterator_ne(&it, ak.hash_table_end(&dom_impls)):
        return *ak.hash_table_iterator_deref(&it) else trap("VERIFY: null QualifiedName::Impl")

    # adopt_ref(*new QualifiedName::Impl(...)): the Impl lives as long as the table (see the header).
    var impl: DomQualifiedNameImpl*? = none
    with memory.heap:
        impl = new DomQualifiedNameImpl else trap("out of memory")
    let new_impl = impl else trap("out of memory")
    dom_qualified_name_impl_construct(new_impl, local_name, prefix, namespace_)
    return new_impl

# mark: QualifiedName ===========================================================================

## Ported from QualifiedName.cpp:52 Web::DOM::QualifiedName::QualifiedName.
pub func dom_qualified_name_construct(this: DomQualifiedName*, local_name: ak.FlyString, prefix: ak.FlyString?, namespace_: ak.FlyString?):
    this.m_impl = dom_ensure_impl(local_name, prefix, namespace_)

## Ported from QualifiedName.cpp:57 Web::DOM::QualifiedName::Impl::Impl.
pub func dom_qualified_name_impl_construct(this: DomQualifiedNameImpl*, a_local_name: ak.FlyString, a_prefix: ak.FlyString?, a_namespace: ak.FlyString?):
    this.local_name = a_local_name
    this.lowercased_local_name = ak.fly_string_to_ascii_lowercase(&this.local_name)
    this.prefix = a_prefix
    this.namespace_ = a_namespace
    with memory.heap:
        _ = ak.hash_table_set(&dom_impls, (DomQualifiedNameImpl*?)this)
    dom_qualified_name_impl_make_internal_string(this)

## Ported from QualifiedName.cpp:74 Web::DOM::QualifiedName::Impl::make_internal_string.
# https://dom.spec.whatwg.org/#concept-attribute-qualified-name
# https://dom.spec.whatwg.org/#concept-element-qualified-name
pub func dom_qualified_name_impl_make_internal_string(this: DomQualifiedNameImpl*):
    # This is possible to do according to the spec: "User agents could have this as an internal slot as an optimization."
    match this.prefix:
        .none:
            this.as_string = this.local_name
            return
        .some(prefix):
            # String::formatted("{}:{}", prefix.value(), local_name)
            var builder = ak.string_builder_new()
            ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&prefix))
            ak.string_builder_append_u8(&builder, ':')
            ak.string_builder_append_string_view(&builder, ak.fly_string_bytes_as_string_view(&this.local_name))
            this.as_string = ak.fly_string_from_string(ak.string_builder_to_string(&builder) else trap("MUST: String::formatted"))

## Ported from QualifiedName.cpp:85 Web::DOM::QualifiedName::set_prefix.
# NOTE: As in C++, this changes the shared Impl: every QualifiedName with the same Impl sees the new
#       prefix, and neither the table's hash nor as_string is updated.
pub func dom_qualified_name_set_prefix(this: DomQualifiedName*, value: ak.FlyString?):
    this.m_impl.prefix = value

# mark: QualifiedName.h =========================================================================

## Ported from QualifiedName.h:21 Web::DOM::QualifiedName::local_name.
pub func dom_qualified_name_local_name(this: const DomQualifiedName*) -> ak.FlyString:
    return this.m_impl.local_name

## Ported from QualifiedName.h:22 Web::DOM::QualifiedName::prefix.
pub func dom_qualified_name_prefix(this: const DomQualifiedName*) -> ak.FlyString?:
    return this.m_impl.prefix

## Ported from QualifiedName.h:23 Web::DOM::QualifiedName::namespace_.
pub func dom_qualified_name_namespace(this: const DomQualifiedName*) -> ak.FlyString?:
    return this.m_impl.namespace_

## Ported from QualifiedName.h:25 Web::DOM::QualifiedName::lowercased_local_name.
pub func dom_qualified_name_lowercased_local_name(this: const DomQualifiedName*) -> ak.FlyString:
    return this.m_impl.lowercased_local_name

## Ported from QualifiedName.h:27 Web::DOM::QualifiedName::as_string.
pub func dom_qualified_name_as_string(this: const DomQualifiedName*) -> ak.FlyString:
    return this.m_impl.as_string