ES6: Implement IsRegExp function and use where needed in String.prototype.* methods
authorsbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Mar 2016 23:18:49 +0000 (23:18 +0000)
committersbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Mar 2016 23:18:49 +0000 (23:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=155854

Reviewed by Mark Lam.

Source/JavaScriptCore:

This patch is a straight forward implementation of IsRegExp
in the ES6 spec:
https://tc39.github.io/ecma262/#sec-isregexp
We now use this IsRegExp function inside String.prototype.(startsWith | endsWith | includes)
as is dictated by the spec.

* runtime/RegExpConstructor.h:
(JSC::RegExpConstructor::recordMatch):
(JSC::isRegExp):
* runtime/StringPrototype.cpp:
(JSC::stringProtoFuncStartsWith):
(JSC::stringProtoFuncEndsWith):
(JSC::stringProtoFuncIncludes):
* tests/es6.yaml:
* tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js: Added.
(test):
* tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js: Added.
(test):
* tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js: Added.
(test):
* tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js: Added.
(assert):
(test):
(test.get let):
(get let):

LayoutTests:

* js/string-includes-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@198652 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/js/string-includes-expected.txt
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/RegExpConstructor.h
Source/JavaScriptCore/runtime/StringPrototype.cpp
Source/JavaScriptCore/tests/es6.yaml
Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js [new file with mode: 0644]
Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js [new file with mode: 0644]
Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js [new file with mode: 0644]
Source/JavaScriptCore/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js [new file with mode: 0644]

index c65adcc..a333d87 100644 (file)
@@ -1,3 +1,12 @@
+2016-03-24  Saam barati  <sbarati@apple.com>
+
+        ES6: Implement IsRegExp function and use where needed in String.prototype.* methods
+        https://bugs.webkit.org/show_bug.cgi?id=155854
+
+        Reviewed by Mark Lam.
+
+        * js/string-includes-expected.txt:
+
 2016-03-24  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r198627.
index fb77c16..42a7d83 100644 (file)
@@ -118,9 +118,9 @@ PASS 'abc'.endsWith('bc', 3) is true
 PASS (function() { var f = String.prototype.startsWith; (function() { f('a'); })(); })() threw exception TypeError: Type error.
 PASS (function() { var f = String.prototype.endsWith; (function() { f('a'); })(); })() threw exception TypeError: Type error.
 PASS (function() { var f = String.prototype.includes; (function() { f('a'); })(); })() threw exception TypeError: Type error.
-PASS 'foo bar'.startsWith(/w+/) threw exception TypeError: Type error.
-PASS 'foo bar'.endsWith(/w+/) threw exception TypeError: Type error.
-PASS 'foo bar'.includes(/w+/) threw exception TypeError: Type error.
+PASS 'foo bar'.startsWith(/w+/) threw exception TypeError: Argument to String.prototype.startsWith can not be a RegExp..
+PASS 'foo bar'.endsWith(/w+/) threw exception TypeError: Argument to String.prototype.endsWith can not be a RegExp..
+PASS 'foo bar'.includes(/w+/) threw exception TypeError: Argument to String.prototype.includes can not be a RegExp..
 PASS stringToSearchIn.startsWith(searchString, startOffset) is true
 PASS sideEffect == 'ABC' is true
 PASS stringToSearchIn.startsWith(searchString, startOffset) threw exception error.
index 0371b4a..8afe60f 100644 (file)
@@ -1,5 +1,38 @@
 2016-03-24  Saam barati  <sbarati@apple.com>
 
+        ES6: Implement IsRegExp function and use where needed in String.prototype.* methods
+        https://bugs.webkit.org/show_bug.cgi?id=155854
+
+        Reviewed by Mark Lam.
+
+        This patch is a straight forward implementation of IsRegExp
+        in the ES6 spec:
+        https://tc39.github.io/ecma262/#sec-isregexp
+        We now use this IsRegExp function inside String.prototype.(startsWith | endsWith | includes)
+        as is dictated by the spec.
+
+        * runtime/RegExpConstructor.h:
+        (JSC::RegExpConstructor::recordMatch):
+        (JSC::isRegExp):
+        * runtime/StringPrototype.cpp:
+        (JSC::stringProtoFuncStartsWith):
+        (JSC::stringProtoFuncEndsWith):
+        (JSC::stringProtoFuncIncludes):
+        * tests/es6.yaml:
+        * tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js: Added.
+        (test):
+        * tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js: Added.
+        (test):
+        * tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js: Added.
+        (test):
+        * tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js: Added.
+        (assert):
+        (test):
+        (test.get let):
+        (get let):
+
+2016-03-24  Saam barati  <sbarati@apple.com>
+
         Web Inspector: Separate Debugger enable state from the debugger breakpoints enabled state
         https://bugs.webkit.org/show_bug.cgi?id=152193
         <rdar://problem/23867520>
index 0699411..2e0a1d6 100644 (file)
@@ -131,6 +131,21 @@ ALWAYS_INLINE void RegExpConstructor::recordMatch(VM& vm, RegExp* regExp, JSStri
     m_cachedResult.record(vm, this, regExp, string, result);
 }
 
+ALWAYS_INLINE bool isRegExp(VM& vm, ExecState* exec, JSValue value)
+{
+    if (!value.isObject())
+        return false;
+
+    JSObject* object = asObject(value);
+    JSValue matchValue = object->get(exec, vm.propertyNames->matchSymbol);
+    if (vm.exception())
+        return false;
+    if (!matchValue.isUndefined())
+        return matchValue.toBoolean(exec);
+
+    return object->inherits(RegExpObject::info());
+}
+
 } // namespace JSC
 
 #endif // RegExpConstructor_h
