From: Igor Sysoev Date: Thu, 21 Apr 2016 12:57:05 +0000 (+0300) Subject: Global toString() function. X-Git-Tag: 0.1.0~26 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=9770c07e0a3d5e6503537509ac10d943635881e5;p=njs.git Global toString() function. --- diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 3354604f..7e3257b6 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -80,11 +80,13 @@ njs_builtin_objects_create(njs_vm_t *vm) static const njs_object_init_t *function_init[] = { &njs_eval_function_init, + NULL, }; static const njs_function_init_t native_functions[] = { /* SunC does not allow empty array initialization. */ - { njs_eval_function, { 0 } }, + { njs_eval_function, { 0 } }, + { njs_object_prototype_to_string, { 0 } }, }; static const njs_object_prop_t null_proto_property = { @@ -127,11 +129,13 @@ njs_builtin_objects_create(njs_vm_t *vm) functions = vm->shared->functions; for (i = NJS_FUNCTION_EVAL; i < NJS_FUNCTION_MAX; i++) { - ret = njs_object_hash_create(vm, &functions[i].object.shared_hash, - function_init[i]->properties, - function_init[i]->items); - if (nxt_slow_path(ret != NXT_OK)) { - return NXT_ERROR; + if (function_init[i] != NULL) { + ret = njs_object_hash_create(vm, &functions[i].object.shared_hash, + function_init[i]->properties, + function_init[i]->items); + if (nxt_slow_path(ret != NXT_OK)) { + return NXT_ERROR; + } } functions[i].object.shared = 1; diff --git a/njs/njs_generator.c b/njs/njs_generator.c index b7eb4ad5..a74fef2d 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -294,6 +294,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node) case NJS_TOKEN_MATH: case NJS_TOKEN_EVAL: + case NJS_TOKEN_TO_STRING: return njs_generate_builtin_object(vm, parser, node); case NJS_TOKEN_FUNCTION: @@ -2050,11 +2051,6 @@ njs_generate_function_call(njs_vm_t *vm, njs_parser_t *parser, func->code.ctor = node->ctor; func->name = name->index; - ret = njs_generator_node_index_release(vm, parser, name); - if (nxt_slow_path(ret != NXT_OK)) { - return ret; - } - ret = njs_generate_call(vm, parser, node); if (nxt_fast_path(ret >= 0)) { diff --git a/njs/njs_lexer_keyword.c b/njs/njs_lexer_keyword.c index 87140e25..4dc32716 100644 --- a/njs/njs_lexer_keyword.c +++ b/njs/njs_lexer_keyword.c @@ -88,6 +88,7 @@ static const njs_keyword_t njs_keywords[] = { { nxt_string("Date"), NJS_TOKEN_DATE_CONSTRUCTOR, 0 }, { nxt_string("eval"), NJS_TOKEN_EVAL, 0 }, + { nxt_string("toString"), NJS_TOKEN_TO_STRING, 0 }, /* Reserved words. */ diff --git a/njs/njs_parser.c b/njs/njs_parser.c index f277aea8..ff2bfb2a 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -1644,6 +1644,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token) break; case NJS_TOKEN_EVAL: + case NJS_TOKEN_TO_STRING: return njs_parser_builtin_function(vm, parser, node); default: diff --git a/njs/njs_parser.h b/njs/njs_parser.h index 976d2ab5..99759e9d 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -175,6 +175,7 @@ typedef enum { #define NJS_TOKEN_FIRST_FUNCTION NJS_TOKEN_EVAL NJS_TOKEN_EVAL, + NJS_TOKEN_TO_STRING, NJS_TOKEN_RESERVED, } njs_token_t; diff --git a/njs/njs_vm.h b/njs/njs_vm.h index 65a2c849..e9e226f9 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -701,7 +701,8 @@ enum njs_object_e { enum njs_function_e { NJS_FUNCTION_EVAL = 0, -#define NJS_FUNCTION_MAX (NJS_FUNCTION_EVAL + 1) + NJS_FUNCTION_TO_STRING = 1, +#define NJS_FUNCTION_MAX (NJS_FUNCTION_TO_STRING + 1) }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 052abebd..cbb7edaf 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -3953,6 +3953,20 @@ static njs_unit_test_t njs_test[] = { nxt_string("/./.__proto__ === RegExp.prototype"), nxt_string("true") }, + { nxt_string("toString()"), + nxt_string("[object Undefined]") }, + + { nxt_string("toString() + Object.prototype.toString"), + nxt_string("[object Undefined][object Function]") }, + +#if 0 + { nxt_string("toString === Object.prototype.toString"), + nxt_string("true") }, + + { nxt_string("Object.prototype.toString.yes = 'OK'; toString.yes"), + nxt_string("OK") }, +#endif + { nxt_string("Object.prototype.toString.call()"), nxt_string("[object Undefined]") },