mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-10-25 16:47:29 +09:00
Issue #1996 - Implement at() method on built-in indexables.
Resolves #1996
This commit is contained in:
parent
a83111160f
commit
59cf6bd808
6 changed files with 112 additions and 1 deletions
|
|
@ -833,7 +833,7 @@ function ArrayFrom(items, mapfn=undefined, thisArg=undefined) {
|
|||
}
|
||||
|
||||
// Step 7.
|
||||
assert(usingIterator === undefined, "`items` can't be an Iterable after step 6.g.iv");
|
||||
assert(usingIterator === undefined, "`items` cannot be an Iterable after step 6.g.iv");
|
||||
|
||||
// Steps 8-9.
|
||||
var arrayLike = ToObject(items);
|
||||
|
|
@ -1181,6 +1181,35 @@ function FlattenIntoArray(target, source, sourceLen, start, depth, mapperFunctio
|
|||
return targetIndex;
|
||||
}
|
||||
|
||||
// ES2022 at() method on the built-in indexables
|
||||
// Array.prototype.at(index)
|
||||
function ArrayAt(index) {
|
||||
// Step 1.
|
||||
var O = ToObject(this);
|
||||
|
||||
// Step 2.
|
||||
var len = ToLength(O.length);
|
||||
|
||||
// Step 3.
|
||||
var relativeIndex = ToInteger(index);
|
||||
|
||||
// Steps 4-5.
|
||||
var k;
|
||||
if (relativeIndex >= 0) {
|
||||
k = relativeIndex;
|
||||
} else {
|
||||
k = len + relativeIndex;
|
||||
}
|
||||
|
||||
// Step 6.
|
||||
if (k < 0 || k >= len) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Step 7.
|
||||
return O[k];
|
||||
}
|
||||
|
||||
function ArrayStaticConcat(arr, arg1) {
|
||||
if (arguments.length < 1)
|
||||
ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, 'Array.concat');
|
||||
|
|
|
|||
|
|
@ -784,6 +784,39 @@ function String_static_raw(callSite, ...substitutions) {
|
|||
}
|
||||
}
|
||||
|
||||
// ES2022 at() method on the built-in indexables
|
||||
// String.prototype.at(index)
|
||||
function String_at(index) {
|
||||
// Step 1.
|
||||
if (this === undefined || this === null)
|
||||
ThrowIncompatibleMethod("at", this);
|
||||
|
||||
// Step 2.
|
||||
var string = ToString(this);
|
||||
|
||||
// Step 3.
|
||||
var len = string.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 string[k];
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare String str1 against String str2, using the locale and collation
|
||||
* options provided.
|
||||
|
|
|
|||
|
|
@ -1308,6 +1308,45 @@ function TypedArraySubarray(begin, end) {
|
|||
return TypedArraySpeciesCreateWithBuffer(obj, buffer, beginByteOffset, newLength);
|
||||
}
|
||||
|
||||
// ES2022 at() method on the built-in indexables
|
||||
// %TypedArray%.prototype.at(index)
|
||||
function TypedArrayAt(index) {
|
||||
// Step 1.
|
||||
var obj = this;
|
||||
|
||||
// Step 2.
|
||||
// This function is not generic.
|
||||
if (!IsObject(obj) || !IsTypedArray(obj)) {
|
||||
return callFunction(CallTypedArrayMethodIfWrapped, obj, index,
|
||||
"TypedArrayAt");
|
||||
}
|
||||
GetAttachedArrayBuffer(obj);
|
||||
|
||||
// If we got here, `this` is either a typed array or a wrapper for one.
|
||||
|
||||
// Step 3.
|
||||
var len = TypedArrayLength(obj);
|
||||
|
||||
// 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 obj[k];
|
||||
}
|
||||
|
||||
// ES6 draft rev30 (2014/12/24) 22.2.3.30 %TypedArray%.prototype.values()
|
||||
function TypedArrayValues() {
|
||||
// Step 1.
|
||||
|
|
|
|||
|
|
@ -3189,6 +3189,9 @@ static const JSFunctionSpec array_methods[] = {
|
|||
JS_SELF_HOSTED_FN("flat", "ArrayFlat", 0,0),
|
||||
JS_SELF_HOSTED_FN("flatMap", "ArrayFlatMap", 1,0),
|
||||
|
||||
/* ES2022 additions */
|
||||
JS_SELF_HOSTED_FN("at", "ArrayAt", 1,0),
|
||||
|
||||
JS_FS_END
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2847,6 +2847,9 @@ static const JSFunctionSpec string_methods[] = {
|
|||
/* Python-esque sequence methods. */
|
||||
JS_FN("concat", str_concat, 1,0),
|
||||
JS_SELF_HOSTED_FN("slice", "String_slice", 2,0),
|
||||
|
||||
/* ES2022 additions */
|
||||
JS_SELF_HOSTED_FN("at", "String_at", 1,0),
|
||||
|
||||
/* HTML string methods. */
|
||||
JS_SELF_HOSTED_FN("bold", "String_bold", 0,0),
|
||||
|
|
|
|||
|
|
@ -1538,6 +1538,10 @@ TypedArrayObject::protoFunctions[] = {
|
|||
JS_SELF_HOSTED_FN("includes", "TypedArrayIncludes", 2, 0),
|
||||
JS_SELF_HOSTED_FN("toString", "ArrayToString", 0, 0),
|
||||
JS_SELF_HOSTED_FN("toLocaleString", "TypedArrayToLocaleString", 2, 0),
|
||||
|
||||
/* ES2022 additions */
|
||||
JS_SELF_HOSTED_FN("at", "TypedArrayAt", 1, 0),
|
||||
|
||||
JS_FS_END
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue