From: Dmitry Volyntsev Date: Tue, 18 Jul 2017 16:25:50 +0000 (+0300) Subject: Fixed temporary variables handling in accumulative mode. X-Git-Tag: 0.1.12~5 X-Git-Url: http://git.kaiwu.me/sitemap.xml?a=commitdiff_plain;h=8d36b47e446dcee2fc71ee2a8aa447a5d7df62af;p=njs.git Fixed temporary variables handling in accumulative mode. --- diff --git a/njs/njs_generator.c b/njs/njs_generator.c index 16ee8baa..b058b7d6 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -2540,17 +2540,36 @@ njs_generator_temp_index_get(njs_vm_t *vm, njs_parser_t *parser, scope = scope->parent; } - value = nxt_array_add(scope->values[0], &njs_array_mem_proto, - vm->mem_cache_pool); - if (nxt_slow_path(value == NULL)) { - return NJS_INDEX_ERROR; + if (vm->accumulative && scope->type == NJS_SCOPE_GLOBAL) { + + /* + * When non-clonable VM runs in accumulative mode + * all global variables are allocated in absolute scope + * to simplify global scope handling. + */ + + value = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t), + sizeof(njs_value_t)); + if (nxt_slow_path(value == NULL)) { + return NJS_INDEX_ERROR; + } + + index = (njs_index_t) value; + + } else { + + value = nxt_array_add(scope->values[0], &njs_array_mem_proto, + vm->mem_cache_pool); + if (nxt_slow_path(value == NULL)) { + return NJS_INDEX_ERROR; + } + + index = scope->next_index[0]; + scope->next_index[0] += sizeof(njs_value_t); } *value = njs_value_invalid; - index = scope->next_index[0]; - scope->next_index[0] += sizeof(njs_value_t); - return index; } diff --git a/njs/test/njs_interactive_test.c b/njs/test/njs_interactive_test.c index b096f35a..d0ca9f98 100644 --- a/njs/test/njs_interactive_test.c +++ b/njs/test/njs_interactive_test.c @@ -63,6 +63,27 @@ static njs_interactive_test_t njs_test[] = "sq(function () { return 3 })" ENTER), nxt_string("9") }, + /* Temporary indexes */ + + { nxt_string("var a = [1,2,3], i; for (i in a) {Object.seal({});}" ENTER), + nxt_string("undefined") }, + + { nxt_string("var i; for (i in [1,2,3]) {Object.seal({});}" ENTER), + nxt_string("undefined") }, + + { nxt_string("var a = 'A'; switch (a) {" + "case 0: a += '0';" + "case 1: a += '1';" + "}; a" ENTER), + nxt_string("A") }, + + { nxt_string("var a = 0; try { a = 5 }" + "catch (e) { a = 9 } finally { a++ } a" ENTER), + nxt_string("6") }, + + { nxt_string("/abc/i.test('ABC')" ENTER), + nxt_string("true") }, + /* Error handling */ { nxt_string("var a = ;" ENTER diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index a62caf4e..e3c83a3e 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -2069,6 +2069,12 @@ static njs_unit_test_t njs_test[] = { nxt_string("var a; for (a = 1; a; a--) switch (a) { case 0: continue }"), nxt_string("undefined") }, + { nxt_string("var a = [1,2,3], i; for (i in a) {Object.seal({})}"), + nxt_string("undefined") }, + + { nxt_string("var i; for (i in [1,2,3]) {Object.seal({});}"), + nxt_string("undefined") }, + /* break. */ { nxt_string("break"),