Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Round trip a P6int containing a small integer correctly.
  • Loading branch information
pmurias committed Sep 24, 2015
1 parent bea9d2d commit 3616d0a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -721,6 +721,7 @@ class QAST::OperationsJS {
add_simple_op('createsc', $T_OBJ, [$T_STR], :sideffects);
add_simple_op('deserialize', $T_OBJ, [$T_STR, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :sideffects);
add_simple_op('scsetobj', $T_OBJ, [$T_OBJ, $T_INT, $T_OBJ], :sideffects);
add_simple_op('scgetobj', $T_OBJ, [$T_OBJ, $T_INT], :sideffects);
add_simple_op('setobjsc', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects);

# Ops for NFA
Expand Down
5 changes: 5 additions & 0 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -370,6 +370,11 @@ P6int.prototype.deserialize_finish = function(object, data) {
object.value = data.varint();
};

P6int.prototype.serialize = function(data, object) {
// TODO integers bigger than 32bit
data.varint(object.value);
};


P6int.prototype.type_object_for = function(HOW) {
var type_object = this.basic_type_object_for(HOW);
Expand Down
71 changes: 70 additions & 1 deletion src/vm/js/nqp-runtime/serialization.js
Expand Up @@ -33,6 +33,56 @@ BinaryWriteCursor.prototype.string = function(str) {
this.I32(this.writer.stringIndex(str));
};

/* Writing function for variable sized integers. Writes out a 64 bit value
using between 1 and 9 bytes. */
BinaryWriteCursor.prototype.varint = function(value) {
var storage_needed;

if (value >= -1 && value <= 126) {
storage_needed = 1;
} else {
var abs_val = value < 0 ? -value - 1 : value;

if (abs_val <= 0x7FF)
storage_needed = 2;
else if (abs_val <= 0x000000000007FFFF)
storage_needed = 3;
else if (abs_val <= 0x0000000007FFFFFF)
storage_needed = 4;
else if (abs_val <= 0x00000007FFFFFFFF)
storage_needed = 5;

/* TODO bigger numbers */
/*else if (abs_val <= 0x000007FFFFFFFFFFLL)
storage_needed = 6;
else if (abs_val <= 0x0007FFFFFFFFFFFFLL)
storage_needed = 7;
else if (abs_val <= 0x07FFFFFFFFFFFFFFLL)
storage_needed = 8;
else
storage_needed = 9;*/
}

if (storage_needed == 1) {
this.U8(0x80 | (value + 129));
} else if (storage_needed == 9) {
this.I8(0x00);
this.I64(value);
} else {
var rest = storage_needed - 1;
var nybble = value >> 8 * rest;
/* All the other high bits should be the same as the top bit of the
nybble we keep. Or we have a bug. */

console.assert((nybble >> 3) == 0
|| (nybble >> 3) == ~0);

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

SerializationWriter.prototype.stringIndex = function(str) {
/* The first entry in the heap represents the null string */
var idx = this.sh.indexOf(str);
Expand All @@ -43,12 +93,27 @@ SerializationWriter.prototype.stringIndex = function(str) {
return idx;
};

BinaryWriteCursor.prototype.I8 = function(value) {
this.growToHold(1);
this.buffer.writeInt8(value, this.offset);
this.offset += 1;
};

BinaryWriteCursor.prototype.U8 = function(value) {
this.growToHold(1);
this.buffer.writeUInt8(value, this.offset);
this.offset += 1;
};


BinaryWriteCursor.prototype.I32 = function(value) {
this.growToHold(4);
this.buffer.writeInt32LE(value, this.offset);
this.offset += 4;
};


/* TODO - numbers bigger than 32bit */
BinaryWriteCursor.prototype.I64 = function(value) {
this.growToHold(8);
this.buffer.writeInt32LE(value, this.offset);
Expand Down Expand Up @@ -79,7 +144,7 @@ SerializationWriter.prototype.serializeObject = function(obj) {
var sc = ref[0];
var sc_idx = ref[1];

var packed = obj._type_object ? OBJECTS_TABLE_ENTRY_IS_CONCRETE : 0;
var packed = !obj._type_object ? OBJECTS_TABLE_ENTRY_IS_CONCRETE : 0;

if (sc <= OBJECTS_TABLE_ENTRY_SC_MAX && sc_idx <= OBJECTS_TABLE_ENTRY_SC_IDX_MAX) {
packed |= (sc << OBJECTS_TABLE_ENTRY_SC_SHIFT) | sc_idx;
Expand Down Expand Up @@ -327,6 +392,10 @@ op.scsetobj = function(sc, idx, obj) {
return obj;
};

op.scgetobj = function(sc, idx) {
return sc.root_objects[idx];
};

op.setobjsc = function(obj, sc) {
obj._SC = sc;
return obj;
Expand Down

0 comments on commit 3616d0a

Please sign in to comment.