Repository navigation
Conversation
…ers with arbitrary performance
| * @param {Array<Number|BigInt|String>} parts - Slices to encode | ||
| */ | ||
| constructor(args) { | ||
| super(); |
There was a problem hiding this comment.
to enforce abstraction, should we add the check:
if (this.constructor === LargeInt) {
throw new Error("cannot create abstract class")
}
here in constructor and on the static methods, read, write?
There was a problem hiding this comment.
That's the performance-critical code, so my intention was to reduce the amount of (probably) excessive typechecks in read/write functions. Have no strong objections regarding the constructor.
There was a problem hiding this comment.
Had a similar thought re: read/write
sreuland
left a comment
There was a problem hiding this comment.
minor comments for consideration, great test coverage also!
| if (i > 0) { | ||
| value >>= shift; | ||
| } | ||
| result[i] = BigInt.asIntN(sliceSize, value & mask); // clamp value |
There was a problem hiding this comment.
Shouldn't this be BigInt.asUintN to get the "bytes"? Incidentally, I think BigInt.asUintN also masks based on the sliceSize, so you don't need both.
There was a problem hiding this comment.
You're right about the mask, but I'm not sure about the "signedness" here... @orbitlens?
There was a problem hiding this comment.
Don't remember why exactly I used BigInt.asIntN, but if I'm not mistaken, some tests were failing with BigInt.asUintN in this place. Probably because you can encode, say, u64 from two i32 parts, but other similar serialization libraries throw an error if you try to provide two u32 parts to reconstruct u64. The convention is to use i32, not u32 for constructing large integers. So it makes sense to always deconstruct an integer to signed smaller ints.
There was a problem hiding this comment.
fair enough, added a comment to that regard.
we should prooooobably modify slice() to use asUintN for UnsignedHyper, but given that's a new method anyway, it should be fine as-is too
There was a problem hiding this comment.
we should prooooobably modify slice() to use asUintN for UnsignedHyper, but given that's a new method anyway, it should be fine as-is too
To my mind, the output of the .slice() method should be uniform for all integers regardless of the size and representation. I'd rather stick to outputting uniform signed integers as a result of this method invocation, otherwise it may result in confusion. So using the asIntN() for all ints looks slightly more appealing to me.
There was a problem hiding this comment.
fine with me! you have a way better perspective given you are likely to be a user of these details so we can keep it as-is
| throw new TypeError(`Invalid i64 string value: ${string}`); | ||
| return new Hyper(BigInt(string)); | ||
| get unsigned() { | ||
| return false; |
There was a problem hiding this comment.
Would these be easier to understand if it was
get signed() {
true
}Instead of phrasing it with the double-negative, of not-unsigned.
buuuuut that might break everything, so...
There was a problem hiding this comment.
I guess I saw such approach in some other serialization library. For example, Long in bson serializer.
|
lol I feel like @orbitlens should reply to the review comments since they're mostly related to code he wrote; I don't feel like I have enough context on his choices to discuss them |
This should probably be done in the type-specific values 🤔
This supersedes #96, incorporating #96 (comment) and separating out the 128 and 256-bit integers to
stellar-base. This is a dependency of stellar/js-stellar-base#620.TODO:
(Unsigned)Hyperconstructor changes?If you have reviewed #96, you can reference this diff to see the changes that this adds on top of that branch: it's just moving files and adding jsdocs.
cc @orbitlens