[njs] Making njs_function_activate() more generic.

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 10 16:00:51 UTC 2019


details:   https://hg.nginx.org/njs/rev/6ee587abd958
branches:  
changeset: 719:6ee587abd958
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Jan 08 04:11:51 2019 +0800
description:
Making njs_function_activate() more generic.

diffstat:

 njs/njs.c          |  46 +++++++++-------------------------------------
 njs/njs_function.c |  43 ++++++++++++++++++++++++++++---------------
 njs/njs_function.h |   3 +++
 njs/njs_vm.c       |   7 ++++++-
 njs/njs_vm.h       |   2 +-
 5 files changed, 47 insertions(+), 54 deletions(-)

diffs (213 lines):

diff -r 57b89039ff23 -r 6ee587abd958 njs/njs.c
--- a/njs/njs.c	Thu Jan 10 17:30:30 2019 +0300
+++ b/njs/njs.c	Tue Jan 08 04:11:51 2019 +0800
@@ -461,50 +461,22 @@ nxt_int_t
 njs_vm_call(njs_vm_t *vm, njs_function_t *function, const njs_value_t *args,
     nxt_uint_t nargs)
 {
-    u_char              *current;
-    njs_ret_t           ret;
-    njs_value_t         *this;
-    njs_continuation_t  *cont;
-
-    static const njs_vmcode_stop_t  stop[] = {
-        { .code = { .operation = njs_vmcode_stop,
-                    .operands =  NJS_VMCODE_1OPERAND,
-                    .retval = NJS_VMCODE_NO_RETVAL },
-          .retval = NJS_INDEX_GLOBAL_RETVAL },
-    };
+    u_char       *current;
+    njs_ret_t    ret;
+    njs_value_t  *this;
 
     this = (njs_value_t *) &njs_value_void;
 
     current = vm->current;
 
-    if (function->native) {
-        ret = njs_function_native_frame(vm, function, this, &args[0],
-                                        nargs, NJS_CONTINUATION_SIZE, 0);
-        if (ret != NJS_OK) {
-            return NJS_ERROR;
-        }
-
-        cont = njs_vm_continuation(vm);
-
-        cont->function = function->u.native;
-        cont->args_types = function->args_types;
-        cont->retval = NJS_INDEX_GLOBAL_RETVAL;
+    vm->current = (u_char *) njs_continuation_nexus;
 
-        cont->return_address = (u_char *) stop;
-        vm->current = (u_char *) njs_continuation_nexus;
+    ret = njs_function_activate(vm, function, this, args, nargs,
+                                NJS_INDEX_GLOBAL_RETVAL,
+                                sizeof(njs_vmcode_generic_t));
 
-    } else {
-        ret = njs_function_frame(vm, function, this, args, nargs, 0);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return ret;
-        }
-
-        vm->current = (u_char *) stop;
-
-        ret = njs_function_call(vm, NJS_INDEX_GLOBAL_RETVAL, 0);
-        if (nxt_slow_path(ret == NXT_ERROR)) {
-            return ret;
-        }
+    if (nxt_slow_path(ret == NXT_ERROR)) {
+        return ret;
     }
 
     ret = njs_vmcode_interpreter(vm);
diff -r 57b89039ff23 -r 6ee587abd958 njs/njs_function.c
--- a/njs/njs_function.c	Thu Jan 10 17:30:30 2019 +0300
+++ b/njs/njs_function.c	Tue Jan 08 04:11:51 2019 +0800
@@ -10,8 +10,6 @@
 
 static njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args,
     uint8_t *args_types, nxt_uint_t nargs);
