Skip to content

Commit

Permalink
[js] Implement nqp::serializetobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Nov 9, 2018
1 parent 22ecefd commit 57848da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/vm/js/Operations.nqp
Expand Up @@ -715,6 +715,8 @@ class QAST::OperationsJS {
%ops<callstatic> := %ops<call>;

add_simple_op('serialize', $T_STR, [$T_OBJ, $T_OBJ], :side_effects);
add_simple_op('serializetobuf', $T_OBJ, [$T_OBJ, $T_OBJ, $T_OBJ], :side_effects);

add_simple_op('scobjcount', $T_INT, [$T_OBJ]);
add_simple_op('createsc', $T_OBJ, [$T_STR], :side_effects);
add_simple_op('deserialize', $T_OBJ, [$T_STR, $T_OBJ, $T_OBJ, $T_OBJ, $T_OBJ], :side_effects, :takes_hll);
Expand Down
2 changes: 2 additions & 0 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -1108,6 +1108,8 @@ function writeBuffer(highLevel, lowLevel) {
}
}

exports.writeBuffer = writeBuffer;

op.encodeconf = function(str, encoding_, output, permissive) {
if (output.array.length) {
throw new NQPException('encode requires an empty array');
Expand Down
12 changes: 10 additions & 2 deletions src/vm/js/nqp-runtime/serialization.js
Expand Up @@ -20,6 +20,8 @@ const Ctx = require('./ctx.js');

const constants = require('./constants.js');

const core = require('./core.js');

const op = {};
exports.op = op;

Expand Down Expand Up @@ -986,7 +988,7 @@ class SerializationWriter {
throw `Serialization sanity check failed: offset != outputSize (${offset} != ${outputSize})`;
}

return this.buffer.toString('base64');
return this.buffer;
}

serialize() {
Expand All @@ -997,9 +999,15 @@ class SerializationWriter {

op.serialize = function(sc, sh) {
const writer = new SerializationWriter(sc, sh.array);
return writer.serialize();
return writer.serialize().toString('base64');
};

op.serializetobuf = function(sc, sh, type) {
const writer = new SerializationWriter(sc, sh.array);
const buffer = type._STable.REPR.allocate(type._STable);
core.writeBuffer(buffer, writer.serialize());
}

op.scsetobj = function(sc, idx, obj) {
sc.setObj(idx, obj);
return obj;
Expand Down

0 comments on commit 57848da

Please sign in to comment.