Skip to content

Commit 71fe204

Browse files
committed
Make array access happen through 12275bindpos/12275atpos instead of using [] directly
1 parent fb42527 commit 71fe204

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/vm/js/QAST/Compiler.nqp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,13 @@ class QAST::OperationsJS {
660660
add_simple_op('push' ~ $suffix, $type, [$T_OBJ, $type], sub ($array, $elem) {"$array.push($elem)"}, :sideffects);
661661
}
662662

663+
for ['_i', $T_INT, '_s', $T_STR] -> $suffix, $type {
664+
add_simple_op('atpos' ~ $suffix, $type, [$T_OBJ, $T_INT], sub ($array, $index) {"$array[$index]"});
665+
}
666+
667+
668+
add_op('atpos', sub ($comp, $node, :$want) { $comp.atpos($node[0], $node[1], :$node) });
669+
663670
add_simple_op('shift', $T_OBJ, [$T_OBJ], sub ($array) {"$array.shift()"}, :sideffects);
664671
add_simple_op('unshift', $T_OBJ, [$T_OBJ, $T_OBJ], sub ($array, $elem) {"$array.unshift($elem)"}, :sideffects);
665672
add_simple_op('splice', $T_OBJ, [$T_OBJ, $T_OBJ, $T_INT, $T_INT], :sideffects);
@@ -2093,6 +2100,11 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
20932100
return 0;
20942101
}
20952102

2103+
method atpos($array, $index, :$node) {
2104+
my $array_chunk := self.as_js($array, :want($T_OBJ));
2105+
my $index_chunk := self.as_js($index, :want($T_INT));
2106+
Chunk.new($T_OBJ, "nqp.op.atpos({$array_chunk.expr},{$index_chunk.expr})", [$array_chunk, $index_chunk], :node($node));
2107+
}
20962108

20972109
method compile_var(QAST::Var $var) {
20982110
if self.var_is_lexicalish($var) && self.is_dynamic_var($var) {
@@ -2132,9 +2144,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
21322144
self.bind_pos($var[0], $var[1], $bindval, :node($var));
21332145
}
21342146
} else {
2135-
my $array := self.as_js($var[0], :want($T_OBJ));
2136-
my $index := self.as_js($var[1], :want($T_INT));
2137-
Chunk.new($T_OBJ, "{$array.expr}[{$index.expr}]", [$array, $index], :node($var));
2147+
self.atpos($var[0], $var[1], :node($var));
21382148
}
21392149
} elsif ($var.scope eq 'associative') {
21402150
# TODO work on things other than nqp lists
@@ -2187,7 +2197,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
21872197
my $index_chunk := self.as_js($index, :want($T_STR));
21882198
my $value_chunk := self.as_js($value, :want($T_OBJ));
21892199

2190-
Chunk.new($T_OBJ, $value_chunk.expr, [$array_chunk, $index_chunk, $value_chunk, "({$array_chunk.expr}[{$index_chunk.expr}] = {$value_chunk.expr});\n"], :node($node));
2200+
Chunk.new($T_OBJ, $value_chunk.expr, [$array_chunk, $index_chunk, $value_chunk, "nqp.op.bindpos({$array_chunk.expr},{$index_chunk.expr},{$value_chunk.expr});\n"], :node($node));
21912201
}
21922202

21932203

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ var reprs = require('./reprs.js');
88

99
exports.CodeRef = CodeRef;
1010

11+
op.atpos = function(array, index) {
12+
return (array instanceof Array ? array[index] : array.$$atpos(index));
13+
};
14+
15+
op.bindpos = function(array, index, value) {
16+
if (array instanceof Array) {
17+
return (array[index] = value);
18+
} else {
19+
return array.$$bindpos(index, value);
20+
}
21+
};
22+
1123
op.getcomp = function(lang) {
1224
if (lang == 'JavaScript') {
1325
return {

0 commit comments

Comments
 (0)