[njs] Using njs_number() and njs_set_number() everywhere.

Dmitry Volyntsev xeioex at nginx.com
Mon Jul 8 14:52:34 UTC 2019


details:   https://hg.nginx.org/njs/rev/254c9b04b0d2
branches:  
changeset: 1033:254c9b04b0d2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jul 08 17:49:14 2019 +0300
description:
Using njs_number() and njs_set_number() everywhere.

diffstat:

 njs/njs.c                   |   4 +-
 njs/njs_array.c             |  20 +++++-----
 njs/njs_builtin.c           |   8 +---
 njs/njs_error.c             |   1 -
 njs/njs_fs.c                |   6 +--
 njs/njs_function.c          |  12 +++---
 njs/njs_json.c              |  17 +++-----
 njs/njs_math.c              |  80 ++++++++++++++++++++++----------------------
 njs/njs_number.c            |  22 ++++++------
 njs/njs_number.h            |   2 +-
 njs/njs_parser_expression.c |   5 +-
 njs/njs_parser_terminal.c   |   8 +---
 njs/njs_string.c            |  36 ++++++++++----------
 njs/njs_time.c              |   4 +-
 njs/njs_value.c             |   2 +-
 njs/njs_vm.c                |  76 +++++++++++++++++++++---------------------
 16 files changed, 144 insertions(+), 159 deletions(-)

diffs (truncated from 1200 to 1000 lines):

diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs.c
--- a/njs/njs.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs.c	Mon Jul 08 17:49:14 2019 +0300
@@ -805,8 +805,8 @@ nxt_int_t
 njs_value_is_valid_number(const njs_value_t *value)
 {
     return njs_is_number(value)
-           && !isnan(value->data.u.number)
-           && !isinf(value->data.u.number);
+           && !isnan(njs_number(value))
+           && !isinf(njs_number(value));
 }
 
 
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_array.c
--- a/njs/njs_array.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_array.c	Mon Jul 08 17:49:14 2019 +0300
@@ -293,7 +293,7 @@ njs_array_constructor(njs_vm_t *vm, njs_
     size = nargs - 1;
 
     if (size == 1 && njs_is_number(&args[0])) {
-        num = args[0].data.u.number;
+        num = njs_number(&args[0]);
         size = (uint32_t) num;
 
         if ((double) size != num) {
@@ -474,7 +474,7 @@ njs_array_length(njs_vm_t *vm, njs_value
         return NJS_ERROR;
     }
 
-    num = setval->data.u.number;
+    num = njs_number(setval);
     length = (uint32_t) num;
 
     if ((double) length != num) {
@@ -843,7 +843,7 @@ njs_array_prototype_splice(njs_vm_t *vm,
         length = array->length;
 
         if (nargs > 1) {
-            start = args[1].data.u.number;
+            start = njs_number(&args[1]);
 
             if (start < 0) {
                 start += length;
@@ -859,7 +859,7 @@ njs_array_prototype_splice(njs_vm_t *vm,
             delete = length - start;
 
             if (nargs > 2) {
-                n = args[2].data.u.number;
+                n = njs_number(&args[2]);
 
                 if (n < 0) {
                     delete = 0;
@@ -1240,7 +1240,7 @@ njs_array_prototype_index_of(njs_vm_t *v
     i = 0;
 
     if (nargs > 2) {
-        i = args[2].data.u.number;
+        i = njs_number(&args[2]);
 
         if (i >= length) {
             goto done;
@@ -1300,7 +1300,7 @@ njs_array_prototype_last_index_of(njs_vm
     i = length - 1;
 
     if (nargs > 2) {
-        n = args[2].data.u.number;
+        n = njs_number(&args[2]);
 
         if (n < 0) {
             i = n + length;
@@ -1360,7 +1360,7 @@ njs_array_prototype_includes(njs_vm_t *v
     i = 0;
 
     if (nargs > 2) {
-        i = args[2].data.u.number;
+        i = njs_number(&args[2]);
 
         if (i >= length) {
             goto done;
@@ -1378,12 +1378,12 @@ njs_array_prototype_includes(njs_vm_t *v
     start = array->start;
     value = &args[1];
 
-    if (njs_is_number(value) && isnan(value->data.u.number)) {
+    if (njs_is_number(value) && isnan(njs_number(value))) {
 
         do {
             value = &start[i];
 
-            if (njs_is_number(value) && isnan(value->data.u.number)) {
+            if (njs_is_number(value) && isnan(njs_number(value))) {
                 retval = &njs_value_true;
                 break;
             }
@@ -2276,7 +2276,7 @@ njs_array_prototype_sort_continuation(nj
          * "goto next" moves control to the appropriate step of the algorithm.
          * The first iteration also goes there because sort->retval is zero.
          */
-        if (sort->retval.data.u.number <= 0) {
+        if (njs_number(&sort->retval) <= 0) {
             goto next;
         }
 
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_builtin.c	Mon Jul 08 17:49:14 2019 +0300
@@ -1305,9 +1305,7 @@ static njs_ret_t
 njs_process_object_pid(njs_vm_t *vm, njs_value_t *unused,
     njs_value_t *unused2, njs_value_t *retval)
 {
-    retval->data.u.number = getpid();
-    retval->type = NJS_NUMBER;
-    retval->data.truth = njs_is_number_true(retval->data.u.number);
+    njs_set_number(retval, getpid());
 
     return NJS_OK;
 }
@@ -1317,9 +1315,7 @@ static njs_ret_t
 njs_process_object_ppid(njs_vm_t *vm, njs_value_t *unused,
     njs_value_t *unused2, njs_value_t *retval)
 {
-    retval->data.u.number = getppid();
-    retval->type = NJS_NUMBER;
-    retval->data.truth = njs_is_number_true(retval->data.u.number);
+    njs_set_number(retval, getppid());
 
     return NJS_OK;
 }
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_error.c
--- a/njs/njs_error.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_error.c	Mon Jul 08 17:49:14 2019 +0300
@@ -515,7 +515,6 @@ njs_memory_error_set(njs_vm_t *vm, njs_v
 
     value->data.type = NJS_OBJECT_INTERNAL_ERROR;
     value->data.truth = 1;
-    value->data.u.number = NAN;
     value->data.u.object = object;
 }
 
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_fs.c
--- a/njs/njs_fs.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_fs.c	Mon Jul 08 17:49:14 2019 +0300
@@ -919,9 +919,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *
         lhq.key_hash = NJS_ERRNO_HASH;
         lhq.proto = &njs_object_hash_proto;
 
-        value.data.type = NJS_NUMBER;
-        value.data.truth = 1;
-        value.data.u.number = errn;
+        njs_set_number(&value, errn);
 
         prop = njs_object_prop_alloc(vm, &njs_fs_errno_string, &value, 1);
         if (nxt_slow_path(prop == NULL)) {
@@ -1013,7 +1011,7 @@ njs_fs_mode(njs_value_t *value)
         /* Fall through. */
 
     case NJS_NUMBER:
-        return (mode_t) value->data.u.number;
+        return (mode_t) njs_number(value);
 
     case NJS_OBJECT_STRING:
     value = &value->data.u.object_value->value;
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_function.c
--- a/njs/njs_function.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_function.c	Mon Jul 08 17:49:14 2019 +0300
@@ -677,14 +677,14 @@ njs_normalize_args(njs_vm_t *vm, njs_val
 
                 /* Numbers are truncated to fit in 32-bit integers. */
 
-                if (isnan(args->data.u.number)) {
-                    args->data.u.number = 0;
+                if (isnan(njs_number(args))) {
+                    njs_number(args) = 0;
 
-                } else if (args->data.u.number > 2147483647.0) {
-                    args->data.u.number = 2147483647.0;
+                } else if (njs_number(args) > 2147483647.0) {
+                    njs_number(args) = 2147483647.0;
 
-                } else if (args->data.u.number < -2147483648.0) {
-                    args->data.u.number = -2147483648.0;
+                } else if (njs_number(args) < -2147483648.0) {
+                    njs_number(args) = -2147483648.0;
                 }
 
                 break;
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_json.c
--- a/njs/njs_json.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_json.c	Mon Jul 08 17:49:14 2019 +0300
@@ -305,7 +305,7 @@ njs_json_stringify(njs_vm_t *vm, njs_val
             stringify->space.length = nxt_min(stringify->space.length, 10);
 
         } else {
-            num = space->data.u.number;
+            num = njs_number(space);
             if (!isnan(num) && !isinf(num) && num > 0) {
                 num = nxt_min(num, 10);
 
@@ -872,10 +872,7 @@ njs_json_parse_number(njs_json_parse_ctx
     start = p;
     num = njs_number_dec_parse(&p, ctx->end);
     if (p != start) {
-        value->data.u.number = sign * num;
-        value->type = NJS_NUMBER;
-        value->data.truth = njs_is_number_true(num);
-
+        njs_set_number(value, sign * num);
         return p;
     }
 
@@ -1890,7 +1887,7 @@ njs_json_append_number(njs_json_stringif
     size_t  size;
     double  num;
 
-    num = value->data.u.number;
+    num = njs_number(value);
 
     if (isnan(num) || isinf(num)) {
         return njs_json_buf_append(stringify, "null", 4);
@@ -2160,8 +2157,8 @@ njs_dump_value(njs_json_stringify_t *str
     case NJS_OBJECT_NUMBER:
         value = &value->data.u.object_value->value;
 
-        if (nxt_slow_path(value->data.u.number == 0.0
-                          && signbit(value->data.u.number)))
+        if (nxt_slow_path(njs_number(value) == 0.0
+                          && signbit(njs_number(value))))
         {
 
             njs_dump("[Number: -0]");
@@ -2275,8 +2272,8 @@ njs_dump_value(njs_json_stringify_t *str
         return njs_json_buf_append(stringify, "]}", 2);
 
     case NJS_NUMBER:
-        if (nxt_slow_path(value->data.u.number == 0.0
-                          && signbit(value->data.u.number)))
+        if (nxt_slow_path(njs_number(value) == 0.0
+                          && signbit(njs_number(value))))
         {
 
             njs_dump("-0");
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_math.c
--- a/njs/njs_math.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_math.c	Mon Jul 08 17:49:14 2019 +0300
@@ -15,7 +15,7 @@ njs_object_math_abs(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = fabs(args[1].data.u.number);
+        num = fabs(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -34,7 +34,7 @@ njs_object_math_acos(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
 #if (NXT_SOLARIS)
         /* On Solaris acos(x) returns 0 for x > 1. */
@@ -62,7 +62,7 @@ njs_object_math_acosh(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = acosh(args[1].data.u.number);
+        num = acosh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -81,7 +81,7 @@ njs_object_math_asin(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
 #if (NXT_SOLARIS)
         /* On Solaris asin(x) returns 0 for x > 1. */
@@ -109,7 +109,7 @@ njs_object_math_asinh(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = asinh(args[1].data.u.number);
+        num = asinh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -128,7 +128,7 @@ njs_object_math_atan(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = atan(args[1].data.u.number);
+        num = atan(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -147,8 +147,8 @@ njs_object_math_atan2(njs_vm_t *vm, njs_
     double  num, y, x;
 
     if (nargs > 2) {
-        y = args[1].data.u.number;
-        x = args[2].data.u.number;
+        y = njs_number(&args[1]);
+        x = njs_number(&args[2]);
 
         num = atan2(y, x);
 
@@ -169,7 +169,7 @@ njs_object_math_atanh(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = atanh(args[1].data.u.number);
+        num = atanh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -188,7 +188,7 @@ njs_object_math_cbrt(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = cbrt(args[1].data.u.number);
+        num = cbrt(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -207,7 +207,7 @@ njs_object_math_ceil(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = ceil(args[1].data.u.number);
+        num = ceil(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -227,7 +227,7 @@ njs_object_math_clz32(njs_vm_t *vm, njs_
     uint32_t  ui32;
 
     if (nargs > 1) {
-        ui32 = njs_number_to_uint32(args[1].data.u.number);
+        ui32 = njs_number_to_uint32(njs_number(&args[1]));
         num = nxt_leading_zeros(ui32);
 
     } else {
@@ -247,7 +247,7 @@ njs_object_math_cos(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = cos(args[1].data.u.number);
+        num = cos(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -266,7 +266,7 @@ njs_object_math_cosh(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = cosh(args[1].data.u.number);
+        num = cosh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -285,7 +285,7 @@ njs_object_math_exp(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = exp(args[1].data.u.number);
+        num = exp(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -304,7 +304,7 @@ njs_object_math_expm1(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = expm1(args[1].data.u.number);
+        num = expm1(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -323,7 +323,7 @@ njs_object_math_floor(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = floor(args[1].data.u.number);
+        num = floor(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -342,7 +342,7 @@ njs_object_math_fround(njs_vm_t *vm, njs
     double  num;
 
     if (nargs > 1) {
-        num = (float) args[1].data.u.number;
+        num = (float) njs_number(&args[1]);
 
     } else {
         num = NAN;
@@ -369,10 +369,10 @@ njs_object_math_hypot(njs_vm_t *vm, njs_
         }
     }
 
-    num = (nargs > 1) ? fabs(args[1].data.u.number) : 0;
+    num = (nargs > 1) ? fabs(njs_number(&args[1])) : 0;
 
     for (i = 2; i < nargs; i++) {
-        num = hypot(num, args[i].data.u.number);
+        num = hypot(num, njs_number(&args[i]));
 
         if (num == INFINITY) {
             break;
@@ -393,8 +393,8 @@ njs_object_math_imul(njs_vm_t *vm, njs_v
     uint32_t  a, b;
 
     if (nargs > 2) {
-        a = njs_number_to_uint32(args[1].data.u.number);
-        b = njs_number_to_uint32(args[2].data.u.number);
+        a = njs_number_to_uint32(njs_number(&args[1]));
+        b = njs_number_to_uint32(njs_number(&args[2]));
 
         num = (int32_t) (a * b);
 
@@ -415,7 +415,7 @@ njs_object_math_log(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = log(args[1].data.u.number);
+        num = log(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -434,7 +434,7 @@ njs_object_math_log10(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = log10(args[1].data.u.number);
+        num = log10(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -453,7 +453,7 @@ njs_object_math_log1p(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = log1p(args[1].data.u.number);
+        num = log1p(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -472,7 +472,7 @@ njs_object_math_log2(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
 #if (NXT_SOLARIS)
         /* On Solaris 10 log(-1) returns -Infinity. */
@@ -513,10 +513,10 @@ njs_object_math_max(njs_vm_t *vm, njs_va
             }
         }
 
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         for (i = 2; i < nargs; i++) {
-            num = fmax(num, args[i].data.u.number);
+            num = fmax(num, njs_number(&args[i]));
         }
 
     } else {
@@ -547,10 +547,10 @@ njs_object_math_min(njs_vm_t *vm, njs_va
             }
         }
 
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         for (i = 2; i < nargs; i++) {
-            num = fmin(num, args[i].data.u.number);
+            num = fmin(num, njs_number(&args[i]));
         }
 
     } else {
@@ -570,8 +570,8 @@ njs_object_math_pow(njs_vm_t *vm, njs_va
     double  num, base, exponent;
 
     if (nargs > 2) {
-        base = args[1].data.u.number;
-        exponent = args[2].data.u.number;
+        base = njs_number(&args[1]);
+        exponent = njs_number(&args[2]);
 
         /*
          * According to ECMA-262:
@@ -617,7 +617,7 @@ njs_object_math_round(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = round(args[1].data.u.number);
+        num = round(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -636,7 +636,7 @@ njs_object_math_sign(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         if (!isnan(num) && num != 0) {
             num = signbit(num) ? -1 : 1;
@@ -659,7 +659,7 @@ njs_object_math_sin(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = sin(args[1].data.u.number);
+        num = sin(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -678,7 +678,7 @@ njs_object_math_sinh(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = sinh(args[1].data.u.number);
+        num = sinh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -697,7 +697,7 @@ njs_object_math_sqrt(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = sqrt(args[1].data.u.number);
+        num = sqrt(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -716,7 +716,7 @@ njs_object_math_tan(njs_vm_t *vm, njs_va
     double  num;
 
     if (nargs > 1) {
-        num = tan(args[1].data.u.number);
+        num = tan(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -735,7 +735,7 @@ njs_object_math_tanh(njs_vm_t *vm, njs_v
     double  num;
 
     if (nargs > 1) {
-        num = tanh(args[1].data.u.number);
+        num = tanh(njs_number(&args[1]));
 
     } else {
         num = NAN;
@@ -754,7 +754,7 @@ njs_object_math_trunc(njs_vm_t *vm, njs_
     double  num;
 
     if (nargs > 1) {
-        num = trunc(args[1].data.u.number);
+        num = trunc(njs_number(&args[1]));
 
     } else {
         num = NAN;
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_number.c
--- a/njs/njs_number.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_number.c	Mon Jul 08 17:49:14 2019 +0300
@@ -31,7 +31,7 @@ njs_value_to_index(const njs_value_t *va
     num = NAN;
 
     if (nxt_fast_path(njs_is_numeric(value))) {
-        num = value->data.u.number;
+        num = njs_number(value);
 
     } else if (njs_is_string(value)) {
         num = njs_string_to_index(value);
@@ -212,7 +212,7 @@ njs_number_to_string(njs_vm_t *vm, njs_v
     const njs_value_t  *value;
     u_char             buf[128];
 
-    num = number->data.u.number;
+    num = njs_number(number);
 
     if (isnan(num)) {
         value = &njs_string_nan;
@@ -263,7 +263,7 @@ njs_number_constructor(njs_vm_t *vm, njs
         vm->retval.data.truth = 1;
 
     } else {
-        njs_set_number(&vm->retval, value->data.u.number);
+        njs_set_number(&vm->retval, njs_number(value));
     }
 
     return NXT_OK;
@@ -280,7 +280,7 @@ njs_number_is_integer(njs_vm_t *vm, njs_
     value = &njs_value_false;
 
     if (nargs > 1 && njs_is_number(&args[1])) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         if (num == trunc(num) && !isinf(num)) {
             value = &njs_value_true;
@@ -304,7 +304,7 @@ njs_number_is_safe_integer(njs_vm_t *vm,
     value = &njs_value_false;
 
     if (nargs > 1 && njs_is_number(&args[1])) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         if (num == (int64_t) num && fabs(num) <= NJS_MAX_SAFE_INTEGER) {
             value = &njs_value_true;
@@ -327,7 +327,7 @@ njs_number_is_nan(njs_vm_t *vm, njs_valu
 
     if (nargs > 1
         && njs_is_number(&args[1])
-        && isnan(args[1].data.u.number))
+        && isnan(njs_number(&args[1])))
     {
         value = &njs_value_true;
     }
@@ -527,14 +527,14 @@ njs_number_prototype_to_string(njs_vm_t 
     }
 
     if (nargs > 1) {
-        radix = args[1].data.u.number;
+        radix = njs_number(&args[1]);
 
         if (radix < 2 || radix > 36 || radix != (int) radix) {
             njs_range_error(vm, NULL);
             return NXT_ERROR;
         }
 
-        number = value->data.u.number;
+        number = njs_number(value);
 
         if (radix != 10 && !isnan(number) && !isinf(number)) {
             return njs_number_to_string_radix(vm, &vm->retval, number, radix);
@@ -663,7 +663,7 @@ njs_number_global_is_nan(njs_vm_t *vm, n
 
     value = &njs_value_true;
 
-    if (nargs > 1 && !isnan(args[1].data.u.number)) {
+    if (nargs > 1 && !isnan(njs_number(&args[1]))) {
         value = &njs_value_false;
     }
 
@@ -683,7 +683,7 @@ njs_number_is_finite(njs_vm_t *vm, njs_v
     value = &njs_value_false;
 
     if (nargs > 1 && njs_is_number(&args[1])) {
-        num = args[1].data.u.number;
+        num = njs_number(&args[1]);
 
         if (!isnan(num) && !isinf(num)) {
             value = &njs_value_true;
@@ -738,7 +738,7 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
         radix = 0;
 
         if (nargs > 2) {
-            radix = args[2].data.u.number;
+            radix = njs_number(&args[2]);
 
             if (radix != 0) {
                 if (radix < 2 || radix > 36) {
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_number.h
--- a/njs/njs_number.h	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_number.h	Mon Jul 08 17:49:14 2019 +0300
@@ -133,7 +133,7 @@ nxt_inline double
 njs_primitive_value_to_number(const njs_value_t *value)
 {
     if (nxt_fast_path(njs_is_numeric(value))) {
-        return value->data.u.number;
+        return njs_number(value);
     }
 
     return njs_string_to_number(value, 1);
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_parser_expression.c
--- a/njs/njs_parser_expression.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_parser_expression.c	Mon Jul 08 17:49:14 2019 +0300
@@ -580,9 +580,8 @@ njs_parser_unary_expression(njs_vm_t *vm
     if (token == NJS_TOKEN_UNARY_NEGATION && node->token == NJS_TOKEN_NUMBER) {
 
         /* Optimization of common negative number. */
-        num = -node->u.value.data.u.number;
-        node->u.value.data.u.number = num;
-        node->u.value.data.truth = njs_is_number_true(num);
+        num = -njs_number(&node->u.value);
+        njs_set_number(&node->u.value, num);
 
         return next;
     }
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_parser_terminal.c	Mon Jul 08 17:49:14 2019 +0300
@@ -157,9 +157,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
             return NJS_TOKEN_ERROR;
         }
 
-        node->u.value.data.u.number = num;
-        node->u.value.type = NJS_NUMBER;
-        node->u.value.data.truth = njs_is_number_true(num);
+        njs_set_number(&node->u.value, num);
 
         break;
 
@@ -710,9 +708,7 @@ njs_parser_array_item(njs_vm_t *vm, njs_
         return NXT_ERROR;
     }
 
-    number->u.value.data.u.number = array->u.length;
-    number->u.value.type = NJS_NUMBER;
-    number->u.value.data.truth = (array->u.length != 0);
+    njs_set_number(&number->u.value, array->u.length);
 
     ret = njs_parser_object_property(vm, parser, array, number, value);
     if (nxt_slow_path(ret != NXT_OK)) {
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_string.c
--- a/njs/njs_string.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_string.c	Mon Jul 08 17:49:14 2019 +0300
@@ -1121,7 +1121,7 @@ njs_string_prototype_substring(njs_vm_t 
     start = 0;
 
     if (nargs > 1) {
-        start = args[1].data.u.number;
+        start = njs_number(&args[1]);
 
         if (start < 0) {
             start = 0;
@@ -1133,7 +1133,7 @@ njs_string_prototype_substring(njs_vm_t 
         end = length;
 
         if (nargs > 2) {
-            end = args[2].data.u.number;
+            end = njs_number(&args[2]);
 
             if (end < 0) {
                 end = 0;
@@ -1177,7 +1177,7 @@ njs_string_prototype_substr(njs_vm_t *vm
     start = 0;
 
     if (nargs > 1) {
-        start = args[1].data.u.number;
+        start = njs_number(&args[1]);
 
         if (start < length) {
             if (start < 0) {
@@ -1191,7 +1191,7 @@ njs_string_prototype_substr(njs_vm_t *vm
             length -= start;
 
             if (nargs > 2) {
-                n = args[2].data.u.number;
+                n = njs_number(&args[2]);
 
                 if (n < 0) {
                     length = 0;
@@ -1228,7 +1228,7 @@ njs_string_prototype_char_at(njs_vm_t *v
     length = 1;
 
     if (nargs > 1) {
-        start = args[1].data.u.number;
+        start = njs_number(&args[1]);
 
         if (start < 0 || start >= (ssize_t) slice.string_length) {
             start = 0;
@@ -1263,7 +1263,7 @@ njs_string_slice_args(njs_slice_prop_t *
     length = slice->string_length;
 
     value = njs_arg(args, nargs, 1);
-    start = value->data.u.number;
+    start = njs_number(value);
 
     if (start < 0) {
         start += length;
@@ -1280,7 +1280,7 @@ njs_string_slice_args(njs_slice_prop_t *
     } else {
         if (!njs_is_undefined(njs_arg(args, nargs, 2))) {
             value = njs_arg(args, nargs, 2);
-            end = value->data.u.number;
+            end = njs_number(value);
 
         } else {
             end = length;
@@ -1385,7 +1385,7 @@ njs_string_prototype_char_code_at(njs_vm
     index = 0;
 
     if (nargs > 1) {
-        index = args[1].data.u.number;
+        index = njs_number(&args[1]);
 
         if (nxt_slow_path(index < 0 || index >= length)) {
             num = NAN;
@@ -1472,7 +1472,7 @@ njs_string_bytes_from_array(njs_vm_t *vm
     octet = array->start;
 
     while (length != 0) {
-        *p++ = (u_char) njs_number_to_uint32(octet->data.u.number);
+        *p++ = (u_char) njs_number_to_uint32(njs_number(octet));
         octet++;
         length--;
     }
@@ -1699,7 +1699,7 @@ njs_string_from_char_code(njs_vm_t *vm, 
     size = 0;
 
     for (i = 1; i < nargs; i++) {
-        num = args[i].data.u.number;
+        num = njs_number(&args[i]);
         if (isnan(num)) {
             goto range_error;
         }
@@ -1719,7 +1719,7 @@ njs_string_from_char_code(njs_vm_t *vm, 
     }
 
     for (i = 1; i < nargs; i++) {
-        p = nxt_utf8_encode(p, args[i].data.u.number);
+        p = nxt_utf8_encode(p, njs_number(&args[i]));
     }
 
     return NXT_OK;
@@ -1747,7 +1747,7 @@ njs_string_prototype_index_of(njs_vm_t *
         index = 0;
 
         if (nargs > 2) {
-            index = args[2].data.u.number;
+            index = njs_number(&args[2]);
 
             if (index < 0) {
                 index = 0;
@@ -1828,7 +1828,7 @@ njs_string_prototype_last_index_of(njs_v
         goto done;
     }
 
-    pos = njs_arg(args, nargs, 2)->data.u.number;
+    pos = njs_number(njs_arg(args, nargs, 2));
 
     if (isnan(pos)) {
         index = NJS_STRING_MAX_LENGTH;
@@ -1929,7 +1929,7 @@ njs_string_prototype_includes(njs_vm_t *
         index = 0;
 
         if (nargs > 2) {
-            index = args[2].data.u.number;
+            index = njs_number(&args[2]);
 
             if (index < 0) {
                 index = 0;
@@ -2006,7 +2006,7 @@ njs_string_starts_or_ends_with(njs_vm_t 
 
         length = njs_string_prop(&string, &args[0]);
 
-        index = (nargs > 2) ? args[2].data.u.number : -1;
+        index = (nargs > 2) ? njs_number(&args[2]) : -1;
 
         if (starts) {
             if (index < 0) {
@@ -2407,7 +2407,7 @@ njs_string_prototype_repeat(njs_vm_t *vm
         max = (string.size > 1) ? NJS_STRING_MAX_LENGTH / string.size
                                 : NJS_STRING_MAX_LENGTH;
 
-        n = args[1].data.u.number;
+        n = njs_number(&args[1]);
 
         if (nxt_slow_path(n < 0 || n >= max)) {
             njs_range_error(vm, NULL);
@@ -2471,7 +2471,7 @@ njs_string_prototype_pad(njs_vm_t *vm, n
     static const njs_value_t  string_space = njs_string(" ");
 
     length = njs_string_prop(&string, &args[0]);
-    new_length = nargs > 1 ? args[1].data.u.number : 0;
+    new_length = nargs > 1 ? njs_number(&args[1]) : 0;
 
     if (new_length <= length) {
         vm->retval = args[0];
@@ -2793,7 +2793,7 @@ njs_string_prototype_split(njs_vm_t *vm,
     if (nargs > 1) {
 
         if (nargs > 2) {
-            limit = args[2].data.u.number;
+            limit = njs_number(&args[2]);
 
             if (limit == 0) {
                 goto done;
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_time.c
--- a/njs/njs_time.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_time.c	Mon Jul 08 17:49:14 2019 +0300
@@ -37,7 +37,7 @@ njs_set_timer(njs_vm_t *vm, njs_value_t 
     delay = 0;
 
     if (!immediate && nargs >= 3 && njs_is_number(&args[2])) {
-        delay = args[2].data.u.number;
+        delay = njs_number(&args[2]);
     }
 
     event = nxt_mp_alloc(vm->mem_pool, sizeof(njs_event_t));
@@ -110,7 +110,7 @@ njs_clear_timeout(njs_vm_t *vm, njs_valu
     }
 
     p = nxt_sprintf(buf, buf + nxt_length(buf), "%uD",
-                    (unsigned) args[1].data.u.number);
+                    (unsigned) njs_number(&args[1]));
 
     lhq.key.start = buf;
     lhq.key.length = p - buf;
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_value.c
--- a/njs/njs_value.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_value.c	Mon Jul 08 17:49:14 2019 +0300
@@ -108,7 +108,7 @@ njs_values_strict_equal(const njs_value_
         }
 
         /* Infinities are handled correctly by comparision. */
-        return (val1->data.u.number == val2->data.u.number);
+        return (njs_number(val1) == njs_number(val2));
     }
 
     if (njs_is_string(val1)) {
diff -r aa688dbdc7de -r 254c9b04b0d2 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Jul 08 15:31:39 2019 +0300
+++ b/njs/njs_vm.c	Mon Jul 08 17:49:14 2019 +0300
@@ -899,7 +899,7 @@ njs_vmcode_increment(njs_vm_t *vm, njs_v
     double  num;
 
     if (nxt_fast_path(njs_is_numeric(value))) {
-        num = value->data.u.number + 1.0;
+        num = njs_number(value) + 1.0;
 
         njs_release(vm, reference);
 
@@ -919,7 +919,7 @@ njs_vmcode_decrement(njs_vm_t *vm, njs_v
     double  num;
 
     if (nxt_fast_path(njs_is_numeric(value))) {
-        num = value->data.u.number - 1.0;
+        num = njs_number(value) - 1.0;
 
         njs_release(vm, reference);
 
@@ -940,7 +940,7 @@ njs_vmcode_post_increment(njs_vm_t *vm, 
     double  num;
 
     if (nxt_fast_path(njs_is_numeric(value))) {
-        num = value->data.u.number;
+        num = njs_number(value);
 
         njs_release(vm, reference);
 
@@ -961,7 +961,7 @@ njs_vmcode_post_decrement(njs_vm_t *vm, 
     double  num;
 
     if (nxt_fast_path(njs_is_numeric(value))) {
-        num = value->data.u.number;
+        num = njs_number(value);
 
         njs_release(vm, reference);
 
@@ -1047,7 +1047,7 @@ njs_ret_t
 njs_vmcode_unary_plus(njs_vm_t *vm, njs_value_t *value, njs_value_t *invld)
 {
     if (nxt_fast_path(njs_is_numeric(value))) {
-        njs_set_number(&vm->retval, value->data.u.number);
+        njs_set_number(&vm->retval, njs_number(value));
         return sizeof(njs_vmcode_2addr_t);
     }
 
@@ -1059,7 +1059,7 @@ njs_ret_t
 njs_vmcode_unary_negation(njs_vm_t *vm, njs_value_t *value, njs_value_t *invld)
 {
     if (nxt_fast_path(njs_is_numeric(value))) {
-        njs_set_number(&vm->retval, - value->data.u.number);
+        njs_set_number(&vm->retval, - njs_number(value));
         return sizeof(njs_vmcode_2addr_t);
     }
 
@@ -1076,7 +1076,7 @@ njs_vmcode_addition(njs_vm_t *vm, njs_va
 
     if (nxt_fast_path(njs_is_numeric(val1) && njs_is_numeric(val2))) {
 
-        num = val1->data.u.number + val2->data.u.number;
+        num = njs_number(val1) + njs_number(val2);
         njs_set_number(&vm->retval, num);
 
         return sizeof(njs_vmcode_3addr_t);
@@ -1157,7 +1157,7 @@ njs_vmcode_substraction(njs_vm_t *vm, nj
 
     if (nxt_fast_path(njs_is_numeric(val1) && njs_is_numeric(val2))) {
 
-        num = val1->data.u.number - val2->data.u.number;
+        num = njs_number(val1) - njs_number(val2);
         njs_set_number(&vm->retval, num);
 
         return sizeof(njs_vmcode_3addr_t);
@@ -1174,7 +1174,7 @@ njs_vmcode_multiplication(njs_vm_t *vm, 
 
     if (nxt_fast_path(njs_is_numeric(val1) && njs_is_numeric(val2))) {
 
-        num = val1->data.u.number * val2->data.u.number;


More information about the nginx-devel mailing list