[njs] Using njs_function() and njs_set_function() where applicable.

Dmitry Volyntsev xeioex at nginx.com
Mon Jul 8 14:52:35 UTC 2019


details:   https://hg.nginx.org/njs/rev/4c9047471ed0
branches:  
changeset: 1035:4c9047471ed0
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jul 08 17:51:35 2019 +0300
description:
Using njs_function() and njs_set_function() where applicable.

diffstat:

 njs/njs_array.c           |  12 ++++++------
 njs/njs_builtin.c         |   6 ++----
 njs/njs_error.c           |   2 +-
 njs/njs_fs.c              |   4 ++--
 njs/njs_function.c        |  12 +++++-------
 njs/njs_generator.c       |   2 +-
 njs/njs_json.c            |  12 ++++++------
 njs/njs_object.c          |   2 +-
 njs/njs_object_property.c |   8 +++-----
 njs/njs_parser.c          |   4 +---
 njs/njs_parser_terminal.c |   5 +----
 njs/njs_string.c          |   2 +-
 njs/njs_time.c            |   2 +-
 njs/njs_value.c           |   2 +-
 njs/njs_value.h           |  13 +++++++++++++
 njs/njs_variable.c        |   2 +-
 njs/njs_vm.c              |   8 +++-----
 17 files changed, 49 insertions(+), 49 deletions(-)

diffs (421 lines):

diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_array.c
--- a/njs/njs_array.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_array.c	Mon Jul 08 17:51:35 2019 +0300
@@ -970,7 +970,7 @@ njs_array_prototype_to_string(njs_vm_t *
         prop = njs_object_property(vm, njs_object(&args[0]), &lhq);
 
         if (nxt_fast_path(prop != NULL && njs_is_function(&prop->value))) {
-            return njs_function_replace(vm, prop->value.data.u.function,
+            return njs_function_replace(vm, njs_function(&prop->value),
                                         args, nargs, retval);
         }
     }
@@ -1879,7 +1879,7 @@ njs_array_prototype_find_apply(njs_vm_t 
 
     arguments[3] = args[0];
 
-    return njs_function_apply(vm, args[1].data.u.function, arguments, 4,
+    return njs_function_apply(vm, njs_function(&args[1]), arguments, 4,
                               (njs_index_t) &iter->retval);
 }
 
@@ -2026,7 +2026,7 @@ njs_array_prototype_reduce_continuation(
 
     arguments[4] = args[0];
 
-    return njs_function_apply(vm, args[1].data.u.function, arguments, 5,
+    return njs_function_apply(vm, njs_function(&args[1]), arguments, 5,
                               (njs_index_t) &iter->retval);
 }
 
@@ -2090,7 +2090,7 @@ njs_array_iterator_apply(njs_vm_t *vm, n
 
     arguments[3] = args[0];
 
-    return njs_function_apply(vm, args[1].data.u.function, arguments, 4,
+    return njs_function_apply(vm, njs_function(&args[1]), arguments, 4,
                               (njs_index_t) &iter->retval);
 }
 
@@ -2163,7 +2163,7 @@ njs_array_prototype_reduce_right_continu
 
     arguments[4] = args[0];
 
-    return njs_function_apply(vm, args[1].data.u.function, arguments, 5,
+    return njs_function_apply(vm, njs_function(&args[1]), arguments, 5,
                               (njs_index_t) &iter->retval);
 }
 
@@ -2236,7 +2236,7 @@ njs_array_prototype_sort(njs_vm_t *vm, n
         sort->retval = njs_value_zero;
 
         if (nargs > 1 && njs_is_function(&args[1])) {
-            sort->function = args[1].data.u.function;
+            sort->function = njs_function(&args[1]);
 
         } else {
             sort->function = (njs_function_t *) &njs_array_string_sort_function;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_builtin.c	Mon Jul 08 17:51:35 2019 +0300
@@ -536,9 +536,7 @@ njs_builtin_objects_clone(njs_vm_t *vm)
     values = vm->scopes[NJS_SCOPE_GLOBAL];
 
     for (i = NJS_CONSTRUCTOR_OBJECT; i < NJS_CONSTRUCTOR_MAX; i++) {
-        values[i].type = NJS_FUNCTION;
-        values[i].data.truth = 1;
-        values[i].data.u.function = &vm->constructors[i];
+        njs_set_function(&values[i], &vm->constructors[i]);
         vm->constructors[i].object.__proto__ = function_prototype;
     }
 
@@ -981,7 +979,7 @@ njs_builtin_match(const njs_object_init_
                 continue;
             }
 
-            if (function != pr->value.data.u.function) {
+            if (function != njs_function(&pr->value)) {
                 continue;
             }
 
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_error.c
--- a/njs/njs_error.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_error.c	Mon Jul 08 17:51:35 2019 +0300
@@ -542,7 +542,7 @@ njs_memory_error_prototype_create(njs_vm
 
     index = NJS_PROTOTYPE_INTERNAL_ERROR;
 
-    function = value->data.u.function;
+    function = njs_function(value);
     proto = njs_property_prototype_create(vm, &function->object.hash,
                                           &vm->prototypes[index].object);
     if (proto == NULL) {
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_fs.c
--- a/njs/njs_fs.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_fs.c	Mon Jul 08 17:51:35 2019 +0300
@@ -289,7 +289,7 @@ done:
     cont = njs_vm_continuation(vm);
     cont->u.cont.function = njs_fs_done;
 
-    return njs_function_apply(vm, callback->data.u.function,
+    return njs_function_apply(vm, njs_function(callback),
                               arguments, 3, (njs_index_t) &vm->retval);
 
 fail:
@@ -709,7 +709,7 @@ done:
     cont = njs_vm_continuation(vm);
     cont->u.cont.function = njs_fs_done;
 
-    return njs_function_apply(vm, callback->data.u.function,
+    return njs_function_apply(vm, njs_function(callback),
                               arguments, 2, (njs_index_t) &vm->retval);
 }
 
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_function.c
--- a/njs/njs_function.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_function.c	Mon Jul 08 17:51:35 2019 +0300
@@ -80,7 +80,7 @@ njs_function_value_copy(njs_vm_t *vm, nj
 {
     njs_function_t  *function, *copy;
 
-    function = value->data.u.function;
+    function = njs_function(value);
 
     if (!function->object.shared) {
         return function;
@@ -974,7 +974,7 @@ njs_function_prototype_call(njs_vm_t *vm
         nargs = 0;
     }
 
-    function = args[0].data.u.function;
+    function = njs_function(&args[0]);
 
     ret = njs_function_activate(vm, function, this, &args[2], nargs, retval,
                                 sizeof(njs_vmcode_function_call_t));
@@ -1018,7 +1018,7 @@ njs_function_prototype_apply(njs_vm_t *v
         return NXT_ERROR;
     }
 
-    func = (njs_argument(args, 0))->data.u.function;
+    func = njs_function(njs_argument(args, 0));
     this = njs_arg(args, nargs, 1);
     arr_like = njs_arg(args, nargs, 2);
 
@@ -1133,7 +1133,7 @@ njs_function_prototype_bind(njs_vm_t *vm
         return NXT_ERROR;
     }
 
-    function = njs_function_copy(vm, args[0].data.u.function);
+    function = njs_function_copy(vm, njs_function(&args[0]));
     if (nxt_slow_path(function == NULL)) {
         njs_memory_error(vm);
         return NXT_ERROR;
@@ -1163,9 +1163,7 @@ njs_function_prototype_bind(njs_vm_t *vm
 
     memcpy(values, args, size);
 
-    vm->retval.data.u.function = function;
-    vm->retval.type = NJS_FUNCTION;
-    vm->retval.data.truth = 1;
+    njs_set_function(&vm->retval, function);
 
     return NXT_OK;
 }
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_generator.c
--- a/njs/njs_generator.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_generator.c	Mon Jul 08 17:51:35 2019 +0300
@@ -2319,7 +2319,7 @@ njs_generate_function_declaration(njs_vm
         return NXT_OK;
     }
 
-    lambda = var->value.data.u.function->u.lambda;
+    lambda = njs_function_lambda(&var->value);
 
     ret = njs_generate_function_scope(vm, lambda, node,
                                       &node->u.reference.name);
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_json.c
--- a/njs/njs_json.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_json.c	Mon Jul 08 17:51:35 2019 +0300
@@ -223,7 +223,7 @@ njs_json_parse(njs_vm_t *vm, njs_value_t
 
         parse = njs_vm_continuation(vm);
         parse->u.cont.function = njs_json_parse_continuation;
-        parse->function = reviver->data.u.function;
+        parse->function = njs_function(reviver);
 
         if (nxt_array_init(&parse->stack, NULL, 4, sizeof(njs_json_state_t),
                            &njs_array_mem_proto, vm->mem_pool)
@@ -256,7 +256,7 @@ njs_vm_json_parse(njs_vm_t *vm, njs_valu
 {
     njs_function_t  *parse;
 
-    parse = njs_json_object_properties[0].value.data.u.function;
+    parse = njs_function(&njs_json_object_properties[0].value);
 
     return njs_vm_call(vm, parse, args, nargs);
 }
@@ -354,7 +354,7 @@ njs_vm_json_stringify(njs_vm_t *vm, njs_
 {
     njs_function_t  *stringify;
 
-    stringify = njs_json_object_properties[1].value.data.u.function;
+    stringify = njs_function(&njs_json_object_properties[1].value);
 
     return njs_vm_call(vm, stringify, args, nargs);
 }
@@ -1490,7 +1490,7 @@ njs_object_to_json_function(njs_vm_t *vm
     prop = njs_object_property(vm, njs_object(value), &lhq);
 
     if (prop != NULL && njs_is_function(&prop->value)) {
-        return prop->value.data.u.function;
+        return njs_function(&prop->value);
     }
 
     return NULL;
@@ -1582,7 +1582,7 @@ njs_json_stringify_replacer(njs_vm_t *vm
 
     njs_set_invalid(&stringify->retval);
 
-    return njs_function_apply(vm, stringify->replacer.data.u.function,
+    return njs_function_apply(vm, njs_function(&stringify->replacer),
                               arguments, 3, (njs_index_t) &stringify->retval);
 }
 
@@ -2212,7 +2212,7 @@ njs_dump_value(njs_json_stringify_t *str
         break;
 
     case NJS_FUNCTION:
-        if (value->data.u.function->native) {
+        if (njs_function(value)->native) {
             njs_dump("[Function: native]");
 
         } else {
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_object.c
--- a/njs/njs_object.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_object.c	Mon Jul 08 17:51:35 2019 +0300
@@ -1594,7 +1594,7 @@ njs_object_prototype_create(njs_vm_t *vm
     const njs_value_t  *proto;
 
     proto = NULL;
-    function = value->data.u.function;
+    function = njs_function(value);
     index = function - vm->constructors;
 
     if (index >= 0 && index < NJS_PROTOTYPE_MAX) {
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_object_property.c
--- a/njs/njs_object_property.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_object_property.c	Mon Jul 08 17:51:35 2019 +0300
@@ -414,9 +414,7 @@ njs_external_property_query(njs_vm_t *vm
 done:
 
     if (ext_proto->type == NJS_EXTERN_METHOD) {
-        prop->value.type = NJS_FUNCTION;
-        prop->value.data.u.function = ext_proto->function;
-        prop->value.data.truth = 1;
+        njs_set_function(&prop->value, ext_proto->function);
     }
 
     pq->lhq.value = prop;
@@ -523,7 +521,7 @@ njs_value_property(njs_vm_t *vm, const n
                 break;
             }
 
-            return njs_function_activate(vm, prop->getter.data.u.function,
+            return njs_function_activate(vm, njs_function(&prop->getter),
                                          value, NULL, 0, (njs_index_t) retval,
                                          advance);
 
@@ -631,7 +629,7 @@ njs_value_property_set(njs_vm_t *vm, njs
 
                 if (njs_is_function(&prop->setter)) {
                     return njs_function_activate(vm,
-                                                 prop->setter.data.u.function,
+                                                 njs_function(&prop->setter),
                                                  object, value, 1,
                                                  (njs_index_t) &vm->retval,
                                                  advance);
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_parser.c	Mon Jul 08 17:51:35 2019 +0300
@@ -619,9 +619,7 @@ njs_parser_function_alloc(njs_vm_t *vm, 
         return NULL;
     }
 
-    var->value.data.u.function = function;
-    var->value.type = NJS_FUNCTION;
-    var->value.data.truth = 1;
+    njs_set_function(&var->value, function);
 
     if (var->index != NJS_INDEX_NONE
         && njs_scope_accumulative(vm, parser->scope))
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_parser_terminal.c	Mon Jul 08 17:51:35 2019 +0300
@@ -452,16 +452,13 @@ njs_parser_builtin(njs_vm_t *vm, njs_par
 
     case NJS_FUNCTION:
         index = node->token - NJS_TOKEN_FIRST_FUNCTION;
-        var->value.data.u.function = &vm->shared->functions[index];
+        njs_set_function(&var->value, &vm->shared->functions[index]);
         break;
 
     default:
         return NXT_ERROR;
     }
 
-    var->value.type = type;
-    var->value.data.truth = 1;
-
     ret = njs_variable_reference(vm, scope, node, name, hash, NJS_REFERENCE);
     if (nxt_slow_path(ret != NXT_OK)) {
         return NXT_ERROR;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_string.c
--- a/njs/njs_string.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_string.c	Mon Jul 08 17:51:35 2019 +0300
@@ -3039,7 +3039,7 @@ njs_string_prototype_replace(njs_vm_t *v
         }
 
     } else {
-        r->function = args[2].data.u.function;
+        r->function = njs_function(&args[2]);
     }
 
     r->part[0].start = string.start;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_time.c
--- a/njs/njs_time.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_time.c	Mon Jul 08 17:51:35 2019 +0300
@@ -48,7 +48,7 @@ njs_set_timer(njs_vm_t *vm, njs_value_t 
     n = immediate ? 2 : 3;
 
     event->destructor = ops->clear_timer;
-    event->function = args[1].data.u.function;
+    event->function = njs_function(&args[1]);
     event->nargs = (nargs >= n) ? nargs - n : 0;
     event->once = 1;
     event->posted = 0;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_value.c
--- a/njs/njs_value.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_value.c	Mon Jul 08 17:51:35 2019 +0300
@@ -210,7 +210,7 @@ njs_value_to_primitive(njs_vm_t *vm, njs
                             continue;
                         }
 
-                        function = prop->value.data.u.function;
+                        function = njs_function(&prop->value);
 
                         ret = njs_function_apply(vm, function, value, 1,
                                                  (njs_index_t) retval);
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_value.h
--- a/njs/njs_value.h	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_value.h	Mon Jul 08 17:51:35 2019 +0300
@@ -516,6 +516,10 @@ typedef enum {
     ((value)->data.u.function)
 
 
+#define njs_function_lambda(value)                                            \
+    ((value)->data.u.function->u.lambda)
+
+
 #define njs_object(value)                                                     \
     ((value)->data.u.object)
 
@@ -607,6 +611,15 @@ njs_set_array(njs_value_t *value, njs_ar
 
 
 nxt_inline void
+njs_set_function(njs_value_t *value, njs_function_t *function)
+{
+    value->data.u.function = function;
+    value->type = NJS_FUNCTION;
+    value->data.truth = 1;
+}
+
+
+nxt_inline void
 njs_set_date(njs_value_t *value, njs_date_t *date)
 {
     value->data.u.date = date;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_variable.c
--- a/njs/njs_variable.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_variable.c	Mon Jul 08 17:51:35 2019 +0300
@@ -666,7 +666,7 @@ njs_vm_function(njs_vm_t *vm, const nxt_
     value = njs_vm_value(vm, name);
 
     if (njs_is_function(value)) {
-        return value->data.u.function;
+        return njs_function(value);
     }
 
     return NULL;
diff -r 5eee6645c7e2 -r 4c9047471ed0 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Jul 08 17:49:43 2019 +0300
+++ b/njs/njs_vm.c	Mon Jul 08 17:51:35 2019 +0300
@@ -294,9 +294,7 @@ njs_vmcode_function(njs_vm_t *vm, njs_va
         return NXT_ERROR;
     }
 
-    vm->retval.data.u.function = function;
-    vm->retval.type = NJS_FUNCTION;
-    vm->retval.data.truth = 1;
+    njs_set_function(&vm->retval, function);
 
     return sizeof(njs_vmcode_function_t);
 }
@@ -1746,7 +1744,7 @@ njs_function_frame_create(njs_vm_t *vm, 
 
     if (nxt_fast_path(njs_is_function(value))) {
 
-        function = value->data.u.function;
+        function = njs_function(value);
 
         if (ctor) {
             if (!function->ctor) {
@@ -1792,7 +1790,7 @@ njs_function_new_object(njs_vm_t *vm, nj
         lhq.key_hash = NJS_PROTOTYPE_HASH;
         lhq.key = nxt_string_value("prototype");
         lhq.proto = &njs_object_hash_proto;
-        function = value->data.u.function;
+        function = njs_function(value);
 
         ret = nxt_lvlhsh_find(&function->object.hash, &lhq);
 


More information about the nginx-devel mailing list