]> git.kaiwu.me - njs.git/commitdiff
Improved next index allocation.
authorhongzhidao <hongzhidao@gmail.com>
Sat, 26 Jan 2019 21:21:32 +0000 (05:21 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Sat, 26 Jan 2019 21:21:32 +0000 (05:21 +0800)
njs/njs_generator.c
njs/njs_variable.c
njs/njs_variable.h

index 9b392e6b58f538b3c5b2a2bf649da2e8fb3ab6fa..5856ec2b92edaffa8d946d6703720a22f7956f74 100644 (file)
@@ -3053,8 +3053,7 @@ njs_generate_temp_index_get(njs_vm_t *vm, njs_generator_t *generator,
     njs_parser_node_t *node)
 {
     nxt_array_t         *cache;
-    njs_value_t         *value;
-    njs_index_t         index, *last;
+    njs_index_t         *last;
     njs_parser_scope_t  *scope;
 
     cache = generator->index_cache;
@@ -3073,34 +3072,8 @@ njs_generate_temp_index_get(njs_vm_t *vm, njs_generator_t *generator,
          scope = scope->parent;
     }
 
-    if (vm->options.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_mp_align(vm->mem_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_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;
-
-    return index;
+    return njs_scope_next_index(vm, scope, NJS_SCOPE_INDEX_LOCAL,
+                                &njs_value_invalid);
 }
 
 
index a3b44f8d0b12311065a7b7390f98b07fc7777bf1..105d28454ad736eb692b4f602ad02b335c97609c 100644 (file)
@@ -286,10 +286,9 @@ njs_variable_resolve(njs_vm_t *vm, njs_parser_node_t *node)
 {
     nxt_int_t                 ret;
     nxt_uint_t                scope_index;
-    nxt_array_t               *values;
     njs_index_t               index;
-    njs_value_t               *value;
     njs_variable_t            *var;
+    const njs_value_t         *default_value;
     njs_variable_reference_t  *vr;
 
     vr = &node->u.reference;
@@ -331,48 +330,12 @@ njs_variable_resolve(njs_vm_t *vm, njs_parser_node_t *node)
         goto not_found;
     }
 
-    if (vm->options.accumulative && vr->scope->type == NJS_SCOPE_GLOBAL) {
-        /*
-         * When non-clonable VM runs in accumulative mode all
-         * global variables should be allocated in absolute scope
-         * to share them among consecutive VM invocations.
-         */
-        value = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
-                             sizeof(njs_value_t));
-        if (nxt_slow_path(value == NULL)) {
-            njs_memory_error(vm);
-            return NULL;
-        }
-
-        index = (njs_index_t) value;
+    default_value = njs_is_object(&var->value) ? &var->value : &njs_value_void;
 
-    } else {
-        values = vr->scope->values[scope_index];
+    index = njs_scope_next_index(vm, vr->scope, scope_index, default_value);
 
-        if (values == NULL) {
-            values = nxt_array_create(4, sizeof(njs_value_t),
-                                      &njs_array_mem_proto, vm->mem_pool);
-            if (nxt_slow_path(values == NULL)) {
-                return NULL;
-            }
-
-            vr->scope->values[scope_index] = values;
-        }
-
-        value = nxt_array_add(values, &njs_array_mem_proto, vm->mem_pool);
-        if (nxt_slow_path(value == NULL)) {
-            return NULL;
-        }
-
-        index = vr->scope->next_index[scope_index];
-        vr->scope->next_index[scope_index] += sizeof(njs_value_t);
-    }
-
-    if (njs_is_object(&var->value)) {
-        *value = var->value;
-
-    } else {
-        *value = njs_value_void;
+    if (nxt_slow_path(index == NJS_INDEX_ERROR)) {
+        return NULL;
     }
 
     var->index = index;
@@ -448,6 +411,56 @@ njs_variable_reference_resolve(njs_vm_t *vm, njs_variable_reference_t *vr,
 }
 
 
+njs_index_t
+njs_scope_next_index(njs_vm_t *vm, njs_parser_scope_t *scope,
+    nxt_uint_t scope_index, const njs_value_t *default_value)
+{
+    njs_index_t  index;
+    njs_value_t  *value;
+    nxt_array_t  *values;
+
+    if (vm->options.accumulative && scope->type == NJS_SCOPE_GLOBAL) {
+        /*
+         * When non-clonable VM runs in accumulative mode all
+         * global variables should be allocated in absolute scope
+         * to share them among consecutive VM invocations.
+         */
+        value = nxt_mp_align(vm->mem_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 {
+        values = scope->values[scope_index];
+
+        if (values == NULL) {
+            values = nxt_array_create(4, sizeof(njs_value_t),
+                                      &njs_array_mem_proto, vm->mem_pool);
+            if (nxt_slow_path(values == NULL)) {
+                return NJS_INDEX_ERROR;
+            }
+
+            scope->values[scope_index] = values;
+        }
+
+        value = nxt_array_add(values, &njs_array_mem_proto, vm->mem_pool);
+        if (nxt_slow_path(value == NULL)) {
+            return NJS_INDEX_ERROR;
+        }
+
+        index = scope->next_index[scope_index];
+        scope->next_index[scope_index] += sizeof(njs_value_t);
+    }
+
+    *value = *default_value;
+
+    return index;
+}
+
+
 static njs_variable_t *
 njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name, njs_variable_type_t type)
 {
index 78e137f1aa98a2de600ffa653ddfb2f51eed3bf5..a812b9d50cf81817fefd1d2e211250b7c3620156 100644 (file)
@@ -59,6 +59,8 @@ njs_ret_t njs_variable_reference(njs_vm_t *vm, njs_parser_scope_t *scope,
     njs_reference_type_t type);
 njs_ret_t njs_variables_scope_reference(njs_vm_t *vm,
     njs_parser_scope_t *scope);
+njs_index_t njs_scope_next_index(njs_vm_t *vm, njs_parser_scope_t *scope,
+    nxt_uint_t scope_index, const njs_value_t *default_value);
 njs_ret_t njs_name_copy(njs_vm_t *vm, nxt_str_t *dst, nxt_str_t *src);
 
 extern const nxt_lvlhsh_proto_t  njs_variables_hash_proto;