Title: [227779] trunk
Revision
227779
Author
[email protected]
Date
2018-01-30 04:06:24 -0800 (Tue, 30 Jan 2018)

Log Message

[JSC] Implement trimStart and trimEnd
https://bugs.webkit.org/show_bug.cgi?id=182233

Reviewed by Mark Lam.

JSTests:

* stress/trim.js: Added.
(shouldBe):
(startTest):
(endTest):
(trimTest):

Source/_javascript_Core:

String.prototype.{trimStart,trimEnd} are now stage 3[1].
String.prototype.{trimLeft,trimRight} are alias to these functions.

We rename these functions to trimStart and trimEnd, and put them as
trimLeft and trimRight too.

[1]: https://tc39.github.io/proposal-string-left-right-trim/

* runtime/StringPrototype.cpp:
(JSC::StringPrototype::finishCreation):
(JSC::trimString):
(JSC::stringProtoFuncTrim):
(JSC::stringProtoFuncTrimStart):
(JSC::stringProtoFuncTrimEnd):
(JSC::stringProtoFuncTrimLeft): Deleted.
(JSC::stringProtoFuncTrimRight): Deleted.

LayoutTests:

* js/Object-getOwnPropertyNames-expected.txt:
* js/script-tests/Object-getOwnPropertyNames.js:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (227778 => 227779)


--- trunk/JSTests/ChangeLog	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/JSTests/ChangeLog	2018-01-30 12:06:24 UTC (rev 227779)
@@ -1,5 +1,18 @@
 2018-01-29  Yusuke Suzuki  <[email protected]>
 
+        [JSC] Implement trimStart and trimEnd
+        https://bugs.webkit.org/show_bug.cgi?id=182233
+
+        Reviewed by Mark Lam.
+
+        * stress/trim.js: Added.
+        (shouldBe):
+        (startTest):
+        (endTest):
+        (trimTest):
+
+2018-01-29  Yusuke Suzuki  <[email protected]>
+
         [JSC] Relax line terminators in String to make JSON subset of JS
         https://bugs.webkit.org/show_bug.cgi?id=182232
 

Added: trunk/JSTests/stress/trim.js (0 => 227779)


--- trunk/JSTests/stress/trim.js	                        (rev 0)
+++ trunk/JSTests/stress/trim.js	2018-01-30 12:06:24 UTC (rev 227779)
@@ -0,0 +1,53 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function startTest(string, expected) {
+    shouldBe(string.trimStart(), expected);
+    shouldBe(string.trimLeft(), expected);
+}
+
+function endTest(string, expected) {
+    shouldBe(string.trimEnd(), expected);
+    shouldBe(string.trimRight(), expected);
+}
+
+function trimTest(string, expected) {
+    shouldBe(string.trim(), expected);
+}
+
+startTest(`    Hello   `, `Hello   `);
+endTest(`    Hello   `, `    Hello`);
+trimTest(`    Hello   `, `Hello`);
+
+startTest(`    日本語   `, `日本語   `);
+endTest(`    日本語   `, `    日本語`);
+trimTest(`    日本語   `, `日本語`);
+
+startTest(`Hello`, `Hello`);
+endTest(`Hello`, `Hello`);
+trimTest(`Hello`, `Hello`);
+
+startTest(`日本語`, `日本語`);
+endTest(`日本語`, `日本語`);
+trimTest(`日本語`, `日本語`);
+
+startTest(``, ``);
+endTest(``, ``);
+trimTest(``, ``);
+
+startTest(`    `, ``);
+endTest(`    `, ``);
+trimTest(`    `, ``);
+
+startTest(`    A`, `A`);
+endTest(`    A`, `    A`);
+trimTest(`    A`, `A`);
+
+startTest(`A    `, `A    `);
+endTest(`A    `, `A`);
+trimTest(`A    `, `A`);
+
+shouldBe(String.prototype.trimStart, String.prototype.trimLeft);
+shouldBe(String.prototype.trimEnd, String.prototype.trimRight);

Modified: trunk/LayoutTests/ChangeLog (227778 => 227779)


--- trunk/LayoutTests/ChangeLog	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/LayoutTests/ChangeLog	2018-01-30 12:06:24 UTC (rev 227779)
@@ -1,5 +1,15 @@
 2018-01-29  Yusuke Suzuki  <[email protected]>
 
+        [JSC] Implement trimStart and trimEnd
+        https://bugs.webkit.org/show_bug.cgi?id=182233
+
+        Reviewed by Mark Lam.
+
+        * js/Object-getOwnPropertyNames-expected.txt:
+        * js/script-tests/Object-getOwnPropertyNames.js:
+
+2018-01-29  Yusuke Suzuki  <[email protected]>
+
         [JSC] Relax line terminators in String to make JSON subset of JS
         https://bugs.webkit.org/show_bug.cgi?id=182232
 

