[njs] Refactored njs_function_lambda_call().

Dmitry Volyntsev xeioex at nginx.com
Fri Jan 11 16:20:28 UTC 2019


details:   https://hg.nginx.org/njs/rev/8fb687d866de
branches:  
changeset: 726:8fb687d866de
user:      hongzhidao <hongzhidao at gmail.com>
date:      Fri Jan 11 18:57:57 2019 +0800
description:
Refactored njs_function_lambda_call().

diffstat:

 njs/njs_function.c |  35 +++++++++++++++++----------------
 njs/njs_function.h |   2 +-
 njs/njs_vm.c       |  55 ++++++++++++++++++++++++-----------------------------
 3 files changed, 44 insertions(+), 48 deletions(-)

diffs (163 lines):

diff -r 112bd230858c -r 8fb687d866de njs/njs_function.c
--- a/njs/njs_function.c	Thu Jan 10 20:18:27 2019 +0300
+++ b/njs/njs_function.c	Fri Jan 11 18:57:57 2019 +0800
@@ -394,7 +394,8 @@ njs_function_frame_alloc(njs_vm_t *vm, s
 
 
 nxt_noinline njs_ret_t
-njs_function_lambda_call(njs_vm_t *vm, njs_index_t retval, size_t advance)
+njs_function_lambda_call(njs_vm_t *vm, njs_index_t retval,
+    u_char *return_address)
 {
     size_t                 size;
     njs_ret_t              ret;
@@ -408,9 +409,9 @@ njs_function_lambda_call(njs_vm_t *vm, n
     frame = (njs_frame_t *) vm->top_frame;
 
     frame->retval = retval;
+    frame->return_address = return_address;
 
     function = frame->native.function;
-    frame->return_address = vm->current + advance;
 
     lambda = function->u.lambda;
     vm->current = lambda->start;
@@ -947,35 +948,35 @@ njs_function_activate(njs_vm_t *vm, njs_
     const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
     njs_index_t retval, size_t advance)
 {
+    u_char              *return_address;
     njs_ret_t           ret;
     njs_continuation_t  *cont;
 
+    ret = njs_function_frame(vm, function, this, args, nargs,
+                             NJS_CONTINUATION_SIZE, 0);
+    if (nxt_slow_path(ret != NXT_OK)) {
+        return ret;
+    }
+
+    return_address = vm->current + advance;
+
     if (function->native) {
-        ret = njs_function_native_frame(vm, function, this, args, nargs,
-                                        NJS_CONTINUATION_SIZE, 0);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return ret;
-        }
-
         cont = njs_vm_continuation(vm);
 
         cont->function = function->u.native;
         cont->args_types = function->args_types;
         cont->retval = retval;
+        cont->return_address = return_address;
 
-        cont->return_address = vm->current + advance;
         vm->current = (u_char *) njs_continuation_nexus;
 
-        return NJS_APPLIED;
+        ret = NJS_APPLIED;
+
+    } else {
+        ret = njs_function_lambda_call(vm, retval, return_address);
     }
 
-    ret = njs_function_lambda_frame(vm, function, this, args, nargs, 0);
-
-    if (nxt_slow_path(ret != NXT_OK)) {
-        return ret;
-    }
-
-    return njs_function_lambda_call(vm, retval, advance);
+    return ret;
 }
 
 
diff -r 112bd230858c -r 8fb687d866de njs/njs_function.h
--- a/njs/njs_function.h	Thu Jan 10 20:18:27 2019 +0300
+++ b/njs/njs_function.h	Fri Jan 11 18:57:57 2019 +0800
@@ -171,7 +171,7 @@ njs_ret_t njs_function_activate(njs_vm_t
     const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
     njs_index_t retval, size_t advance);
 njs_ret_t njs_function_lambda_call(njs_vm_t *vm, njs_index_t retval,
-    size_t advance);
+    u_char *return_address);
 njs_ret_t njs_function_native_call(njs_vm_t *vm, njs_function_native_t native,
     njs_value_t *args, uint8_t *args_types, nxt_uint_t nargs,
     njs_index_t retval);
diff -r 112bd230858c -r 8fb687d866de njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Jan 10 20:18:27 2019 +0300
+++ b/njs/njs_vm.c	Fri Jan 11 18:57:57 2019 +0800
@@ -2014,9 +2014,8 @@ njs_vmcode_method_frame(njs_vm_t *vm, nj
 njs_ret_t
 njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval)
 {
+    u_char              *return_address;
     njs_ret_t           ret;
-    nxt_uint_t          nargs;
-    njs_value_t         *args;
     njs_function_t      *function;
     njs_continuation_t  *cont;
     njs_native_frame_t  *frame;
@@ -2024,37 +2023,33 @@ njs_vmcode_function_call(njs_vm_t *vm, n
     frame = vm->top_frame;
     function = frame->function;
 
-    if (!function->native) {
+    return_address = vm->current + sizeof(njs_vmcode_function_call_t);
+
+    if (function->native) {
+        if (function->continuation_size != 0) {
+            cont = njs_vm_continuation(vm);
+
+            cont->function = function->u.native;
+            cont->args_types = function->args_types;
+            cont->retval = (njs_index_t) retval;
+            cont->return_address = return_address;
+
+            vm->current = (u_char *) njs_continuation_nexus;
+
+            ret = NJS_APPLIED;
+
+        } else {
+            ret = njs_function_native_call(vm, function->u.native,
+                                           frame->arguments,
+                                           function->args_types, frame->nargs,
+                                           (njs_index_t) retval);
+        }
+
+    } else {
         ret = njs_function_lambda_call(vm, (njs_index_t) retval,
-                                       sizeof(njs_vmcode_function_call_t));
-
-        if (nxt_fast_path(ret != NJS_ERROR)) {
-            return 0;
-        }
-
-        return ret;
+                                       return_address);
     }
 
-    args = frame->arguments;
-    nargs = frame->nargs;
-
-    if (function->continuation_size != 0) {
-        cont = njs_vm_continuation(vm);
-
-        cont->function = function->u.native;
-        cont->args_types = function->args_types;
-        cont->retval = (njs_index_t) retval;
-
-        cont->return_address = vm->current + sizeof(njs_vmcode_function_call_t);
-        vm->current = (u_char *) njs_continuation_nexus;
-
-        return 0;
-    }
-
-    ret = njs_function_native_call(vm, function->u.native, args,
-                                   function->args_types, nargs,
-                                   (njs_index_t) retval);
-
     switch (ret) {
     case NXT_OK:
         return sizeof(njs_vmcode_function_call_t);


More information about the nginx-devel mailing list