]> git.kaiwu.me - njs.git/commitdiff
Moving global functions to global object.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 18 Oct 2019 13:38:55 +0000 (16:38 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 18 Oct 2019 13:38:55 +0000 (16:38 +0300)
This closes #132 issue on Github.

18 files changed:
src/njs_builtin.c
src/njs_generator.c
src/njs_lexer.h
src/njs_lexer_keyword.c
src/njs_module.c
src/njs_module.h
src/njs_number.c
src/njs_number.h
src/njs_parser.c
src/njs_parser.h
src/njs_parser_expression.c
src/njs_parser_terminal.c
src/njs_string.c
src/njs_string.h
src/njs_timer.c
src/njs_timer.h
src/njs_vm.h
src/test/njs_unit_test.c

index 6358b509b7a23caa2c9c4520a92a72d2b641294a..38eca6c947cccf7d10d5d4c7a19fa92e3186c44a 100644 (file)
@@ -93,47 +93,6 @@ const njs_object_init_t  *njs_constructor_init[] = {
 };
 
 
-const njs_object_init_t  *njs_function_init[] = {
-    &njs_eval_function_init,
-    &njs_to_string_function_init,
-    &njs_is_nan_function_init,
-    &njs_is_finite_function_init,
-    &njs_parse_int_function_init,
-    &njs_parse_float_function_init,
-    &njs_encode_uri_function_init,
-    &njs_encode_uri_component_function_init,
-    &njs_decode_uri_function_init,
-    &njs_decode_uri_component_function_init,
-    &njs_require_function_init,
-    &njs_set_timeout_function_init,
-    &njs_set_immediate_function_init,
-    &njs_clear_timeout_function_init,
-    NULL
-};
-
-
-const njs_function_init_t  njs_native_functions[] = {
-    /* SunC does not allow empty array initialization. */
-    { njs_eval_function,               { 0 } },
-    { njs_object_prototype_to_string,  { 0 } },
-    { njs_number_global_is_nan,        { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
-    { njs_number_is_finite,            { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
-    { njs_number_parse_int,
-      { NJS_SKIP_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG } },
-    { njs_number_parse_float,          { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_string_encode_uri,           { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_string_encode_uri_component, { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_string_decode_uri,           { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_string_decode_uri_component, { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_module_require,              { NJS_SKIP_ARG, NJS_STRING_ARG } },
-    { njs_set_timeout,
-      { NJS_SKIP_ARG, NJS_FUNCTION_ARG, NJS_NUMBER_ARG } },
-    { njs_set_immediate,
-      { NJS_SKIP_ARG, NJS_FUNCTION_ARG } },
-    { njs_clear_timeout,               { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
-};
-
-
 const njs_function_init_t  njs_native_constructors[] = {
     /* SunC does not allow empty array initialization. */
     { njs_object_constructor,     { 0 } },
@@ -331,29 +290,6 @@ njs_builtin_objects_create(njs_vm_t *vm)
         }
     }
 
-    f = njs_native_functions;
-    func = shared->functions;
-
-    for (p = njs_function_init; *p != NULL; p++) {
-        obj = *p;
-
-        ret = njs_object_hash_init(vm, &func->object.shared_hash, obj);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return NJS_ERROR;
-        }
-
-        func->object.shared = 1;
-        func->object.extensible = 1;
-        func->native = 1;
-        func->args_offset = 1;
-
-        func->u.native = f->native;
-        memcpy(func->args_types, f->args_types, NJS_ARGS_TYPES_MAX);
-
-        f++;
-        func++;
-    }
-
     prototype = shared->prototypes;
     memcpy(prototype, njs_prototype_values, sizeof(njs_prototype_values));
 
@@ -994,28 +930,21 @@ njs_int_t
 njs_builtin_match_native_function(njs_vm_t *vm, njs_function_t *function,
     njs_str_t *name)
 {
-    size_t                     len;
-    njs_str_t                  string, middle;
-    njs_int_t                  ret;
-    const njs_object_init_t    *obj, **p;
-    const njs_object_prop_t    *prop;
-    const njs_function_init_t  *fun;
-
-    fun = njs_native_functions;
-
-    for (p = njs_function_init; *p != NULL; p++, fun++) {
-        if (function->u.native == fun->native) {
-            *name = (*p)->name;
-
-            return NJS_OK;
-        }
-    }
+    size_t                   len;
+    njs_str_t                string, middle;
+    njs_int_t                ret;
+    const njs_object_init_t  *obj;
+    const njs_object_prop_t  *prop;
 
     middle = njs_str_value(".");
 
     ret = njs_builtin_match(njs_object_init, function, &prop, &obj);
 
     if (ret == NJS_OK) {
+        if (!obj->name.length) {
+            middle = njs_str_value("");
+        }
+
         goto found;
     }
 
@@ -1100,11 +1029,137 @@ static const njs_object_prop_t  njs_global_this_object_properties[] =
         .name = njs_string("undefined"),
         .value = njs_value(NJS_UNDEFINED, 0, NAN),
     },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("isFinite"),
+        .value = njs_native_function(njs_number_is_finite, 1,
+                                     NJS_SKIP_ARG, NJS_NUMBER_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("isNaN"),
+        .value = njs_native_function(njs_number_global_is_nan, 1,
+                                     NJS_SKIP_ARG, NJS_NUMBER_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("parseFloat"),
+        .value = njs_native_function(njs_number_parse_float, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("parseInt"),
+        .value = njs_native_function(njs_number_parse_int, 2,
+                     NJS_SKIP_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("toString"),
+        .value = njs_native_function(njs_object_prototype_to_string, 0, 0),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("encodeURI"),
+        .value = njs_native_function(njs_string_encode_uri, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_long_string("encodeURIComponent"),
+        .value = njs_native_function(njs_string_encode_uri_component, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("decodeURI"),
+        .value = njs_native_function(njs_string_decode_uri, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_long_string("decodeURIComponent"),
+        .value = njs_native_function(njs_string_decode_uri_component, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("eval"),
+        .value = njs_native_function(njs_eval_function, 1, 0),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("setTimeout"),
+        .value = njs_native_function(njs_set_timeout, 2,
+                                     NJS_SKIP_ARG, NJS_FUNCTION_ARG,
+                                     NJS_NUMBER_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("setImmediate"),
+        .value = njs_native_function(njs_set_immediate, 4,
+                                     NJS_SKIP_ARG, NJS_FUNCTION_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("clearTimeout"),
+        .value = njs_native_function(njs_clear_timeout, 1,
+                                     NJS_SKIP_ARG, NJS_NUMBER_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("require"),
+        .value = njs_native_function(njs_module_require, 1,
+                                     NJS_SKIP_ARG, NJS_STRING_ARG),
+        .writable = 1,
+        .configurable = 1,
+    },
+
 };
 
 
 const njs_object_init_t  njs_global_this_init = {
-    njs_str("this"),
+    njs_str(""),
     njs_global_this_object_properties,
     njs_nitems(njs_global_this_object_properties)
 };
index c8690aabe665809e4ea913207a3a3f82de3b111b..139748f1308f8c0622f998cf3c336c8de4f7d238 100644 (file)
@@ -441,6 +441,7 @@ njs_generate(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node)
 
     case NJS_TOKEN_NAME:
     case NJS_TOKEN_ARGUMENTS:
+    case NJS_TOKEN_EVAL:
     case NJS_TOKEN_NON_LOCAL_THIS:
         return njs_generate_name(vm, generator, node);
 
@@ -463,20 +464,6 @@ njs_generate(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node)
     case NJS_TOKEN_PROCESS:
     case NJS_TOKEN_MATH:
     case NJS_TOKEN_JSON:
-    case NJS_TOKEN_EVAL:
-    case NJS_TOKEN_TO_STRING:
-    case NJS_TOKEN_IS_NAN:
-    case NJS_TOKEN_IS_FINITE:
-    case NJS_TOKEN_PARSE_INT:
-    case NJS_TOKEN_PARSE_FLOAT:
-    case NJS_TOKEN_ENCODE_URI:
-    case NJS_TOKEN_ENCODE_URI_COMPONENT:
-    case NJS_TOKEN_DECODE_URI:
-    case NJS_TOKEN_DECODE_URI_COMPONENT:
-    case NJS_TOKEN_REQUIRE:
-    case NJS_TOKEN_SET_TIMEOUT:
-    case NJS_TOKEN_SET_IMMEDIATE:
-    case NJS_TOKEN_CLEAR_TIMEOUT:
         return njs_generate_builtin_object(vm, generator, node);
 
     case NJS_TOKEN_FUNCTION:
index 99319fd8a870d7d3dcdb2b380c521c13cf1c06ce..61775b61df51797f1aee796343807427a1d11276 100644 (file)
@@ -169,6 +169,7 @@ typedef enum {
     NJS_TOKEN_THIS,
     NJS_TOKEN_NON_LOCAL_THIS,
     NJS_TOKEN_ARGUMENTS,
+    NJS_TOKEN_EVAL,
 
 #define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_GLOBAL_OBJECT
 
@@ -196,23 +197,6 @@ typedef enum {
     NJS_TOKEN_URI_ERROR_CONSTRUCTOR,
     NJS_TOKEN_MEMORY_ERROR_CONSTRUCTOR,
 
-#define NJS_TOKEN_FIRST_FUNCTION   NJS_TOKEN_EVAL
-
-    NJS_TOKEN_EVAL,
-    NJS_TOKEN_TO_STRING,
-    NJS_TOKEN_IS_NAN,
-    NJS_TOKEN_IS_FINITE,
-    NJS_TOKEN_PARSE_INT,
-    NJS_TOKEN_PARSE_FLOAT,
-    NJS_TOKEN_ENCODE_URI,
-    NJS_TOKEN_ENCODE_URI_COMPONENT,
-    NJS_TOKEN_DECODE_URI,
-    NJS_TOKEN_DECODE_URI_COMPONENT,
-    NJS_TOKEN_REQUIRE,
-    NJS_TOKEN_SET_TIMEOUT,
-    NJS_TOKEN_SET_IMMEDIATE,
-    NJS_TOKEN_CLEAR_TIMEOUT,
-
     NJS_TOKEN_IMPORT,
     NJS_TOKEN_EXPORT,
 
index af6322ab91449c09d0edf2707314c25e3654168f..59d7224e67e62c5e3ff6d64758a0fea9663527de 100644 (file)
@@ -53,7 +53,6 @@ static const njs_keyword_t  njs_keywords[] = {
     /* Builtin objects. */
 
     { njs_str("this"),          NJS_TOKEN_THIS, 0 },
-    { njs_str("arguments"),     NJS_TOKEN_ARGUMENTS, 0 },
     { njs_str("njs"),           NJS_TOKEN_NJS, 0 },
     { njs_str("process"),       NJS_TOKEN_PROCESS, 0 },
     { njs_str("Math"),          NJS_TOKEN_MATH, 0 },
@@ -79,27 +78,15 @@ static const njs_keyword_t  njs_keywords[] = {
     { njs_str("URIError"),      NJS_TOKEN_URI_ERROR_CONSTRUCTOR, 0 },
     { njs_str("MemoryError"),   NJS_TOKEN_MEMORY_ERROR_CONSTRUCTOR, 0 },
 
-    { njs_str("eval"),          NJS_TOKEN_EVAL, 0 },
-    { njs_str("toString"),      NJS_TOKEN_TO_STRING, 0 },
-    { njs_str("isNaN"),         NJS_TOKEN_IS_NAN, 0 },
-    { njs_str("isFinite"),      NJS_TOKEN_IS_FINITE, 0 },
-    { njs_str("parseInt"),      NJS_TOKEN_PARSE_INT, 0 },
-    { njs_str("parseFloat"),    NJS_TOKEN_PARSE_FLOAT, 0 },
-    { njs_str("encodeURI"),     NJS_TOKEN_ENCODE_URI, 0 },
-    { njs_str("encodeURIComponent"),  NJS_TOKEN_ENCODE_URI_COMPONENT, 0 },
-    { njs_str("decodeURI"),     NJS_TOKEN_DECODE_URI, 0 },
-    { njs_str("decodeURIComponent"),  NJS_TOKEN_DECODE_URI_COMPONENT, 0 },
-    { njs_str("require"),       NJS_TOKEN_REQUIRE, 0 },
-    { njs_str("setTimeout"),    NJS_TOKEN_SET_TIMEOUT, 0 },
-    { njs_str("setImmediate"),  NJS_TOKEN_SET_IMMEDIATE, 0 },
-    { njs_str("clearTimeout"),  NJS_TOKEN_CLEAR_TIMEOUT, 0 },
-
     /* Module. */
     { njs_str("import"),        NJS_TOKEN_IMPORT, 0 },
     { njs_str("export"),        NJS_TOKEN_EXPORT, 0 },
 
     /* Reserved words. */
 
+    { njs_str("arguments"),     NJS_TOKEN_ARGUMENTS, 0 },
+    { njs_str("eval"),          NJS_TOKEN_EVAL, 0 },
+
     { njs_str("await"),         NJS_TOKEN_RESERVED, 0 },
     { njs_str("class"),         NJS_TOKEN_RESERVED, 0 },
     { njs_str("const"),         NJS_TOKEN_RESERVED, 0 },
index ee72d8d4be24546fd28fd84c8844552e0c8817e8..68eef62ebc11b322f2807f1cd3b96fdd9bc48a2f 100644 (file)
@@ -541,10 +541,3 @@ njs_module_require(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
     return NJS_ERROR;
 }
-
-
-const njs_object_init_t  njs_require_function_init = {
-    njs_str("require"),
-    NULL,
-    0,
-};
index 2fbc0c2e40d416daf1d06e077957efd4383f7a05..955ef5b890f0e80acb7ce99c187e00d2cab71608 100644 (file)
@@ -24,7 +24,6 @@ njs_int_t njs_module_require(njs_vm_t *vm, njs_value_t *args,
 
 
 extern const njs_lvlhsh_proto_t  njs_modules_hash_proto;
-extern const njs_object_init_t   njs_require_function_init;
 
 
 #endif /* _NJS_MODULE_H_INCLUDED_ */
index 0a4d6d991af6ae7cabd586a5d48446750be425f8..958658110267dbe2ec73dd3bb254283a8b64a000 100644 (file)
@@ -1062,33 +1062,6 @@ njs_number_parse_float(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 }
 
 
-static const njs_object_prop_t  njs_is_nan_function_properties[] =
-{
-    /* isNaN.name == "isNaN". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("isNaN"),
-        .configurable = 1,
-    },
-
-    /* isNaN.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_is_nan_function_init = {
-    njs_str("isNaN"),
-    njs_is_nan_function_properties,
-    njs_nitems(njs_is_nan_function_properties),
-};
-
-
 static const njs_object_prop_t  njs_is_finite_function_properties[] =
 {
     /* isFinite.name == "isFinite". */
@@ -1115,56 +1088,3 @@ const njs_object_init_t  njs_is_finite_function_init = {
     njs_nitems(njs_is_finite_function_properties),
 };
 
-
-static const njs_object_prop_t  njs_parse_int_function_properties[] =
-{
-    /* parseInt.name == "parseInt". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("parseInt"),
-        .configurable = 1,
-    },
-
-    /* parseInt.length == 2. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 2.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_parse_int_function_init = {
-    njs_str("parseInt"),
-    njs_parse_int_function_properties,
-    njs_nitems(njs_parse_int_function_properties),
-};
-
-
-static const njs_object_prop_t  njs_parse_float_function_properties[] =
-{
-    /* parseFloat.name == "parseFloat". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("parseFloat"),
-        .configurable = 1,
-    },
-
-    /* parseFloat.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_parse_float_function_init = {
-    njs_str("parseFloat"),
-    njs_parse_float_function_properties,
-    njs_nitems(njs_parse_float_function_properties),
-};
index 6317c31b507380104e10aa3ddc0a353730501d18..4046aa37cf547172765de0c436504bddd175ae82 100644 (file)
@@ -186,10 +186,5 @@ njs_uint32_to_string(njs_value_t *value, uint32_t u32)
 extern const njs_object_init_t  njs_number_constructor_init;
 extern const njs_object_init_t  njs_number_prototype_init;
 
-extern const njs_object_init_t  njs_is_nan_function_init;
-extern const njs_object_init_t  njs_is_finite_function_init;
-extern const njs_object_init_t  njs_parse_int_function_init;
-extern const njs_object_init_t  njs_parse_float_function_init;
-
 
 #endif /* _NJS_NUMBER_H_INCLUDED_ */
index 750ebc04133fdc84da15690c3732f161eccbdd75..c1620b7d15cc582b13a997d8d13ae45abf9295f8 100644 (file)
@@ -636,7 +636,7 @@ njs_parser_function_declaration(njs_vm_t *vm, njs_parser_t *parser)
     }
 
     if (token != NJS_TOKEN_NAME) {
-        if (token == NJS_TOKEN_ARGUMENTS || token == NJS_TOKEN_EVAL) {
+        if (njs_parser_restricted_identifier(token)) {
             njs_parser_syntax_error(vm, parser, "Identifier \"%V\" "
                                     "is forbidden in function declaration",
                                     njs_parser_text(parser));
@@ -1034,7 +1034,7 @@ njs_parser_var_statement(njs_vm_t *vm, njs_parser_t *parser, njs_token_t parent,
         }
 
         if (token != NJS_TOKEN_NAME) {
-            if (token == NJS_TOKEN_ARGUMENTS || token == NJS_TOKEN_EVAL) {
+            if (njs_parser_restricted_identifier(token)) {
                 njs_parser_syntax_error(vm, parser, "Identifier \"%V\" "
                                         "is forbidden in var declaration",
                                         njs_parser_text(parser));
index 5a33c8607a178a211cda3919d674b6a18f4b2484..066f96ad5034b9a61e656c7c6acd1531559149b1 100644 (file)
@@ -126,6 +126,10 @@ void njs_parser_node_error(njs_vm_t *vm, njs_parser_node_t *node,
 #define njs_parser_leave(parser) ((parser)->count--)
 
 
+#define njs_parser_restricted_identifier(token)                               \
+    (token == NJS_TOKEN_ARGUMENTS || token == NJS_TOKEN_EVAL)
+
+
 #define njs_parser_is_lvalue(node)                                            \
     ((node)->token == NJS_TOKEN_NAME || (node)->token == NJS_TOKEN_PROPERTY)
 
index 5507c6becd751a5c8b9d292d85339ff99b789fc6..cb1c247c23dc500dc9cde10acde2cf9ca07f08d5 100644 (file)
@@ -310,7 +310,7 @@ njs_parser_assignment_expression(njs_vm_t *vm, njs_parser_t *parser,
         if (!njs_parser_is_lvalue(parser->node)) {
             token = parser->node->token;
 
-            if (token == NJS_TOKEN_ARGUMENTS || token == NJS_TOKEN_EVAL) {
+            if (njs_parser_restricted_identifier(token)) {
                 njs_parser_syntax_error(vm, parser, "Identifier \"%s\" "
                                       "is forbidden as left-hand in assignment",
                                        (token == NJS_TOKEN_EVAL) ? "eval"
index d410b485631fe132d9c97f6b74487d233008adf3..2dacd195ce8d7d47edadceb91a39ebc6708b99c6 100644 (file)
@@ -344,27 +344,6 @@ njs_parser_reference(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token,
         node->index = NJS_INDEX_OBJECT_MEMORY_ERROR;
         break;
 
-    case NJS_TOKEN_EVAL:
-    case NJS_TOKEN_TO_STRING:
-    case NJS_TOKEN_IS_NAN:
-    case NJS_TOKEN_IS_FINITE:
-    case NJS_TOKEN_PARSE_INT:
-    case NJS_TOKEN_PARSE_FLOAT:
-    case NJS_TOKEN_ENCODE_URI:
-    case NJS_TOKEN_ENCODE_URI_COMPONENT:
-    case NJS_TOKEN_DECODE_URI:
-    case NJS_TOKEN_DECODE_URI_COMPONENT:
-    case NJS_TOKEN_REQUIRE:
-    case NJS_TOKEN_SET_TIMEOUT:
-    case NJS_TOKEN_SET_IMMEDIATE:
-    case NJS_TOKEN_CLEAR_TIMEOUT:
-        ret = njs_parser_builtin(vm, parser, node, NJS_FUNCTION, name, hash);
-        if (njs_slow_path(ret != NJS_OK)) {
-            return NULL;
-        }
-
-        break;
-
     case NJS_TOKEN_ARGUMENTS:
         njs_thread_log_debug("JS: arguments");
 
@@ -395,6 +374,7 @@ njs_parser_reference(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token,
         break;
 
     case NJS_TOKEN_NAME:
+    case NJS_TOKEN_EVAL:
         njs_thread_log_debug("JS: %V", name);
 
         node->token_line = token_line;
@@ -448,11 +428,6 @@ njs_parser_builtin(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node,
         njs_set_object(&var->value, &vm->shared->objects[index]);
         break;
 
-    case NJS_FUNCTION:
-        index = node->token - NJS_TOKEN_FIRST_FUNCTION;
-        njs_set_function(&var->value, &vm->shared->functions[index]);
-        break;
-
     default:
         return NJS_ERROR;
     }
index 04892eaa2697e1afe0afdd53b83a7259e3eda892..7e59a971251ebeec259da5572a14eb1789b711a8 100644 (file)
@@ -4857,138 +4857,3 @@ njs_value_index(njs_vm_t *vm, const njs_value_t *src, njs_uint_t runtime)
 
     return (njs_index_t) value;
 }
-
-
-static const njs_object_prop_t  njs_to_string_function_properties[] =
-{
-    /* toString.name == "toString". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("toString"),
-        .configurable = 1,
-    },
-
-    /* toString.length == 0. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 0, 0.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_to_string_function_init = {
-    njs_str("toString"),
-    njs_to_string_function_properties,
-    njs_nitems(njs_to_string_function_properties),
-};
-
-
-static const njs_object_prop_t  njs_encode_uri_function_properties[] =
-{
-    /* encodeURI.name == "encodeURI". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("encodeURI"),
-        .configurable = 1,
-    },
-
-    /* encodeURI.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_encode_uri_function_init = {
-    njs_str("encodeURI"),
-    njs_encode_uri_function_properties,
-    njs_nitems(njs_encode_uri_function_properties),
-};
-
-
-static const njs_object_prop_t  njs_encode_uri_component_function_properties[] =
-{
-    /* encodeURIComponent.name == "encodeURIComponent". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_long_string("encodeURIComponent"),
-        .configurable = 1,
-    },
-
-    /* encodeURIComponent.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_encode_uri_component_function_init = {
-    njs_str("encodeURIComponent"),
-    njs_encode_uri_component_function_properties,
-    njs_nitems(njs_encode_uri_component_function_properties),
-};
-
-
-static const njs_object_prop_t  njs_decode_uri_function_properties[] =
-{
-    /* decodeURI.name == "decodeURI". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("decodeURI"),
-        .configurable = 1,
-    },
-
-    /* decodeURI.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_decode_uri_function_init = {
-    njs_str("decodeURI"),
-    njs_decode_uri_function_properties,
-    njs_nitems(njs_decode_uri_function_properties),
-};
-
-
-static const njs_object_prop_t  njs_decode_uri_component_function_properties[] =
-{
-    /* decodeURIComponent.name == "decodeURIComponent". */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_long_string("decodeURIComponent"),
-        .configurable = 1,
-    },
-
-    /* decodeURIComponent.length == 1. */
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("length"),
-        .value = njs_value(NJS_NUMBER, 1, 1.0),
-        .configurable = 1,
-    },
-};
-
-
-const njs_object_init_t  njs_decode_uri_component_function_init = {
-    njs_str("decodeURIComponent"),
-    njs_decode_uri_component_function_properties,
-    njs_nitems(njs_decode_uri_component_function_properties),
-};
index d5cbe144325b716e52a39af30f6690753f4e9f3f..ef88a1abdec309fc3bdd30adb33d92005b2c6af4 100644 (file)
@@ -198,11 +198,5 @@ extern const njs_object_init_t  njs_string_constructor_init;
 extern const njs_object_init_t  njs_string_prototype_init;
 extern const njs_object_init_t  njs_string_instance_init;
 
-extern const njs_object_init_t  njs_to_string_function_init;
-extern const njs_object_init_t  njs_encode_uri_function_init;
-extern const njs_object_init_t  njs_encode_uri_component_function_init;
-extern const njs_object_init_t  njs_decode_uri_function_init;
-extern const njs_object_init_t  njs_decode_uri_component_function_init;
-
 
 #endif /* _NJS_STRING_H_INCLUDED_ */
index c985c86ebbad8e82b632115146122c13be18c764..6c21ce0578a4a6718cdbf981b9df30e3305f6d40 100644 (file)
@@ -127,23 +127,3 @@ njs_clear_timeout(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
     return NJS_OK;
 }
-
-
-const njs_object_init_t  njs_set_timeout_function_init = {
-    njs_str("setTimeout"),
-    NULL,
-    0,
-};
-
-const njs_object_init_t  njs_set_immediate_function_init = {
-    njs_str("setImmediate"),
-    NULL,
-    0,
-};
-
-
-const njs_object_init_t  njs_clear_timeout_function_init = {
-    njs_str("clearTimeout"),
-    NULL,
-    0,
-};
index c250b87b62e1cacfb4a6ad23ab51e82022aef877..ccb432e7b96fadec956ed233b2cc2552e51293c9 100644 (file)
@@ -16,8 +16,4 @@ njs_int_t njs_clear_timeout(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused);
 
 
-extern const njs_object_init_t  njs_set_timeout_function_init;
-extern const njs_object_init_t  njs_set_immediate_function_init;
-extern const njs_object_init_t  njs_clear_timeout_function_init;
-
 #endif /* _NJS_TIMER_H_INCLUDED_ */
index 63a49a14f63b27a3e8f8da5290a9677da2c5400a..a0e098387d9fe255666cdb79d2904f5f0311453d 100644 (file)
@@ -144,31 +144,13 @@ enum njs_constructor_e {
 enum njs_object_e {
     NJS_OBJECT_THIS = 0,
     NJS_OBJECT_NJS,
+    NJS_OBJECT_PROCESS,
     NJS_OBJECT_MATH,
     NJS_OBJECT_JSON,
 #define NJS_OBJECT_MAX         (NJS_OBJECT_JSON + 1)
 };
 
 
-enum njs_function_e {
-    NJS_FUNCTION_EVAL = 0,
-    NJS_FUNCTION_TO_STRING,
-    NJS_FUNCTION_IS_NAN,
-    NJS_FUNCTION_IS_FINITE,
-    NJS_FUNCTION_PARSE_INT,
-    NJS_FUNCTION_PARSE_FLOAT,
-    NJS_FUNCTION_STRING_ENCODE_URI,
-    NJS_FUNCTION_STRING_ENCODE_URI_COMPONENT,
-    NJS_FUNCTION_STRING_DECODE_URI,
-    NJS_FUNCTION_STRING_DECODE_URI_COMPONENT,
-    NJS_FUNCTION_REQUIRE,
-    NJS_FUNCTION_SET_TIMEOUT,
-    NJS_FUNCTION_SET_IMMEDIATE,
-    NJS_FUNCTION_CLEAR_TIMEOUT,
-#define NJS_FUNCTION_MAX       (NJS_FUNCTION_CLEAR_TIMEOUT + 1)
-};
-
-
 #define njs_scope_index(value, type)                                          \
     ((njs_index_t) (((value) << NJS_SCOPE_SHIFT) | (type)))
 
@@ -329,7 +311,6 @@ struct njs_vm_shared_s {
 
     njs_object_t             string_object;
     njs_object_t             objects[NJS_OBJECT_MAX];
-    njs_function_t           functions[NJS_FUNCTION_MAX];
 
     /*
      * The prototypes and constructors arrays must be togther because they are
index 65051bf4e0b5bcf036a56f82c96eef255f9e02dc..77972c4f30ddfa46b202e5fac4de240da09ad2a3 100644 (file)
@@ -12137,6 +12137,13 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("eval()"),
       njs_str("InternalError: Not implemented") },
 
+    { njs_str("delete this.eval; eval"),
+      njs_str("ReferenceError: \"eval\" is not defined in 1") },
+
+    { njs_str("var d = Object.getOwnPropertyDescriptor(this, 'eval');"
+              "d.writable && !d.enumerable && d.configurable"),
+      njs_str("true") },
+
     /* Math. */
 
     { njs_str("Math.PI"),
@@ -14025,6 +14032,18 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("require()"),
       njs_str("TypeError: missing path") },
 
+    { njs_str("require.length"),
+      njs_str("1") },
+
+    { njs_str("require.name"),
+      njs_str("require") },
+
+    { njs_str("typeof require"),
+      njs_str("function") },
+
+    { njs_str("require.hasOwnProperty('length')"),
+      njs_str("true") },
+
     { njs_str("var fs = require('fs'); typeof fs"),
       njs_str("object") },
 
@@ -14474,6 +14493,18 @@ static njs_unit_test_t  njs_shared_test[] =
 
     { njs_str("import cr from 'crypto'; cr.createHash('md5')"),
       njs_str("[object Hash]") },
+
+    { njs_str("isFinite()"),
+      njs_str("false") },
+
+    { njs_str("Number.isFinite(function(){})"),
+      njs_str("false") },
+
+    { njs_str("isFin()"),
+      njs_str("ReferenceError: \"isFin\" is not defined in 1") },
+
+    { njs_str("isNaN(function(){})"),
+      njs_str("true") },
 };