#include <njs_object.h>
#include <njs_function.h>
#include <math.h>
+#include <errno.h>
static njs_ret_t
}
+static njs_ret_t
+njs_object_math_hypot(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ double num;
+ nxt_uint_t i;
+
+ for (i = 1; i < nargs; i++) {
+ if (!njs_is_numeric(&args[i])) {
+ vm->frame->trap_scratch.data.u.value = &args[i];
+
+ return NJS_TRAP_NUMBER_ARG;
+ }
+ }
+
+ num = (nargs > 1) ? fabs(args[1].data.u.number) : 0;
+
+ for (i = 2; i < nargs; i++) {
+ num = hypot(num, args[i].data.u.number);
+
+ if (num == HUGE_VAL) {
+ /* HUGE_VAL is equal to INFINITY on IEEE-754 systems. */
+
+ if (nxt_slow_path(errno == ERANGE)) {
+ vm->exception = &njs_exception_range_error;
+ return NXT_ERROR;
+ }
+
+ break;
+ }
+ }
+
+ njs_number_set(&vm->retval, num);
+
+ return NXT_OK;
+}
+
+
static njs_ret_t
njs_object_math_log(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
NJS_SKIP_ARG, NJS_NUMBER_ARG),
},
+ /* ES6. */
+ {
+ .type = NJS_METHOD,
+ .name = njs_string("hypot"),
+ .value = njs_native_function(njs_object_math_hypot, 0, 0),
+ },
+
{
.type = NJS_METHOD,
.name = njs_string("log"),
{ nxt_string("Math.abs('abc')"),
nxt_string("NaN") },
+ { nxt_string("Math.hypot()"),
+ nxt_string("0") },
+
+ { nxt_string("Math.hypot(1, 2, 'abc')"),
+ nxt_string("NaN") },
+
+ { nxt_string("Math.hypot(1, NaN, 3)"),
+ nxt_string("NaN") },
+
+ { nxt_string("Math.hypot(1, NaN, -Infinity)"),
+ nxt_string("Infinity") },
+
+ { nxt_string("Math.hypot(-42)"),
+ nxt_string("42") },
+
+ { nxt_string("Math.hypot(8, -15)"),
+ nxt_string("17") },
+
+ { nxt_string("Math.hypot(3, -4, 12.0, '84', 132)"),
+ nxt_string("157") },
+
{ nxt_string("Math.max()"),
nxt_string("-Infinity") },