-static njs_ret_t njs_function_activate(njs_vm_t *vm, njs_function_t *function,
-    njs_value_t *this, njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
 
 
 njs_function_t *
@@ -897,6 +895,7 @@ static njs_ret_t
 njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t retval)
 {
+    njs_ret_t       ret;
     njs_value_t     *this;
     njs_function_t  *function;
 
@@ -916,7 +915,16 @@ njs_function_prototype_call(njs_vm_t *vm
 
     function = args[0].data.u.function;
 
-    return njs_function_activate(vm, function, this, &args[2], nargs, retval);
+    ret = njs_function_activate(vm, function, this, &args[2], nargs, retval,
+                                sizeof(njs_vmcode_function_call_t));
+    if (nxt_slow_path(ret == NXT_ERROR)) {
+        return ret;
+    }
+
+    /* Skip the "call" method frame. */
+    vm->top_frame->previous->skip = 1;
+
+    return NJS_APPLIED;
 }
 
 
@@ -924,6 +932,7 @@ static njs_ret_t
 njs_function_prototype_apply(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t retval)
 {
+    njs_ret_t       ret;
     njs_array_t     *array;
     njs_value_t     *this;
     njs_function_t  *function;
@@ -954,13 +963,24 @@ njs_function_prototype_apply(njs_vm_t *v
         nargs = 0;
     }
 
-    return njs_function_activate(vm, function, this, args, nargs, retval);
+    ret = njs_function_activate(vm, function, this, args, nargs, retval,
+                                sizeof(njs_vmcode_function_call_t));
+
+    if (nxt_slow_path(ret == NXT_ERROR)) {
+        return ret;
+    }
+
+    /* Skip the "apply" method frame. */
+    vm->top_frame->previous->skip = 1;
+
+    return NJS_APPLIED;
 }
 
 
-static njs_ret_t
+njs_ret_t
 njs_function_activate(njs_vm_t *vm, njs_function_t *function, njs_value_t *this,
-    njs_value_t *args, nxt_uint_t nargs, njs_index_t retval)
+    const njs_value_t *args, nxt_uint_t nargs, njs_index_t retval,
+    size_t advance)
 {
     njs_ret_t           ret;
     njs_continuation_t  *cont;
@@ -972,17 +992,13 @@ njs_function_activate(njs_vm_t *vm, njs_
             return ret;
         }
 
-        /* Skip the "call/apply" method frame. */
-        vm->top_frame->previous->skip = 1;
-
         cont = njs_vm_continuation(vm);
 
         cont->function = function->u.native;
         cont->args_types = function->args_types;
         cont->retval = retval;
 
-        cont->return_address = vm->current
-                               + sizeof(njs_vmcode_function_call_t);
+        cont->return_address = vm->current + advance;
         vm->current = (u_char *) njs_continuation_nexus;
 
         return NJS_APPLIED;
@@ -994,10 +1010,7 @@ njs_function_activate(njs_vm_t *vm, njs_
         return ret;
     }
 
-    /* Skip the "call/apply" method frame. */
-    vm->top_frame->previous->skip = 1;
-
-    return njs_function_call(vm, retval, sizeof(njs_vmcode_function_call_t));
+    return njs_function_call(vm, retval, advance);
 }
 
 
diff -r 57b89039ff23 -r 6ee587abd958 njs/njs_function.h
--- a/njs/njs_function.h	Thu Jan 10 17:30:30 2019 +0300
+++ b/njs/njs_function.h	Tue Jan 08 04:11:51 2019 +0800
@@ -170,6 +170,9 @@ njs_ret_t njs_function_native_frame(njs_
 njs_ret_t njs_function_frame(njs_vm_t *vm, njs_function_t *function,
     const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
     nxt_bool_t ctor);
+njs_ret_t njs_function_activate(njs_vm_t *vm, njs_function_t *function,
+    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_call(njs_vm_t *vm, njs_index_t retval, size_t advance);
 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,
diff -r 57b89039ff23 -r 6ee587abd958 njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Jan 10 17:30:30 2019 +0300
+++ b/njs/njs_vm.c	Tue Jan 08 04:11:51 2019 +0800
@@ -2281,10 +2281,15 @@ njs_vm_scopes_restore(njs_vm_t *vm, njs_
 }
 
 
-const njs_vmcode_1addr_t  njs_continuation_nexus[] = {
+const njs_vmcode_generic_t  njs_continuation_nexus[] = {
     { .code = { .operation = njs_vmcode_continuation,
                 .operands =  NJS_VMCODE_NO_OPERAND,
                 .retval = NJS_VMCODE_NO_RETVAL } },
+
+    { .code = { .operation = njs_vmcode_stop,
+                .operands =  NJS_VMCODE_1OPERAND,
+                .retval = NJS_VMCODE_NO_RETVAL },
+      .operand1 = NJS_INDEX_GLOBAL_RETVAL },
 };
 
 
diff -r 57b89039ff23 -r 6ee587abd958 njs/njs_vm.h
--- a/njs/njs_vm.h	Thu Jan 10 17:30:30 2019 +0300
+++ b/njs/njs_vm.h	Tue Jan 08 04:11:51 2019 +0800
@@ -1304,7 +1304,7 @@ extern const njs_value_t  njs_string_mem
 extern const nxt_mem_proto_t     njs_array_mem_proto;
 extern const nxt_lvlhsh_proto_t  njs_object_hash_proto;
 
-extern const njs_vmcode_1addr_t  njs_continuation_nexus[];
+extern const njs_vmcode_generic_t  njs_continuation_nexus[];
 
 
 #endif /* _NJS_VM_H_INCLUDED_ */


More information about the nginx-devel mailing list