dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / external / lib_js / date_and_number.lucb
#==============================================================================================
#
# date_and_number - the time value arithmetic of JS::Date and Number::toString before scripting
#
# DESCRIPTION:
# Port of the parts of Libraries/LibJS that HTMLInputElement's conversions use before
# luce-js arrives (DESIGN.md ยง3.6): the time value helpers of Runtime/Date.h and
# Runtime/Date.cpp (msPerDay and friends, Day, TimeWithinDay, DaysInYear, DayFromYear,
# TimeFromYear, YearFromTime, DayWithinYear, InLeapYear, MonthFromTime, DateFromTime,
# HourFromTime, MinFromTime, SecFromTime, msFromTime, MakeDay, MakeDate), Number::toString
# for radix 10 (Runtime/Value.cpp number_to_string) and Value::is_finite_number. These are
# plain arithmetic on doubles; JS::Date objects themselves stay phase 3. Added in region
# r24.
#
# Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2022-2023, Tim Flynn <trflynn89@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Constants of Date.h =====================================================================
## Ported from Libraries/LibJS/Runtime/Date.h:50 JS::hours_per_day.
# https://tc39.es/ecma262/#eqn-HoursPerDay
pub let js_hours_per_day: f64 = 24.0
## Ported from Libraries/LibJS/Runtime/Date.h:52 JS::minutes_per_hour.
# https://tc39.es/ecma262/#eqn-MinutesPerHour
pub let js_minutes_per_hour: f64 = 60.0
## Ported from Libraries/LibJS/Runtime/Date.h:54 JS::seconds_per_minute.
# https://tc39.es/ecma262/#eqn-SecondsPerMinute
pub let js_seconds_per_minute: f64 = 60.0
## Ported from Libraries/LibJS/Runtime/Date.h:56 JS::ms_per_second.
# https://tc39.es/ecma262/#eqn-msPerSecond
pub let js_ms_per_second: f64 = 1000.0
## Ported from Libraries/LibJS/Runtime/Date.h:58 JS::ms_per_minute.
# https://tc39.es/ecma262/#eqn-msPerMinute
pub let js_ms_per_minute: f64 = 60000.0
## Ported from Libraries/LibJS/Runtime/Date.h:60 JS::ms_per_hour.
# https://tc39.es/ecma262/#eqn-msPerHour
pub let js_ms_per_hour: f64 = 3600000.0
## Ported from Libraries/LibJS/Runtime/Date.h:62 JS::ms_per_day.
# https://tc39.es/ecma262/#eqn-msPerDay
pub let js_ms_per_day: f64 = 86400000.0
## Ported from Libraries/LibJS/Runtime/Date.h:69 JS::max_time_value.
# https://tc39.es/ecma262/#sec-time-values-and-time-range
# A time value supports a [...] range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds
pub let js_max_time_value: f64 = 8.64e15
# mark: Time value arithmetic of Date.cpp =======================================================
## Ported from Libraries/LibJS/Runtime/Date.cpp:67 JS::day.
# 21.4.1.3 Day ( t ), https://tc39.es/ecma262/#sec-day
pub func js_day(time_value: f64) -> f64:
# 1. Return ๐ฝ(floor(โ(t / msPerDay))).
return math.floor(time_value / js_ms_per_day)
## Ported from Libraries/LibJS/Runtime/Date.cpp:74 JS::time_within_day.
# 21.4.1.4 TimeWithinDay ( t ), https://tc39.es/ecma262/#sec-timewithinday
pub func js_time_within_day(time: f64) -> f64:
# 1. Return ๐ฝ(โ(t) modulo โ(msPerDay)).
return js_modulo(time, js_ms_per_day)
## Ported from Libraries/LibJS/Runtime/Date.cpp:81 JS::days_in_year.
# 21.4.1.5 DaysInYear ( y ), https://tc39.es/ecma262/#sec-daysinyear
pub func js_days_in_year(y: i32) -> u16:
# 1. Let ry be โ(y).
let ry = f64(y)
# 2. If (ry modulo 400) = 0, return 366๐ฝ.
if js_modulo(ry, 400.0) == 0.0:
return 366
# 3. If (ry modulo 100) = 0, return 365๐ฝ.
if js_modulo(ry, 100.0) == 0.0:
return 365
# 4. If (ry modulo 4) = 0, return 366๐ฝ.
if js_modulo(ry, 4.0) == 0.0:
return 366
# 5. Return 365๐ฝ.
return 365
## Ported from Libraries/LibJS/Runtime/Date.cpp:103 JS::day_from_year.
# 21.4.1.6 DayFromYear ( y ), https://tc39.es/ecma262/#sec-dayfromyear
pub func js_day_from_year(y: i32) -> f64:
# 1. Let ry be โ(y).
let ry = f64(y)
# 2. NOTE: In the following steps, each _numYearsN_ is the number of years divisible by N that occur between the
# epoch and the start of year y. (The number is negative if y is before the epoch.)
# 3. Let numYears1 be (ry - 1970).
let num_years_1 = ry - 1970.0
# 4. Let numYears4 be floor((ry - 1969) / 4).
let num_years_4 = math.floor((ry - 1969.0) / 4.0)
# 5. Let numYears100 be floor((ry - 1901) / 100).
let num_years_100 = math.floor((ry - 1901.0) / 100.0)
# 6. Let numYears400 be floor((ry - 1601) / 400).
let num_years_400 = math.floor((ry - 1601.0) / 400.0)
# 7. Return ๐ฝ(365 ร numYears1 + numYears4 - numYears100 + numYears400).
return 365.0 * num_years_1 + num_years_4 - num_years_100 + num_years_400
## Ported from Libraries/LibJS/Runtime/Date.cpp:128 JS::time_from_year.
# 21.4.1.7 TimeFromYear ( y ), https://tc39.es/ecma262/#sec-timefromyear
pub func js_time_from_year(y: i32) -> f64:
# 1. Return msPerDay ร DayFromYear(y).
return js_ms_per_day * js_day_from_year(y)
## Ported from Libraries/LibJS/Runtime/Date.cpp:135 JS::year_from_time.
# 21.4.1.8 YearFromTime ( t ), https://tc39.es/ecma262/#sec-yearfromtime
pub func js_year_from_time(t: f64) -> i32:
# 1. Return the largest integral Number y (closest to +โ) such that TimeFromYear(y) โค t.
if not math.is_finite(t):
return ak.i32_max
# Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
var year = (i32)math.floor(t / (365.2425 * js_ms_per_day) + 1970.0)
let year_t = js_time_from_year(year)
if year_t > t:
year -= 1
elif year_t + f64(js_days_in_year(year)) * js_ms_per_day <= t:
year += 1
return year
## Ported from Libraries/LibJS/Runtime/Date.cpp:154 JS::day_within_year.
# 21.4.1.9 DayWithinYear ( t ), https://tc39.es/ecma262/#sec-daywithinyear
pub func js_day_within_year(t: f64) -> u16:
if not math.is_finite(t):
return 0
# 1. Return Day(t) - DayFromYear(YearFromTime(t)).
return (u16)(js_day(t) - js_day_from_year(js_year_from_time(t)))
## Ported from Libraries/LibJS/Runtime/Date.cpp:164 JS::in_leap_year.
# 21.4.1.10 InLeapYear ( t ), https://tc39.es/ecma262/#sec-inleapyear
pub func js_in_leap_year(t: f64) -> bool:
# 1. If DaysInYear(YearFromTime(t)) is 366๐ฝ, return 1๐ฝ; else return +0๐ฝ.
return js_days_in_year(js_year_from_time(t)) == 366
## Ported from Libraries/LibJS/Runtime/Date.cpp:171 JS::month_from_time.
# 21.4.1.11 MonthFromTime ( t ), https://tc39.es/ecma262/#sec-monthfromtime
pub func js_month_from_time(t: f64) -> u8:
# 1. Let inLeapYear be InLeapYear(t).
let in_leap_year = (u32)js_in_leap_year(t)
# 2. Let dayWithinYear be DayWithinYear(t).
let day_within_year = (u32)js_day_within_year(t)
# 3. If dayWithinYear < 31๐ฝ, return +0๐ฝ.
if day_within_year < 31:
return 0
# 4. If dayWithinYear < 59๐ฝ + inLeapYear, return 1๐ฝ.
if day_within_year < (59 + in_leap_year):
return 1
# 5. If dayWithinYear < 90๐ฝ + inLeapYear, return 2๐ฝ.
if day_within_year < (90 + in_leap_year):
return 2
# 6. If dayWithinYear < 120๐ฝ + inLeapYear, return 3๐ฝ.
if day_within_year < (120 + in_leap_year):
return 3
# 7. If dayWithinYear < 151๐ฝ + inLeapYear, return 4๐ฝ.
if day_within_year < (151 + in_leap_year):
return 4
# 8. If dayWithinYear < 181๐ฝ + inLeapYear, return 5๐ฝ.
if day_within_year < (181 + in_leap_year):
return 5
# 9. If dayWithinYear < 212๐ฝ + inLeapYear, return 6๐ฝ.
if day_within_year < (212 + in_leap_year):
return 6
# 10. If dayWithinYear < 243๐ฝ + inLeapYear, return 7๐ฝ.
if day_within_year < (243 + in_leap_year):
return 7
# 11. If dayWithinYear < 273๐ฝ + inLeapYear, return 8๐ฝ.
if day_within_year < (273 + in_leap_year):
return 8
# 12. If dayWithinYear < 304๐ฝ + inLeapYear, return 9๐ฝ.
if day_within_year < (304 + in_leap_year):
return 9
# 13. If dayWithinYear < 334๐ฝ + inLeapYear, return 10๐ฝ.
if day_within_year < (334 + in_leap_year):
return 10
# 14. Assert: dayWithinYear < 365๐ฝ + inLeapYear.
assert(day_within_year < (365 + in_leap_year))
# 15. Return 11๐ฝ.
return 11
## Ported from Libraries/LibJS/Runtime/Date.cpp:228 JS::date_from_time.
# 21.4.1.12 DateFromTime ( t ), https://tc39.es/ecma262/#sec-datefromtime
pub func js_date_from_time(t: f64) -> u8:
# 1. Let inLeapYear be InLeapYear(t).
let in_leap_year = (u32)js_in_leap_year(t)
# 2. Let dayWithinYear be DayWithinYear(t).
let day_within_year = (u32)js_day_within_year(t)
# 3. Let month be MonthFromTime(t).
let month = js_month_from_time(t)
# 4. If month is +0๐ฝ, return dayWithinYear + 1๐ฝ.
if month == 0:
return (u8)(day_within_year + 1)
# 5. If month is 1๐ฝ, return dayWithinYear - 30๐ฝ.
if month == 1:
return (u8)(day_within_year -% 30)
# 6. If month is 2๐ฝ, return dayWithinYear - 58๐ฝ - inLeapYear.
if month == 2:
return (u8)(day_within_year -% 58 -% in_leap_year)
# 7. If month is 3๐ฝ, return dayWithinYear - 89๐ฝ - inLeapYear.
if month == 3:
return (u8)(day_within_year -% 89 -% in_leap_year)
# 8. If month is 4๐ฝ, return dayWithinYear - 119๐ฝ - inLeapYear.
if month == 4:
return (u8)(day_within_year -% 119 -% in_leap_year)
# 9. If month is 5๐ฝ, return dayWithinYear - 150๐ฝ - inLeapYear.
if month == 5:
return (u8)(day_within_year -% 150 -% in_leap_year)
# 10. If month is 6๐ฝ, return dayWithinYear - 180๐ฝ - inLeapYear.
if month == 6:
return (u8)(day_within_year -% 180 -% in_leap_year)
# 11. If month is 7๐ฝ, return dayWithinYear - 211๐ฝ - inLeapYear.
if month == 7:
return (u8)(day_within_year -% 211 -% in_leap_year)
# 12. If month is 8๐ฝ, return dayWithinYear - 242๐ฝ - inLeapYear.
if month == 8:
return (u8)(day_within_year -% 242 -% in_leap_year)
# 13. If month is 9๐ฝ, return dayWithinYear - 272๐ฝ - inLeapYear.
if month == 9:
return (u8)(day_within_year -% 272 -% in_leap_year)
# 14. If month is 10๐ฝ, return dayWithinYear - 303๐ฝ - inLeapYear.
if month == 10:
return (u8)(day_within_year -% 303 -% in_leap_year)
# 15. Assert: month is 11๐ฝ.
assert(month == 11)
# 16. Return dayWithinYear - 333๐ฝ - inLeapYear.
return (u8)(day_within_year -% 333 -% in_leap_year)
## Ported from Libraries/LibJS/Runtime/Date.cpp:306 JS::hour_from_time.
# 21.4.1.14 HourFromTime ( t ), https://tc39.es/ecma262/#sec-hourfromtime
pub func js_hour_from_time(t: f64) -> u8:
if not math.is_finite(t):
return 0
# 1. Return ๐ฝ(floor(โ(t / msPerHour)) modulo HoursPerDay).
return (u8)js_modulo(math.floor(t / js_ms_per_hour), js_hours_per_day)
## Ported from Libraries/LibJS/Runtime/Date.cpp:316 JS::min_from_time.
# 21.4.1.15 MinFromTime ( t ), https://tc39.es/ecma262/#sec-minfromtime
pub func js_min_from_time(t: f64) -> u8:
if not math.is_finite(t):
return 0
# 1. Return ๐ฝ(floor(โ(t / msPerMinute)) modulo MinutesPerHour).
return (u8)js_modulo(math.floor(t / js_ms_per_minute), js_minutes_per_hour)
## Ported from Libraries/LibJS/Runtime/Date.cpp:326 JS::sec_from_time.
# 21.4.1.16 SecFromTime ( t ), https://tc39.es/ecma262/#sec-secfromtime
pub func js_sec_from_time(t: f64) -> u8:
if not math.is_finite(t):
return 0
# 1. Return ๐ฝ(floor(โ(t / msPerSecond)) modulo SecondsPerMinute).
return (u8)js_modulo(math.floor(t / js_ms_per_second), js_seconds_per_minute)
## Ported from Libraries/LibJS/Runtime/Date.cpp:336 JS::ms_from_time.
# 21.4.1.17 msFromTime ( t ), https://tc39.es/ecma262/#sec-msfromtime
pub func js_ms_from_time(t: f64) -> u16:
if not math.is_finite(t):
return 0
# 1. Return ๐ฝ(โ(t) modulo โ(msPerSecond)).
return (u16)js_modulo(t, js_ms_per_second)
## Ported from Libraries/LibJS/Runtime/Date.cpp:599 JS::make_day.
# 21.4.1.28 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
pub func js_make_day(year: f64, month: f64, date: f64) -> f64:
# 1. If year is not finite or month is not finite or date is not finite, return NaN.
if not math.is_finite(year) or not math.is_finite(month) or not math.is_finite(date):
return math.nan
# 2. Let y be ๐ฝ(! ToIntegerOrInfinity(year)).
let y = js_to_integer_or_infinity(year)
# 3. Let m be ๐ฝ(! ToIntegerOrInfinity(month)).
let m = js_to_integer_or_infinity(month)
# 4. Let dt be ๐ฝ(! ToIntegerOrInfinity(date)).
let dt = js_to_integer_or_infinity(date)
# 5. Let ym be y + ๐ฝ(floor(โ(m) / 12)).
let ym = y + math.floor(m / 12.0)
# 6. If ym is not finite, return NaN.
if not math.is_finite(ym):
return math.nan
# 7. Let mn be ๐ฝ(โ(m) modulo 12).
let mn = js_modulo(m, 12.0)
# 8. Find a finite time value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn and DateFromTime(t) is 1๐ฝ; but if this is not possible (because some argument is out of range), return NaN.
# (AK::is_within_range<int> of a double: within [INT_MIN, INT_MAX].)
if not (ym <= 2147483647.0 and -2147483648.0 <= ym) or not (mn + 1.0 <= 2147483647.0 and -2147483648.0 <= mn + 1.0):
return math.nan
let t = f64(ak.days_since_epoch((i32)ym, (i32)mn + 1, 1)) * js_ms_per_day
# 9. Return Day(t) + dt - 1๐ฝ.
return js_day(t) + dt - 1.0
## Ported from Libraries/LibJS/Runtime/Date.cpp:630 JS::make_date.
# 21.4.1.29 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
pub func js_make_date(day: f64, time: f64) -> f64:
# 1. If day is not finite or time is not finite, return NaN.
if not math.is_finite(day) or not math.is_finite(time):
return math.nan
# 2. Let tv be day ร msPerDay + time.
let tv_ = day * js_ms_per_day + time
# 3. If tv is not finite, return NaN.
if not math.is_finite(tv_):
return math.nan
# 4. Return tv.
return tv_
# mark: Number::toString and Value::is_finite_number ============================================
## `convert_to_decimal_digits_array` of number_to_string: the decimal digits of `x`, most
## significant first, appended to `digits` from `length` on.
func js_convert_to_decimal_digits_array(x: u64, digits: u8[], length: i32*):
var value = x
while value != 0:
digits[(usize)*length] = (u8)(value % 10) | '0'
*length += 1
value = value // 10
var i: i32 = 0
while 2 * i + 1 < *length:
let swapped = digits[(usize)i]
digits[(usize)i] = digits[(usize)(*length - i - 1)]
digits[(usize)(*length - i - 1)] = swapped
i += 1
## Ported from Libraries/LibJS/Runtime/Value.cpp:75 JS::number_to_string(StringBuilder&, double, NumberToStringMode)
## (NumberToStringMode::WithExponent).
# 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
# Implementation for radix = 10
pub func js_number_to_string_builder(builder: ak.StringBuilder*, d: f64):
# 1. If x is NaN, return "NaN".
if math.is_nan(d):
ak.string_builder_append_string_view(builder, ak.sv("NaN"))
return
# 2. If x is +0๐ฝ or -0๐ฝ, return "0".
if d == 0.0:
ak.string_builder_append_string_view(builder, ak.sv("0"))
return
# 4. If x is +โ๐ฝ, return "Infinity".
if math.is_infinite(d):
if d > 0.0:
ak.string_builder_append_string_view(builder, ak.sv("Infinity"))
return
ak.string_builder_append_string_view(builder, ak.sv("-Infinity"))
return
# 5. Let n, k, and s be integers such that k โฅ 1, radix ^ (k - 1) โค s < radix ^ k, ๐ฝ(s ร radix ^ (n - k)) is x, and
# k is as small as possible. Note that k is the number of digits in the representation of s using radix radix,
# that s is not divisible by radix, and that the least significant digit of s is not necessarily uniquely
# determined by these criteria.
#
# NB: guarantees provided by convert_to_decimal_exponential_form satisfy requirements of NOTE 2.
let form = ak.convert_to_decimal_exponential_form_f64(d)
var k: i32 = 0
var mantissa_digits: u8[20]
js_convert_to_decimal_digits_array(form.fraction, mantissa_digits, &k)
let n = form.exponent + k # s = mantissa
# 3. If x < -0๐ฝ, return the string-concatenation of "-" and Number::toString(-x, radix).
if form.sign:
ak.string_builder_append_u8(builder, '-')
# 6. If radix โ 10 or n is in the inclusive interval from -5 to 21, then
if n >= -5 and n <= 21:
# a. If n โฅ k, then
if n >= k:
# i. Return the string-concatenation of:
# the code units of the k digits of the representation of s using radix radix
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(mantissa_digits[..<(usize)k]))
# n - k occurrences of the code unit 0x0030 (DIGIT ZERO)
ak.string_builder_append_repeated_u8(builder, '0', (usize)(n - k))
# b. Else if n > 0, then
elif n > 0:
# i. Return the string-concatenation of:
# the code units of the most significant n digits of the representation of s using radix radix
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(mantissa_digits[..<(usize)n]))
# the code unit 0x002E (FULL STOP)
ak.string_builder_append_u8(builder, '.')
# the code units of the remaining k - n digits of the representation of s using radix radix
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(mantissa_digits[(usize)n..<(usize)k]))
# c. Else,
else:
# i. Assert: n โค 0.
assert(n <= 0)
# ii. Return the string-concatenation of:
# the code unit 0x0030 (DIGIT ZERO)
ak.string_builder_append_u8(builder, '0')
# the code unit 0x002E (FULL STOP)
ak.string_builder_append_u8(builder, '.')
# -n occurrences of the code unit 0x0030 (DIGIT ZERO)
ak.string_builder_append_repeated_u8(builder, '0', (usize)(-n))
# the code units of the k digits of the representation of s using radix radix
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(mantissa_digits[..<(usize)k]))
return
# 7. NOTE: In this case, the input will be represented using scientific E notation, such as 1.2e+3.
# 9. If n < 0, then
# a. Let exponentSign be the code unit 0x002D (HYPHEN-MINUS).
# 10. Else,
# a. Let exponentSign be the code unit 0x002B (PLUS SIGN).
let exponent_sign: u8 = '-' if n < 0 else '+'
var exponent_digits: u8[5]
var exponent_length: i32 = 0
js_convert_to_decimal_digits_array((u64)math.iabs((i64)(n - 1)), exponent_digits, &exponent_length)
# 11. If k is 1, then
if k == 1:
# a. Return the string-concatenation of:
# the code unit of the single digit of s
ak.string_builder_append_u8(builder, mantissa_digits[0])
# the code unit 0x0065 (LATIN SMALL LETTER E)
ak.string_builder_append_u8(builder, 'e')
# exponentSign
ak.string_builder_append_u8(builder, exponent_sign)
# the code units of the decimal representation of abs(n - 1)
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(exponent_digits[..<(usize)exponent_length]))
return
# 12. Return the string-concatenation of:
# the code unit of the most significant digit of the decimal representation of s
ak.string_builder_append_u8(builder, mantissa_digits[0])
# the code unit 0x002E (FULL STOP)
ak.string_builder_append_u8(builder, '.')
# the code units of the remaining k - 1 digits of the decimal representation of s
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(mantissa_digits[1..<(usize)k]))
# the code unit 0x0065 (LATIN SMALL LETTER E)
ak.string_builder_append_u8(builder, 'e')
# exponentSign
ak.string_builder_append_u8(builder, exponent_sign)
# the code units of the decimal representation of abs(n - 1)
ak.string_builder_append_string_view(builder, ak.string_view_from_bytes(exponent_digits[..<(usize)exponent_length]))
## Ported from Libraries/LibJS/Runtime/Value.cpp:213 JS::number_to_utf16_string (NumberToStringMode::WithExponent).
pub func js_number_to_utf16_string(d: f64) -> ak.Utf16String:
var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
js_number_to_string_builder(&builder, d)
return ak.string_builder_to_utf16_string(&builder)
## Ported from Libraries/LibJS/Runtime/Value.h:148 JS::Value::is_finite_number.
pub func js_value_is_finite_number(this: JsValue) -> bool:
if not js_value_is_number(this):
return false
if js_value_is_int32(this):
return true
let value = js_value_as_double(this)
return not math.is_nan(value) and not math.is_infinite(value)