Implement {Array, %TypedArray%, String}.prototype.at method (#4681)

The following methods were implemented:
- Array.prototype.at based on ECMA-262 Stage 3 Draft Relative Indexing Method proposal
- String.prototype.at based on ECMA-262 Stage 3 Draft Relative Indexing Method proposal
- TypedArray.prototype.at based on ECMA-262 Stage 3 Draft Relative Indexing Method proposal

https://tc39.es/proposal-relative-indexing-method/

JerryScript-DCO-1.0-Signed-off-by: Daniel Batiz batizjob@gmail.com
This commit is contained in:
batizdaniel
2021-08-10 17:19:25 +02:00
committed by GitHub
parent 444806d78a
commit a25b824509
15 changed files with 363 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var obj = {};
var array = ['Apple', 'Banana', "zero", 0, obj, 'Apple'];
var index = array.at(0);
assert(index === 'Apple');
assert(array[index] === undefined);
assert(array.at(array.length) === undefined);
assert(array.at(array.length+1) === undefined);
assert(array.at(array.length-1) === 'Apple');
assert(array.at("1") === 'Banana');
assert(array.at(-1) === 'Apple');
assert(array.at("-1") === 'Apple');
assert(array.at("-20") === undefined);
/* 7 */
var obj = {}
obj.length = 1;
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
obj.at = Array.prototype.at;
try {
obj.at(0);
assert(false);
} catch(e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}
try {
Array.prototype.at.call(undefined)
assert (false);
} catch(e) {
assert(e instanceof TypeError);
}
@@ -13,5 +13,5 @@
// limitations under the License.
var expected = '{"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"flat":true,"flatMap":true,"includes":true,"keys":true,"values":true}';
var expected = '{"at":true,"copyWithin":true,"entries":true,"fill":true,"find":true,"findIndex":true,"flat":true,"flatMap":true,"includes":true,"keys":true,"values":true}';
assert(JSON.stringify(Array.prototype[Symbol.unscopables]) === expected);
@@ -0,0 +1,50 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var str = 'This is an Apple';
/* Basic tests */
var index = str.at(0);
assert(index === 'T');
assert(str[index] === undefined);
assert(str.at(str.length) === undefined);
assert(str.at(str.length+1) === undefined);
assert(str.at(str.length-1) === 'e');
assert(str.at("1") === 'h');
assert(str.at(-1) === 'e');
assert(str.at("-1") === 'e');
assert(str.at("-20") === undefined);
try {
String.prototype.at.call(undefined)
assert (false);
} catch(e) {
assert(e instanceof TypeError);
}
var obj = {toString: function() { return "Apple"; } };
obj.at = String.prototype.at;
assert(obj.at(0) === 'A');
/* BigInteger as num and char */
assert(str.at("1n") === 'T')
try {
str.at (10n);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
@@ -0,0 +1,82 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Integer */
var normal_typedarrays = [
new Int8Array([0, 1, 2, 3, 0, 5, 6]),
new Uint8Array([0, 1, 2, 3, 0, 5, 6]),
new Uint8ClampedArray([0, 1, 2, 3, 0, 5, 6]),
new Int16Array([0, 1, 2, 3, 0, 5, 6]),
new Uint16Array([0, 1, 2, 3, 0, 5, 6]),
new Int32Array([0, 1, 2, 3, 0, 5, 6]),
new Uint32Array([0, 1, 2, 3, 0, 5, 6]),
];
/* Float */
var float_typedarrays = [
new Float32Array([1.5, 1.5, 2.5, 3.5, 0.5, 5.5, 10.5]),
new Float64Array([1.5, 2.5, 3.5, 0.5, 5.5, 6.5, 10.5]),
];
/* BigInt */
var bigint_typedarrays = [
new BigInt64Array([3n, 10n, 2n, 3n, 4n, 5n, 6n]),
new BigUint64Array([3n, 1n, 2n, 3n, 33n, 5n, 6n])
];
var index = normal_typedarrays[0].at(0);
assert(index === 0);
assert(normal_typedarrays[index].at(0) === 0);
/* Integer input */
normal_typedarrays.forEach(function(e){
assert(e.at(normal_typedarrays[1].length) === undefined);
assert(e.at(normal_typedarrays[2].length+1) === undefined);
assert(e.at(normal_typedarrays[3].length-1) === 6);
assert(e.at(0) === 0);
assert(e.at("-1") === 6);
assert(e.at(-1) === 6);
assert(e.at("-20") === undefined);
assert(e.at(100) === undefined)
});
/* Float input */
float_typedarrays.forEach(function(f){
assert(f.at(float_typedarrays[1].length) === undefined);
assert(f.at(0) === 1.5);
assert(f.at("-1") === 10.5);
assert(f.at(-1) === 10.5);
assert(f.at("-20") === undefined);
assert(f.at(100) === undefined)
});
/* BigInt input */
bigint_typedarrays.forEach(function(b){
assert(b.at(bigint_typedarrays[1].length) === undefined);
assert(b.at(0) === 3n);
assert(b.at("-1") === 6n);
assert(b.at(-1) === 6n);
assert(b.at("-20") === undefined);
assert(b.at(100) === undefined)
});
normal_typedarrays.forEach(function(e){
try {
e.prototype.at.call(undefined);
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
});