Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
A tiny bit of progress on deserializing objects.
  • Loading branch information
pmurias committed Sep 24, 2014
1 parent 4d1f45f commit 489db46
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -1344,6 +1344,14 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
self.declare_var($node);
self.compile_var($node);
}

multi method as_js(QAST::WVal $node, :$want) {
my $value := $node.value;
my $sc := nqp::getobjsc($value);
my $handle := nqp::scgethandle($sc);
my $idx := nqp::scgetobjidx($sc, $value);
Chunk.new($T_OBJ, "nqp.wval({quote_string($handle)},$idx)", []);
}

method var_is_lexicalish(QAST::Var $var) {
$var.scope eq 'lexical' || $var.scope eq 'typevar';
Expand Down
83 changes: 82 additions & 1 deletion src/vm/js/nqp-runtime/deserialization.js
Expand Up @@ -6,12 +6,16 @@ var reprs = require('./reprs.js');
var sixmodel = require('./sixmodel.js');
var SerializationContext = require('./serialization-context');
var __6MODEL_CORE__ = require('./bootstrap.js').core;

var Hash = require('./hash.js');


/** All the loaded serialization contexts using their unique IDs as keys */
var serialization_contexts = {'__6MODEL_CORE__': __6MODEL_CORE__};

module.exports.wval = function(handle, idx) {
return serialization_contexts[handle].root_objects[idx];
};

op.deserialize = function(blob, sc, sh, cr, conflict) {
var buffer = new Buffer(blob, 'base64');
var cursor = new BinaryCursor(buffer, 0, sh, sc);
Expand Down Expand Up @@ -59,6 +63,16 @@ BinaryCursor.prototype.times = function(count, cb) {
return array;
};

/** Read an array of elements parsed by the callback */
BinaryCursor.prototype.array = function(readElem) {
var elems = this.I32();
var array = [];
for (var i = 0; i < elems; i++) {
array.push(readElem(this));
}
return array;
};

function SerializedObjRef(sc, id) {
this.sc = sc;
this.id = id;
Expand All @@ -73,6 +87,69 @@ BinaryCursor.prototype.objectEntry = function(objectsData) {
};
};

/** */
BinaryCursor.prototype.objRef = function() {
return sc.deps[this.I32()].root_objects[this.I32()];
};

/** Read a hash of variants */
BinaryCursor.prototype.hashOfVariants = function() {
var elems = this.I32();
var hash = new Hash();
for (var i = 0; i < elems; i++) {
var str = this.str();
hash[str] = this.variant();
}
return hash;
};

/** Read a variant reference */
BinaryCursor.prototype.variant = function() {
var type = this.buffer.readUInt16LE(this.offset);
this.offset += 2;
switch (type) {
case 2:
return this.objRef();
case 1:
case 3:
return null;
case 4:
//TODO: think about that
//FIXME: negative
var low = this.I32();
var high = this.I32();
return this.low + this.high * Math.pow(2, 32);
case 5:
return this.double();
case 6:
return this.str();
case 7:
return this.array(function(cursor) {return cursor.variant()});
case 8:
return this.array(function(cursor) {return cursor.str()});
case 9:
return this.array(function(cursor) {return cursor.I64()});
case 10:
return this.hashOfVariants(this);
case 11:
return new SerializedCodeRef(this.I32(), this.I32());
case 12:
return new SerializedCodeRef(this.I32(), this.I32());
default:
console.trace('unknown variant');
throw 'unknown variant: ' + type;
}

};

/** Read an entry from the STable table */
BinaryCursor.prototype.STable = function(STable) {
var STable = {};
STable.HOW = this.objRef();
STable.WHAT = this.objRef();
console.log(STable);
STable.WHO = this.variant();
}

/** Read a whole serialization context */
BinaryCursor.prototype.deserialize = function(sc) {
Expand Down Expand Up @@ -149,6 +226,10 @@ BinaryCursor.prototype.deserialize = function(sc) {
sc.root_objects[i]._SC = sc;
}

for (var i = 0; i < STables.length; i++) {
STables[i][1].STable(sc.root_stables[i]);
}

/*
var closures_offset = this.I32();
var closures_number = this.I32();
Expand Down
1 change: 1 addition & 0 deletions src/vm/js/nqp-runtime/runtime.js
Expand Up @@ -16,6 +16,7 @@ var io = require('./io.js');
load_ops(io);

var deserialization = require('./deserialization.js');
exports.wval = deserialization.wval;
load_ops(deserialization);


Expand Down

0 comments on commit 489db46

Please sign in to comment.