]> git.kaiwu.me - njs.git/commitdiff
Global toString() function.
authorIgor Sysoev <igor@sysoev.ru>
Thu, 21 Apr 2016 12:57:05 +0000 (15:57 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Thu, 21 Apr 2016 12:57:05 +0000 (15:57 +0300)
njs/njs_builtin.c
njs/njs_generator.c
njs/njs_lexer_keyword.c
njs/njs_parser.c
njs/njs_parser.h
njs/njs_vm.h
njs/test/njs_unit_test.c

index 3354604f30d2a8b372dad796ffc68b63e627ed9c..7e3257b684e98649e75883e4dd1c51cff47551d2 100644 (file)
@@ -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;
index b7eb4ad512d7f2fb46b1ea6222f1e27a408f39bf..a74fef2ded6c7aa147795a6d34cc649b04eaaefd 100644 (file)
@@ -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)) {
index 87140e253eb1dc6f8d71e55dc2a66e8ab6334181..4dc327160b00b21ae5e570f3d525d748fa5d7505 100644 (file)
@@ -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. */
 
index f277aea82f8b392a14d85ba152243d4ec386a433..ff2bfb2a64c86b5ae616912ceee847c264971782 100644 (file)
@@ -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:
index 976d2ab5812c3b2acd3a4d4cd7a01fa6b96b8d61..99759e9df28323a632a1f14151778def53f78ae5 100644 (file)
@@ -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;
index 65a2c8494b3850136294d7317216075e46941412..e9e226f92bd22a668381b43f984053f9c1837dde 100644 (file)
@@ -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)
 };
 
 
index 052abebd72cfd92d08475d503fd5c44e32b02cb0..cbb7edaf5f6e26046bf7344b06ebe1cbdb0b284c 100644 (file)
@@ -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]") },