Commit 08217e45 authored by petermarshall's avatar petermarshall Committed by Commit bot

[builtins] Move StringStartsWith to a C++ builtin.

BUG=v8:5364

Review-Url: https://codereview.chromium.org/2407173002
Cr-Commit-Position: refs/heads/master@{#40165}
parent 5af6ec76
......@@ -1453,6 +1453,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
2, true);
SimpleInstallFunction(prototype, "substring",
Builtins::kStringPrototypeSubstring, 2, true);
SimpleInstallFunction(prototype, "startsWith",
Builtins::kStringPrototypeStartsWith, 1, false);
SimpleInstallFunction(prototype, "toString",
Builtins::kStringPrototypeToString, 0, true);
SimpleInstallFunction(prototype, "trim", Builtins::kStringPrototypeTrim, 0,
......
......@@ -1202,6 +1202,55 @@ void Builtins::Generate_StringPrototypeSubstring(CodeStubAssembler* a) {
}
}
BUILTIN(StringPrototypeStartsWith) {
HandleScope handle_scope(isolate);
TO_THIS_STRING(str, "String.prototype.startsWith");
// Check if the search string is a regExp and fail if it is.
Handle<Object> search = args.atOrUndefined(isolate, 1);
Maybe<bool> is_reg_exp = Object::IsRegExp(isolate, search);
if (is_reg_exp.IsNothing()) {
DCHECK(isolate->has_pending_exception());
return isolate->heap()->exception();
}
if (is_reg_exp.FromJust()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kFirstArgumentNotRegExp,
isolate->factory()->NewStringFromStaticChars(
"String.prototype.startsWith")));
}
Handle<String> search_string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, search_string,
Object::ToString(isolate, search));
Handle<Object> position = args.atOrUndefined(isolate, 2);
int start;
if (position->IsUndefined(isolate)) {
start = 0;
} else {
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position,
Object::ToInteger(isolate, position));
double index = std::max(position->Number(), 0.0);
index = std::min(index, static_cast<double>(str->length()));
start = static_cast<uint32_t>(index);
}
if (start + search_string->length() > str->length()) {
return *isolate->factory()->false_value();
}
FlatStringReader str_reader(isolate, String::Flatten(str));
FlatStringReader search_reader(isolate, String::Flatten(search_string));
for (int i = 0; i < search_string->length(); i++) {
if (str_reader.Get(start + i) != search_reader.Get(i)) {
return *isolate->factory()->false_value();
}
}
return *isolate->factory()->true_value();
}
// ES6 section 21.1.3.25 String.prototype.toString ()
void Builtins::Generate_StringPrototypeToString(CodeStubAssembler* assembler) {
typedef compiler::Node Node;
......
......@@ -632,6 +632,9 @@ namespace internal {
TFJ(StringPrototypeSubstr, 3) \
/* ES6 section 21.1.3.19 String.prototype.substring ( start, end ) */ \
TFJ(StringPrototypeSubstring, 3) \
/* ES6 section 21.1.3.20 */ \
/* String.prototype.startsWith ( searchString [ , position ] ) */ \
CPP(StringPrototypeStartsWith) \
/* ES6 section 21.1.3.25 String.prototype.toString () */ \
TFJ(StringPrototypeToString, 1) \
CPP(StringPrototypeTrim) \
......
......@@ -380,32 +380,6 @@ function StringRepeat(count) {
}
// ES6 draft 04-05-14, section 21.1.3.18
function StringStartsWith(searchString, position) { // length == 1
CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
var s = TO_STRING(this);
if (IsRegExp(searchString)) {
throw %make_type_error(kFirstArgumentNotRegExp, "String.prototype.startsWith");
}
var ss = TO_STRING(searchString);
var pos = TO_INTEGER(position);
var s_len = s.length;
var start = MinSimple(MaxSimple(pos, 0), s_len);
var ss_len = ss.length;
if (ss_len + start > s_len) {
return false;
}
return %_SubString(s, start, start + ss_len) === ss;
}
%FunctionSetLength(StringStartsWith, 1);
// ES6 Draft 05-22-2014, section 21.1.3.3
function StringCodePointAt(pos) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
......@@ -469,7 +443,6 @@ utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
"search", StringSearch,
"slice", StringSlice,
"split", StringSplitJS,
"startsWith", StringStartsWith,
"toLowerCase", StringToLowerCaseJS,
"toLocaleLowerCase", StringToLocaleLowerCase,
"toUpperCase", StringToUpperCaseJS,
......
......@@ -34,6 +34,7 @@ assertFalse(testString.startsWith("hello"));
assertFalse(testString.startsWith("Hello World!"));
assertFalse(testString.startsWith(null));
assertFalse(testString.startsWith(undefined));
assertFalse(testString.startsWith());
assertTrue("null".startsWith(null));
assertTrue("undefined".startsWith(undefined));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment