From: Dmitry Volyntsev Date: Thu, 3 Sep 2026 00:39:41 +0000 (-0700) Subject: Fix large array literal initialization X-Git-Tag: 1.0.2~70 X-Git-Url: https://git.kaiwu.me/$%7BGITURL%7D/doc/static/gitweb.js?a=commitdiff_plain;h=3557c57ddef00b286f2147a21bc02c92135e7a02;p=njs.git Fix large array literal initialization Large literals switch to slow arrays above the fast-array capacity limit, but the previous initialization path left their indexed values unset. Initialize slow arrays directly so literals above 32760 elements preserve their contents. --- diff --git a/src/njs_vmcode.c b/src/njs_vmcode.c index c4bbeff6..6d51ab67 100644 --- a/src/njs_vmcode.c +++ b/src/njs_vmcode.c @@ -2075,6 +2075,17 @@ njs_vmcode_property_init(njs_vm_t *vm, njs_value_t *value, njs_value_t *key, index = (uint32_t) num; array = value->data.u.array; + if (njs_slow_path(!array->object.fast_array)) { + prop = njs_object_property_add(vm, value, njs_number_atom(index), + 0); + if (njs_slow_path(prop == NULL)) { + return NJS_ERROR; + } + + njs_value_assign(njs_prop_value(prop), init); + break; + } + if (index >= array->length) { size = index - array->length; diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 3c01172d..c30604f4 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -5001,6 +5001,11 @@ static njs_unit_test_t njs_test[] = { njs_str("[1,2].length"), njs_str("2") }, + { njs_str("var src = 'return [' + '1,'.repeat(32760) + '1]';" + "var a = Function(src)();" + "[a.length, a[0], a[32760], Object.keys(a).length]"), + njs_str("32761,1,1,32761") }, + { njs_str("var a = [1,2]; a.length"), njs_str("2") },