Skip to content

Commit 51d350b

Browse files
committed
[js] Start working on multidimensional ops.
Make them work on 1 dimensional arrays.
1 parent abd74b2 commit 51d350b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/vm/js/QAST/Compiler.nqp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,15 @@ class QAST::OperationsJS {
785785

786786
add_simple_op('pop' ~ $suffix, $type, [$T_OBJ], sub ($array) {"$array.pop()"}, :sideffects);
787787
add_simple_op('push' ~ $suffix, $type, [$T_OBJ, $type], sub ($array, $elem) {"$array.push($elem)"}, :sideffects);
788+
789+
add_simple_op('atposnd' ~ $suffix, $type, [$T_OBJ, $T_OBJ], :ctx);
790+
add_simple_op('bindposnd' ~ $suffix, $type, [$T_OBJ, $T_OBJ, $type], :ctx, :sideffects);
788791
}
789792

793+
add_simple_op('numdimensions', $T_INT, [$T_OBJ]);
794+
add_simple_op('dimensions', $T_OBJ, [$T_OBJ]);
795+
add_simple_op('setdimensions', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects, :ctx);
796+
790797
add_op('hash', sub ($comp, $node, :$want, :$cps) {
791798
# TODO CPS
792799
my $hash := $*BLOCK.add_tmp();

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,3 +870,52 @@ op.getcodelocation = function(code) {
870870
op.getcodecuid = function(codeRef) {
871871
return codeRef.cuid;
872872
};
873+
874+
op.numdimensions = function(array) {
875+
if (array instanceof Array) {
876+
return 1;
877+
}
878+
};
879+
880+
op.dimensions = function(array) {
881+
if (array instanceof Array) {
882+
return [array.length];
883+
}
884+
};
885+
886+
op.setdimensions = function(ctx, array, dimensions) {
887+
if (array instanceof Array) {
888+
if (dimensions.length != 1) {
889+
ctx.die("A dynamic array can only have a single dimension");
890+
} else {
891+
//console.log("setting ", array, dimensions);
892+
array.length = dimensions[0];
893+
}
894+
}
895+
};
896+
897+
op.atposnd = function(ctx, array, idx) {
898+
if (array instanceof Array) {
899+
if (idx.length != 1) {
900+
ctx.die("A dynamic array can only be indexed with a single dimension");
901+
}
902+
return array[idx[0]];
903+
}
904+
};
905+
906+
op.atposnd_n = op.atposnd;
907+
op.atposnd_s = op.atposnd;
908+
op.atposnd_i = op.atposnd;
909+
910+
op.bindposnd = function(ctx, array, idx, value) {
911+
if (array instanceof Array) {
912+
if (idx.length != 1) {
913+
ctx.die("A dynamic array can only be indexed with a single dimension");
914+
}
915+
return (array[idx[0]] = value);
916+
}
917+
};
918+
919+
op.bindposnd_n = op.bindposnd;
920+
op.bindposnd_s = op.bindposnd;
921+
op.bindposnd_i = op.bindposnd;

0 commit comments

Comments
 (0)