From 739bc8dc12d3337074d669caeab364b71571bc8b Mon Sep 17 00:00:00 2001 From: hongzhidao Date: Sat, 16 Feb 2019 23:18:43 +0800 Subject: [PATCH] Introduced njs_parser_global_scope(). --- njs/njs_parser.c | 6 +----- njs/njs_parser.h | 15 +++++++++++++++ njs/njs_variable.c | 8 +++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/njs/njs_parser.c b/njs/njs_parser.c index caf14ed1..8f6a8ee6 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -2178,11 +2178,7 @@ njs_parser_builtin(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node, njs_variable_t *var; njs_parser_scope_t *scope; - scope = parser->scope; - - while (scope->type != NJS_SCOPE_GLOBAL) { - scope = scope->parent; - } + scope = njs_parser_global_scope(vm); var = njs_variable_add(vm, scope, name, hash, NJS_VARIABLE_VAR); if (nxt_slow_path(var == NULL)) { diff --git a/njs/njs_parser.h b/njs/njs_parser.h index 61079488..4b8f68bd 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -360,6 +360,21 @@ njs_parser_node_new(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token) } +nxt_inline njs_parser_scope_t * +njs_parser_global_scope(njs_vm_t *vm) +{ + njs_parser_scope_t *scope; + + scope = vm->parser->scope; + + while (scope->type != NJS_SCOPE_GLOBAL) { + scope = scope->parent; + } + + return scope; +} + + extern const nxt_lvlhsh_proto_t njs_keyword_hash_proto; diff --git a/njs/njs_variable.c b/njs/njs_variable.c index 8daca9d7..536722aa 100644 --- a/njs/njs_variable.c +++ b/njs/njs_variable.c @@ -356,7 +356,7 @@ njs_variable_reference_resolve(njs_vm_t *vm, njs_variable_reference_t *vr, njs_parser_scope_t *node_scope) { nxt_lvlhsh_query_t lhq; - njs_parser_scope_t *scope, *parent, *previous; + njs_parser_scope_t *scope, *previous; lhq.key_hash = vr->hash; lhq.key = vr->name; @@ -395,9 +395,7 @@ njs_variable_reference_resolve(njs_vm_t *vm, njs_variable_reference_t *vr, return NXT_OK; } - parent = scope->parent; - - if (parent == NULL) { + if (scope->parent == NULL) { /* A global scope. */ vr->scope = scope; @@ -405,7 +403,7 @@ njs_variable_reference_resolve(njs_vm_t *vm, njs_variable_reference_t *vr, } previous = scope; - scope = parent; + scope = scope->parent; } } -- 2.47.3