dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / tools / gen_css_values / emit_numeric_factory_methods.lucb
#==============================================================================================
#
# emit_numeric_factory_methods - CSS.number(), CSS.px() and the other numeric factories
#
# DESCRIPTION:
# Port of Meta/Generators/generate_libweb_css_numeric_factory_methods.py
# (write_implementation_file; the header's declarations are the same functions, and the
# IDL file is for the bindings, phase 3). One factory per unit of Units.json, after
# number() and percent().
#
#==============================================================================================
## Python's make_name_acceptable_cpp(unit_name.lower()): the factory's name (`float` would be `float_`).
func numeric_factory_name(unit_name: str) -> str!:
let lowered = try lower(unit_name)
if lowered == "float":
return "float_"
return lowered
## One factory: `GC::Ref<CSSUnitValue> <name>(JS::VM& vm, WebIDL::Double value)`.
func put_numeric_factory(out: Out*, name: str, unit_name: str, after_mark: bool = false) -> !:
if not after_mark:
try out.blank()
try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSNumericFactoryMethods.cpp Web::CSS::{name}.")
try out.line(f"pub func css_{name}(vm: Vm*, value: f64) -> CssUnitValue*:")
try out.line(f" return css_numeric_factory(vm, value, ak.fly_string_from_str({try literal(unit_name)}))")
## Emit generated/generated_css_numeric_factory_methods.lucb from Units.json.
func emit_numeric_factory_methods(units_data: const Json*) -> str!:
var out = try Out.create()
try put_header(&out, "generated_css_numeric_factory_methods", "the CSS Typed OM numeric factories", [
"Generated by tools/gen_css_values from data/css/Units.json; do not edit. The C++ it",
"stands for, at Ladybird 47c82b38d0, is",
"Build/Libraries/LibWeb/CSS/GeneratedCSSNumericFactoryMethods.cpp, written by",
"Meta/Generators/generate_libweb_css_numeric_factory_methods.py.",
"",
"Copyright (c) 2025-2026, Sam Atkins <sam@ladybird.org>",
"Copyright (c) 2026-present, the Ladybird developers.",
"Copyright (c) 2026 the luce-browser authors.",
"SPDX-License-Identifier: BSD-2-Clause (see data/css/LICENSE)",
])
try out.blank()
try out.line("## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSNumericFactoryMethods.cpp Web::CSS::numeric_factory.")
try out.line("# https://drafts.css-houdini.org/css-typed-om-1/#numeric-factory")
try out.line("func css_numeric_factory(vm: Vm*, value: f64, unit_: ak.FlyString) -> CssUnitValue*:")
try out.line(" # All of the above methods must, when called with a double value, return a new CSSUnitValue whose value internal")
try out.line(" # slot is set to value and whose unit internal slot is set to the name of the method as defined here.")
try out.line(" return css_unit_value_create(vm_current_realm(vm) else trap(\"VERIFY: a current realm\"), value, unit_)")
try put_numeric_factory(&out, "number", "number")
try put_numeric_factory(&out, "percent", "percent")
for dimension_index in 0..<units_data.count:
let dimension_name = units_data.key(dimension_index)
let units = units_data.at(dimension_index)
try put_mark(&out, try cat(["<", try snake_casify(dimension_name), ">"]))
for unit_index in 0..<units.count:
let unit_name = units.key(unit_index)
try put_numeric_factory(&out, try numeric_factory_name(unit_name), unit_name, after_mark = unit_index == 0)
return out.text()