Commit 80e223aa authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[runtime] Cleanup native methods creation in js/string.js.

This CL replaces usages of utils.InstallFunctions and utils.InstallGetter()
with the DEFINE_METHOD* macros that ensure that the native function is
created in proper form from the beginning. Thus the function will not
require further reconfiguring like adding a computed name or removing of
'prototype' property.

This CL is one of a series of cleanup CL which are the preliminary steps for
improving function closures creation.

Bug: v8:6459
Change-Id: I2a858625242d9b20ae9d7d933d5ba4d6163769f6
Reviewed-on: https://chromium-review.googlesource.com/548076
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46296}
parent 4234ac9e
...@@ -17,8 +17,17 @@ var searchSymbol = utils.ImportNow("search_symbol"); ...@@ -17,8 +17,17 @@ var searchSymbol = utils.ImportNow("search_symbol");
//------------------------------------------------------------------- //-------------------------------------------------------------------
// ES6 21.1.3.11. // ES#sec-createhtml
function StringMatchJS(pattern) { function HtmlEscape(str) {
return %RegExpInternalReplace(/"/g, TO_STRING(str), "&quot;");
}
// Set up the non-enumerable functions on the String prototype object.
DEFINE_METHODS(
GlobalString.prototype,
{
/* ES#sec-string.prototype.match */
match(pattern) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.match"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
if (!IS_NULL_OR_UNDEFINED(pattern)) { if (!IS_NULL_OR_UNDEFINED(pattern)) {
...@@ -33,10 +42,10 @@ function StringMatchJS(pattern) { ...@@ -33,10 +42,10 @@ function StringMatchJS(pattern) {
// Equivalent to RegExpCreate (ES#sec-regexpcreate) // Equivalent to RegExpCreate (ES#sec-regexpcreate)
var regexp = %RegExpCreate(pattern); var regexp = %RegExpCreate(pattern);
return regexp[matchSymbol](subject); return regexp[matchSymbol](subject);
} }
// ES6 21.1.3.15. /* ES#sec-string.prototype.search */
function StringSearch(pattern) { search(pattern) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.search"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.search");
if (!IS_NULL_OR_UNDEFINED(pattern)) { if (!IS_NULL_OR_UNDEFINED(pattern)) {
...@@ -51,110 +60,91 @@ function StringSearch(pattern) { ...@@ -51,110 +60,91 @@ function StringSearch(pattern) {
// Equivalent to RegExpCreate (ES#sec-regexpcreate) // Equivalent to RegExpCreate (ES#sec-regexpcreate)
var regexp = %RegExpCreate(pattern); var regexp = %RegExpCreate(pattern);
return %_Call(regexp[searchSymbol], regexp, subject); return %_Call(regexp[searchSymbol], regexp, subject);
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.2.1
function HtmlEscape(str) {
return %RegExpInternalReplace(/"/g, TO_STRING(str), "&quot;");
}
// ES6 draft, revision 26 (2014-07-18), section B.2.3.2 /* ES#sec-string.prototype.anchor */
function StringAnchor(name) { anchor(name) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.anchor"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.anchor");
return "<a name=\"" + HtmlEscape(name) + "\">" + TO_STRING(this) + return "<a name=\"" + HtmlEscape(name) + "\">" + TO_STRING(this) +
"</a>"; "</a>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.3 /* ES#sec-string.prototype.big */
function StringBig() { big() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.big"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.big");
return "<big>" + TO_STRING(this) + "</big>"; return "<big>" + TO_STRING(this) + "</big>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.4 /* ES#sec-string.prototype.blink */
function StringBlink() { blink() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.blink"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.blink");
return "<blink>" + TO_STRING(this) + "</blink>"; return "<blink>" + TO_STRING(this) + "</blink>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.5 /* ES#sec-string.prototype.bold */
function StringBold() { bold() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.bold"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.bold");
return "<b>" + TO_STRING(this) + "</b>"; return "<b>" + TO_STRING(this) + "</b>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.6 /* ES#sec-string.prototype.fixed */
function StringFixed() { fixed() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fixed"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.fixed");
return "<tt>" + TO_STRING(this) + "</tt>"; return "<tt>" + TO_STRING(this) + "</tt>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.7 /* ES#sec-string.prototype.fontcolor */
function StringFontcolor(color) { fontcolor(color) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontcolor"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontcolor");
return "<font color=\"" + HtmlEscape(color) + "\">" + TO_STRING(this) + return "<font color=\"" + HtmlEscape(color) + "\">" + TO_STRING(this) +
"</font>"; "</font>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.8 /* ES#sec-string.prototype.fontsize */
function StringFontsize(size) { fontsize(size) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontsize"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.fontsize");
return "<font size=\"" + HtmlEscape(size) + "\">" + TO_STRING(this) + return "<font size=\"" + HtmlEscape(size) + "\">" + TO_STRING(this) +
"</font>"; "</font>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.9 /* ES#sec-string.prototype.italics */
function StringItalics() { italics() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.italics"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.italics");
return "<i>" + TO_STRING(this) + "</i>"; return "<i>" + TO_STRING(this) + "</i>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.10 /* ES#sec-string.prototype.link */
function StringLink(s) { link(s) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.link"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.link");
return "<a href=\"" + HtmlEscape(s) + "\">" + TO_STRING(this) + "</a>"; return "<a href=\"" + HtmlEscape(s) + "\">" + TO_STRING(this) + "</a>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.11 /* ES#sec-string.prototype.small */
function StringSmall() { small() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.small"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.small");
return "<small>" + TO_STRING(this) + "</small>"; return "<small>" + TO_STRING(this) + "</small>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.12 /* ES#sec-string.prototype.strike */
function StringStrike() { strike() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.strike"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.strike");
return "<strike>" + TO_STRING(this) + "</strike>"; return "<strike>" + TO_STRING(this) + "</strike>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.13 /* ES#sec-string.prototype.sub */
function StringSub() { sub() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.sub"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.sub");
return "<sub>" + TO_STRING(this) + "</sub>"; return "<sub>" + TO_STRING(this) + "</sub>";
} }
// ES6 draft, revision 26 (2014-07-18), section B.2.3.14 /* ES#sec-string.prototype.sup */
function StringSup() { sup() {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.sup");
return "<sup>" + TO_STRING(this) + "</sup>"; return "<sup>" + TO_STRING(this) + "</sup>";
} }
// ES6, section 21.1.3.13 /* ES#sec-string.prototype.repeat */
function StringRepeat(count) { repeat(count) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
var s = TO_STRING(this); var s = TO_STRING(this);
...@@ -176,11 +166,10 @@ function StringRepeat(count) { ...@@ -176,11 +166,10 @@ function StringRepeat(count) {
if (n === 0) return r; if (n === 0) return r;
s += s; s += s;
} }
} }
// ES6 Draft 05-22-2014, section 21.1.3.3 /* ES#sec-string.prototype.codepointat */
function StringCodePointAt(pos) { codePointAt(pos) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.codePointAt");
var string = TO_STRING(this); var string = TO_STRING(this);
...@@ -198,7 +187,9 @@ function StringCodePointAt(pos) { ...@@ -198,7 +187,9 @@ function StringCodePointAt(pos) {
return first; return first;
} }
return (first - 0xD800) * 0x400 + second + 0x2400; return (first - 0xD800) * 0x400 + second + 0x2400;
} }
}
);
function StringPad(thisString, maxLength, fillString) { function StringPad(thisString, maxLength, fillString) {
maxLength = TO_LENGTH(maxLength); maxLength = TO_LENGTH(maxLength);
...@@ -235,32 +226,39 @@ function StringPad(thisString, maxLength, fillString) { ...@@ -235,32 +226,39 @@ function StringPad(thisString, maxLength, fillString) {
return filler; return filler;
} }
// ES#sec-string.prototype.padstart DEFINE_METHODS_LEN(
// String.prototype.padStart(maxLength [, fillString]) GlobalString.prototype,
function StringPadStart(maxLength, fillString) { {
/* ES#sec-string.prototype.padstart */
/* String.prototype.padStart(maxLength [, fillString]) */
padStart(maxLength, fillString) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart");
var thisString = TO_STRING(this); var thisString = TO_STRING(this);
return StringPad(thisString, maxLength, fillString) + thisString; return StringPad(thisString, maxLength, fillString) + thisString;
} }
%FunctionSetLength(StringPadStart, 1);
// ES#sec-string.prototype.padend /* ES#sec-string.prototype.padend */
// String.prototype.padEnd(maxLength [, fillString]) /* String.prototype.padEnd(maxLength [, fillString]) */
function StringPadEnd(maxLength, fillString) { padEnd(maxLength, fillString) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd"); CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd");
var thisString = TO_STRING(this); var thisString = TO_STRING(this);
return thisString + StringPad(thisString, maxLength, fillString); return thisString + StringPad(thisString, maxLength, fillString);
} }
%FunctionSetLength(StringPadEnd, 1); },
1 /* Set functions length */
);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// String methods related to templates // String methods related to templates
// ES6 Draft 03-17-2015, section 21.1.2.4 // Set up the non-enumerable functions on the String object.
function StringRaw(callSite) { DEFINE_METHOD(
"use strict"; GlobalString,
/* ES#sec-string.raw */
raw(callSite) {
var numberOfSubstitutions = arguments.length; var numberOfSubstitutions = arguments.length;
var cooked = TO_OBJECT(callSite); var cooked = TO_OBJECT(callSite);
var raw = TO_OBJECT(cooked.raw); var raw = TO_OBJECT(cooked.raw);
...@@ -277,36 +275,9 @@ function StringRaw(callSite) { ...@@ -277,36 +275,9 @@ function StringRaw(callSite) {
} }
return result; return result;
} }
);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Set up the non-enumerable functions on the String object.
utils.InstallFunctions(GlobalString, DONT_ENUM, [
"raw", StringRaw
]);
// Set up the non-enumerable functions on the String prototype object.
utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
"codePointAt", StringCodePointAt,
"match", StringMatchJS,
"padEnd", StringPadEnd,
"padStart", StringPadStart,
"repeat", StringRepeat,
"search", StringSearch,
"link", StringLink,
"anchor", StringAnchor,
"fontcolor", StringFontcolor,
"fontsize", StringFontsize,
"big", StringBig,
"blink", StringBlink,
"bold", StringBold,
"fixed", StringFixed,
"italics", StringItalics,
"small", StringSmall,
"strike", StringStrike,
"sub", StringSub,
"sup", StringSup
]);
}) })
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