From: hongzhidao Date: Wed, 21 Aug 2019 03:03:26 +0000 (-0400) Subject: Introduced njs_variables_copy(). X-Git-Tag: 0.3.6~53 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=cde1cecaf4f1270664012ca618f953af698b0548;p=njs.git Introduced njs_variables_copy(). --- diff --git a/src/njs_parser.c b/src/njs_parser.c index d3a403ec..ff8e20d0 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -76,13 +76,9 @@ static njs_token_t njs_parser_grouping_expression(njs_vm_t *vm, njs_int_t njs_parser(njs_vm_t *vm, njs_parser_t *parser, njs_parser_t *prev) { - njs_int_t ret; - njs_token_t token; - njs_lvlhsh_t *variables, *prev_variables; - njs_variable_t *var; - njs_parser_node_t *node; - njs_lvlhsh_each_t lhe; - njs_lvlhsh_query_t lhq; + njs_int_t ret; + njs_token_t token; + njs_parser_node_t *node; ret = njs_parser_scope_begin(vm, parser, NJS_SCOPE_GLOBAL); if (njs_slow_path(ret != NJS_OK)) { @@ -94,30 +90,10 @@ njs_parser(njs_vm_t *vm, njs_parser_t *parser, njs_parser_t *prev) * Copy the global scope variables from the previous * iteration of the accumulative mode. */ - njs_lvlhsh_each_init(&lhe, &njs_variables_hash_proto); - - lhq.proto = &njs_variables_hash_proto; - lhq.replace = 0; - lhq.pool = vm->mem_pool; - - variables = &parser->scope->variables; - prev_variables = &prev->scope->variables; - - for ( ;; ) { - var = njs_lvlhsh_each(prev_variables, &lhe); - - if (var == NULL) { - break; - } - - lhq.value = var; - lhq.key = var->name; - lhq.key_hash = njs_djb_hash(var->name.start, var->name.length); - - ret = njs_lvlhsh_insert(variables, &lhq); - if (njs_slow_path(ret != NJS_OK)) { - return NJS_ERROR; - } + ret = njs_variables_copy(vm, &parser->scope->variables, + &prev->scope->variables); + if (njs_slow_path(ret != NJS_OK)) { + return ret; } } diff --git a/src/njs_variable.c b/src/njs_variable.c index 392a6672..d89f3e79 100644 --- a/src/njs_variable.c +++ b/src/njs_variable.c @@ -80,6 +80,42 @@ njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope, njs_str_t *name, } +njs_int_t +njs_variables_copy(njs_vm_t *vm, njs_lvlhsh_t *variables, + njs_lvlhsh_t *prev_variables) +{ + njs_int_t ret; + njs_variable_t *var; + njs_lvlhsh_each_t lhe; + njs_lvlhsh_query_t lhq; + + njs_lvlhsh_each_init(&lhe, &njs_variables_hash_proto); + + lhq.proto = &njs_variables_hash_proto; + lhq.replace = 0; + lhq.pool = vm->mem_pool; + + for ( ;; ) { + var = njs_lvlhsh_each(prev_variables, &lhe); + + if (var == NULL) { + break; + } + + lhq.value = var; + lhq.key = var->name; + lhq.key_hash = njs_djb_hash(var->name.start, var->name.length); + + ret = njs_lvlhsh_insert(variables, &lhq); + if (njs_slow_path(ret != NJS_OK)) { + return NJS_ERROR; + } + } + + return NJS_OK; +} + + static njs_variable_t * njs_variable_scope_add(njs_vm_t *vm, njs_parser_scope_t *scope, njs_lvlhsh_query_t *lhq, njs_variable_type_t type) diff --git a/src/njs_variable.h b/src/njs_variable.h index ec405f3e..5d6c110b 100644 --- a/src/njs_variable.h +++ b/src/njs_variable.h @@ -51,6 +51,8 @@ typedef struct { njs_variable_t *njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope, njs_str_t *name, uint32_t hash, njs_variable_type_t type); +njs_int_t njs_variables_copy(njs_vm_t *vm, njs_lvlhsh_t *variables, + njs_lvlhsh_t *prev_variables); njs_variable_t * njs_label_add(njs_vm_t *vm, njs_parser_scope_t *scope, njs_str_t *name, uint32_t hash); njs_variable_t *njs_label_find(njs_vm_t *vm, njs_parser_scope_t *scope,