Skip to content

Commit

Permalink
[js] Use NQPNum for native nums in nqp instead of using monkey patching
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Feb 13, 2018
1 parent bc476c1 commit 351f0fc
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 76 deletions.
5 changes: 4 additions & 1 deletion src/vm/js/Compiler.nqp
Expand Up @@ -619,7 +619,10 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
}

if $*HLL eq 'nqp' {
if $got == $T_NUM || $got == $T_STR {
if $got == $T_NUM {
return Chunk.new($T_OBJ, "new nqp.NQPNum({$chunk.expr})", $chunk);
}
elsif $got == $T_STR {
return $chunk;
}
elsif $got == $T_INT {
Expand Down
4 changes: 2 additions & 2 deletions src/vm/js/RegexCompiler.nqp
Expand Up @@ -59,7 +59,7 @@ class RegexCompiler {
"{$!pos} = nqp.toInt($start[2], $*CTX);\n",
($!has_cursor_type ?? '' !! "{$!cursor_type_runtime} = $start[3];\n"),
"{$!bstack} = $start[4].array;\n",
"{$!restart} = $start[5];\n",
"{$!restart} = nqp.toInt($start[5], $*CTX);\n",
"if ($!pos > $!target.length) \{$!label = $!fail_label\}\n",
"if ($!restart) \{$!label = $restart_label\}\n",
"{$!js_loop_label}: while (1) \{\nswitch ({$!label}) \{\n",
Expand Down Expand Up @@ -474,7 +474,7 @@ class RegexCompiler {
self.compile_rx($node[0]),
self.peek($fail_label,$subcapture_from),
self.set_cursor_pos,
"$!subcur = " ~ call($!cursor, '!cursor_start_subcapture', $subcapture_from) ~ ";\n",
"$!subcur = " ~ call($!cursor, '!cursor_start_subcapture', "new nqp.NativeIntArg($subcapture_from)") ~ ";\n",
call($!subcur, '!cursor_pass', "new nqp.NQPInt($!pos)") ~ ";\n",
"$!cstack = " ~ call($!cursor, '!cursor_capture', $!subcur, quote_string($node.name)) ~ ".array;\n",
self.goto($done_label),
Expand Down
8 changes: 5 additions & 3 deletions src/vm/js/nqp-runtime/bignum.js
Expand Up @@ -7,6 +7,8 @@ const core = require('./core.js');

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

const NQPInt = require('./nqp-int.js');

const op = {};
exports.op = op;
function intishBool(b) {
Expand Down Expand Up @@ -255,12 +257,12 @@ op.bool_I = function(n) {
op.radix_I = function(currentHLL, radix, str, zpos, flags, type) {
const extracted = core.radixHelper(radix, str, zpos, flags);
if (extracted == null) {
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1]);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(0)), makeBI(type, bignum(1)), new NQPInt(-1)]);
}

if (radix == 10 || radix == 16) {
const pow = bignum(radix).pow(extracted.power);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset]);
return hll.slurpyArray(currentHLL, [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), new NQPInt(extracted.offset)]);
} else {
const n = extracted.number;
let base = bignum(1);
Expand All @@ -279,6 +281,6 @@ op.radix_I = function(currentHLL, radix, str, zpos, flags, type) {

if (n[0] == '-') result = result.neg();

return hll.slurpyArray(currentHLL, [makeBI(type, result), makeBI(type, base), extracted.offset]);
return hll.slurpyArray(currentHLL, [makeBI(type, result), makeBI(type, base), new NQPInt(extracted.offset)]);
}
};
22 changes: 12 additions & 10 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -11,6 +11,7 @@ const CodeRef = require('./code-ref.js');
const Ctx = require('./ctx.js');

const NQPInt = require('./nqp-int.js');
const NQPNum = require('./nqp-num.js');

const NQPException = require('./nqp-exception.js');

