Skip to content

Commit d003e33

Browse files
committed
[js] Avoid special casing Array. Make nqp::radix* ops return NQPArrays.
1 parent c0eed50 commit d003e33

File tree

5 files changed

+20
-79
lines changed

5 files changed

+20
-79
lines changed

src/vm/js/Operations.nqp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ class QAST::OperationsJS {
673673

674674
add_simple_op('elems', $T_INT, [$T_OBJ]);
675675

676-
add_simple_op('islist', $T_BOOL, [$T_OBJ], sub ($obj) {"($obj instanceof Array || $obj instanceof nqp.NQPArray)"});
676+
add_simple_op('islist', $T_BOOL, [$T_OBJ], sub ($obj) {"($obj instanceof nqp.NQPArray)"});
677677

678678

679679

src/vm/js/nqp-runtime/bignum.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var bignum = require('bignum');
44

55
var core = require('./core.js');
66

7+
var NQPArray = require('./array.js');
8+
79
var op = {};
810
exports.op = op;
911

@@ -218,12 +220,12 @@ op.bool_I = function(n) {
218220
op.radix_I = function(radix, str, zpos, flags, type) {
219221
var extracted = core.radix_helper(radix, str, zpos, flags);
220222
if (extracted == null) {
221-
return [makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1];
223+
return new NQPArray([makeBI(type, bignum(0)), makeBI(type, bignum(1)), -1]);
222224
}
223225

224226
if (radix == 10 || radix == 16) {
225227
var pow = bignum(radix).pow(extracted.power);
226-
return [makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset];
228+
return new NQPArray([makeBI(type, bignum(extracted.number, radix)), makeBI(type, pow), extracted.offset]);
227229
} else {
228230
var n = extracted.number;
229231
var base = bignum(1);
@@ -243,6 +245,6 @@ op.radix_I = function(radix, str, zpos, flags, type) {
243245

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

246-
return [makeBI(type, result), makeBI(type, base), extracted.offset];
248+
return new NQPArray([makeBI(type, result), makeBI(type, base), extracted.offset]);
247249
}
248250
};

src/vm/js/nqp-runtime/core.js

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,11 @@ var NQPArray = require('./array.js');
1818
exports.CodeRef = CodeRef;
1919

2020
op.atpos = function(array, index) {
21-
if (array instanceof Array) {
22-
if (index < 0) {
23-
index = array.length + index;
24-
}
25-
return array[index];
26-
} else {
27-
return array.$$atpos(index);
28-
}
21+
return array.$$atpos(index);
2922
};
3023

3124
op.bindpos = function(array, index, value) {
32-
if (array instanceof Array) {
33-
if (index < 0) {
34-
index = array.length + index;
35-
}
36-
return (array[index] = value);
37-
} else {
38-
return array.$$bindpos(index, value);
39-
}
25+
return array.$$bindpos(index, value);
4026
};
4127

4228
op.getcomp = function(lang) {
@@ -93,10 +79,10 @@ exports.radix_helper = radix_helper;
9379
op.radix = function(radix, str, zpos, flags) {
9480
var extracted = radix_helper(radix, str, zpos, flags);
9581
if (extracted == null) {
96-
return [0, 1, -1];
82+
return new NQPArray([0, 1, -1]);
9783
}
9884
var pow = Math.pow(radix, extracted.power);
99-
return [parseInt(extracted.number, radix), pow, extracted.offset];
85+
return new NQPArray([parseInt(extracted.number, radix), pow, extracted.offset]);
10086
};
10187

10288
function Iter(array) {
@@ -155,8 +141,6 @@ IterPair.prototype.value = function(ctx, named) {
155141
op.iterator = function(obj) {
156142
if (obj instanceof NQPArray) {
157143
return new Iter(obj.array);
158-
} else if (obj instanceof Array) {
159-
return new Iter(obj);
160144
} else if (obj instanceof Hash) {
161145
return new HashIter(obj);
162146
} else if (obj instanceof LexPadHack) {
@@ -339,7 +323,7 @@ op.istype = function(obj, type) {
339323
}
340324

341325
// HACK
342-
if (obj instanceof Array || obj instanceof NQPArray) {
326+
if (obj instanceof NQPArray) {
343327
if (hll.hllConfigs.nqp && type == hll.hllConfigs.nqp.content.get('list')) {
344328
return 1;
345329
} else {
@@ -424,8 +408,6 @@ op.composetype = function(obj, reprinfo) {
424408
op.clone = function(obj) {
425409
if (obj.$$clone) {
426410
return obj.$$clone();
427-
} else if (obj instanceof Array) {
428-
return obj.slice();
429411
} else {
430412
// STUB
431413
console.log('NYI cloning', obj);
@@ -511,21 +493,12 @@ op.unbox_s = function(obj) {
511493
};
512494

513495
op.elems = function(obj) {
514-
if (obj instanceof Array) {
515-
return obj.length;
516-
} else {
517-
return obj.$$elems();
518-
}
496+
return obj.$$elems();
519497
};
520498

521499
op.setelems = function(obj, elems) {
522-
if (obj instanceof Array) {
523-
obj.length = elems;
524-
return obj;
525-
} else {
526-
obj.$$setelems(elems);
527-
return obj;
528-
}
500+
obj.$$setelems(elems);
501+
return obj;
529502
};
530503

531504
op.markcodestatic = function(code) {
@@ -915,57 +888,27 @@ op.getcodecuid = function(codeRef) {
915888
};
916889

917890
op.numdimensions = function(array) {
918-
if (array instanceof Array) {
919-
return 1;
920-
} else {
921-
return array.$$numdimensions();
922-
}
891+
return array.$$numdimensions();
923892
};
924893

925894
op.dimensions = function(array) {
926-
if (array instanceof Array) {
927-
return [array.length];
928-
} else {
929-
return array.$$dimensions();
930-
}
895+
return array.$$dimensions();
931896
};
932897

933898
op.setdimensions = function(array, dimensions) {
934-
if (array instanceof Array) {
935-
if (dimensions.length != 1) {
936-
throw new NQPException("A dynamic array can only have a single dimension");
937-
} else {
938-
array.length = dimensions[0];
939-
}
940-
} else {
941-
array.$$setdimensions(dimensions);
942-
}
899+
array.$$setdimensions(dimensions);
943900
return dimensions;
944901
};
945902

946903
// TODO optimize
947904

948905
['_n', '_s', '_i', ''].forEach(function(type) {
949906
op['atposnd' + type] = function(array, idx) {
950-
if (array instanceof Array) {
951-
if (idx.length != 1) {
952-
throw new NQPException("A dynamic array can only be indexed with a single dimension");
953-
}
954-
return array[idx[0]];
955-
} else {
956-
return array['$$atposnd' + type](idx);
957-
}
907+
return array['$$atposnd' + type](idx);
958908
};
959909

960910
op['bindposnd' + type] = function(array, idx, value) {
961-
if (array instanceof Array) {
962-
if (idx.length != 1) {
963-
throw new NQPException("A dynamic array can only be indexed with a single dimension");
964-
}
965-
return (array[idx[0]] = value);
966-
} else {
967-
return array['$$bindposnd' + type](idx, value);
968-
}
911+
return array['$$bindposnd' + type](idx, value);
969912
};
970913

971914
op['atpos2d' + type] = function(array, x, y) {

src/vm/js/nqp-runtime/hll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ op.hllizefor = function(ctx, obj, language) {
5151
var foreign_transform_hash = config.get('foreign_transform_hash');
5252
if (foreign_transform_hash === undefined) return obj;
5353
return foreign_transform_hash.$call(ctx, {}, obj);
54-
} else if (obj instanceof Array || obj instanceof NQPArray || role == 4) {
54+
} else if (obj instanceof NQPArray || role == 4) {
5555
var foreign_transform_array = config.get('foreign_transform_array');
5656
if (foreign_transform_array === undefined) return obj;
5757
return foreign_transform_array.$call(ctx, {}, obj);

src/vm/js/nqp-runtime/runtime.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ exports.to_num = function(arg, ctx) {
140140
} else if (typeof arg == 'string') {
141141
var ret = parseFloat(arg);
142142
return isNaN(ret) ? 0 : ret;
143-
} else if (arg instanceof Array) {
144-
return arg.length;
145143
} else if (arg.type_object_) {
146144
// TODO - is that a correct way to do that?
147145
return 0;
@@ -175,8 +173,6 @@ exports.to_bool = function(arg, ctx) {
175173
return arg ? 1 : 0;
176174
} else if (typeof arg == 'string') {
177175
return arg == '' ? 0 : 1;
178-
} else if (arg instanceof Array) {
179-
return arg.length == 0 ? 0 : 1;
180176
} else if (arg === undefined || arg == null) {
181177
return 0;
182178
} else if (arg.$$to_bool) {

0 commit comments

Comments
 (0)