[njs] Global toString() function.

Igor Sysoev igor at sysoev.ru
Wed Jun 1 12:50:28 UTC 2016


details:   http://hg.nginx.org/njs/rev/a69b4eaebdcf
branches:  
changeset: 106:a69b4eaebdcf
user:      Igor Sysoev <igor at sysoev.ru>
date:      Thu Apr 21 15:57:05 2016 +0300
description:
Global toString() function.

diffstat:

 njs/njs_builtin.c        |  16 ++++++++++------
 njs/njs_generator.c      |   6 +-----
 njs/njs_lexer_keyword.c  |   1 +
 njs/njs_parser.c         |   1 +
 njs/njs_parser.h         |   1 +
 njs/njs_vm.h             |   3 ++-
 njs/test/njs_unit_test.c |  14 ++++++++++++++
 7 files changed, 30 insertions(+), 12 deletions(-)

diffs (130 lines):

diff -r 6fa680a54811 -r a69b4eaebdcf njs/njs_builtin.c
--- a/njs/njs_builtin.c	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_builtin.c	Thu Apr 21 15:57:05 2016 +0300
@@ -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 -r 6fa680a54811 -r a69b4eaebdcf njs/njs_generator.c
--- a/njs/njs_generator.c	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_generator.c	Thu Apr 21 15:57:05 2016 +0300
@@ -294,6 +294,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t
 
     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,
     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 -r 6fa680a54811 -r a69b4eaebdcf njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_lexer_keyword.c	Thu Apr 21 15:57:05 2016 +0300
@@ -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 -r 6fa680a54811 -r a69b4eaebdcf njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_parser.c	Thu Apr 21 15:57:05 2016 +0300
@@ -1644,6 +1644,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
         break;
 
     case NJS_TOKEN_EVAL:
+    case NJS_TOKEN_TO_STRING:
         return njs_parser_builtin_function(vm, parser, node);
 
     default:
diff -r 6fa680a54811 -r a69b4eaebdcf njs/njs_parser.h
--- a/njs/njs_parser.h	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_parser.h	Thu Apr 21 15:57:05 2016 +0300
@@ -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 -r 6fa680a54811 -r a69b4eaebdcf njs/njs_vm.h
--- a/njs/njs_vm.h	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/njs_vm.h	Thu Apr 21 15:57:05 2016 +0300
@@ -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 -r 6fa680a54811 -r a69b4eaebdcf njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Apr 20 18:30:31 2016 +0300
+++ b/njs/test/njs_unit_test.c	Thu Apr 21 15:57:05 2016 +0300
@@ -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]") },
 



More information about the nginx-devel mailing list