Skip to content

Commit

Permalink
[js] Make nqp::decode changed the VMArray instead of returning a hack…
Browse files Browse the repository at this point in the history
…ed NQPArray.
  • Loading branch information
pmurias committed Nov 18, 2016
1 parent ec8f19d commit e1e73a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -596,8 +596,6 @@ function renameEncoding(encoding) {
exports.renameEncoding = renameEncoding;

function byteSize(buf) {
if (buf.bytes) return buf.bytes;

var bits = buf._STable.REPR.type._STable.REPR.bits;

if (bits % 8) {
Expand All @@ -607,9 +605,6 @@ function byteSize(buf) {
return bits / 8;
}

// HACK should be using buf instead of creating a new one
// TODO needs to be fixed after an Array handling refactor

op.encode = function(str, encoding_, buf) {
var encoding = renameEncoding(encoding_);

Expand All @@ -621,13 +616,11 @@ op.encode = function(str, encoding_, buf) {

var offset = 0;
for (var i = 0; i < buffer.length / elementSize; i++) {
ret[i] = buffer.readIntLE(offset, elementSize);
buf.array[i] = buffer.readIntLE(offset, elementSize);
offset += elementSize;
}

ret = new NQPArray(ret);
ret.bytes = elementSize;
return ret;
return buf;
};

op.decode = function(buf, encoding_) {
Expand Down
20 changes: 20 additions & 0 deletions src/vm/js/nqp-runtime/reprs.js
Expand Up @@ -744,6 +744,26 @@ class VMArray extends REPR {
return value;
});

/* TODO test how things should be converted */

STable.addInternalMethod('$$atpos_s', function(index) {
var value = this.array[index < 0 ? this.array.length + index : index];
if (value === undefined) return null_s;
return value;
});

STable.addInternalMethod('$$atpos_n', function(index) {
var value = this.array[index < 0 ? this.array.length + index : index];
if (value === undefined) return 0.0;
return value;
});

STable.addInternalMethod('$$atpos_i', function(index) {
var value = this.array[index < 0 ? this.array.length + index : index];
if (value === undefined) return 0;
return value;
});

STable.addInternalMethod('$$bindpos', function(index, value) {
return this.array[index < 0 ? this.array.length + index : index] = value;
});
Expand Down

0 comments on commit e1e73a2

Please sign in to comment.