From: hongzhidao Date: Mon, 7 Jan 2019 20:11:51 +0000 (+0800) Subject: Making njs_function_activate() more generic. X-Git-Tag: 0.2.8~83 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=b8b6265662b22a505220856983bf47c4a83314d5;p=njs.git Making njs_function_activate() more generic. --- diff --git a/njs/njs.c b/njs/njs.c index 58c3ddde..58339c90 100644 --- 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); diff --git a/njs/njs_function.c b/njs/njs_function.c index 4e2b428c..dd100014 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -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); } diff --git a/njs/njs_function.h b/njs/njs_function.h index 076cb62c..fee32ad1 100644 --- a/njs/njs_function.h +++ b/njs/njs_function.h @@ -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, diff --git a/njs/njs_vm.c b/njs/njs_vm.c index be9f1ff3..289012ae 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -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 }, }; diff --git a/njs/njs_vm.h b/njs/njs_vm.h index 0e0fb5d5..aa6a925f 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -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_ */