From: hongzhidao Date: Mon, 7 Jan 2019 20:04:02 +0000 (+0800) Subject: Moving njs_normalize_args() into njs_function_native_call(). X-Git-Tag: 0.2.8~85 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=e7528ff3ba57487f79611c51f3c9fd6a59641e2d;p=njs.git Moving njs_normalize_args() into njs_function_native_call(). --- diff --git a/njs/njs_function.c b/njs/njs_function.c index 41032d67..9372227a 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -8,6 +8,8 @@ #include +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); @@ -533,13 +535,19 @@ 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, nxt_uint_t nargs, njs_index_t retval) + njs_value_t *args, uint8_t *args_types, nxt_uint_t nargs, + njs_index_t retval) { njs_ret_t ret; njs_value_t *value; njs_function_t *function; njs_native_frame_t *frame; + ret = njs_normalize_args(vm, args, args_types, nargs); + if (ret != NJS_OK) { + return ret; + } + ret = native(vm, args, nargs, retval); /* @@ -587,6 +595,140 @@ njs_function_native_call(njs_vm_t *vm, njs_function_native_t native, } +static njs_ret_t +njs_normalize_args(njs_vm_t *vm, njs_value_t *args, uint8_t *args_types, + nxt_uint_t nargs) +{ + nxt_uint_t n; + njs_trap_t trap; + + n = nxt_min(nargs, NJS_ARGS_TYPES_MAX); + + while (n != 0) { + + switch (*args_types) { + + case NJS_STRING_OBJECT_ARG: + + if (njs_is_null_or_void(args)) { + goto type_error; + } + + /* Fall through. */ + + case NJS_STRING_ARG: + + if (njs_is_string(args)) { + break; + } + + trap = NJS_TRAP_STRING_ARG; + goto trap; + + case NJS_NUMBER_ARG: + + if (njs_is_numeric(args)) { + break; + } + + trap = NJS_TRAP_NUMBER_ARG; + goto trap; + + case NJS_INTEGER_ARG: + + if (njs_is_numeric(args)) { + + /* Numbers are truncated to fit in 32-bit integers. */ + + if (isnan(args->data.u.number)) { + args->data.u.number = 0; + + } else if (args->data.u.number > 2147483647.0) { + args->data.u.number = 2147483647.0; + + } else if (args->data.u.number < -2147483648.0) { + args->data.u.number = -2147483648.0; + } + + break; + } + + trap = NJS_TRAP_NUMBER_ARG; + goto trap; + + case NJS_FUNCTION_ARG: + + switch (args->type) { + case NJS_STRING: + case NJS_FUNCTION: + break; + + default: + trap = NJS_TRAP_STRING_ARG; + goto trap; + } + + break; + + case NJS_REGEXP_ARG: + + switch (args->type) { + case NJS_VOID: + case NJS_STRING: + case NJS_REGEXP: + break; + + default: + trap = NJS_TRAP_STRING_ARG; + goto trap; + } + + break; + + case NJS_DATE_ARG: + if (!njs_is_date(args)) { + goto type_error; + } + + break; + + case NJS_OBJECT_ARG: + + if (njs_is_null_or_void(args)) { + goto type_error; + } + + break; + + case NJS_SKIP_ARG: + break; + + case 0: + return NJS_OK; + } + + args++; + args_types++; + n--; + } + + return NJS_OK; + +trap: + + njs_vm_trap_value(vm, args); + + return njs_trap(vm, trap); + +type_error: + + njs_type_error(vm, "cannot convert %s to %s", njs_type_string(args->type), + njs_arg_type_string(*args_types)); + + return NXT_ERROR; +} + + njs_native_frame_t * njs_function_previous_frame(njs_native_frame_t *frame) { diff --git a/njs/njs_function.h b/njs/njs_function.h index 77bc0489..ec869726 100644 --- a/njs/njs_function.h +++ b/njs/njs_function.h @@ -172,7 +172,8 @@ njs_ret_t njs_function_frame(njs_vm_t *vm, njs_function_t *function, nxt_bool_t ctor); 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, nxt_uint_t nargs, njs_index_t retval); + njs_value_t *args, uint8_t *args_types, nxt_uint_t nargs, + njs_index_t retval); njs_native_frame_t *njs_function_previous_frame(njs_native_frame_t *frame); void njs_function_frame_free(njs_vm_t *vm, njs_native_frame_t *frame); diff --git a/njs/njs_vm.c b/njs/njs_vm.c index 9c9b6a72..be9f1ff3 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -2042,11 +2042,6 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval) args = frame->arguments; nargs = frame->nargs; - ret = njs_normalize_args(vm, args, function->args_types, nargs); - if (ret != NJS_OK) { - return ret; - } - if (function->continuation_size != 0) { cont = njs_vm_continuation(vm); @@ -2060,7 +2055,8 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval) return 0; } - ret = njs_function_native_call(vm, function->u.native, args, nargs, + ret = njs_function_native_call(vm, function->u.native, args, + function->args_types, nargs, (njs_index_t) retval); switch (ret) { @@ -2076,140 +2072,6 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval) } -njs_ret_t -njs_normalize_args(njs_vm_t *vm, njs_value_t *args, uint8_t *args_types, - nxt_uint_t nargs) -{ - nxt_uint_t n; - njs_trap_t trap; - - n = nxt_min(nargs, NJS_ARGS_TYPES_MAX); - - while (n != 0) { - - switch (*args_types) { - - case NJS_STRING_OBJECT_ARG: - - if (njs_is_null_or_void(args)) { - goto type_error; - } - - /* Fall through. */ - - case NJS_STRING_ARG: - - if (njs_is_string(args)) { - break; - } - - trap = NJS_TRAP_STRING_ARG; - goto trap; - - case NJS_NUMBER_ARG: - - if (njs_is_numeric(args)) { - break; - } - - trap = NJS_TRAP_NUMBER_ARG; - goto trap; - - case NJS_INTEGER_ARG: - - if (njs_is_numeric(args)) { - - /* Numbers are truncated to fit in 32-bit integers. */ - - if (isnan(args->data.u.number)) { - args->data.u.number = 0; - - } else if (args->data.u.number > 2147483647.0) { - args->data.u.number = 2147483647.0; - - } else if (args->data.u.number < -2147483648.0) { - args->data.u.number = -2147483648.0; - } - - break; - } - - trap = NJS_TRAP_NUMBER_ARG; - goto trap; - - case NJS_FUNCTION_ARG: - - switch (args->type) { - case NJS_STRING: - case NJS_FUNCTION: - break; - - default: - trap = NJS_TRAP_STRING_ARG; - goto trap; - } - - break; - - case NJS_REGEXP_ARG: - - switch (args->type) { - case NJS_VOID: - case NJS_STRING: - case NJS_REGEXP: - break; - - default: - trap = NJS_TRAP_STRING_ARG; - goto trap; - } - - break; - - case NJS_DATE_ARG: - if (!njs_is_date(args)) { - goto type_error; - } - - break; - - case NJS_OBJECT_ARG: - - if (njs_is_null_or_void(args)) { - goto type_error; - } - - break; - - case NJS_SKIP_ARG: - break; - - case 0: - return NJS_OK; - } - - args++; - args_types++; - n--; - } - - return NJS_OK; - -trap: - - njs_vm_trap_value(vm, args); - - return njs_trap(vm, trap); - -type_error: - - njs_type_error(vm, "cannot convert %s to %s", njs_type_string(args->type), - njs_arg_type_string(*args_types)); - - return NXT_ERROR; -} - - const char * njs_type_string(njs_value_type_t type) { @@ -2433,19 +2295,12 @@ njs_vmcode_continuation(njs_vm_t *vm, njs_value_t *invld1, njs_value_t *invld2) njs_native_frame_t *frame; njs_continuation_t *cont; - cont = njs_vm_continuation(vm); frame = vm->top_frame; - - if (cont->args_types != NULL) { - ret = njs_normalize_args(vm, frame->arguments, cont->args_types, - frame->nargs); - if (ret != NJS_OK) { - return ret; - } - } + cont = njs_vm_continuation(vm); ret = njs_function_native_call(vm, cont->function, frame->arguments, - frame->nargs, cont->retval); + cont->args_types, frame->nargs, + cont->retval); switch (ret) { case NXT_OK: diff --git a/njs/njs_vm.h b/njs/njs_vm.h index 50d2e9a3..0e0fb5d5 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -1262,8 +1262,6 @@ njs_ret_t njs_vmcode_finally(njs_vm_t *vm, njs_value_t *invld, nxt_bool_t njs_values_strict_equal(const njs_value_t *val1, const njs_value_t *val2); -njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args, - uint8_t *args_types, nxt_uint_t nargs); const char *njs_type_string(njs_value_type_t type); const char *njs_arg_type_string(uint8_t arg);