[njs] isFinite() function.

Igor Sysoev igor at sysoev.ru
Wed Jun 1 12:50:31 UTC 2016


details:   http://hg.nginx.org/njs/rev/fd72dd7112c2
branches:  
changeset: 108:fd72dd7112c2
user:      Igor Sysoev <igor at sysoev.ru>
date:      Thu Apr 21 18:23:16 2016 +0300
description:
isFinite() function.

diffstat:

 njs/njs_builtin.c        |   2 ++
 njs/njs_generator.c      |   1 +
 njs/njs_lexer_keyword.c  |   1 +
 njs/njs_number.c         |  23 +++++++++++++++++++++++
 njs/njs_number.h         |   2 ++
 njs/njs_parser.c         |   1 +
 njs/njs_parser.h         |   1 +
 njs/njs_vm.h             |   3 ++-
 njs/test/njs_unit_test.c |  18 ++++++++++++++++++
 9 files changed, 51 insertions(+), 1 deletions(-)

diffs (146 lines):

diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_builtin.c	Thu Apr 21 18:23:16 2016 +0300
@@ -82,6 +82,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
         &njs_eval_function_init,
         NULL,
         NULL,
+        NULL,
     };
 
     static const njs_function_init_t  native_functions[] = {
@@ -89,6 +90,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
         { njs_eval_function,               { 0 } },
         { njs_object_prototype_to_string,  { 0 } },
         { njs_number_is_nan,               { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
+        { njs_number_is_finite,            { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
     };
 
     static const njs_object_prop_t    null_proto_property = {
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_generator.c
--- a/njs/njs_generator.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_generator.c	Thu Apr 21 18:23:16 2016 +0300
@@ -296,6 +296,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t
     case NJS_TOKEN_EVAL:
     case NJS_TOKEN_TO_STRING:
     case NJS_TOKEN_IS_NAN:
+    case NJS_TOKEN_IS_FINITE:
         return njs_generate_builtin_object(vm, parser, node);
 
     case NJS_TOKEN_FUNCTION:
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_lexer_keyword.c	Thu Apr 21 18:23:16 2016 +0300
@@ -90,6 +90,7 @@ static const njs_keyword_t  njs_keywords
     { nxt_string("eval"),          NJS_TOKEN_EVAL, 0 },
     { nxt_string("toString"),      NJS_TOKEN_TO_STRING, 0 },
     { nxt_string("isNaN"),         NJS_TOKEN_IS_NAN, 0 },
+    { nxt_string("isFinite"),      NJS_TOKEN_IS_FINITE, 0 },
 
     /* Reserved words. */
 
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_number.c
--- a/njs/njs_number.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_number.c	Thu Apr 21 18:23:16 2016 +0300
@@ -451,3 +451,26 @@ njs_number_is_nan(njs_vm_t *vm, njs_valu
 
     return NXT_OK;
 }
+
+
+njs_ret_t
+njs_number_is_finite(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    double             num;
+    const njs_value_t  *value;
+
+    value = &njs_value_false;
+
+    if (nargs > 1) {
+        num = args[1].data.u.number;
+
+        if (!njs_is_nan(num) && !njs_is_infinity(num)) {
+            value = &njs_value_true;
+        }
+    }
+
+    vm->retval = *value;
+
+    return NXT_OK;
+}
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_number.h
--- a/njs/njs_number.h	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_number.h	Thu Apr 21 18:23:16 2016 +0300
@@ -32,6 +32,8 @@ njs_ret_t njs_number_constructor(njs_vm_
     nxt_uint_t nargs, njs_index_t unused);
 njs_ret_t njs_number_is_nan(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused);
+njs_ret_t njs_number_is_finite(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused);
 
 
 extern const njs_object_init_t  njs_number_constructor_init;
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_parser.c
--- a/njs/njs_parser.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_parser.c	Thu Apr 21 18:23:16 2016 +0300
@@ -1646,6 +1646,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
     case NJS_TOKEN_EVAL:
     case NJS_TOKEN_TO_STRING:
     case NJS_TOKEN_IS_NAN:
+    case NJS_TOKEN_IS_FINITE:
         return njs_parser_builtin_function(vm, parser, node);
 
     default:
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_parser.h
--- a/njs/njs_parser.h	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_parser.h	Thu Apr 21 18:23:16 2016 +0300
@@ -177,6 +177,7 @@ typedef enum {
     NJS_TOKEN_EVAL,
     NJS_TOKEN_TO_STRING,
     NJS_TOKEN_IS_NAN,
+    NJS_TOKEN_IS_FINITE,
 
     NJS_TOKEN_RESERVED,
 } njs_token_t;
diff -r a61f70d8874a -r fd72dd7112c2 njs/njs_vm.h
--- a/njs/njs_vm.h	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/njs_vm.h	Thu Apr 21 18:23:16 2016 +0300
@@ -703,7 +703,8 @@ enum njs_function_e {
     NJS_FUNCTION_EVAL =        0,
     NJS_FUNCTION_TO_STRING =   1,
     NJS_FUNCTION_IS_NAN =      2,
-#define NJS_FUNCTION_MAX       (NJS_FUNCTION_IS_NAN + 1)
+    NJS_FUNCTION_IS_FINITE =   3,
+#define NJS_FUNCTION_MAX       (NJS_FUNCTION_IS_FINITE + 1)
 };
 
 
diff -r a61f70d8874a -r fd72dd7112c2 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Apr 21 18:23:12 2016 +0300
+++ b/njs/test/njs_unit_test.c	Thu Apr 21 18:23:16 2016 +0300
@@ -4413,6 +4413,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("isNaN('abc')"),
       nxt_string("true") },
 
+    { nxt_string("isFinite"),
+      nxt_string("[object Function]") },
+
+    { nxt_string("isFinite()"),
+      nxt_string("false") },
+
+    { nxt_string("isFinite(123)"),
+      nxt_string("true") },
+
+    { nxt_string("isFinite('123')"),
+      nxt_string("true") },
+
+    { nxt_string("isFinite('Infinity')"),
+      nxt_string("false") },
+
+    { nxt_string("isFinite('abc')"),
+      nxt_string("false") },
+
     /* External interface. */
 
     { nxt_string("function f(req) { return req.uri }"),



More information about the nginx-devel mailing list