[njs] For-in loop does not discard the last value of property variable.
Igor Sysoev
igor at sysoev.ru
Tue Jan 3 15:35:30 UTC 2017
details: http://hg.nginx.org/njs/rev/8e1de8ab59e6
branches:
changeset: 295:8e1de8ab59e6
user: Igor Sysoev <igor at sysoev.ru>
date: Mon Jan 02 22:59:31 2017 +0300
description:
For-in loop does not discard the last value of property variable.
diffstat:
njs/njs_generator.c | 2 +-
njs/njs_vm.c | 10 +++++-----
njs/test/njs_unit_test.c | 9 +++++++++
3 files changed, 15 insertions(+), 6 deletions(-)
diffs (82 lines):
diff -r 6c5bebb914ef -r 8e1de8ab59e6 njs/njs_generator.c
--- a/njs/njs_generator.c Mon Jan 02 22:59:29 2017 +0300
+++ b/njs/njs_generator.c Mon Jan 02 22:59:31 2017 +0300
@@ -1047,7 +1047,7 @@ njs_generate_for_in_statement(njs_vm_t *
njs_generate_code(parser, njs_vmcode_prop_next_t, prop_next);
prop_next->code.operation = njs_vmcode_property_next;
prop_next->code.operands = NJS_VMCODE_3OPERANDS;
- prop_next->code.retval = NJS_VMCODE_RETVAL;
+ prop_next->code.retval = NJS_VMCODE_NO_RETVAL;
prop_next->retval = foreach->left->index;
prop_next->object = foreach->right->index;
prop_next->next = index;
diff -r 6c5bebb914ef -r 8e1de8ab59e6 njs/njs_vm.c
--- a/njs/njs_vm.c Mon Jan 02 22:59:29 2017 +0300
+++ b/njs/njs_vm.c Mon Jan 02 22:59:31 2017 +0300
@@ -1227,6 +1227,7 @@ njs_vmcode_property_next(njs_vm_t *vm, n
{
njs_ret_t ret;
nxt_uint_t n;
+ njs_value_t *retval;
njs_array_t *array;
njs_extern_t *ext;
njs_object_prop_t *prop;
@@ -1234,6 +1235,7 @@ njs_vmcode_property_next(njs_vm_t *vm, n
njs_vmcode_prop_next_t *code;
code = (njs_vmcode_prop_next_t *) vm->current;
+ retval = njs_vmcode_operand(vm, code->retval);
if (njs_is_object(object)) {
next = value->data.u.next;
@@ -1245,7 +1247,7 @@ njs_vmcode_property_next(njs_vm_t *vm, n
n = next->index++;
if (njs_is_valid(&array->start[n])) {
- njs_number_set(&vm->retval, n);
+ njs_number_set(retval, n);
return code->offset;
}
@@ -1257,20 +1259,18 @@ njs_vmcode_property_next(njs_vm_t *vm, n
prop = nxt_lvlhsh_each(&object->data.u.object->hash, &next->lhe);
if (prop != NULL) {
- vm->retval = prop->name;
+ *retval = prop->name;
return code->offset;
}
nxt_mem_cache_free(vm->mem_cache_pool, next);
- vm->retval = njs_value_void;
-
} else if (njs_is_external(object)) {
ext = object->data.u.external;
if (ext->next != NULL) {
- ret = ext->next(vm, &vm->retval, vm->external[ext->object], value);
+ ret = ext->next(vm, retval, vm->external[ext->object], value);
if (ret == NXT_OK) {
return code->offset;
diff -r 6c5bebb914ef -r 8e1de8ab59e6 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Jan 02 22:59:29 2017 +0300
+++ b/njs/test/njs_unit_test.c Mon Jan 02 22:59:31 2017 +0300
@@ -4519,6 +4519,15 @@ static njs_unit_test_t njs_test[] =
"for (var i in o) { s += i }; s"),
nxt_string("abc") },
+ { nxt_string("var o = { a: 1, b: 2, c: 3 }; for (var i in o); i"),
+ nxt_string("c") },
+
+ { nxt_string("var o = {}; i = 7; for (var i in o); i"),
+ nxt_string("7") },
+
+ { nxt_string("var a = [1,2,3]; for (var i in a); i"),
+ nxt_string("2") },
+
/* RegExp. */
{ nxt_string("/./x"),
More information about the nginx-devel
mailing list