Commit 84c9f05d authored by peterwmwong's avatar peterwmwong Committed by Commit Bot

[builtins] Port String.raw to CPP

- Add StringRaw CPP Builtin
- Remove string.js

Bug: v8:5049
Change-Id: I0d067c5b5aa9231383c2f9f2a9cf80f478fbbaa8
Reviewed-on: https://chromium-review.googlesource.com/727723Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48748}
parent 9b88818c
......@@ -568,7 +568,6 @@ action("js2c") {
"src/js/prologue.js",
"src/js/v8natives.js",
"src/js/array.js",
"src/js/string.js",
"src/js/typedarray.js",
"src/js/weak-collection.js",
"src/js/messages.js",
......
......@@ -1937,6 +1937,9 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
SimpleInstallFunction(string_fun, "fromCodePoint",
Builtins::kStringFromCodePoint, 1, false);
// Install the String.raw function.
SimpleInstallFunction(string_fun, "raw", Builtins::kStringRaw, 1, false);
// Create the %StringPrototype%
Handle<JSValue> prototype =
Handle<JSValue>::cast(factory->NewJSObject(string_fun, TENURED));
......
......@@ -984,6 +984,8 @@ namespace internal {
SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 #sec-string.prototype.valueof */ \
TFJ(StringPrototypeValueOf, 0) \
/* ES6 #sec-string.raw */ \
CPP(StringRaw) \
/* ES6 #sec-string.prototype-@@iterator */ \
TFJ(StringPrototypeIterator, 0) \
\
......
......@@ -8,6 +8,7 @@
#include "src/counters.h"
#include "src/objects-inl.h"
#include "src/regexp/regexp-utils.h"
#include "src/string-builder.h"
#include "src/string-case.h"
#include "src/unicode-inl.h"
#include "src/unicode.h"
......@@ -511,5 +512,65 @@ BUILTIN(StringPrototypeToUpperCase) {
}
#endif // !V8_INTL_SUPPORT
// ES6 #sec-string.prototype.raw
BUILTIN(StringRaw) {
HandleScope scope(isolate);
Handle<Object> templ = args.atOrUndefined(isolate, 1);
const uint32_t argc = args.length();
Handle<String> raw_string =
isolate->factory()->NewStringFromAsciiChecked("raw");
Handle<Object> cooked;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, cooked,
Object::ToObject(isolate, templ));
Handle<Object> raw;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, raw,
Object::GetProperty(cooked, raw_string));
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, raw,
Object::ToObject(isolate, raw));
Handle<Object> raw_len;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, raw_len,
Object::GetProperty(raw, isolate->factory()->length_string()));
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, raw_len,
Object::ToLength(isolate, raw_len));
IncrementalStringBuilder result_builder(isolate);
const uint32_t length = static_cast<uint32_t>(raw_len->Number());
if (length > 0) {
Handle<Object> first_element;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, first_element,
Object::GetElement(isolate, raw, 0));
Handle<String> first_string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, first_string, Object::ToString(isolate, first_element));
result_builder.AppendString(first_string);
for (uint32_t i = 1, arg_i = 2; i < length; i++, arg_i++) {
if (arg_i < argc) {
Handle<String> argument_string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, argument_string,
Object::ToString(isolate, args.at(arg_i)));
result_builder.AppendString(argument_string);
}
Handle<Object> element;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element,
Object::GetElement(isolate, raw, i));
Handle<String> element_string;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, element_string,
Object::ToString(isolate, element));
result_builder.AppendString(element_string);
}
}
RETURN_RESULT_OR_FAILURE(isolate, result_builder.Finish());
}
} // namespace internal
} // namespace v8
// Copyright 2012 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.
(function(global, utils) {
"use strict";
%CheckIsBootstrapping();
// -------------------------------------------------------------------
// Imports
var GlobalString = global.String;
// -------------------------------------------------------------------
// String methods related to templates
// Set up the non-enumerable functions on the String object.
DEFINE_METHOD(
GlobalString,
/* ES#sec-string.raw */
raw(callSite) {
var numberOfSubstitutions = arguments.length;
var cooked = TO_OBJECT(callSite);
var raw = TO_OBJECT(cooked.raw);
var literalSegments = TO_LENGTH(raw.length);
if (literalSegments <= 0) return "";
var result = TO_STRING(raw[0]);
for (var i = 1; i < literalSegments; ++i) {
if (i < numberOfSubstitutions) {
result += TO_STRING(arguments[i]);
}
result += TO_STRING(raw[i]);
}
return result;
}
);
// -------------------------------------------------------------------
})
......@@ -2329,7 +2329,6 @@
'js/prologue.js',
'js/v8natives.js',
'js/array.js',
'js/string.js',
'js/typedarray.js',
'js/weak-collection.js',
'js/messages.js',
......
......@@ -1103,7 +1103,7 @@ TEST(SnapshotDataBlobWithWarmup) {
// Running the warmup script has effect on whether functions are
// pre-compiled, but does not pollute the context.
CHECK(IsCompiled("Math.abs"));
CHECK(!IsCompiled("String.raw"));
CHECK(IsCompiled("String.raw"));
CHECK(CompileRun("Math.random")->IsFunction());
}
isolate->Dispose();
......@@ -1140,7 +1140,7 @@ TEST(CustomSnapshotDataBlobWithWarmup) {
CHECK(IsCompiled("f"));
CHECK(IsCompiled("Math.abs"));
CHECK(!IsCompiled("g"));
CHECK(!IsCompiled("String.raw"));
CHECK(IsCompiled("String.raw"));
CHECK(!IsCompiled("Array.prototype.sort"));
CHECK_EQ(5, CompileRun("a")->Int32Value(context).FromJust());
}
......
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