[builtins] Move StringIncludes to a builtin.

BUG=v8:5364

Review-Url: https://codereview.chromium.org/2399423003
Cr-Commit-Position: refs/heads/master@{#40110}
This commit is contained in:
petermarshall 2016-10-10 01:31:25 -07:00 committed by Commit bot
parent f6bd23f244
commit b374d719e7
4 changed files with 41 additions and 29 deletions

View File

@ -1437,6 +1437,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
1, true);
SimpleInstallFunction(prototype, "charCodeAt",
Builtins::kStringPrototypeCharCodeAt, 1, true);
SimpleInstallFunction(prototype, "includes",
Builtins::kStringPrototypeIncludes, 1, false);
SimpleInstallFunction(prototype, "indexOf",
Builtins::kStringPrototypeIndexOf, 1, false);
SimpleInstallFunction(prototype, "lastIndexOf",

View File

@ -781,6 +781,42 @@ void Builtins::Generate_StringPrototypeCharCodeAt(
assembler->Return(result);
}
// ES6 section 21.1.3.7
// String.prototype.includes ( searchString [ , position ] )
BUILTIN(StringPrototypeIncludes) {
HandleScope handle_scope(isolate);
TO_THIS_STRING(str, "String.prototype.includes");
Handle<Object> search = args.atOrUndefined(isolate, 1);
if (search->IsObject()) {
Handle<Symbol> match_name = isolate->factory()->match_symbol();
Handle<Object> match_property;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, match_property,
Object::GetProperty(search, match_name));
if (!match_property->IsUndefined(isolate) &&
match_property->BooleanValue()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kFirstArgumentNotRegExp,
isolate->factory()->NewStringFromStaticChars(
"String.prototype.includes")));
}
}
Handle<String> search_string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string,
Object::ToString(isolate, search));
Handle<Object> position;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, position,
Object::ToInteger(isolate, args.atOrUndefined(isolate, 2)));
double index = std::max(position->Number(), 0.0);
index = std::min(index, static_cast<double>(str->length()));
int index_in_str = String::IndexOf(isolate, str, search_string,
static_cast<uint32_t>(index));
return *isolate->factory()->ToBoolean(index_in_str != -1);
}
// ES6 section 21.1.3.8 String.prototype.indexOf ( searchString [ , position ] )
BUILTIN(StringPrototypeIndexOf) {
HandleScope handle_scope(isolate);

View File

@ -604,6 +604,9 @@ namespace internal {
TFJ(StringPrototypeCharAt, 2) \
/* ES6 section 21.1.3.2 String.prototype.charCodeAt ( pos ) */ \
TFJ(StringPrototypeCharCodeAt, 2) \
/* ES6 section 21.1.3.7 */ \
/* String.prototype.includes ( searchString [ , position ] ) */ \
CPP(StringPrototypeIncludes) \
/* ES6 section 21.1.3.8 */ \
/* String.prototype.indexOf ( searchString [ , position ] ) */ \
CPP(StringPrototypeIndexOf) \

View File

@ -433,34 +433,6 @@ function StringEndsWith(searchString, position) { // length == 1
%FunctionSetLength(StringEndsWith, 1);
// ES6 draft 04-05-14, section 21.1.3.6
function StringIncludes(searchString, position) { // length == 1
CHECK_OBJECT_COERCIBLE(this, "String.prototype.includes");
var string = TO_STRING(this);
if (IsRegExp(searchString)) {
throw %make_type_error(kFirstArgumentNotRegExp, "String.prototype.includes");
}
searchString = TO_STRING(searchString);
var pos = TO_INTEGER(position);
var stringLength = string.length;
if (pos < 0) pos = 0;
if (pos > stringLength) pos = stringLength;
var searchStringLength = searchString.length;
if (searchStringLength + pos > stringLength) {
return false;
}
return %StringIndexOf(string, searchString, pos) !== -1;
}
%FunctionSetLength(StringIncludes, 1);
// ES6 Draft 05-22-2014, section 21.1.3.3
function StringCodePointAt(pos) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
@ -519,7 +491,6 @@ utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
"codePointAt", StringCodePointAt,
"concat", StringConcat,
"endsWith", StringEndsWith,
"includes", StringIncludes,
"match", StringMatchJS,
"repeat", StringRepeat,
"replace", StringReplace,