From 40b8d669f1e16f0401ef792b8bc1974c4196dc37 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 26 Dec 2018 16:05:23 +0300 Subject: [PATCH] Added support of native function calls in njs_vm_call(). --- njs/njs.c | 44 ++++++++++++++++++++++++++++++++------------ njs/njs_function.c | 2 +- njs/njs_function.h | 2 +- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/njs/njs.c b/njs/njs.c index 209f7c05..67801eb6 100644 --- a/njs/njs.c +++ b/njs/njs.c @@ -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 *function, const njs_value_t *args, 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; - 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 (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; + + 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; + } } ret = njs_vmcode_interpreter(vm); diff --git a/njs/njs_function.c b/njs/njs_function.c index 83d3db1d..a3d860d8 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -214,7 +214,7 @@ njs_function_arguments_thrower(njs_vm_t *vm, njs_value_t *value, 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 --git a/njs/njs_function.h b/njs/njs_function.h index c1396a0f..feb83bc9 100644 --- a/njs/njs_function.h +++ b/njs/njs_function.h @@ -165,7 +165,7 @@ njs_ret_t njs_function_constructor(njs_vm_t *vm, njs_value_t *args, 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, -- 2.47.3