@@ -660,6 +660,13 @@ class QAST::OperationsJS {
660
660
add_simple_op(' push' ~ $ suffix , $ type , [$ T_OBJ , $ type ], sub ($ array , $ elem ) {" $ array. push ($ elem )" }, : sideffects);
661
661
}
662
662
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
+
663
670
add_simple_op(' shift' , $ T_OBJ , [$ T_OBJ ], sub ($ array ) {" $ array. shift ()" }, : sideffects);
664
671
add_simple_op(' unshift' , $ T_OBJ , [$ T_OBJ , $ T_OBJ ], sub ($ array , $ elem ) {" $ array. unshift ($ elem )" }, : sideffects);
665
672
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 {
2093
2100
return 0 ;
2094
2101
}
2095
2102
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
+ }
2096
2108
2097
2109
method compile_var (QAST ::Var $ var ) {
2098
2110
if self . var_is_lexicalish($ var ) && self . is_dynamic_var($ var ) {
@@ -2132,9 +2144,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
2132
2144
self . bind_pos($ var [0 ], $ var [1 ], $ bindval , : node($ var ));
2133
2145
}
2134
2146
} 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 ));
2138
2148
}
2139
2149
} elsif ($ var . scope eq ' associative' ) {
2140
2150
# TODO work on things other than nqp lists
@@ -2187,7 +2197,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
2187
2197
my $ index_chunk := self . as_js($ index , : want($ T_STR ));
2188
2198
my $ value_chunk := self . as_js($ value , : want($ T_OBJ ));
2189
2199
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 ));
2191
2201
}
2192
2202
2193
2203
0 commit comments