Welcome! Log In Create A New Profile

Advanced

[njs] isNaN() function.

June 01, 2016 08:52AM
details: http://hg.nginx.org/njs/rev/a61f70d8874a
branches:
changeset: 107:a61f70d8874a
user: Igor Sysoev <igor@sysoev.ru>
date: Thu Apr 21 18:23:12 2016 +0300
description:
isNaN() function.

diffstat:

njs/njs_builtin.c | 2 ++
njs/njs_generator.c | 1 +
njs/njs_lexer_keyword.c | 1 +
njs/njs_number.c | 18 ++++++++++++++++++
njs/njs_number.h | 3 +++
njs/njs_parser.c | 1 +
njs/njs_parser.h | 1 +
njs/njs_string.c | 13 ++++++++++++-
njs/njs_vm.h | 3 ++-
njs/test/njs_unit_test.c | 18 ++++++++++++++++++
10 files changed, 59 insertions(+), 2 deletions(-)

diffs (175 lines):

diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_builtin.c
--- a/njs/njs_builtin.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_builtin.c Thu Apr 21 18:23:12 2016 +0300
@@ -81,12 +81,14 @@ njs_builtin_objects_create(njs_vm_t *vm)
static const njs_object_init_t *function_init[] = {
&njs_eval_function_init,
NULL,
+ NULL,
};

static const njs_function_init_t native_functions[] = {
/* SunC does not allow empty array initialization. */
{ njs_eval_function, { 0 } },
{ njs_object_prototype_to_string, { 0 } },
+ { njs_number_is_nan, { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
};

static const njs_object_prop_t null_proto_property = {
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_generator.c
--- a/njs/njs_generator.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_generator.c Thu Apr 21 18:23:12 2016 +0300
@@ -295,6 +295,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t
case NJS_TOKEN_MATH:
case NJS_TOKEN_EVAL:
case NJS_TOKEN_TO_STRING:
+ case NJS_TOKEN_IS_NAN:
return njs_generate_builtin_object(vm, parser, node);

case NJS_TOKEN_FUNCTION:
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_lexer_keyword.c Thu Apr 21 18:23:12 2016 +0300
@@ -89,6 +89,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 },

/* Reserved words. */

diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_number.c
--- a/njs/njs_number.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_number.c Thu Apr 21 18:23:12 2016 +0300
@@ -433,3 +433,21 @@ const njs_object_init_t njs_number_prot
njs_number_prototype_properties,
nxt_nitems(njs_number_prototype_properties),
};
+
+
+njs_ret_t
+njs_number_is_nan(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ const njs_value_t *value;
+
+ value = &njs_value_true;
+
+ if (nargs > 1 && !njs_is_nan(args[1].data.u.number)) {
+ value = &njs_value_false;
+ }
+
+ vm->retval = *value;
+
+ return NXT_OK;
+}
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_number.h
--- a/njs/njs_number.h Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_number.h Thu Apr 21 18:23:12 2016 +0300
@@ -30,6 +30,9 @@ njs_ret_t njs_number_to_string(njs_vm_t
const njs_value_t *number);
njs_ret_t njs_number_constructor(njs_vm_t *vm, njs_value_t *args,
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);
+

extern const njs_object_init_t njs_number_constructor_init;
extern const njs_object_init_t njs_number_prototype_init;
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_parser.c
--- a/njs/njs_parser.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_parser.c Thu Apr 21 18:23:12 2016 +0300
@@ -1645,6 +1645,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa

case NJS_TOKEN_EVAL:
case NJS_TOKEN_TO_STRING:
+ case NJS_TOKEN_IS_NAN:
return njs_parser_builtin_function(vm, parser, node);

default:
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_parser.h
--- a/njs/njs_parser.h Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_parser.h Thu Apr 21 18:23:12 2016 +0300
@@ -176,6 +176,7 @@ typedef enum {

NJS_TOKEN_EVAL,
NJS_TOKEN_TO_STRING,
+ NJS_TOKEN_IS_NAN,

NJS_TOKEN_RESERVED,
} njs_token_t;
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_string.c
--- a/njs/njs_string.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_string.c Thu Apr 21 18:23:12 2016 +0300
@@ -1685,6 +1685,8 @@ njs_string_to_number(njs_value_t *value)
nxt_bool_t minus;
const u_char *p, *end;

+ const size_t infinity = sizeof("Infinity") - 1;
+
size = value->short_string.size;

if (size != NJS_STRING_LONG) {
@@ -1719,11 +1721,20 @@ njs_string_to_number(njs_value_t *value)
minus = 1;
}

+ if (p == end) {
+ return NJS_NAN;
+ }
+
if (*p >= '0' && *p <= '9') {
num = njs_number_parse(&p, end);

} else {
- return NJS_NAN;
+ if (p + infinity > end || memcmp(p, "Infinity", infinity) != 0) {
+ return NJS_NAN;
+ }
+
+ num = NJS_INFINITY;
+ p += infinity;
}

while (p < end) {
diff -r a69b4eaebdcf -r a61f70d8874a njs/njs_vm.h
--- a/njs/njs_vm.h Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/njs_vm.h Thu Apr 21 18:23:12 2016 +0300
@@ -702,7 +702,8 @@ enum njs_object_e {
enum njs_function_e {
NJS_FUNCTION_EVAL = 0,
NJS_FUNCTION_TO_STRING = 1,
-#define NJS_FUNCTION_MAX (NJS_FUNCTION_TO_STRING + 1)
+ NJS_FUNCTION_IS_NAN = 2,
+#define NJS_FUNCTION_MAX (NJS_FUNCTION_IS_NAN + 1)
};


diff -r a69b4eaebdcf -r a61f70d8874a njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Thu Apr 21 15:57:05 2016 +0300
+++ b/njs/test/njs_unit_test.c Thu Apr 21 18:23:12 2016 +0300
@@ -4395,6 +4395,24 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("Math"),
nxt_string("[object Object]") },

+ { nxt_string("isNaN"),
+ nxt_string("[object Function]") },
+
+ { nxt_string("isNaN()"),
+ nxt_string("true") },
+
+ { nxt_string("isNaN(123)"),
+ nxt_string("false") },
+
+ { nxt_string("isNaN('123')"),
+ nxt_string("false") },
+
+ { nxt_string("isNaN('Infinity')"),
+ nxt_string("false") },
+
+ { nxt_string("isNaN('abc')"),
+ nxt_string("true") },
+
/* External interface. */

{ nxt_string("function f(req) { return req.uri }"),

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] isNaN() function.

Igor Sysoev 721 June 01, 2016 08:52AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 125
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready