fix(js): keep Bytes.toHex character codes non-null - #13004
Closed
fullofcaffeine wants to merge 1 commit into
Closed
Conversation
Member
|
It can also use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13003.
Why
The JavaScript standard library correctly types
String.charCodeAtasNull<Int>because an arbitrary index can be outside the string.Bytes.toHex, however, reads the fixed hexadecimal alphabet only at indicesfrom
0throughstr.length - 1:Every value added to this particular lookup table is therefore an
Int, butthe unannotated array is inferred as
Array<Null<Int>>.That difference is invisible in ordinary JavaScript output because Haxe types
are erased. It matters to typed custom generators, though. The later
StringBuf.addChar(c:Int)andString.fromCharCode(code:Int)calls are inline,so their
Intparameter boundaries no longer exist in the final typedexpression. A strict TypeScript generator can consequently receive a nullable
array read inside raw JavaScript syntax:
TypeScript reports both arguments because
number | nullis not assignable tothe host API's
numberparameter.What
Declare the lookup table's intended element type:
How
The annotation records the local invariant where the table is constructed,
before inlining can erase later parameter boundaries.
A typed custom generator can now observe the ordinary Haxe call boundary:
Null<Int>Array<Int>.pushparameter:IntFor example, Genes renders that already-typed boundary as:
Register.unsafeCast<number>is an identity operation used by that generator:it returns the same value once without conversion or validation. The important
part for Haxe is not the helper spelling; it is that the final typed tree still
contains the exact
Null<Int>-to-Intcall boundary instead of requiring agenerator to infer a type from raw JavaScript template text.
Classic JavaScript remains unchanged:
Tests
The new JavaScript unit test checks both parts of the contract:
Bytes.toHexfield and requires the locallookup table to be
Array<Int>;00 0f 10 7f 80 ffproduce000f107f80ff.Validation performed:
Array<Null<Int>>without the annotationand passes as
Array<Int>with it;Bytes.toHexvectors with Haxe 4.3.7;byte-identical SHA-256 hashes;
temporarily patched Haxe 4.3.7: exactly 16 diagnostics became exactly 14,
removing only the two
Bytes.toHexargument reports.Scope
This does not change
String.charCodeAt's general nullable contract, add rawsyntax recognition to a custom generator, or introduce a downstream standard
library override. It only states the stronger invariant already established by
this bounded loop.
Prepared by the GameCarry agent.