Title: [198652] trunk
Revision
198652
Author
[email protected]
Date
2016-03-24 16:18:49 -0700 (Thu, 24 Mar 2016)

Log Message

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.

Source/_javascript_Core:

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:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (198651 => 198652)


--- trunk/LayoutTests/ChangeLog	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/LayoutTests/ChangeLog	2016-03-24 23:18:49 UTC (rev 198652)
@@ -1,3 +1,12 @@
+2016-03-24  Saam barati  <[email protected]>
+
+        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  <[email protected]>
 
         Unreviewed, rolling out r198627.

Modified: trunk/LayoutTests/js/string-includes-expected.txt (198651 => 198652)


--- trunk/LayoutTests/js/string-includes-expected.txt	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/LayoutTests/js/string-includes-expected.txt	2016-03-24 23:18:49 UTC (rev 198652)
@@ -118,9 +118,9 @@
 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.

Modified: trunk/Source/_javascript_Core/ChangeLog (198651 => 198652)


--- trunk/Source/_javascript_Core/ChangeLog	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-03-24 23:18:49 UTC (rev 198652)
@@ -1,5 +1,38 @@
 2016-03-24  Saam barati  <[email protected]>
 
+        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  <[email protected]>
+
         Web Inspector: Separate Debugger enable state from the debugger breakpoints enabled state
         https://bugs.webkit.org/show_bug.cgi?id=152193
         <rdar://problem/23867520>

Modified: trunk/Source/_javascript_Core/runtime/RegExpConstructor.h (198651 => 198652)


--- trunk/Source/_javascript_Core/runtime/RegExpConstructor.h	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/Source/_javascript_Core/runtime/RegExpConstructor.h	2016-03-24 23:18:49 UTC (rev 198652)
@@ -131,6 +131,21 @@
     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

Modified: trunk/Source/_javascript_Core/runtime/StringPrototype.cpp (198651 => 198652)


--- trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2016-03-24 23:18:49 UTC (rev 198652)
@@ -1840,8 +1840,12 @@
         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 @@
         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 @@
         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())

Added: trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js (0 => 198652)


--- trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.endsWith.js	2016-03-24 23:18:49 UTC (rev 198652)
@@ -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");

Added: trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js (0 => 198652)


--- trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.includes.js	2016-03-24 23:18:49 UTC (rev 198652)
@@ -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");

Added: trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js (0 => 198652)


--- trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/es6/well-known_symbols_Symbol.match_String.prototype.startsWith.js	2016-03-24 23:18:49 UTC (rev 198652)
@@ -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");

Modified: trunk/Source/_javascript_Core/tests/es6.yaml (198651 => 198652)


--- trunk/Source/_javascript_Core/tests/es6.yaml	2016-03-24 23:16:53 UTC (rev 198651)
+++ trunk/Source/_javascript_Core/tests/es6.yaml	2016-03-24 23:18:49 UTC (rev 198652)
@@ -1220,6 +1220,12 @@
   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

Added: trunk/Source/_javascript_Core/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js (0 => 198652)


--- trunk/Source/_javascript_Core/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/string-prototype-methods-endsWith-startsWith-includes-correctness.js	2016-03-24 23:18:49 UTC (rev 198652)
@@ -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");
+});
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to