Skip to content

Commit 289d25c

Browse files
duongnhnCommit Bot
authored andcommitted
[builtins] Port StringTrim to Torque
StringPrototypeTrim, StringPrototypeTrimStart, StringPrototypeTrimEnd Bug: v8:8996 Change-Id: Ic1155b072d7de888f81a739236d224d00ae46c79 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2511529 Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com> Reviewed-by: Tobias Tebbi <tebbi@chromium.org> Cr-Commit-Position: refs/heads/master@{#70995}
1 parent d619296 commit 289d25c

9 files changed

Lines changed: 188 additions & 208 deletions

File tree

‎BUILD.gn‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,9 @@ torque_files = [
12211221
"src/builtins/string-replaceall.tq",
12221222
"src/builtins/string-slice.tq",
12231223
"src/builtins/string-startswith.tq",
1224-
"src/builtins/string-substring.tq",
12251224
"src/builtins/string-substr.tq",
1225+
"src/builtins/string-substring.tq",
1226+
"src/builtins/string-trim.tq",
12261227
"src/builtins/symbol.tq",
12271228
"src/builtins/torque-internal.tq",
12281229
"src/builtins/typed-array-createtypedarray.tq",

‎src/builtins/base.tq‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,10 @@ extern operator '==' macro
834834
ConstexprInt31Equal(constexpr int31, constexpr int31): constexpr bool;
835835
extern operator '!=' macro
836836
ConstexprInt31NotEqual(constexpr int31, constexpr int31): constexpr bool;
837+
extern operator '==' macro
838+
ConstexprUint32Equal(constexpr uint32, constexpr uint32): constexpr bool;
839+
extern operator '!=' macro
840+
ConstexprUint32NotEqual(constexpr uint32, constexpr uint32): constexpr bool;
837841
extern operator '>=' macro
838842
ConstexprInt31GreaterThanEqual(
839843
constexpr int31, constexpr int31): constexpr bool;

‎src/builtins/builtins-definitions.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,6 @@ namespace internal {
763763
TFJ(StringPrototypeSearch, 1, kReceiver, kRegexp) \
764764
/* ES6 #sec-string.prototype.split */ \
765765
TFJ(StringPrototypeSplit, kDontAdaptArgumentsSentinel) \
766-
TFJ(StringPrototypeTrim, kDontAdaptArgumentsSentinel) \
767-
TFJ(StringPrototypeTrimEnd, kDontAdaptArgumentsSentinel) \
768-
TFJ(StringPrototypeTrimStart, kDontAdaptArgumentsSentinel) \
769766
/* ES6 #sec-string.raw */ \
770767
CPP(StringRaw) \
771768
\

‎src/builtins/builtins-string-gen.cc‎

Lines changed: 0 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,178 +1803,6 @@ TF_BUILTIN(StringSubstring, StringBuiltinsAssembler) {
18031803
Return(SubString(string, from, to));
18041804
}
18051805

1806-
// ES6 #sec-string.prototype.trim
1807-
TF_BUILTIN(StringPrototypeTrim, StringTrimAssembler) {
1808-
TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1809-
UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1810-
auto context = Parameter<Context>(Descriptor::kContext);
1811-
1812-
Generate(String::kTrim, "String.prototype.trim", argc, context);
1813-
}
1814-
1815-
// https://github.com/tc39/proposal-string-left-right-trim
1816-
TF_BUILTIN(StringPrototypeTrimStart, StringTrimAssembler) {
1817-
TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1818-
UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1819-
auto context = Parameter<Context>(Descriptor::kContext);
1820-
1821-
Generate(String::kTrimStart, "String.prototype.trimLeft", argc, context);
1822-
}
1823-
1824-
// https://github.com/tc39/proposal-string-left-right-trim
1825-
TF_BUILTIN(StringPrototypeTrimEnd, StringTrimAssembler) {
1826-
TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1827-
UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1828-
auto context = Parameter<Context>(Descriptor::kContext);
1829-
1830-
Generate(String::kTrimEnd, "String.prototype.trimRight", argc, context);
1831-
}
1832-
1833-
void StringTrimAssembler::Generate(String::TrimMode mode,
1834-
const char* method_name, TNode<IntPtrT> argc,
1835-
TNode<Context> context) {
1836-
Label return_emptystring(this), if_runtime(this);
1837-
1838-
CodeStubArguments arguments(this, argc);
1839-
TNode<Object> receiver = arguments.GetReceiver();
1840-
1841-
// Check that {receiver} is coercible to Object and convert it to a String.
1842-
const TNode<String> string = ToThisString(context, receiver, method_name);
1843-
const TNode<IntPtrT> string_length = LoadStringLengthAsWord(string);
1844-
1845-
ToDirectStringAssembler to_direct(state(), string);
1846-
to_direct.TryToDirect(&if_runtime);
1847-
const TNode<RawPtrT> string_data = to_direct.PointerToData(&if_runtime);
1848-
const TNode<Int32T> instance_type = to_direct.instance_type();
1849-
const TNode<BoolT> is_stringonebyte =
1850-
IsOneByteStringInstanceType(instance_type);
1851-
const TNode<IntPtrT> string_data_offset = to_direct.offset();
1852-
1853-
TVARIABLE(IntPtrT, var_start, IntPtrConstant(0));
1854-
TVARIABLE(IntPtrT, var_end, IntPtrSub(string_length, IntPtrConstant(1)));
1855-
1856-
if (mode == String::kTrimStart || mode == String::kTrim) {
1857-
ScanForNonWhiteSpaceOrLineTerminator(string_data, string_data_offset,
1858-
is_stringonebyte, &var_start,
1859-
string_length, 1, &return_emptystring);
1860-
}
1861-
if (mode == String::kTrimEnd || mode == String::kTrim) {
1862-
ScanForNonWhiteSpaceOrLineTerminator(
1863-
string_data, string_data_offset, is_stringonebyte, &var_end,
1864-
IntPtrConstant(-1), -1, &return_emptystring);
1865-
}
1866-
1867-
arguments.PopAndReturn(
1868-
SubString(string, var_start.value(),
1869-
IntPtrAdd(var_end.value(), IntPtrConstant(1))));
1870-
1871-
BIND(&if_runtime);
1872-
arguments.PopAndReturn(
1873-
CallRuntime(Runtime::kStringTrim, context, string, SmiConstant(mode)));
1874-
1875-
BIND(&return_emptystring);
1876-
arguments.PopAndReturn(EmptyStringConstant());
1877-
}
1878-
1879-
void StringTrimAssembler::ScanForNonWhiteSpaceOrLineTerminator(
1880-
const TNode<RawPtrT> string_data, const TNode<IntPtrT> string_data_offset,
1881-
const TNode<BoolT> is_stringonebyte, TVariable<IntPtrT>* const var_index,
1882-
const TNode<IntPtrT> end, int increment, Label* const if_none_found) {
1883-
Label if_stringisonebyte(this), out(this);
1884-
1885-
GotoIf(is_stringonebyte, &if_stringisonebyte);
1886-
1887-
// Two Byte String
1888-
BuildLoop<Uint16T>(
1889-
var_index, end, increment, if_none_found, &out,
1890-
[&](const TNode<IntPtrT> index) {
1891-
return Load<Uint16T>(
1892-
string_data,
1893-
WordShl(IntPtrAdd(index, string_data_offset), IntPtrConstant(1)));
1894-
});
1895-
1896-
BIND(&if_stringisonebyte);
1897-
BuildLoop<Uint8T>(var_index, end, increment, if_none_found, &out,
1898-
[&](const TNode<IntPtrT> index) {
1899-
return Load<Uint8T>(string_data,
1900-
IntPtrAdd(index, string_data_offset));
1901-
});
1902-
1903-
BIND(&out);
1904-
}
1905-
1906-
template <typename T>
1907-
void StringTrimAssembler::BuildLoop(
1908-
TVariable<IntPtrT>* const var_index, const TNode<IntPtrT> end,
1909-
int increment, Label* const if_none_found, Label* const out,
1910-
const std::function<TNode<T>(const TNode<IntPtrT>)>& get_character) {
1911-
Label loop(this, var_index);
1912-
Goto(&loop);
1913-
BIND(&loop);
1914-
{
1915-
TNode<IntPtrT> index = var_index->value();
1916-
GotoIf(IntPtrEqual(index, end), if_none_found);
1917-
GotoIfNotWhiteSpaceOrLineTerminator(
1918-
UncheckedCast<Uint32T>(get_character(index)), out);
1919-
Increment(var_index, increment);
1920-
Goto(&loop);
1921-
}
1922-
}
1923-
1924-
void StringTrimAssembler::GotoIfNotWhiteSpaceOrLineTerminator(
1925-
const TNode<Word32T> char_code, Label* const if_not_whitespace) {
1926-
Label out(this);
1927-
1928-
// 0x0020 - SPACE (Intentionally out of order to fast path a commmon case)
1929-
GotoIf(Word32Equal(char_code, Int32Constant(0x0020)), &out);
1930-
1931-
// 0x0009 - HORIZONTAL TAB
1932-
GotoIf(Uint32LessThan(char_code, Int32Constant(0x0009)), if_not_whitespace);
1933-
// 0x000A - LINE FEED OR NEW LINE
1934-
// 0x000B - VERTICAL TAB
1935-
// 0x000C - FORMFEED
1936-
// 0x000D - HORIZONTAL TAB
1937-
GotoIf(Uint32LessThanOrEqual(char_code, Int32Constant(0x000D)), &out);
1938-
1939-
// Common Non-whitespace characters
1940-
GotoIf(Uint32LessThan(char_code, Int32Constant(0x00A0)), if_not_whitespace);
1941-
1942-
// 0x00A0 - NO-BREAK SPACE
1943-
GotoIf(Word32Equal(char_code, Int32Constant(0x00A0)), &out);
1944-
1945-
// 0x1680 - Ogham Space Mark
1946-
GotoIf(Word32Equal(char_code, Int32Constant(0x1680)), &out);
1947-
1948-
// 0x2000 - EN QUAD
1949-
GotoIf(Uint32LessThan(char_code, Int32Constant(0x2000)), if_not_whitespace);
1950-
// 0x2001 - EM QUAD
1951-
// 0x2002 - EN SPACE
1952-
// 0x2003 - EM SPACE
1953-
// 0x2004 - THREE-PER-EM SPACE
1954-
// 0x2005 - FOUR-PER-EM SPACE
1955-
// 0x2006 - SIX-PER-EM SPACE
1956-
// 0x2007 - FIGURE SPACE
1957-
// 0x2008 - PUNCTUATION SPACE
1958-
// 0x2009 - THIN SPACE
1959-
// 0x200A - HAIR SPACE
1960-
GotoIf(Uint32LessThanOrEqual(char_code, Int32Constant(0x200A)), &out);
1961-
1962-
// 0x2028 - LINE SEPARATOR
1963-
GotoIf(Word32Equal(char_code, Int32Constant(0x2028)), &out);
1964-
// 0x2029 - PARAGRAPH SEPARATOR
1965-
GotoIf(Word32Equal(char_code, Int32Constant(0x2029)), &out);
1966-
// 0x202F - NARROW NO-BREAK SPACE
1967-
GotoIf(Word32Equal(char_code, Int32Constant(0x202F)), &out);
1968-
// 0x205F - MEDIUM MATHEMATICAL SPACE
1969-
GotoIf(Word32Equal(char_code, Int32Constant(0x205F)), &out);
1970-
// 0xFEFF - BYTE ORDER MARK
1971-
GotoIf(Word32Equal(char_code, Int32Constant(0xFEFF)), &out);
1972-
// 0x3000 - IDEOGRAPHIC SPACE
1973-
Branch(Word32Equal(char_code, Int32Constant(0x3000)), &out,
1974-
if_not_whitespace);
1975-
1976-
BIND(&out);
1977-
}
19781806

19791807
// Return the |word32| codepoint at {index}. Supports SeqStrings and
19801808
// ExternalStrings.

‎src/builtins/builtins-string-gen.h‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,6 @@ class StringIncludesIndexOfAssembler : public StringBuiltinsAssembler {
184184
TNode<Context> context);
185185
};
186186

187-
class StringTrimAssembler : public StringBuiltinsAssembler {
188-
public:
189-
explicit StringTrimAssembler(compiler::CodeAssemblerState* state)
190-
: StringBuiltinsAssembler(state) {}
191-
192-
V8_EXPORT_PRIVATE void GotoIfNotWhiteSpaceOrLineTerminator(
193-
const TNode<Word32T> char_code, Label* const if_not_whitespace);
194-
195-
protected:
196-
void Generate(String::TrimMode mode, const char* method, TNode<IntPtrT> argc,
197-
TNode<Context> context);
198-
199-
void ScanForNonWhiteSpaceOrLineTerminator(
200-
const TNode<RawPtrT> string_data, const TNode<IntPtrT> string_data_offset,
201-
const TNode<BoolT> is_stringonebyte, TVariable<IntPtrT>* const var_index,
202-
const TNode<IntPtrT> end, int increment, Label* const if_none_found);
203-
204-
template <typename T>
205-
void BuildLoop(
206-
TVariable<IntPtrT>* const var_index, const TNode<IntPtrT> end,
207-
int increment, Label* const if_none_found, Label* const out,
208-
const std::function<TNode<T>(const TNode<IntPtrT>)>& get_character);
209-
};
210-
211187
} // namespace internal
212188
} // namespace v8
213189

‎src/builtins/convert.tq‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ FromConstexpr<IterationKind, constexpr IterationKind>(
115115
return %RawDownCast<IterationKind>(Unsigned(%FromConstexpr<int32>(c)));
116116
}
117117

118+
FromConstexpr<string::TrimMode, string::constexpr TrimMode>(
119+
c: string::constexpr TrimMode): string::TrimMode {
120+
return %RawDownCast<string::TrimMode>(Unsigned(%FromConstexpr<int32>(c)));
121+
}
122+
118123
macro Convert<To: type, From: type>(i: From): To {
119124
return i;
120125
}

0 commit comments

Comments
 (0)