]> git.kaiwu.me - njs.git/commitdiff
Making njs_function_activate() more generic.
authorhongzhidao <hongzhidao@gmail.com>
Mon, 7 Jan 2019 20:11:51 +0000 (04:11 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Mon, 7 Jan 2019 20:11:51 +0000 (04:11 +0800)
njs/njs.c
njs/njs_function.c
njs/njs_function.h
njs/njs_vm.c
njs/njs_vm.h

index 58c3ddde5b32b8bb7c4c11a94a440de293f3866f..58339c90c2a96b8c536ff3503ec1cfc256a50645 100644 (file)
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -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);
+    vm->current = (u_char *) njs_continuation_nexus;
 
-        cont->function = function->u.native;
-        cont->args_types = function->args_types;
-        cont->retval = NJS_INDEX_GLOBAL_RETVAL;
+    ret = njs_function_activate(vm, function, this, args, nargs,
+                                NJS_INDEX_GLOBAL_RETVAL,
+                                sizeof(njs_vmcode_generic_t));
 
-        cont->return_address = (u_char *) stop;
-        vm->current = (u_char *) njs_continuation_nexus;
-
-    } 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);
index 4e2b428c752c9fb905135a48a00dc275cafff434..dd100014dc352acb2dd60c9248ee200d926df2c8 100644 (file)
@@ -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, njs_value_t *args, nxt_uint_t nargs,
 
     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 *vm, njs_value_t *args, nxt_uint_t nargs,
         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_function_t *function, njs_value_t *this,
             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_function_t *function, njs_value_t *this,
         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);
 }
 
 
index 076cb62c44ca61b42470e5061df32a88be134fd3..fee32ad169bce7a11a76f8e10246fba34456b4c5 100644 (file)
@@ -170,6 +170,9 @@ njs_ret_t njs_function_native_frame(njs_vm_t *vm, njs_function_t *function,
 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,
index be9f1ff3304ae41593c7a4f72166911d26f983d2..289012ae9dd24c06424f4d516123881519932aa0 100644 (file)
@@ -2281,10 +2281,15 @@ njs_vm_scopes_restore(njs_vm_t *vm, njs_frame_t *frame,
 }
 
 
-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 },
 };
 
 
index 0e0fb5d59d7f3ae54d55d6e8ab4f969a59fa654f..aa6a925fba66774c3d39b606c22e8858bf639496 100644 (file)
@@ -1304,7 +1304,7 @@ extern const njs_value_t  njs_string_memory_error;
 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_ */