feat(libSceNKWebKit): parse decimal numbers as WTF double-conversion #13564

Merged
KernelPanicNT merged 2 commits from upstream-pr/13564 into main 2026-10-31 03:32:36 +00:00
KernelPanicNT commented 2026-10-30 18:28:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KernelPanicNT on 2026-10-30T18:28:43Z.


Source of the behaviour

Public WebKit at commit c55364d53f97 (September 2020). The export inventory mangles RefPtr<T, DumbPtrTraits<T>> with two template arguments and uses WTF::AtomString, WTF::Optional, WTF::Variant and JSONImpl. That is the RefPtr.h of this commit; the next commit to that file, b5d72dbcfc24, adds a third RefDerefTraits argument, and 5c5f0584738c renames DumbPtrTraits. The cited parsing code is the same, apart from 0 becoming nullptr, from the AtomString rename 4da2bec052e0 to this commit.

Behaviour WebKit source
Six overloads, return types, ok default WTFString.h#L70-L75
Leading ASCII spaces skipped, ok / parsedLength, failure returns 0, float overloads are static_cast<float> of the double WTFString.cpp#L1040-L1098
ASCII space set (space, \t to \r), ASCII test ASCIICType.h#L75-L78, #L146-L149
UTF-16: code units above 0x7F become 0, 64-unit stack buffer, longer input through a Vector<LChar> dtoa.h#L48-L62, dtoa.cpp#L124-L130
Vector capacity above UINT_MAX crashes; CRASH() traps in a release PlayStation build Vector.h#L287-L296, #L392-L399, #L626-L628, Assertions.h#L251-L258
Grammar (sign, leading zeros, ., exponent rewind on e, e+, ex), 772 significant digits plus a sticky 1, exponent saturating at INT_MAX / 2, no Infinity/NaN/hex; the processed length is written before any character is read double-conversion.cc#L424-L640
Exponent and dropped-digit counts are int double-conversion.cc#L488-L490, strtod.cc#L140-L157, #L434-L447
Correctly rounded decimal conversion, overflow to infinity, underflow to zero strtod.cc#L433-L483
StringToDoubleConverter::StringToDouble and StringToFloat are static members (no this), uc16 is uint16_t double-conversion.h#L387-L415, utils.h#L128
The four converters call StringToIeee directly: no space skipping, code units compared as they are, the processed count written first and set to the parsed length double-conversion.cc#L459-L664
StringToFloat is not a cast of the double result: StringToIeee<float> calls Strtof, which rounds the decimal digits to float once, catching double rounding, with the overflow cut at the float midpoint double-conversion.h#L401-L411, double-conversion.cc#L446-L457, strtod.cc#L485-L588
The parser is built for every port; the PlayStation port adds no number parser of its own WTF CMakeLists.txt#L459-L468, PlatformPlayStation.cmake, OptionsPlayStation.cmake

AnyPS5 lines replaced:

What

Implements WTF::charactersToDouble and WTF::charactersToFloat, all six Latin-1 and UTF-16 overloads (bool* ok and size_t& parsedLength), and the four static WTF::double_conversion::StringToDoubleConverter::StringToDouble and StringToFloat overloads for char and uint16_t input, in libSceNKWebKit, in a new CharactersToNumber.cpp. The library gets a CMakeLists.txt for the new source and test.

  • One parser, templated on the character and result type, follows WebKit's StringToIeee step by step: up to 772 significant digits with a sticky 1 for dropped nonzero digits, exponent saturation and the rewind to before e when no exponent digits follow. It converts the digits with std::from_chars into the result type, which rounds correctly. Overflow and underflow give infinity and zero as Strtod and Strtof do.
  • StringToDouble and StringToFloat are that parser as WebKit exports it: no leading-space skipping, the processed count set to 0 first and then to the parsed length, and StringToFloat rounds the digits to float once, as Strtof does, so a decimal just above the midpoint between 1 and the next float gives that next float, and one just below the midpoint between FLT_MAX and infinity gives FLT_MAX, where a cast of the double result gives 1 and infinity.
  • charactersToDouble and charactersToFloat add toDoubleType: leading ASCII spaces, ok and parsedLength. UTF-16 code units above 0x7F map to 0 before parsing, so U+0131 or U+0135 never read as 1 or 5. The float overloads round through double, the way WebKit does.
  • Where WebKit faults, the call throws std::invalid_argument: null characters with a nonzero length, and a null parsedLength or processed count. A UTF-16 charactersTo* length above UINT32_MAX after the leading spaces throws where WebKit's conversion Vector crashes. Inputs of about 1 GiB or more whose decimal exponent leaves the range of WebKit's int throw std::overflow_error, since WebKit's result is undefined there.
  • size_t& is modelled as a pointer, as the tree does for references under APS5_VABI.
  • Two Functional debt entries record the gaps: the exact-power fast path and Strtof's double-to-float cast round in the current rounding mode, and the overflow and crash cases above. The ten rows leave the table of exports that still throw.

