[njs] RegExp.prototype.toString() should be applied only to RegExp.

Igor Sysoev igor at sysoev.ru
Mon Dec 5 15:17:47 UTC 2016


details:   http://hg.nginx.org/njs/rev/709ac03a4bfd
branches:  
changeset: 274:709ac03a4bfd
user:      Igor Sysoev <igor at sysoev.ru>
date:      Mon Dec 05 17:47:00 2016 +0300
description:
RegExp.prototype.toString() should be applied only to RegExp.

diffstat:

 njs/njs_regexp.c         |  19 ++++++++++++++-----
 njs/test/njs_unit_test.c |   3 +++
 2 files changed, 17 insertions(+), 5 deletions(-)

diffs (46 lines):

diff -r 15ec6a09cece -r 709ac03a4bfd njs/njs_regexp.c
--- a/njs/njs_regexp.c	Mon Dec 05 17:45:02 2016 +0300
+++ b/njs/njs_regexp.c	Mon Dec 05 17:47:00 2016 +0300
@@ -560,15 +560,24 @@ njs_regexp_prototype_to_string(njs_vm_t 
     u_char                *source;
     int32_t               length;
     uint32_t              size;
+    njs_value_t           *value;
     njs_regexp_pattern_t  *pattern;
 
-    pattern = args[0].data.u.regexp->pattern;
-    source = pattern->source;
+    value = &args[0];
 
-    size = strlen((char *) source);
-    length = nxt_utf8_length(source, size);
+    if (value->type == NJS_REGEXP) {
+        pattern = value->data.u.regexp->pattern;
+        source = pattern->source;
 
-    return njs_regexp_string_create(vm, &vm->retval, source, size, length);
+        size = strlen((char *) source);
+        length = nxt_utf8_length(source, size);
+
+        return njs_regexp_string_create(vm, &vm->retval, source, size, length);
+    }
+
+    vm->exception = &njs_exception_type_error;
+
+    return NXT_ERROR;
 }
 
 
diff -r 15ec6a09cece -r 709ac03a4bfd njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Dec 05 17:45:02 2016 +0300
+++ b/njs/test/njs_unit_test.c	Mon Dec 05 17:47:00 2016 +0300
@@ -4410,6 +4410,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var r = new RegExp('abc', 'i'); r.test('00ABC11')"),
       nxt_string("true") },
 
+    { nxt_string("[0].map(RegExp().toString)"),
+      nxt_string("TypeError") },
+
     /* Non-standard ECMA-262 features. */
 
     /* 0x10400 is not a surrogate pair of 0xD801 and 0xDC00. */


More information about the nginx-devel mailing list