[njs] Moving global functions to global object.

Dmitry Volyntsev xeioex at nginx.com
Fri Oct 18 14:00:10 UTC 2019


details:   https://hg.nginx.org/njs/rev/c86310ffb38a
branches:  
changeset: 1189:c86310ffb38a
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Oct 18 16:38:55 2019 +0300
description:
Moving global functions to global object.

This closes #132 issue on Github.

diffstat:

 src/njs_builtin.c           |  217 +++++++++++++++++++++++++++----------------
 src/njs_generator.c         |   15 +--
 src/njs_lexer.h             |   18 +---
 src/njs_lexer_keyword.c     |   19 +---
 src/njs_module.c            |    7 -
 src/njs_module.h            |    1 -
 src/njs_number.c            |   80 ----------------
 src/njs_number.h            |    5 -
 src/njs_parser.c            |    4 +-
 src/njs_parser.h            |    4 +
 src/njs_parser_expression.c |    2 +-
 src/njs_parser_terminal.c   |   27 +-----
 src/njs_string.c            |  135 ---------------------------
 src/njs_string.h            |    6 -
 src/njs_timer.c             |   20 ----
 src/njs_timer.h             |    4 -
 src/njs_vm.h                |   21 +----
 src/test/njs_unit_test.c    |   31 ++++++
 18 files changed, 181 insertions(+), 435 deletions(-)

diffs (893 lines):

diff -r 5f192dbb694e -r c86310ffb38a src/njs_builtin.c
--- a/src/njs_builtin.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_builtin.c	Fri Oct 18 16:38:55 2019 +0300
@@ -93,47 +93,6 @@ const njs_object_init_t  *njs_constructo
 };
 
 
-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_glob
         .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)
 };
diff -r 5f192dbb694e -r c86310ffb38a src/njs_generator.c
--- a/src/njs_generator.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_generator.c	Fri Oct 18 16:38:55 2019 +0300
@@ -441,6 +441,7 @@ njs_generate(njs_vm_t *vm, njs_generator
 
     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
     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:
diff -r 5f192dbb694e -r c86310ffb38a src/njs_lexer.h
--- a/src/njs_lexer.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_lexer.h	Fri Oct 18 16:38:55 2019 +0300
@@ -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,
 
diff -r 5f192dbb694e -r c86310ffb38a src/njs_lexer_keyword.c
--- a/src/njs_lexer_keyword.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_lexer_keyword.c	Fri Oct 18 16:38:55 2019 +0300
@@ -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 },
diff -r 5f192dbb694e -r c86310ffb38a src/njs_module.c
--- a/src/njs_module.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_module.c	Fri Oct 18 16:38:55 2019 +0300
@@ -541,10 +541,3 @@ njs_module_require(njs_vm_t *vm, njs_val
 
     return NJS_ERROR;
 }
-
-
-const njs_object_init_t  njs_require_function_init = {
-    njs_str("require"),
-    NULL,
-    0,
-};
diff -r 5f192dbb694e -r c86310ffb38a src/njs_module.h
--- a/src/njs_module.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_module.h	Fri Oct 18 16:38:55 2019 +0300
@@ -24,7 +24,6 @@ njs_int_t njs_module_require(njs_vm_t *v
 
 
 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_ */
diff -r 5f192dbb694e -r c86310ffb38a src/njs_number.c
--- a/src/njs_number.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_number.c	Fri Oct 18 16:38:55 2019 +0300
@@ -1062,33 +1062,6 @@ njs_number_parse_float(njs_vm_t *vm, njs
 }
 
 
-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_f
     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),
-};
diff -r 5f192dbb694e -r c86310ffb38a src/njs_number.h
--- a/src/njs_number.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_number.h	Fri Oct 18 16:38:55 2019 +0300
@@ -186,10 +186,5 @@ njs_uint32_to_string(njs_value_t *value,
 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_ */
diff -r 5f192dbb694e -r c86310ffb38a src/njs_parser.c
--- a/src/njs_parser.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_parser.c	Fri Oct 18 16:38:55 2019 +0300
@@ -636,7 +636,7 @@ njs_parser_function_declaration(njs_vm_t
     }
 
     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, n
         }
 
         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));
diff -r 5f192dbb694e -r c86310ffb38a src/njs_parser.h
--- a/src/njs_parser.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_parser.h	Fri Oct 18 16:38:55 2019 +0300
@@ -126,6 +126,10 @@ void njs_parser_node_error(njs_vm_t *vm,
 #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)
 
diff -r 5f192dbb694e -r c86310ffb38a src/njs_parser_expression.c
--- a/src/njs_parser_expression.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_parser_expression.c	Fri Oct 18 16:38:55 2019 +0300
@@ -310,7 +310,7 @@ njs_parser_assignment_expression(njs_vm_
         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"
diff -r 5f192dbb694e -r c86310ffb38a src/njs_parser_terminal.c
--- a/src/njs_parser_terminal.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_parser_terminal.c	Fri Oct 18 16:38:55 2019 +0300
@@ -344,27 +344,6 @@ njs_parser_reference(njs_vm_t *vm, njs_p
         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_p
         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_par
         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;
     }
diff -r 5f192dbb694e -r c86310ffb38a src/njs_string.c
--- a/src/njs_string.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_string.c	Fri Oct 18 16:38:55 2019 +0300
@@ -4857,138 +4857,3 @@ njs_value_index(njs_vm_t *vm, const njs_
 
     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),
-};
diff -r 5f192dbb694e -r c86310ffb38a src/njs_string.h
--- a/src/njs_string.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_string.h	Fri Oct 18 16:38:55 2019 +0300
@@ -198,11 +198,5 @@ extern const njs_object_init_t  njs_stri
 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_ */
diff -r 5f192dbb694e -r c86310ffb38a src/njs_timer.c
--- a/src/njs_timer.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_timer.c	Fri Oct 18 16:38:55 2019 +0300
@@ -127,23 +127,3 @@ njs_clear_timeout(njs_vm_t *vm, njs_valu
 
     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,
-};
diff -r 5f192dbb694e -r c86310ffb38a src/njs_timer.h
--- a/src/njs_timer.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_timer.h	Fri Oct 18 16:38:55 2019 +0300
@@ -16,8 +16,4 @@ njs_int_t njs_clear_timeout(njs_vm_t *vm
     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_ */
diff -r 5f192dbb694e -r c86310ffb38a src/njs_vm.h
--- a/src/njs_vm.h	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/njs_vm.h	Fri Oct 18 16:38:55 2019 +0300
@@ -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
diff -r 5f192dbb694e -r c86310ffb38a src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Oct 18 16:34:50 2019 +0300
+++ b/src/test/njs_unit_test.c	Fri Oct 18 16:38:55 2019 +0300
@@ -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") },
 };
 
 


More information about the nginx-devel mailing list