Skip to content

Commit 1293b60

Browse files
committed
Finish varint implementation
1 parent f3184f1 commit 1293b60

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/vm/js/nqp-runtime/deserialization.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ BinaryCursor.prototype.hashOfVariants = function() {
124124

125125
/** Read a int of variable length */
126126
/* TODO - make it work correctly for values bigger than 32bit integers*/
127+
// XXX TODO - length checks
127128
BinaryCursor.prototype.varint = function() {
128129
var result;
129130
var first;
@@ -138,7 +139,6 @@ BinaryCursor.prototype.varint = function() {
138139

139140
need = first >> 4;
140141

141-
// XXX TODO - length checks
142142
if (!need) {
143143
// unrolled loop for optimization
144144
result =
@@ -154,8 +154,19 @@ BinaryCursor.prototype.varint = function() {
154154

155155
return result;
156156
}
157-
// TODO
158-
throw new Error('Reading a varint with need is NYI');
157+
158+
result = first << 8 * need;
159+
160+
var shift_places = 0;
161+
for(var i = 0; i < need; i++) {
162+
var byte = buffer.readUInt8(this.offset++);
163+
result |= (byte << shift_places);
164+
shift_places += 8;
165+
}
166+
result = result << (64 - 4 - 8 * need);
167+
result = result >> (64 - 4 - 8 * need);
168+
169+
return result;
159170
}
160171

161172
/** Read a variant reference */

0 commit comments

Comments
 (0)