index afb636f..d856ee0 100644 (file)
@@ -1840,8 +1840,12 @@ EncodedJSValue JSC_HOST_CALL stringProtoFuncStartsWith(ExecState* exec)
         return JSValue::encode(jsUndefined());
 
     JSValue a0 = exec->argument(0);
-    if (jsDynamicCast<RegExpObject*>(a0))
-        return throwVMTypeError(exec);
+    VM& vm = exec->vm();
+    bool isRegularExpression = isRegExp(vm, exec, a0);
+    if (vm.exception())
+        return JSValue::encode(JSValue());
+    if (isRegularExpression)
+        return throwVMTypeError(exec, "Argument to String.prototype.startsWith can not be a RegExp.");
 
     String searchString = a0.toString(exec)->value(exec);
     if (exec->hadException())
@@ -1872,8 +1876,12 @@ EncodedJSValue JSC_HOST_CALL stringProtoFuncEndsWith(ExecState* exec)
         return JSValue::encode(jsUndefined());
 
     JSValue a0 = exec->argument(0);
-    if (jsDynamicCast<RegExpObject*>(a0))
-        return throwVMTypeError(exec);
+    VM& vm = exec->vm();
+    bool isRegularExpression = isRegExp(vm, exec, a0);
+    if (vm.exception())
+        return JSValue::encode(JSValue());
+    if (isRegularExpression)
+        return throwVMTypeError(exec, "Argument to String.prototype.endsWith can not be a RegExp.");
 
     String searchString = a0.toString(exec)->value(exec);
     if (exec->hadException())
@@ -1905,8 +1913,12 @@ EncodedJSValue JSC_HOST_CALL stringProtoFuncIncludes(ExecState* exec)
         return JSValue::encode(jsUndefined());
 
     JSValue a0 = exec->argument(0);
-    if (jsDynamicCast<RegExpObject*>(a0))
-        return throwVMTypeError(exec);
+    VM& vm = exec->vm();
+    bool isRegularExpression = isRegExp(vm, exec, a0);
+    if (vm.exception())
+        return JSValue::encode(JSValue());
+    if (isRegularExpression)
+        return throwVMTypeError(exec, "Argument to String.prototype.includes can not be a RegExp.");
 
     String searchString = a0.toString(exec)->value(exec);
     if (exec->hadException())
index 6577730..3ef3db6 100644 (file)
   cmd: runES6 :normal
 - path: es6/well-known_symbols_Symbol.toStringTag_misc._built-ins.js
   cmd: runES6 :normal
