Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] box_n/unbox_n, serializing the P6num repr
  • Loading branch information
pmurias committed Sep 24, 2015
1 parent 3616d0a commit 24662cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -1222,6 +1222,9 @@ class QAST::OperationsJS {
add_simple_op('box_i', $T_OBJ, [$T_INT, $T_OBJ]);
add_simple_op('unbox_i', $T_INT, [$T_OBJ]);

add_simple_op('box_n', $T_OBJ, [$T_NUM, $T_OBJ]);
add_simple_op('unbox_n', $T_NUM, [$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 @@ -416,3 +416,14 @@ op.iscont = function(cont) {
op.decont = function(ctx, cont) {
return cont.$$decont ? cont.$$decont(ctx) : cont;
};

op.box_n = function(n, type) {
var repr = type._STable.REPR;
var obj = repr.allocate(type._STable);
obj.$$set_num(n);
return obj;
};

op.unbox_n = function(obj) {
return obj.$$get_num();
};
26 changes: 24 additions & 2 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -386,9 +386,31 @@ module.exports.P6int = P6int;
function P6num() {
}

P6num.prototype.create_obj_constructor = basic_constructor;
P6num.name = 'P6int';
P6num.prototype.allocate = basic_allocate;
P6num.prototype.basic_constructor = basic_constructor;
P6num.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_num', function(value) {
this.value = value;
});
STable.addInternalMethod('$$get_num', function() {
return this.value;
});
return c;
};

P6num.prototype.serialize = function(data, object) {
data.double(object.value);
};

P6num.prototype.deserialize_finish = function(object, data) {
object.value = data.double();
};

P6num.name = 'P6num';
module.exports.P6num = P6num;

function P6str() {
Expand Down

0 comments on commit 24662cb

Please sign in to comment.