Open PR #13500 also adds core/libs/prx/libSceNKWebKit/CMakeLists.txt and edits neighbouring lines of Export.cpp, InventoryExports.hpp and the debt table; it implements none of these ten exports. Whichever merges later rebases. Open PR #13515 also appends to docs/dev/debt/libSceNKWebKit.md, with no shared export.

Tested

  • Linux (codespace, GCC, Release): ctest --test-dir build -R "^guest_nkwebkit_numbers$" passes. Negative controls: with Export.cpp and InventoryExports.hpp restored from main and the new source emptied, the test aborts with _ZN3WTF18charactersToDoubleEPKhmPb not implemented; with the three files as they were before the four StringToDoubleConverter exports were added, it aborts with _ZN3WTF17double_conversion23StringToDoubleConverter14StringToDoubleEPKcmPm not implemented. With StringToFloat changed to a cast of the double result, the float rounding checks fail.
  • The built libSceNKWebKit.prx exports the ten names, and each one points at the new function.
  • Differential check outside the tree: WebKit's own StringToIeee, Strtod, Strtof, Bignum, DiyFp and cached powers from the pinned commit, built on the host with the WTF wrappers copied as written, against this implementation. For the six charactersTo* overloads, millions of inputs (random strings over digits, signs, ., e, ASCII and non-ASCII spaces, bytes above 0x7F, long and short UTF-16, midpoints between adjacent doubles printed with up to 800 digits, denormal and overflow boundaries, 700 to 1500 digit inputs, exponents beyond INT_MAX / 2) gave bit-identical values, ok and parsedLength. For the four StringToDoubleConverter exports, 8 million inputs of the same kinds plus exact midpoints between adjacent floats and their neighbouring doubles, float denormal and FLT_MAX boundaries, and code units such as U+0131 and U+0165 gave bit-identical values and processed counts. A 1 GiB input on each side of the int exponent limit returns infinity and throws std::overflow_error respectively.
  • Windows was not built.
  • python3 tools/check_conventions.py --base origin/main passes. tools/progress.py --compare: +10 implemented functions in libSceNKWebKit.
  • No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5089210104 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13564). Originally posted by [KernelPanicNT](https://github.com/KernelPanicNT) on 2026-10-30T18:28:43Z. --- ### Source of the behaviour Public WebKit at commit [`c55364d53f97`](https://github.com/WebKit/WebKit/commit/c55364d53f97) (September 2020). The export inventory mangles `RefPtr<T, DumbPtrTraits<T>>` with two template arguments and uses `WTF::AtomString`, `WTF::Optional`, `WTF::Variant` and `JSONImpl`. That is the [RefPtr.h](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/RefPtr.h#L33) of this commit; the next commit to that file, [`b5d72dbcfc24`](https://github.com/WebKit/WebKit/commit/b5d72dbcfc24), adds a third `RefDerefTraits` argument, and [`5c5f0584738c`](https://github.com/WebKit/WebKit/commit/5c5f0584738c) renames `DumbPtrTraits`. The cited parsing code is the same, apart from `0` becoming `nullptr`, from the `AtomString` rename [`4da2bec052e0`](https://github.com/WebKit/WebKit/commit/4da2bec052e0) to this commit. | Behaviour | WebKit source | | --- | --- | | Six overloads, return types, `ok` default | [WTFString.h#L70-L75](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/text/WTFString.h#L70-L75) | | Leading ASCII spaces skipped, `ok` / `parsedLength`, failure returns 0, float overloads are `static_cast<float>` of the double | [WTFString.cpp#L1040-L1098](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/text/WTFString.cpp#L1040-L1098) | | ASCII space set (space, \t to \r), ASCII test | [ASCIICType.h#L75-L78](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/ASCIICType.h#L75-L78), [#L146-L149](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/ASCIICType.h#L146-L149) | | UTF-16: code units above 0x7F become 0, 64-unit stack buffer, longer input through a `Vector<LChar>` | [dtoa.h#L48-L62](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa.h#L48-L62), [dtoa.cpp#L124-L130](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa.cpp#L124-L130) | | `Vector` capacity above `UINT_MAX` crashes; `CRASH()` traps in a release PlayStation build | [Vector.h#L287-L296](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/Vector.h#L287-L296), [#L392-L399](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/Vector.h#L392-L399), [#L626-L628](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/Vector.h#L626-L628), [Assertions.h#L251-L258](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/Assertions.h#L251-L258) | | Grammar (sign, leading zeros, `.`, exponent rewind on `e`, `e+`, `ex`), 772 significant digits plus a sticky `1`, exponent saturating at `INT_MAX / 2`, no Infinity/NaN/hex; the processed length is written before any character is read | [double-conversion.cc#L424-L640](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.cc#L424-L640) | | Exponent and dropped-digit counts are `int` | [double-conversion.cc#L488-L490](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.cc#L488-L490), [strtod.cc#L140-L157](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/strtod.cc#L140-L157), [#L434-L447](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/strtod.cc#L434-L447) | | Correctly rounded decimal conversion, overflow to infinity, underflow to zero | [strtod.cc#L433-L483](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/strtod.cc#L433-L483) | | `StringToDoubleConverter::StringToDouble` and `StringToFloat` are static members (no `this`), `uc16` is `uint16_t` | [double-conversion.h#L387-L415](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.h#L387-L415), [utils.h#L128](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/utils.h#L128) | | The four converters call `StringToIeee` directly: no space skipping, code units compared as they are, the processed count written first and set to the parsed length | [double-conversion.cc#L459-L664](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.cc#L459-L664) | | `StringToFloat` is not a cast of the double result: `StringToIeee<float>` calls `Strtof`, which rounds the decimal digits to float once, catching double rounding, with the overflow cut at the float midpoint | [double-conversion.h#L401-L411](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.h#L401-L411), [double-conversion.cc#L446-L457](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/double-conversion.cc#L446-L457), [strtod.cc#L485-L588](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/dtoa/strtod.cc#L485-L588) | | The parser is built for every port; the PlayStation port adds no number parser of its own | [WTF CMakeLists.txt#L459-L468](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/CMakeLists.txt#L459-L468), [PlatformPlayStation.cmake](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/WTF/wtf/PlatformPlayStation.cmake), [OptionsPlayStation.cmake](https://github.com/WebKit/WebKit/blob/c55364d53f97/Source/cmake/OptionsPlayStation.cmake) | AnyPS5 lines replaced: - [Export.cpp#L11187-L11201](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/Export.cpp#L11187-L11201), [#L11207-L11221](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/Export.cpp#L11207-L11221) and [#L11283-L11289](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/Export.cpp#L11283-L11289): the ten throwing stubs. - [InventoryExports.hpp#L2650-L2653](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp#L2650-L2653), [#L2655-L2658](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp#L2655-L2658) and [#L2674-L2675](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/core/libs/prx/libSceNKWebKit/include/InventoryExports.hpp#L2674-L2675): the parameterless declarations, now typed. - [libSceNKWebKit.md#L2658-L2661](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/docs/dev/debt/libSceNKWebKit.md#L2658-L2661), [#L2663-L2666](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/docs/dev/debt/libSceNKWebKit.md#L2663-L2666) and [#L2682-L2683](https://github.com/boykopovar/AnyPS5/blob/4bc9e779aa7a/docs/dev/debt/libSceNKWebKit.md#L2682-L2683): the ten rows of the throwing-export table. ### What Implements `WTF::charactersToDouble` and `WTF::charactersToFloat`, all six Latin-1 and UTF-16 overloads (`bool* ok` and `size_t& parsedLength`), and the four static `WTF::double_conversion::StringToDoubleConverter::StringToDouble` and `StringToFloat` overloads for `char` and `uint16_t` input, in `libSceNKWebKit`, in a new `CharactersToNumber.cpp`. The library gets a `CMakeLists.txt` for the new source and test. - One parser, templated on the character and result type, follows WebKit's `StringToIeee` step by step: up to 772 significant digits with a sticky `1` for dropped nonzero digits, exponent saturation and the rewind to before `e` when no exponent digits follow. It converts the digits with `std::from_chars` into the result type, which rounds correctly. Overflow and underflow give infinity and zero as `Strtod` and `Strtof` do. - `StringToDouble` and `StringToFloat` are that parser as WebKit exports it: no leading-space skipping, the processed count set to 0 first and then to the parsed length, and `StringToFloat` rounds the digits to float once, as `Strtof` does, so a decimal just above the midpoint between 1 and the next float gives that next float, and one just below the midpoint between `FLT_MAX` and infinity gives `FLT_MAX`, where a cast of the double result gives 1 and infinity. - `charactersToDouble` and `charactersToFloat` add `toDoubleType`: leading ASCII spaces, `ok` and `parsedLength`. UTF-16 code units above 0x7F map to 0 before parsing, so `U+0131` or `U+0135` never read as `1` or `5`. The float overloads round through double, the way WebKit does. - Where WebKit faults, the call throws `std::invalid_argument`: null characters with a nonzero length, and a null `parsedLength` or processed count. A UTF-16 `charactersTo*` length above `UINT32_MAX` after the leading spaces throws where WebKit's conversion `Vector` crashes. Inputs of about 1 GiB or more whose decimal exponent leaves the range of WebKit's `int` throw `std::overflow_error`, since WebKit's result is undefined there. - `size_t&` is modelled as a pointer, as the tree does for references under `APS5_VABI`. - Two `Functional` debt entries record the gaps: the exact-power fast path and `Strtof`'s double-to-float cast round in the current rounding mode, and the overflow and crash cases above. The ten rows leave the table of exports that still throw. Open PR #13500 also adds `core/libs/prx/libSceNKWebKit/CMakeLists.txt` and edits neighbouring lines of `Export.cpp`, `InventoryExports.hpp` and the debt table; it implements none of these ten exports. Whichever merges later rebases. Open PR #13515 also appends to `docs/dev/debt/libSceNKWebKit.md`, with no shared export. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Linux (codespace, GCC, Release): `ctest --test-dir build -R "^guest_nkwebkit_numbers$"` passes. Negative controls: with `Export.cpp` and `InventoryExports.hpp` restored from `main` and the new source emptied, the test aborts with `_ZN3WTF18charactersToDoubleEPKhmPb not implemented`; with the three files as they were before the four `StringToDoubleConverter` exports were added, it aborts with `_ZN3WTF17double_conversion23StringToDoubleConverter14StringToDoubleEPKcmPm not implemented`. With `StringToFloat` changed to a cast of the double result, the float rounding checks fail. - The built `libSceNKWebKit.prx` exports the ten names, and each one points at the new function. - Differential check outside the tree: WebKit's own `StringToIeee`, `Strtod`, `Strtof`, `Bignum`, `DiyFp` and cached powers from the pinned commit, built on the host with the WTF wrappers copied as written, against this implementation. For the six `charactersTo*` overloads, millions of inputs (random strings over digits, signs, `.`, `e`, ASCII and non-ASCII spaces, bytes above 0x7F, long and short UTF-16, midpoints between adjacent doubles printed with up to 800 digits, denormal and overflow boundaries, 700 to 1500 digit inputs, exponents beyond `INT_MAX / 2`) gave bit-identical values, `ok` and `parsedLength`. For the four `StringToDoubleConverter` exports, 8 million inputs of the same kinds plus exact midpoints between adjacent floats and their neighbouring doubles, float denormal and `FLT_MAX` boundaries, and code units such as `U+0131` and `U+0165` gave bit-identical values and processed counts. A 1 GiB input on each side of the `int` exponent limit returns infinity and throws `std::overflow_error` respectively. - Windows was not built. - `python3 tools/check_conventions.py --base origin/main` passes. `tools/progress.py --compare`: +10 implemented functions in libSceNKWebKit. - No title tested. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!13564
No description provided.