[njs] Introduced njs_parser_init().

Dmitry Volyntsev xeioex at nginx.com
Mon Feb 14 14:28:33 UTC 2022


details:   https://hg.nginx.org/njs/rev/26fd49ea3f72
branches:  
changeset: 1826:26fd49ea3f72
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Feb 14 14:10:59 2022 +0000
description:
Introduced njs_parser_init().

diffstat:

 src/njs_function.c |   9 ++-------
 src/njs_parser.c   |  27 +++++++++++++++++++++++++++
 src/njs_parser.h   |   4 ++++
 src/njs_vm.c       |  14 ++++----------
 4 files changed, 37 insertions(+), 17 deletions(-)

diffs (131 lines):

diff -r 4e045c33a32e -r 26fd49ea3f72 src/njs_function.c
--- a/src/njs_function.c	Mon Feb 14 14:10:47 2022 +0000
+++ b/src/njs_function.c	Mon Feb 14 14:10:59 2022 +0000
@@ -1103,7 +1103,6 @@ njs_function_constructor(njs_vm_t *vm, n
     njs_int_t               ret;
     njs_str_t               str, file;
     njs_uint_t              i;
-    njs_lexer_t             lexer;
     njs_parser_t            parser;
     njs_vm_code_t           *code;
     njs_function_t          *function;
@@ -1171,16 +1170,12 @@ njs_function_constructor(njs_vm_t *vm, n
 
     file = njs_str_value("runtime");
 
-    ret = njs_lexer_init(vm, &lexer, &file, str.start, str.start + str.length,
-                         1);
+    ret = njs_parser_init(vm, &parser, NULL, &file, str.start,
+                          str.start + str.length, 1);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
 
-    njs_memzero(&parser, sizeof(njs_parser_t));
-
-    parser.lexer = &lexer;
-
     ret = njs_parser(vm, &parser);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
diff -r 4e045c33a32e -r 26fd49ea3f72 src/njs_parser.c
--- a/src/njs_parser.c	Mon Feb 14 14:10:47 2022 +0000
+++ b/src/njs_parser.c	Mon Feb 14 14:10:59 2022 +0000
@@ -519,6 +519,33 @@ njs_parser_reject(njs_parser_t *parser)
 
 
 njs_int_t
+njs_parser_init(njs_vm_t *vm, njs_parser_t *parser, njs_parser_scope_t *scope,
+    njs_str_t *file, u_char *start, u_char *end, njs_uint_t runtime)
+{
+    njs_lexer_t  *lexer;
+
+    njs_memzero(parser, sizeof(njs_parser_t));
+
+    parser->scope = scope;
+
+    lexer = &parser->lexer0;
+    parser->lexer = lexer;
+
+    lexer->file = *file;
+    lexer->start = start;
+    lexer->end = end;
+    lexer->line = 1;
+    lexer->keywords_hash = (runtime) ? &vm->keywords_hash
+                                     : &vm->shared->keywords_hash;
+    lexer->mem_pool = vm->mem_pool;
+
+    njs_queue_init(&lexer->preread);
+
+    return NJS_OK;
+}
+
+
+njs_int_t
 njs_parser(njs_vm_t *vm, njs_parser_t *parser)
 {
     njs_int_t                        ret;
diff -r 4e045c33a32e -r 26fd49ea3f72 src/njs_parser.h
--- a/src/njs_parser.h	Mon Feb 14 14:10:47 2022 +0000
+++ b/src/njs_parser.h	Mon Feb 14 14:10:59 2022 +0000
@@ -74,6 +74,7 @@ typedef njs_int_t (*njs_parser_state_fun
 struct njs_parser_s {
     njs_parser_state_func_t         state;
     njs_queue_t                     stack;
+    njs_lexer_t                     lexer0;
     njs_lexer_t                     *lexer;
     njs_vm_t                        *vm;
     njs_parser_node_t               *node;
@@ -119,6 +120,9 @@ njs_int_t njs_parser_failed_state(njs_pa
 
 intptr_t njs_parser_scope_rbtree_compare(njs_rbtree_node_t *node1,
     njs_rbtree_node_t *node2);
+njs_int_t njs_parser_init(njs_vm_t *vm, njs_parser_t *parser,
+    njs_parser_scope_t *scope, njs_str_t *file, u_char *start, u_char *end,
+    njs_uint_t runtime);
 njs_int_t njs_parser(njs_vm_t *vm, njs_parser_t *parser);
 
 njs_int_t njs_parser_module_lambda(njs_parser_t *parser,
diff -r 4e045c33a32e -r 26fd49ea3f72 src/njs_vm.c
--- a/src/njs_vm.c	Mon Feb 14 14:10:47 2022 +0000
+++ b/src/njs_vm.c	Mon Feb 14 14:10:59 2022 +0000
@@ -143,27 +143,21 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
     njs_str_t           ast;
     njs_chb_t           chain;
     njs_value_t         **global, **new;
-    njs_lexer_t         lexer;
     njs_parser_t        parser;
     njs_vm_code_t       *code;
     njs_generator_t     generator;
     njs_parser_scope_t  *scope;
 
-    njs_memzero(&parser, sizeof(njs_parser_t));
-
-    parser.scope = vm->global_scope;
-
-    if (parser.scope != NULL && vm->modules != NULL) {
+    if (vm->modules != NULL) {
         njs_module_reset(vm);
     }
 
-    ret = njs_lexer_init(vm, &lexer, &vm->options.file, *start, end, 0);
+    ret = njs_parser_init(vm, &parser, vm->global_scope, &vm->options.file,
+                          *start, end, 0);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
 
-    parser.lexer = &lexer;
-
     ret = njs_parser(vm, &parser);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
@@ -186,7 +180,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
         njs_mp_free(vm->mem_pool, ast.start);
     }
 
-    *start = lexer.start;
+    *start = parser.lexer->start;
     scope = parser.scope;
 
     ret = njs_generator_init(&generator, 0, 0);



More information about the nginx-devel mailing list