+- path: es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js
+  cmd: runES6 :normal
+- path: es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js
+  cmd: runES6 :normal
+- path: es6/well-known_symbols_Symbol.match_String.prototype.includes.js
+  cmd: runES6 :normal
 # Late-stage proposals for a future ECMAScript standard
 # FIXME: move these to a new directory?
 - path: es6/Object_static_methods_Object.getOwnPropertyDescriptors.js
diff --git a/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js b/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js
new file mode 100644 (file)
index 0000000..a63b184
--- /dev/null
@@ -0,0 +1,12 @@
+function test() {
+    var re = /./;
+    try {
+        '/./'.endsWith(re);
+    } catch(e){
+        re[Symbol.match] = false;
+        return '/./'.endsWith(re);
+    }
+}
+
+if (!test())
+    throw new Error("Test failed");
diff --git a/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js b/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js
new file mode 100644 (file)
index 0000000..9e14d37
--- /dev/null
@@ -0,0 +1,12 @@
+function test() {
+    var re = /./;
+    try {
+        '/./'.includes(re);
+    } catch(e){
+        re[Symbol.match] = false;
+        return '/./'.includes(re);
+    }
+}
+
+if (!test())
+    throw new Error("Test failed");
diff --git a/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js b/Source/JavaScriptCore/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js
new file mode 100644 (file)
index 0000000..cec7f1c
--- /dev/null
@@ -0,0 +1,12 @@
+function test() {
+    var re = /./;
+    try {
+        '/./'.startsWith(re);
+    } catch(e){
+        re[Symbol.match] = false;
+        return '/./'.startsWith(re);
+    }
+}
+
+if (!test())
+    throw new Error("Test failed");
diff --git a/Source/JavaScriptCore/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js b/Source/JavaScriptCore/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js
new file mode 100644 (file)
index 0000000..fb25197
--- /dev/null
@@ -0,0 +1,180 @@
+function assert(b) {
+    if (!b)
+        throw new Error("Bad assertion!");
+}
+
+function test(f) {
+    for (let i = 0; i < 500; i++)
+        f();
+}
+
+test(function() {
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.endsWith(/foo/);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.endsWith can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+});
+
+test(function() {
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.startsWith(/foo/);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.startsWith can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+});
+
+test(function() {
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.includes(/foo/);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.includes can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.endsWith(proxy);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.endsWith can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+    assert(props.length === 1);
+    assert(props[0] === Symbol.match);
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.startsWith(proxy);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.startsWith can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+    assert(props.length === 1);
+    assert(props[0] === Symbol.match);
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "hello";
+    let threw = false;
+    try {
+        foo.includes(proxy);
+    } catch(e) {
+        assert(e.toString() === "TypeError: Argument to String.prototype.includes can not be a RegExp.");
+        threw = true;
+    }
+    assert(threw);
+    assert(props.length === 1);
+    assert(props[0] === Symbol.match);
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            if (prop === Symbol.match)
+                return undefined;
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "/foo/";
+    let threw = false;
+    let result = foo.includes(proxy);
+    assert(result);
+    assert(props.length === 5);
+    assert(props[0] === Symbol.match);
+    assert(props[1] === Symbol.toPrimitive);
+    assert(props[2] === "toString");
+    assert(props[3] === "source");
+    assert(props[4] === "flags");
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            if (prop === Symbol.match)
+                return undefined;
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "/foo/";
+    let threw = false;
+    let result = foo.startsWith(proxy);
+    assert(result);
+    assert(props.length === 5);
+    assert(props[0] === Symbol.match);
+    assert(props[1] === Symbol.toPrimitive);
+    assert(props[2] === "toString");
+    assert(props[3] === "source");
+    assert(props[4] === "flags");
+});
+
+test(function() {
+    let props = [];
+    let proxy = new Proxy(/foo/, {
+        get(theTarget, prop) {
+            props.push(prop);
+            if (prop === Symbol.match)
+                return undefined;
+            return theTarget[prop];
+        }
+    });
+
+    let foo = "/foo/";
+    let threw = false;
+    let result = foo.endsWith(proxy);
+    assert(result);
+    assert(props.length === 5);
+    assert(props[0] === Symbol.match);
+    assert(props[1] === Symbol.toPrimitive);
+    assert(props[2] === "toString");
+    assert(props[3] === "source");
+    assert(props[4] === "flags");
+});