What's with the comment at the end of the blog entry? lol.
BigInt Shipping in Firefox
31–40 of 172 posts
Re: BigInt Shipping in Firefox
#32Re: BigInt Shipping in Firefox
#33I 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…
Re: BigInt Shipping in Firefox
#34Anyone 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…
Re: BigInt Shipping in Firefox
#35What's with the comment at the end of the blog entry? lol.
Re: BigInt Shipping in Firefox
#36Anyone 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…
Re: BigInt Shipping in Firefox
#37Anyone 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.
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
#38I 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...
That doesn't sound like a recommendation.
Re: BigInt Shipping in Firefox
#39Earlier 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...
Re: BigInt Shipping in Firefox
#40Earlier 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?
forty years past K&R.
Wish I knew Tcl.