From: Andrey Zelenkov Date: Fri, 17 Mar 2017 15:18:01 +0000 (+0300) Subject: Array.prototype.fill() method. X-Git-Tag: 0.1.10~24 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=a1b3a85133ea8c25c90699dc6565c45765a694f6;p=njs.git Array.prototype.fill() method. --- diff --git a/njs/njs_array.c b/njs/njs_array.c index 05c6f912..2361c4f8 100644 --- a/njs/njs_array.c +++ b/njs/njs_array.c @@ -1291,6 +1291,69 @@ njs_array_prototype_every_continuation(njs_vm_t *vm, njs_value_t *args, } +static njs_ret_t +njs_array_prototype_fill(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + nxt_int_t start, end, length; + njs_array_t *array; + + vm->retval = args[0]; + + if (nargs < 2 || !njs_is_array(&args[0])) { + return NXT_OK; + } + + array = args[0].data.u.array; + length = array->length; + + if (length == 0) { + return NXT_OK; + } + + start = 0; + end = length; + + if (nargs > 2) { + start = args[2].data.u.number; + + if (start > length) { + start = length; + } + + if (start < 0) { + start += length; + + if (start < 0) { + start = 0; + } + } + + if (nargs > 3) { + end = args[3].data.u.number; + + if (end > length) { + end = length; + } + + if (end < 0) { + end += length; + + if (end < 0) { + end = 0; + } + } + } + } + + while (start < end) { + array->start[start++] = args[1]; + } + + return NXT_OK; +} + + static njs_ret_t njs_array_prototype_filter(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) @@ -1911,6 +1974,15 @@ static const njs_object_prop_t njs_array_prototype_properties[] = njs_continuation_size(njs_array_iter_t), 0), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("fill"), + .value = njs_native_function(njs_array_prototype_fill, 0, + NJS_OBJECT_ARG, NJS_SKIP_ARG, NJS_NUMBER_ARG, + NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("filter"), diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 0a2e8ee2..c4d7ed0d 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -2728,6 +2728,60 @@ static njs_unit_test_t njs_test[] = "a.every(function(v, i, a) { return v > 0 })"), nxt_string("true") }, + { nxt_string("[].fill(1);"), + nxt_string("") }, + + { nxt_string("[1,2,3].fill(5);"), + nxt_string("5,5,5") }, + + { nxt_string("[1,2,3].fill(5, 0);"), + nxt_string("5,5,5") }, + + { nxt_string("[1,2,3].fill(5, 1);"), + nxt_string("1,5,5") }, + + { nxt_string("[1,2,3].fill(5, 4);"), + nxt_string("1,2,3") }, + + { nxt_string("[1,2,3].fill(5, -2);"), + nxt_string("1,5,5") }, + + { nxt_string("[1,2,3].fill(5, -3);"), + nxt_string("5,5,5") }, + + { nxt_string("[1,2,3].fill(5, -4);"), + nxt_string("5,5,5") }, + + { nxt_string("[1,2,3].fill(5, 1, 0);"), + nxt_string("1,2,3") }, + + { nxt_string("[1,2,3].fill(5, 1, 1);"), + nxt_string("1,2,3") }, + + { nxt_string("[1,2,3].fill(5, 1, 2);"), + nxt_string("1,5,3") }, + + { nxt_string("[1,2,3].fill(5, 1, 3);"), + nxt_string("1,5,5") }, + + { nxt_string("[1,2,3].fill(5, 1, 4);"), + nxt_string("1,5,5") }, + + { nxt_string("[1,2,3].fill(5, 1, -1);"), + nxt_string("1,5,3") }, + + { nxt_string("[1,2,3].fill(5, 1, -3);"), + nxt_string("1,2,3") }, + + { nxt_string("[1,2,3].fill(5, 1, -4);"), + nxt_string("1,2,3") }, + + { nxt_string("[1,2,3].fill(\"a\", 1, 2);"), + nxt_string("1,a,3") }, + + { nxt_string("[1,2,3].fill({a:\"b\"}, 1, 2);"), + nxt_string("1,[object Object],3") }, + { nxt_string("var a = [];" "a.filter(function(v, i, a) { return v > 1 })"), nxt_string("") },