Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Implement box_s/unbox_s. Serializing a P6str.
  • Loading branch information
pmurias committed Sep 24, 2015
1 parent 24662cb commit cf7b3a1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -1225,6 +1225,9 @@ class QAST::OperationsJS {
add_simple_op('box_n', $T_OBJ, [$T_NUM, $T_OBJ]);
add_simple_op('unbox_n', $T_NUM, [$T_OBJ]);

add_simple_op('box_s', $T_OBJ, [$T_STR, $T_OBJ]);
add_simple_op('unbox_s', $T_STR, [$T_OBJ]);

add_simple_op('iseq_I', $T_INT, [$T_OBJ, $T_OBJ]);

# bigint arithmetic operators operators
Expand Down
11 changes: 11 additions & 0 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -427,3 +427,14 @@ op.box_n = function(n, type) {
op.unbox_n = function(obj) {
return obj.$$get_num();
};

op.box_s = function(value, type) {
var repr = type._STable.REPR;
var obj = repr.allocate(type._STable);
obj.$$set_str(value);
return obj;
};

op.unbox_s = function(obj) {
return obj.$$get_str();
};
25 changes: 24 additions & 1 deletion src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -415,7 +415,30 @@ module.exports.P6num = P6num;

function P6str() {
}
P6str.prototype.create_obj_constructor = basic_constructor;

P6str.prototype.allocate = basic_allocate;
P6str.prototype.basic_constructor = basic_constructor;
P6str.prototype.create_obj_constructor = function(STable) {
var c = this.basic_constructor(STable);

STable.obj_constructor = c; // HACK it's set again later, we set it for addInternalMethod

STable.addInternalMethod('$$set_str', function(value) {
this.value = value;
});
STable.addInternalMethod('$$get_str', function() {
return this.value;
});
return c;
};

P6str.prototype.serialize = function(data, object) {
data.str(object.value);
};

P6str.prototype.deserialize_finish = function(object, data) {
object.value = data.str();
};

P6str.name = 'P6str';
module.exports.P6str = P6str;
Expand Down
6 changes: 3 additions & 3 deletions src/vm/js/nqp-runtime/serialization.js
Expand Up @@ -25,7 +25,7 @@ BinaryWriteCursor.prototype.growToHold = function(space) {
}
};

BinaryWriteCursor.prototype.string = function(str) {
BinaryWriteCursor.prototype.str = function(str) {
if (str === undefined) {
console.trace('undefined string');
str = '?';
Expand Down Expand Up @@ -275,8 +275,8 @@ SerializationWriter.prototype.getSCId = function(sc) {
/* Otherwise, need to add it to our dependencies list. */
this.dependentSCs.push(sc);

this.deps.string(sc.handle);
this.deps.string(sc.description);
this.deps.str(sc.handle);
this.deps.str(sc.description);

return this.dependentSCs.length; /* Deliberately index + 1. */
};
Expand Down

0 comments on commit cf7b3a1

Please sign in to comment.