[njs] Simplified code using magic arguments introduced in 6df48738a043.

Dmitry Volyntsev xeioex at nginx.com
Mon Feb 17 14:01:47 UTC 2020


details:   https://hg.nginx.org/njs/rev/db3a66bd71c1
branches:  
changeset: 1328:db3a66bd71c1
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Feb 17 16:18:38 2020 +0300
description:
Simplified code using magic arguments introduced in 6df48738a043.

This reduces code duplication and the binary size.

diffstat:

 src/njs_error.c  |    92 +---
 src/njs_math.c   |  1136 ++++++++++++-----------------------------------------
 src/njs_string.c |   118 +----
 3 files changed, 306 insertions(+), 1040 deletions(-)

diffs (truncated from 1853 to 1000 lines):

diff -r cbc09d7edfc8 -r db3a66bd71c1 src/njs_error.c
--- a/src/njs_error.c	Mon Feb 17 16:18:18 2020 +0300
+++ b/src/njs_error.c	Mon Feb 17 16:18:38 2020 +0300
@@ -257,8 +257,8 @@ memory_error:
 
 
 static njs_int_t
-njs_error_create(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_object_type_t type)
+njs_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+    njs_index_t type)
 {
     njs_int_t     ret;
     njs_value_t   *value;
@@ -287,14 +287,6 @@ njs_error_create(njs_vm_t *vm, njs_value
 }
 
 
-static njs_int_t
-njs_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_error_constructor_properties[] =
 {
     {
@@ -325,14 +317,6 @@ const njs_object_init_t  njs_error_const
 };
 
 
-static njs_int_t
-njs_eval_error_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_EVAL_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_eval_error_constructor_properties[] =
 {
     {
@@ -363,14 +347,6 @@ const njs_object_init_t  njs_eval_error_
 };
 
 
-static njs_int_t
-njs_internal_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_INTERNAL_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_internal_error_constructor_properties[] =
 {
     {
@@ -401,14 +377,6 @@ const njs_object_init_t  njs_internal_er
 };
 
 
-static njs_int_t
-njs_range_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_RANGE_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_range_error_constructor_properties[] =
 {
     {
@@ -439,14 +407,6 @@ const njs_object_init_t  njs_range_error
 };
 
 
-static njs_int_t
-njs_reference_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_REF_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_reference_error_constructor_properties[] =
 {
     {
@@ -477,14 +437,6 @@ const njs_object_init_t  njs_reference_e
 };
 
 
-static njs_int_t
-njs_syntax_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_SYNTAX_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_syntax_error_constructor_properties[] =
 {
     {
@@ -515,14 +467,6 @@ const njs_object_init_t  njs_syntax_erro
 };
 
 
-static njs_int_t
-njs_type_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_TYPE_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_type_error_constructor_properties[] =
 {
     {
@@ -553,14 +497,6 @@ const njs_object_init_t  njs_type_error_
 };
 
 
-static njs_int_t
-njs_uri_error_constructor(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused)
-{
-    return njs_error_create(vm, args, nargs, NJS_OBJ_TYPE_URI_ERROR);
-}
-
-
 static const njs_object_prop_t  njs_uri_error_constructor_properties[] =
 {
     {
@@ -874,7 +810,8 @@ const njs_object_init_t  njs_error_proto
 
 
 const njs_object_type_init_t  njs_error_type_init = {
-    .constructor = njs_native_ctor(njs_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_ERROR),
     .constructor_props = &njs_error_constructor_init,
     .prototype_props = &njs_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -916,7 +853,8 @@ const njs_object_init_t  njs_eval_error_
 
 
 const njs_object_type_init_t  njs_eval_error_type_init = {
-    .constructor = njs_native_ctor(njs_eval_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_EVAL_ERROR),
     .constructor_props = &njs_eval_error_constructor_init,
     .prototype_props = &njs_eval_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -978,7 +916,8 @@ const njs_object_init_t  njs_internal_er
 
 
 const njs_object_type_init_t  njs_internal_error_type_init = {
-    .constructor = njs_native_ctor(njs_internal_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_INTERNAL_ERROR),
     .constructor_props = &njs_internal_error_constructor_init,
     .prototype_props = &njs_internal_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -1028,7 +967,8 @@ const njs_object_init_t  njs_range_error
 
 
 const njs_object_type_init_t  njs_range_error_type_init = {
-    .constructor = njs_native_ctor(njs_range_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_RANGE_ERROR),
     .constructor_props = &njs_range_error_constructor_init,
     .prototype_props = &njs_range_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -1070,7 +1010,8 @@ const njs_object_init_t  njs_reference_e
 
 
 const njs_object_type_init_t  njs_reference_error_type_init = {
-    .constructor = njs_native_ctor(njs_reference_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_REF_ERROR),
     .constructor_props = &njs_reference_error_constructor_init,
     .prototype_props = &njs_reference_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -1112,7 +1053,8 @@ const njs_object_init_t  njs_syntax_erro
 
 
 const njs_object_type_init_t  njs_syntax_error_type_init = {
-    .constructor = njs_native_ctor(njs_syntax_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_SYNTAX_ERROR),
     .constructor_props = &njs_syntax_error_constructor_init,
     .prototype_props = &njs_syntax_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -1154,7 +1096,8 @@ const njs_object_init_t  njs_type_error_
 
 
 const njs_object_type_init_t  njs_type_error_type_init = {
-    .constructor = njs_native_ctor(njs_type_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_TYPE_ERROR),
     .constructor_props = &njs_type_error_constructor_init,
     .prototype_props = &njs_type_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
@@ -1196,7 +1139,8 @@ const njs_object_init_t  njs_uri_error_p
 
 
 const njs_object_type_init_t  njs_uri_error_type_init = {
-    .constructor = njs_native_ctor(njs_uri_error_constructor, 1, 0),
+    .constructor = njs_native_ctor(njs_error_constructor, 1,
+                                   NJS_OBJ_TYPE_URI_ERROR),
     .constructor_props = &njs_uri_error_constructor_init,
     .prototype_props = &njs_uri_error_prototype_init,
     .prototype_value = { .object = { .type = NJS_OBJECT } },
diff -r cbc09d7edfc8 -r db3a66bd71c1 src/njs_math.c
--- a/src/njs_math.c	Mon Feb 17 16:18:18 2020 +0300
+++ b/src/njs_math.c	Mon Feb 17 16:18:38 2020 +0300
@@ -8,444 +8,267 @@
 #include <njs_main.h>
 
 
-static njs_int_t
-njs_object_math_abs(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, fabs(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_acos(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    double     num;
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    num = njs_number(&args[1]);
-
-#if (NJS_SOLARIS)
-    /* On Solaris acos(x) returns 0 for x > 1. */
-    if (fabs(num) > 1.0) {
-        num = NAN;
-    }
-#endif
-
-    njs_set_number(&vm->retval, acos(num));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_acosh(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, acosh(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_asin(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    double     num;
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    num = njs_number(&args[1]);
-
-#if (NJS_SOLARIS)
-    /* On Solaris asin(x) returns 0 for x > 1. */
-    if (fabs(num) > 1.0) {
-        num = NAN;
-    }
-#endif
-
-    njs_set_number(&vm->retval, asin(num));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_asinh(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, asinh(njs_number(&args[1])));
-
-    return NJS_OK;
-}
+typedef enum {
+    NJS_MATH_ABS,
+    NJS_MATH_ACOS,
+    NJS_MATH_ACOSH,
+    NJS_MATH_ASIN,
+    NJS_MATH_ASINH,
+    NJS_MATH_ATAN,
+    NJS_MATH_ATAN2,
+    NJS_MATH_ATANH,
+    NJS_MATH_CBRT,
+    NJS_MATH_CEIL,
+    NJS_MATH_CLZ32,
+    NJS_MATH_COS,
+    NJS_MATH_COSH,
+    NJS_MATH_EXP,
+    NJS_MATH_EXPM1,
+    NJS_MATH_FLOOR,
+    NJS_MATH_FROUND,
+    NJS_MATH_IMUL,
+    NJS_MATH_LOG,
+    NJS_MATH_LOG10,
+    NJS_MATH_LOG1P,
+    NJS_MATH_LOG2,
+    NJS_MATH_POW,
+    NJS_MATH_ROUND,
+    NJS_MATH_SIGN,
+    NJS_MATH_SIN,
+    NJS_MATH_SINH,
+    NJS_MATH_SQRT,
+    NJS_MATH_TAN,
+    NJS_MATH_TANH,
+    NJS_MATH_TRUNC,
+} njs_math_func_t;
 
 
 static njs_int_t
-njs_object_math_atan(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
+njs_object_math_func(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+    njs_index_t magic)
 {
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, atan(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_atan2(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    double     y, x;
-    njs_int_t  ret;
+    double            num, num2;
+    uint8_t           sign;
+    uint32_t          u32;
+    uint64_t          one, fraction_mask;
+    njs_int_t         ret, ep;
+    njs_math_func_t   func;
+    njs_diyfp_conv_t  conv;
 
-    if (njs_slow_path(nargs < 3)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[2]))) {
-        ret = njs_value_to_numeric(vm, &args[2], &args[2]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
+    func = magic;
 
-    y = njs_number(&args[1]);
-    x = njs_number(&args[2]);
-
-    njs_set_number(&vm->retval, atan2(y, x));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_atanh(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
+    ret = njs_value_to_number(vm, njs_arg(args, nargs, 1), &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
     }
 
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
+    switch (func) {
+    case NJS_MATH_ABS:
+        num = fabs(num);
+        break;
+
+    case NJS_MATH_ACOS:
+#if (NJS_SOLARIS)
+        /* On Solaris acos(x) returns 0 for x > 1. */
+        if (fabs(num) > 1.0) {
+            num = NAN;
         }
-    }
+#endif
 
-    njs_set_number(&vm->retval, atanh(njs_number(&args[1])));
+        num = acos(num);
+        break;
+
+    case NJS_MATH_ACOSH:
+        num = acosh(num);
+        break;
 
-    return NJS_OK;
-}
+    case NJS_MATH_ASIN:
+#if (NJS_SOLARIS)
+        /* On Solaris asin(x) returns 0 for x > 1. */
+        if (fabs(num) > 1.0) {
+            num = NAN;
+        }
+#endif
+
+        num = asin(num);
+        break;
 
+    case NJS_MATH_ASINH:
+        num = asinh(num);
+        break;
+
+    case NJS_MATH_ATAN:
+        num = atan(num);
+        break;
+
+    case NJS_MATH_ATANH:
+        num = atanh(num);
+        break;
 
-static njs_int_t
-njs_object_math_cbrt(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
+    case NJS_MATH_CBRT:
+        num = cbrt(num);
+        break;
+
+    case NJS_MATH_CEIL:
+        num = ceil(num);
+        break;
 
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
+    case NJS_MATH_CLZ32:
+        u32 = njs_number_to_uint32(num);
+        num = njs_leading_zeros(u32);
+        break;
+
+    case NJS_MATH_COS:
+        num = cos(num);
+        break;
+
+    case NJS_MATH_COSH:
+        num = cosh(num);
+        break;
 
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
+    case NJS_MATH_EXP:
+        num = exp(num);
+        break;
+
+    case NJS_MATH_EXPM1:
+        num = expm1(num);
+        break;
+
+    case NJS_MATH_FLOOR:
+        num = floor(num);
+        break;
 
-    njs_set_number(&vm->retval, cbrt(njs_number(&args[1])));
+    case NJS_MATH_FROUND:
+        num = (float) num;
+        break;
 
-    return NJS_OK;
-}
+    case NJS_MATH_LOG:
+        num = log(num);
+        break;
 
+    case NJS_MATH_LOG10:
+        num = log10(num);
+        break;
 
-static njs_int_t
-njs_object_math_ceil(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
+    case NJS_MATH_LOG1P:
+        num = log1p(num);
+        break;
+
+    case NJS_MATH_LOG2:
+#if (NJS_SOLARIS)
+        /* On Solaris 10 log(-1) returns -Infinity. */
+        if (num < 0) {
+            num = NAN;
+        }
+#endif
+
+        num = log2(num);
+        break;
+
+    case NJS_MATH_SIGN:
+        if (!isnan(num) && num != 0) {
+            num = signbit(num) ? -1 : 1;
+        }
 
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
+        break;
+
+    case NJS_MATH_SIN:
+        num = sin(num);
+        break;
+
+    case NJS_MATH_SINH:
+        num = sinh(num);
+        break;
 
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
+    case NJS_MATH_SQRT:
+        num = sqrt(num);
+        break;
+
+    case NJS_MATH_ROUND:
+        conv.d = num;
+        ep = (conv.u64 & NJS_DBL_EXPONENT_MASK) >> NJS_DBL_SIGNIFICAND_SIZE;
+
+        if (ep < NJS_DBL_EXPONENT_OFFSET) {
+
+            /* |v| < 1. */
 
-    njs_set_number(&vm->retval, ceil(njs_number(&args[1])));
+            if (ep == (NJS_DBL_EXPONENT_OFFSET - 1)
+                && conv.u64 != njs_uint64(0xbfe00000, 0x00000000))
+            {
+                /* (|v| > 0.5 || v == 0.5) => +-1.0 */
+
+                conv.u64 = conv.u64 & NJS_DBL_SIGN_MASK;
+                conv.u64 |= NJS_DBL_EXPONENT_OFFSET << NJS_DBL_SIGNIFICAND_SIZE;
+
+            } else {
 
-    return NJS_OK;
-}
+                /* (|v| < 0.5 || v == -0.5) => +-0. */
+
+                conv.u64 &= ((uint64_t) 1) << 63;
+            }
 
+        } else if (ep < NJS_DBL_EXPONENT_BIAS) {
+
+            /* |v| <= 2^52 - 1 (largest safe integer). */
+
+            one = ((uint64_t) 1) << (NJS_DBL_EXPONENT_BIAS - ep);
+            fraction_mask = one - 1;
 
-static njs_int_t
-njs_object_math_clz32(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    uint32_t   ui32;
-    njs_int_t  ret;
+            /* truncation. */
+
+            sign = conv.u64 >> 63;
+            conv.u64 += (one >> 1) - sign;
+            conv.u64 &= ~fraction_mask;
+        }
+
+        num = conv.d;
+        break;
 
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, 32);
-        return NJS_OK;
-    }
+    case NJS_MATH_TAN:
+        num = tan(num);
+        break;
 
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_uint32(vm, &args[1], &ui32);
+    case NJS_MATH_TANH:
+        num = tanh(num);
+        break;
+
+    case NJS_MATH_TRUNC:
+        num = trunc(num);
+        break;
+
+    default:
+        ret = njs_value_to_number(vm, njs_arg(args, nargs, 2), &num2);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
         }
 
-    } else {
-        ui32 = njs_number_to_uint32(njs_number(&args[1]));
-    }
-
-    njs_set_number(&vm->retval, njs_leading_zeros(ui32));
-
-    return NJS_OK;
-}
-
+        switch (func) {
+        case NJS_MATH_ATAN2:
+            num = atan2(num, num2);
+            break;
 
-static njs_int_t
-njs_object_math_cos(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
+        case NJS_MATH_IMUL:
+            u32 = njs_number_to_uint32(num);
+            num = (int32_t) (u32 * njs_number_to_uint32(num2));
+            break;
 
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, cos(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
+        default:
+            /*
+             * According to ECMA-262:
+             *  1. If exponent is NaN, the result should be NaN;
+             *  2. The result of Math.pow(+/-1, +/-Infinity) should be NaN.
+             */
 
-static njs_int_t
-njs_object_math_cosh(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
+            if (fabs(num) != 1 || (!isnan(num2) && !isinf(num2))) {
+                num = pow(num, num2);
 
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
+            } else {
+                num = NAN;
+            }
         }
     }
 
-    njs_set_number(&vm->retval, cosh(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_exp(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, exp(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_expm1(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, expm1(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_floor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, floor(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_fround(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, (float) njs_number(&args[1]));
+    njs_set_number(&vm->retval, num);
 
     return NJS_OK;
 }
@@ -459,21 +282,22 @@ njs_object_math_hypot(njs_vm_t *vm, njs_
     njs_int_t   ret;
     njs_uint_t  i;
 
-    for (i = 1; i < nargs; i++) {
-        if (!njs_is_numeric(&args[i])) {
-            ret = njs_value_to_numeric(vm, &args[i], &args[i]);
-            if (ret != NJS_OK) {
-                return ret;
-            }
-        }
+    ret = njs_value_to_number(vm, njs_arg(args, nargs, 1), &num);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
     }
 
-    num = (nargs > 1) ? fabs(njs_number(&args[1])) : 0;
+    num = (nargs > 1) ? fabs(num) : 0;
 
     for (i = 2; i < nargs; i++) {
+        ret = njs_value_to_numeric(vm, &args[i], &args[i]);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return ret;
+        }
+
         num = hypot(num, njs_number(&args[i]));
 
-        if (num == INFINITY) {
+        if (njs_slow_path(isinf(num))) {
             break;
         }
     }
@@ -484,150 +308,6 @@ njs_object_math_hypot(njs_vm_t *vm, njs_
 }
 
 
-static njs_int_t
-njs_object_math_imul(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    uint32_t   a, b;
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 3)) {
-        njs_set_number(&vm->retval, 0);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_uint32(vm, &args[1], &a);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-
-    } else {
-        a = njs_number_to_uint32(njs_number(&args[1]));
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[2]))) {
-        ret = njs_value_to_uint32(vm, &args[2], &b);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-
-    } else {
-        b = njs_number_to_uint32(njs_number(&args[2]));
-    }
-
-    njs_set_number(&vm->retval, (int32_t) (a * b));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }
-
-    njs_set_number(&vm->retval, log(njs_number(&args[1])));
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-njs_object_math_log10(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused)
-{
-    njs_int_t  ret;
-
-    if (njs_slow_path(nargs < 2)) {
-        njs_set_number(&vm->retval, NAN);
-        return NJS_OK;
-    }
-
-    if (njs_slow_path(!njs_is_number(&args[1]))) {
-        ret = njs_value_to_numeric(vm, &args[1], &args[1]);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return ret;
-        }
-    }


More information about the nginx-devel mailing list