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 = {
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:
{ 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. */
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;
+}
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;
case NJS_TOKEN_EVAL:
case NJS_TOKEN_TO_STRING:
+ case NJS_TOKEN_IS_NAN:
return njs_parser_builtin_function(vm, parser, node);
default:
NJS_TOKEN_EVAL,
NJS_TOKEN_TO_STRING,
+ NJS_TOKEN_IS_NAN,
NJS_TOKEN_RESERVED,
} njs_token_t;
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) {
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) {
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)
};
{ 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 }"),