Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Serialize variable length integers up to 32bit.
Add a unit test for that.
  • Loading branch information
pmurias committed Oct 4, 2015
1 parent 71dbe59 commit c28a669
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/vm/js/bin/run_tests
@@ -1,3 +1,4 @@
#!/bin/bash
# 19 and 30 where moved out as they were parrot specific, 52,54 is missing, we can't pass 49 till we are bootstraped
prove -e 'node' t/js/varint.js # unit tests for the js runtime
prove "$@" -e './nqp-js' gen/js/qregex.t t/nqp/{01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,25,26,27,28,31,32,33,34,35,36,37,38,39,40,41,42,44,45,46,47,48,50,51,53,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,88,89,90,91,92,93,94,95,96}*.t t/js/getcomp-js.t t/qast/02* t/serialization/02-types.t
7 changes: 5 additions & 2 deletions src/vm/js/nqp-runtime/serialization.js
Expand Up @@ -67,6 +67,7 @@ BinaryWriteCursor.prototype.varint = function(value) {
storage_needed = 4;
else if (abs_val <= 0x00000007FFFFFFFF)
storage_needed = 5;
else console.log("TODO serializing bigger integers");

/* TODO bigger numbers */
/*else if (abs_val <= 0x000007FFFFFFFFFFLL)
Expand Down Expand Up @@ -94,8 +95,10 @@ BinaryWriteCursor.prototype.varint = function(value) {
|| (nybble >> 3) == ~0);

this.I8((rest << 4) | (nybble & 0xF));
console.log("TODO - writing varints that take 2-8 bytes", value, storage_needed);
//memcpy(buffer + offset, &value, rest);

this.growToHold(rest);
this.buffer.writeIntLE(value, this.offset, rest, true);
this.offset += rest;
}
};

Expand Down
37 changes: 37 additions & 0 deletions t/js/varint.js
@@ -0,0 +1,37 @@
var tap = require('tap');
var BinaryWriteCursor = require('nqp-runtime/serialization').BinaryWriteCursor;
var BinaryCursor = require('nqp-runtime/deserialization').BinaryCursor;


function check(n, expected) {
expected = new Buffer(expected);
var cursor = new BinaryWriteCursor(null);
cursor.varint(n);
var got = cursor.buffer.slice(0, cursor.offset);

var correct = got.equals(expected);
tap.ok(correct, "writing " + n);

if (!correct) {
tap.comment("got", got);
tap.comment("expected", expected);
}


var reader = new BinaryCursor(expected, 0, null, null);

var got_n = reader.varint();

tap.equal(got_n, n, "reading "+n);
}

check(-1, [128]);
check(0, [129]);
check(1, [130]);
check(30, [159]);
check(126, [255]);
check(200, [16,200]);
check(-200, [31,56]);
check(10000, [32,16,39]);
check(-10000, [47,240,216]);
check(1000000, [48,64,66,15]);

0 comments on commit c28a669

Please sign in to comment.