Created
December 29, 2018 14:57
-
-
Save xeioex/f97ba8a6ffa60861d90bef6ec032ee61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # HG changeset patch | |
| # User hongzhidao <[email protected]> | |
| # Date 1546070637 -28800 | |
| # Sat Dec 29 16:03:57 2018 +0800 | |
| # Node ID 4538d14396e65616cb37733d617fbb57305763ab | |
| # Parent 157bbb5d50ebc222e5d95d1be47fb05757da5ad9 | |
| Improved working with scope indexes of variables. | |
| diff --git a/njs/njs_parser.h b/njs/njs_parser.h | |
| --- 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 | |
| --- a/njs/njs_variable.c | |
| +++ b/njs/njs_variable.c | |
| @@ -9,7 +9,7 @@ | |
| #include <string.h> | |
| -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 | |
| 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_parse | |
| 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_pars | |
| 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 | |
| --- 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; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment