diff options
author | Nicolas Williams <nico@cryptonector.com> | 2014-08-09 13:10:41 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2014-08-09 13:10:41 -0500 |
commit | 2d1a75f550a14cb18cc85014430d33aa1c617a5b (patch) | |
tree | d5cdebc40fc6e063c2e5d928dc61a4ebf5a03782 | |
parent | 8cddb7c6816e237cc64e2954d0f91cb6564e63ca (diff) |
`.foo[-1] = ...` trips assertion (fix #490)
-rw-r--r-- | jv.c | 6 | ||||
-rw-r--r-- | tests/all.test | 20 |
2 files changed, 25 insertions, 1 deletions
@@ -230,9 +230,13 @@ static jv* jvp_array_read(jv a, int i) { } static jv* jvp_array_write(jv* a, int i) { - assert(i >= 0); jvp_array* array = jvp_array_ptr(*a); + if (i < 0) + i = array->length + i; + if (i < 0) + i = 0; + int pos = i + jvp_array_offset(*a); if (pos < array->alloc_length && jvp_refcnt_unshared(a->u.ptr)) { // use existing array space diff --git a/tests/all.test b/tests/all.test index 7edd14d8..99e95b76 100644 --- a/tests/all.test +++ b/tests/all.test @@ -153,6 +153,26 @@ null [null,"bc",[],[2,3],[2]] # +# Negative array indices +# + +.foo[-1] = 0 +null +{"foo":[0]} + +.foo[-2] = 0 +null +{"foo":[0]} + +.[-1] = 5 +[0,1,2] +[0,1,5] + +.[-2] = 5 +[0,1,2] +[0,5,2] + +# # Multiple outputs, iteration # |