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

Fix uint32 bug #109

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/write-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,28 @@ function getWriteType(options) {
}

function number(encoder, value) {
var ivalue = value | 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be leaved as is: it has the same semantics, but there are performance consideration to take into account. See https://jsperf.com/math-floor-alternatives

var type;
if (value !== ivalue) {
if (Math.floor(value) != value) {
// float 64 -- 0xcb
type = 0xcb;
token[type](encoder, value);
return;
} else if (-0x20 <= ivalue && ivalue <= 0x7F) {
} else if (-0x20 <= value && value <= 0x7F) {
// positive fixint -- 0x00 - 0x7f
// negative fixint -- 0xe0 - 0xff
type = ivalue & 0xFF;
} else if (0 <= ivalue) {
type = value & 0xFF;
} else if (0 <= value) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
type = (ivalue <= 0xFF) ? 0xcc : (ivalue <= 0xFFFF) ? 0xcd : 0xce;
type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this, together with line 74 are the most important. All the others should be reversed.

} else {
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
type = (-0x80 <= ivalue) ? 0xd0 : (-0x8000 <= ivalue) ? 0xd1 : 0xd2;
type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3;
}
token[type](encoder, ivalue);
token[type](encoder, value);
}

// uint 64 -- 0xcf
Expand Down