From 00196bcb0ba8c477177106097b9b49353051dc46 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 18 Oct 2019 16:38:55 +0300 Subject: [PATCH] Moving global functions to global object. This closes #132 issue on Github. --- 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(-) diff --git a/src/njs_builtin.c b/src/njs_builtin.c index 6358b509..38eca6c9 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -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) }; diff --git a/src/njs_generator.c b/src/njs_generator.c index c8690aab..139748f1 100644 --- a/src/njs_generator.c +++ b/src/njs_generator.c @@ -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: diff --git a/src/njs_lexer.h b/src/njs_lexer.h index 99319fd8..61775b61 100644 --- a/src/njs_lexer.h +++ b/src/njs_lexer.h @@ -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 --git a/src/njs_lexer_keyword.c b/src/njs_lexer_keyword.c index af6322ab..59d7224e 100644 --- a/src/njs_lexer_keyword.c +++ b/src/njs_lexer_keyword.c @@ -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 --git a/src/njs_module.c b/src/njs_module.c index ee72d8d4..68eef62e 100644 --- a/src/njs_module.c +++ b/src/njs_module.c @@ -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, -}; diff --git a/src/njs_module.h b/src/njs_module.h index 2fbc0c2e..955ef5b8 100644 --- a/src/njs_module.h +++ b/src/njs_module.h @@ -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_ */ diff --git a/src/njs_number.c b/src/njs_number.c index 0a4d6d99..95865811 100644 --- a/src/njs_number.c +++ b/src/njs_number.c @@ -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), -}; diff --git a/src/njs_number.h b/src/njs_number.h index 6317c31b..4046aa37 100644 --- a/src/njs_number.h +++ b/src/njs_number.h @@ -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_ */ diff --git a/src/njs_parser.c b/src/njs_parser.c index 750ebc04..c1620b7d 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -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)); diff --git a/src/njs_parser.h b/src/njs_parser.h index 5a33c860..066f96ad 100644 --- a/src/njs_parser.h +++ b/src/njs_parser.h @@ -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) diff --git a/src/njs_parser_expression.c b/src/njs_parser_expression.c index 5507c6be..cb1c247c 100644 --- a/src/njs_parser_expression.c +++ b/src/njs_parser_expression.c @@ -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" diff --git a/src/njs_parser_terminal.c b/src/njs_parser_terminal.c index d410b485..2dacd195 100644 --- a/src/njs_parser_terminal.c +++ b/src/njs_parser_terminal.c @@ -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; } diff --git a/src/njs_string.c b/src/njs_string.c index 04892eaa..7e59a971 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -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), -}; diff --git a/src/njs_string.h b/src/njs_string.h index d5cbe144..ef88a1ab 100644 --- a/src/njs_string.h +++ b/src/njs_string.h @@ -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_ */ diff --git a/src/njs_timer.c b/src/njs_timer.c index c985c86e..6c21ce05 100644 --- a/src/njs_timer.c +++ b/src/njs_timer.c @@ -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, -}; diff --git a/src/njs_timer.h b/src/njs_timer.h index c250b87b..ccb432e7 100644 --- a/src/njs_timer.h +++ b/src/njs_timer.h @@ -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_ */ diff --git a/src/njs_vm.h b/src/njs_vm.h index 63a49a14..a0e09838 100644 --- a/src/njs_vm.h +++ b/src/njs_vm.h @@ -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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 65051bf4..77972c4f 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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") }, }; -- 2.47.3