]> git.kaiwu.me - njs.git/commitdiff
Passing to native function additional magic argument.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 8 Nov 2019 13:29:24 +0000 (16:29 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 8 Nov 2019 13:29:24 +0000 (16:29 +0300)
This allows to make more generic function handlers.

src/njs_function.c
src/njs_value.h

index e72d8cdb1cd553bd93c2dd17a3b91eb932a1394a..d6fcccf88e5d9bbe9e9c981d84530d3425a467b6 100644 (file)
@@ -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 *vm, njs_object_prop_t *prop,
 
 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, njs_value_t *args, njs_uint_t nargs,
         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, njs_value_t *args, njs_uint_t nargs,
         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, njs_value_t *args, njs_uint_t nargs,
 
 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;
     }
index 75a1ec2ecc0e1bba87d9986d95a49344b0ce45ee..cf79c12f745ea123db389669eae916b408e593ef 100644 (file)
@@ -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,                                                  \