Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend CoreFn's Int literal encoding with a stringified value #4506

Open
natefaubion opened this issue Sep 21, 2023 · 8 comments
Open

Extend CoreFn's Int literal encoding with a stringified value #4506

natefaubion opened this issue Sep 21, 2023 · 8 comments

Comments

@natefaubion
Copy link
Contributor

Summary

CoreFn's integer encoding encodes the Integer (unbounded) as a JSON Number literal. This is technically valid as JSON does not require Number bounds, but makes tooling across languages complicated. For example, stock JSON tooling for JS will only support a 64bit double (52 bit max integer), and anything outside that range is difficult to parse.

Motivation

It's not possible for tools like purescript-backend-optimizer to parse CoreFn JSON for backends which use unbounded integers with any of the stock JSON tooling available in the PureScript ecosystem as they all rely on builtin JSON.parse.

Proposal

= object
[ "literalType" .= "IntLiteral"
, "value" .= n
]

Extend this with a stringValue field which prints the value as a String, making it easier for tooling to parse this as a BigInt of some kind. The additional field is necessary to keep CoreFn backwards compatible.

@sigma-andex
Copy link
Contributor

It's not possible for tools like purescript-backend-optimizer to parse CoreFn JSON for backends which use unbounded integers with any of the stock JSON tooling available in the PureScript ecosystem as they all rely on builtin JSON.parse.

Not sure if this helps you, but yoga-json is able to parse BigInt by passing a reviver function to JSON.parse.

@natefaubion
Copy link
Contributor Author

Not sure if this helps you, but yoga-json is able to parse BigInt by passing a reviver function to JSON.parse.

Reviver functions don’t help here because it’s already a JSON Number. Reviver functions can’t recover the original JSON syntax in order to load a Number as a BigInt without losing precision.

@michaelficarra
Copy link
Contributor

@natefaubion You may be interested in the Stage 3 proposal JSON.parse source text access which makes this possible through revivers. Stage 3 means recommended for implementation, and it is already available in Chrome stable.

@garyb
Copy link
Member

garyb commented Sep 24, 2023

Very cool!

@JordanMartinez
Copy link
Contributor

@natefaubion You may be interested in the Stage 3 proposal JSON.parse source text access which makes this possible through revivers. Stage 3 means recommended for implementation, and it is already available in Chrome stable.

I still think it would be easier to just encode the value as a String.

@JordanMartinez
Copy link
Contributor

@natefaubion While this issue is for an Integer value, should the same be done on a Number value, too?

@natefaubion
Copy link
Contributor Author

I don't think so. Number values are Double which are IEEE floats, same as JS, so there's no precision issue.

@JordanMartinez JordanMartinez mentioned this issue Sep 28, 2023
5 tasks
@anttih
Copy link
Contributor

anttih commented Oct 7, 2023

This might be a slightly different incarnation of this issue I just came across while compiling the integers test suite using backend-optimizer (thus using corefn as input). The PS literal -2147483648 gets compiled to (negate 2147483648). That positive number literal doesn't fit the 32-bit Integer once it's converted to int with 2147483648|0. Changing the JSON representation to a string wouldn't actually help if the negative literal gets compiled to negate x, the damage is already done in the corefn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants