Update ToLength operation to conform ES6 spec (#4007)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2020-07-29 11:13:34 +02:00
committed by GitHub
parent 56e328be41
commit 3eb69075f7
31 changed files with 735 additions and 485 deletions
@@ -13,11 +13,11 @@
// limitations under the License.
var str = "foo//bar/baz//foo";
res = str.split("a", -1);
assert (res.length === 3);
assert (res[0] === "foo//b");
assert (res[1] === "r/b");
assert (res[2] === "z//foo");
res = str.split(/\/\//, Infinity);
res = str.split("a", Infinity);
assert (res.length === 0);
res = str.split(/\/\//, -1);
assert (res.length === 3);
assert (res[0] === "foo");
assert (res[1] === "bar/baz");
assert (res[2] === "foo");
+149
View File
@@ -0,0 +1,149 @@
// 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.
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (b[i] !== a[i]) {
return false;
}
}
return true;
}
// concat
(function() {
try {
Array.prototype.concat.call({}, {[Symbol.isConcatSpreadable]: true, length : 2 **53 - 1})
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}
try {
Array.prototype.concat.call([1, 2, 3, 4], {[Symbol.isConcatSpreadable]: true, length : 2 **53 - 4})
assert(false);
} catch (e) {
assert(e instanceof RangeError);
}
})();
// fill
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo'}
Array.prototype.fill.call(obj, 'bar', 2 ** 53 - 5)
assert(obj[2 ** 53 - 5] === 'bar');
assert(obj[2 ** 53 - 4] === 'bar');
assert(obj[2 ** 53 - 3] === 'bar');
assert(obj[2 ** 53 - 2] === 'bar');
obj = { length: 2 ** 53 + 2, [2 ** 53 - 2] : 'foo'}
Array.prototype.fill.call(obj, 'bar', 2 ** 53 - 2)
assert(obj[2 ** 53 - 2] === 'bar');
assert(obj[2 ** 53 - 1] === undefined);
})();
// includes
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo'}
assert(Array.prototype.includes.call(obj, 'foo', 2 ** 53 - 10) === true);
var obj = { length: 2 ** 53 - 1, [2 ** 53 + 5] : 'foo'}
assert(Array.prototype.includes.call(obj, 'foo', 2 ** 53 - 10) === false);
})();
// indexOf
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo'}
assert(Array.prototype.indexOf.call(obj, 'foo', 2 ** 53 - 10) === 2 ** 53 - 5);
var obj = { length: 2 ** 53 - 1, [2 ** 53 + 5] : 'foo'}
assert(Array.prototype.indexOf.call(obj, 'foo', 2 ** 53 - 10) === -1);
})();
// lastIndexOf
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo', [2 ** 53 - 6] : 'foo'}
assert(Array.prototype.lastIndexOf.call(obj, 'foo', 2 ** 53 - 2) === 2 ** 53 - 5);
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo', [2 ** 53 - 6] : 'foo'}
assert(Array.prototype.lastIndexOf.call(obj, 'foo', 10) === -1);
})();
// pop
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 2] : 'foo', [2 ** 53 - 3] : 'bar'}
assert(Array.prototype.pop.call(obj) === 'foo');
assert(obj.length === 2 ** 53 - 2);
assert(Array.prototype.pop.call(obj) === 'bar');
assert(obj.length === 2 ** 53 - 3);
})();
// push
(function() {
var obj = { length: 2 ** 53 - 1 };
try {
Array.prototype.push.call(obj, 'foo');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var obj = { length: 2 ** 53 - 2, [2 ** 53 - 3] : 'foo'}
assert(Array.prototype.push.call(obj, 'bar') === 2 ** 53 - 1);
assert(obj.length === 2 ** 53 - 1);
})();
// slice
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo', [2 ** 53 - 2] : 'bar'};
var sliced = Array.prototype.slice.call(obj, 2 ** 53 - 10);
assert(compareArray(sliced, [,,,,,'foo',,,'bar']));
var sliced = Array.prototype.slice.call(obj, 2 ** 53 - 8, 2 ** 53 - 4);
assert(compareArray(sliced, [,,,'foo',]));
})();
// splice
(function() {
var obj = { length: 2 ** 53 - 1, [2 ** 53 - 5] : 'foo', [2 ** 53 - 2] : 'bar'};
var spliced = Array.prototype.splice.call(obj, 2 ** 53 - 10, 5, '1', '2', '3', '4');
assert(compareArray(spliced, [,,,,,]));
assert(obj.length === 2 ** 53 - 2);
assert(obj[2 ** 53 - 3] === 'bar');
assert(obj[2 ** 53 - 6] === 'foo');
assert(obj[2 ** 53 - 7] === '4');
assert(obj[2 ** 53 - 8] === '3');
assert(obj[2 ** 53 - 9] === '2');
assert(obj[2 ** 53 - 10] === '1');
})();
// unshift
(function() {
var obj = { length: 2 ** 53 - 1 };
try {
Array.prototype.unshift.call(obj, 'foo');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
})();
-13
View File
@@ -44,19 +44,6 @@
<test id="annexB/B.2.3.7.js"><reason></reason></test>
<test id="annexB/B.2.3.8.js"><reason></reason></test>
<test id="annexB/B.2.3.9.js"><reason></reason></test>
<test id="built-ins/Array/prototype/lastIndexOf/15.4.4.15-3-28.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/15.4.4.19-3-14.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/15.4.4.19-3-28.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/15.4.4.19-3-29.js"><reason></reason></test>
<test id="built-ins/Array/prototype/map/15.4.4.19-3-8.js"><reason></reason></test>
<test id="built-ins/Array/prototype/pop/S15.4.4.6_A2_T2.js"><reason></reason></test>
<test id="built-ins/Array/prototype/pop/S15.4.4.6_A3_T1.js"><reason></reason></test>
<test id="built-ins/Array/prototype/pop/S15.4.4.6_A3_T2.js"><reason></reason></test>
<test id="built-ins/Array/prototype/push/S15.4.4.7_A2_T2.js"><reason></reason></test>
<test id="built-ins/Array/prototype/push/S15.4.4.7_A4_T1.js"><reason></reason></test>
<test id="built-ins/Array/prototype/slice/S15.4.4.10_A3_T1.js"><reason></reason></test>
<test id="built-ins/Array/prototype/slice/S15.4.4.10_A3_T2.js"><reason></reason></test>
<test id="built-ins/Array/prototype/splice/S15.4.4.12_A3_T1.js"><reason></reason></test>
<test id="built-ins/decodeURIComponent/S15.1.3.2_A2.5_T1.js"><reason></reason></test>
<test id="built-ins/decodeURI/S15.1.3.1_A2.5_T1.js"><reason></reason></test>
<test id="built-ins/GeneratorPrototype/next/context-constructor-invocation.js"><reason></reason></test>
+3 -3
View File
@@ -35,11 +35,11 @@ main (void)
jmem_init ();
ecma_init ();
uint32_t num;
ecma_length_t num;
ecma_value_t int_num = ecma_make_int32_value (123);
uint32_t result = ecma_op_to_length (int_num, &num);
ecma_value_t result = ecma_op_to_length (int_num, &num);
ecma_free_value (int_num);
@@ -88,7 +88,7 @@ main (void)
TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
#if ENABLED (JERRY_ESNEXT)
TEST_ASSERT (num == UINT32_MAX);
TEST_ASSERT (num == ECMA_NUMBER_MAX_SAFE_INTEGER);
#else /* !ENABLED (JERRY_ESNEXT) */
TEST_ASSERT (num == 0);
#endif /* ENABLED (JERRY_ESNEXT) */