[njs] Added support of native function calls in njs_vm_call().

Dmitry Volyntsev xeioex at nginx.com
Wed Dec 26 13:06:48 UTC 2018


details:   https://hg.nginx.org/njs/rev/36989f329e16
branches:  
changeset: 697:36989f329e16
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Dec 26 16:05:23 2018 +0300
description:
Added support of native function calls in njs_vm_call().

diffstat:

 njs/njs.c          |  44 ++++++++++++++++++++++++++++++++------------
 njs/njs_function.c |   2 +-
 njs/njs_function.h |   2 +-
 3 files changed, 34 insertions(+), 14 deletions(-)

diffs (87 lines):

diff -r e1e54922e085 -r 36989f329e16 njs/njs.c
--- a/njs/njs.c	Wed Dec 26 13:23:16 2018 +0300
+++ b/njs/njs.c	Wed Dec 26 16:05:23 2018 +0300
@@ -461,9 +461,10 @@ 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;
+    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,
@@ -474,17 +475,36 @@ njs_vm_call(njs_vm_t *vm, njs_function_t
 
     this = (njs_value_t *) &njs_value_void;
 
-    ret = njs_function_frame(vm, function, this, args, nargs, 0);
-    if (nxt_slow_path(ret != NXT_OK)) {
-        return ret;
-    }
+    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;
 
-    current = vm->current;
-    vm->current = (u_char *) stop;
+        cont->return_address = (u_char *) stop;
+        vm->current = (u_char *) njs_continuation_nexus;
 
-    ret = njs_function_call(vm, NJS_INDEX_GLOBAL_RETVAL, 0);
-    if (nxt_slow_path(ret == NXT_ERROR)) {
-        return ret;
+    } 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;
+        }
     }
 
     ret = njs_vmcode_interpreter(vm);
diff -r e1e54922e085 -r 36989f329e16 njs/njs_function.c
--- a/njs/njs_function.c	Wed Dec 26 13:23:16 2018 +0300
+++ b/njs/njs_function.c	Wed Dec 26 16:05:23 2018 +0300
@@ -214,7 +214,7 @@ njs_function_arguments_thrower(njs_vm_t 
 
 njs_ret_t
 njs_function_native_frame(njs_vm_t *vm, njs_function_t *function,
-    const njs_value_t *this, njs_value_t *args, nxt_uint_t nargs,
+    const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
     size_t reserve, nxt_bool_t ctor)
 {
     size_t              size;
diff -r e1e54922e085 -r 36989f329e16 njs/njs_function.h
--- a/njs/njs_function.h	Wed Dec 26 13:23:16 2018 +0300
+++ b/njs/njs_function.h	Wed Dec 26 16:05:23 2018 +0300
@@ -165,7 +165,7 @@ njs_ret_t njs_function_constructor(njs_v
 njs_ret_t njs_function_apply(njs_vm_t *vm, njs_function_t *function,
     njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
 njs_ret_t njs_function_native_frame(njs_vm_t *vm, njs_function_t *function,
-    const njs_value_t *this, njs_value_t *args, nxt_uint_t nargs,
+    const njs_value_t *this, const njs_value_t *args, nxt_uint_t nargs,
     size_t reserve, nxt_bool_t ctor);
 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,


More information about the nginx-devel mailing list