Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / structured_serialize.lucb

#==============================================================================================
#
#   structured_serialize - StructuredSerializeForStorage and StructuredDeserialize of the
#   primitives undefined and null: the history states of session history entries
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/StructuredSerialize.cpp lines 1248-1253
#       (structured_serialize_for_storage), 1266-1280 (structured_deserialize,
#       structured_deserialize_internal) with the Serializer's and the Deserializer's steps for
#       undefined and null (lines 256-287 and 590-636): every session history entry serializes
#       null as its classic history API state and undefined as its navigation API state, and
#       restoring a document's history object state deserializes the former. A record is the
#       bytes TransferDataEncoder writes: one ValueTag byte for a primitive without a value.
#
#       Serializing and deserializing anything else (booleans, numbers, strings, objects,
#       transfers, the memory of already serialized objects) needs JS values: phase 3. Region
#       r28 wrote structured_serialize_for_storage among the closure stubs; region p2r moved it
#       here beside the deserializer.
#
#       Copyright (c) 2022, Daniel Ehrenberg <dan@littledan.dev>
#       Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
#       Copyright (c) 2023-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
#       Copyright (c) 2023, Idan Horowitz <idan.horowitz@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## The first cases of StructuredSerialize.cpp's `enum class ValueTag : u8` (line 71): the ones without a value
## after the tag that phase 2 writes and reads.
enum HtmlStructuredSerializeValueTag as u8:
    empty = 0  # Unused, for ease of catching bugs.
    undefined_primitive = 1
    null_primitive = 2

## Ported from StructuredSerialize.cpp:1248 Web::HTML::structured_serialize_for_storage.
# https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeforstorage
pub func html_structured_serialize_for_storage(vm: Vm*, value: JsValue) -> ak.Vector[u8]!:
    # 1. Return ? StructuredSerializeInternal(value, true).
    # (Serializer::serialize, step 4: undefined and null are primitives.)
    var serialized = ak.Vector[u8]()
    if js_value_is_undefined(value):
        ak.vector_append_t(&serialized, (u8)HtmlStructuredSerializeValueTag.undefined_primitive)
    elif js_value_is_null(value):
        ak.vector_append_t(&serialized, (u8)HtmlStructuredSerializeValueTag.null_primitive)
    else:
        trap("unported (P3): Web::HTML::structured_serialize_for_storage of a value other than undefined or null")
    return serialized

## Ported from StructuredSerialize.cpp:1266 Web::HTML::structured_deserialize, with
## structured_deserialize_internal (line 1277) and Deserializer::deserialize's primitives undefined and null.
pub func html_structured_deserialize(vm: Vm*, serialized: ak.Vector[u8], target_realm: Realm*) -> JsValue!:
    var execution_context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&execution_context, target_realm)
    defer html_temporary_execution_context_destruct(&execution_context)

    # (DeserializationMemory { vm.heap() }: no object is deserialized before phase 3.)
    # TransferDataDecoder decoder { serialized };
    # Deserializer::deserialize: auto tag = m_serialized.decode<ValueTag>();
    let tag = *ak.vector_at_const(&serialized, 0)

    # 5. If serialized.[[Type]] is "primitive", then set value to serialized.[[Value]].
    if tag == (u8)HtmlStructuredSerializeValueTag.undefined_primitive:
        return js_undefined()
    if tag == (u8)HtmlStructuredSerializeValueTag.null_primitive:
        return js_null()
    trap("unported (P3): Web::HTML::structured_deserialize of a value other than undefined or null")