Bug 1039774 - Implement ES6 String.raw r=till

This commit is contained in:
Guptha Rajagopal 2014-07-30 10:18:00 +02:00
parent deb432d62c
commit 312fee9319
4 changed files with 115 additions and 0 deletions

View File

@ -185,6 +185,59 @@ function String_static_fromCodePoint() {
return callFunction(std_Function_apply, std_String_fromCharCode, null, elements);
}
/* ES6 Draft May 22, 2014 21.1.2.4 */
function String_static_raw(callSite, ...substitutions) {
// Step 1 (implicit).
// Step 2.
var numberOfSubstitutions = substitutions.length;
// Steps 3-4.
var cooked = ToObject(callSite);
// Steps 5-7.
var raw = ToObject(cooked.raw);
// Steps 8-10.
var literalSegments = ToLength(raw.length);
// Step 11.
if (literalSegments <= 0)
return "";
// Step 12.
var resultString = "";
// Step 13.
var nextIndex = 0;
// Step 14.
while (true) {
// Steps a-d.
var nextSeg = ToString(raw[nextIndex]);
// Step e.
resultString = resultString + nextSeg;
// Step f.
if (nextIndex + 1 === literalSegments)
// Step f.i.
return resultString;
// Steps g-j.
var nextSub;
if (nextIndex < numberOfSubstitutions)
nextSub = ToString(substitutions[nextIndex]);
else
nextSub = "";
// Step k.
resultString = resultString + nextSub;
// Step l.
nextIndex++;
}
}
/**
* Compare String str1 against String str2, using the locale and collation
* options provided.

View File

@ -4297,6 +4297,7 @@ js::str_fromCharCode_one_arg(JSContext *cx, HandleValue code, MutableHandleValue
static const JSFunctionSpec string_static_methods[] = {
JS_FN("fromCharCode", js::str_fromCharCode, 1, 0),
JS_SELF_HOSTED_FN("fromCodePoint", "String_static_fromCodePoint", 0,0),
JS_SELF_HOSTED_FN("raw", "String_static_raw", 2, 0),
// This must be at the end because of bug 853075: functions listed after
// self-hosted methods aren't available in self-hosted code.

View File

@ -0,0 +1,55 @@
var BUGNUMBER = 1039774;
var summary = 'String.raw';
print(BUGNUMBER + ": " + summary);
assertThrowsInstanceOf(function() { String.raw(); }, TypeError);
assertEq(String.raw.length, 1);
var cooked = [];
assertThrowsInstanceOf(function() { String.raw(cooked); }, TypeError);
cooked.raw = {};
assertEq(String.raw(cooked), "");
cooked.raw = {lengt: 0};
assertEq(String.raw(cooked), "");
cooked.raw = {length: 0};
assertEq(String.raw(cooked), "");
cooked.raw = {length: -1};
assertEq(String.raw(cooked), "");
cooked.raw = [];
assertEq(String.raw(cooked), "");
cooked.raw = ["a"];
assertEq(String.raw(cooked), "a");
cooked.raw = ["a", "b"];
assertEq(String.raw(cooked, "x"), "axb");
cooked.raw = ["a", "b"];
assertEq(String.raw(cooked, "x", "y"), "axb");
cooked.raw = ["a", "b", "c"];
assertEq(String.raw(cooked, "x"), "axbc");
cooked.raw = ["a", "b", "c"];
assertEq(String.raw(cooked, "x", "y"), "axbyc");
cooked.raw = ["\n", "\r\n", "\r"];
assertEq(String.raw(cooked, "x", "y"), "\nx\r\ny\r");
cooked.raw = ["\n", "\r\n", "\r"];
assertEq(String.raw(cooked, "\r\r", "\n"), "\n\r\r\r\n\n\r");
cooked.raw = {length: 2, '0':"a", '1':"b", '2':"c"};
assertEq(String.raw(cooked, "x", "y"), "axb");
cooked.raw = {length: 4, '0':"a", '1':"b", '2':"c"};
assertEq(String.raw(cooked, "x", "y"), "axbycundefined");
reportCompare(0, 0, "ok");

View File

@ -284,6 +284,12 @@ assertEq(func`hey``there``amine`, "was not mine");
assertEq(func`hey``tshere``amine`, "was not there");
assertEq(func`heys``there``mine`, "was not hey");
// String.raw
assertEq(String.raw`h\r\ney${4}there\n`, "h\\r\\ney4there\\n");
assertEq(String.raw`hey`, "hey");
assertEq(String.raw``, "");
*/
/*End func*/}