Port StringPrototypeSlice to Torque
Bug: v8:8996 Change-Id: I712f7eea934581f8d0889889a8737548ef8fa255 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1611752 Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com> Reviewed-by: Simon Zünd <szuend@chromium.org> Reviewed-by: Jakob Gruber <jgruber@chromium.org> Reviewed-by: Peter Wong <peter.wm.wong@gmail.com> Cr-Commit-Position: refs/heads/master@{#61558}
This commit is contained in:
parent
7bbd0bfe51
commit
93e9126786
2
BUILD.gn
2
BUILD.gn
@ -968,6 +968,7 @@ torque_files = [
|
||||
"src/builtins/string-html.tq",
|
||||
"src/builtins/string-iterator.tq",
|
||||
"src/builtins/string-repeat.tq",
|
||||
"src/builtins/string-slice.tq",
|
||||
"src/builtins/string-startswith.tq",
|
||||
"src/builtins/string-substring.tq",
|
||||
"src/builtins/typed-array-createtypedarray.tq",
|
||||
@ -1025,6 +1026,7 @@ torque_namespaces = [
|
||||
"string-html",
|
||||
"string-iterator",
|
||||
"string-repeat",
|
||||
"string-slice",
|
||||
"string-substring",
|
||||
"test",
|
||||
"typed-array",
|
||||
|
@ -973,8 +973,6 @@ namespace internal {
|
||||
TFJ(StringPrototypeReplace, 2, kReceiver, kSearch, kReplace) \
|
||||
/* ES6 #sec-string.prototype.search */ \
|
||||
TFJ(StringPrototypeSearch, 1, kReceiver, kRegexp) \
|
||||
/* ES6 #sec-string.prototype.slice */ \
|
||||
TFJ(StringPrototypeSlice, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
|
||||
/* ES6 #sec-string.prototype.split */ \
|
||||
TFJ(StringPrototypeSplit, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
|
||||
/* ES6 #sec-string.prototype.substr */ \
|
||||
|
@ -1593,57 +1593,6 @@ TF_BUILTIN(StringPrototypeSearch, StringMatchSearchAssembler) {
|
||||
Generate(kSearch, "String.prototype.search", receiver, maybe_regexp, context);
|
||||
}
|
||||
|
||||
// ES6 section 21.1.3.18 String.prototype.slice ( start, end )
|
||||
TF_BUILTIN(StringPrototypeSlice, StringBuiltinsAssembler) {
|
||||
Label out(this);
|
||||
TVARIABLE(IntPtrT, var_start);
|
||||
TVARIABLE(IntPtrT, var_end);
|
||||
|
||||
const int kStart = 0;
|
||||
const int kEnd = 1;
|
||||
Node* argc =
|
||||
ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
|
||||
CodeStubArguments args(this, argc);
|
||||
Node* const receiver = args.GetReceiver();
|
||||
TNode<Object> start = args.GetOptionalArgumentValue(kStart);
|
||||
TNode<Object> end = args.GetOptionalArgumentValue(kEnd);
|
||||
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
|
||||
|
||||
// 1. Let O be ? RequireObjectCoercible(this value).
|
||||
RequireObjectCoercible(context, receiver, "String.prototype.slice");
|
||||
|
||||
// 2. Let S be ? ToString(O).
|
||||
TNode<String> const subject_string =
|
||||
CAST(CallBuiltin(Builtins::kToString, context, receiver));
|
||||
|
||||
// 3. Let len be the number of elements in S.
|
||||
TNode<IntPtrT> const length = LoadStringLengthAsWord(subject_string);
|
||||
|
||||
// Convert {start} to a relative index.
|
||||
var_start = ConvertToRelativeIndex(context, start, length);
|
||||
|
||||
// 5. If end is undefined, let intEnd be len;
|
||||
var_end = length;
|
||||
GotoIf(IsUndefined(end), &out);
|
||||
|
||||
// Convert {end} to a relative index.
|
||||
var_end = ConvertToRelativeIndex(context, end, length);
|
||||
Goto(&out);
|
||||
|
||||
Label return_emptystring(this);
|
||||
BIND(&out);
|
||||
{
|
||||
GotoIf(IntPtrLessThanOrEqual(var_end.value(), var_start.value()),
|
||||
&return_emptystring);
|
||||
TNode<String> const result =
|
||||
SubString(subject_string, var_start.value(), var_end.value());
|
||||
args.PopAndReturn(result);
|
||||
}
|
||||
|
||||
BIND(&return_emptystring);
|
||||
args.PopAndReturn(EmptyStringConstant());
|
||||
}
|
||||
|
||||
TNode<JSArray> StringBuiltinsAssembler::StringToArray(
|
||||
TNode<Context> context, TNode<String> subject_string,
|
||||
TNode<Smi> subject_length, TNode<Number> limit_number) {
|
||||
|
35
src/builtins/string-slice.tq
Normal file
35
src/builtins/string-slice.tq
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright 2019 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
namespace string_slice {
|
||||
|
||||
extern macro SubString(String, intptr, intptr): String;
|
||||
|
||||
// ES6 #sec-string.prototype.slice ( start, end )
|
||||
// https://tc39.github.io/ecma262/#sec-string.prototype.slice
|
||||
transitioning javascript builtin StringPrototypeSlice(
|
||||
implicit context: Context)(receiver: Object, ...arguments): String {
|
||||
// 1. Let O be ? RequireObjectCoercible(this value).
|
||||
// 2. Let S be ? ToString(O).
|
||||
const string: String = ToThisString(receiver, 'String.prototype.slice');
|
||||
|
||||
// 3. Let len be the number of elements in S.
|
||||
const length: intptr = string.length_intptr;
|
||||
|
||||
// Convert {start} to a relative index.
|
||||
const start: intptr = ConvertToRelativeIndex(arguments[0], length);
|
||||
|
||||
// 5. If end is undefined, let intEnd be len;
|
||||
// else Convert {end} to a relative index.
|
||||
const temp = arguments[1];
|
||||
const end: intptr =
|
||||
temp == Undefined ? length : ConvertToRelativeIndex(temp, length);
|
||||
|
||||
if (end <= start) {
|
||||
return kEmptyString;
|
||||
}
|
||||
|
||||
return SubString(string, start, end);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user