]> git.kaiwu.me - njs.git/commitdiff
Parser: simplifed working with function variables.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 13 Jun 2025 00:33:35 +0000 (17:33 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 17 Jun 2025 00:36:31 +0000 (17:36 -0700)
src/njs_builtin.c
src/njs_function.c
src/njs_parser.c
src/njs_value.c
src/njs_variable.c
src/njs_variable.h

index 230b4c1c1da4715d24d22257f0442d5155a4baf5..c960fe1ff9a3f759067e93a914e3ceefdbf24559 100644 (file)
@@ -759,7 +759,6 @@ njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *prop,
 {
     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 @@ njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *prop,
 
     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);
     }
index 90a542042e13abe05ce0b0fa4b9a1e3b2bb6fc2f..7db342f6b4a0035df138cabf10baa59acf38572c 100644 (file)
@@ -934,9 +934,8 @@ njs_function_prototype_create(njs_vm_t *vm, njs_object_prop_t *prop,
     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 @@ njs_function_prototype_create(njs_vm_t *vm, njs_object_prop_t *prop,
         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)) {
index 3cd56fdf77679008f8ef5ff521474f576ed87b80..de11b8c93077a2d210596b355216500a49079129 100644 (file)
@@ -7091,8 +7091,7 @@ njs_parser_function_declaration(njs_parser_t *parser, njs_lexer_token_t *token,
 
     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;
     }
index 0c616a376b14b0718485a0564761403a8c54aa62..fe64afe6ee693d428f7d344c34eaf62f9a4fbb21 100644 (file)
@@ -562,7 +562,6 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *value,
     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 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *value,
 
     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 @@ njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *value,
         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:
index bdf0d95910d8fd570741630cdc98cabb4f7699fd..b65e5934980a2b20740c4a0125b362708cf76a81 100644 (file)
@@ -36,7 +36,7 @@ njs_variable_add(njs_parser_t *parser, njs_parser_scope_t *scope,
 
 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_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope,
     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 @@ njs_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope,
     }
 
     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 @@ njs_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope,
     root->items++;
 
     var->type = NJS_VARIABLE_FUNCTION;
-    var->function = 1;
 
     return var;
 }
@@ -174,7 +174,6 @@ njs_variable_scope_find(njs_parser_t *parser, njs_parser_scope_t *scope,
 
         if (var != NULL && var->scope == root) {
             if (var->self) {
-                var->function = 0;
                 return scope;
             }
 
index 2ed5220a2e522c0943e7b43950e3bf63535da580..db40de752d0a00d4af52e5e2d185811324b4f6c8 100644 (file)
@@ -26,7 +26,6 @@ typedef struct {
     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 @@ typedef struct {
 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,