Live data from Hacker News

BigInt Shipping in Firefox

wingolog.org

31–40 of 172 posts

Re: BigInt Shipping in Firefox

#32
post #28

What's with the comment at the end of the blog entry? lol.

I know right? Big accusation (that’s wrong), with nothing to back it up?

Maybe it's the algorithmic evolution of those gibberish comments WordPress blogs used to get all the time...

Re: BigInt Shipping in Firefox

#33
post #18
post #7

I feel sorry for whoever has to write the polyfill for this...

Bignums themselves are actually pretty easy to implement as a plain library, provided your language has a uint8-array type (not just a String type, which might require the content be utf-8 valid, or might only have facilities to operate on codepoints, or might cut the data off at the first NUL.) JS already has a Uint8Array, so bignum libraries are easy. (Let me tell you though, there were JS bignum libraries before U…

Babel (a popular transpiler) already has a plug-in for BigInts https://babeljs.io/docs/en/babel-plugin-syntax-bigint

Re: BigInt Shipping in Firefox

#34
post #5

Anyone knows how does bigint serialization and deserialization to JSON look like in practice in Chrome and Firefox?

You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get() { "use strict"; return () => String(this); } }); Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might f…

I'm sure its for backwards compatibility but hot damn is that one hell of a footgun. Even with that in place would you ever feel confident returning an id that didn't fit in a double knowing the sheer amount of code that definitely didn't handle that case?

Re: BigInt Shipping in Firefox

#36
post #5

Anyone knows how does bigint serialization and deserialization to JSON look like in practice in Chrome and Firefox?

You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get() { "use strict"; return () => String(this); } }); Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might f…

Only in JS do people seem okay with constantly mucking with built-ins.

Re: BigInt Shipping in Firefox

#37
post #11
post #5

Anyone knows how does bigint serialization and deserialization to JSON look like in practice in Chrome and Firefox?

JSON serialization does not seem to be supported yet. `JSON.serialize(42n)` gives me a TypeError "Do not know how to serialize a BigInt" in Chrome. Deserialization support also doesn't exist as far as I'm aware. Would be great to be able to choose to decode integer JSON literals as BigInt, but I'm not holding my breath.

You could decode all of them as BigInt, but then you would be accepting a large overhead for the much greater number of constants like 1 or 42 where it's unnecessary. Everywhere you pass that JSON data would need to be updated to operate in terms of BigInt instead of number.

If you only decoded big ones as BigInt, you would have the same problem of updating the code but now you have 2 codepaths at every callsite!

It's perhaps a defect in the JSON spec that it doesn't have a provision to support BigInt, but it's not really clear what should happen!

Re: BigInt Shipping in Firefox

#38
post #13
post #7

I feel sorry for whoever has to write the polyfill for this...

Yeah, the Chrome team basically recommended not to, favoring using something like Babel instead: https://developers.google.com/web/updates/2018/05/bigint#pol...

> and they are also making it infeasible (in most cases) to transpile BigInt code to fallback code using Babel or similar tools.

That doesn't sound like a recommendation.

Re: BigInt Shipping in Firefox

#39
post #21
post #10

Earlier quoted context omitted.

Afaict, JSON serialization is not supported yet, got a "TypeError: BigInt value can't be serialized in JSON". I'm on Firefox Developer edition 68.0b3.

I suppose one could use a replacer[0] when calling JSON.stringify and a reviver[1] when calling JSON.parse [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

You can certainly serialize to a string but knowing when you should revive to a BigInt seems tricky. You could prefix the strings I guess?

Re: BigInt Shipping in Firefox

#40
post #34

Earlier quoted context omitted.

You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get() { "use strict"; return () => String(this); } }); Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might f…

I'm sure its for backwards compatibility but hot damn is that one hell of a footgun. Even with that in place would you ever feel confident returning an id that didn't fit in a double knowing the sheer amount of code that definitely didn't handle that case?

Only Strings are safe,

forty years past K&R.

Wish I knew Tcl.

Post reply on HN