From: Alexander Borisov Date: Mon, 15 Apr 2019 14:23:21 +0000 (+0300) Subject: Fixed ToInteger function in accordance with the specification. X-Git-Tag: 0.3.1~2 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=c9bf47ec6bc7f09e2cf878ae1449479e88535504;p=njs.git Fixed ToInteger function in accordance with the specification. According to ES6 type conversion 7.1.4. --- diff --git a/njs/njs_array.c b/njs/njs_array.c index 635561b4..841e43ee 100644 --- a/njs/njs_array.c +++ b/njs/njs_array.c @@ -493,7 +493,7 @@ njs_array_prototype_slice_continuation(njs_vm_t *vm, njs_value_t *args, return njs_trap(vm, NJS_TRAP_NUMBER_ARG); } - start = (int32_t) njs_primitive_value_to_integer(njs_arg(args, nargs, 1)); + start = njs_primitive_value_to_integer(njs_arg(args, nargs, 1)); length = njs_primitive_value_to_integer(&slice->length); if (start < 0) { @@ -510,7 +510,7 @@ njs_array_prototype_slice_continuation(njs_vm_t *vm, njs_value_t *args, } else { if (!njs_is_undefined(njs_arg(args, nargs, 2))) { - end = (int32_t) njs_primitive_value_to_integer(&args[2]); + end = njs_primitive_value_to_integer(&args[2]); } else { end = length; diff --git a/njs/njs_number.c b/njs/njs_number.c index 3a89c4db..4a1f8b72 100644 --- a/njs/njs_number.c +++ b/njs/njs_number.c @@ -73,7 +73,7 @@ njs_primitive_value_to_number(const njs_value_t *value) } -uint32_t +int32_t njs_primitive_value_to_integer(const njs_value_t *value) { return njs_number_to_integer(njs_primitive_value_to_number(value)); @@ -783,11 +783,9 @@ njs_number_parse_float(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } -nxt_noinline uint32_t -njs_number_to_integer(double num) +nxt_inline int64_t +njs_number_to_int64(double num) { - int64_t i64; - #if (NXT_NAN_TO_UINT_CONVERSION != 0) /* * PPC32: NaN and Inf are converted to 0x8000000080000000 @@ -810,13 +808,17 @@ njs_number_to_integer(double num) */ if (fabs(num) > 9007199254740992.0) { - i64 = fmod(num, 4294967296.0); - - } else { - i64 = num; + return (int64_t) fmod(num, 4294967296.0); } - return (uint32_t) i64; + return (int64_t) num; +} + + +nxt_noinline int32_t +njs_number_to_integer(double num) +{ + return (int32_t) njs_number_to_int64(num); } diff --git a/njs/njs_number.h b/njs/njs_number.h index d3b699d9..767a5a39 100644 --- a/njs/njs_number.h +++ b/njs/njs_number.h @@ -13,7 +13,7 @@ uint32_t njs_value_to_index(const njs_value_t *value); double njs_primitive_value_to_number(const njs_value_t *value); -uint32_t njs_primitive_value_to_integer(const njs_value_t *value); +int32_t njs_primitive_value_to_integer(const njs_value_t *value); double njs_number_dec_parse(const u_char **start, const u_char *end); uint64_t njs_number_oct_parse(const u_char **start, const u_char *end); uint64_t njs_number_bin_parse(const u_char **start, const u_char *end); @@ -32,7 +32,7 @@ njs_ret_t njs_number_parse_int(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused); njs_ret_t njs_number_parse_float(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused); -nxt_noinline uint32_t njs_number_to_integer(double num); +nxt_noinline int32_t njs_number_to_integer(double num); nxt_inline nxt_int_t