Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   value_conversions - JS::Value's ToString, ToInt32, ToUint32 and ToLength before luce-js
#
#   DESIGN.md ยง3.6's phase-1 stand-ins: LibWeb converts the values its own JS objects hold
#   (the keys and arrays of CSSFontFeatureValuesMap's JS::Map, strings its native callbacks
#   get), so the conversions answer for the values that exist before phase 3: numbers,
#   booleans, null, undefined and the stand-in strings.
#
#   DESCRIPTION:
#       Port of Libraries/LibJS/Runtime/Value.cpp lines 440-485 (to_string), 1010-1045
#       (to_u32, to_i32_slow_case) and 1185-1200 (to_length), with
#       to_integer_or_infinity's steps for a number. Converting an object (ToPrimitive), a
#       symbol or a BigInt, and the shortest decimal form of a number that is not an integer
#       (Number::toString), are phase 3.
#
#       Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibJS/Runtime/Value.cpp JS::number_to_string for the numbers that are
## integers (their decimal digits, -0 as "0"), NaN and the infinities.
func js_number_to_string(value: f64) -> ak.String:
    if math.is_nan(value):
        return ak.string_from_str("NaN")
    if math.is_infinite(value):
        return ak.string_from_str("-Infinity" if value < 0.0 else "Infinity")
    if value == 0.0:
        return ak.string_from_str("0")
    if math.trunc(value) == value and math.abs(value) < 1e21 and math.abs(value) < 9.2e18:
        return ak.string_number_i64((i64)value)
    trap("unported (P3): JS::number_to_string of a number that is not an integer (the shortest round-trip form)")

## Ported from Libraries/LibJS/Runtime/Value.cpp:440 JS::Value::to_string: 7.1.17 ToString ( argument ),
## https://tc39.es/ecma262/#sec-tostring
pub func js_value_to_string(value: JsValue, vm: Vm*) -> ak.String!:
    _ = vm
    if js_value_is_double(value):
        return js_number_to_string(js_value_as_double(value))

    let tag = js_value_tag(value)
    # 1. If argument is a String, return argument.
    if tag == js_string_tag:
        return js_value_as_string_utf8_string(value)
    # 3. If argument is undefined, return "undefined".
    if tag == js_undefined_tag:
        return ak.string_from_str("undefined")
    # 4. If argument is null, return "null".
    if tag == js_null_tag:
        return ak.string_from_str("null")
    # 5. If argument is true, return "true".
    # 6. If argument is false, return "false".
    if tag == js_boolean_tag:
        return ak.string_from_str("true" if js_value_as_bool(value) else "false")
    # 7. If argument is a Number, return Number::toString(argument, 10).
    if tag == js_int32_tag:
        return ak.string_number_i64((i64)js_value_as_double(value))
    # 2. If argument is a Symbol, throw a TypeError exception.
    # 8. If argument is a BigInt, return BigInt::toString(argument, 10).
    # 9.-12. Otherwise argument is an Object: ? ToString(? ToPrimitive(argument, string)).
    trap("unported (P3): JS::Value::to_string of a symbol, BigInt or object")

## Ported from Libraries/LibJS/Runtime/Value.cpp JS::Value::to_i32 and to_i32_slow_case: 7.1.6 ToInt32 ( argument ),
## https://tc39.es/ecma262/#sec-toint32
pub func js_value_to_i32(value: JsValue, vm: Vm*) -> i32!:
    if js_value_is_int32(value):
        return (i32)js_value_as_double(value)

    # 1. Let number be ? ToNumber(argument).
    let number = js_value_as_double(try js_value_to_number(vm, value))

    # 2. If number is not finite or number is either +0๐”ฝ or -0๐”ฝ, return +0๐”ฝ.
    if math.is_nan(number) or math.is_infinite(number) or number == 0.0:
        return 0

    # 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(โ„(number))).
    var int_val = math.floor(math.abs(number))
    if math.signbit(number):
        int_val = -int_val

    # 4. Let int32bit be int modulo 2^32.
    let two_32 = 4294967296.0
    var int32bit = math.mod(int_val, two_32)
    if int32bit < 0.0:
        int32bit += two_32

    # 5. If int32bit โ‰ฅ 2^31, return ๐”ฝ(int32bit - 2^32); otherwise return ๐”ฝ(int32bit).
    if int32bit >= 2147483648.0:
        return (i32)(int32bit - two_32)
    return (i32)int32bit

## Ported from Libraries/LibJS/Runtime/Value.cpp:1010 JS::Value::to_u32: 7.1.7 ToUint32 ( argument ).
pub func js_value_to_u32(value: JsValue, vm: Vm*) -> u32!:
    return (u32)(try js_value_to_i32(value, vm))

## Ported from Libraries/LibJS/Runtime/Value.cpp:1185 JS::Value::to_length: 7.1.20 ToLength ( argument ),
## https://tc39.es/ecma262/#sec-tolength
pub func js_value_to_length(value: JsValue, vm: Vm*) -> u64!:
    # 1. Let len be ? ToIntegerOrInfinity(argument).
    let len = js_to_integer_or_infinity(js_value_as_double(try js_value_to_number(vm, value)))

    # 2. If len โ‰ค 0, return +0๐”ฝ.
    if len <= 0.0:
        return 0

    # 3. Return ๐”ฝ(min(len, 2^53 - 1)).
    let length_limit = 9007199254740991.0
    return (u64)(len if len < length_limit else length_limit)