[njs] Passing to native function additional magic argument.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 8 13:30:16 UTC 2019


details:   https://hg.nginx.org/njs/rev/6df48738a043
branches:  
changeset: 1225:6df48738a043
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 08 16:29:24 2019 +0300
description:
Passing to native function additional magic argument.

This allows to make more generic function handlers.

diffstat:

 src/njs_function.c |  17 +++++++++++------
 src/njs_value.h    |  17 +++++++++++++----
 2 files changed, 24 insertions(+), 10 deletions(-)

diffs (123 lines):

diff -r 4d33ea223de0 -r 6df48738a043 src/njs_function.c
--- a/src/njs_function.c	Tue Nov 05 20:49:57 2019 +0300
+++ b/src/njs_function.c	Fri Nov 08 16:29:24 2019 +0300
@@ -591,8 +591,7 @@ njs_function_native_call(njs_vm_t *vm)
     function = native->function;
 
     ret = function->u.native(vm, native->arguments, native->nargs,
-                             frame->retval);
-
+                             function->magic);
     if (njs_slow_path(ret == NJS_ERROR)) {
         return ret;
     }
@@ -919,9 +918,10 @@ njs_function_instance_length(njs_vm_t *v
 
 static njs_int_t
 njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t retval)
+    njs_index_t unused)
 {
     njs_int_t          ret;
+    njs_frame_t        *frame;
     njs_function_t     *function;
     const njs_value_t  *this;
 
@@ -939,6 +939,8 @@ njs_function_prototype_call(njs_vm_t *vm
         nargs = 0;
     }
 
+    frame = (njs_frame_t *) vm->top_frame;
+
     function = njs_function(&args[0]);
 
     /* Skip the "call" method frame. */
@@ -949,7 +951,7 @@ njs_function_prototype_call(njs_vm_t *vm
         return ret;
     }
 
-    ret = njs_function_frame_invoke(vm, retval);
+    ret = njs_function_frame_invoke(vm, frame->retval);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
@@ -960,10 +962,11 @@ njs_function_prototype_call(njs_vm_t *vm
 
 static njs_int_t
 njs_function_prototype_apply(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t retval)
+    njs_index_t unused)
 {
     uint32_t        i, length;
     njs_int_t       ret;
+    njs_frame_t     *frame;
     njs_value_t     name, *this, *arr_like;
     njs_array_t     *arr;
     njs_function_t  *func;
@@ -1021,12 +1024,14 @@ activate:
     /* Skip the "apply" method frame. */
     vm->top_frame->skip = 1;
 
+    frame = (njs_frame_t *) vm->top_frame;
+
     ret = njs_function_frame(vm, func, this, args, length, 0);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
 
-    ret = njs_function_frame_invoke(vm, retval);
+    ret = njs_function_frame_invoke(vm, frame->retval);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
diff -r 4d33ea223de0 -r 6df48738a043 src/njs_value.h
--- a/src/njs_value.h	Tue Nov 05 20:49:57 2019 +0300
+++ b/src/njs_value.h	Fri Nov 08 16:29:24 2019 +0300
@@ -234,14 +234,14 @@ struct njs_function_s {
     njs_object_t                      object;
 
     uint8_t                           args_offset;
-    uint8_t                           args_count;
 
-    /* Function is a closure. */
+    uint8_t                           args_count:5;
     uint8_t                           closure:1;
-
     uint8_t                           native:1;
     uint8_t                           ctor:1;
 
+    uint8_t                           magic;
+
     union {
         njs_function_lambda_t         *lambda;
         njs_function_native_t         native;
@@ -391,12 +391,13 @@ typedef struct {
 }
 
 
-#define njs_native_function(_function, _args_count) {                         \
+#define _njs_native_function(_function, _args_count, _magic) {                \
     .data = {                                                                 \
         .type = NJS_FUNCTION,                                                 \
         .truth = 1,                                                           \
         .u.function = & (njs_function_t) {                                    \
             .native = 1,                                                      \
+            .magic = _magic,                                                  \
             .args_count = _args_count,                                        \
             .args_offset = 1,                                                 \
             .u.native = _function,                                            \
@@ -408,6 +409,14 @@ typedef struct {
 }
 
 
+#define njs_native_function(_function, _args_count)                           \
+    _njs_native_function(_function, _args_count, 0)
+
+
+#define njs_native_function2(_function, _args_count, _magic)                  \
+    _njs_native_function(_function, _args_count, _magic)
+
+
 #define njs_prop_handler(_handler) {                                          \
     .data = {                                                                 \
         .type = NJS_INVALID,                                                  \


More information about the nginx-devel mailing list