[njs] Introduced njs_function_lambda_alloc().

Dmitry Volyntsev xeioex at nginx.com
Wed Jul 3 17:39:38 UTC 2019


details:   https://hg.nginx.org/njs/rev/5a7de2ee74ba
branches:  
changeset: 1029:5a7de2ee74ba
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Jul 02 22:10:19 2019 -0400
description:
Introduced njs_function_lambda_alloc().

diffstat:

 njs/njs_function.c |   2 +-
 njs/njs_function.h |  17 ++++++++++++++++-
 njs/njs_parser.c   |  14 +++++---------
 3 files changed, 22 insertions(+), 11 deletions(-)

diffs (102 lines):

diff -r 5c57b9efd77e -r 5a7de2ee74ba njs/njs_function.c
--- a/njs/njs_function.c	Wed Jul 03 18:30:59 2019 +0300
+++ b/njs/njs_function.c	Tue Jul 02 22:10:19 2019 -0400
@@ -37,7 +37,7 @@ njs_function_alloc(njs_vm_t *vm, njs_fun
      *   function->object.__proto__ = NULL;
      */
 
-    function->ctor = !lambda->arrow;
+    function->ctor = lambda->ctor;
     function->args_offset = 1;
     function->u.lambda = lambda;
 
diff -r 5c57b9efd77e -r 5a7de2ee74ba njs/njs_function.h
--- a/njs/njs_function.h	Wed Jul 03 18:30:59 2019 +0300
+++ b/njs/njs_function.h	Tue Jul 02 22:10:19 2019 -0400
@@ -30,7 +30,7 @@ struct njs_function_lambda_s {
     /* Function internal block closures levels. */
     uint8_t                        block_closures;    /* 4 bits */
 
-    uint8_t                        arrow;             /* 1 bit */
+    uint8_t                        ctor;              /* 1 bit */
     uint8_t                        rest_parameters;   /* 1 bit */
 
     /* Initial values of local scope. */
@@ -176,6 +176,21 @@ njs_ret_t njs_function_native_call(njs_v
 void njs_function_frame_free(njs_vm_t *vm, njs_native_frame_t *frame);
 
 
+nxt_inline njs_function_lambda_t *
+njs_function_lambda_alloc(njs_vm_t *vm, uint8_t ctor)
+{
+    njs_function_lambda_t  *lambda;
+
+    lambda = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_function_lambda_t));
+
+    if (nxt_fast_path(lambda != NULL)) {
+        lambda->ctor = ctor;
+    }
+
+    return lambda;
+}
+
+
 nxt_inline njs_ret_t
 njs_function_frame(njs_vm_t *vm, njs_function_t *function,
     const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
diff -r 5c57b9efd77e -r 5a7de2ee74ba njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Jul 03 18:30:59 2019 +0300
+++ b/njs/njs_parser.c	Tue Jul 02 22:10:19 2019 -0400
@@ -602,7 +602,7 @@ njs_parser_function_alloc(njs_vm_t *vm, 
     njs_function_t         *function;
     njs_function_lambda_t  *lambda;
 
-    lambda = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_function_lambda_t));
+    lambda = njs_function_lambda_alloc(vm, 1);
     if (nxt_slow_path(lambda == NULL)) {
         njs_memory_error(vm);
         return NULL;
@@ -687,9 +687,7 @@ njs_parser_function_declaration(njs_vm_t
         return NJS_TOKEN_ERROR;
     }
 
-    token = njs_parser_function_lambda(vm, parser, function->u.lambda, token);
-
-    return token;
+    return njs_parser_function_lambda(vm, parser, function->u.lambda, token);
 }
 
 
@@ -745,7 +743,7 @@ njs_parser_function_expression(njs_vm_t 
 
     } else {
         /* Anonymous function. */
-        lambda = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_function_lambda_t));
+        lambda = njs_function_lambda_alloc(vm, 1);
         if (nxt_slow_path(lambda == NULL)) {
             return NJS_TOKEN_ERROR;
         }
@@ -1896,7 +1894,7 @@ njs_parser_module_lambda(njs_vm_t *vm, n
         return token;
     }
 
-    lambda = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_function_lambda_t));
+    lambda = njs_function_lambda_alloc(vm, 1);
     if (nxt_slow_path(lambda == NULL)) {
         return NJS_TOKEN_ERROR;
     }
@@ -2156,13 +2154,11 @@ njs_parser_arrow_expression(njs_vm_t *vm
     node->token_line = njs_parser_token_line(parser);
     parser->node = node;
 
-    lambda = nxt_mp_zalloc(vm->mem_pool, sizeof(njs_function_lambda_t));
+    lambda = njs_function_lambda_alloc(vm, 0);
     if (nxt_slow_path(lambda == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    lambda->arrow = 1;
-
     node->u.value.data.u.lambda = lambda;
 
     ret = njs_parser_scope_begin(vm, parser, NJS_SCOPE_FUNCTION);


More information about the nginx-devel mailing list