Skip to content

Commit f49d614

Browse files
committed
[js] Fix a bug in nqp::bindpos, make it work for negative indexes.
1 parent 0b106de commit f49d614

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/vm/js/QAST/Compiler.nqp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
29902990

29912991
method bind_pos($array, $index, $value, :$node) {
29922992
my $array_chunk := self.as_js($array, :want($T_OBJ));
2993-
my $index_chunk := self.as_js($index, :want($T_STR));
2993+
my $index_chunk := self.as_js($index, :want($T_INT));
29942994
my $value_chunk := self.as_js($value, :want($T_OBJ));
29952995

29962996
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));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ op.atpos = function(array, index) {
2323

2424
op.bindpos = function(array, index, value) {
2525
if (array instanceof Array) {
26+
if (index < 0) {
27+
index = array.length + index;
28+
}
2629
return (array[index] = value);
2730
} else {
2831
return array.$$bindpos(index, value);

0 commit comments

Comments
 (0)