[njs] Fixed temporary variables handling in accumulative mode.

Dmitry Volyntsev xeioex at nginx.com
Tue Jul 18 16:28:05 UTC 2017


details:   http://hg.nginx.org/njs/rev/c347ea0bbdd6
branches:  
changeset: 390:c347ea0bbdd6
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jul 18 19:25:50 2017 +0300
description:
Fixed temporary variables handling in accumulative mode.

diffstat:

 njs/njs_generator.c             |  33 ++++++++++++++++++++++++++-------
 njs/test/njs_interactive_test.c |  21 +++++++++++++++++++++
 njs/test/njs_unit_test.c        |   6 ++++++
 3 files changed, 53 insertions(+), 7 deletions(-)

diffs (94 lines):

diff -r ce44f3320fdf -r c347ea0bbdd6 njs/njs_generator.c
--- a/njs/njs_generator.c	Tue Jul 18 14:22:16 2017 +0300
+++ b/njs/njs_generator.c	Tue Jul 18 19:25:50 2017 +0300
@@ -2540,17 +2540,36 @@ njs_generator_temp_index_get(njs_vm_t *v
          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 -r ce44f3320fdf -r c347ea0bbdd6 njs/test/njs_interactive_test.c
--- a/njs/test/njs_interactive_test.c	Tue Jul 18 14:22:16 2017 +0300
+++ b/njs/test/njs_interactive_test.c	Tue Jul 18 19:25:50 2017 +0300
@@ -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 -r ce44f3320fdf -r c347ea0bbdd6 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Jul 18 14:22:16 2017 +0300
+++ b/njs/test/njs_unit_test.c	Tue Jul 18 19:25:50 2017 +0300
@@ -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"),


More information about the nginx-devel mailing list