зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1658308 - Implement String.prototyp.item in Nightly. r=anba
Differential Revision: https://phabricator.services.mozilla.com/D92640
This commit is contained in:
Родитель
b560ffb6f2
Коммит
915c793341
|
@ -3639,6 +3639,10 @@ static const JSFunctionSpec string_methods[] = {
|
|||
JS_SELF_HOSTED_FN("fontcolor", "String_fontcolor", 1, 0),
|
||||
JS_SELF_HOSTED_FN("fontsize", "String_fontsize", 1, 0),
|
||||
|
||||
#ifdef NIGHTLY_BUILD
|
||||
JS_SELF_HOSTED_FN("item", "String_item", 1, 0),
|
||||
#endif
|
||||
|
||||
JS_SELF_HOSTED_SYM_FN(iterator, "String_iterator", 0, 0), JS_FS_END};
|
||||
|
||||
// ES6 rev 27 (2014 Aug 24) 21.1.1
|
||||
|
|
|
@ -888,6 +888,39 @@ function String_toLocaleUpperCase() {
|
|||
}
|
||||
#endif // JS_HAS_INTL_API
|
||||
|
||||
// https://github.com/tc39/proposal-item-method
|
||||
// String.prototype.item ( index )
|
||||
function String_item(index) {
|
||||
// Step 1.
|
||||
if (this === undefined || this === null)
|
||||
ThrowIncompatibleMethod("item", this);
|
||||
|
||||
// Step 2.
|
||||
var str = ToString(this);
|
||||
|
||||
// Step 3.
|
||||
var len = str.length;
|
||||
|
||||
// Step 4.
|
||||
var relativeIndex = ToInteger(index);
|
||||
|
||||
// Steps 5-6.
|
||||
var k;
|
||||
if (relativeIndex >= 0) {
|
||||
k = relativeIndex;
|
||||
} else {
|
||||
k = len + relativeIndex;
|
||||
}
|
||||
|
||||
// Step 7.
|
||||
if (k < 0 || k >= len) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Step 8.
|
||||
return str[k];
|
||||
}
|
||||
|
||||
// ES2018 draft rev 8fadde42cf6a9879b4ab0cb6142b31c4ee501667
|
||||
// 21.1.2.4 String.raw ( template, ...substitutions )
|
||||
function String_static_raw(callSite/*, ...substitutions*/) {
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
// |reftest| skip-if(!String.prototype.item)
|
||||
|
||||
function basic() {
|
||||
assertEq("a".item(0), "a");
|
||||
assertEq("a".item(-1), "a");
|
||||
|
||||
assertEq("".item(0), undefined);
|
||||
assertEq("".item(-1), undefined);
|
||||
assertEq("".item(1), undefined);
|
||||
|
||||
assertEq("ab".item(0), "a");
|
||||
assertEq("ab".item(1), "b");
|
||||
assertEq("ab".item(-2), "a");
|
||||
assertEq("ab".item(-1), "b");
|
||||
|
||||
assertEq("ab".item(2), undefined);
|
||||
assertEq("ab".item(-3), undefined);
|
||||
assertEq("ab".item(-4), undefined);
|
||||
assertEq("ab".item(Infinity), undefined);
|
||||
assertEq("ab".item(-Infinity), undefined);
|
||||
assertEq("ab".item(NaN), "a"); // ToInteger(NaN) = 0
|
||||
|
||||
assertEq("\u{1F921}".item(0), "\u{D83E}");
|
||||
assertEq("\u{1F921}".item(1), "\u{DD21}");
|
||||
}
|
||||
|
||||
basic();
|
||||
|
||||
if (typeof reportCompare === "function")
|
||||
reportCompare(true, true);
|
Загрузка…
Ссылка в новой задаче