[njs] Generator: introduced njs_generator_init().

Dmitry Volyntsev xeioex at nginx.com
Wed Dec 8 13:16:53 UTC 2021


details:   https://hg.nginx.org/njs/rev/58786d97f25f
branches:  
changeset: 1762:58786d97f25f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Dec 08 13:14:58 2021 +0000
description:
Generator: introduced njs_generator_init().

diffstat:

 src/njs_function.c  |   7 +++++--
 src/njs_generator.c |  24 +++++++++++++++++++++---
 src/njs_generator.h |   2 ++
 src/njs_vm.c        |   6 +++++-
 4 files changed, 33 insertions(+), 6 deletions(-)

diffs (93 lines):

diff -r f32bf14b74e1 -r 58786d97f25f src/njs_function.c
--- a/src/njs_function.c	Wed Dec 08 13:14:56 2021 +0000
+++ b/src/njs_function.c	Wed Dec 08 13:14:58 2021 +0000
@@ -1220,8 +1220,11 @@ njs_function_constructor(njs_vm_t *vm, n
         return ret;
     }
 
-    njs_memzero(&generator, sizeof(njs_generator_t));
-    generator.runtime = 1;
+    ret = njs_generator_init(&generator, 0, 1);
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_internal_error(vm, "njs_generator_init() failed");
+        return NJS_ERROR;
+    }
 
     code = njs_generate_scope(vm, &generator, scope, &njs_entry_anonymous);
     if (njs_slow_path(code == NULL)) {
diff -r f32bf14b74e1 -r 58786d97f25f src/njs_generator.c
--- a/src/njs_generator.c	Wed Dec 08 13:14:56 2021 +0000
+++ b/src/njs_generator.c	Wed Dec 08 13:14:58 2021 +0000
@@ -437,6 +437,21 @@ static const njs_str_t  return_label = n
 static const njs_str_t  undef_label  = { 0xffffffff, (u_char *) "" };
 
 
+njs_int_t
+njs_generator_init(njs_generator_t *generator, njs_int_t depth,
+    njs_bool_t runtime)
+{
+    njs_memzero(generator, sizeof(njs_generator_t));
+
+    njs_queue_init(&generator->stack);
+
+    generator->depth = depth;
+    generator->runtime = runtime;
+
+    return NJS_OK;
+}
+
+
 njs_inline void
 njs_generator_next(njs_generator_t *generator, njs_generator_state_func_t state,
     njs_parser_node_t *node)
@@ -3617,6 +3632,7 @@ njs_generate_function_scope(njs_vm_t *vm
     njs_function_lambda_t *lambda, njs_parser_node_t *node,
     const njs_str_t *name)
 {
+    njs_int_t          ret;
     njs_arr_t          *arr;
     njs_bool_t         module;
     njs_uint_t         depth;
@@ -3631,9 +3647,11 @@ njs_generate_function_scope(njs_vm_t *vm
         return NJS_ERROR;
     }
 
-    njs_memzero(&generator, sizeof(njs_generator_t));
-    generator.depth = depth;
-    generator.runtime = prev->runtime;
+    ret = njs_generator_init(&generator, depth, prev->runtime);
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_internal_error(vm, "njs_generator_init() failed");
+        return NJS_ERROR;
+    }
 
     node = node->right;
 
diff -r f32bf14b74e1 -r 58786d97f25f src/njs_generator.h
--- a/src/njs_generator.h	Wed Dec 08 13:14:56 2021 +0000
+++ b/src/njs_generator.h	Wed Dec 08 13:14:58 2021 +0000
@@ -40,6 +40,8 @@ struct njs_generator_s {
 };
 
 
+njs_int_t njs_generator_init(njs_generator_t *generator, njs_int_t depth,
+    njs_bool_t runtime);
 njs_vm_code_t *njs_generate_scope(njs_vm_t *vm, njs_generator_t *generator,
     njs_parser_scope_t *scope, const njs_str_t *name);
 uint32_t njs_lookup_line(njs_vm_code_t *code, uint32_t offset);
diff -r f32bf14b74e1 -r 58786d97f25f src/njs_vm.c
--- a/src/njs_vm.c	Wed Dec 08 13:14:56 2021 +0000
+++ b/src/njs_vm.c	Wed Dec 08 13:14:58 2021 +0000
@@ -170,7 +170,11 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
     *start = lexer.start;
     scope = parser.scope;
 
-    njs_memzero(&generator, sizeof(njs_generator_t));
+    ret = njs_generator_init(&generator, 0, 0);
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_internal_error(vm, "njs_generator_init() failed");
+        return NJS_ERROR;
+    }
 
     code = njs_generate_scope(vm, &generator, scope, &njs_entry_main);
     if (njs_slow_path(code == NULL)) {


More information about the nginx-devel mailing list