details: http://hg.nginx.org/njs/rev/4f4bda866d8e branches: changeset: 262:4f4bda866d8e user: Valentin Bartenev <vb...@nginx.com> date: Fri Nov 18 17:25:25 2016 +0300 description: More Math methods from ES6.
diffstat: njs/njs_math.c | 333 +++++++++++++++++++++++++++++++++++++++++++++++ njs/test/njs_unit_test.c | 292 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 625 insertions(+), 0 deletions(-) diffs (820 lines): diff -r f402a8c64d7a -r 4f4bda866d8e njs/njs_math.c --- a/njs/njs_math.c Wed Nov 16 15:21:07 2016 +0300 +++ b/njs/njs_math.c Fri Nov 18 17:25:25 2016 +0300 @@ -69,6 +69,25 @@ njs_object_math_acos(njs_vm_t *vm, njs_v static njs_ret_t +njs_object_math_acosh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = acosh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_asin(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -97,6 +116,25 @@ njs_object_math_asin(njs_vm_t *vm, njs_v static njs_ret_t +njs_object_math_asinh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = asinh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_atan(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -138,6 +176,44 @@ njs_object_math_atan2(njs_vm_t *vm, njs_ static njs_ret_t +njs_object_math_atanh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = atanh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t +njs_object_math_cbrt(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = cbrt(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_ceil(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -176,6 +252,25 @@ njs_object_math_cos(njs_vm_t *vm, njs_va static njs_ret_t +njs_object_math_cosh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = cosh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_exp(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -195,6 +290,25 @@ njs_object_math_exp(njs_vm_t *vm, njs_va static njs_ret_t +njs_object_math_expm1(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = expm1(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_floor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -214,6 +328,25 @@ njs_object_math_floor(njs_vm_t *vm, njs_ static njs_ret_t +njs_object_math_fround(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = (float) args[1].data.u.number; + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_hypot(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -264,6 +397,72 @@ njs_object_math_log(njs_vm_t *vm, njs_va static njs_ret_t +njs_object_math_log10(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = log10(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t +njs_object_math_log1p(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = log1p(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t +njs_object_math_log2(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = args[1].data.u.number; + +#if (NXT_SOLARIS) + /* On Solaris 10 log(-1) returns -Infinity. */ + if (num < 0) { + num = NAN; + } +#endif + + num = log2(num); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_max(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -423,6 +622,25 @@ njs_object_math_sin(njs_vm_t *vm, njs_va static njs_ret_t +njs_object_math_sinh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = sinh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_sqrt(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -461,6 +679,25 @@ njs_object_math_tan(njs_vm_t *vm, njs_va static njs_ret_t +njs_object_math_tanh(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = tanh(args[1].data.u.number); + + } else { + num = NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + +static njs_ret_t njs_object_math_trunc(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) { @@ -549,6 +786,14 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("acosh"), + .value = njs_native_function(njs_object_math_acosh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("asin"), @@ -556,6 +801,14 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("asinh"), + .value = njs_native_function(njs_object_math_asinh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("atan"), @@ -570,6 +823,22 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("atanh"), + .value = njs_native_function(njs_object_math_atanh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("cbrt"), + .value = njs_native_function(njs_object_math_cbrt, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("ceil"), @@ -584,6 +853,14 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("cosh"), + .value = njs_native_function(njs_object_math_cosh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("exp"), @@ -591,6 +868,14 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("expm1"), + .value = njs_native_function(njs_object_math_expm1, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("floor"), @@ -601,6 +886,14 @@ static const njs_object_prop_t njs_math /* ES6. */ { .type = NJS_METHOD, + .name = njs_string("fround"), + .value = njs_native_function(njs_object_math_fround, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + + /* ES6. */ + { + .type = NJS_METHOD, .name = njs_string("hypot"), .value = njs_native_function(njs_object_math_hypot, 0, 0), }, @@ -612,6 +905,30 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("log10"), + .value = njs_native_function(njs_object_math_log10, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("log1p"), + .value = njs_native_function(njs_object_math_log1p, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("log2"), + .value = njs_native_function(njs_object_math_log2, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("max"), @@ -659,6 +976,14 @@ static const njs_object_prop_t njs_math NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("sinh"), + .value = njs_native_function(njs_object_math_sinh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("sqrt"), @@ -676,6 +1001,14 @@ static const njs_object_prop_t njs_math /* ES6. */ { .type = NJS_METHOD, + .name = njs_string("tanh"), + .value = njs_native_function(njs_object_math_tanh, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + + /* ES6. */ + { + .type = NJS_METHOD, .name = njs_string("trunc"), .value = njs_native_function(njs_object_math_trunc, 0, NJS_SKIP_ARG, NJS_NUMBER_ARG), diff -r f402a8c64d7a -r 4f4bda866d8e njs/test/njs_unit_test.c --- a/njs/test/njs_unit_test.c Wed Nov 16 15:21:07 2016 +0300 +++ b/njs/test/njs_unit_test.c Fri Nov 18 17:25:25 2016 +0300 @@ -5549,6 +5549,29 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.acos(0) - Math.PI/2"), nxt_string("0") }, + { nxt_string("Math.acosh()"), + nxt_string("NaN") }, + + { nxt_string("Math.acosh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.acosh(0.9)"), + nxt_string("NaN") }, + + { nxt_string("Math.acosh(1)"), + nxt_string("0") }, + + { nxt_string("Math.acosh('Infinity')"), + nxt_string("Infinity") }, + + /* + * The difference is 2 * Number.EPSILON on FreeBSD + * and zero on other platforms. + */ + { nxt_string("Math.abs(Math.cosh(1) - (1/Math.E + Math.E)/2)" + " <= 2 * Number.EPSILON"), + nxt_string("true") }, + { nxt_string("Math.asin()"), nxt_string("NaN") }, @@ -5573,6 +5596,27 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.asin(1) - Math.PI/2"), nxt_string("0") }, + { nxt_string("Math.asinh()"), + nxt_string("NaN") }, + + { nxt_string("Math.asinh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.asinh(0)"), + nxt_string("0") }, + + { nxt_string("Math.asinh('-0')"), + nxt_string("-0") }, + + { nxt_string("Math.asinh(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.asinh(-Infinity)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.asinh((Math.E - 1/Math.E)/2)"), + nxt_string("1") }, + { nxt_string("Math.atan()"), nxt_string("NaN") }, @@ -5675,6 +5719,62 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.atan2(1, 1) - Math.atan2(-5, -5) - Math.PI"), nxt_string("0") }, + { nxt_string("Math.atanh()"), + nxt_string("NaN") }, + + { nxt_string("Math.atanh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.atanh(-1.1)"), + nxt_string("NaN") }, + + { nxt_string("Math.atanh(1.1)"), + nxt_string("NaN") }, + + { nxt_string("Math.atanh(1)"), + nxt_string("Infinity") }, + + { nxt_string("Math.atanh('-1')"), + nxt_string("-Infinity") }, + + { nxt_string("Math.atanh(0)"), + nxt_string("0") }, + + { nxt_string("Math.atanh(-0)"), + nxt_string("-0") }, + + /* + * The difference is Number.EPSILON on Linux/i686 + * and zero on other platforms. + */ + { nxt_string("Math.abs(1 - Math.atanh((Math.E - 1)/(Math.E + 1)) * 2)" + " <= Number.EPSILON"), + nxt_string("true") }, + + { nxt_string("Math.cbrt()"), + nxt_string("NaN") }, + + { nxt_string("Math.cbrt('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.cbrt(0)"), + nxt_string("0") }, + + { nxt_string("Math.cbrt('-0')"), + nxt_string("-0") }, + + { nxt_string("Math.cbrt(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.cbrt(-Infinity)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.cbrt('27')"), + nxt_string("3") }, + + { nxt_string("Math.cbrt(-1)"), + nxt_string("-1") }, + { nxt_string("Math.ceil()"), nxt_string("NaN") }, @@ -5720,6 +5820,32 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.cos(Math.PI*2)"), nxt_string("1") }, + { nxt_string("Math.cosh()"), + nxt_string("NaN") }, + + { nxt_string("Math.cosh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.cosh('0')"), + nxt_string("1") }, + + { nxt_string("Math.cosh(-0)"), + nxt_string("1") }, + + { nxt_string("Math.cosh(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.cosh(-Infinity)"), + nxt_string("Infinity") }, + + /* + * The difference is Number.EPSILON on FreeBSD + * and zero on other platforms. + */ + { nxt_string("Math.abs(Math.cosh(1) - (1/Math.E + Math.E)/2)" + " <= Number.EPSILON"), + nxt_string("true") }, + { nxt_string("Math.exp()"), nxt_string("NaN") }, @@ -5745,6 +5871,31 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.exp(1) - Math.E <= 2 * Number.EPSILON"), nxt_string("true") }, + { nxt_string("Math.expm1()"), + nxt_string("NaN") }, + + { nxt_string("Math.expm1('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.expm1('0')"), + nxt_string("0") }, + + { nxt_string("Math.expm1(-0)"), + nxt_string("-0") }, + + { nxt_string("Math.expm1(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.expm1(-Infinity)"), + nxt_string("-1") }, + + /* + * The difference is 2 * Number.EPSILON on FreeBSD, Solaris, + * and MacOSX and zero on other platforms. + */ + { nxt_string("Math.abs(1 + Math.expm1(1) - Math.E) <= 2 * Number.EPSILON"), + nxt_string("true") }, + { nxt_string("Math.floor()"), nxt_string("NaN") }, @@ -5769,6 +5920,33 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.floor(-3.1)"), nxt_string("-4") }, + { nxt_string("Math.fround()"), + nxt_string("NaN") }, + + { nxt_string("Math.fround('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.fround(0)"), + nxt_string("0") }, + + { nxt_string("Math.fround('-0')"), + nxt_string("-0") }, + + { nxt_string("Math.fround('Infinity')"), + nxt_string("Infinity") }, + + { nxt_string("Math.fround(-Infinity)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.fround('-1.5')"), + nxt_string("-1.5") }, + + { nxt_string("Math.fround(16777216)"), + nxt_string("16777216") }, + + { nxt_string("Math.fround(-16777217)"), + nxt_string("-16777216") }, + { nxt_string("Math.hypot()"), nxt_string("0") }, @@ -5814,6 +5992,78 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.log(Math.E)"), nxt_string("1") }, + { nxt_string("Math.log10()"), + nxt_string("NaN") }, + + { nxt_string("Math.log10('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.log10(-1)"), + nxt_string("NaN") }, + + { nxt_string("Math.log10(0)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.log10('-0')"), + nxt_string("-Infinity") }, + + { nxt_string("Math.log10(1)"), + nxt_string("0") }, + + { nxt_string("Math.log10(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.log10(1000)"), + nxt_string("3") }, + + { nxt_string("Math.log1p()"), + nxt_string("NaN") }, + + { nxt_string("Math.log1p('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.log1p(-2)"), + nxt_string("NaN") }, + + { nxt_string("Math.log1p('-1')"), + nxt_string("-Infinity") }, + + { nxt_string("Math.log1p(0)"), + nxt_string("0") }, + + { nxt_string("Math.log1p(-0)"), + nxt_string("-0") }, + + { nxt_string("Math.log1p(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.log1p(Math.E - 1)"), + nxt_string("1") }, + + { nxt_string("Math.log2()"), + nxt_string("NaN") }, + + { nxt_string("Math.log2('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.log2(-1)"), + nxt_string("NaN") }, + + { nxt_string("Math.log2(0)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.log2('-0')"), + nxt_string("-Infinity") }, + + { nxt_string("Math.log2(1)"), + nxt_string("0") }, + + { nxt_string("Math.log2(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.log2(128)"), + nxt_string("7") }, + { nxt_string("Math.max()"), nxt_string("-Infinity") }, @@ -5919,6 +6169,27 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.sin(-Math.PI/2)"), nxt_string("-1") }, + { nxt_string("Math.sinh()"), + nxt_string("NaN") }, + + { nxt_string("Math.sinh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.sinh('0')"), + nxt_string("0") }, + + { nxt_string("Math.sinh(-0)"), + nxt_string("-0") }, + + { nxt_string("Math.sinh(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.sinh(-Infinity)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.sinh(1) - (Math.E - 1/Math.E)/2"), + nxt_string("0") }, + { nxt_string("Math.sqrt()"), nxt_string("NaN") }, @@ -5961,6 +6232,27 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.tan(Math.PI/3) + Math.tan(-Math.PI/3)"), nxt_string("0") }, + { nxt_string("Math.tanh()"), + nxt_string("NaN") }, + + { nxt_string("Math.tanh('abc')"), + nxt_string("NaN") }, + + { nxt_string("Math.tanh('0')"), + nxt_string("0") }, + + { nxt_string("Math.tanh(-0)"), + nxt_string("-0") }, + + { nxt_string("Math.tanh(Infinity)"), + nxt_string("1") }, + + { nxt_string("Math.tanh(-Infinity)"), + nxt_string("-1") }, + + { nxt_string("Math.tanh(0.5) - (Math.E - 1)/(Math.E + 1)"), + nxt_string("0") }, + { nxt_string("Math.trunc(3.9)"), nxt_string("3") }, _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx-devel