Modified: trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt (227778 => 227779)


--- trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt	2018-01-30 12:06:24 UTC (rev 227779)
@@ -49,7 +49,7 @@
 PASS getSortedOwnPropertyNames(Array) is ['from', 'isArray', 'length', 'name', 'of', 'prototype']
 PASS getSortedOwnPropertyNames(Array.prototype) is ['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']
 PASS getSortedOwnPropertyNames(String) is ['fromCharCode', 'fromCodePoint', 'length', 'name', 'prototype', 'raw']
-PASS getSortedOwnPropertyNames(String.prototype) is ['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'valueOf']
+PASS getSortedOwnPropertyNames(String.prototype) is ['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimEnd', 'trimLeft', 'trimRight', 'trimStart', 'valueOf']
 PASS getSortedOwnPropertyNames(Boolean) is ['length', 'name', 'prototype']
 PASS getSortedOwnPropertyNames(Boolean.prototype) is ['constructor', 'toString', 'valueOf']
 PASS getSortedOwnPropertyNames(Number) is ['EPSILON', 'MAX_SAFE_INTEGER', 'MAX_VALUE', 'MIN_SAFE_INTEGER', 'MIN_VALUE', 'NEGATIVE_INFINITY', 'NaN', 'POSITIVE_INFINITY', 'isFinite', 'isInteger', 'isNaN', 'isSafeInteger', 'length', 'name', 'parseFloat', 'parseInt', 'prototype']

Modified: trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js (227778 => 227779)


--- trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js	2018-01-30 12:06:24 UTC (rev 227779)
@@ -58,7 +58,7 @@
     "Array": "['from', 'isArray', 'length', 'name', 'of', 'prototype']",
     "Array.prototype": "['concat', 'constructor', 'copyWithin', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift', 'values']",
     "String": "['fromCharCode', 'fromCodePoint', 'length', 'name', 'prototype', 'raw']",
-    "String.prototype": "['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'valueOf']",
+    "String.prototype": "['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'codePointAt', 'concat', 'constructor', 'endsWith', 'fixed', 'fontcolor', 'fontsize', 'includes', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'search', 'slice', 'small', 'split', 'startsWith', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimEnd', 'trimLeft', 'trimRight', 'trimStart', 'valueOf']",
     "Boolean": "['length', 'name', 'prototype']",
     "Boolean.prototype": "['constructor', 'toString', 'valueOf']",
     "Number": "['EPSILON', 'MAX_SAFE_INTEGER', 'MAX_VALUE', 'MIN_SAFE_INTEGER', 'MIN_VALUE', 'NEGATIVE_INFINITY', 'NaN', 'POSITIVE_INFINITY', 'isFinite', 'isInteger', 'isNaN', 'isSafeInteger', 'length', 'name', 'parseFloat', 'parseInt', 'prototype']",

Modified: trunk/Source/_javascript_Core/ChangeLog (227778 => 227779)


--- trunk/Source/_javascript_Core/ChangeLog	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/Source/_javascript_Core/ChangeLog	2018-01-30 12:06:24 UTC (rev 227779)
@@ -1,5 +1,29 @@
 2018-01-29  Yusuke Suzuki  <[email protected]>
 
+        [JSC] Implement trimStart and trimEnd
+        https://bugs.webkit.org/show_bug.cgi?id=182233
+
+        Reviewed by Mark Lam.
+
+        String.prototype.{trimStart,trimEnd} are now stage 3[1].
+        String.prototype.{trimLeft,trimRight} are alias to these functions.
+
+        We rename these functions to trimStart and trimEnd, and put them as
+        trimLeft and trimRight too.
+
+        [1]: https://tc39.github.io/proposal-string-left-right-trim/
+
+        * runtime/StringPrototype.cpp:
+        (JSC::StringPrototype::finishCreation):
+        (JSC::trimString):
+        (JSC::stringProtoFuncTrim):
+        (JSC::stringProtoFuncTrimStart):
+        (JSC::stringProtoFuncTrimEnd):
+        (JSC::stringProtoFuncTrimLeft): Deleted.
+        (JSC::stringProtoFuncTrimRight): Deleted.
+
+2018-01-29  Yusuke Suzuki  <[email protected]>
+
         [JSC] Relax line terminators in String to make JSON subset of JS
         https://bugs.webkit.org/show_bug.cgi?id=182232
 

Modified: trunk/Source/_javascript_Core/runtime/StringPrototype.cpp (227778 => 227779)


--- trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2018-01-30 11:37:12 UTC (rev 227778)
+++ trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2018-01-30 12:06:24 UTC (rev 227779)
@@ -76,8 +76,8 @@
 EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleLowerCase(ExecState*);
 EncodedJSValue JSC_HOST_CALL stringProtoFuncToLocaleUpperCase(ExecState*);
 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState*);
-EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState*);
-EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState*);
+EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimStart(ExecState*);
+EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimEnd(ExecState*);
 EncodedJSValue JSC_HOST_CALL stringProtoFuncStartsWith(ExecState*);
 EncodedJSValue JSC_HOST_CALL stringProtoFuncEndsWith(ExecState*);
 EncodedJSValue JSC_HOST_CALL stringProtoFuncIncludes(ExecState*);
@@ -153,8 +153,6 @@
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("toLocaleUpperCase", stringProtoFuncToUpperCase, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
 #endif
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("trim", stringProtoFuncTrim, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("trimLeft", stringProtoFuncTrimLeft, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("trimRight", stringProtoFuncTrimRight, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("startsWith", stringProtoFuncStartsWith, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("endsWith", stringProtoFuncEndsWith, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("includes", stringProtoFuncIncludes, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
@@ -161,6 +159,13 @@
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("normalize", stringProtoFuncNormalize, static_cast<unsigned>(PropertyAttribute::DontEnum), 0);
     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().charCodeAtPrivateName(), stringProtoFuncCharCodeAt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, CharCodeAtIntrinsic);
 
+    JSFunction* trimStartFunction = JSFunction::create(vm, globalObject, 0, ASCIILiteral("trimStart"), stringProtoFuncTrimStart, NoIntrinsic);
+    JSFunction* trimEndFunction = JSFunction::create(vm, globalObject, 0, ASCIILiteral("trimEnd"), stringProtoFuncTrimEnd, NoIntrinsic);
+    putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimStart"), trimStartFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
+    putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimLeft"), trimStartFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
+    putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimEnd"), trimEndFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
+    putDirectWithoutTransition(vm, Identifier::fromString(&vm, "trimRight"), trimEndFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
+
     JSFunction* iteratorFunction = JSFunction::create(vm, globalObject, 0, ASCIILiteral("[Symbol.iterator]"), stringProtoFuncIterator, NoIntrinsic);
     putDirectWithoutTransition(vm, vm.propertyNames->iteratorSymbol, iteratorFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
 
@@ -1669,8 +1674,8 @@
 #endif // ENABLE(INTL)
 
 enum {
-    TrimLeft = 1,
-    TrimRight = 2
+    TrimStart = 1,
+    TrimEnd = 2
 };
 
 static inline JSValue trimString(ExecState* exec, JSValue thisValue, int trimKind)
@@ -1684,12 +1689,12 @@
     RETURN_IF_EXCEPTION(scope, { });
 
     unsigned left = 0;
-    if (trimKind & TrimLeft) {
+    if (trimKind & TrimStart) {
         while (left < str.length() && isStrWhiteSpace(str[left]))
             left++;
     }
     unsigned right = str.length();
-    if (trimKind & TrimRight) {
+    if (trimKind & TrimEnd) {
         while (right > left && isStrWhiteSpace(str[right - 1]))
             right--;
     }
@@ -1705,19 +1710,19 @@
 EncodedJSValue JSC_HOST_CALL stringProtoFuncTrim(ExecState* exec)
 {
     JSValue thisValue = exec->thisValue();
-    return JSValue::encode(trimString(exec, thisValue, TrimLeft | TrimRight));
+    return JSValue::encode(trimString(exec, thisValue, TrimStart | TrimEnd));
 }
 
-EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimLeft(ExecState* exec)
+EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimStart(ExecState* exec)
 {
     JSValue thisValue = exec->thisValue();
-    return JSValue::encode(trimString(exec, thisValue, TrimLeft));
+    return JSValue::encode(trimString(exec, thisValue, TrimStart));
 }
 
-EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimRight(ExecState* exec)
+EncodedJSValue JSC_HOST_CALL stringProtoFuncTrimEnd(ExecState* exec)
 {
     JSValue thisValue = exec->thisValue();
-    return JSValue::encode(trimString(exec, thisValue, TrimRight));
+    return JSValue::encode(trimString(exec, thisValue, TrimEnd));
 }
 
 static inline unsigned clampAndTruncateToUnsigned(double value, unsigned min, unsigned max)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to