Skip to content

Commit

Permalink
[js] Implement nqp::multidimref_*
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Oct 25, 2017
1 parent b81bed8 commit 0418b0b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vm/js/Operations.nqp
Expand Up @@ -351,6 +351,7 @@ class QAST::OperationsJS {
for ['_i', '_n', '_s'] -> $suffix {
add_simple_op('getattrref' ~ $suffix, $T_OBJ, [$T_OBJ, $T_OBJ, $T_STR], :takes_hll);
add_simple_op('atposref' ~ $suffix, $T_OBJ, [$T_OBJ, $T_INT], :takes_hll);
add_simple_op('multidimref' ~ $suffix, $T_OBJ, [$T_OBJ, $T_OBJ], :takes_hll);
}

add_simple_op('attrinited', $T_INT, [$T_OBJ, $T_OBJ, $T_STR], :decont(1), :method_call);
Expand Down
39 changes: 39 additions & 0 deletions src/vm/js/nqp-runtime/refs.js
@@ -1,6 +1,8 @@
const helpers = exports.helpers = {};
const op = exports.op = {};

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

function attrRef_i(currentHLL, get, set) {
const refType = currentHLL.get('int_attr_ref');
if (refType === undefined) {
Expand Down Expand Up @@ -55,6 +57,43 @@ op.getattrref_s = function(currentHLL, obj, classHandle, attrName) {
value => obj.$$bindattr_s(classHandle, attrName, value));
};

// TODO do the index calculation once
op.multidimref_i = function(currentHLL, array, indexes) {
const refType = currentHLL.get('int_multidim_ref');
if (refType === undefined) {
throw new NQPException('No int multidim positional reference type registered for current HLL');
}
const STable = refType._STable;
const ref = STable.REPR.allocate(STable);
ref.get = () => array.$$atposnd_i(indexes);
ref.set = value => array.$$bindposnd_i(indexes, value);
return ref;
};

op.multidimref_n = function(currentHLL, array, indexes) {
const refType = currentHLL.get('num_multidim_ref');
if (refType === undefined) {
throw new NQPException('No num multidim positional reference type registered for current HLL');
}
const STable = refType._STable;
const ref = STable.REPR.allocate(STable);
ref.get = () => array.$$atposnd_n(indexes);
ref.set = value => array.$$bindposnd_n(indexes, value);
return ref;
};

op.multidimref_s = function(currentHLL, array, indexes) {
const refType = currentHLL.get('str_multidim_ref');
if (refType === undefined) {
throw new NQPException('No str multidim positional reference type registered for current HLL');
}
const STable = refType._STable;
const ref = STable.REPR.allocate(STable);
ref.get = () => array.$$atposnd_s(indexes);
ref.set = value => array.$$bindposnd_s(indexes, value);
return ref;
};

helpers.lexRef_i = function(currentHLL, get, set) {
const refType = currentHLL.get('int_lex_ref');
if (refType === undefined) {
Expand Down

0 comments on commit 0418b0b

Please sign in to comment.