changeset 2575:9be6834ec6ff

Parser: simplifed working with function variables.
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 12 Jun 2025 17:33:35 -0700
parents eead47ce1ad2
children 6018de6f6c59
files src/njs_builtin.c src/njs_function.c src/njs_parser.c src/njs_value.c src/njs_variable.c src/njs_variable.h
diffstat 6 files changed, 11 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/njs_builtin.c	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_builtin.c	Thu Jun 12 17:33:35 2025 -0700
@@ -759,7 +759,6 @@
 {
     njs_value_t          *value;
     njs_variable_t       *var;
-    njs_function_t       *function;
     njs_rbtree_node_t    *rb_node;
     njs_variable_node_t  *node, var_node;
 
@@ -788,15 +787,6 @@
 
     value = njs_scope_valid_value(vm, var->index);
 
-    if (var->type == NJS_VARIABLE_FUNCTION && njs_is_undefined(value)) {
-        njs_value_assign(value, &var->value);
-
-        function = njs_function_value_copy(vm, value);
-        if (njs_slow_path(function == NULL)) {
-            return NJS_ERROR;
-        }
-    }
-
     if (setval != NULL) {
         njs_value_assign(value, setval);
     }
--- a/src/njs_function.c	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_function.c	Thu Jun 12 17:33:35 2025 -0700
@@ -934,9 +934,8 @@
     uint32_t unused, njs_value_t *value, njs_value_t *setval,
     njs_value_t *retval)
 {
-    njs_value_t     *proto, proto_value, *cons;
-    njs_object_t    *prototype;
-    njs_function_t  *function;
+    njs_value_t   *proto, proto_value, *cons;
+    njs_object_t  *prototype;
 
     if (setval == NULL) {
         prototype = njs_object_alloc(vm);
@@ -949,11 +948,6 @@
         setval = &proto_value;
     }
 
-    function = njs_function_value_copy(vm, value);
-    if (njs_slow_path(function == NULL)) {
-        return NJS_ERROR;
-    }
-
     proto = njs_function_property_prototype_set(vm, njs_object_hash(value),
                                                 setval);
     if (njs_slow_path(proto == NULL)) {
--- a/src/njs_parser.c	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_parser.c	Thu Jun 12 17:33:35 2025 -0700
@@ -7091,8 +7091,7 @@
 
     njs_lexer_consume_token(parser->lexer, 1);
 
-    var = njs_variable_function_add(parser, parser->scope, atom_id,
-                                    NJS_VARIABLE_FUNCTION);
+    var = njs_variable_function_add(parser, parser->scope, atom_id);
     if (var == NULL) {
         return NJS_ERROR;
     }
--- a/src/njs_value.c	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_value.c	Thu Jun 12 17:33:35 2025 -0700
@@ -562,7 +562,6 @@
     uint32_t        index;
     njs_int_t       ret;
     njs_object_t    *obj;
-    njs_function_t  *function;
 
     njs_assert(atom_id != NJS_ATOM_STRING_unknown);
 
@@ -585,6 +584,7 @@
 
     case NJS_OBJECT:
     case NJS_ARRAY:
+    case NJS_FUNCTION:
     case NJS_ARRAY_BUFFER:
     case NJS_DATA_VIEW:
     case NJS_TYPED_ARRAY:
@@ -595,15 +595,6 @@
         obj = njs_object(value);
         break;
 
-    case NJS_FUNCTION:
-        function = njs_function_value_copy(vm, value);
-        if (njs_slow_path(function == NULL)) {
-            return NJS_ERROR;
-        }
-
-        obj = &function->object;
-        break;
-
     case NJS_UNDEFINED:
     case NJS_NULL:
     default:
--- a/src/njs_variable.c	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_variable.c	Thu Jun 12 17:33:35 2025 -0700
@@ -36,7 +36,7 @@
 
 njs_variable_t *
 njs_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope,
-    uintptr_t atom_id, njs_variable_type_t type)
+    uintptr_t atom_id)
 {
     njs_bool_t             ctor;
     njs_variable_t         *var;
@@ -44,14 +44,15 @@
     njs_parser_scope_t     *root;
     njs_function_lambda_t  *lambda;
 
-    root = njs_variable_scope_find(parser, scope, atom_id, type);
+    root = njs_variable_scope_find(parser, scope, atom_id,
+                                   NJS_VARIABLE_FUNCTION);
     if (njs_slow_path(root == NULL)) {
         njs_parser_ref_error(parser, "scope not found");
         return NULL;
     }
 
-    var = njs_variable_scope_add(parser, root, scope, atom_id, type,
-                                 NJS_INDEX_ERROR);
+    var = njs_variable_scope_add(parser, root, scope, atom_id,
+                                 NJS_VARIABLE_FUNCTION, NJS_INDEX_ERROR);
     if (njs_slow_path(var == NULL)) {
         return NULL;
     }
@@ -77,7 +78,7 @@
     }
 
     var->index = njs_scope_index(root->type, root->items, NJS_LEVEL_LOCAL,
-                                 type);
+                                 NJS_VARIABLE_FUNCTION);
 
     declr->lambda = lambda;
     declr->async = !ctor;
@@ -86,7 +87,6 @@
     root->items++;
 
     var->type = NJS_VARIABLE_FUNCTION;
-    var->function = 1;
 
     return var;
 }
@@ -174,7 +174,6 @@
 
         if (var != NULL && var->scope == root) {
             if (var->self) {
-                var->function = 0;
                 return scope;
             }
 
--- a/src/njs_variable.h	Wed Jun 11 16:17:42 2025 -0700
+++ b/src/njs_variable.h	Thu Jun 12 17:33:35 2025 -0700
@@ -26,7 +26,6 @@
     njs_bool_t            self;
     njs_bool_t            init;
     njs_bool_t            closure;
-    njs_bool_t            function;
 
     njs_parser_scope_t    *scope;
     njs_parser_scope_t    *original;
@@ -62,7 +61,7 @@
 njs_variable_t *njs_variable_add(njs_parser_t *parser,
     njs_parser_scope_t *scope, uintptr_t atom_id, njs_variable_type_t type);
 njs_variable_t *njs_variable_function_add(njs_parser_t *parser,
-    njs_parser_scope_t *scope, uintptr_t atom_id, njs_variable_type_t type);
+    njs_parser_scope_t *scope, uintptr_t atom_id);
 njs_variable_t * njs_label_add(njs_vm_t *vm, njs_parser_scope_t *scope,
     uintptr_t atom_id);
 njs_variable_t *njs_label_find(njs_vm_t *vm, njs_parser_scope_t *scope,