Expand Down Expand Up @@ -160,10 +161,11 @@ exports.radixHelper = radixHelper;
op.radix = function(currentHLL, radix, str, zpos, flags) {
const extracted = radixHelper(radix, str, zpos, flags);
if (extracted == null) {
return hll.slurpyArray(currentHLL, [0, 1, -1]);
return hll.slurpyArray(currentHLL, [new NQPInt(0), new NQPInt(1), new NQPInt(-1)]);
}
const pow = Math.pow(radix, extracted.power);
return hll.slurpyArray(currentHLL, [parseInt(extracted.number, radix), pow, extracted.offset]);
// TODO: use hll int type
return hll.slurpyArray(currentHLL, [new NQPInt(parseInt(extracted.number, radix)), new NQPInt(pow), new NQPInt(extracted.offset)]);
};

op.setdebugtypename = function(type, debugName) {
Expand Down Expand Up @@ -195,7 +197,7 @@ const intToObj = exports.intToObj = function(currentHLL, i) {
const numToObj = exports.numToObj = function(currentHLL, n) {
const type = currentHLL.get('num_box');
if (!type) {
return n;
return new NQPNum(n);
} else {
const repr = type._STable.REPR;
const obj = repr.allocate(type._STable);
Expand Down Expand Up @@ -385,7 +387,7 @@ op.reprname = function(obj) {
return 'P6int';
} else if (typeof obj == 'string') {
return 'P6str';
} else if (typeof obj == 'number') {
} else if (obj instanceof NQPNum) {
return 'P6num';
} else {
console.log(obj);
Expand Down Expand Up @@ -501,7 +503,6 @@ op.box_n = function(n, type) {
};

op.unbox_n = function(obj) {
if (typeof obj == 'number') return obj;
return obj.$$getNum();
};

Expand All @@ -526,7 +527,6 @@ op.box_i = function(i, type) {
};

op.unbox_i = function(obj) {
if (typeof obj == 'number') return obj;
return obj.$$getInt();
};

Expand Down Expand Up @@ -599,6 +599,8 @@ function fromJS(obj) {
return new WrappedFunction(obj);
} else if (obj === undefined || obj === null) {
return Null;
} else if (typeof obj === 'number') {
return new NQPNum(obj);
} else {
return obj;
}
Expand Down Expand Up @@ -775,7 +777,7 @@ function getConfigFromPerl() {

op.backendconfig = function() {
const config = new Hash();
config.content.set('intvalsize', 4);
config.content.set('intvalsize', new NQPInt(4));
config.content.set('osname', os.platform());
const nativecallConfig = getConfigFromPerl();
for (const key of Object.keys(nativecallConfig)) {
Expand Down Expand Up @@ -811,7 +813,7 @@ op.setpayload = function(exception, payload) {
};

op.isnum = function(value) {
return (typeof value == 'number') ? 1 : 0;
return (value instanceof NQPNum) ? 1 : 0;
};

op.isint = function(value) {
Expand Down Expand Up @@ -968,11 +970,11 @@ op.objprimspec = function(obj) {
if (typeof obj === 'object') {
if (obj instanceof NQPInt) {
return 1;
} else if (obj instanceof NQPNum) {
return 2;
} else {
return (obj._STable && obj._STable.REPR.boxedPrimitive ? obj._STable.REPR.boxedPrimitive : 0);
}
} else if (typeof obj == 'number') {
return 2;
} else if (typeof obj == 'string') {
return 3;
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/vm/js/nqp-runtime/deserialization.js
Expand Up @@ -19,6 +19,9 @@ const hll = require('./hll.js');

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

const NQPInt = require('./nqp-int.js');
const NQPNum = require('./nqp-num.js');

/* Possible reference types we can serialize. */
const REFVAR_NULL = 1;
const REFVAR_OBJECT = 2;
Expand Down Expand Up @@ -311,10 +314,9 @@ class BinaryCursor {
case REFVAR_VM_NULL:
return Null;
case REFVAR_VM_INT:
// TODO deserialize bigger integers then can fit into a 32bit number
return this.varint();
return new NQPInt(this.varint());
case REFVAR_VM_NUM:
return this.double();
return new NQPNum(this.double());
case REFVAR_VM_STR:
return this.str();
case REFVAR_VM_ARR_VAR:
Expand Down
5 changes: 3 additions & 2 deletions src/vm/js/nqp-runtime/hll.js
Expand Up @@ -2,6 +2,7 @@
const Hash = require('./hash.js');
const CodeRef = require('./code-ref.js');
const NQPInt = require('./nqp-int.js');
const NQPNum = require('./nqp-num.js');
const Null = require('./null.js');
const BOOT = require('./BOOT.js');

Expand Down Expand Up @@ -58,11 +59,11 @@ op.hllizefor = function(ctx, obj, language) {
const boxed = repr.allocate(foreignTypeInt._STable);
boxed.$$setInt(obj.value);
return boxed;
} else if (typeof obj == 'number') {
} else if (obj instanceof NQPNum) {
const foreignTypeNum = config.get('foreign_type_num');
const repr = foreignTypeNum._STable.REPR;
const boxed = repr.allocate(foreignTypeNum._STable);
boxed.$$setNum(obj);
boxed.$$setNum(obj.value);
return boxed;
} else if (typeof obj == 'string') {
const foreignTypeStr = config.get('foreign_type_str');
Expand Down
3 changes: 2 additions & 1 deletion src/vm/js/nqp-runtime/multicache.js
@@ -1,5 +1,6 @@
'use strict';
const NQPInt = require('./nqp-int.js');
const NQPNum = require('./nqp-num.js');
const Null = require('./null.js');

const nativeArgs = require('./native-args.js');
Expand Down Expand Up @@ -62,7 +63,7 @@ function posTypes(ctx, capture) {
types[i] = 3;
} else if (obj instanceof NQPInt) {
types[i] = 1;
} else if (typeof obj == 'number') {
} else if (obj instanceof NQPNum) {
types[i] = 2;
} else if (typeof obj == 'string') {
types[i] = 3;
Expand Down
37 changes: 37 additions & 0 deletions src/vm/js/nqp-runtime/nqp-num.js
@@ -0,0 +1,37 @@
'use strict';

const NQPObject = require('./nqp-object.js');

class NQPNum extends NQPObject {
constructor(value) {
super();
this.value = value;
}

$$toBool(ctx) {
return (this.value === 0 ? 0 : 1);
}

$$istype(ctx, type) {
return 0;
}

$$can(ctx, name) {
return 0;
}

$$clone() {
return this;
}

$$getInt() {
console.log('Numer - getInt what?');
return this.value;
}

$$getNum() {
return this.value;
}
};

module.exports = NQPNum;
44 changes: 32 additions & 12 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -1310,26 +1310,46 @@ class VMArray extends REPR {
}

deserializeFinish(obj, data) {
if (this.type !== Null) {
console.log('NYI: VMArrays of a type different then null');
}

obj.array = [];
const size = data.varint();
for (let i = 0; i < size; i++) {
obj.array[i] = data.variant();
if (this.primType === 0) {
for (let i = 0; i < size; i++) {
obj.array[i] = data.variant();
}
} else if (this.primType === 1) {
for (let i = 0; i < size; i++) {
obj.array[i] = data.varint();
}
} else if (this.primType === 2) {
for (let i = 0; i < size; i++) {
obj.array[i] = data.double();
}
} else if (this.primType === 3) {
for (let i = 0; i < size; i++) {
obj.array[i] = data.str();
}
}
}


serialize(cursor, obj) {
if (this.type !== Null) {
console.log('NYI: VMArrays of a type different then null');
}

cursor.varint(obj.array.length);
for (let i = 0; i < obj.array.length; i++) {
cursor.ref(obj.array[i] === undefined ? Null : obj.array[i]);
if (this.primType === 0) {
for (let i = 0; i < obj.array.length; i++) {
cursor.ref(obj.array[i] === undefined ? Null : obj.array[i]);
}
} else if (this.primType === 1) {
for (let i = 0; i < obj.array.length; i++) {
cursor.varint(obj.array[i] === undefined ? 0 : obj.array[i]);
}
} else if (this.primType === 2) {
for (let i = 0; i < obj.array.length; i++) {
cursor.double(obj.array[i] === undefined ? 0 : obj.array[i]);
}
} else if (this.primType === 3) {
for (let i = 0; i < obj.array.length; i++) {
cursor.str(obj.array[i] === undefined ? nullStr : obj.array[i]);
}
}
}

Expand Down

0 comments on commit 351f0fc

Please sign in to comment.