From: hongzhidao Date: Sat, 29 Dec 2018 08:03:57 +0000 (+0800) Subject: Improved working with scope indexes of variables. X-Git-Tag: 0.2.8~91 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=9a11a68ea25f695c1960e477a119d66e98b78be1;p=njs.git Improved working with scope indexes of variables. --- diff --git a/njs/njs_parser.h b/njs/njs_parser.h index 8259b4c3..cf903162 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -238,10 +238,9 @@ struct njs_parser_scope_s { nxt_lvlhsh_t variables; nxt_lvlhsh_t references; - /* - * 0: local scope index; - * 1: closure scope index. - */ +#define NJS_SCOPE_INDEX_LOCAL 0 +#define NJS_SCOPE_INDEX_CLOSURE 1 + nxt_array_t *values[2]; /* Array of njs_value_t. */ njs_index_t next_index[2]; diff --git a/njs/njs_variable.c b/njs/njs_variable.c index 34691180..ce60fd3a 100644 --- a/njs/njs_variable.c +++ b/njs/njs_variable.c @@ -9,7 +9,7 @@ #include -static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope, +static njs_ret_t njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *node_scope, njs_variable_reference_t *vr); static njs_variable_t *njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name, njs_variable_type_t type); @@ -196,15 +196,7 @@ njs_variables_scope_resolve(njs_vm_t *vm, njs_parser_scope_t *scope, continue; } - if (vr->scope->type == NJS_SCOPE_GLOBAL) { - continue; - } - - if (node->scope->nesting == vr->scope->nesting) { - /* - * A variable is referenced locally here, but may be - * referenced non-locally in other places, skipping. - */ + if (vr->scope_index == NJS_SCOPE_INDEX_LOCAL) { continue; } } @@ -308,18 +300,16 @@ njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node) goto not_found; } - scope_index = 0; - - if (vr->scope->type > NJS_SCOPE_GLOBAL) { - scope_index = (node->scope->nesting != vr->scope->nesting); - } + scope_index = vr->scope_index; var = vr->variable; index = var->index; if (index != NJS_INDEX_NONE) { - if (scope_index == 0 || njs_scope_type(index) != NJS_SCOPE_ARGUMENTS) { + if (scope_index == NJS_SCOPE_INDEX_LOCAL + || njs_scope_type(index) != NJS_SCOPE_ARGUMENTS) + { node->index = index; return var; @@ -400,16 +390,17 @@ not_found: static njs_ret_t -njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope, +njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *node_scope, njs_variable_reference_t *vr) { nxt_lvlhsh_query_t lhq; - njs_parser_scope_t *parent, *previous; + njs_parser_scope_t *scope, *parent, *previous; lhq.key_hash = vr->hash; lhq.key = vr->name; lhq.proto = &njs_variables_hash_proto; + scope = node_scope; previous = NULL; for ( ;; ) { @@ -431,6 +422,14 @@ njs_variable_find(njs_vm_t *vm, njs_parser_scope_t *scope, vr->scope = scope; + vr->scope_index = NJS_SCOPE_INDEX_LOCAL; + + if (vr->scope->type > NJS_SCOPE_GLOBAL + && node_scope->nesting != vr->scope->nesting) + { + vr->scope_index = NJS_SCOPE_INDEX_CLOSURE; + } + return NXT_OK; } diff --git a/njs/njs_variable.h b/njs/njs_variable.h index 818d6c76..78e137f1 100644 --- a/njs/njs_variable.h +++ b/njs/njs_variable.h @@ -48,6 +48,7 @@ typedef struct { nxt_str_t name; njs_variable_t *variable; njs_parser_scope_t *scope; + nxt_uint_t scope_index; /* NJS_SCOPE_INDEX_... */ } njs_variable_reference_t;