add jerryscript source code
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// 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 fail() {
|
||||
assert (0);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (false && fail()) {
|
||||
assert (0);
|
||||
}
|
||||
|
||||
if (true && false && fail()) {
|
||||
assert (0);
|
||||
}
|
||||
|
||||
if (true || fail()) {
|
||||
} else {
|
||||
assert (0);
|
||||
}
|
||||
|
||||
if (false || true || fail()) {
|
||||
} else {
|
||||
assert (0);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 check_parse_error (txt) {
|
||||
try {
|
||||
eval (txt)
|
||||
assert (false)
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
function f_args (a,b,c) {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
check_parse_error ("f_args (1 2 3)");
|
||||
check_parse_error ("f_args (1; 2; 3)");
|
||||
check_parse_error ("f_args (())");
|
||||
check_parse_error ("f_args (1, 2, 3");
|
||||
check_parse_error ("f_args 1, 2, 3)");
|
||||
check_parse_error ("f_args 1, 2, 3");
|
||||
check_parse_error ("f_args 1; 2; 3");
|
||||
check_parse_error ("f_args{1, 2, 3}");
|
||||
@@ -0,0 +1,144 @@
|
||||
// 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 f_arg (arguments)
|
||||
{
|
||||
return arguments;
|
||||
}
|
||||
assert (f_arg (1) === 1);
|
||||
|
||||
function f (a, b, c)
|
||||
{
|
||||
return arguments;
|
||||
}
|
||||
|
||||
args = f();
|
||||
assert (args[0] === undefined);
|
||||
|
||||
args = f (1, 2, 3, 4, 5);
|
||||
assert (args[0] === 1);
|
||||
assert (args[1] === 2);
|
||||
assert (args[2] === 3);
|
||||
assert (args[3] === 4);
|
||||
assert (args[4] === 5);
|
||||
assert (args[5] === undefined);
|
||||
|
||||
assert (args.callee === f);
|
||||
assert (typeof args.caller === 'undefined');
|
||||
|
||||
function g (a, b, c)
|
||||
{
|
||||
assert (arguments[0] === 1);
|
||||
assert (arguments[1] === undefined);
|
||||
assert (arguments[2] === undefined);
|
||||
|
||||
a = 'a';
|
||||
b = 'b';
|
||||
c = 'c';
|
||||
|
||||
assert (arguments[0] === 'a');
|
||||
assert (arguments[1] === 'b');
|
||||
assert (arguments[2] === 'c');
|
||||
|
||||
arguments [0] = 1;
|
||||
arguments [1] = 2;
|
||||
arguments [2] = 3;
|
||||
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
assert (c === 3);
|
||||
|
||||
delete arguments [0];
|
||||
arguments[0] = 'new value';
|
||||
assert (a === 1);
|
||||
|
||||
a = 'a';
|
||||
b = 'b';
|
||||
c = 'c';
|
||||
|
||||
assert (arguments[0] === 'new value');
|
||||
assert (arguments[1] === 'b');
|
||||
assert (arguments[2] === 'c');
|
||||
}
|
||||
|
||||
g (1);
|
||||
|
||||
fn_expr = function (a, b, c)
|
||||
{
|
||||
'use strict';
|
||||
|
||||
assert (arguments[0] === 1);
|
||||
assert (arguments[1] === undefined);
|
||||
assert (arguments[2] === undefined);
|
||||
|
||||
a = 'a';
|
||||
b = 'b';
|
||||
c = 'c';
|
||||
|
||||
assert (arguments[0] === 1);
|
||||
assert (arguments[1] === undefined);
|
||||
assert (arguments[2] === undefined);
|
||||
|
||||
arguments [0] = 1;
|
||||
arguments [1] = 'p';
|
||||
arguments [2] = 'q';
|
||||
|
||||
assert (a === 'a');
|
||||
assert (b === 'b');
|
||||
assert (c === 'c');
|
||||
|
||||
delete arguments [0];
|
||||
arguments[0] = 'new value';
|
||||
assert (a === 'a');
|
||||
|
||||
a = 'a';
|
||||
b = 'b';
|
||||
c = 'c';
|
||||
|
||||
assert (arguments[0] === 'new value');
|
||||
assert (arguments[1] === 'p');
|
||||
assert (arguments[2] === 'q');
|
||||
|
||||
function check_type_error_for_property (obj, prop) {
|
||||
try {
|
||||
var v = obj[prop];
|
||||
assert (false);
|
||||
}
|
||||
catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
check_type_error_for_property (arguments, 'callee');
|
||||
}
|
||||
|
||||
fn_expr (1);
|
||||
|
||||
(function () {
|
||||
var a = [arguments];
|
||||
})();
|
||||
|
||||
function nested_args()
|
||||
{
|
||||
var a;
|
||||
for (var i = 0; i < 1; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
a = arguments[i];
|
||||
}
|
||||
}
|
||||
assert(a === 3);
|
||||
}
|
||||
nested_args(3);
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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 big = 2147483646;
|
||||
|
||||
big++;
|
||||
assert(big == 2147483647);
|
||||
|
||||
big += 1;
|
||||
assert(big == 2147483648); // overflow on 32bit numbers
|
||||
|
||||
big++;
|
||||
assert(big == 2147483649); // overflow on 32bit numbers
|
||||
|
||||
assert ((1152921504606846976).toString() === "1152921504606847000");
|
||||
|
||||
assert (1.797693134862315808e+308 === Infinity);
|
||||
|
||||
assert (9999999999999999 == 10000000000000000);
|
||||
|
||||
assert((9007199254740993).toString() === "9007199254740992");
|
||||
|
||||
assert((9007199254740992).toString() === "9007199254740992");
|
||||
|
||||
assert((9007199254740994).toString() === "9007199254740994");
|
||||
|
||||
assert((1.00517e+21).toString() === "1.0051699999999999e+21");
|
||||
|
||||
assert((1.00001e+21).toString() === "1.0000099999999999e+21");
|
||||
|
||||
assert((9007199254740995).toString() === "9007199254740996");
|
||||
|
||||
assert((18014398509481989).toString() === "18014398509481988");
|
||||
|
||||
assert((18014398509481990).toString() === "18014398509481992");
|
||||
|
||||
assert((18014398509481991).toString() === "18014398509481992");
|
||||
|
||||
assert((18014398509481993).toString() === "18014398509481992");
|
||||
|
||||
assert((18014398509481994).toString() === "18014398509481992");
|
||||
|
||||
assert((18014398509481997).toString() === "18014398509481996");
|
||||
|
||||
assert((18014398509481998).toString() === "18014398509482000");
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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.
|
||||
|
||||
assert((1 + 2) == 3);
|
||||
assert((2 + 1) == 3);
|
||||
assert((2 + 1) != 4);
|
||||
|
||||
assert((7 + 7) == 14);
|
||||
assert((7 - 7) == 0);
|
||||
assert((7 * 7) == 49);
|
||||
assert((7 / 7) == 1);
|
||||
assert((7 + 7) == 14);
|
||||
assert((7 % 7) == 0);
|
||||
|
||||
var number = 81;
|
||||
assert((number + 9) == 90);
|
||||
assert((number - 9) == 72);
|
||||
assert((number * 10) == 810);
|
||||
assert((number / 9) == 9);
|
||||
assert((number % 79) == 2);
|
||||
|
||||
var num1 = 1234567, num2 = 1234000;
|
||||
assert((num1 % num2) == 567);
|
||||
|
||||
assert (1 / (-1 % -1) < 0);
|
||||
assert (1 / (-1 % 1) < 0);
|
||||
assert (1 / (1 % -1) > 0);
|
||||
assert (1 / (1 % 1) > 0);
|
||||
|
||||
assert (eval ("x\n\n=\n\n6\n\n/\n\n3") === 2)
|
||||
@@ -0,0 +1,90 @@
|
||||
// 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 array = ["foo", [], Infinity, 4]
|
||||
var new_arr = array.concat();
|
||||
|
||||
assert(new_arr.length === array.length)
|
||||
for (i = 0; i < array.length; i++) {
|
||||
assert(array[i] === new_arr[i]);
|
||||
}
|
||||
|
||||
var obj = { concat : Array.prototype.concat };
|
||||
var arr1 = ["Apple", 6, "Peach"];
|
||||
var arr2 = [obj, "Cherry", "Grape"];
|
||||
|
||||
var new_array = obj.concat(arr1);
|
||||
assert(new_array.length === 4);
|
||||
assert(new_array[0] === obj);
|
||||
assert(new_array[1] === "Apple");
|
||||
assert(new_array[2] === 6);
|
||||
assert(new_array[3] === "Peach");
|
||||
|
||||
var new_array = arr1.concat(arr2, obj, 1);
|
||||
|
||||
assert(new_array.length === 8);
|
||||
assert(new_array[0] === "Apple");
|
||||
assert(new_array[1] === 6);
|
||||
assert(new_array[2] === "Peach");
|
||||
assert(new_array[3] === obj);
|
||||
assert(new_array[4] === "Cherry");
|
||||
assert(new_array[5] === "Grape");
|
||||
assert(new_array[6] === obj);
|
||||
assert(new_array[7] === 1);
|
||||
|
||||
var arr1 = [1,2];
|
||||
var arr2 = [4,5,6,7,8];
|
||||
var arr3 = [,,9,10];
|
||||
var arr4 = [];
|
||||
var expected = [1,2,4,5,6,7,8,,,9,10];
|
||||
|
||||
var result = arr1.concat(arr2, arr3, arr4);
|
||||
|
||||
assert(result.length === expected.length)
|
||||
for (i = 0; i < result.length; i++) {
|
||||
assert(result[i] === expected[i]);
|
||||
}
|
||||
|
||||
var arr1 = [];
|
||||
arr1.length = 2;
|
||||
var arr2 = [];
|
||||
arr2.length = 3;
|
||||
assert(arr1.concat(arr2).length === arr1.length + arr2.length);
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var arr = []
|
||||
Object.defineProperty(arr, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
arr.length = 1;
|
||||
|
||||
try {
|
||||
arr.concat();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.4.5.
|
||||
Checking behavior when unable to get element from a given array */
|
||||
arr1 = [];
|
||||
arr2 = [];
|
||||
arr3 = [];
|
||||
Object.defineProperty(arr2, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
arr1.concat(arr2, arr3);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// 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 array = ["foo", [], Infinity, 4];
|
||||
|
||||
function f(arg1, arg2, arg3) {
|
||||
assert(arg1 === array[arg2]);
|
||||
assert(arg3 === array);
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(array.every(f) === true);
|
||||
|
||||
function g(arg1, arg2, arg3) {
|
||||
if (arg1 === 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var arr1 = [1, 1, 1, 1, 1, 2];
|
||||
assert(arr1.every(g) === false);
|
||||
|
||||
var arr2 = [1, 1, 1, 1, 1, 1];
|
||||
assert(arr2.every(g) === true);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { every : Array.prototype.every };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.every(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { every : Array.prototype.every, length : 1};
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.every(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// 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 array = ["foo", [], Infinity, 4]
|
||||
|
||||
function f(arg1, arg2, arg3) {
|
||||
assert(arg1 === array[arg2]);
|
||||
assert(arg3 === array);
|
||||
return true;
|
||||
}
|
||||
|
||||
var filtered = array.filter(f);
|
||||
assert(filtered.length === array.length);
|
||||
for (i = 0; i < filtered.length; i++) {
|
||||
assert(filtered[i] === array[i]);
|
||||
}
|
||||
|
||||
var array = [1, 2, 3, 4, 5, 6, 7, 8];
|
||||
|
||||
function g (arg1, arg2, arg3) {
|
||||
if (arg2 % 2 === 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
filtered = array.filter(g)
|
||||
assert(filtered.length === 4);
|
||||
assert(filtered[0] === 1);
|
||||
assert(filtered[1] === 3);
|
||||
assert(filtered[2] === 5);
|
||||
assert(filtered[3] === 7);
|
||||
|
||||
var arr = [1,2];
|
||||
Array.prototype[0] = 3;
|
||||
var newArr = arr.filter(function() { return true; });
|
||||
delete Array.prototype[0];
|
||||
assert(newArr.hasOwnProperty("0"));
|
||||
assert(newArr[0] === 1);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.filter = Array.prototype.filter;
|
||||
|
||||
try {
|
||||
obj.filter(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.filter = Array.prototype.filter
|
||||
|
||||
try {
|
||||
obj.filter(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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 array = ["foo", [], Infinity, 4]
|
||||
|
||||
function f(arg1, arg2, arg3) {
|
||||
assert(arg1 === array[arg2]);
|
||||
assert(arg3 === array);
|
||||
}
|
||||
|
||||
array.forEach(f);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.forEach = Array.prototype.forEach;
|
||||
|
||||
try {
|
||||
obj.forEach(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.forEach = Array.prototype.forEach
|
||||
|
||||
try {
|
||||
obj.forEach(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// 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 = ["foo", 19, "bar", obj, "foo", 29, "baz"];
|
||||
|
||||
var index = array.indexOf("foo");
|
||||
assert(index === 0);
|
||||
assert(array[index] === "foo");
|
||||
|
||||
assert(array.indexOf("foo", 1) === 4);
|
||||
assert(array.indexOf("foo", 5) === -1);
|
||||
|
||||
var index = array.indexOf("baz");
|
||||
assert(index === 6);
|
||||
assert(array[index] === "baz");
|
||||
|
||||
assert(array.indexOf("baz", 7) === -1);
|
||||
|
||||
var index = array.indexOf(obj);
|
||||
assert(index === 3);
|
||||
assert(array[index] === obj);
|
||||
|
||||
assert(array.indexOf("foo", NaN) === 0);
|
||||
assert(array.indexOf("foo", Infinity) === -1);
|
||||
assert(array.indexOf("foo", -Infinity) === 0);
|
||||
|
||||
assert([true].indexOf(true, -0) === 0);
|
||||
|
||||
// Checking behavior when length is zero
|
||||
var obj = { indexOf : Array.prototype.indexOf, length : 0 };
|
||||
assert(obj.indexOf("foo") === -1);
|
||||
|
||||
// Checking behavior when start index >= length
|
||||
var arr = [11, 22, 33, 44];
|
||||
assert(arr.indexOf(44, 4) === -1);
|
||||
|
||||
var fromIndex = {
|
||||
toString: function () {
|
||||
return {};
|
||||
},
|
||||
|
||||
valueOf: function () {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
[0, 1].indexOf(1, fromIndex);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { indexOf : Array.prototype.indexOf}
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.indexOf("bar");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { indexOf : Array.prototype.indexOf, length : 1}
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.indexOf("bar");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.indexOf(4, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === -1);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3];
|
||||
var value = array.indexOf(2, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === 1);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7];
|
||||
var value = array.indexOf(6, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === -1);
|
||||
@@ -0,0 +1,89 @@
|
||||
// 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.
|
||||
|
||||
assert ([].join() === "");
|
||||
assert ([1].join() === "1");
|
||||
assert ([1, 2].join() === "1,2");
|
||||
|
||||
|
||||
assert ([].join('--') === "");
|
||||
assert ([1].join("--") === "1");
|
||||
assert ([1, 2].join('--') === "1--2");
|
||||
|
||||
assert ([1,2,3].join({toString: function() { return "--"; }}) === "1--2--3");
|
||||
|
||||
|
||||
// Join should use 'length' to as the number of elements int the array.
|
||||
var lst = [1,2,3,4];
|
||||
lst.length = 3;
|
||||
assert (lst.join() === [1,2,3].join());
|
||||
|
||||
// Checking behavior when unable to get length.
|
||||
var obj = {};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.join = Array.prototype.join;
|
||||
|
||||
try {
|
||||
obj.join();
|
||||
// Should not be reached.
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Check join argument fail.
|
||||
try {
|
||||
[1,2,3].join({toString: function() { throw new ReferenceError ("foo"); }});
|
||||
// Should not be reached.
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Check single join element fail.
|
||||
try {
|
||||
[1, 2, {toString: function() { throw new ReferenceError ("foo"); }}, 4].join();
|
||||
// Should not be reached.
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Check join on different object.
|
||||
var obj_2 = {};
|
||||
obj_2.length = 3;
|
||||
obj_2[0] = 1;
|
||||
obj_2[1] = 2;
|
||||
obj_2[2] = 3;
|
||||
obj_2[3] = 4;
|
||||
|
||||
obj_2.join = Array.prototype.join;
|
||||
|
||||
assert (obj_2.join() === "1,2,3");
|
||||
|
||||
/* ES v5.1 15.4.4.5.7.
|
||||
Checking behavior when an element throws error */
|
||||
try {
|
||||
var f = function () { throw new TypeError("ooo");};
|
||||
var arr = [0, 1, 2, 3];
|
||||
Object.defineProperty(arr, '0', { 'get' : f });
|
||||
Array.prototype.join.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
assert(e.message == "ooo");
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
var obj = {};
|
||||
var array = ["foo", 19, "bar", obj, "foo", 29, "baz"];
|
||||
|
||||
var index = array.lastIndexOf("foo");
|
||||
assert(index === 4);
|
||||
assert(array[index] === "foo");
|
||||
|
||||
assert(array.lastIndexOf("foo", 3) === 0);
|
||||
assert(array.lastIndexOf("foo", -8) === -1);
|
||||
|
||||
var index = array.lastIndexOf("baz");
|
||||
assert(index === 6);
|
||||
assert(array[index] === "baz");
|
||||
|
||||
assert(array.lastIndexOf("baz", -2) === -1);
|
||||
|
||||
var index = array.lastIndexOf(obj);
|
||||
assert(index === 3);
|
||||
assert(array[index] === obj);
|
||||
|
||||
assert(array.lastIndexOf("foo", NaN) === 0);
|
||||
assert(array.lastIndexOf("foo", Infinity) === 4);
|
||||
assert(array.lastIndexOf("foo", -Infinity) === -1);
|
||||
|
||||
var arr = [];
|
||||
arr[4294967294] = "foo";
|
||||
assert(arr.lastIndexOf("foo", -1) === 4294967294)
|
||||
|
||||
var arr = [1,2];
|
||||
assert(arr.lastIndexOf(2, undefined) === -1);
|
||||
assert(arr.lastIndexOf(2) === 1);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { lastIndexOf : Array.prototype.lastIndexOf}
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.lastIndexOf("bar");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { lastIndexOf : Array.prototype.lastIndexOf, length : 1}
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.lastIndexOf("bar");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.lastIndexOf(4, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === -1);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3];
|
||||
var value = array.lastIndexOf(1, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === 0);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7];
|
||||
var value = array.indexOf(5, {
|
||||
valueOf: function() {
|
||||
array.length = 2;
|
||||
}
|
||||
})
|
||||
|
||||
assert(value === -1);
|
||||
@@ -0,0 +1,111 @@
|
||||
// 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.
|
||||
|
||||
// helper function - simple implementation
|
||||
Array.prototype.equals = function (array) {
|
||||
if (this.length != array.length)
|
||||
return false;
|
||||
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
if (this[i] instanceof Array && array[i] instanceof Array) {
|
||||
if (!this[i].equals(array[i]))
|
||||
return false;
|
||||
}
|
||||
else if (this[i] != array[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// check function type
|
||||
try {
|
||||
[0].map(new Object());
|
||||
assert(false);
|
||||
} catch(e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// various checks
|
||||
assert ([1, 4, 9].map(Math.sqrt).equals([1, 2, 3]));
|
||||
|
||||
assert (isNaN([1, 4, "X"].map(Number)[2]));
|
||||
|
||||
var func = function(val, idx) {
|
||||
return val + idx;
|
||||
};
|
||||
|
||||
assert ([1, 4, 9].map(func).equals([1,5,11]));
|
||||
|
||||
assert ([1, "X", 10].map(func).equals([1, "X1", 12]));
|
||||
|
||||
var arr = [1,2,3];
|
||||
arr.length = 5;
|
||||
assert(arr.map(func).length === arr.length);
|
||||
|
||||
var long_array = [0, 1];
|
||||
assert (long_array.map(func).equals([0,2]));
|
||||
|
||||
long_array[100] = 1;
|
||||
assert (long_array.map(func).equals([0,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101]));
|
||||
|
||||
var arr = [1,2];
|
||||
Array.prototype[0] = 3;
|
||||
var newArr = arr.map(function(value) { return value; });
|
||||
delete Array.prototype[0];
|
||||
assert(newArr.hasOwnProperty("0"));
|
||||
assert(newArr[0] === 1);
|
||||
|
||||
// check behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.map = Array.prototype.map;
|
||||
|
||||
try {
|
||||
obj.map(func);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// check behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
obj.map = Array.prototype.map
|
||||
|
||||
try {
|
||||
obj.map(func);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// check thisArg
|
||||
var thisArg = {add: 10};
|
||||
var func2 = function(value) {
|
||||
return this.add + value;
|
||||
}
|
||||
assert([1,2].map(func2, thisArg).equals([11, 12]));
|
||||
|
||||
// check passed Object
|
||||
var array_example = [1,2];
|
||||
Object.defineProperty(array_example, 'const', { 'get' : function () {return "CT";} });
|
||||
var func3 = function(value, idx, thisobj) {
|
||||
return value * idx + thisobj.const;
|
||||
}
|
||||
assert(array_example.map(func3).equals(["0CT", "2CT"]));
|
||||
@@ -0,0 +1,98 @@
|
||||
// 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 array = ["foo", [], Infinity, 4]
|
||||
assert(array.length === 4);
|
||||
|
||||
assert(array.pop() === 4)
|
||||
assert(array.length === 3);
|
||||
|
||||
assert(array.pop() === Infinity);
|
||||
assert(array.length === 2);
|
||||
|
||||
var a = array.pop()
|
||||
assert(a instanceof Array);
|
||||
assert(array.length === 1);
|
||||
|
||||
assert(array.pop() === "foo");
|
||||
assert(array.length === 0);
|
||||
|
||||
assert(array.pop() === undefined);
|
||||
assert(array.length === 0);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { pop : Array.prototype.pop };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.pop();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to set length
|
||||
var obj = { pop : Array.prototype.pop };
|
||||
Object.defineProperty(obj, 'length', { 'set' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.pop();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when no length property defined
|
||||
var obj = { pop : Array.prototype.pop };
|
||||
assert(obj.length === undefined)
|
||||
assert(obj.pop() === undefined)
|
||||
assert(obj.length === 0)
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { pop : Array.prototype.pop, length : 1 };
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.pop();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.6.5.c
|
||||
Checking behavior when unable to delete property */
|
||||
var obj = {pop : Array.prototype.pop, length : 2};
|
||||
Object.defineProperty(obj, '1', function () {});
|
||||
|
||||
try {
|
||||
obj.pop();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.6.5.d
|
||||
Checking behavior when array is not modifiable */
|
||||
var obj = {pop : Array.prototype.pop, length : 2};
|
||||
Object.freeze(obj);
|
||||
|
||||
try {
|
||||
obj.pop();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
var len;
|
||||
var d = [];
|
||||
assert (d.length === 0);
|
||||
len = d.push();
|
||||
assert (d.length === 0);
|
||||
assert (d.length === len);
|
||||
len = d.push(1);
|
||||
assert (d.length === 1);
|
||||
assert (d.length === len);
|
||||
len = d.push(2);
|
||||
assert (d.length === 2);
|
||||
assert (d.length === len);
|
||||
len = d.push('a');
|
||||
assert (d.length === 3);
|
||||
assert (d.length === len);
|
||||
len = d.push('b', 'c', 3);
|
||||
assert (d.length == 6);
|
||||
assert (d.length === len);
|
||||
assert (d[0] === 1);
|
||||
assert (d[1] === 2);
|
||||
assert (d[2] === 'a');
|
||||
assert (d[3] === 'b');
|
||||
assert (d[4] === 'c');
|
||||
assert (d[5] === 3);
|
||||
|
||||
var a = [];
|
||||
a.length = 4294967294;
|
||||
assert(a.push("x") === 4294967295);
|
||||
assert(a.length === 4294967295);
|
||||
assert(a[4294967294] === "x");
|
||||
|
||||
try {
|
||||
a.push("y");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof RangeError);
|
||||
}
|
||||
assert(a.length === 4294967295)
|
||||
|
||||
var o = { length : 4294967294, push : Array.prototype.push };
|
||||
assert(o.push("x") === 4294967295);
|
||||
assert(o.length === 4294967295);
|
||||
assert(o[4294967294] === "x");
|
||||
|
||||
try {
|
||||
assert(o.push("y") === 4294967296);
|
||||
} catch (e) {
|
||||
assert(false);
|
||||
}
|
||||
assert(o.length === 4294967296);
|
||||
assert(o[4294967295] === "y");
|
||||
|
||||
/* ES v5.1 15.4.4.7.5.
|
||||
Checking behavior when array is non-extensible while pushing */
|
||||
var arr = [];
|
||||
Object.freeze(arr);
|
||||
|
||||
try {
|
||||
arr.push(1, 2);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
var func = function(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// check function type
|
||||
try {
|
||||
[0].reduceRight(new Object());
|
||||
assert(false);
|
||||
} catch(e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// check for init value
|
||||
try {
|
||||
[].reduceRight(func);
|
||||
assert(false);
|
||||
} catch(e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
var a = new Array();
|
||||
a.length = 10;
|
||||
a.reduceRight(func);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
|
||||
// various checks
|
||||
assert([].reduceRight(func, 1) === 1);
|
||||
|
||||
assert([].reduceRight(func, undefined) === undefined);
|
||||
|
||||
assert([0].reduceRight(func) === 0);
|
||||
|
||||
assert([0, 1].reduceRight(func) === 1);
|
||||
|
||||
assert([0, 1].reduceRight(func, 1) === 2);
|
||||
|
||||
assert([0, 1, 2, 3].reduceRight(func, 1) === 7);
|
||||
|
||||
assert (["A","B"].reduceRight(func) === "BA");
|
||||
|
||||
assert (["A","B"].reduceRight(func, "Init:") === "Init:BA");
|
||||
|
||||
assert ([0, 1].reduceRight(func, 3.2) === 4.2);
|
||||
|
||||
assert ([0, "x", 1].reduceRight(func) === "1x0");
|
||||
|
||||
assert ([0, "x", 1].reduceRight(func, 3.2) === "4.2x0");
|
||||
|
||||
var long_array = [0, 1];
|
||||
assert (long_array.reduceRight(func,10) === 11);
|
||||
|
||||
long_array[10000] = 1;
|
||||
assert (long_array.reduceRight(func,10) === 12);
|
||||
|
||||
var accessed = false;
|
||||
function callbackfn(prevVal, curVal, idx, obj) {
|
||||
accessed = true;
|
||||
return typeof prevVal === "undefined";
|
||||
}
|
||||
|
||||
var obj = { 0: 11, length: 1 };
|
||||
|
||||
assert (Array.prototype.reduceRight.call(obj, callbackfn, undefined) === true && accessed);
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
var func = function(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// check function type
|
||||
try {
|
||||
[0].reduce(new Object());
|
||||
assert(false);
|
||||
}
|
||||
catch(e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// check for init value
|
||||
try {
|
||||
[].reduce(func);
|
||||
assert(false);
|
||||
}
|
||||
catch(e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// various checks
|
||||
assert ([].reduce(func, 1) === 1);
|
||||
|
||||
assert ([].reduce(func, undefined) === undefined);
|
||||
|
||||
assert ([0].reduce(func) === 0);
|
||||
|
||||
assert ([0, 1].reduce(func) === 1);
|
||||
|
||||
assert ([0, 1].reduce(func, 1) === 2);
|
||||
|
||||
assert ([0, 1, 2, 3].reduce(func, 1) === 7);
|
||||
|
||||
assert (["A","B"].reduce(func) === "AB");
|
||||
|
||||
assert (["A","B"].reduce(func, "Init:") === "Init:AB");
|
||||
|
||||
assert ([0, 1].reduce(func, 3.2) === 4.2);
|
||||
|
||||
assert ([0, "x", 1].reduce(func) === "0x1");
|
||||
|
||||
assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1");
|
||||
|
||||
var long_array = [0, 1];
|
||||
assert (long_array.reduce(func,10) === 11);
|
||||
|
||||
long_array[10000] = 1;
|
||||
assert (long_array.reduce(func,10) === 12);
|
||||
|
||||
var accessed = false;
|
||||
function callbackfn(prevVal, curVal, idx, obj) {
|
||||
accessed = true;
|
||||
return typeof prevVal === "undefined";
|
||||
}
|
||||
|
||||
var obj = { 0: 11, length: 1 };
|
||||
|
||||
assert (Array.prototype.reduce.call(obj, callbackfn, undefined) === true && accessed);
|
||||
@@ -0,0 +1,119 @@
|
||||
// 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 array = [4, 3, 2, 1, 0]
|
||||
|
||||
array.reverse();
|
||||
|
||||
for (i = 0; i < array.length; i++) {
|
||||
assert(array[i] === i);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { reverse : Array.prototype.reverse };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.reverse();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { reverse : Array.prototype.reverse, length : 3 };
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.reverse();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.e.
|
||||
Checking behavior when unable to get the last element */
|
||||
var obj = { reverse : Array.prototype.reverse, length : 4 };
|
||||
Object.defineProperty(obj, '3', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.reverse();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.h.i.
|
||||
Checking behavior when first 3 elements are not writable */
|
||||
try {
|
||||
var arr = [,,, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
|
||||
Object.defineProperty(arr, '0', {});
|
||||
Object.defineProperty(arr, '1', {});
|
||||
Object.defineProperty(arr, '2', {});
|
||||
Array.prototype.reverse.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.h.ii.
|
||||
Checking behavior when last 3 elements are not writable */
|
||||
try {
|
||||
var arr = [0, 1, 2, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3,,,];
|
||||
Object.defineProperty(arr, '19', {});
|
||||
Object.defineProperty(arr, '20', {});
|
||||
Object.defineProperty(arr, '21', {});
|
||||
Array.prototype.reverse.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.i.i.
|
||||
Checking behavior when first elements do not exist and the array is freezed */
|
||||
try {
|
||||
var arr = [,,,,,,,,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
|
||||
arr = Object.freeze(arr);
|
||||
Array.prototype.reverse.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.i.ii.
|
||||
Checking behavior when unable to get the first 2 elements */
|
||||
var obj = { reverse : Array.prototype.reverse, length : 4 };
|
||||
Object.defineProperty(obj, '2', { value : 0 });
|
||||
Object.defineProperty(obj, '3', { value : 0 });
|
||||
try {
|
||||
obj.reverse();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.8.6.j.i.
|
||||
Checking behavior when unable to get the last 2 elements */
|
||||
var obj = { reverse : Array.prototype.reverse, length : 4 };
|
||||
Object.defineProperty(obj, '0', { value : 0 });
|
||||
Object.defineProperty(obj, '1', { value : 0 });
|
||||
try {
|
||||
obj.reverse();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// 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 array = ["foo", [], Infinity, 4]
|
||||
|
||||
assert(array.length === 4);
|
||||
|
||||
assert(array.shift() === "foo");
|
||||
assert(array.length === 3);
|
||||
|
||||
var a = array.shift();
|
||||
assert(a instanceof Array);
|
||||
assert(array.length === 2);
|
||||
|
||||
assert(array.shift() === Infinity);
|
||||
assert(array.length === 1);
|
||||
|
||||
assert(array.shift() === 4);
|
||||
assert(array.length === 0);
|
||||
|
||||
assert(array.shift() === undefined);
|
||||
assert(array.length === 0);
|
||||
|
||||
var referenceErrorThrower = function () {
|
||||
throw new ReferenceError ("foo");
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { shift : Array.prototype.shift };
|
||||
Object.defineProperty(obj, 'length', { 'get' : referenceErrorThrower });
|
||||
|
||||
try {
|
||||
obj.shift();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to set length
|
||||
var obj = { shift : Array.prototype.shift };
|
||||
Object.defineProperty(obj, 'length', { 'set' : referenceErrorThrower });
|
||||
|
||||
try {
|
||||
obj.shift();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when no length property defined
|
||||
var obj = { shift : Array.prototype.shift };
|
||||
assert (obj.length === undefined)
|
||||
assert (obj.shift() === undefined)
|
||||
assert (obj.length === 0)
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { shift : Array.prototype.shift, length : 1 };
|
||||
Object.defineProperty(obj, '0', { 'get' : referenceErrorThrower });
|
||||
|
||||
try {
|
||||
obj.shift();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.9.7.c.
|
||||
Checking behavior when the array is freezed */
|
||||
try {
|
||||
f = function () { throw new ReferenceError("getter"); };
|
||||
arr = { length : 9 };
|
||||
Object.defineProperty(arr, '8', { 'get' : f });
|
||||
Array.prototype.shift.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError);
|
||||
assert(e.message == "getter");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.9.7.d.ii.
|
||||
Checking behavior when the array is freezed */
|
||||
try {
|
||||
arr = { length : 9 };
|
||||
Object.defineProperty(arr, '8', { value : 8 });
|
||||
Object.defineProperty(arr, '7', { value : 7 });
|
||||
Array.prototype.shift.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.9.7.e.i.
|
||||
Checking behavior when the first element is null */
|
||||
try {
|
||||
arr = { length : 9 };
|
||||
Object.defineProperty(arr, '0', { value : null });
|
||||
Array.prototype.shift.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.9.8.
|
||||
Checking behavior when last element is not writable */
|
||||
try {
|
||||
arr = { length : 9 };
|
||||
Object.defineProperty(arr, '8', { writable : false });
|
||||
Array.prototype.shift.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.9.9.
|
||||
Checking behavior when the array is freezed */
|
||||
try {
|
||||
arr = { length : 9 };
|
||||
Object.freeze(arr);
|
||||
Array.prototype.shift.call(arr);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
var array = [54, undefined, "Lemon", -127];
|
||||
|
||||
var array1 = array.slice();
|
||||
var array2 = array.slice("a", "3");
|
||||
var array3 = array.slice(-2);
|
||||
var array4 = array.slice(-12, undefined);
|
||||
var array5 = array.slice(undefined, -3);
|
||||
var array6 = array.slice(Infinity, NaN);
|
||||
var array7 = array.slice(-Infinity, Infinity);
|
||||
var array8 = array.slice(NaN, -Infinity);
|
||||
|
||||
assert (array1.length == 4);
|
||||
assert (array1[0] == 54);
|
||||
assert (array1[1] == undefined);
|
||||
assert (array1[2] == "Lemon");
|
||||
assert (array1[3] == -127);
|
||||
|
||||
assert (array2.length == 3);
|
||||
assert (array2[0] == 54);
|
||||
assert (array2[1] == undefined);
|
||||
assert (array2[2] == "Lemon");
|
||||
|
||||
assert (array3.length == 2);
|
||||
assert (array3[0] == "Lemon");
|
||||
assert (array3[1] == -127);
|
||||
|
||||
assert (array4.length == 4);
|
||||
assert (array4[0] == 54);
|
||||
assert (array4[1] == undefined);
|
||||
assert (array4[2] == "Lemon");
|
||||
assert (array4[3] == -127);
|
||||
|
||||
assert (array5.length == 1);
|
||||
assert (array5[0] == 54);
|
||||
|
||||
assert (array6.length == 0);
|
||||
|
||||
assert (array7.length == 4);
|
||||
assert (array7[0] == 54);
|
||||
assert (array7[1] == undefined);
|
||||
assert (array7[2] == "Lemon");
|
||||
assert (array7[3] == -127);
|
||||
|
||||
assert (array8.length == 0);
|
||||
|
||||
var array = [];
|
||||
array[4294967293] = "foo";
|
||||
array.length = 4294967295;
|
||||
var result = array.slice(4294967293, -1)
|
||||
assert(result.length === 1)
|
||||
assert(result[0] === "foo")
|
||||
|
||||
array[0] = "bar";
|
||||
var result = array.slice(-4294967295, -4294967294)
|
||||
assert(result.length === 1)
|
||||
assert(result[0] === "bar")
|
||||
|
||||
var array = [];
|
||||
array[0] = "foo";
|
||||
var result = array.slice(4294967296, 4294967297);
|
||||
assert(result.length === 0);
|
||||
|
||||
array[4294967293] = "bar";
|
||||
var result = array.slice(-4294967297, -4294967296);
|
||||
assert(result.length === 0);
|
||||
|
||||
var arr = [1,2];
|
||||
Array.prototype[0] = 3;
|
||||
var newArr = arr.slice(0, 1);
|
||||
delete Array.prototype[0];
|
||||
assert(newArr.hasOwnProperty("0"));
|
||||
assert(newArr[0] === 1);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { slice : Array.prototype.slice };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.slice(1, 2);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { length : 1, slice : Array.prototype.slice };
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.slice(0, 1);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.10.5.
|
||||
Checking behavior when start value throws exception */
|
||||
var arg1 = { };
|
||||
Object.defineProperty(arg1, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
var obj = { slice : Array.prototype.slice };
|
||||
|
||||
try {
|
||||
obj.slice(arg1);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === 'foo');
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.10.7.
|
||||
Checking behavior when end value throws exception */
|
||||
var arg2 = { };
|
||||
Object.defineProperty(arg2, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
var obj = { slice : Array.prototype.slice };
|
||||
|
||||
try {
|
||||
obj.slice(0, arg2);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === 'foo');
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.10.10.
|
||||
Checking behavior when unable to get element */
|
||||
var obj = { length : 3, slice : Array.prototype.slice };
|
||||
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.slice(0, 3);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
function array_check(result_array, expected_array) {
|
||||
assert(result_array instanceof Array);
|
||||
assert(result_array.length === expected_array.length);
|
||||
for (var idx = 0; idx < expected_array.length; idx++) {
|
||||
assert(result_array[idx] === expected_array[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.slice(4, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, []);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.slice(6, {
|
||||
valueOf: function() {
|
||||
array.length = 10;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, []);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.slice(1, {
|
||||
valueOf: function() {
|
||||
array.length = 3;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, []);
|
||||
@@ -0,0 +1,61 @@
|
||||
// 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 array = ["foo", [], Infinity, 4];
|
||||
|
||||
function f(arg1, arg2, arg3) {
|
||||
assert(arg1 === array[arg2]);
|
||||
assert(arg3 === array);
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(array.some(f) === false);
|
||||
|
||||
function g(arg1, arg2, arg3) {
|
||||
if (arg1 === 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var arr1 = [2, 2, 2, 2, 2, 2];
|
||||
assert(arr1.some(g) === false);
|
||||
|
||||
var arr2 = [2, 2, 2, 2, 2, 1];
|
||||
assert(arr2.some(g) === true);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { some : Array.prototype.some };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.some(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { some : Array.prototype.some, length : 1};
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.some(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// 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 array = ["Peach", "Apple", "Orange", "Grape", "Cherry", "Apricot", "Grapefruit"];
|
||||
array.sort();
|
||||
|
||||
assert(array[0] === "Apple");
|
||||
assert(array[1] === "Apricot");
|
||||
assert(array[2] === "Cherry");
|
||||
assert(array[3] === "Grape");
|
||||
assert(array[4] === "Grapefruit");
|
||||
assert(array[5] === "Orange");
|
||||
assert(array[6] === "Peach");
|
||||
|
||||
var array = [6, 4, 5, 1, 2, 9, 7, 3, 0, 8];
|
||||
|
||||
// Default comparison
|
||||
array.sort();
|
||||
for (i = 0; i < array.length; i++) {
|
||||
assert(array[i] === i);
|
||||
}
|
||||
|
||||
// Using custom comparison function
|
||||
function f(arg1, arg2) {
|
||||
if (arg1 < arg2) {
|
||||
return 1;
|
||||
} else if (arg1 > arg2) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
array.sort(f);
|
||||
for (i = 0; i < array.length; i++) {
|
||||
assert(array[array.length - i - 1] === i);
|
||||
}
|
||||
|
||||
// Sorting sparse array
|
||||
var array = [1,,2,,3,,4,undefined,5];
|
||||
var expected = [1,2,3,4,5,undefined,,,,];
|
||||
|
||||
array.sort();
|
||||
|
||||
assert(array.length === expected.length);
|
||||
for (i = 0; i < array.length; i++) {
|
||||
assert(expected.hasOwnProperty (i) === array.hasOwnProperty (i));
|
||||
assert(array[i] === expected[i]);
|
||||
}
|
||||
|
||||
// Checking behavior when provided comparefn is not callable
|
||||
var obj = {};
|
||||
var arr = [];
|
||||
try {
|
||||
arr.sort(obj);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { sort : Array.prototype.sort}
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { sort : Array.prototype.sort, length : 1}
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get elements
|
||||
var obj = { sort : Array.prototype.sort, length : 2};
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("bar"); } });
|
||||
|
||||
try {
|
||||
obj.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when array is non-extensible while sorting
|
||||
var arr = [1, 0];
|
||||
|
||||
try {
|
||||
arr.sort(function () { Object.freeze(arr) });
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to delete property
|
||||
var obj = {sort : Array.prototype.sort, '0' : 2, '1' : 1, length : 4};
|
||||
Object.defineProperty(obj, '3', function () {});
|
||||
|
||||
try {
|
||||
obj.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get the last element
|
||||
var arr = [1, 2, ];
|
||||
Object.defineProperty(arr, '2', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
arr.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === 'foo');
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when lhs_value throws exception at comparefn
|
||||
f = function () { throw new ReferenceError('foo'); };
|
||||
obj = { 'toString' : f };
|
||||
arr = [obj, 1];
|
||||
|
||||
try {
|
||||
arr.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === 'foo');
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when rhs_value throws exception at comparefn
|
||||
f = function () { throw new ReferenceError('foo'); };
|
||||
obj = { 'toString' : f };
|
||||
arr = [1, obj];
|
||||
|
||||
try {
|
||||
arr.sort();
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === 'foo');
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Sorting when array elements are the same string
|
||||
arr = ['foo', 'foo'];
|
||||
arr.sort();
|
||||
|
||||
assert(arr[0] === 'foo');
|
||||
assert(arr[1] === 'foo');
|
||||
|
||||
// Checking behavior when comparefn's call value cannot be converted to number
|
||||
obj = { };
|
||||
Object.defineProperty(obj, 'toString', function () { });
|
||||
f = function () { return obj };
|
||||
arr = [1, 2];
|
||||
|
||||
try {
|
||||
arr.sort(f);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
// 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.
|
||||
|
||||
// This test will not pass on FLOAT32 due to precision issues
|
||||
|
||||
function setDefaultValues()
|
||||
{
|
||||
return [54, undefined, -127, "sunshine"];
|
||||
}
|
||||
|
||||
var array = setDefaultValues();
|
||||
var array1 = array.splice();
|
||||
|
||||
assert (array.length == 4);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == -127);
|
||||
assert (array[3] == "sunshine");
|
||||
assert (array1.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array2 = array.splice(2);
|
||||
|
||||
assert (array.length == 2);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array2.length == 2);
|
||||
assert (array2[0] == -127);
|
||||
assert (array2[1] == "sunshine");
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array3 = array.splice(2, 1);
|
||||
|
||||
assert (array.length == 3);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == "sunshine");
|
||||
assert (array3.length == 1);
|
||||
assert (array3[0] == -127);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array4 = array.splice(0, 3, 6720, "Szeged");
|
||||
|
||||
assert (array.length == 3);
|
||||
assert (array[0] == 6720);
|
||||
assert (array[1] == "Szeged");
|
||||
assert (array[2] == "sunshine");
|
||||
assert (array4.length == 3);
|
||||
assert (array4[0] == 54);
|
||||
assert (array4[1] == undefined);
|
||||
assert (array4[2] == -127);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array5 = array.splice(-2, -2, 6720, "Szeged");
|
||||
|
||||
assert (array.length == 6);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == 6720);
|
||||
assert (array[3] == "Szeged");
|
||||
assert (array[4] == -127);
|
||||
assert (array[5] == "sunshine");
|
||||
assert (array5.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array6 = array.splice(undefined, undefined, undefined);
|
||||
|
||||
assert (array.length == 5);
|
||||
assert (array[0] == undefined);
|
||||
assert (array[1] == 54);
|
||||
assert (array[2] == undefined);
|
||||
assert (array[3] == -127);
|
||||
assert (array[4] == "sunshine");
|
||||
assert (array6.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array7 = array.splice(Infinity, NaN);
|
||||
assert (array.length == 4);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == -127);
|
||||
assert (array[3] == "sunshine");
|
||||
assert (array7.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array8 = array.splice(-Infinity, Infinity);
|
||||
|
||||
assert (array.length == 0);
|
||||
assert (array8.length == 4);
|
||||
assert (array8[0] == 54);
|
||||
assert (array8[1] == undefined);
|
||||
assert (array8[2] == -127);
|
||||
assert (array8[3] == "sunshine");
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array9 = array.splice(NaN, -Infinity);
|
||||
assert (array.length == 4);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == -127);
|
||||
assert (array[3] == "sunshine");
|
||||
assert (array9.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues(); // 54, undefined, -127, "sunshine"
|
||||
var array10 = array.splice(-3, 4, Infinity, "university");
|
||||
assert (array.length == 3);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == Infinity);
|
||||
assert (array[2] == "university");
|
||||
assert (array10.length == 3);
|
||||
assert (array10[0] == undefined);
|
||||
assert (array10[1] == -127);
|
||||
assert (array10[2] == "sunshine");
|
||||
|
||||
var array = [];
|
||||
array[4294967294] = "foo";
|
||||
var result = array.splice(4294967294, 1, "x")
|
||||
assert(result.length === 1)
|
||||
assert(result[0] === "foo")
|
||||
assert(array[4294967294] === "x")
|
||||
|
||||
array[0] = "bar";
|
||||
var result = array.splice(-4294967295, 1, "y");
|
||||
assert(result.length === 1)
|
||||
assert(result[0] === "bar")
|
||||
assert(array[0] === "y")
|
||||
|
||||
var arr = [1,2];
|
||||
Array.prototype[0] = 3;
|
||||
var newArr = arr.splice(0, 1);
|
||||
delete Array.prototype[0];
|
||||
assert(newArr.hasOwnProperty("0"));
|
||||
assert(newArr[0] === 1);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {splice : Array.prototype.splice};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.splice(1, 2, "item1", "item2");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {length : 1, splice : Array.prototype.splice};
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.splice(0, 1, "item1", "item2");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.5.
|
||||
Checking behavior when the first argument of the function is an object, which throws error */
|
||||
try {
|
||||
var o = {};
|
||||
Object.defineProperty(o, 'toString', { 'get' : function() { throw new ReferenceError("1"); } });
|
||||
[1, 2].splice(o);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError);
|
||||
assert(e.message == "1");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.7.
|
||||
Checking behavior when the second argument of the function is an object, which throws error */
|
||||
try {
|
||||
var o = {};
|
||||
Object.defineProperty(o, 'toString', { 'get' : function() { throw new ReferenceError("2"); } });
|
||||
[1, 2].splice(1, o);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError);
|
||||
assert(e.message == "2");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.9.b
|
||||
Checking behavior when the first element throws error */
|
||||
try {
|
||||
var a = [1, 5, 6, 7, 8, 5];
|
||||
Object.defineProperty(a, '0', { 'get' : function() { throw new ReferenceError("foo0"); } });
|
||||
Array.prototype.splice.call(a, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError);
|
||||
assert(e.message == "foo0");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.12.b.iii.
|
||||
Checking behavior when an element of the array throws error */
|
||||
function f0() { throw new TypeError("4"); };
|
||||
|
||||
try {
|
||||
obj = {get: f0, valueOf : f0, toString: f0};
|
||||
arr = [1, 2, obj, 4, 5];
|
||||
Object.defineProperty(arr, '4', { 'get' : f0 });
|
||||
arr.splice(1, 3, obj);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
assert(e.message == "4");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12 12.b.iv.
|
||||
Checking behavior when a modified object is an element of the array */
|
||||
function f() {
|
||||
delete arr[3];
|
||||
arr.length = 13;
|
||||
Object.defineProperty(arr, '5', function() { });
|
||||
};
|
||||
|
||||
try {
|
||||
obj = {get: f, valueOf : f, toString: f};
|
||||
arr = [1, 2, obj, 4, 5];
|
||||
Object.defineProperty(arr, '2',{ 'get' : f } );
|
||||
for(var i = 0; i < arr.length; i++) {
|
||||
var a = arr[i];
|
||||
}
|
||||
arr.splice(1, 4, obj);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.12.b.v.
|
||||
Checking behavior when elements are getting deleted by an element which only has a get function */
|
||||
function f1() {
|
||||
for(var i = 0; i < arr.length; i++) {
|
||||
delete arr[i];
|
||||
}
|
||||
};
|
||||
|
||||
try{
|
||||
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
delete arr[2];
|
||||
Object.defineProperty(arr, '2', { 'get' : f1 });
|
||||
arr.splice(1, 7, 5);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.12.d.i.
|
||||
Checking behavior when a modified object is an element of the array and deletes the elements */
|
||||
function f2() {
|
||||
for(var i = 0; i < arr.length; i++) {
|
||||
delete arr[i];
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
obj = {get: f2, valueOf : f2, toString: f2 };
|
||||
arr = [1, 2, obj, 4, 5];
|
||||
for(var i = 0; i < 6; i++) {
|
||||
Object.defineProperty(arr, i, { 'get' : f2 });
|
||||
}
|
||||
arr.splice(1, 3, obj);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.13.b.iii.
|
||||
Checking behavior when a yet non existing element will throw an error */
|
||||
function f3() { throw new TypeError("6");};
|
||||
|
||||
try {
|
||||
arr = [1, 2, 4, 5];
|
||||
Object.defineProperty(arr, '4',{ 'get' : f3 });
|
||||
arr.splice(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
assert(e.message == "6");
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.13.b.iv.2.
|
||||
Checking behavior when the last element gets deleted */
|
||||
function f4() { delete arr[23]; };
|
||||
|
||||
try {
|
||||
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
|
||||
delete arr[23];
|
||||
Object.defineProperty(arr, '23', { 'get' : f4 });
|
||||
arr.splice(1, 7, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.13.b.v.1.
|
||||
Checking behavior when the last element throws error */
|
||||
function f5() {
|
||||
for(var i = 0; i < arr.length; i++) {
|
||||
delete arr[i];
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24];
|
||||
delete arr[23];
|
||||
Object.defineProperty(arr, '23', { 'get' : f5 });
|
||||
arr.splice(1, 7, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.15.b.
|
||||
Checking behavior when the issue is the same as above, but splice has more arguments */
|
||||
function f6() {
|
||||
for(var i = 0; i < arr.length; i++) {
|
||||
delete arr[i];
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
||||
delete arr[2];
|
||||
Object.defineProperty(arr, '2', { 'get' : f6 });
|
||||
arr.splice(1, 7, 5, 5, 5, 5);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.12.16.
|
||||
Checking behavior when the array is empty, large, and not writable */
|
||||
try {
|
||||
arr = [];
|
||||
Object.defineProperty(arr, 'length', { value : 999, writable: false });
|
||||
arr.splice(1, 2, 4, 5);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// 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.
|
||||
|
||||
assert ([].toLocaleString() === "");
|
||||
assert ([1].toLocaleString() === "1");
|
||||
assert ([1,2].toLocaleString() === "1,2");
|
||||
assert ([1,2,3].toLocaleString() === "1,2,3");
|
||||
|
||||
var test_ok = {
|
||||
length: 1,
|
||||
toLocaleString: function() { return "1"; }
|
||||
};
|
||||
|
||||
assert ([3, test_ok, 4, test_ok].toLocaleString() === "3,1,4,1");
|
||||
|
||||
|
||||
var obj = { toLocaleString: function() {} };
|
||||
var test_non_str_locale = [undefined, obj, null, obj, obj];
|
||||
|
||||
assert(test_non_str_locale.toLocaleString() === ",undefined,,undefined,undefined");
|
||||
|
||||
var test_fail = {
|
||||
toLocaleString: "FAIL"
|
||||
};
|
||||
|
||||
try {
|
||||
[test_fail].toLocaleString();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
|
||||
var test_fail_call = {
|
||||
toLocaleString: function() { throw new ReferenceError("foo"); }
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
[1, 2, test_fail_call].toLocaleString();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// 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.
|
||||
|
||||
// Our own join method if the internal join is not implemented.
|
||||
function join(sep)
|
||||
{
|
||||
sep = sep ? sep : ",";
|
||||
var result = "";
|
||||
|
||||
for (var i = 0; i < this.length; ++i) {
|
||||
result += this[i];
|
||||
if (i + 1 < this.length) {
|
||||
result += sep;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Force fallback to object.prototype.toString()
|
||||
Array.prototype.join = 1;
|
||||
|
||||
assert ([1].toString() === "[object Array]");
|
||||
|
||||
Array.prototype.join = join;
|
||||
|
||||
assert ([1, 2].toString() === "1,2");
|
||||
|
||||
var test = [1,2,3];
|
||||
test.join = function() { throw ReferenceError ("foo"); };
|
||||
|
||||
try {
|
||||
test.toString();
|
||||
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
|
||||
// Test if the join returns a ReferenceError
|
||||
var arr = [1,2]
|
||||
Object.defineProperty(arr, 'join', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
try {
|
||||
arr.toString();
|
||||
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
// 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 array = []
|
||||
|
||||
assert(array.length === 0);
|
||||
|
||||
array.unshift("foo");
|
||||
assert(array.length === 1);
|
||||
assert(array[0] === "foo");
|
||||
|
||||
array.unshift(new Array())
|
||||
assert(array.length === 2);
|
||||
assert(array[0] instanceof Array);
|
||||
assert(array[1] === "foo")
|
||||
|
||||
array.unshift(Infinity);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === Infinity);
|
||||
assert(array[1] instanceof Array);
|
||||
assert(array[2] === "foo")
|
||||
|
||||
array.unshift("bar", 0);
|
||||
assert(array.length === 5);
|
||||
assert(array[0] === "bar");
|
||||
assert(array[1] === 0);
|
||||
assert(array[2] === Infinity);
|
||||
assert(array[3] instanceof Array);
|
||||
assert(array[4] === "foo")
|
||||
|
||||
|
||||
// Checking behavior when no length property defined
|
||||
var obj = { unshift : Array.prototype.unshift };
|
||||
|
||||
assert(obj.length === undefined);
|
||||
obj.unshift(1,2,3);
|
||||
assert(obj.length === 3);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { unshift : Array.prototype.unshift };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.unshift(1);
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to set length
|
||||
var obj = { unshift : Array.prototype.unshift };
|
||||
Object.defineProperty(obj, 'length', { 'set' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.unshift(2);
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable shift elements
|
||||
var obj = { unshift : Array.prototype.unshift, length : 1 };
|
||||
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.unshift(3);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
var obj = { unshift : Array.prototype.unshift, length : 1 };
|
||||
Object.defineProperty(obj, '0', { 'set' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.unshift(4);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when a property is not defined
|
||||
var obj = { '0' : "foo", '2' : "bar", length : 3, unshift : Array.prototype.unshift };
|
||||
assert(obj.unshift("baz") === 4);
|
||||
assert(obj[0] === "baz");
|
||||
assert(obj[1] === "foo");
|
||||
assert(obj[2] === undefined);
|
||||
assert(obj[3] === "bar");
|
||||
|
||||
/* ES v5.1 15.4.4.13.6.d.ii.
|
||||
Checking behavior when the array is freezed */
|
||||
try {
|
||||
var arr = [0, 1];
|
||||
Object.freeze(arr);
|
||||
Array.prototype.unshift.call(arr, 2, 3);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
/* ES v5.1 15.4.4.13.6.e.i.
|
||||
Checking behavior when the array has only one property and bigger length */
|
||||
try {
|
||||
var arr = { length : 9 };
|
||||
Object.defineProperty(arr, '6', { value : 2 });
|
||||
Array.prototype.unshift.call(arr, 2, 3);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// 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 cars = ["Saab", "Volvo", "BMW"];
|
||||
|
||||
assert (cars[0] === "Saab");
|
||||
assert (cars[1] === "Volvo");
|
||||
assert (cars[2] === "BMW");
|
||||
|
||||
var cars1 = new Array("Saab", "Volvo", "BMW");
|
||||
assert (cars[0] === cars1[0]);
|
||||
assert (cars[1] === cars1[1]);
|
||||
assert (cars[2] === cars1[2]);
|
||||
|
||||
var a = new Array();
|
||||
assert (typeof (a) === "object");
|
||||
assert (Array.isArray (a));
|
||||
assert (Array.isArray ([1, 2, 3]));
|
||||
|
||||
var b = new Array (30000);
|
||||
assert(b.length === 30000);
|
||||
assert (b[20000] === undefined);
|
||||
b[20000] = 1;
|
||||
assert (b[20000] === 1);
|
||||
b[20000] = 10;
|
||||
assert (b[20000] === 10);
|
||||
|
||||
assert(b.length === 30000);
|
||||
assert(b[10000] === undefined);
|
||||
Object.defineProperty (b, '10000', {value : 25, writable : false});
|
||||
assert(b[10000] === 25);
|
||||
b[10000] = 30;
|
||||
assert(b[10000] === 25);
|
||||
|
||||
assert(b.length === 30000);
|
||||
assert(b[50000] === undefined);
|
||||
assert(b.length === 30000);
|
||||
b[50000] = 5;
|
||||
assert(b.length === 50001);
|
||||
assert(b[50000] === 5);
|
||||
b[50000] = 10;
|
||||
assert(b[50000] === 10);
|
||||
Object.defineProperty (b, '50000', {writable : false});
|
||||
assert(b[50000] === 10);
|
||||
b[50000] = 20;
|
||||
assert(b[50000] === 10);
|
||||
|
||||
Object.defineProperty (b, '50000', {writable : true});
|
||||
assert(b[50000] === 10);
|
||||
b[50000] = 30;
|
||||
assert(b[50000] === 30);
|
||||
|
||||
b.length = 5;
|
||||
assert(b[50000] === undefined);
|
||||
|
||||
assert(([1, 2, 3]).length === 3);
|
||||
|
||||
assert(Array.prototype.constructor === Array);
|
||||
assert(Array.prototype.length === 0);
|
||||
Array.prototype[0] = 'string value';
|
||||
assert(Array.prototype.length === 1);
|
||||
assert(Array.prototype[0] === 'string value');
|
||||
|
||||
var c = [0,,,'3'];
|
||||
assert (c[0] === 0);
|
||||
assert (c[1] === undefined);
|
||||
assert (c[2] === undefined);
|
||||
assert (c[3] === '3');
|
||||
|
||||
b[0] = 1;
|
||||
c[0] += b[0];
|
||||
assert (c[0] == 1);
|
||||
|
||||
var arr = [
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
|
||||
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
||||
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
|
||||
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
|
||||
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
|
||||
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
|
||||
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
|
||||
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
|
||||
145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
|
||||
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
|
||||
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192,
|
||||
193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
|
||||
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
|
||||
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
|
||||
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256,
|
||||
257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272,
|
||||
273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
|
||||
289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304,
|
||||
305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320,
|
||||
321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336,
|
||||
337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352,
|
||||
353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368,
|
||||
369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384,
|
||||
385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400,
|
||||
401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416,
|
||||
417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432,
|
||||
433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448,
|
||||
449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464,
|
||||
465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480,
|
||||
481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496,
|
||||
497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512,
|
||||
513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528,
|
||||
529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544,
|
||||
545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560,
|
||||
561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576,
|
||||
577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592,
|
||||
593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608,
|
||||
609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624,
|
||||
625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640,
|
||||
641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656,
|
||||
657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672,
|
||||
673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688,
|
||||
689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704,
|
||||
705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720,
|
||||
721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736,
|
||||
737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752,
|
||||
753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768,
|
||||
769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784,
|
||||
785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800,
|
||||
801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816,
|
||||
817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832,
|
||||
833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848,
|
||||
849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864,
|
||||
865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880,
|
||||
881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896,
|
||||
897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912,
|
||||
913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928,
|
||||
929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944,
|
||||
945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960,
|
||||
961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976,
|
||||
977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992,
|
||||
993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008,
|
||||
1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024 ];
|
||||
|
||||
for (i = 0; i < 1024; i++)
|
||||
{
|
||||
assert (arr[i] === i + 1);
|
||||
}
|
||||
|
||||
var elision = [0,,2 ,3];
|
||||
assert (elision.hasOwnProperty(1) == false);
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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 b = 5;
|
||||
|
||||
assert((b += 10) == 15);
|
||||
assert((b -= 3) == 12);
|
||||
assert((b *= 10) == 120);
|
||||
assert((b /= 10) == 12);
|
||||
assert((b %= 10) == 2);
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
|
||||
assert((5 & 2) === 0);
|
||||
assert((2 & 2) === 2);
|
||||
assert((5 | 2) === 7);
|
||||
assert((5 | 5) === 5);
|
||||
assert((5 ^ 2) === 7);
|
||||
assert((5 ^ 5) === 0);
|
||||
assert((~5) == -6);
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
// 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.
|
||||
|
||||
/* with */
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
with ({})
|
||||
{
|
||||
break;
|
||||
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 0);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
with ({})
|
||||
{
|
||||
continue;
|
||||
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 10);
|
||||
|
||||
/* try */
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
break;
|
||||
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
assert (i === 0);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
continue;
|
||||
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
assert (i === 10);
|
||||
|
||||
/* catch */
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw new TypeError ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
break;
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 0);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw new TypeError ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
continue;
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 10);
|
||||
|
||||
|
||||
/* finally */
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw new TypeError ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
break;
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 0);
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw new TypeError ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
continue;
|
||||
assert (false);
|
||||
}
|
||||
}
|
||||
assert (i === 10);
|
||||
|
||||
|
||||
/* with - switch */
|
||||
|
||||
str = '';
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
with ({})
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
str += 'A';
|
||||
break;
|
||||
default:
|
||||
str += 'B';
|
||||
continue;
|
||||
}
|
||||
|
||||
str += 'C';
|
||||
}
|
||||
}
|
||||
assert (str === 'ACBBBBBBBBB');
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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.prototype.toString = Object.prototype.toString;
|
||||
assert(Array.toString() === "[object Function]");
|
||||
assert(Number.toString() === "[object Function]");
|
||||
assert(String.toString() === "[object Function]");
|
||||
assert(Boolean.toString() === "[object Function]");
|
||||
assert(Object.toString() === "[object Function]");
|
||||
assert(Function.toString() === "[object Function]");
|
||||
assert(Date.toString() === "[object Function]");
|
||||
assert(RegExp.toString() === "[object Function]");
|
||||
|
||||
assert(Math.toString() === "[object Math]");
|
||||
assert(JSON.toString() === "[object JSON]");
|
||||
@@ -0,0 +1,23 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
assert (Object.prototype.toString.call (String.prototype) === '[object String]');
|
||||
assert (String.prototype.toString() === "");
|
||||
|
||||
assert (Object.prototype.toString.call (Boolean.prototype) === '[object Boolean]');
|
||||
assert (Boolean.prototype.valueOf() === false);
|
||||
|
||||
assert (Object.prototype.toString.call (Number.prototype) === '[object Number]');
|
||||
assert (Number.prototype.valueOf() === 0);
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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 d = new Date(1999, 1, 1);
|
||||
assert (d.getYear() === 99);
|
||||
d = new Date(1874, 4, 9);
|
||||
assert (d.getYear() === -26);
|
||||
d = new Date(2015, 8, 17);
|
||||
assert (d.getYear() === 115);
|
||||
d = new Date(NaN);
|
||||
assert (isNaN (d.getYear()));
|
||||
|
||||
var d = new Date();
|
||||
d.setYear(91);
|
||||
assert (d.getFullYear() === 1991 && d.getYear() === 91);
|
||||
|
||||
d = new Date();
|
||||
d.setYear(NaN);
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
d = new Date();
|
||||
d.setYear(2015);
|
||||
assert (d.getFullYear() === 2015);
|
||||
|
||||
d = new Date(2000, 1, 29);
|
||||
d.setYear(2004);
|
||||
assert (d.getFullYear() === 2004 && d.getMonth() === 1 && d.getDate() === 29);
|
||||
d.setYear(2015);
|
||||
assert (d.getFullYear() === 2015 && d.getMonth() === 2 && d.getDate() === 1);
|
||||
|
||||
assert (/Thu, 17 Sep 2015 \d{2}:\d{2}:\d{2} GMT/.test (new Date("2015-09-17").toGMTString()));
|
||||
|
||||
d = new Date(NaN);
|
||||
assert (d.toGMTString() === "Invalid Date");
|
||||
@@ -0,0 +1,77 @@
|
||||
// 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.
|
||||
|
||||
assert (Date.length == 7);
|
||||
|
||||
var d;
|
||||
|
||||
try
|
||||
{
|
||||
d = new Date({toString: function() { throw new Error("foo"); }});
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof Error);
|
||||
assert (e.message === "foo");
|
||||
}
|
||||
|
||||
d = new Date("abcd");
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
d = new Date();
|
||||
assert (!isNaN(d.valueOf()));
|
||||
|
||||
d = new Date("2015-01-01");
|
||||
assert (d.valueOf() == 1420070400000);
|
||||
|
||||
d = new Date(1420070400000);
|
||||
assert (d.valueOf() == 1420070400000);
|
||||
|
||||
d = new Date(2015,0,1,0,0,0,0);
|
||||
assert (d.valueOf() - (d.getTimezoneOffset() * 60000) == 1420070400000);
|
||||
|
||||
d = new Date(8.64e+15);
|
||||
assert (d.valueOf() == 8.64e+15);
|
||||
|
||||
d = new Date(8.64e+15 + 1);
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
d = new Date(20000000, 0, 1);
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
d = new Date(0, 20000000, 1);
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
var Obj = function (val)
|
||||
{
|
||||
this.value = val;
|
||||
this.valueOf = function () { throw new ReferenceError ("valueOf-" + this.value); };
|
||||
this.toString = function () { throw new ReferenceError ("toString-"+ this.value); };
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
d = new Date (new Obj (1), new Obj (2));
|
||||
// Should not be reached.
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof ReferenceError);
|
||||
assert (e.message === "valueOf-1");
|
||||
}
|
||||
|
||||
assert (typeof Date (2015) == "string");
|
||||
assert (typeof Date() != typeof (new Date ()));
|
||||
@@ -0,0 +1,106 @@
|
||||
// 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.
|
||||
|
||||
/* 1. test case */
|
||||
var d = new Date(2015, 6, 9, 12, 13, 14, 121);
|
||||
|
||||
assert (d.getFullYear() == 2015);
|
||||
assert (d.getUTCFullYear() == 2015);
|
||||
assert (d.getMonth() == 6);
|
||||
assert (d.getUTCMonth() == 6);
|
||||
assert (d.getDate() == 9);
|
||||
assert (d.getUTCDate() == 9);
|
||||
assert (d.getDay() == 4);
|
||||
assert (d.getUTCDay() == 4);
|
||||
assert (d.getHours() == 12);
|
||||
assert (d.getUTCHours() == (12 + d.getTimezoneOffset() / 60));
|
||||
assert (d.getMinutes() == 13);
|
||||
assert (d.getUTCMinutes() == 13);
|
||||
assert (d.getSeconds() == 14);
|
||||
assert (d.getUTCSeconds() == 14);
|
||||
assert (d.getMilliseconds() == 121);
|
||||
assert (d.getUTCMilliseconds() == 121);
|
||||
|
||||
/* 2. test case */
|
||||
var d = new Date("2015-07-09T12:13:14.121+01:30");
|
||||
|
||||
assert (d.getFullYear() == 2015);
|
||||
assert (d.getUTCFullYear() == 2015);
|
||||
assert (d.getMonth() == 6);
|
||||
assert (d.getUTCMonth() == 6);
|
||||
assert (d.getDate() == 9);
|
||||
assert (d.getUTCDate() == 9);
|
||||
assert (d.getDay() == 4);
|
||||
assert (d.getUTCDay() == 4);
|
||||
assert (d.getHours() == Math.floor(12 - 1.5 + d.getTimezoneOffset() / 60));
|
||||
assert (d.getUTCHours() == Math.floor(12 - 1.5));
|
||||
assert (d.getMinutes() == 43);
|
||||
assert (d.getUTCMinutes() == 43);
|
||||
assert (d.getSeconds() == 14);
|
||||
assert (d.getUTCSeconds() == 14);
|
||||
assert (d.getMilliseconds() == 121);
|
||||
assert (d.getUTCMilliseconds() == 121);
|
||||
|
||||
/* 3. test case */
|
||||
var d = new Date(0);
|
||||
|
||||
assert (d.getFullYear() == 1970);
|
||||
assert (d.getUTCFullYear() == 1970);
|
||||
assert (d.getMonth() == 0);
|
||||
assert (d.getUTCMonth() == 0);
|
||||
assert (d.getDate() == 1);
|
||||
assert (d.getUTCDate() == 1);
|
||||
assert (d.getDay() == 4);
|
||||
assert (d.getUTCDay() == 4);
|
||||
assert (d.getHours() == 0 - (d.getTimezoneOffset() / 60));
|
||||
assert (d.getUTCHours() == 0);
|
||||
assert (d.getMinutes() == 0);
|
||||
assert (d.getUTCMinutes() == 0);
|
||||
assert (d.getSeconds() == 0);
|
||||
assert (d.getUTCSeconds() == 0);
|
||||
assert (d.getMilliseconds() == 0);
|
||||
assert (d.getUTCMilliseconds() == 0);
|
||||
|
||||
/* 4. test case */
|
||||
var d = new Date("abcd");
|
||||
assert (isNaN (d));
|
||||
|
||||
assert (isNaN (d.getFullYear()));
|
||||
assert (isNaN (d.getUTCFullYear()));
|
||||
assert (isNaN (d.getMonth()));
|
||||
assert (isNaN (d.getUTCMonth()));
|
||||
assert (isNaN (d.getDate()));
|
||||
assert (isNaN (d.getUTCDate()));
|
||||
assert (isNaN (d.getDay()));
|
||||
assert (isNaN (d.getUTCDay()));
|
||||
assert (isNaN (d.getHours()));
|
||||
assert (isNaN (d.getUTCHours()));
|
||||
assert (isNaN (d.getMinutes()));
|
||||
assert (isNaN (d.getUTCMinutes()));
|
||||
assert (isNaN (d.getSeconds()));
|
||||
assert (isNaN (d.getUTCSeconds()));
|
||||
assert (isNaN (d.getMilliseconds()));
|
||||
assert (isNaN (d.getUTCMilliseconds()));
|
||||
assert (isNaN (d.getTimezoneOffset()));
|
||||
|
||||
/* 5. test case */
|
||||
assert (new Date(2013, -1).getMonth() === 11);
|
||||
assert (new Date(-2, -2).getFullYear() === -3);
|
||||
assert (new Date(-1, -1).getFullYear() === -2);
|
||||
assert (new Date(-1, -1, -1).getMonth() === 10);
|
||||
assert (new Date(-1, -1, -1, -1).getDate() === 28);
|
||||
assert (new Date(-1, -1, -1, -1, -1).getHours() === 22);
|
||||
assert (new Date(-1, -1, -1, -1, -1, -1).getMinutes() === 58);
|
||||
assert (new Date(-1, -1, -1, -1, -1, -1, -1).getSeconds() === 58);
|
||||
assert (new Date(-1, -1, -1, -1, -1, -1, -1, -1).getMilliseconds() === 999);
|
||||
@@ -0,0 +1,178 @@
|
||||
// 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 wrongFormats = ["",
|
||||
"2",
|
||||
"20",
|
||||
"201",
|
||||
"2015-",
|
||||
"2015-01-",
|
||||
"2015-01-01-",
|
||||
"qwerty",
|
||||
"2015-01-01T",
|
||||
"2015-01-01T1:1",
|
||||
"2015-01-01T01",
|
||||
"2015-01-01T01",
|
||||
"2015-01-01T01:01F",
|
||||
"T2015",
|
||||
"2015-01-01Z",
|
||||
"2015-01-01+01:00",
|
||||
"2015-01-01T00:00+01",
|
||||
"2015-01-01T00:00+1",
|
||||
"2015-01-01T00:00-01",
|
||||
"2015-01-01T00:00.000",
|
||||
"2015-01-01T00:00:",
|
||||
"2015-01-01T00:",
|
||||
"2015-01-01T00:00:00.1",
|
||||
"2015-01-01T00:00:00.01",
|
||||
"2015-01-01T24:01:00.000",
|
||||
"2015-01-01T24:00:01.000",
|
||||
"2015-01-01T24:00:00.001",
|
||||
"2015-01-01T00:00+01:00Z",
|
||||
"2015/01/01",
|
||||
"2015-01-32",
|
||||
"2015--1",
|
||||
"2015-13",
|
||||
"2015-01--1",
|
||||
"-215",
|
||||
"-215-01-01",
|
||||
"2015-01-00",
|
||||
"2015-01-01T25:00",
|
||||
"2015-01-01T00:60",
|
||||
"2015-01-01T-1:00",
|
||||
"2015-01-01T00:-1",
|
||||
"2e+3",
|
||||
"+2015-01-01",
|
||||
"-2015-01-01",
|
||||
"+02015-01-01",
|
||||
"-02015-01-01",
|
||||
"002015-01-01",
|
||||
"+0002015-01-01",
|
||||
"-0002015-01-01",
|
||||
"2015-01T00:00:00.000-03X00",
|
||||
"2015-01-01T00-03:00",
|
||||
"Fri Jan 01 1 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 11 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 111 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 1234567 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 +1000 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 -1 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 -11 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 -111 00:00:00 GMT+0000",
|
||||
"Fri Jan 01 -1234567 00:00:00 GMT+0000",
|
||||
"Thu Apr 10 1997"];
|
||||
|
||||
for (i in wrongFormats) {
|
||||
var d = Date.parse(wrongFormats[i]);
|
||||
assert (isNaN(d));
|
||||
}
|
||||
|
||||
var d;
|
||||
|
||||
d = Date.parse(undefined);
|
||||
assert (isNaN(d));
|
||||
|
||||
d = Date.parse({});
|
||||
assert (isNaN(d));
|
||||
|
||||
d = Date.parse(2000 + 15);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.parse("2015");
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.parse("2015-01");
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.parse("2015-01-01");
|
||||
assert (d == 1420070400000);
|
||||
|
||||
var timezoneOffsetMS = new Date(0).getTimezoneOffset() * 60000;
|
||||
|
||||
d = Date.parse("2015-01T00:00");
|
||||
assert (d == 1420070400000 + timezoneOffsetMS);
|
||||
|
||||
d = Date.parse("2015-01T00:00:00");
|
||||
assert (d == 1420070400000 + timezoneOffsetMS);
|
||||
|
||||
d = Date.parse("2015-01T00:00:00.000");
|
||||
assert (d == 1420070400000 + timezoneOffsetMS);
|
||||
|
||||
d = Date.parse("2015-01T24:00:00.000");
|
||||
assert (d == 1420156800000 + timezoneOffsetMS);
|
||||
|
||||
d = Date.parse("2015-01T00:00:00.000+03:00");
|
||||
assert (d == 1420059600000);
|
||||
|
||||
d = Date.parse("2015-01T00:00:00.000-03:00");
|
||||
assert (d == 1420081200000);
|
||||
|
||||
d = Date.parse("2015-07-03T14:35:43.123+01:30");
|
||||
assert (d == 1435928743123);
|
||||
|
||||
assert (isNaN(Date.parse("-271821-04-19T23:59:59.999Z"))) // -8640000000000001 - out of range
|
||||
assert (Date.parse("-271821-04-20T00:00:00.000Z") == -8640000000000000)
|
||||
assert (Date.parse("-000001-12-31T23:59:59.999Z") == -62167219200001)
|
||||
assert (Date.parse("0000-01-01T00:00:00.000Z") == -62167219200000)
|
||||
assert (Date.parse("0009-12-31T23:59:59.999Z") == -61851600000001)
|
||||
assert (Date.parse("0010-01-01T00:00:00.000Z") == -61851600000000)
|
||||
assert (Date.parse("0099-12-31T23:59:59.999Z") == -59011459200001)
|
||||
assert (Date.parse("0100-01-01T00:00:00.000Z") == -59011459200000)
|
||||
assert (Date.parse("0999-12-31T23:59:59.999Z") == -30610224000001)
|
||||
assert (Date.parse("1000-01-01T00:00:00.000Z") == -30610224000000)
|
||||
assert (Date.parse("1969-12-31T23:59:59.999Z") == -1)
|
||||
assert (Date.parse("1970-01-01T00:00:00.000Z") == 0)
|
||||
assert (Date.parse("1970-01-01T00:00:00.001Z") == 1)
|
||||
assert (Date.parse("9999-12-31T23:59:59.999Z") == 253402300799999)
|
||||
assert (Date.parse("+010000-01-01T00:00:00.000Z") == 253402300800000)
|
||||
assert (Date.parse("+275760-09-13T00:00:00.000Z") == 8640000000000000)
|
||||
assert (isNaN(Date.parse("+275760-09-13T00:00:00.001Z"))) // 8640000000000001 - out of range
|
||||
|
||||
// Date.toString() format
|
||||
assert (isNaN(Date.parse("Mon Apr 19 -271821 23:59:59 GMT+0000"))) // -8640000000001000 - out of range
|
||||
assert (Date.parse("Tue Apr 20 -271821 00:00:00 GMT+0000") == -8640000000000000)
|
||||
assert (Date.parse("Fri Dec 31 -0001 23:59:59 GMT+0000") == -62167219201000)
|
||||
assert (Date.parse("Sat Jan 01 0000 00:00:00 GMT+0000") == -62167219200000)
|
||||
assert (Date.parse("Thu Dec 31 0009 23:59:59 GMT+0000") == -61851600001000)
|
||||
assert (Date.parse("Fri Jan 01 0010 00:00:00 GMT+0000") == -61851600000000)
|
||||
assert (Date.parse("Thu Dec 31 0099 23:59:59 GMT+0000") == -59011459201000)
|
||||
assert (Date.parse("Fri Jan 01 0100 00:00:00 GMT+0000") == -59011459200000)
|
||||
assert (Date.parse("Tue Dec 31 0999 23:59:59 GMT+0000") == -30610224001000)
|
||||
assert (Date.parse("Wed Jan 01 1000 00:00:00 GMT+0000") == -30610224000000)
|
||||
assert (Date.parse("Wed Dec 31 1969 23:59:59 GMT+0000") == -1000)
|
||||
assert (Date.parse("Thu Jan 01 1970 00:00:00 GMT+0000") == 0)
|
||||
assert (Date.parse("Thu Jan 01 1970 00:00:01 GMT+0000") == 1000)
|
||||
assert (Date.parse("Fri Dec 31 9999 23:59:59 GMT+0000") == 253402300799000)
|
||||
assert (Date.parse("Sat Jan 01 10000 00:00:00 GMT+0000") == 253402300800000)
|
||||
assert (Date.parse("Sat Sep 13 275760 00:00:00 GMT+0000") == 8640000000000000)
|
||||
assert (isNaN(Date.parse("Sat Sep 13 275760 00:00:01 GMT+0000"))) // 8640000000001000 - out of range
|
||||
|
||||
// Date.toUTCString() format
|
||||
assert (isNaN(Date.parse("Mon, 19 Apr -271821 23:59:59 GMT"))) // -8640000000001000 - out of range
|
||||
assert (Date.parse("Tue, 20 Apr -271821 00:00:00 GMT") == -8640000000000000)
|
||||
assert (Date.parse("Fri, 31 Dec -0001 23:59:59 GMT") == -62167219201000)
|
||||
assert (Date.parse("Sat, 01 Jan 0000 00:00:00 GMT") == -62167219200000)
|
||||
assert (Date.parse("Thu, 31 Dec 0009 23:59:59 GMT") == -61851600001000)
|
||||
assert (Date.parse("Fri, 01 Jan 0010 00:00:00 GMT") == -61851600000000)
|
||||
assert (Date.parse("Thu, 31 Dec 0099 23:59:59 GMT") == -59011459201000)
|
||||
assert (Date.parse("Fri, 01 Jan 0100 00:00:00 GMT") == -59011459200000)
|
||||
assert (Date.parse("Tue, 31 Dec 0999 23:59:59 GMT") == -30610224001000)
|
||||
assert (Date.parse("Wed, 01 Jan 1000 00:00:00 GMT") == -30610224000000)
|
||||
assert (Date.parse("Wed, 31 Dec 1969 23:59:59 GMT") == -1000)
|
||||
assert (Date.parse("Thu, 01 Jan 1970 00:00:00 GMT") == 0)
|
||||
assert (Date.parse("Thu, 01 Jan 1970 00:00:01 GMT") == 1000)
|
||||
assert (Date.parse("Fri, 31 Dec 9999 23:59:59 GMT") == 253402300799000)
|
||||
assert (Date.parse("Sat, 01 Jan 10000 00:00:00 GMT") == 253402300800000)
|
||||
assert (Date.parse("Sat, 13 Sep 275760 00:00:00 GMT") == 8640000000000000)
|
||||
assert (isNaN(Date.parse("Sat, 13 Sep 275760 00:00:01 GMT"))) // 8640000000001000 - out of range
|
||||
@@ -0,0 +1,243 @@
|
||||
// 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 ms = 1;
|
||||
var sec = 1000 * ms;
|
||||
var min = 60 * sec;
|
||||
var hour = 60 * min;
|
||||
var day = 24 * hour; /* 86400000 */
|
||||
var d = new Date();
|
||||
|
||||
/* 15.9.5.27 Date.prototype.setTime (time) */
|
||||
assert (d.setTime(0) == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setTime(day) == day);
|
||||
assert (d.getDate() == 2);
|
||||
|
||||
/* 15.9.5.28 Date.prototype.setMilliseconds (ms) */
|
||||
d.setTime(0);
|
||||
assert (d.setMilliseconds(1) == ms);
|
||||
assert (d.getMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.29 Date.prototype.setUTCMilliseconds (ms) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMilliseconds(1) == ms);
|
||||
assert (d.getUTCMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.30 Date.prototype.setSeconds (sec [, ms ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setSeconds(1) == sec);
|
||||
assert (d.getSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setSeconds(1, 1) == sec + ms);
|
||||
assert (d.getSeconds() == 1);
|
||||
assert (d.getMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.31 Date.prototype.setUTCSeconds (sec [, ms ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCSeconds(1) == sec);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCSeconds(1, 1) == sec + ms);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
assert (d.getUTCMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.32 Date.prototype.setMinutes (min [, sec [, ms ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setMinutes(1) == min);
|
||||
assert (d.getMinutes() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setMinutes(1, 1) == min + sec);
|
||||
assert (d.getMinutes() == 1);
|
||||
assert (d.getSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setMinutes(1, 1, 1) == min + sec + ms);
|
||||
assert (d.getMinutes() == 1);
|
||||
assert (d.getSeconds() == 1);
|
||||
assert (d.getMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.33 Date.prototype.setUTCMinutes (min [, sec [, ms ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMinutes(1) == min);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMinutes(1, 1) == min + sec);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMinutes(1, 1, 1) == min + sec + ms);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
assert (d.getUTCMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.34 Date.prototype.setHours (hour [, min [, sec [, ms ] ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setHours(1) == hour + d.getTimezoneOffset() * 60000);
|
||||
assert (d.getHours() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setHours(1, 1) == hour + min + d.getTimezoneOffset() * 60000);
|
||||
assert (d.getHours() == 1);
|
||||
assert (d.getMinutes() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setHours(1, 1, 1) == hour + min + sec + d.getTimezoneOffset() * 60000);
|
||||
assert (d.getHours() == 1);
|
||||
assert (d.getMinutes() == 1);
|
||||
assert (d.getSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setHours(1, 1, 1, 1) == hour + min + sec + ms + d.getTimezoneOffset() * 60000);
|
||||
assert (d.getHours() == 1);
|
||||
assert (d.getMinutes() == 1);
|
||||
assert (d.getSeconds() == 1);
|
||||
assert (d.getMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.35 Date.prototype.setUTCHours (hour [, min [, sec [, ms ] ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCHours(1) == hour);
|
||||
assert (d.getUTCHours() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCHours(1, 1) == hour + min);
|
||||
assert (d.getUTCHours() == 1);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCHours(1, 1, 1) == hour + min + sec);
|
||||
assert (d.getUTCHours() == 1);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCHours(1, 1, 1, 1) == hour + min + sec + ms);
|
||||
assert (d.getUTCHours() == 1);
|
||||
assert (d.getUTCMinutes() == 1);
|
||||
assert (d.getUTCSeconds() == 1);
|
||||
assert (d.getUTCMilliseconds() == 1);
|
||||
|
||||
/* 15.9.5.36 Date.prototype.setDate (date) */
|
||||
d.setTime(0);
|
||||
assert (d.setDate(0) == -day);
|
||||
assert (d.getDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setDate(1) == 0);
|
||||
assert (d.getDate() == 1);
|
||||
|
||||
/* 15.9.5.37 Date.prototype.setUTCDate (date) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCDate(0) == -day);
|
||||
assert (d.getUTCDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCDate(1) == 0);
|
||||
assert (d.getUTCDate() == 1);
|
||||
|
||||
/* 15.9.5.38 Date.prototype.setMonth (month [, date ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setMonth(0) == 0);
|
||||
assert (d.getMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setMonth(0, 0) == -day);
|
||||
assert (d.getMonth() == 11);
|
||||
assert (d.getDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setMonth(1) == 31 * day);
|
||||
assert (d.getMonth() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setMonth(12) == 365 * day);
|
||||
assert (d.getMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setMonth(13) == (365 + 31) * day);
|
||||
assert (d.getMonth() == 1);
|
||||
|
||||
/* 15.9.5.39 Date.prototype.setUTCMonth (month [, date ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMonth(0) == 0);
|
||||
assert (d.getUTCMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMonth(0, 0) == -day);
|
||||
assert (d.getUTCMonth() == 11);
|
||||
assert (d.getUTCDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMonth(1) == 31 * day);
|
||||
assert (d.getUTCMonth() == 1);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMonth(12) == 365 * day);
|
||||
assert (d.getUTCMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCMonth(13) == (365 + 31) * day);
|
||||
assert (d.getUTCMonth() == 1);
|
||||
|
||||
/* 15.9.5.40 Date.prototype.setFullYear (year [, month [, date ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setFullYear(0) == -62167219200000);
|
||||
assert (d.getFullYear() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setFullYear(0, 0) == -62167219200000);
|
||||
assert (d.getFullYear() == 0);
|
||||
assert (d.getMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setFullYear(0, 0, 0) == -62167219200000 - day);
|
||||
assert (d.getFullYear() == -1);
|
||||
assert (d.getMonth() == 11);
|
||||
assert (d.getDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setFullYear(1970) == 0);
|
||||
assert (d.getFullYear() == 1970);
|
||||
|
||||
/* 15.9.5.41 Date.prototype.setUTCFullYear (year [, month [, date ] ] ) */
|
||||
d.setTime(0);
|
||||
assert (d.setUTCFullYear(0) == -62167219200000);
|
||||
assert (d.getUTCFullYear() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCFullYear(0, 0) == -62167219200000);
|
||||
assert (d.getUTCFullYear() == 0);
|
||||
assert (d.getUTCMonth() == 0);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCFullYear(0, 0, 0) == -62167219200000 - day);
|
||||
assert (d.getUTCFullYear() == -1);
|
||||
assert (d.getUTCMonth() == 11);
|
||||
assert (d.getUTCDate() == 31);
|
||||
d.setTime(0);
|
||||
assert (d.setUTCFullYear(1970) == 0);
|
||||
assert (d.getUTCFullYear() == 1970);
|
||||
|
||||
/* ECMA262 v11 20.4.1.2 Day Number and Time within Day
|
||||
msPerDay = 86400000
|
||||
TimeWithinDay(t) = t modulo msPerDay
|
||||
|
||||
ECMA262 v11 5.2.5 Mathematical Operations
|
||||
The notation “x modulo y” (y must be finite and nonzero) computes a value k of the same sign as y (or zero).
|
||||
|
||||
Consequently TimeWithinDay(t) >= 0. It can be tested properly with dates close to 1970.
|
||||
*/
|
||||
d = new Date("1969-12-01T01:00:00.000Z");
|
||||
d.setFullYear(1968);
|
||||
assert (d.toISOString() == "1968-12-01T01:00:00.000Z");
|
||||
|
||||
d = new Date("1970-01-31T01:00:00.000Z");
|
||||
d.setFullYear(1971);
|
||||
assert (d.toISOString() == "1971-01-31T01:00:00.000Z");
|
||||
|
||||
/* Without argument */
|
||||
d = new Date();
|
||||
assert (isNaN (d.setTime()));
|
||||
assert (isNaN (d.setMilliseconds()));
|
||||
assert (isNaN (d.setUTCMilliseconds()));
|
||||
assert (isNaN (d.setSeconds()));
|
||||
assert (isNaN (d.setUTCSeconds()));
|
||||
assert (isNaN (d.setMinutes()));
|
||||
assert (isNaN (d.setUTCMinutes()));
|
||||
assert (isNaN (d.setHours()));
|
||||
assert (isNaN (d.setUTCHours()));
|
||||
assert (isNaN (d.setDate()));
|
||||
assert (isNaN (d.getUTCDate()));
|
||||
assert (isNaN (d.setMonth()));
|
||||
assert (isNaN (d.setUTCMonth()));
|
||||
assert (isNaN (d.setFullYear()));
|
||||
assert (isNaN (d.setUTCFullYear()));
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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.
|
||||
|
||||
try {
|
||||
d = new Date (-8640000000000001)
|
||||
assert (d == "Invalid Date")
|
||||
d.toISOString()
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
|
||||
assert (new Date (-8640000000000000).toISOString() == "-271821-04-20T00:00:00.000Z")
|
||||
|
||||
assert (new Date(-62167219200001).toISOString() == "-000001-12-31T23:59:59.999Z")
|
||||
assert (new Date(-62167219200000).toISOString() == "0000-01-01T00:00:00.000Z")
|
||||
|
||||
assert (new Date(-61851600000001).toISOString() == "0009-12-31T23:59:59.999Z")
|
||||
assert (new Date(-61851600000000).toISOString() == "0010-01-01T00:00:00.000Z")
|
||||
|
||||
assert (new Date(-59011459200001).toISOString() == "0099-12-31T23:59:59.999Z")
|
||||
assert (new Date(-59011459200000).toISOString() == "0100-01-01T00:00:00.000Z")
|
||||
|
||||
assert (new Date(-30610224000001).toISOString() == "0999-12-31T23:59:59.999Z")
|
||||
assert (new Date(-30610224000000).toISOString() == "1000-01-01T00:00:00.000Z")
|
||||
|
||||
assert (new Date(-1).toISOString() == "1969-12-31T23:59:59.999Z")
|
||||
assert (new Date(0).toISOString() == "1970-01-01T00:00:00.000Z")
|
||||
assert (new Date(1).toISOString() == "1970-01-01T00:00:00.001Z")
|
||||
|
||||
assert (new Date(253402300799999).toISOString() == "9999-12-31T23:59:59.999Z")
|
||||
assert (new Date(253402300800000).toISOString() == "+010000-01-01T00:00:00.000Z")
|
||||
|
||||
assert (new Date (8640000000000000).toISOString() == "+275760-09-13T00:00:00.000Z")
|
||||
|
||||
try {
|
||||
d = new Date (8640000000000001)
|
||||
assert (d == "Invalid Date")
|
||||
d.toISOString()
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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 = {
|
||||
toISOString: function() { return "RESULT-ToISOString"; },
|
||||
valueOf: function() { return "RESULT-valueOf"; }
|
||||
};
|
||||
|
||||
var result = Date.prototype.toJSON.call(obj);
|
||||
|
||||
assert(result === "RESULT-ToISOString");
|
||||
@@ -0,0 +1,170 @@
|
||||
// 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.
|
||||
|
||||
assert (new Date (NaN) == "Invalid Date");
|
||||
assert (new Date (Infinity, 1, 1, 0, 0, 0) == "Invalid Date");
|
||||
assert (new Date (2015, Infinity, 1, 0, 0, 0) == "Invalid Date");
|
||||
assert (new Date (2015, 7, 1, 0, Infinity, 0) == "Invalid Date");
|
||||
assert (new Date (NaN, 1, 1, 0, 0, 0) == "Invalid Date");
|
||||
assert (new Date (2015, NaN, 1, 0, 0, 0) == "Invalid Date");
|
||||
assert (new Date (2015, 7, 1, 0, NaN, 0) == "Invalid Date");
|
||||
assert (/Fri Feb 13 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (new Date ("2015-02-13")));
|
||||
assert (/Wed Jul 08 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (new Date ("2015-07-08T11:29:05.023")));
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toString.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
var date = new Date(0);
|
||||
assert (/Thu Jan 01 1970 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (date.toString()));
|
||||
assert (date.toUTCString() === "Thu, 01 Jan 1970 00:00:00 GMT");
|
||||
assert (date.toISOString() === "1970-01-01T00:00:00.000Z");
|
||||
|
||||
date = new Date("2015-08-12T09:40:20.000Z")
|
||||
assert (/Wed Aug 12 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (date.toString()));
|
||||
assert (date.toUTCString() === "Wed, 12 Aug 2015 09:40:20 GMT");
|
||||
assert (date.toISOString() === "2015-08-12T09:40:20.000Z");
|
||||
|
||||
assert (new Date (NaN).toDateString () == "Invalid Date");
|
||||
assert (new Date ("2015-02-13").toDateString () == "2015-02-13");
|
||||
assert (new Date ("2015-07-08T11:29:05.023").toDateString () == "2015-07-08");
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toDateString.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert (new Date (NaN).toTimeString () == "Invalid Date");
|
||||
assert (new Date (Number.POSITIVE_INFINITY).toString () === "Invalid Date");
|
||||
assert (new Date ("2015-02-13").toTimeString () == "00:00:00.000");
|
||||
assert (new Date ("2015-07-08T11:29:05.023").toTimeString () == "11:29:05.023");
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toTimeString.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert (new Date ("2015-07-16").toISOString () == "2015-07-16T00:00:00.000Z");
|
||||
assert (new Date ("2015-07-16T11:29:05.023").toISOString () == "2015-07-16T11:29:05.023Z");
|
||||
|
||||
try
|
||||
{
|
||||
new Date (NaN).toISOString ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof RangeError);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
new Date (Number.POSITIVE_INFINITY).toISOString ();
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof RangeError);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toISOString.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert (new Date (NaN).toUTCString () == "Invalid Date");
|
||||
assert (new Date ("2015-07-16").toUTCString () == "Thu, 16 Jul 2015 00:00:00 GMT");
|
||||
assert (new Date ("2015-07-16T11:29:05.023").toUTCString () == "Thu, 16 Jul 2015 11:29:05 GMT");
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toUTCString.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert (new Date (NaN).toJSON () == null);
|
||||
assert (new Date ("2015-07-16").toJSON () == "2015-07-16T00:00:00.000Z");
|
||||
assert (new Date ("2015-07-16T11:29:05.023").toJSON () == "2015-07-16T11:29:05.023Z");
|
||||
|
||||
try
|
||||
{
|
||||
Date.prototype.toJSON.call(-1);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
date_time = new Date ("2015-07-08T11:29:05.023").toJSON ();
|
||||
assert (new Date (date_time).toISOString () == "2015-07-08T11:29:05.023Z");
|
||||
|
||||
assert (typeof Date (2015) == "string");
|
||||
assert (typeof Date() != typeof (new Date ()));
|
||||
assert (Date () == (new Date ()).toString ());
|
||||
assert (Date (2015, 1, 1) == (new Date ()).toString ());
|
||||
assert (Date (Number.NaN) == Date ());
|
||||
|
||||
assert (new Date ("2015-07-08T11:29:05.023Z").toISOString() == "2015-07-08T11:29:05.023Z");
|
||||
|
||||
// corner cases
|
||||
assert (new Date (-8640000000000001).toString() == "Invalid Date")
|
||||
assert (new Date (-8640000000000000).toString() == "Tue Apr 20 -271821 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(-62167219200001).toString() == "Fri Dec 31 -0001 23:59:59 GMT+0000")
|
||||
assert (new Date(-62167219200000).toString() == "Sat Jan 01 0000 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(-61851600000001).toString() == "Thu Dec 31 0009 23:59:59 GMT+0000")
|
||||
assert (new Date(-61851600000000).toString() == "Fri Jan 01 0010 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(-59011459200001).toString() == "Thu Dec 31 0099 23:59:59 GMT+0000")
|
||||
assert (new Date(-59011459200000).toString() == "Fri Jan 01 0100 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(-30610224000001).toString() == "Tue Dec 31 0999 23:59:59 GMT+0000")
|
||||
assert (new Date(-30610224000000).toString() == "Wed Jan 01 1000 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(-1).toString() == "Wed Dec 31 1969 23:59:59 GMT+0000")
|
||||
assert (new Date(0).toString() == "Thu Jan 01 1970 00:00:00 GMT+0000")
|
||||
assert (new Date(1).toString() == "Thu Jan 01 1970 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date(253402300799999).toString() == "Fri Dec 31 9999 23:59:59 GMT+0000")
|
||||
assert (new Date(253402300800000).toString() == "Sat Jan 01 10000 00:00:00 GMT+0000")
|
||||
|
||||
assert (new Date (8640000000000000).toString() == "Sat Sep 13 275760 00:00:00 GMT+0000")
|
||||
assert (new Date (8640000000000001).toString() == "Invalid Date")
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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 d;
|
||||
|
||||
d = Date.UTC(undefined);
|
||||
assert (isNaN(d));
|
||||
|
||||
d = Date.UTC({});
|
||||
assert (isNaN(d));
|
||||
|
||||
d = Date.UTC(2000 + 15, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0, 1);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0, 1, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0, 1, 0, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0, 1, 0, 0, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 0, 1, 0, 0, 0, 0);
|
||||
assert (d == 1420070400000);
|
||||
|
||||
d = Date.UTC(2015, 6, 3, 14, 35, 43, 123);
|
||||
assert (d == 1435934143123);
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
|
||||
/* argument is not reference */
|
||||
assert (delete 0 === true);
|
||||
assert (delete "0" === true);
|
||||
assert (delete (a = 1) === true);
|
||||
assert (delete delete a === true);
|
||||
|
||||
/* argument is unresolvable reference */
|
||||
assert (delete undefined_variable === true);
|
||||
|
||||
/* argument is object-based reference */
|
||||
var a = [1];
|
||||
assert (a[0] === 1);
|
||||
assert (delete a[0] === true);
|
||||
assert (a[0] == undefined);
|
||||
|
||||
var b = {c:0};
|
||||
assert (b.c === 0);
|
||||
assert (delete b.c === true);
|
||||
assert (b.c === undefined);
|
||||
|
||||
/* argument is lexical environment-based reference */
|
||||
var a = 1;
|
||||
assert (a === 1);
|
||||
assert (delete a === false);
|
||||
assert (a === 1);
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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 a = 0;
|
||||
|
||||
do a++
|
||||
while (false)
|
||||
|
||||
do { a++ } while (false)
|
||||
|
||||
try {
|
||||
eval("do a++ while (false)");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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 a = Object();
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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.
|
||||
|
||||
assert((5 == 5) == true);
|
||||
assert((7 != 2) == true);
|
||||
|
||||
var num = 0;
|
||||
var obj = new String("0");
|
||||
var str = "0";
|
||||
var b = false;
|
||||
|
||||
assert(num === num);
|
||||
assert(obj === obj);
|
||||
assert(str === str);
|
||||
|
||||
assert((num === obj) == false);
|
||||
assert((num === str) == false);
|
||||
assert((obj === str) == false);
|
||||
assert((null === undefined) == false);
|
||||
assert((obj === null) == false);
|
||||
assert((obj === undefined) == false);
|
||||
@@ -0,0 +1,200 @@
|
||||
// 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 e;
|
||||
|
||||
/* Error */
|
||||
e = new Error ();
|
||||
assert (e.name === "Error");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "Error");
|
||||
|
||||
e = new Error("some message");
|
||||
assert (e.name === "Error");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "Error: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (Error.prototype.toString !== Object.prototype.toString);
|
||||
assert (Error.prototype.constructor === Error);
|
||||
assert (Error.prototype.name === "Error");
|
||||
assert (Error.prototype.message === "");
|
||||
assert (Error.prototype.toString() === "Error");
|
||||
|
||||
/* TypeError */
|
||||
e = new TypeError ();
|
||||
assert (e.name === "TypeError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "TypeError");
|
||||
|
||||
e = new TypeError("some message");
|
||||
assert (e.name === "TypeError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "TypeError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (TypeError.prototype.toString === Error.prototype.toString);
|
||||
assert (TypeError.prototype.constructor === TypeError);
|
||||
assert (TypeError.prototype.name === "TypeError");
|
||||
assert (TypeError.prototype.message === "");
|
||||
assert (TypeError.prototype.toString() === "TypeError");
|
||||
|
||||
try
|
||||
{
|
||||
null[1] = 'abcd';
|
||||
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert(e instanceof TypeError);
|
||||
assert(e instanceof Error);
|
||||
assert(e instanceof Object);
|
||||
|
||||
assert(!(e instanceof Function));
|
||||
}
|
||||
|
||||
|
||||
/* ReferenceError */
|
||||
e = new ReferenceError ();
|
||||
assert (e.name === "ReferenceError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "ReferenceError");
|
||||
|
||||
e = new ReferenceError("some message");
|
||||
assert (e.name === "ReferenceError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "ReferenceError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (ReferenceError.prototype.toString === Error.prototype.toString);
|
||||
assert (ReferenceError.prototype.constructor === ReferenceError);
|
||||
assert (ReferenceError.prototype.name === "ReferenceError");
|
||||
assert (ReferenceError.prototype.message === "");
|
||||
assert (ReferenceError.prototype.toString() === "ReferenceError");
|
||||
|
||||
try
|
||||
{
|
||||
var a = non_existing_variable;
|
||||
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert(e instanceof ReferenceError);
|
||||
assert(e instanceof Error);
|
||||
assert(e instanceof Object);
|
||||
|
||||
assert(!(e instanceof Function));
|
||||
}
|
||||
|
||||
/* EvalError */
|
||||
e = new EvalError ();
|
||||
assert (e.name === "EvalError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "EvalError");
|
||||
|
||||
e = new EvalError("some message");
|
||||
assert (e.name === "EvalError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "EvalError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (EvalError.prototype.toString === Error.prototype.toString);
|
||||
assert (EvalError.prototype.constructor === EvalError);
|
||||
assert (EvalError.prototype.name === "EvalError");
|
||||
assert (EvalError.prototype.message === "");
|
||||
assert (EvalError.prototype.toString() === "EvalError");
|
||||
|
||||
/* RangeError */
|
||||
e = new RangeError ();
|
||||
assert (e.name === "RangeError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "RangeError");
|
||||
|
||||
e = new RangeError("some message");
|
||||
assert (e.name === "RangeError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "RangeError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (RangeError.prototype.toString === Error.prototype.toString);
|
||||
assert (RangeError.prototype.constructor === RangeError);
|
||||
assert (RangeError.prototype.name === "RangeError");
|
||||
assert (RangeError.prototype.message === "");
|
||||
assert (RangeError.prototype.toString() === "RangeError");
|
||||
|
||||
/* SyntaxError */
|
||||
e = new SyntaxError ();
|
||||
assert (e.name === "SyntaxError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "SyntaxError");
|
||||
|
||||
e = new SyntaxError("some message");
|
||||
assert (e.name === "SyntaxError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "SyntaxError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (SyntaxError.prototype.toString === Error.prototype.toString);
|
||||
assert (SyntaxError.prototype.constructor === SyntaxError);
|
||||
assert (SyntaxError.prototype.name === "SyntaxError");
|
||||
assert (SyntaxError.prototype.message === "");
|
||||
assert (SyntaxError.prototype.toString() === "SyntaxError");
|
||||
|
||||
/* URIError */
|
||||
e = new URIError ();
|
||||
assert (e.name === "URIError");
|
||||
assert (e.message === "");
|
||||
assert (e.toString() === "URIError");
|
||||
|
||||
e = new URIError("some message");
|
||||
assert (e.name === "URIError");
|
||||
assert (e.message === "some message");
|
||||
assert (e.toString() === "URIError: some message");
|
||||
|
||||
e.name = "";
|
||||
assert (e.toString() === "some message");
|
||||
e.message = "";
|
||||
assert (e.toString() === "");
|
||||
|
||||
assert (URIError.prototype.toString === Error.prototype.toString);
|
||||
assert (URIError.prototype.constructor === URIError);
|
||||
assert (URIError.prototype.name === "URIError");
|
||||
assert (URIError.prototype.message === "");
|
||||
assert (URIError.prototype.toString() === "URIError");
|
||||
@@ -0,0 +1,111 @@
|
||||
// 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.
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
|
||||
|
||||
function mustThrow(str) {
|
||||
try {
|
||||
eval(str);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
function assertArrayEqual(actual, expected) {
|
||||
assert(actual.length === expected.length);
|
||||
|
||||
for (var i = 0; i < actual.length; i++) {
|
||||
assert(actual[i] === expected[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Spread syntax
|
||||
function sum(x, y, z) {
|
||||
return x + y + z;
|
||||
}
|
||||
|
||||
const numbers = [1, 2, 3];
|
||||
|
||||
assert(sum(...numbers) === 6);
|
||||
|
||||
// Replace apply()
|
||||
function myFunction (v, w, x, y, z) {
|
||||
return v + w + x + y + z;
|
||||
}
|
||||
var args = [0, 1];
|
||||
|
||||
assert(myFunction (-1, ...args, 2, ...[3]) == 5);
|
||||
|
||||
// Apply for new
|
||||
var dateFields = [1970, 0, 1];
|
||||
var d = new Date(...dateFields);
|
||||
|
||||
assert(d.toString().substring(0, 24) === "Thu Jan 01 1970 00:00:00");
|
||||
|
||||
function applyAndNew(constructor, args) {
|
||||
function partial () {
|
||||
return constructor.apply (this, args);
|
||||
};
|
||||
if (typeof constructor.prototype === "object") {
|
||||
partial.prototype = Object.create(constructor.prototype);
|
||||
}
|
||||
return partial;
|
||||
}
|
||||
|
||||
function myConstructor () {
|
||||
assertArrayEqual(arguments, myArguments);
|
||||
this.prop1 = "val1";
|
||||
this.prop2 = "val2";
|
||||
};
|
||||
|
||||
var myArguments = ["hi", "how", "are", "you", "mr", null];
|
||||
var myConstructorWithArguments = applyAndNew(myConstructor, myArguments);
|
||||
|
||||
var obj = new myConstructorWithArguments;
|
||||
assert(Object.keys(obj).length === 2);
|
||||
assert(obj.prop1 === "val1");
|
||||
assert(obj.prop2 === "val2");
|
||||
|
||||
// Test spread prop call
|
||||
var o = { f(a,b,c) { return a + b + c },
|
||||
g(a,b) { throw new TypeError ("5") }
|
||||
};
|
||||
|
||||
assert (o.f(...["a", "b", "c"]) === "abc");
|
||||
|
||||
mustThrow ("o.g (...[1,2])")
|
||||
|
||||
// Test spread super call
|
||||
class MyArray extends Array {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
}
|
||||
}
|
||||
|
||||
var array = new MyArray(1, 2, 3);
|
||||
assertArrayEqual(array, [1,2,3]);
|
||||
assert(array instanceof MyArray);
|
||||
assert(array instanceof Array);
|
||||
|
||||
function argumentOrderTest() {
|
||||
var result = []
|
||||
for (i = 0; i < arguments.length; i++) {
|
||||
result.push(arguments[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
assertArrayEqual(argumentOrderTest(1, 2, ...[3, 4]), [1, 2, 3, 4]);
|
||||
@@ -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 arrayPrototypeValues = Array.prototype.values;
|
||||
|
||||
function f_mapped() {
|
||||
assert(typeof arguments[Symbol.iterator] === 'function');
|
||||
assert(arguments[Symbol.iterator] === arrayPrototypeValues);
|
||||
assert(Object.hasOwnProperty.call(arguments, Symbol.iterator));
|
||||
|
||||
let sum = 0;
|
||||
for (a of arguments) {
|
||||
sum += a;
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
|
||||
function f_unmapped(b = 2) {
|
||||
assert(typeof arguments[Symbol.iterator] === 'function');
|
||||
assert(arguments[Symbol.iterator] === arrayPrototypeValues);
|
||||
assert(Object.hasOwnProperty.call(arguments, Symbol.iterator));
|
||||
|
||||
let sum = 0;
|
||||
for (a of arguments) {
|
||||
sum += a;
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
|
||||
assert(f_mapped(1, 2, 3, 4, 5) === 15);
|
||||
assert(f_unmapped(1, 2, 3, 4, 5) === 15);
|
||||
|
||||
Object.defineProperty(Array.prototype, "values", { get : function () {
|
||||
/* should not be executed */
|
||||
assert(false);
|
||||
}});
|
||||
|
||||
assert(f_mapped(1, 2, 3, 4, 5) === 15);
|
||||
assert(f_unmapped(1, 2, 3, 4, 5) === 15);
|
||||
@@ -0,0 +1,201 @@
|
||||
// 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 f1(a, b, c)
|
||||
{
|
||||
'use strict';
|
||||
assert(!Object.hasOwnProperty(arguments,'caller'));
|
||||
}
|
||||
|
||||
f1(1, 2, 3);
|
||||
|
||||
// Normal arguments access
|
||||
|
||||
function f2(a = arguments)
|
||||
{
|
||||
assert(arguments[1] === 2)
|
||||
var arguments = 1
|
||||
assert(arguments === 1)
|
||||
assert(a[1] === 2)
|
||||
}
|
||||
f2(undefined, 2)
|
||||
|
||||
function f3(a = arguments)
|
||||
{
|
||||
assert(arguments() === "X")
|
||||
function arguments() { return "X" }
|
||||
assert(arguments() === "X")
|
||||
assert(a[1] === "R")
|
||||
}
|
||||
f3(undefined, "R")
|
||||
|
||||
function f4(a = arguments)
|
||||
{
|
||||
const arguments = 3.25
|
||||
assert(arguments === 3.25)
|
||||
assert(a[1] === -1.5)
|
||||
}
|
||||
f4(undefined, -1.5)
|
||||
|
||||
// Normal arguments access with eval
|
||||
|
||||
function f5(a = arguments)
|
||||
{
|
||||
assert(arguments[1] === 2)
|
||||
var arguments = 1
|
||||
assert(arguments === 1)
|
||||
assert(a[1] === 2)
|
||||
eval()
|
||||
}
|
||||
f5(undefined, 2)
|
||||
|
||||
function f6(a = arguments)
|
||||
{
|
||||
assert(arguments() === "X")
|
||||
function arguments() { return "X" }
|
||||
assert(arguments() === "X")
|
||||
assert(a[1] === "R")
|
||||
eval()
|
||||
}
|
||||
f6(undefined, "R")
|
||||
|
||||
function f7(a = arguments)
|
||||
{
|
||||
const arguments = 3.25
|
||||
assert(arguments === 3.25)
|
||||
assert(a[1] === -1.5)
|
||||
eval()
|
||||
}
|
||||
f7(undefined, -1.5)
|
||||
|
||||
// Argument access through a function
|
||||
|
||||
function f8(a = () => arguments)
|
||||
{
|
||||
assert(arguments[1] === 2)
|
||||
var arguments = 1
|
||||
assert(arguments === 1)
|
||||
assert(a()[1] === 2)
|
||||
}
|
||||
f8(undefined, 2)
|
||||
|
||||
function f9(a = () => arguments)
|
||||
{
|
||||
assert(arguments() === "X")
|
||||
function arguments() { return "X" }
|
||||
assert(arguments() === "X")
|
||||
assert(a()[1] === "R")
|
||||
}
|
||||
f9(undefined, "R")
|
||||
|
||||
function f10(a = () => arguments)
|
||||
{
|
||||
let arguments = 3.25
|
||||
assert(arguments === 3.25)
|
||||
assert(a()[1] === -1.5)
|
||||
}
|
||||
f10(undefined, -1.5)
|
||||
|
||||
// Argument access through an eval
|
||||
|
||||
function f11(a = eval("() => arguments"))
|
||||
{
|
||||
assert(arguments[1] === 2)
|
||||
var arguments = 1
|
||||
assert(arguments === 1)
|
||||
assert(a()[1] === 2)
|
||||
}
|
||||
f11(undefined, 2)
|
||||
|
||||
function f12(a = eval("() => arguments"))
|
||||
{
|
||||
assert(arguments() === "X")
|
||||
function arguments() { return "X" }
|
||||
assert(arguments() === "X")
|
||||
assert(a()[1] === "R")
|
||||
}
|
||||
f12(undefined, "R")
|
||||
|
||||
function f13(a = eval("() => arguments"))
|
||||
{
|
||||
const arguments = 3.25
|
||||
assert(arguments === 3.25)
|
||||
assert(a()[1] === -1.5)
|
||||
}
|
||||
f13(undefined, -1.5)
|
||||
|
||||
// Other cases
|
||||
|
||||
try {
|
||||
function f14(a = arguments)
|
||||
{
|
||||
assert(a[1] === 6)
|
||||
arguments;
|
||||
let arguments = 1;
|
||||
}
|
||||
f14(undefined, 6)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
|
||||
try {
|
||||
eval("'use strict'; function f(a = arguments) { arguments = 5; eval() }");
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError)
|
||||
}
|
||||
|
||||
function f15()
|
||||
{
|
||||
assert(arguments[0] === "A")
|
||||
var arguments = 1
|
||||
assert(arguments === 1)
|
||||
}
|
||||
f15("A")
|
||||
|
||||
function f16()
|
||||
{
|
||||
assert(arguments() === "W")
|
||||
function arguments() { return "W" }
|
||||
assert(arguments() === "W")
|
||||
}
|
||||
f16("A")
|
||||
|
||||
function f17(a = arguments = "Val")
|
||||
{
|
||||
assert(arguments === "Val")
|
||||
}
|
||||
f17();
|
||||
|
||||
function f18(s = (v) => arguments = v, g = () => arguments)
|
||||
{
|
||||
const arguments = -3.25
|
||||
s("X")
|
||||
|
||||
assert(g() === "X")
|
||||
assert(arguments === -3.25)
|
||||
}
|
||||
f18()
|
||||
|
||||
function f19(e = (v) => eval(v))
|
||||
{
|
||||
var arguments = -12.5
|
||||
e("arguments[0] = 4.5")
|
||||
|
||||
assert(e("arguments[0]") === 4.5)
|
||||
assert(e("arguments[1]") === "A")
|
||||
assert(arguments === -12.5)
|
||||
}
|
||||
f19(undefined, "A");
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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 check_syntax_error (txt) {
|
||||
try {
|
||||
eval (txt)
|
||||
assert (false)
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
var a = 21;
|
||||
var b = 10;
|
||||
var c;
|
||||
|
||||
check_syntax_error ("c = a++b");
|
||||
check_syntax_error ("c = a--b");
|
||||
|
||||
check_syntax_error ("c = a +* b");
|
||||
check_syntax_error ("c = a -* b");
|
||||
check_syntax_error ("c = a +/ b");
|
||||
check_syntax_error ("c = a -/ b");
|
||||
check_syntax_error ("c = a +% b");
|
||||
check_syntax_error ("c = a -% b");
|
||||
|
||||
check_syntax_error ("a =* b");
|
||||
check_syntax_error ("a =/ b");
|
||||
check_syntax_error ("a =% b");
|
||||
|
||||
check_syntax_error ("c = a+");
|
||||
check_syntax_error ("c = a-");
|
||||
|
||||
check_syntax_error("a++\n()");
|
||||
check_syntax_error("a--\n.b");
|
||||
|
||||
assert((-2 .toString()) === -2);
|
||||
|
||||
Number.prototype[0] = 123;
|
||||
assert(-2[0] === -123);
|
||||
|
||||
function f() {
|
||||
var a = 0;
|
||||
function g() {}
|
||||
|
||||
try {
|
||||
eval ("g(this, 'a' = 1)");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
try {
|
||||
eval ("g(this, 'a' += 1)");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
assert (a === 0);
|
||||
}
|
||||
f();
|
||||
|
||||
function g(a, b)
|
||||
{
|
||||
assert(b === "undefined");
|
||||
}
|
||||
g(this, typeof undeclared_var)
|
||||
|
||||
function h()
|
||||
{
|
||||
var done = false;
|
||||
var o = { a: function () { done = (this === o) } }
|
||||
function f() {}
|
||||
|
||||
with (o) {
|
||||
f(this, a());
|
||||
}
|
||||
assert(done);
|
||||
}
|
||||
h();
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 a = 21;
|
||||
var b = 10;
|
||||
var c;
|
||||
|
||||
c = a + b;
|
||||
assert(c == 31);
|
||||
|
||||
c = a - b;
|
||||
assert(c == 11);
|
||||
|
||||
c = a * b;
|
||||
assert(c == 210);
|
||||
|
||||
c = a / b;
|
||||
assert(c >= 2.1 - 0.000001 && c <= 2.1 + 0.000001);
|
||||
|
||||
c = a % b;
|
||||
assert(c == 1);
|
||||
|
||||
c = a++;
|
||||
assert(c == 21);
|
||||
|
||||
c = a--;
|
||||
assert(c == 22);
|
||||
|
||||
var o = { p : 1 };
|
||||
|
||||
assert (++o.p === 2);
|
||||
assert (o.p === 2);
|
||||
assert (--o.p === 1);
|
||||
assert (o.p === 1);
|
||||
|
||||
try {
|
||||
eval ('++ ++ a');
|
||||
assert (false);
|
||||
}
|
||||
catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
assert (0.1 + 0.2 != 0.3);
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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 o1 = { valueOf() { return Symbol() } }
|
||||
var o2 = { valueOf() { throw "Should not reach here" } }
|
||||
|
||||
function check_type_error(code) {
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
}
|
||||
|
||||
check_type_error("o1 - o2")
|
||||
check_type_error("o1 * o2")
|
||||
check_type_error("o1 / o2")
|
||||
check_type_error("o1 % o2")
|
||||
check_type_error("o1 ** o2")
|
||||
check_type_error("o1 | o2")
|
||||
check_type_error("o1 & o2")
|
||||
check_type_error("o1 ^ o2")
|
||||
check_type_error("o1 << o2")
|
||||
check_type_error("o1 >> o2")
|
||||
check_type_error("o1 >>> o2")
|
||||
@@ -0,0 +1,344 @@
|
||||
// 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.
|
||||
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Some methods are taken from v8/test/mjsunit/mjsunit.js
|
||||
|
||||
function classOf(object) {
|
||||
// Argument must not be null or undefined.
|
||||
var string = Object.prototype.toString.call(object);
|
||||
// String has format [object <ClassName>].
|
||||
return string.substring(8, string.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two objects for key/value equality.
|
||||
* Returns true if they are equal, false otherwise.
|
||||
*/
|
||||
function deepObjectEquals(a, b) {
|
||||
var aProps = Object.keys(a);
|
||||
aProps.sort();
|
||||
var bProps = Object.keys(b);
|
||||
bProps.sort();
|
||||
if (!deepEquals(aProps, bProps)) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < aProps.length; i++) {
|
||||
if (!deepEquals(a[aProps[i]], b[aProps[i]])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
var assertInstanceof = function assertInstanceof(obj, type) {
|
||||
if (!(obj instanceof type)) {
|
||||
var actualTypeName = null;
|
||||
var actualConstructor = Object.getPrototypeOf(obj).constructor;
|
||||
if (typeof actualConstructor == "function") {
|
||||
actualTypeName = actualConstructor.name || String(actualConstructor);
|
||||
} print("Object <" + obj + "> is not an instance of <" + (type.name || type) + ">" + (actualTypeName ? " but of < " + actualTypeName + ">" : ""));
|
||||
}
|
||||
};
|
||||
|
||||
function deepEquals(a, b) {
|
||||
if (a === b) {
|
||||
if (a === 0) return (1 / a) === (1 / b);
|
||||
return true;
|
||||
}
|
||||
if (typeof a != typeof b) return false;
|
||||
if (typeof a == "number") return isNaN(a) && isNaN(b);
|
||||
if (typeof a !== "object" && typeof a !== "function") return false;
|
||||
var objectClass = classOf(a);
|
||||
if (objectClass !== classOf(b)) return false;
|
||||
if (objectClass === "RegExp") {
|
||||
return (a.toString() === b.toString());
|
||||
}
|
||||
if (objectClass === "Function") return false;
|
||||
if (objectClass === "Array") {
|
||||
var elementCount = 0;
|
||||
if (a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (!deepEquals(a[i], b[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (objectClass == "String" || objectClass == "Number" || objectClass == "Boolean" || objectClass == "Date") {
|
||||
if (a.valueOf() !== b.valueOf()) return false;
|
||||
}
|
||||
return deepObjectEquals(a, b);
|
||||
}
|
||||
|
||||
var assertEquals = function assertEquals(expected, found, name_opt) {
|
||||
if (!deepEquals(found, expected)) {
|
||||
assert(false);
|
||||
}
|
||||
};
|
||||
|
||||
function assertArrayLikeEquals(value, expected, type) {
|
||||
assertInstanceof(value, type);
|
||||
assert(expected.length === value.length);
|
||||
for (var i=0; i<value.length; ++i) {
|
||||
assertEquals(expected[i], value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
assert(1 === Array.from.length);
|
||||
|
||||
// Assert that constructor is called with "length" for array-like objects
|
||||
|
||||
var myCollectionCalled = false;
|
||||
function MyCollection(length) {
|
||||
myCollectionCalled = true;
|
||||
assert(1 === arguments.length);
|
||||
|
||||
assert(5 === length);
|
||||
}
|
||||
|
||||
Array.from.call(MyCollection, {length: 5});
|
||||
assert(myCollectionCalled === true);
|
||||
// Assert that calling mapfn with / without thisArg in sloppy and strict modes
|
||||
// works as expected.
|
||||
|
||||
var global = this;
|
||||
function non_strict(){ assert(global === this); }
|
||||
function strict(){ "use strict"; assert(void 0 === this); }
|
||||
function strict_null(){ "use strict"; assert(null === this); }
|
||||
Array.from([1], non_strict);
|
||||
Array.from([1], non_strict, void 0);
|
||||
Array.from([1], strict);
|
||||
Array.from([1], strict, void 0);
|
||||
|
||||
function testArrayFrom(thisArg, constructor) {
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], constructor);
|
||||
|
||||
assertArrayLikeEquals(Array.from.call(thisArg,
|
||||
{ length: 1, '0': { 'foo': 'bar' } }), [{'foo': 'bar'}], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, { length: -1, '0': { 'foo': 'bar' } }), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg,
|
||||
[ 'foo', 'bar', 'baz' ]), ['foo', 'bar', 'baz'], constructor);
|
||||
var kSet = new Set(['foo', 'bar', 'baz']);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'], constructor);
|
||||
var kMap = new Map(['foo', 'bar', 'baz'].entries());
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, kMap), [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
|
||||
return this.filter(x);
|
||||
}, {
|
||||
filter: function(x) { return x.toUpperCase(); }
|
||||
}), ['T', 'E', 'S', 'T'], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
|
||||
return x.toUpperCase();
|
||||
}), ['T', 'E', 'S', 'T'], constructor);
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, undefined);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], "noncallable");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
var nullIterator = {};
|
||||
nullIterator[Symbol.iterator] = null;
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, nullIterator), [],
|
||||
constructor);
|
||||
|
||||
var nonObjIterator = {};
|
||||
nonObjIterator[Symbol.iterator] = function() { return "nonObject"; };
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, nonObjIterator);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Ensure iterator is only accessed once, and only invoked once
|
||||
var called = false;
|
||||
var arr = [1, 2, 3];
|
||||
var obj = {};
|
||||
|
||||
// Test order --- only get iterator method once
|
||||
function testIterator() {
|
||||
assert(called !== "@@iterator should be called only once");
|
||||
called = true;
|
||||
assert(obj === this);
|
||||
return arr[Symbol.iterator]();
|
||||
}
|
||||
var getCalled = false;
|
||||
Object.defineProperty(obj, Symbol.iterator, {
|
||||
get: function() {
|
||||
assert(getCalled !== "@@iterator should be accessed only once");
|
||||
getCalled = true;
|
||||
return testIterator;
|
||||
},
|
||||
set: function() {
|
||||
// "@@iterator should not be set"
|
||||
assert(false);
|
||||
}
|
||||
});
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, obj), [1, 2, 3], constructor);
|
||||
}
|
||||
|
||||
function Other() {}
|
||||
|
||||
var boundFn = (function() {}).bind(Array, 27);
|
||||
|
||||
testArrayFrom(Array, Array);
|
||||
testArrayFrom(null, Array);
|
||||
testArrayFrom({}, Array);
|
||||
testArrayFrom(Object, Object);
|
||||
testArrayFrom(Other, Other);
|
||||
testArrayFrom(Math.cos, Array);
|
||||
testArrayFrom(boundFn, boundFn);
|
||||
|
||||
// Assert that [[DefineOwnProperty]] is used in ArrayFrom, meaning a
|
||||
// setter isn't called, and a failed [[DefineOwnProperty]] will throw.
|
||||
var setterCalled = 0;
|
||||
function exotic() {
|
||||
Object.defineProperty(this, '0', {
|
||||
get: function() { return 2; },
|
||||
set: function() { setterCalled++; }
|
||||
});
|
||||
}
|
||||
|
||||
// Non-configurable properties can't be overwritten with DefineOwnProperty
|
||||
// The setter wasn't called
|
||||
try {
|
||||
Array.from.call(exotic, [1]);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert( 0 === setterCalled);
|
||||
|
||||
// Non-callable iterators should cause a TypeError before calling the target
|
||||
// constructor.
|
||||
items = {};
|
||||
items[Symbol.iterator] = 7;
|
||||
function TestError() {}
|
||||
function ArrayLike() { throw new TestError() }
|
||||
|
||||
try {
|
||||
Array.from.call(ArrayLike, items);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Check that array properties defined are writable, enumerable, configurable
|
||||
function ordinary() { }
|
||||
var x = Array.from.call(ordinary, [2]);
|
||||
var xlength = Object.getOwnPropertyDescriptor(x, 'length');
|
||||
assert(1 === xlength.value);
|
||||
assert(true === xlength.writable);
|
||||
assert(true === xlength.enumerable);
|
||||
assert(true === xlength.configurable);
|
||||
var x0 = Object.getOwnPropertyDescriptor(x, 0);
|
||||
assert(2 === x0.value);
|
||||
assert(true === xlength.writable);
|
||||
assert(true === xlength.enumerable);
|
||||
assert(true === xlength.configurable);
|
||||
|
||||
/* Test iterator close */
|
||||
function __createIterableObject (arr, methods) {
|
||||
methods = methods || {};
|
||||
if (typeof Symbol !== 'function' || !Symbol.iterator) {
|
||||
return {};
|
||||
}
|
||||
arr.length++;
|
||||
var iterator = {
|
||||
next: function() {
|
||||
return { value: arr.shift(), done: arr.length <= 0 };
|
||||
},
|
||||
'return': methods['return'],
|
||||
'throw': methods['throw']
|
||||
};
|
||||
var iterable = {};
|
||||
iterable[Symbol.iterator] = function () { return iterator; };
|
||||
return iterable;
|
||||
};
|
||||
|
||||
function close1() {
|
||||
var closed = false;
|
||||
var iter = __createIterableObject([1, 2, 3], {
|
||||
'return': function() { closed = true; return {}; }
|
||||
});
|
||||
|
||||
try {
|
||||
Array.from(iter, x => { throw 5 });
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 5);
|
||||
}
|
||||
|
||||
return closed;
|
||||
}
|
||||
assert(close1());
|
||||
|
||||
function close2() {
|
||||
var closed = false;
|
||||
var iter = __createIterableObject([1, 2, 3], {
|
||||
'return': function() { closed = true; throw 6 }
|
||||
});
|
||||
|
||||
try {
|
||||
Array.from(iter, x => { throw 5 });
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 5);
|
||||
}
|
||||
|
||||
return closed;
|
||||
}
|
||||
assert(close2());
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// 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.
|
||||
|
||||
assert(Array.isArray([]) === true);
|
||||
assert(Array.isArray([1]) === true);
|
||||
assert(Array.isArray(new Array()) === true);
|
||||
assert(Array.isArray(new Array('a', 'b', 'c', 'd')) === true);
|
||||
assert(Array.isArray(new Array(3)) === true);
|
||||
assert(Array.isArray(Array.prototype) === true);
|
||||
assert(Array.isArray(new Proxy([], {})) === true);
|
||||
|
||||
assert(Array.isArray() === false);
|
||||
assert(Array.isArray({}) === false);
|
||||
assert(Array.isArray(null) === false);
|
||||
assert(Array.isArray(undefined) === false);
|
||||
assert(Array.isArray(17) === false);
|
||||
assert(Array.isArray('Array') === false);
|
||||
assert(Array.isArray(true) === false);
|
||||
assert(Array.isArray(false) === false);
|
||||
assert(Array.isArray(new Uint8Array(32)) === false);
|
||||
assert(Array.isArray({ __proto__: Array.prototype }) === false);
|
||||
|
||||
var revocable = Proxy.revocable ({}, {});
|
||||
var proxy = revocable.proxy;
|
||||
revocable.revoke();
|
||||
|
||||
try {
|
||||
Array.isArray(proxy);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
var revocable = Proxy.revocable ([], {});
|
||||
var proxy = revocable.proxy;
|
||||
|
||||
assert(Array.isArray(proxy) === true);
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// 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 F (){}
|
||||
var obj = Reflect.construct (Array, [], F);
|
||||
obj[2] = 'foo';
|
||||
assert (obj.length === 3 && obj instanceof F);
|
||||
|
||||
try {
|
||||
Reflect.construct (Array, [-1], F);
|
||||
} catch (e) {
|
||||
assert (e instanceof RangeError);
|
||||
}
|
||||
|
||||
var o = new Proxy (function f () {}, { get (t,p,r) { if (p == "prototype") { throw "Kitten" } Reflect.get (...arguments) }})
|
||||
|
||||
try {
|
||||
Reflect.construct (Array, [], o)
|
||||
} catch (e) {
|
||||
assert (e === "Kitten");
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// 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.
|
||||
|
||||
// Test with regular inputs
|
||||
var array1 = Array.of(1, 2, 3, 4, 5);
|
||||
assert(array1.length === 5);
|
||||
assert(array1[2] === 3);
|
||||
|
||||
// Test with no input
|
||||
var array2 = Array.of();
|
||||
assert(array2.length === 0);
|
||||
assert(array2[0] === undefined);
|
||||
|
||||
// Test when an input is another array
|
||||
var array3 = Array.of(array1, 6, 7);
|
||||
assert(array3.length === 3);
|
||||
assert(array3[0] instanceof Array);
|
||||
assert(array3[0][3] === 4);
|
||||
assert(array3[2] === 7);
|
||||
|
||||
// Test with undefined
|
||||
var array4 = Array.of(undefined);
|
||||
assert(array4.length === 1);
|
||||
assert(array4[0] === undefined);
|
||||
|
||||
// Test when input is an object
|
||||
var obj = {
|
||||
0: 0,
|
||||
1: 1
|
||||
};
|
||||
|
||||
var array5 = Array.of(obj, 2, 3);
|
||||
assert(array5[0] instanceof Object);
|
||||
assert(array5[0][0] === 0);
|
||||
assert(array5[0][1] === 1);
|
||||
assert(array5[2] === 3);
|
||||
|
||||
// Test with array holes
|
||||
var array6 = Array.of.apply(null, [,,undefined]);
|
||||
assert(array6.length === 3);
|
||||
assert(array6[0] === undefined);
|
||||
|
||||
// Test with another class
|
||||
var hits = 0;
|
||||
function Test() {
|
||||
hits++;
|
||||
}
|
||||
Test.of = Array.of;
|
||||
|
||||
hits = 0;
|
||||
var array6 = Test.of(1, 2);
|
||||
assert(hits === 1);
|
||||
assert(array6.length === 2);
|
||||
assert(array6[1] === 2);
|
||||
|
||||
// Test with bounded builtin function
|
||||
var boundedBuiltinFn = Array.of.bind(Array);
|
||||
var array7 = Array.of.call(boundedBuiltinFn, boundedBuiltinFn);
|
||||
assert(array7.length === 1);
|
||||
assert(array7[0] === boundedBuiltinFn);
|
||||
|
||||
// Test superficial features
|
||||
var desc = Object.getOwnPropertyDescriptor(Array, "of");
|
||||
assert(desc.configurable === true);
|
||||
assert(desc.writable === true);
|
||||
assert(desc.enumerable === false);
|
||||
assert(Array.of.length === 0);
|
||||
@@ -0,0 +1,364 @@
|
||||
// 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 checkSyntax (str) {
|
||||
try {
|
||||
eval (str);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
}
|
||||
|
||||
function assertArrayEqual (actual, expected) {
|
||||
assert (actual.length === expected.length);
|
||||
|
||||
for (var i = 0; i < actual.length; i++) {
|
||||
assert (actual[i] === expected[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function mustThrow (str) {
|
||||
try {
|
||||
eval (str);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
checkSyntax ("var [a]");
|
||||
checkSyntax ("var [a, o.a]");
|
||||
checkSyntax ("var [a, ...b,]");
|
||||
checkSyntax ("var [a, ...b = 4]");
|
||||
checkSyntax ("var [a, ...[b] = 4]");
|
||||
checkSyntax ("var [let]");
|
||||
checkSyntax ("var [get = []");
|
||||
checkSyntax ("var [get : 5]");
|
||||
checkSyntax ("var [[a = {},]");
|
||||
checkSyntax ("let [a,a] = []");
|
||||
checkSyntax ("let [a, ...a] = []");
|
||||
checkSyntax ("const [a,a] = []");
|
||||
checkSyntax ("const [a, ...a] = []");
|
||||
checkSyntax ("[new Object()] = []");
|
||||
checkSyntax ("[Object()] = []");
|
||||
checkSyntax ("[(a, b, d, c)] = []");
|
||||
checkSyntax ("[super] = []");
|
||||
checkSyntax ("[this] = []");
|
||||
checkSyntax ("[()] = []");
|
||||
checkSyntax ("try { let [$] = $;");
|
||||
checkSyntax ("let a, [ b.c ] = [6];");
|
||||
checkSyntax ("let [(a)] = [1]");
|
||||
checkSyntax ("[...a = []] = [1]");
|
||||
checkSyntax ("[...[a] = []] = [1]");
|
||||
checkSyntax ("[...[a, [...b] = []] = []] = [1]");
|
||||
|
||||
mustThrow ("var [a] = 4");
|
||||
mustThrow ("var [a] = 5");
|
||||
mustThrow ("var [a] = {}");
|
||||
mustThrow ("var [a] = { get [Symbol.iterator] () { throw new TypeError } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () {} }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return {} } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next: 5 } } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next: 5 } } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { get next() { throw new TypeError } } } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next () { } } } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next () { } } } }");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next () { return { get value () { throw new TypeError }}}}}}");
|
||||
mustThrow ("var [a] = { [Symbol.iterator] () { return { next () { return { get done () { throw new TypeError }}}}}}");
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
|
||||
|
||||
// Basic variable assignment
|
||||
(function () {
|
||||
var foo = ["one", "two", "three"];
|
||||
|
||||
var [red, yellow, green] = foo;
|
||||
assert (red === "one");
|
||||
assert (yellow === "two");
|
||||
assert (green === "three");
|
||||
}) ();
|
||||
|
||||
// Assignment separate from declaration
|
||||
(function () {
|
||||
var a, b;
|
||||
|
||||
[a, b] = [1, 2];
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
}) ();
|
||||
|
||||
// Default values
|
||||
(function () {
|
||||
var a, b;
|
||||
[a = 5, b = 7] = [1];
|
||||
|
||||
assert (a === 1);
|
||||
assert (b === 7);
|
||||
}) ();
|
||||
|
||||
// Swapping variables
|
||||
(function () {
|
||||
var a = 1;
|
||||
var b = 3;
|
||||
|
||||
[a, b] = [b, a];
|
||||
assert (a === 3);
|
||||
assert (b === 1);
|
||||
|
||||
var arr = [1,2,3];
|
||||
[arr[2], arr[1]] = [arr[1], arr[2]];
|
||||
assertArrayEqual (arr, [1, 3, 2]);
|
||||
}) ();
|
||||
|
||||
// Parsing an array returned from a function
|
||||
(function () {
|
||||
function f() {
|
||||
return [1, 2];
|
||||
}
|
||||
|
||||
var a, b;
|
||||
[a, b] = f();
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
}) ();
|
||||
|
||||
// Ignoring some returned values
|
||||
(function () {
|
||||
function f() {
|
||||
return [1, 2, 3];
|
||||
}
|
||||
|
||||
var a, b;
|
||||
[a, ,b] = f();
|
||||
assert (a === 1);
|
||||
assert (b === 3);
|
||||
}) ();
|
||||
|
||||
// Ignoring some returned values
|
||||
(function () {
|
||||
var [a, ...b] = [1, 2, 3];
|
||||
assert (a === 1);
|
||||
assertArrayEqual (b, [2, 3]);
|
||||
}) ();
|
||||
|
||||
// Unpacking values from a regular expression match
|
||||
(function () {
|
||||
function parseProtocol(url) {
|
||||
var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
|
||||
if (!parsedURL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var [, protocol, fullhost, fullpath] = parsedURL;
|
||||
return protocol;
|
||||
}
|
||||
|
||||
assert (parseProtocol("https://developer.mozilla.org/en-US/Web/JavaScript") === "https");
|
||||
}) ();
|
||||
|
||||
// Test inner patterns I.
|
||||
(function () {
|
||||
let [a, [b, [c = 4, d = 5]], [e] = [6]] = [1, [2, [3,undefined]]];
|
||||
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
assert (c === 3);
|
||||
assert (d === 5);
|
||||
assert (e === 6);
|
||||
}) ();
|
||||
|
||||
// Test inner patterns II.
|
||||
(function () {
|
||||
var o = {};
|
||||
[a, b, c, o.a = 4, o.b, o.c = 3] = ["1", "2", "3", undefined, "8", "6"];
|
||||
|
||||
assert (a === "1");
|
||||
assert (b === "2");
|
||||
assert (c === "3");
|
||||
assert (o.a === 4);
|
||||
assert (o.b === "8");
|
||||
assert (o.c === "6");
|
||||
}) ();
|
||||
|
||||
// Test rest element I.
|
||||
(function () {
|
||||
var o = {};
|
||||
[...o.a] = ["1", "2", "3"];
|
||||
|
||||
assertArrayEqual (o.a, ["1", "2", "3"]);
|
||||
}) ();
|
||||
|
||||
// Test rest element II.
|
||||
(function () {
|
||||
[...[a,b,c]] = ["1", "2", "3"];
|
||||
|
||||
assert (a === "1");
|
||||
assert (b === "2");
|
||||
assert (c === "3");
|
||||
}) ();
|
||||
|
||||
// Test inner object pattern I.
|
||||
(function () {
|
||||
[{f : a, g : b}, , , ...[c, d, e]] = [{ f : "1", g : "2"}, 3, 4, 5, 6, 7];
|
||||
|
||||
assert (a === "1");
|
||||
assert (b === "2");
|
||||
assert (c === 5);
|
||||
assert (d === 6);
|
||||
assert (e === 7);
|
||||
}) ();
|
||||
|
||||
// Multiple declaration
|
||||
(function () {
|
||||
var [a] = [1], [b] = [2];
|
||||
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
}) ();
|
||||
|
||||
// Force the creation of lexical environment I.
|
||||
(function () {
|
||||
const [a] = [1];
|
||||
eval();
|
||||
|
||||
assert (a === 1);
|
||||
}) ();
|
||||
|
||||
// Force the creation of lexical environment II.
|
||||
(function () {
|
||||
let [a] = [1];
|
||||
eval();
|
||||
|
||||
assert (a === 1);
|
||||
}) ();
|
||||
|
||||
// Check the parsing of AssignmentElement
|
||||
(function () {
|
||||
var a = 6;
|
||||
[((a))] = [7];
|
||||
assert (a === 7);
|
||||
}) ();
|
||||
|
||||
// Test iterator closing
|
||||
function __createIterableObject (arr, methods) {
|
||||
methods = methods || {};
|
||||
if (typeof Symbol !== 'function' || !Symbol.iterator) {
|
||||
return {};
|
||||
}
|
||||
arr.length++;
|
||||
var iterator = {
|
||||
next: function() {
|
||||
return { value: arr.shift(), done: arr.length <= 0 };
|
||||
},
|
||||
'return': methods['return'],
|
||||
'throw': methods['throw']
|
||||
};
|
||||
var iterable = {};
|
||||
iterable[Symbol.iterator] = function () { return iterator; };
|
||||
return iterable;
|
||||
};
|
||||
|
||||
(function () {
|
||||
var closed = false;
|
||||
var iter = __createIterableObject([1, 2, 3], {
|
||||
'return': function() { closed = true; return {}; }
|
||||
});
|
||||
var [a, b] = iter;
|
||||
assert (closed === true);
|
||||
assert (a === 1);
|
||||
assert (b === 2);
|
||||
}) ();
|
||||
|
||||
(function () {
|
||||
var value = { y: "42" };
|
||||
var x = {};
|
||||
var assignmentResult, iterationResult, iter;
|
||||
|
||||
iter = (function*() {
|
||||
assignmentResult = { y: x[yield] } = value;
|
||||
}());
|
||||
|
||||
iterationResult = iter.next();
|
||||
|
||||
assert (assignmentResult === undefined);
|
||||
assert (iterationResult.value === undefined);
|
||||
assert (iterationResult.done === false);
|
||||
assert (x.prop === undefined);
|
||||
|
||||
iterationResult = iter.next('prop');
|
||||
|
||||
assert (assignmentResult === value);
|
||||
assert (iterationResult.value === undefined);
|
||||
assert (iterationResult.done === true);
|
||||
assert (x.prop === "42");
|
||||
}) ();
|
||||
|
||||
(function () {
|
||||
var value = { foo: "42" };
|
||||
var x = {};
|
||||
var assignmentResult, iterationResult, iter;
|
||||
|
||||
iter = (function*() {
|
||||
assignmentResult = { ['f' + 'o' + 'o']: x[yield] } = value;
|
||||
}());
|
||||
|
||||
iterationResult = iter.next();
|
||||
|
||||
assert (assignmentResult === undefined);
|
||||
assert (iterationResult.value === undefined);
|
||||
assert (iterationResult.done === false);
|
||||
assert (x.prop === undefined);
|
||||
|
||||
iterationResult = iter.next('prop');
|
||||
|
||||
assert (assignmentResult === value);
|
||||
assert (iterationResult.value === undefined);
|
||||
assert (iterationResult.done === true);
|
||||
assert (x.prop === "42");
|
||||
}) ();
|
||||
|
||||
mustThrow ("var iter = __createIterableObject([], "
|
||||
+ "{ get 'return'() { throw new TypeError() }});"
|
||||
+ "var [a] = iter");
|
||||
|
||||
mustThrow ("var iter = __createIterableObject([], "
|
||||
+ "{ 'return': 5 });"
|
||||
+ "var [a] = iter");
|
||||
|
||||
mustThrow ("var iter = __createIterableObject([], "
|
||||
+ "{ 'return': function() { return 5; }});"
|
||||
+ "var [a] = iter");
|
||||
|
||||
mustThrow ("try { throw 5 } catch (e) {"
|
||||
+ "var iter = __createIterableObject([], "
|
||||
+ "{ get 'return'() { throw new TypeError() }});"
|
||||
+ "var [a] = iter }");
|
||||
|
||||
mustThrow ("try { throw 5 } catch (e) {"
|
||||
+ "var iter = __createIterableObject([], "
|
||||
+ "{ 'return': 5 });"
|
||||
+ "var [a] = iter }");
|
||||
|
||||
mustThrow ("try { throw 5 } catch (e) {"
|
||||
+ "var iter = __createIterableObject([], "
|
||||
+ "{ 'return': function() { return 5; }});"
|
||||
+ "var [a] = iter }");
|
||||
|
||||
try {
|
||||
eval ("var a = 0; 1 + [a] = [1]");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
// 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 = {};
|
||||
|
||||
// Checking behavior with normal inputs
|
||||
var array = ["foo", 1, "bar", obj, 2, "baz"];
|
||||
assert(array.copyWithin(2,0,6).toString() === "foo,1,foo,1,bar,[object Object]");
|
||||
assert(array.copyWithin(0,1,3).toString() === "1,foo,foo,1,bar,[object Object]");
|
||||
assert(array.copyWithin(3,0,4).toString() === "1,foo,foo,1,foo,foo");
|
||||
|
||||
// Checking behavior with default inputs
|
||||
var array = ["foo", 1, "bar", obj, 2, "baz"];
|
||||
assert(array.copyWithin().toString() === "foo,1,bar,[object Object],2,baz");
|
||||
assert(array.copyWithin(2).toString() === "foo,1,foo,1,bar,[object Object]");
|
||||
assert(array.copyWithin(1,4).toString() === "foo,bar,[object Object],1,bar,[object Object]");
|
||||
|
||||
// Checking behavior when argument is negative or bigger then length
|
||||
var array = ["foo", 1, "bar", obj, 2, "baz"];
|
||||
assert(array.copyWithin(12,3,-3).toString() === "foo,1,bar,[object Object],2,baz");
|
||||
assert(array.copyWithin(-2,-4,3).toString() === "foo,1,bar,[object Object],bar,baz");
|
||||
assert(array.copyWithin(1,-5,30).toString() === "foo,1,bar,[object Object],bar,baz");
|
||||
|
||||
// Checking behavior with undefined, NaN, +/- Infinity
|
||||
var array = ["foo", 1, "bar", obj, 2, "baz"];
|
||||
assert(array.copyWithin(undefined).toString() === "foo,1,bar,[object Object],2,baz");
|
||||
assert(array.copyWithin(2, NaN).toString()=== "foo,1,foo,1,bar,[object Object]");
|
||||
assert(array.copyWithin(2,undefined,5).toString() === "foo,1,foo,1,foo,1");
|
||||
|
||||
var array = ["foo", 1, "bar", obj, 2, "baz"];
|
||||
assert(array.copyWithin(Infinity,2,NaN).toString() === "foo,1,bar,[object Object],2,baz");
|
||||
assert(array.copyWithin(Infinity,-Infinity,4).toString()=== "foo,1,bar,[object Object],2,baz");
|
||||
assert(array.copyWithin(NaN,0,3).toString() === "foo,1,bar,[object Object],2,baz");
|
||||
|
||||
// Checking behavior when no length property defined
|
||||
var obj = { copyWithin : Array.prototype.copyWithin };
|
||||
|
||||
obj.copyWithin();
|
||||
assert(obj.length === undefined);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = { copyWithin : Array.prototype.copyWithin };
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.copyWithin(1);
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = { copyWithin : Array.prototype.copyWithin, length : 5 };
|
||||
Object.defineProperty(obj, '2', { 'get' : function () {throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.copyWithin(2);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e.message === "foo");
|
||||
assert(e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when a property is not defined
|
||||
var obj = { '0' : 2, '2' : "foo", length : 3, copyWithin : Array.prototype.copyWithin };
|
||||
|
||||
obj.copyWithin(1);
|
||||
assert(obj[0] === 2);
|
||||
assert(obj[1] === 2);
|
||||
|
||||
function array_check(result_array, expected_array) {
|
||||
assert(result_array instanceof Array);
|
||||
assert(result_array.length === expected_array.length);
|
||||
for (var idx = 0; idx < expected_array.length; idx++) {
|
||||
assert(result_array[idx] === expected_array[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3];
|
||||
var value = array.copyWithin(0, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
array_check(value, []);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3];
|
||||
var value = array.copyWithin(1, {
|
||||
valueOf: function() {
|
||||
array.length = 6;
|
||||
}
|
||||
})
|
||||
array_check(value, [1, 1, 2, undefined, undefined, undefined]);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7];
|
||||
var value = array.copyWithin(4, 2, {
|
||||
valueOf: function() {
|
||||
array.length = 3;
|
||||
}
|
||||
})
|
||||
array_check(value, [1, 2, 3]);
|
||||
|
||||
// Reduce the buffer and extend the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
var value = array.copyWithin(7, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
array_check(value, [1, 2, 3, 4, 5, , , 1, 2, 3]);
|
||||
|
||||
// Copy with overlapping (backward copy)
|
||||
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
var value = array.copyWithin(0, 2, 8)
|
||||
array_check(value, [3, 4, 5, 6, 7, 8, 7, 8, 9, 10]);
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
// 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.
|
||||
|
||||
try {
|
||||
Array.prototype.entries.call (undefined);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
|
||||
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
|
||||
|
||||
var iterator = array.entries ();
|
||||
|
||||
var current_item = iterator.next ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
assert (current_item.value[0] === i);
|
||||
assert (current_item.value[1] === array[i]);
|
||||
assert (current_item.done === false);
|
||||
|
||||
current_item = iterator.next ();
|
||||
}
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
function foo_error () {
|
||||
throw new ReferenceError ("foo");
|
||||
}
|
||||
|
||||
array = [1, 2, 3, 4, 5, 6, 7];
|
||||
|
||||
['0', '3', '5'].forEach (function (e) {
|
||||
Object.defineProperty (array, e, { 'get' : foo_error });
|
||||
})
|
||||
|
||||
iterator = array.entries ();
|
||||
|
||||
var expected_values = [2, 3, 5, 7];
|
||||
var expected_indices = [1, 2, 4, 6];
|
||||
var expected_values_idx = 0;
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
try {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value[0] === expected_indices[expected_values_idx]);
|
||||
assert (current_item.value[1] === expected_values[expected_values_idx]);
|
||||
assert (current_item.done === false);
|
||||
expected_values_idx++;
|
||||
} catch (e) {
|
||||
assert (e instanceof ReferenceError);
|
||||
assert (e.message === "foo");
|
||||
}
|
||||
}
|
||||
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test empty array */
|
||||
array = [];
|
||||
|
||||
iterator = array.entries ();
|
||||
current_item = iterator.next ();
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test delete elements after the iterator has been constructed */
|
||||
|
||||
array = [0, 1, 2, 3, 4, 5];
|
||||
iterator = array.entries ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value[0] === i);
|
||||
assert (current_item.value[1] === array[i]);
|
||||
assert (current_item.done === false);
|
||||
array.pop();
|
||||
}
|
||||
|
||||
assert ([].entries ().toString () === "[object Array Iterator]");
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
// 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.
|
||||
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
function assertArrayEquals (array1, array2) {
|
||||
if (array1.length !== array2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < array1.length; i++) {
|
||||
if (array1[i] !== array2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
assert (1 === Array.prototype.fill.length);
|
||||
|
||||
assert (assertArrayEquals ([].fill (8), []));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (), [undefined, undefined, undefined, undefined, undefined]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8), [8, 8, 8, 8, 8]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, 1), [0, 8, 8, 8, 8]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, 10), [0, 0, 0, 0, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, -5), [8, 8, 8, 8, 8]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, 1, 4), [0, 8, 8, 8, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, 1, -1), [0, 8, 8, 8, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, 1, 42), [0, 8, 8, 8, 8]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, -3, 42), [0, 0, 8, 8, 8]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, -3, 4), [0, 0, 8, 8, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, -2, -1), [0, 0, 0, 8, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, -1, -3), [0, 0, 0, 0, 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (8, undefined, 4), [8, 8, 8, 8, 0]));
|
||||
assert (assertArrayEquals ([ , , , , 0].fill (8, 1, 3), [, 8, 8, , 0]));
|
||||
assert (assertArrayEquals ([0, 0, 0, 0, 0].fill (7.8), [7.8, 7.8, 7.8, 7.8, 7.8]));
|
||||
assert (assertArrayEquals (["foo", "bar", "baz"].fill (1), [1, 1, 1]));
|
||||
|
||||
|
||||
// If the range is empty, the array is not actually modified and
|
||||
// should not throw, even when applied to a frozen object.
|
||||
assert (assertArrayEquals (Object.freeze ([1, 2, 3]).fill (0, 0, 0), [1, 2, 3]));
|
||||
|
||||
// Test exceptions
|
||||
try {
|
||||
Object.freeze ([0]).fill ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
try {
|
||||
Array.prototype.fill.call (null)
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
try {
|
||||
Array.prototype.fill.call (undefined)
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
function TestFillObjectWithAccessors () {
|
||||
var kLength = 5;
|
||||
|
||||
var log = [];
|
||||
|
||||
var object = {
|
||||
length: kLength,
|
||||
get 1 () {
|
||||
log.push ("get 1");
|
||||
return this.foo;
|
||||
},
|
||||
|
||||
set 1 (val) {
|
||||
log.push ("set 1 " + val);
|
||||
this.foo = val;
|
||||
}
|
||||
};
|
||||
|
||||
Array.prototype.fill.call (object, 42);
|
||||
|
||||
assert (kLength === object.length);
|
||||
assert (assertArrayEquals (["set 1 42"], log));
|
||||
|
||||
for (var i = 0; i < kLength; ++i) {
|
||||
assert (42 === object[i]);
|
||||
}
|
||||
}
|
||||
TestFillObjectWithAccessors ();
|
||||
|
||||
function TestFillObjectWithMaxNumberLength () {
|
||||
var kMaxSafeInt = Math.pow (2, 32) - 1;
|
||||
var object = {};
|
||||
object.length = kMaxSafeInt;
|
||||
|
||||
Array.prototype.fill.call (object, 42, Math.pow (2, 32) - 4);
|
||||
|
||||
assert (kMaxSafeInt === object.length);
|
||||
assert (42 === object[kMaxSafeInt - 3]);
|
||||
assert (42 === object[kMaxSafeInt - 2]);
|
||||
assert (42 === object[kMaxSafeInt - 1]);
|
||||
}
|
||||
TestFillObjectWithMaxNumberLength ();
|
||||
|
||||
function TestFillObjectWithPrototypeAccessors () {
|
||||
var kLength = 5;
|
||||
var log = [];
|
||||
var proto = {
|
||||
get 1 () {
|
||||
log.push ("get 0");
|
||||
return this.foo;
|
||||
},
|
||||
|
||||
set 1 (val) {
|
||||
log.push ("set 1 " + val);
|
||||
this.foo = val;
|
||||
}
|
||||
};
|
||||
|
||||
var object = { 0:0, 2:2, length: kLength};
|
||||
Object.setPrototypeOf (object, proto);
|
||||
|
||||
Array.prototype.fill.call (object, "42");
|
||||
|
||||
assert (kLength === object.length);
|
||||
assert (assertArrayEquals (["set 1 42"], log));
|
||||
assert (object.hasOwnProperty (0) == true);
|
||||
assert (object.hasOwnProperty (1) == false);
|
||||
assert (object.hasOwnProperty (2) == true);
|
||||
assert (object.hasOwnProperty (3) == true);
|
||||
assert (object.hasOwnProperty (4) == true);
|
||||
|
||||
for (var i = 0; i < kLength; ++i) {
|
||||
assert ("42" === object[i]);
|
||||
}
|
||||
}
|
||||
TestFillObjectWithPrototypeAccessors ();
|
||||
|
||||
function TestFillSealedObject () {
|
||||
var object = { length: 42 };
|
||||
Object.seal (object);
|
||||
|
||||
try {
|
||||
Array.prototype.fill.call (object);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
TestFillSealedObject ();
|
||||
|
||||
function TestFillFrozenObject () {
|
||||
var object = { length: 42 };
|
||||
Object.freeze (object);
|
||||
|
||||
try {
|
||||
Array.prototype.fill.call (object);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
TestFillFrozenObject ();
|
||||
|
||||
function array_check(result_array, expected_array) {
|
||||
assert(result_array instanceof Array);
|
||||
assert(result_array.length === expected_array.length);
|
||||
for (var idx = 0; idx < expected_array.length; idx++) {
|
||||
assert(result_array[idx] === expected_array[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var value = array.fill(2, 0, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, []);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3];
|
||||
var value = array.fill(1, {
|
||||
valueOf: function() {
|
||||
array.length = 6;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, [1, 1, 1, undefined, undefined, undefined]);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7];
|
||||
var value = array.fill(4, {
|
||||
valueOf: function() {
|
||||
array.length = 3;
|
||||
}
|
||||
})
|
||||
|
||||
array_check(value, [4, 4, 4]);
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
// 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 arrow_is_available = false;
|
||||
|
||||
try {
|
||||
assert (eval ("(f => 5) ()") === 5);
|
||||
arrow_is_available = true;
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
var array1 = [5, 12, 0, 8, 130, 44];
|
||||
|
||||
function bigger_than_10 (element) {
|
||||
return element > 10;
|
||||
}
|
||||
|
||||
assert (array1.findIndex (bigger_than_10) === 1);
|
||||
|
||||
function less_than_0 (element) {
|
||||
if (element == 0) {
|
||||
throw new Error ("zero");
|
||||
}
|
||||
return element < 0;
|
||||
}
|
||||
|
||||
try {
|
||||
array1.findIndex (less_than_0);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof Error);
|
||||
assert (e.message === "zero");
|
||||
}
|
||||
|
||||
var inventory = [
|
||||
{name: 'apples', quantity: 2},
|
||||
{name: 'bananas', quantity: 0},
|
||||
{name: 'cherries', quantity: 5}
|
||||
];
|
||||
|
||||
function isCherries (fruit) {
|
||||
return fruit.name === 'cherries';
|
||||
}
|
||||
|
||||
assert (JSON.stringify (inventory.findIndex (isCherries)) === "2");
|
||||
|
||||
if (arrow_is_available) {
|
||||
assert (eval ("inventory.findIndex (fruit => fruit.name === 'bananas')") === 1);
|
||||
}
|
||||
|
||||
/* Test the callback function arguments */
|
||||
var src_array = [4, 6, 8, 12];
|
||||
var array_index = 0;
|
||||
|
||||
function isPrime (element, index, array) {
|
||||
assert (array_index++ === index);
|
||||
assert (array === src_array)
|
||||
assert (element === array[index]);
|
||||
|
||||
var start = 2;
|
||||
while (start <= Math.sqrt (element)) {
|
||||
if (element % start++ < 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return element > 1;
|
||||
}
|
||||
|
||||
assert (src_array.findIndex (isPrime) === -1);
|
||||
|
||||
src_array = [4, 5, 8, 12];
|
||||
array_index = 0;
|
||||
assert (src_array.findIndex (isPrime) === 1);
|
||||
|
||||
// Checking behavior when the given object is not %Array%
|
||||
try {
|
||||
Array.prototype.findIndex.call (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty (obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.findIndex = Array.prototype.findIndex;
|
||||
|
||||
try {
|
||||
obj.findIndex ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
var data = { 0: 1, 2: -3, 3: "string" }
|
||||
assert (Array.prototype.findIndex.call (data, function (e) { return e < 5; }) === -1);
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty (obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.findIndex = Array.prototype.findIndex;
|
||||
|
||||
try {
|
||||
obj.findIndex (function () { return undefined; });
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument is not a callback
|
||||
var array = [1, 2, 3];
|
||||
|
||||
try {
|
||||
array.findIndex (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument does not exist
|
||||
try {
|
||||
array.findIndex ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when there are more than 2 arguments
|
||||
assert (array.findIndex (function (e) { return e < 2 }, {}, 8, 4, 5, 6, 6) === 0);
|
||||
|
||||
function func (element) {
|
||||
return element > 8;
|
||||
}
|
||||
|
||||
/* ES v6.0 22.1.3.9.8.c
|
||||
Checking behavior when the first element deletes the second */
|
||||
function f() { delete arr[1]; };
|
||||
var arr = [0, 1, 2, 3];
|
||||
Object.defineProperty(arr, '0', { 'get' : f });
|
||||
Array.prototype.findIndex.call(arr, func);
|
||||
|
||||
/* ES v6.0 22.1.3.9.8
|
||||
Checking whether predicate is called also for empty elements */
|
||||
var count = 0;
|
||||
|
||||
[,,,].findIndex(function() { count++; return false; });
|
||||
assert (count == 3);
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
// 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 arrow_is_available = false;
|
||||
|
||||
try {
|
||||
assert (eval ("(f => 5) ()") === 5);
|
||||
arrow_is_available = true;
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
|
||||
var array1 = [5, 12, 0, 8, 130, 44];
|
||||
|
||||
function bigger_than_10 (element) {
|
||||
return element > 10;
|
||||
}
|
||||
|
||||
assert (array1.find (bigger_than_10) === 12);
|
||||
|
||||
function less_than_0 (element) {
|
||||
if (element == 0) {
|
||||
throw new Error ("zero");
|
||||
}
|
||||
return element < 0;
|
||||
}
|
||||
|
||||
try {
|
||||
array1.find (less_than_0);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof Error);
|
||||
assert (e.message === "zero");
|
||||
}
|
||||
|
||||
var inventory = [
|
||||
{name: 'apples', quantity: 2},
|
||||
{name: 'bananas', quantity: 0},
|
||||
{name: 'cherries', quantity: 5}
|
||||
];
|
||||
|
||||
function isCherries (fruit) {
|
||||
return fruit.name === 'cherries';
|
||||
}
|
||||
|
||||
assert (JSON.stringify (inventory.find (isCherries)) === '{"name":"cherries","quantity":5}');
|
||||
|
||||
if (arrow_is_available) {
|
||||
assert (eval ("inventory.find (fruit => fruit.name === 'bananas')") === inventory[1]);
|
||||
}
|
||||
|
||||
/* Test the callback function arguments */
|
||||
var src_array = [4, 6, 8, 12];
|
||||
var array_index = 0;
|
||||
|
||||
function isPrime (element, index, array) {
|
||||
assert (array_index++ === index);
|
||||
assert (array === src_array)
|
||||
assert (element === array[index]);
|
||||
|
||||
var start = 2;
|
||||
while (start <= Math.sqrt (element)) {
|
||||
if (element % start++ < 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return element > 1;
|
||||
}
|
||||
|
||||
assert (src_array.find (isPrime) === undefined);
|
||||
|
||||
src_array = [4, 5, 8, 12];
|
||||
array_index = 0;
|
||||
assert (src_array.find (isPrime) === 5);
|
||||
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {};
|
||||
Object.defineProperty (obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.find = Array.prototype.find;
|
||||
|
||||
try {
|
||||
obj.find ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
var data = { 0: 1, 2: -3, 3: "string" }
|
||||
assert (Array.prototype.find.call (data, function (e) { return e < 5; }) === undefined);
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {}
|
||||
obj.length = 1;
|
||||
Object.defineProperty (obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
obj.find = Array.prototype.find
|
||||
|
||||
try {
|
||||
obj.find (function () { return undefined; });
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument is not a callback
|
||||
var array = [1, 2, 3];
|
||||
|
||||
try {
|
||||
array.find (5);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the first argument does not exist
|
||||
try {
|
||||
array.find ();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Checking behavior when the there are more than 2 arguments
|
||||
assert (array.find (function (e) { return e < 2 }, {}, 8, 4, 5, 6, 6) === 1);
|
||||
|
||||
function func (element) {
|
||||
return element > 8;
|
||||
}
|
||||
|
||||
/* ES v6.0 22.1.3.8.8.c
|
||||
Checking behavior when the first element deletes the second */
|
||||
function f() { delete arr[1]; };
|
||||
var arr = [0, 1, 2, 3];
|
||||
Object.defineProperty(arr, '0', { 'get' : f });
|
||||
Array.prototype.find.call(arr, func);
|
||||
|
||||
/* ES v6.0 22.1.3.8.8
|
||||
Checking whether predicate is called also for empty elements */
|
||||
var count = 0;
|
||||
|
||||
[,,,].find(function() { count++; return false; });
|
||||
assert (count == 3);
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
// 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.
|
||||
|
||||
// helper function - simple implementation
|
||||
Array.prototype.equals = function (array) {
|
||||
if (this.length != array.length)
|
||||
return false;
|
||||
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
if (this[i] instanceof Array && array[i] instanceof Array) {
|
||||
if (!this[i].equals(array[i]))
|
||||
return false;
|
||||
}
|
||||
else if (this[i] != array[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
// various checks on flat prototype
|
||||
assert ([1, 4, 9].flat(2).equals([1, 4, 9]))
|
||||
assert ([[4,9]].flat(0).equals([[4,9]]))
|
||||
assert ([[4,9]].flat(1).equals([4,9]))
|
||||
assert ([1, 4, [9,3,[2,4,[3,4]]]].flat(5).equals([1, 4, 9, 3, 2, 4, 3, 4]));
|
||||
|
||||
var array1 = [1,3,4]
|
||||
var array2 = [array1,array1]
|
||||
assert(array2.flat(1).equals([1,3,4,1,3,4]))
|
||||
assert(array2.flat(0).equals([[1,3,4],[1,3,4]]))
|
||||
|
||||
var array3 = [array2]
|
||||
assert(array3.flat(0).equals([[[1,3,4],[1,3,4]]]))
|
||||
assert(array3.flat(1).equals([[1,3,4],[1,3,4]]))
|
||||
assert(array3.flat(2).equals([1,3,4,1,3,4]))
|
||||
|
||||
var array4 = []
|
||||
assert(array4.flat(10).equals([]))
|
||||
|
||||
var array5 = [null, array4]
|
||||
assert(array5.flat(10).equals([null]))
|
||||
|
||||
// various checks on flatMap Prototype
|
||||
assert([1, 2, 3, 4].flatMap(x => [x, x * 2]).equals([1,2,2,4,3,6,4,8]))
|
||||
assert([1, 2, 3, 4].flatMap(x => [x, x * x]).equals([1,1,2,4,3,9,4,16]))
|
||||
assert(array1.flatMap(x => [x, x * 2]).equals([1,2,3,6,4,8]))
|
||||
assert(array4.flatMap(x => [x, x * 2]).equals([]))
|
||||
|
||||
function check_flat (map, depth)
|
||||
{
|
||||
try {
|
||||
map.flat (depth)
|
||||
assert (false)
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
function check_flat_map (map, mapper)
|
||||
{
|
||||
try {
|
||||
map.flatMap (mapper)
|
||||
assert (false)
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
// invalid depth
|
||||
check_flat ([1,2], Symbol())
|
||||
|
||||
// constructor is null
|
||||
var a = new Array();
|
||||
a.constructor = null;
|
||||
check_flat (a,1)
|
||||
|
||||
// callback is not object
|
||||
check_flat_map ([1,2], null)
|
||||
|
||||
// constructor is null
|
||||
check_flat_map (a,x => [x, x * x])
|
||||
|
||||
// "0" get is a TypeError
|
||||
var array_2 = [1,2]
|
||||
Object.defineProperty (array_2, '0', { 'get' : function () { throw new TypeError (); } });
|
||||
check_flat (array_2, 1)
|
||||
check_flat_map (array_2,x => [x, x * x])
|
||||
|
||||
// "0" is not Array and throw error
|
||||
var revocable = Proxy.revocable ({}, {});
|
||||
var proxy = revocable.proxy;
|
||||
revocable.revoke();
|
||||
var array_3 = [proxy,2]
|
||||
check_flat (array_3, 1)
|
||||
|
||||
// second call of FlattenIntoArray return with a error
|
||||
var array_4_1 = [1,2]
|
||||
Object.defineProperty (array_4_1, '0', { 'get' : function () { throw new TypeError (); } });
|
||||
var array_4 = [array_4_1,1,2]
|
||||
check_flat (array_4, 1)
|
||||
|
||||
// unable to add new property
|
||||
var array_5 = [array_2,1,2]
|
||||
check_flat (array_2, 1)
|
||||
|
||||
var A = function(_length) {
|
||||
Object.defineProperty(this, "0", {
|
||||
writable: true,
|
||||
configurable: false,
|
||||
});
|
||||
};
|
||||
|
||||
var arr = [1];
|
||||
arr.constructor = {};
|
||||
arr.constructor[Symbol.species] = A;
|
||||
|
||||
check_flat_map (arr, A)
|
||||
|
||||
// element value is not found
|
||||
var array_6 = []
|
||||
array_6.length = 2
|
||||
array_6.flat()
|
||||
|
||||
// mapped function is error
|
||||
var array_7 = [1,2]
|
||||
var f = function () {
|
||||
throw new TypeError()
|
||||
}
|
||||
check_flat_map (array_7, f)
|
||||
|
||||
var obj = new Proxy ([], { get(t, p, r) {
|
||||
if (p === 'length') {
|
||||
throw new TypeError();
|
||||
}
|
||||
}})
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
// 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.
|
||||
|
||||
assert(Array.prototype.includes.length === 1);
|
||||
assert(Array.prototype.includes.name === "includes");
|
||||
|
||||
var num_arr = [1, 2, 3,,4, 5];
|
||||
var str_arr = ['foo', 'bar', 'baz', NaN, 'foo'];
|
||||
var obj = {};
|
||||
var obj_arr = [1, obj, 2];
|
||||
var empry_arr = [];
|
||||
|
||||
assert(num_arr.includes(3) === true);
|
||||
assert(num_arr.includes(3, 4) === false);
|
||||
assert(num_arr.includes(3, -5) === true);
|
||||
assert(num_arr.includes(undefined) === true);
|
||||
assert(num_arr.includes(3, Infinity) === false);
|
||||
assert(num_arr.includes(3, -0) === true);
|
||||
assert(str_arr.includes(NaN) === true);
|
||||
assert(str_arr.includes('foo', 4) === true);
|
||||
assert(str_arr.includes('f') === false);
|
||||
assert(obj_arr.includes(obj) === true);
|
||||
assert(obj_arr.includes({}) === false);
|
||||
assert(empry_arr.includes() === false);
|
||||
assert(empry_arr.includes(3) === false);
|
||||
assert([undefined].includes() === true);
|
||||
|
||||
Object.defineProperty(num_arr, "1", { get: function() {throw 42}});
|
||||
|
||||
try {
|
||||
num_arr.includes(4);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 42);
|
||||
}
|
||||
|
||||
var sym = Symbol('foo');
|
||||
|
||||
try {
|
||||
num_arr.includes(3, sym);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Remove the buffer
|
||||
var array = [1, 2, 3, 4, 5];
|
||||
var found = array.includes(4, {
|
||||
valueOf: function() {
|
||||
array.length = 0;
|
||||
}
|
||||
})
|
||||
|
||||
assert(found === false);
|
||||
|
||||
// Extend the buffer
|
||||
var array = [1, 2, 3];
|
||||
var found = array.includes(2, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
|
||||
assert(found === true);
|
||||
|
||||
// Reduce the buffer
|
||||
var array = [1, 2, 3, 4, 5, 6, 7];
|
||||
var found = array.includes(6, {
|
||||
valueOf: function() {
|
||||
array.length = 5;
|
||||
}
|
||||
})
|
||||
|
||||
assert(found === false);
|
||||
@@ -0,0 +1,86 @@
|
||||
// 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.
|
||||
|
||||
try {
|
||||
Array.prototype.keys.call (undefined);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
|
||||
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
|
||||
|
||||
var iterator = array.keys ();
|
||||
|
||||
var current_item = iterator.next ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
assert (current_item.value === i);
|
||||
assert (current_item.done === false);
|
||||
|
||||
current_item = iterator.next ();
|
||||
}
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
function foo_error () {
|
||||
throw new ReferenceError ("foo");
|
||||
}
|
||||
|
||||
array = [1, 2, 3, 4, 5, 6, 7];
|
||||
|
||||
['0', '3', '5'].forEach (function (e) {
|
||||
Object.defineProperty (array, e, { 'get' : foo_error });
|
||||
})
|
||||
|
||||
iterator = array.keys ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
try {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === i);
|
||||
assert (current_item.done === false);
|
||||
} catch (e) {
|
||||
assert (e instanceof ReferenceError);
|
||||
assert (e.message === "foo");
|
||||
}
|
||||
}
|
||||
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test empty array */
|
||||
array = [];
|
||||
|
||||
iterator = array.keys ();
|
||||
current_item = iterator.next ();
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test delete elements after the iterator has been constructed */
|
||||
|
||||
array = [0, 1, 2, 3, 4, 5];
|
||||
iterator = array.keys ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === i);
|
||||
assert (current_item.done === false);
|
||||
array.pop();
|
||||
}
|
||||
|
||||
assert ([].keys ().toString () === "[object Array Iterator]");
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// 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.
|
||||
|
||||
// Constructor creates longer array than expected.
|
||||
class LongArray extends Array {
|
||||
constructor(len) {
|
||||
super (42);
|
||||
this.fill ("foo");
|
||||
}
|
||||
}
|
||||
|
||||
var a = new LongArray (5);
|
||||
a.length = 5;
|
||||
var sliced = a.slice ();
|
||||
assert (sliced.length == 5);
|
||||
assert (JSON.stringify (sliced) == '["foo","foo","foo","foo","foo"]')
|
||||
|
||||
// Constructor creates shorter array than expected.
|
||||
class ShortArray extends Array {
|
||||
constructor(len) {
|
||||
super (2);
|
||||
this.fill ("bar");
|
||||
}
|
||||
}
|
||||
|
||||
var b = new ShortArray (8);
|
||||
b.length = 8;
|
||||
b.fill ("asd", 2);
|
||||
var sliced2 = b.slice ();
|
||||
assert (sliced2.length == 8);
|
||||
assert (JSON.stringify (sliced2) == '["bar","bar","asd","asd","asd","asd","asd","asd"]');
|
||||
|
||||
// Constructor creates array of the expected size.
|
||||
class ExactArray extends Array {
|
||||
constructor(len) {
|
||||
super (len);
|
||||
this.fill ("baz");
|
||||
}
|
||||
}
|
||||
|
||||
var c = new ExactArray (5);
|
||||
var sliced3 = c.slice();
|
||||
assert (sliced3.length == 5);
|
||||
assert (JSON.stringify (sliced3) == '["baz","baz","baz","baz","baz"]');
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 proxy = new Proxy({length: 5}, {
|
||||
getOwnPropertyDescriptor() { throw 42.5; }
|
||||
})
|
||||
|
||||
try {
|
||||
Array.prototype.sort.call(proxy);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 42.5);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// 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 arrayLike = {get 5() { throw "shouldn't throw"; }};
|
||||
arrayLike.length = 10;
|
||||
Array.prototype.unshift.call(arrayLike);
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
// 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.
|
||||
|
||||
try {
|
||||
Array.prototype.values.call (undefined);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
|
||||
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
|
||||
|
||||
var iterator = array.values ();
|
||||
|
||||
/* The initial value of the @@iterator property is the same function object
|
||||
as the initial value of the Array.prototype.values property. */
|
||||
var symbol_iterator = array[Symbol.iterator] ();
|
||||
|
||||
var current_item = iterator.next ();
|
||||
var symbol_current_item = symbol_iterator.next ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
assert (current_item.value === array[i]);
|
||||
assert (current_item.done === false);
|
||||
|
||||
assert (current_item.value === symbol_current_item.value);
|
||||
assert (current_item.done === symbol_current_item.done);
|
||||
|
||||
current_item = iterator.next ();
|
||||
symbol_current_item = symbol_iterator.next ();
|
||||
}
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
assert (current_item.value === symbol_current_item.value);
|
||||
assert (current_item.done === symbol_current_item.done);
|
||||
|
||||
function foo_error () {
|
||||
throw new ReferenceError ("foo");
|
||||
}
|
||||
|
||||
array = [1, 2, 3, 4, 5, 6, 7];
|
||||
|
||||
['0', '3', '5'].forEach (function (e) {
|
||||
Object.defineProperty (array, e, { 'get' : foo_error });
|
||||
})
|
||||
|
||||
iterator = array.values ();
|
||||
|
||||
var expected_values = [2, 3, 5, 7];
|
||||
var expected_values_idx = 0;
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
try {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === expected_values[expected_values_idx++]);
|
||||
assert (current_item.done === false);
|
||||
} catch (e) {
|
||||
assert (e instanceof ReferenceError);
|
||||
assert (e.message === "foo");
|
||||
}
|
||||
}
|
||||
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test empty array */
|
||||
array = [];
|
||||
|
||||
iterator = array.values ();
|
||||
current_item = iterator.next ();
|
||||
|
||||
assert (current_item.value === undefined);
|
||||
assert (current_item.done === true);
|
||||
|
||||
/* Test delete elements after the iterator has been constructed */
|
||||
|
||||
array = [0, 1, 2, 3, 4, 5];
|
||||
iterator = array.values ();
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
current_item = iterator.next ();
|
||||
assert (current_item.value === array[i]);
|
||||
assert (current_item.done === false);
|
||||
array.pop();
|
||||
}
|
||||
|
||||
assert ([].values ().toString () === "[object Array Iterator]");
|
||||
@@ -0,0 +1,184 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// Copyright 2015 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Test the ES2015 @@species feature
|
||||
|
||||
'use strict';
|
||||
|
||||
// Subclasses of Array construct themselves under map, etc
|
||||
|
||||
class MyArray extends Array { }
|
||||
|
||||
function assertEquals(a, b) {
|
||||
assert(a === b);
|
||||
}
|
||||
|
||||
assertEquals(MyArray, new MyArray().map(()=>{}).constructor);
|
||||
assertEquals(MyArray, new MyArray().filter(()=>{}).constructor);
|
||||
assertEquals(MyArray, new MyArray().slice().constructor);
|
||||
assertEquals(MyArray, new MyArray().splice().constructor);
|
||||
assertEquals(MyArray, new MyArray().concat([1]).constructor);
|
||||
assertEquals(1, new MyArray().concat([1])[0]);
|
||||
|
||||
// Subclasses can override @@species to return the another class
|
||||
|
||||
class MyOtherArray extends Array {
|
||||
static get [Symbol.species]() { return MyArray; }
|
||||
}
|
||||
|
||||
assertEquals(MyArray, new MyOtherArray().map(()=>{}).constructor);
|
||||
assertEquals(MyArray, new MyOtherArray().filter(()=>{}).constructor);
|
||||
assertEquals(MyArray, new MyOtherArray().slice().constructor);
|
||||
assertEquals(MyArray, new MyOtherArray().splice().constructor);
|
||||
assertEquals(MyArray, new MyOtherArray().concat().constructor);
|
||||
|
||||
// Array methods on non-arrays return arrays
|
||||
|
||||
class MyNonArray extends Array {
|
||||
static get [Symbol.species]() { return MyObject; }
|
||||
}
|
||||
|
||||
class MyObject { }
|
||||
|
||||
assertEquals(MyObject,
|
||||
Array.prototype.map.call(new MyNonArray(), ()=>{}).constructor);
|
||||
assertEquals(MyObject,
|
||||
Array.prototype.filter.call(new MyNonArray(), ()=>{}).constructor);
|
||||
assertEquals(MyObject,
|
||||
Array.prototype.slice.call(new MyNonArray()).constructor);
|
||||
assertEquals(MyObject,
|
||||
Array.prototype.splice.call(new MyNonArray()).constructor);
|
||||
assertEquals(MyObject,
|
||||
Array.prototype.concat.call(new MyNonArray()).constructor);
|
||||
|
||||
assertEquals(undefined,
|
||||
Array.prototype.map.call(new MyNonArray(), ()=>{}).length);
|
||||
assertEquals(undefined,
|
||||
Array.prototype.filter.call(new MyNonArray(), ()=>{}).length);
|
||||
// slice, splice, and concat actually do explicitly define the length.
|
||||
assertEquals(0, Array.prototype.slice.call(new MyNonArray()).length);
|
||||
assertEquals(0, Array.prototype.splice.call(new MyNonArray()).length);
|
||||
assertEquals(1, Array.prototype.concat.call(new MyNonArray(), ()=>{}).length);
|
||||
|
||||
// Defaults when constructor or @@species is missing or non-constructor
|
||||
|
||||
class MyDefaultArray extends Array {
|
||||
static get [Symbol.species]() { return undefined; }
|
||||
}
|
||||
assertEquals(Array, new MyDefaultArray().map(()=>{}).constructor);
|
||||
|
||||
class MyOtherDefaultArray extends Array { }
|
||||
assertEquals(MyOtherDefaultArray,
|
||||
new MyOtherDefaultArray().map(()=>{}).constructor);
|
||||
MyOtherDefaultArray.prototype.constructor = undefined;
|
||||
assertEquals(Array, new MyOtherDefaultArray().map(()=>{}).constructor);
|
||||
assertEquals(Array, new MyOtherDefaultArray().concat().constructor);
|
||||
|
||||
// Exceptions propagated when getting constructor @@species throws
|
||||
|
||||
class SpeciesError extends Error { }
|
||||
class ConstructorError extends Error { }
|
||||
class MyThrowingArray extends Array {
|
||||
static get [Symbol.species]() { throw new SpeciesError; }
|
||||
}
|
||||
|
||||
function assertThrows (a, b) {
|
||||
try {
|
||||
a();
|
||||
} catch (e) {
|
||||
assert(e instanceof b);
|
||||
}
|
||||
}
|
||||
|
||||
assertThrows(() => new MyThrowingArray().map(()=>{}), SpeciesError);
|
||||
Object.defineProperty(MyThrowingArray.prototype, 'constructor', {
|
||||
get() { throw new ConstructorError; }
|
||||
});
|
||||
assertThrows(() => new MyThrowingArray().map(()=>{}), ConstructorError);
|
||||
|
||||
// Previously unexpected errors from setting properties in arrays throw
|
||||
|
||||
class FrozenArray extends Array {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
Object.freeze(this);
|
||||
}
|
||||
}
|
||||
assertThrows(() => new FrozenArray([1]).map(()=>0), TypeError);
|
||||
assertThrows(() => new FrozenArray([1]).filter(()=>true), TypeError);
|
||||
assertThrows(() => new FrozenArray([1]).slice(0, 1), TypeError);
|
||||
assertThrows(() => new FrozenArray([1]).splice(0, 1), TypeError);
|
||||
assertThrows(() => new FrozenArray([]).concat([1]), TypeError);
|
||||
|
||||
// Verify call counts and constructor parameters
|
||||
|
||||
var count;
|
||||
var params;
|
||||
class MyObservedArray extends Array {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
params = args;
|
||||
}
|
||||
static get [Symbol.species]() {
|
||||
count++
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
function assertArrayEquals(value, expected, type) {
|
||||
assert(expected.length === value.length);
|
||||
for (var i=0; i<value.length; ++i) {
|
||||
assertEquals(expected[i], value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
count = 0;
|
||||
params = undefined;
|
||||
assertEquals(MyObservedArray,
|
||||
new MyObservedArray().map(()=>{}).constructor);
|
||||
assertEquals(1, count);
|
||||
assertArrayEquals([0], params);
|
||||
|
||||
count = 0;
|
||||
params = undefined;
|
||||
assertEquals(MyObservedArray,
|
||||
new MyObservedArray().filter(()=>{}).constructor);
|
||||
assertEquals(1, count);
|
||||
assertArrayEquals([0], params);
|
||||
|
||||
count = 0;
|
||||
params = undefined;
|
||||
assertEquals(MyObservedArray,
|
||||
new MyObservedArray().concat().constructor);
|
||||
assertEquals(1, count);
|
||||
assertArrayEquals([0], params);
|
||||
|
||||
count = 0;
|
||||
params = undefined;
|
||||
assertEquals(MyObservedArray,
|
||||
new MyObservedArray().slice().constructor);
|
||||
assertEquals(1, count);
|
||||
assertArrayEquals([0], params);
|
||||
|
||||
count = 0;
|
||||
params = undefined;
|
||||
assertEquals(MyObservedArray,
|
||||
new MyObservedArray().splice().constructor);
|
||||
assertEquals(1, count);
|
||||
assertArrayEquals([0], params);
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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 assertArrayEqual (actual, expected) {
|
||||
assert (actual.length === expected.length);
|
||||
|
||||
for (var i = 0; i < actual.length; i++) {
|
||||
assert (actual[i] === expected[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function checkSyntax (str) {
|
||||
try {
|
||||
eval (str);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
}
|
||||
|
||||
function mustThrow (str) {
|
||||
try {
|
||||
eval (str);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
}
|
||||
|
||||
checkSyntax ("{...a}");
|
||||
checkSyntax ("...a");
|
||||
checkSyntax ("[...]");
|
||||
checkSyntax ("[...(...)]");
|
||||
checkSyntax ("[......]");
|
||||
|
||||
mustThrow ("[...5]");
|
||||
mustThrow ("[...5, 'foo', 'bar']");
|
||||
mustThrow ("[...{}]");
|
||||
mustThrow ("[...{ get [Symbol.iterator] () { throw new TypeError } }]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () {} }, 'foo']");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return {} } }]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next: 5 } } }]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next: 5 } } }], 'foo'");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { get next() { throw new TypeError } } } }]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next () { } } } }]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next () { } } } }, 'foo']");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next () { return { get value () { throw new TypeError } } } } } } ]");
|
||||
mustThrow ("[...{ [Symbol.iterator] () { return { next () { return { get done () { throw new TypeError } } } } } } ]");
|
||||
|
||||
var arr1 = [0, 1, 2];
|
||||
var arr2 = [3, 4, 5];
|
||||
var arr3 = [{}, {}, {}];
|
||||
var expected = [0, 1, 2, 3 ,4, 5];
|
||||
|
||||
assertArrayEqual ([...arr1, ...arr2], [0, 1, 2, 3 ,4, 5]);
|
||||
assertArrayEqual ([...arr2, ...arr1], [3 ,4, 5, 0, 1, 2]);
|
||||
assertArrayEqual ([...arr1, 9, 9, 9, ...arr2], [0, 1, 2, 9, 9, 9, 3 ,4, 5]);
|
||||
assertArrayEqual ([...arr1, ...[...arr2]], [0, 1, 2, 3 ,4, 5]);
|
||||
assertArrayEqual (["0" , "1", ...arr1, ...[...arr2]], ["0", "1", 0, 1, 2, 3 ,4, 5]);
|
||||
assertArrayEqual ([...arr3], arr3);
|
||||
assertArrayEqual ([..."foobar"], ["f", "o", "o", "b", "a" ,"r"]);
|
||||
assertArrayEqual ([...(new Set([1, "foo", arr3]))], [1, "foo", arr3]);
|
||||
|
||||
var holyArray = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...arr1];
|
||||
assert (holyArray.length === 83);
|
||||
assert (holyArray[82] === 2);
|
||||
assert (holyArray[81] === 1);
|
||||
assert (holyArray[80] === 0);
|
||||
@@ -0,0 +1,37 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
assert(ArrayBuffer.isView() === false);
|
||||
assert(ArrayBuffer.isView([]) === false);
|
||||
assert(ArrayBuffer.isView({}) === false);
|
||||
assert(ArrayBuffer.isView(null) === false);
|
||||
assert(ArrayBuffer.isView(undefined) === false);
|
||||
assert(ArrayBuffer.isView(new ArrayBuffer(10)) === false);
|
||||
|
||||
assert(ArrayBuffer.isView(new Int8Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Uint8Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Uint8ClampedArray()) === true);
|
||||
assert(ArrayBuffer.isView(new Int16Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Uint16Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Int32Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Uint32Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Float32Array()) === true);
|
||||
assert(ArrayBuffer.isView(new Float64Array()) === true);
|
||||
|
||||
assert(ArrayBuffer.isView(new Int8Array(10).subarray(0, 3)) === true);
|
||||
|
||||
var buffer = new ArrayBuffer(2);
|
||||
var dv = new DataView(buffer);
|
||||
assert(ArrayBuffer.isView(dv) === true);
|
||||
@@ -0,0 +1,46 @@
|
||||
/* 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 y = 0
|
||||
var prot = Object.getPrototypeOf(/ /)
|
||||
|
||||
prot.setY = function (v) { y = v }
|
||||
|
||||
assert(y === 0)
|
||||
// Since arrow function is an assignment expression, this affects certain constructs
|
||||
var f = x => {}
|
||||
/ /.setY(5)
|
||||
assert(y === 5)
|
||||
|
||||
var s
|
||||
// This is not a function call
|
||||
assert(eval("s = x => { return 1 }\n(3)") === 3)
|
||||
assert(typeof s === "function")
|
||||
|
||||
// This is a function call
|
||||
assert(eval("s = function () { return 1 }\n(3)") === 1)
|
||||
assert(s === 1)
|
||||
|
||||
var f = 5 ? x => 1 : x => 2
|
||||
assert(f() === 1)
|
||||
|
||||
var f = [x => 2][0]
|
||||
assert(f() === 2)
|
||||
|
||||
var f = 123; f += x => y
|
||||
assert(typeof f === "string")
|
||||
|
||||
// Comma operator
|
||||
assert(eval("x => {}, 5") === 5)
|
||||
@@ -0,0 +1,41 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
let a = 4
|
||||
|
||||
var f = () => {
|
||||
eval("var a = 5")
|
||||
assert(a === 5)
|
||||
}
|
||||
f()
|
||||
|
||||
assert(a === 4)
|
||||
|
||||
function g() {
|
||||
eval("var b = 6")
|
||||
|
||||
assert(b === 6)
|
||||
|
||||
var h = () => delete b
|
||||
h()
|
||||
|
||||
try {
|
||||
b
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
}
|
||||
g()
|
||||
@@ -0,0 +1,194 @@
|
||||
/* 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 must_throw (str)
|
||||
{
|
||||
try
|
||||
{
|
||||
eval ("switch (1) { default: " + str + "}");
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
eval (str);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
function must_throw_strict (str)
|
||||
{
|
||||
try
|
||||
{
|
||||
eval ("'use strict'; switch (1) { default: " + str + "}");
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
eval ("'use strict'; " + str);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
switch (1)
|
||||
{
|
||||
default:
|
||||
|
||||
var func = x => { return x + 3 }
|
||||
assert (func(5) == 8);
|
||||
|
||||
a => 5 /* no semicolon after */
|
||||
|
||||
assert (((x =>
|
||||
x + 1))(4) == 5)
|
||||
|
||||
assert ((a => a += 3, b => b -= 3)(4) == 1);
|
||||
|
||||
func = true ? x=>x+2:y=>y-2
|
||||
assert (func(10) == 12);
|
||||
|
||||
func = arguments =>
|
||||
{ return arguments + 4; }
|
||||
assert (func(2) == 6);
|
||||
|
||||
func = (
|
||||
) => { return typeof
|
||||
arguments
|
||||
}
|
||||
assert (func() === "undefined");
|
||||
|
||||
if (a => 0)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
assert (false);
|
||||
}
|
||||
|
||||
assert ((
|
||||
(
|
||||
static
|
||||
,
|
||||
package
|
||||
) => static + package
|
||||
) (2, 12) == 14);
|
||||
|
||||
var global_var = 7;
|
||||
|
||||
assert ((
|
||||
(
|
||||
static
|
||||
,
|
||||
package
|
||||
) => { global_var = 5; return static + package }
|
||||
)(4, 5) == 9);
|
||||
|
||||
assert (global_var == 5);
|
||||
|
||||
func = (x , y) => {}
|
||||
assert (func() === undefined)
|
||||
|
||||
assert ((x => y => z => 6)()()() == 6)
|
||||
|
||||
func = x => x - 6
|
||||
var func2 = y => func(y)
|
||||
assert (func2 (17) == 11)
|
||||
|
||||
func = (m) => m++
|
||||
assert (func (4) == 4)
|
||||
|
||||
func = () =>
|
||||
((([0,0,0])))
|
||||
assert (func ().length == 3);
|
||||
|
||||
func = (a = 5, b = 7 * 2) => a + b;
|
||||
assert (func() == 19);
|
||||
assert (func(1) == 15);
|
||||
|
||||
func = (a = Math.cos(0)) => a;
|
||||
assert (func() == 1);
|
||||
}
|
||||
|
||||
must_throw ("var x => x;");
|
||||
must_throw ("(()) => 0");
|
||||
must_throw ("((x)) => 0");
|
||||
must_throw ("(((x))) => 0");
|
||||
must_throw ("(x==6) => 0");
|
||||
must_throw ("(x y) => 0");
|
||||
must_throw ("x\n => 0");
|
||||
must_throw ("this => 0");
|
||||
must_throw ("(true) => 0");
|
||||
must_throw ("()\n=>5");
|
||||
must_throw ("3 + x => 3");
|
||||
must_throw ("3 || x => 3");
|
||||
must_throw ("a = 3 || (x,y) => 3");
|
||||
must_throw ("x => {} (4)");
|
||||
must_throw ("!x => 4");
|
||||
must_throw ("x => {} = 1");
|
||||
must_throw ("x => {} a = 1");
|
||||
must_throw ("x => {} ? 1 : 0");
|
||||
must_throw ("(x,x,x) => 0");
|
||||
must_throw ("(x,x,x) => { }");
|
||||
must_throw_strict ("(package) => 0");
|
||||
must_throw_strict ("(package) => { return 5 }");
|
||||
must_throw_strict ("(x,x,x) => 0");
|
||||
must_throw_strict ("(x,x,x) => { }");
|
||||
|
||||
var f = (a) => 1;
|
||||
assert(f() === 1);
|
||||
|
||||
var f = (a => 2);
|
||||
assert(f() === 2);
|
||||
|
||||
var f = ((((a => ((3))))));
|
||||
assert(f() === 3);
|
||||
|
||||
var f = (((a) => 4));
|
||||
assert(f() === 4);
|
||||
|
||||
var f = (a,b) => 5;
|
||||
assert(f() === 5);
|
||||
|
||||
var f = (((a,b) => 6));
|
||||
assert(f() === 6);
|
||||
|
||||
var f = ((a,b) => x => (a) => 7);
|
||||
assert(f()()() === 7);
|
||||
|
||||
var f = (((a=1,b=2) => ((x => (((a) => 8))))));
|
||||
assert(f()()() === 8);
|
||||
|
||||
var f = () => {};
|
||||
|
||||
assert(f.hasOwnProperty('caller') === false);
|
||||
assert(f.hasOwnProperty('arguments') === false);
|
||||
|
||||
must_throw("var f = () => {}; f.caller")
|
||||
must_throw("var f = () => {}; f.arguments")
|
||||
must_throw("var f = () => {}; f.caller = 1")
|
||||
must_throw("var f = () => {}; f.arguments = 2")
|
||||
@@ -0,0 +1,40 @@
|
||||
/* 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 o = {
|
||||
x: 13,
|
||||
|
||||
f: function()
|
||||
{
|
||||
return () => this.x + 1
|
||||
},
|
||||
|
||||
g: function()
|
||||
{
|
||||
return function() {
|
||||
return this.x + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(o.f().call(o) === 14);
|
||||
assert(o.g().call(o) === 14);
|
||||
|
||||
assert(o.f()() === 14);
|
||||
|
||||
var o2 = { x:4, f:o.f(), g:o.g() }
|
||||
|
||||
assert(o2.f() === 14);
|
||||
assert(o2.g() === 5);
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// 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 arrayEquals(result, expected) {
|
||||
assert(result.length === expected.length);
|
||||
|
||||
for (var idx = 0; idx < result.length; idx++) {
|
||||
assert(result[idx] === expected[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
var bigint64_array = new BigInt64Array([1n, 2n, 3n, -4n, 5n]);
|
||||
|
||||
function positive(element, index, array) {
|
||||
return element > 0n;
|
||||
}
|
||||
|
||||
var bigint64_filter = bigint64_array.filter(positive);
|
||||
arrayEquals(bigint64_filter, [1n, 2n, 3n, 5n]);
|
||||
|
||||
var biguint64_array = new BigUint64Array([1n, 2n, 3n, -4n, 5n]);
|
||||
var biguint64_filter = biguint64_array.filter(positive);
|
||||
assert(biguint64_filter.length === 5);
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// 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 bigint64_array = new BigInt64Array([1n, 2n, 3n, -4n, 5n]);
|
||||
|
||||
function sum(prev, current) {
|
||||
return prev + current;
|
||||
}
|
||||
|
||||
var bigint64_reduce_result = bigint64_array.reduce(sum, 7n);
|
||||
assert(bigint64_reduce_result === 14n);
|
||||
|
||||
var biguint64_array = new BigUint64Array([1n, 2n, 3n, -4n, 5n]);
|
||||
var biguint64_reduce_result = biguint64_array.reduce(sum, 7n);
|
||||
assert(biguint64_reduce_result === 18446744073709551630n);
|
||||
@@ -0,0 +1,247 @@
|
||||
/* 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 check_result(bigint, expected)
|
||||
{
|
||||
assert(bigint.toString() === expected)
|
||||
}
|
||||
|
||||
function check_result16(bigint, expected)
|
||||
{
|
||||
assert(bigint.toString(16) === expected)
|
||||
}
|
||||
|
||||
function check_syntax_error(code)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
assert(typeof BigInt("0") == "bigint")
|
||||
|
||||
// Test BigInt string parsing and toString
|
||||
|
||||
check_syntax_error("BigInt('-0x5')");
|
||||
check_syntax_error("BigInt('-')");
|
||||
check_syntax_error("BigInt('00x5')");
|
||||
check_syntax_error("BigInt('11a')");
|
||||
check_syntax_error("BigInt('0b2')");
|
||||
check_syntax_error("BigInt('1n')");
|
||||
|
||||
check_result(BigInt("0"), "0")
|
||||
check_result(BigInt("-0"), "0")
|
||||
check_result(BigInt("100000000000000000000000000000000000000"), "100000000000000000000000000000000000000")
|
||||
check_result(BigInt("-1234567890123456789012345678901234567890"), "-1234567890123456789012345678901234567890")
|
||||
check_result(BigInt("+1"), "1")
|
||||
check_result(BigInt("+000000000000000000001"), "1")
|
||||
check_result(BigInt("-000000000000000000000"), "0")
|
||||
check_result(BigInt("0x00abcdefABCDEF0123456789000000000000000"), "239460437713606077082343926293727858623774720")
|
||||
check_result(BigInt("0b00100000000000010000000000010000000000010"), "274911469570")
|
||||
|
||||
assert(BigInt("100000000000000000000000000000000000000").toString(22) === "2ci67fiek1bkhec5fig7aiii9hf8c")
|
||||
check_result16(BigInt("239460437713606077082343926293727858623774720"), "abcdefabcdef0123456789000000000000000")
|
||||
|
||||
// Test negate
|
||||
|
||||
check_result(-BigInt("0"), "0")
|
||||
check_result(-BigInt("100"), "-100")
|
||||
check_result(-BigInt("-100"), "100")
|
||||
check_result(-BigInt("100000000000000000000000000000000000000000000"), "-100000000000000000000000000000000000000000000")
|
||||
check_result(-BigInt("-100000000000000000000000000000000000000000000"), "100000000000000000000000000000000000000000000")
|
||||
|
||||
// Test addition
|
||||
|
||||
check_result(BigInt("0") + BigInt("0"), "0")
|
||||
check_result(BigInt("1") + BigInt("1"), "2")
|
||||
check_result(BigInt("0") + BigInt("100"), "100")
|
||||
check_result(BigInt("0") + BigInt("-100"), "-100")
|
||||
check_result(BigInt("100") + BigInt("0"), "100")
|
||||
check_result(BigInt("-100") + BigInt("0"), "-100")
|
||||
|
||||
check_result(BigInt("100000000000000000000000000000000000000") + BigInt("100000000000000000000000000000000000000"),
|
||||
"200000000000000000000000000000000000000");
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") + BigInt("-100000000000000000000000000000000000000"),
|
||||
"-200000000000000000000000000000000000000");
|
||||
check_result(BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3") + BigInt("0xd"),
|
||||
"115792089237316195423570985008687907853269984665640564039457584007913129639936");
|
||||
check_result(BigInt("0xd") + BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3"),
|
||||
"115792089237316195423570985008687907853269984665640564039457584007913129639936");
|
||||
|
||||
check_result(BigInt("100000000000000000000000000000000000000") + BigInt("-100000000000000000000000000000000000000"), "0")
|
||||
check_result(BigInt("100000000000000000000000000000000000001") + BigInt("-100000000000000000000000000000000000000"), "1")
|
||||
check_result(BigInt("100000000000000000000000000000000000000") + BigInt("-100000000000000000000000000000000000001"), "-1")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") + BigInt("100000000000000000000000000000000000000"), "0")
|
||||
check_result(BigInt("-100000000000000000000000000000000000001") + BigInt("100000000000000000000000000000000000000"), "-1")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") + BigInt("100000000000000000000000000000000000001"), "1")
|
||||
|
||||
// Test substraction
|
||||
|
||||
check_result(BigInt("0") - BigInt("0"), "0")
|
||||
check_result(BigInt("2") - BigInt("1"), "1")
|
||||
check_result(BigInt("0") - BigInt("100"), "-100")
|
||||
check_result(BigInt("0") - BigInt("-100"), "100")
|
||||
check_result(BigInt("100") - BigInt("0"), "100")
|
||||
check_result(BigInt("-100") - BigInt("0"), "-100")
|
||||
|
||||
check_result(BigInt("100000000000000000000000000000000000000") - BigInt("-100000000000000000000000000000000000000"),
|
||||
"200000000000000000000000000000000000000");
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") - BigInt("100000000000000000000000000000000000000"),
|
||||
"-200000000000000000000000000000000000000");
|
||||
check_result(BigInt("100000000000000000000000000000000000000") - BigInt("-1"),
|
||||
"100000000000000000000000000000000000001");
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") - BigInt("1"),
|
||||
"-100000000000000000000000000000000000001");
|
||||
check_result(BigInt("1") - BigInt("-100000000000000000000000000000000000000"),
|
||||
"100000000000000000000000000000000000001");
|
||||
check_result(BigInt("-1") - BigInt("100000000000000000000000000000000000000"),
|
||||
"-100000000000000000000000000000000000001");
|
||||
|
||||
check_result(BigInt("100000000000000000000000000000000000000") - BigInt("100000000000000000000000000000000000000"), "0")
|
||||
check_result(BigInt("100000000000000000000000000000000000001") - BigInt("100000000000000000000000000000000000000"), "1")
|
||||
check_result(BigInt("100000000000000000000000000000000000000") - BigInt("100000000000000000000000000000000000001"), "-1")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") - BigInt("-100000000000000000000000000000000000000"), "0")
|
||||
check_result(BigInt("-100000000000000000000000000000000000001") - BigInt("-100000000000000000000000000000000000000"), "-1")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") - BigInt("-100000000000000000000000000000000000001"), "1")
|
||||
|
||||
// Test multiplication
|
||||
|
||||
check_result(BigInt("0") * BigInt("0"), "0")
|
||||
check_result(BigInt("1000") * BigInt("0"), "0")
|
||||
check_result(BigInt("0") * BigInt("1000"), "0")
|
||||
check_result(BigInt("1") * BigInt("100000000000000000000000000000000000000"), "100000000000000000000000000000000000000")
|
||||
check_result(BigInt("1") * BigInt("-100000000000000000000000000000000000000"), "-100000000000000000000000000000000000000")
|
||||
check_result(BigInt("-1") * BigInt("100000000000000000000000000000000000000"), "-100000000000000000000000000000000000000")
|
||||
check_result(BigInt("-1") * BigInt("-100000000000000000000000000000000000000"), "100000000000000000000000000000000000000")
|
||||
check_result(BigInt("100000000000000000000000000000000000000") * BigInt("1"), "100000000000000000000000000000000000000")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") * BigInt("1"), "-100000000000000000000000000000000000000")
|
||||
check_result(BigInt("100000000000000000000000000000000000000") * BigInt("-1"), "-100000000000000000000000000000000000000")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") * BigInt("-1"), "100000000000000000000000000000000000000")
|
||||
|
||||
check_result(BigInt("100000000000000000000000000000000000000") * BigInt("100000000000000000000000000000000000000"),
|
||||
"10000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
check_result(BigInt("100000000000000000000000000000000000000") * BigInt("-100000000000000000000000000000000000000"),
|
||||
"-10000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") * BigInt("100000000000000000000000000000000000000"),
|
||||
"-10000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
check_result(BigInt("-100000000000000000000000000000000000000") * BigInt("-100000000000000000000000000000000000000"),
|
||||
"10000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
|
||||
// Test divide
|
||||
|
||||
try {
|
||||
BigInt("32") / BigInt("0")
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
|
||||
try {
|
||||
BigInt("32") % BigInt("0")
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
|
||||
check_result(BigInt("0") / BigInt("1234"), "0")
|
||||
check_result(BigInt("0") % BigInt("1234"), "0")
|
||||
|
||||
check_result(BigInt("100") / BigInt("70"), "1")
|
||||
check_result(BigInt("100") % BigInt("70"), "30")
|
||||
check_result(BigInt("-100") / BigInt("70"), "-1")
|
||||
check_result(BigInt("-100") % BigInt("70"), "-30")
|
||||
check_result(BigInt("100") / BigInt("-70"), "-1")
|
||||
check_result(BigInt("100") % BigInt("-70"), "30")
|
||||
check_result(BigInt("-100") / BigInt("-70"), "1")
|
||||
check_result(BigInt("-100") % BigInt("-70"), "-30")
|
||||
|
||||
check_result(BigInt("100") / BigInt("100"), "1")
|
||||
check_result(BigInt("100") % BigInt("100"), "0")
|
||||
check_result(BigInt("-100") / BigInt("100"), "-1")
|
||||
check_result(BigInt("-100") % BigInt("100"), "0")
|
||||
check_result(BigInt("100") / BigInt("-100"), "-1")
|
||||
check_result(BigInt("100") % BigInt("-100"), "0")
|
||||
check_result(BigInt("-100") / BigInt("-100"), "1")
|
||||
check_result(BigInt("-100") % BigInt("-100"), "0")
|
||||
|
||||
/* Division by small value. */
|
||||
check_result(BigInt("100000000000000000000") / BigInt("1000000"), "100000000000000")
|
||||
check_result(BigInt("100000000000000000000") % BigInt("1000000"), "0")
|
||||
check_result(BigInt("12345678901234567890") / BigInt("1000000"), "12345678901234")
|
||||
check_result(BigInt("12345678901234567890") % BigInt("1000000"), "567890")
|
||||
|
||||
/* Division by large value. */
|
||||
check_result(BigInt("100000000000000000000") / BigInt("100000000000000000"), "1000")
|
||||
check_result(BigInt("100000000000000000000") % BigInt("100000000000000000"), "0")
|
||||
check_result(BigInt("12345678901234567890123456789012345678901234567890123456789012345678901234567890") / BigInt("10000000000000000000000000000000000000"),
|
||||
"1234567890123456789012345678901234567890123")
|
||||
check_result(BigInt("12345678901234567890123456789012345678901234567890123456789012345678901234567890") % BigInt("10000000000000000000000000000000000000"),
|
||||
"4567890123456789012345678901234567890")
|
||||
check_result16(BigInt("0xffffffffffffffffffffffff") / BigInt("0x100000000"), "ffffffffffffffff")
|
||||
check_result16(BigInt("0xffffffffffffffffffffffff") % BigInt("0x100000000"), "ffffffff")
|
||||
|
||||
/* Triggers a corner case. */
|
||||
check_result(BigInt("170141183420855150493001878992821682176") / BigInt("39614081266355540842216685573"), "4294967293")
|
||||
check_result(BigInt("170141183420855150493001878992821682176") % BigInt("39614081266355540842216685573"), "39614081266355540837921718287")
|
||||
|
||||
// Test shift
|
||||
|
||||
check_result(BigInt("0") << BigInt("10000000"), "0")
|
||||
check_result(BigInt("0") >> BigInt("10000000"), "0")
|
||||
check_result(BigInt("10000000") << BigInt("0"), "10000000")
|
||||
check_result(BigInt("10000000") >> BigInt("0"), "10000000")
|
||||
|
||||
check_result(BigInt("4096") << BigInt("2"), "16384")
|
||||
check_result(BigInt("4096") << BigInt("-2"), "1024")
|
||||
check_result(BigInt("4096") >> BigInt("2"), "1024")
|
||||
check_result(BigInt("4096") >> BigInt("-2"), "16384")
|
||||
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("1"), "11fdebf9fffdebf9fffdebf9fffdebf9fe")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("19"), "47f7afe7fff7afe7fff7afe7fff7afe7f80000")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("31"), "47f7afe7fff7afe7fff7afe7fff7afe7f80000000")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("32"), "8fef5fcfffef5fcfffef5fcfffef5fcff00000000")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("51"), "47f7afe7fff7afe7fff7afe7fff7afe7f8000000000000")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("63"), "47f7afe7fff7afe7fff7afe7fff7afe7f8000000000000000")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("64"), "8fef5fcfffef5fcfffef5fcfffef5fcff0000000000000000")
|
||||
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("1"), "47f7afe7fff7afe7fff7afe7fff7afe7f")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("19"), "11fdebf9fffdebf9fffdebf9fffde")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("31"), "11fdebf9fffdebf9fffdebf9ff")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("32"), "8fef5fcfffef5fcfffef5fcff")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("51"), "11fdebf9fffdebf9fffde")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("63"), "11fdebf9fffdebf9ff")
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("64"), "8fef5fcfffef5fcff")
|
||||
|
||||
check_result16(-BigInt("0xff") >> BigInt("8"), "-1")
|
||||
check_result16(-BigInt("0xff") >> BigInt("1000"), "-1")
|
||||
check_result16(-BigInt("0xff") >> BigInt("7"), "-2")
|
||||
check_result16(-BigInt("0xff00000000") >> BigInt("32"), "-ff")
|
||||
check_result16(-BigInt("0xff80000000") >> BigInt("32"), "-100")
|
||||
check_result16(-BigInt("0xff00000000000000000000000000000000") >> BigInt("128"), "-ff")
|
||||
check_result16(-BigInt("0xff80000000000000000000000000000000") >> BigInt("128"), "-100")
|
||||
check_result16(-BigInt("0xfe00000000000000000000000000000000") >> BigInt("129"), "-7f")
|
||||
check_result16(-BigInt("0xff00000000000000000000000000000000") >> BigInt("129"), "-80")
|
||||
|
||||
check_result16(BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") >> BigInt("10000000000000000000000000"), "0")
|
||||
|
||||
try {
|
||||
BigInt("0x8fef5fcfffef5fcfffef5fcfffef5fcff") << BigInt("10000000000000000000000000");
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
try {
|
||||
new BigInt("1")
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
|
||||
function check_type_error (code)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
}
|
||||
|
||||
check_type_error("+BigInt('0')")
|
||||
|
||||
check_type_error("BigInt('1') + 1")
|
||||
check_type_error("BigInt('2') - 2")
|
||||
check_type_error("BigInt('3') * 3")
|
||||
check_type_error("BigInt('4') / 4")
|
||||
check_type_error("BigInt('5') % 5")
|
||||
check_type_error("BigInt('6') ** 6")
|
||||
|
||||
check_type_error("1 + BigInt('1')")
|
||||
check_type_error("2 - BigInt('2')")
|
||||
check_type_error("3 * BigInt('3')")
|
||||
check_type_error("4 / BigInt('4')")
|
||||
check_type_error("5 % BigInt('5')")
|
||||
check_type_error("6 ** BigInt('6')")
|
||||
|
||||
check_type_error("BigInt('1') & 1")
|
||||
check_type_error("BigInt('2') | 2")
|
||||
check_type_error("BigInt('3') ^ 3")
|
||||
check_type_error("BigInt('4') << 4")
|
||||
check_type_error("BigInt('5') >> 5")
|
||||
check_type_error("BigInt('6') >>> 6")
|
||||
|
||||
check_type_error("1 & BigInt('1')")
|
||||
check_type_error("2 | BigInt('2')")
|
||||
check_type_error("3 ^ BigInt('3')")
|
||||
check_type_error("4 << BigInt('4')")
|
||||
check_type_error("5 >> BigInt('5')")
|
||||
check_type_error("6 >>> BigInt('6')")
|
||||
@@ -0,0 +1,56 @@
|
||||
/* 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 check_result(bigint, expected)
|
||||
{
|
||||
assert(bigint.toString() === expected)
|
||||
}
|
||||
|
||||
function check_error (code, error_type)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof error_type)
|
||||
}
|
||||
}
|
||||
|
||||
check_error("BigInt(undefined)", TypeError)
|
||||
check_error("BigInt(null)", TypeError)
|
||||
check_error("BigInt(Symbol())", TypeError)
|
||||
|
||||
check_error("BigInt(0.25)", RangeError)
|
||||
check_error("BigInt(-0.25)", RangeError)
|
||||
check_error("BigInt(-10000000.25)", RangeError)
|
||||
check_error("BigInt(4503599627370495.5)", RangeError)
|
||||
check_error("BigInt(NaN)", RangeError)
|
||||
check_error("BigInt(Infinity)", RangeError)
|
||||
|
||||
check_result(BigInt(true), "1")
|
||||
check_result(BigInt(false), "0")
|
||||
check_result(BigInt({ valueOf() { return "0x100" } }), "256")
|
||||
|
||||
check_result(BigInt(0), "0")
|
||||
check_result(BigInt(-0), "0")
|
||||
check_result(BigInt(8192), "8192")
|
||||
check_result(BigInt(-0xffffffffff), "-1099511627775")
|
||||
check_result(BigInt(0x1fffffffffffff), "9007199254740991")
|
||||
check_result(BigInt(-4503599627370496), "-4503599627370496")
|
||||
check_result(BigInt(4503599627370496.5), "4503599627370496")
|
||||
check_result(BigInt(9007199254740991.5), "9007199254740992")
|
||||
check_result(BigInt(0x1fffffffffffff * (2 ** 70)), "10633823966279325802638835764831453184")
|
||||
check_result(BigInt(-0x1fffffffffffff * (2 ** 128)), "-3064991081731777376434327133362154903862870812598992896")
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
/* Boolean. */
|
||||
|
||||
assert(!BigInt("0") === true)
|
||||
assert(!BigInt("1") === false)
|
||||
assert(!BigInt("-1") === false)
|
||||
|
||||
/* Strict equal. */
|
||||
|
||||
assert(BigInt("0") === BigInt("0"))
|
||||
assert(BigInt("-77") === BigInt("-77"))
|
||||
assert(!(BigInt("-77") !== BigInt("-77")))
|
||||
|
||||
assert(!(Object(BigInt("-77")) === BigInt("-77")))
|
||||
assert(BigInt("-77") !== Object(BigInt("-77")))
|
||||
|
||||
assert(BigInt("0xffffffffffffffffffffffffffffffff") === BigInt("0xffffffffffffffffffffffffffffffff"))
|
||||
assert(!(BigInt("0xfffffffffffffffffffffffffffffffe") === BigInt("0xffffffffffffffffffffffffffffffff")))
|
||||
assert(BigInt("0x100000000000000000000000000000000") !== BigInt("0xffffffffffffffffffffffffffffffff"))
|
||||
assert(!(BigInt("1234") === 1234))
|
||||
assert(-4567 !== BigInt("-4567"))
|
||||
|
||||
/* Equal. */
|
||||
|
||||
assert(BigInt("0x100") == BigInt("256"))
|
||||
assert(BigInt("-77") == "-77")
|
||||
assert("168" == BigInt("168"))
|
||||
assert(!("0xffffffffffffffffffffffffffffffff" != BigInt("0xffffffffffffffffffffffffffffffff")))
|
||||
|
||||
assert(BigInt("0x1000") == 0x1000)
|
||||
assert(468123 == BigInt("468123"))
|
||||
|
||||
assert(!(BigInt("100000000") == 100000000.5))
|
||||
assert(-0.125 != BigInt("0"))
|
||||
assert(!("InvalidBigIntString" == BigInt("100000000")))
|
||||
assert(BigInt("100000000") != "10000 0000")
|
||||
|
||||
assert(BigInt("0") == 0)
|
||||
assert(!(-0 != BigInt("0")))
|
||||
assert(!(BigInt("0") == 0.0000152587890625))
|
||||
assert(0.0000152587890625 != BigInt("0"))
|
||||
|
||||
assert(!(BigInt("100000000") == NaN))
|
||||
assert(NaN != BigInt("-100000000"))
|
||||
assert(!(BigInt("100000000000000000000000000") == Infinity))
|
||||
assert(Infinity != BigInt("100000000000000000000000000"))
|
||||
|
||||
/* Relational. */
|
||||
|
||||
assert(!(BigInt("1234") < BigInt("1234")))
|
||||
assert(BigInt("1234") >= BigInt("1234"))
|
||||
assert(BigInt("1234") <= BigInt("1234"))
|
||||
assert(!(BigInt("1234") > BigInt("1234")))
|
||||
|
||||
assert(BigInt("1234") < BigInt("1235"))
|
||||
assert(!(BigInt("1234") >= BigInt("1235")))
|
||||
assert(BigInt("1234") <= BigInt("1235"))
|
||||
assert(!(BigInt("1234") > BigInt("1235")))
|
||||
|
||||
assert(!(BigInt("123456789012345678901234567890") < "123456789012345678901234567890"))
|
||||
assert(BigInt("123456789012345678901234567890") >= "123456789012345678901234567890")
|
||||
assert(BigInt("123456789012345678901234567890") <= "123456789012345678901234567890")
|
||||
assert(!(BigInt("123456789012345678901234567890") > "123456789012345678901234567890"))
|
||||
|
||||
assert(!("0x1234567890abcdef1234567890abcdef" < BigInt("0x1234567890abcdef1234567890abcdef")))
|
||||
assert("0x1234567890abcdef1234567890abcdef" >= BigInt("0x1234567890abcdef1234567890abcdef"))
|
||||
assert("0x1234567890abcdef1234567890abcdef" <= BigInt("0x1234567890abcdef1234567890abcdef"))
|
||||
assert(!("0x1234567890abcdef1234567890abcdef" > BigInt("0x1234567890abcdef1234567890abcdef")))
|
||||
|
||||
assert(!("Invalid" < BigInt("100")))
|
||||
assert(!("Invalid" >= BigInt("100")))
|
||||
assert(!("Invalid" <= BigInt("100")))
|
||||
assert(!("Invalid" > BigInt("100")))
|
||||
|
||||
assert(!(BigInt("0") < "NotABigInt"))
|
||||
assert(!(BigInt("0") >= "NotABigInt"))
|
||||
assert(!(BigInt("0") <= "NotABigInt"))
|
||||
assert(!(BigInt("0") > "NotABigInt"))
|
||||
|
||||
assert(!(BigInt("0") < 0))
|
||||
assert(BigInt("0") >= 0)
|
||||
assert(BigInt("0") <= 0)
|
||||
assert(!(BigInt("0") > 0))
|
||||
|
||||
assert(!(-0 < BigInt("0")))
|
||||
assert(-0 >= BigInt("0"))
|
||||
assert(-0 <= BigInt("0"))
|
||||
assert(!(-0 > BigInt("0")))
|
||||
|
||||
assert(BigInt("0") < 67)
|
||||
assert(!(BigInt("0") > 67))
|
||||
assert(!(BigInt("0") < -0.125))
|
||||
assert(BigInt("0") > -0.125)
|
||||
|
||||
assert(!(BigInt("7") < NaN))
|
||||
assert(!(BigInt("7") >= NaN))
|
||||
assert(!(BigInt("7") <= NaN))
|
||||
assert(!(BigInt("7") > NaN))
|
||||
|
||||
assert(!(Infinity < BigInt("1000000000000000000000000000000")))
|
||||
assert(!(Infinity <= BigInt("1000000000000000000000000000000")))
|
||||
assert(Infinity >= BigInt("1000000000000000000000000000000"))
|
||||
assert(Infinity > BigInt("1000000000000000000000000000000"))
|
||||
|
||||
assert(-Infinity < BigInt("1000000000000000000000000000000"))
|
||||
assert(-Infinity <= BigInt("1000000000000000000000000000000"))
|
||||
assert(!(-Infinity >= BigInt("1000000000000000000000000000000")))
|
||||
assert(!(-Infinity > BigInt("1000000000000000000000000000000")))
|
||||
|
||||
assert(BigInt("-10000") < 1)
|
||||
assert(BigInt("-10000") <= 1)
|
||||
assert(!(BigInt("-10000") >= 1))
|
||||
assert(!(BigInt("-10000") > 1))
|
||||
|
||||
assert(!(1 < BigInt("-12345678")))
|
||||
assert(!(1 <= BigInt("-12345678")))
|
||||
assert(1 >= BigInt("-12345678"))
|
||||
assert(1 > BigInt("-12345678"))
|
||||
|
||||
assert(!(BigInt("1") < 0.5))
|
||||
assert(!(BigInt("1") <= 0.5))
|
||||
assert(BigInt("1") >= 0.5)
|
||||
assert(BigInt("1") > 0.5)
|
||||
|
||||
assert(!(-0.5 < BigInt("-1")))
|
||||
assert(!(-0.5 <= BigInt("-1")))
|
||||
assert(-0.5 >= BigInt("-1"))
|
||||
assert(-0.5 > BigInt("-1"))
|
||||
|
||||
assert(!(BigInt("0x1000000000000000000000000000000") < 0x100000))
|
||||
assert(!(BigInt("0x1000000000000000000000000000000") <= 0x100000))
|
||||
assert(BigInt("0x1000000000000000000000000000000") >= 0x100000)
|
||||
assert(BigInt("0x1000000000000000000000000000000") > 0x100000)
|
||||
|
||||
assert(-0x1000000000000000000000000000000 < BigInt("-1234"))
|
||||
assert(-0x1000000000000000000000000000000 <= BigInt("-1234"))
|
||||
assert(!(-0x1000000000000000000000000000000 >= BigInt("-1234")))
|
||||
assert(!(-0x1000000000000000000000000000000 > BigInt("-1234")))
|
||||
|
||||
assert(0x1234567880000000000000000000000 < BigInt("0x1234567890000000000000000000000"))
|
||||
assert(0x1234567880000000000000000000000 <= BigInt("0x1234567890000000000000000000000"))
|
||||
assert(!(0x1234567880000000000000000000000 > BigInt("0x1234567890000000000000000000000")))
|
||||
assert(!(0x1234567880000000000000000000000 >= BigInt("0x1234567890000000000000000000000")))
|
||||
|
||||
assert(-BigInt("0x1234567890000000000000000000000") < -0x1234567880000000000000000000000)
|
||||
assert(-BigInt("0x1234567890000000000000000000000") <= -0x1234567880000000000000000000000)
|
||||
assert(!(-BigInt("0x1234567890000000000000000000000") >= -0x1234567880000000000000000000000))
|
||||
assert(!(-BigInt("0x1234567890000000000000000000000") > -0x1234567880000000000000000000000))
|
||||
|
||||
// True because of rounding
|
||||
assert(0x1234567890000000000000000000001 < BigInt("0x1234567890000000000000000000001"))
|
||||
assert(0x1234567890000000000000000000001 <= BigInt("0x1234567890000000000000000000001"))
|
||||
assert(!(0x1234567890000000000000000000001 >= BigInt("0x1234567890000000000000000000001")))
|
||||
assert(!(0x1234567890000000000000000000001 > BigInt("0x1234567890000000000000000000001")))
|
||||
|
||||
assert(-BigInt("0x1234567890000000000000000000001") < -0x1234567890000000000000000000001)
|
||||
assert(-BigInt("0x1234567890000000000000000000001") <= -0x1234567890000000000000000000001)
|
||||
assert(!(-BigInt("0x1234567890000000000000000000001") >= -0x1234567890000000000000000000001))
|
||||
assert(!(-BigInt("0x1234567890000000000000000000001") > -0x1234567890000000000000000000001))
|
||||
|
||||
assert(!(1.0000152587890625 < BigInt("1")))
|
||||
assert(!(1.0000152587890625 <= BigInt("1")))
|
||||
assert(1.0000152587890625 >= BigInt("1"))
|
||||
assert(1.0000152587890625 > BigInt("1"))
|
||||
|
||||
assert(!(BigInt("-1") < -1.0000152587890625))
|
||||
assert(!(BigInt("-1") <= -1.0000152587890625))
|
||||
assert(BigInt("-1") >= -1.0000152587890625)
|
||||
assert(BigInt("-1") > -1.0000152587890625)
|
||||
@@ -0,0 +1,57 @@
|
||||
/* 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 check_syntax_error(code)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
check_syntax_error("1N")
|
||||
check_syntax_error("3.5n")
|
||||
check_syntax_error("3e10n")
|
||||
check_syntax_error("3e+10n")
|
||||
check_syntax_error("0xn")
|
||||
check_syntax_error("0on")
|
||||
check_syntax_error("0bn")
|
||||
check_syntax_error("0777n")
|
||||
check_syntax_error("00777n")
|
||||
check_syntax_error("0x1 n")
|
||||
|
||||
assert(0n == 0n)
|
||||
assert(0n == -0n)
|
||||
assert(12n == 12n)
|
||||
assert(123456789012345678901234567890123456789012345678901234567890n == 123456789012345678901234567890123456789012345678901234567890n)
|
||||
assert(12n != -12n)
|
||||
assert(123456789012345678901234567890123456789012345678901234567890n != -123456789012345678901234567890123456789012345678901234567890n)
|
||||
|
||||
assert(0xffn == 255n)
|
||||
assert(0o77777n == 0x7fffn)
|
||||
assert(255n.toString(16) == "ff")
|
||||
|
||||
var o = { 12n : "data" }
|
||||
assert(o[12] === "data")
|
||||
|
||||
var c = class C { static 19n () { return "BigInt" } }
|
||||
assert(c[19]() === "BigInt")
|
||||
|
||||
function f(p, q) {
|
||||
assert(p + q === 5000n)
|
||||
}
|
||||
f(-1000n, 6000n)
|
||||
@@ -0,0 +1,54 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// Test bitwise 'and' operation
|
||||
|
||||
assert((0n & 0n) === 0n)
|
||||
assert((0x12345678n & 0n) === 0n)
|
||||
assert((0n & 0x12345678n) === 0n)
|
||||
assert((0xff00ff00ff00ff00ff00n & 0xff00ff00ff00ff00ffn) === 0n)
|
||||
assert((0x12345678ffffffff12345678ffffffffn & 0xffff87654321n) === 0x567887654321n)
|
||||
assert((0xf56cd2479efcdn & 0x56cdf23bc02134e3bdc56f43297be4c27n) === 0x6540004384c05n)
|
||||
assert((0x45c308bd83cf279n & -0x100000000n) === 0x45c308b00000000n)
|
||||
assert((-0x10000000000000000n & 0xffffffffffffffffn) === 0n)
|
||||
assert((-0x11234567890abcdefn & 0xffffffffffffffffffffn) === 0xfffeedcba9876f543211n)
|
||||
assert((-0x10000000000000n & -0x10000000000000n) === -0x10000000000000n)
|
||||
assert((-0x100000000000000001n & -0x1n) === -0x100000000000000001n)
|
||||
|
||||
// Test bitwise 'or' operation
|
||||
|
||||
assert((0n | 0n) === 0n)
|
||||
assert((0x123456789abcdefn | 0n) === 0x123456789abcdefn)
|
||||
assert((0n | 0x123456789abcdefn) === 0x123456789abcdefn)
|
||||
assert((0xaa00bb00cc00dd00ee00n | 0xff00ee00dd00cc00bbn) === 0xaaffbbeeccddddcceebbn);
|
||||
assert((0xfedcba09876543210fedcba09876543210n | 0x7n) === 0xfedcba09876543210fedcba09876543217n)
|
||||
assert((0x8n | 0xfedcba09876543210fedcba09876543210n) === 0xfedcba09876543210fedcba09876543218n)
|
||||
assert((-0xc34bd5f946c7a92b69b3a96cd7c2a12n | 0xfcbacfbn) === -0xc34bd5f946c7a92b69b3a96c0340201n)
|
||||
assert((-0xb314c297ba3n | 0xfeacb00000000n) === -0x1304c297ba3n)
|
||||
assert((-0x74b186cd308b377cb23n | -0x5cba7935b213cd657d937c42975de63802a7b92cd49an) === -0x74900280200b124c001n)
|
||||
assert((-0x10000000000000000n | -0x100000000000000000000000000000000n) === -0x10000000000000000n)
|
||||
|
||||
// Test bitwise 'xor' operation
|
||||
|
||||
assert((0n ^ 0n) === 0n)
|
||||
assert((0x123456789abcdefn ^ 0n) === 0x123456789abcdefn)
|
||||
assert((0n ^ 0x123456789abcdefn) === 0x123456789abcdefn)
|
||||
assert((0x74b186cd308b355cb23cd28cd75n ^ 0x74b186cd308b355cb23cd28cd75n) === 0n)
|
||||
assert((0xff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ffn ^ 0xf0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0fn) === 0xff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff0n)
|
||||
assert((0x31988644a57e18n ^ 0xb2303b6f2efcb4de7761c01622440f9d985d07dbfe03c9f1n) === 0xb2303b6f2efcb4de7761c01622440f9d986c9f5dbaa6b7e9n)
|
||||
assert((-0xd858541b8eb3e6ae247b1f84dbd8cc2db66n ^ 0x0811a0e70710fcf965n) === -0xd858541b8eb3e6ae24fa058aaba9c3e2201n)
|
||||
assert((0x38d00faa3f33n ^ -0x89c40cdc4a064dcd8b3663feb322026dn) === -0x89c40cdc4a064dcd8b365b2ebc883d60n)
|
||||
assert((-0x66cb3001b88361a25b8715922n ^ -0x66cb3001b88361a25b8715922n) === 0n)
|
||||
assert((-0x893bff556397300afe6411d8727c0aaffn ^ -0xef69f24dfcd1447397d62217c6ad2n) === 0x893b103c91daccdbba17860e506bcc02fn)
|
||||
@@ -0,0 +1,115 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// Bitwise not
|
||||
|
||||
assert(~BigInt("0") === -1n)
|
||||
assert(~BigInt("-1") === 0n)
|
||||
|
||||
assert(~BigInt("0xffffffff") === -0x100000000n)
|
||||
assert(~BigInt("0x100000000") === -0x100000001n)
|
||||
assert(~BigInt("0x10000ffff") === -0x100010000n)
|
||||
assert(~(-BigInt("0xffffffff")) === 0xfffffffen)
|
||||
assert(~(-BigInt("0x100000000")) === 0xffffffffn)
|
||||
assert(~(-BigInt("0x10000ffff")) === 0x10000fffen)
|
||||
|
||||
assert(~BigInt("0xffffffffffffffff") === -0x10000000000000000n)
|
||||
assert(~BigInt("0xffffffffffffffffffffffffffffffff") === -0x100000000000000000000000000000000n)
|
||||
assert(~BigInt("0x100000000000000000000000000000000") === -0x100000000000000000000000000000001n)
|
||||
assert(~BigInt("0x100000000000000ffffffffffffffffff") === -0x100000000000001000000000000000000n)
|
||||
assert(~(-BigInt("0xffffffffffffffff")) === 0xfffffffffffffffen)
|
||||
assert(~(-BigInt("0xffffffffffffffffffffffffffffffff")) === 0xfffffffffffffffffffffffffffffffen)
|
||||
assert(~(-BigInt("0x100000000000000000000000000000000")) === 0xffffffffffffffffffffffffffffffffn)
|
||||
assert(~(-BigInt("0xffffffffffffff000000000000000000")) === 0xfffffffffffffeffffffffffffffffffn)
|
||||
|
||||
// Increase
|
||||
|
||||
var a = 0n
|
||||
assert(++a === 1n)
|
||||
assert(a === 1n)
|
||||
|
||||
a = -1n
|
||||
assert(++a === 0n)
|
||||
assert(a === 0n)
|
||||
|
||||
a = 1n
|
||||
assert(++a === 2n)
|
||||
assert(a === 2n)
|
||||
|
||||
a = 0xffffffffn
|
||||
assert(++a === 0x100000000n)
|
||||
assert(a === 0x100000000n)
|
||||
|
||||
a = { b:0xffffffffffffffffn }
|
||||
assert(++a.b === 0x10000000000000000n)
|
||||
assert(a.b === 0x10000000000000000n)
|
||||
|
||||
a = 0xffffffffffffffffffffffffffffffffn
|
||||
assert(a++ === 0xffffffffffffffffffffffffffffffffn)
|
||||
assert(a === 0x100000000000000000000000000000000n)
|
||||
|
||||
a = { b:0x100000000000000ffffffffffffffffffn }
|
||||
assert(a.b++ === 0x100000000000000ffffffffffffffffffn)
|
||||
assert(a.b === 0x100000000000001000000000000000000n)
|
||||
|
||||
a = -0x10000000000000001n;
|
||||
for (var i = 0; i < 1; i++, a++) ;
|
||||
assert(a === -0x10000000000000000n)
|
||||
|
||||
a = { b:-0x100000000000001000000000000000000n }
|
||||
for (var i = 0; i < 1; i++, ++a.b) ;
|
||||
assert(a.b === -0x100000000000000ffffffffffffffffffn)
|
||||
|
||||
// Decrease
|
||||
|
||||
a = 0n
|
||||
assert(--a === -1n)
|
||||
assert(a === -1n)
|
||||
|
||||
a = 1n
|
||||
assert(--a === 0n)
|
||||
assert(a === 0n)
|
||||
|
||||
a = -1n
|
||||
assert(--a === -2n)
|
||||
assert(a === -2n)
|
||||
|
||||
a = 0x100000000n
|
||||
assert(--a === 0xffffffffn)
|
||||
assert(a === 0xffffffffn)
|
||||
|
||||
a = -0xffffffffffffffffn
|
||||
assert(a-- === -0xffffffffffffffffn)
|
||||
assert(a === -0x10000000000000000n)
|
||||
|
||||
a = { b:0x10000000000000000n }
|
||||
assert(--a.b === 0xffffffffffffffffn)
|
||||
assert(a.b === 0xffffffffffffffffn)
|
||||
|
||||
a = 0x100000000000000000000000000000000n
|
||||
assert(a-- === 0x100000000000000000000000000000000n)
|
||||
assert(a === 0xffffffffffffffffffffffffffffffffn)
|
||||
|
||||
a = { b:0x100000000000001000000000000000000n }
|
||||
assert(a.b-- === 0x100000000000001000000000000000000n)
|
||||
assert(a.b === 0x100000000000000ffffffffffffffffffn)
|
||||
|
||||
a = 0x10000000000000001n;
|
||||
for (var i = 0; i < 1; i++, a--) ;
|
||||
assert(a === 0x10000000000000000n)
|
||||
|
||||
a = { b:-0x100000000000000ffffffffffffffffffn }
|
||||
for (var i = 0; i < 1; i++, --a.b) ;
|
||||
assert(a.b === -0x100000000000001000000000000000000n)
|
||||
@@ -0,0 +1,55 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// Exponentiation
|
||||
|
||||
try {
|
||||
12n ** -7n
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof RangeError)
|
||||
}
|
||||
|
||||
assert((0n ** 0n) === 1n)
|
||||
assert((1n ** 0n) === 1n)
|
||||
assert(((-2n) ** 0n) === 1n)
|
||||
assert((1000000000000000000000000000000000n ** 0n) === 1n)
|
||||
assert(((-1000000000000000000000000000000000n) ** 0n) === 1n)
|
||||
|
||||
assert((1n ** 1n) === 1n)
|
||||
assert((1n ** 10000000000000000000000000000000n) === 1n)
|
||||
assert(((-1n) ** 1n) === -1n)
|
||||
assert(((-1n) ** 10000000000000000000000000000000n) === 1n)
|
||||
assert(((-1n) ** 10000000000000000000000000000001n) === -1n)
|
||||
|
||||
assert((2n ** 10n) === 1024n)
|
||||
assert((2n ** 11n) === 2048n)
|
||||
assert(((-2n) ** 10n) === 1024n)
|
||||
assert(((-2n) ** 11n) === -2048n)
|
||||
assert((2n ** 64n) === 0x10000000000000000n)
|
||||
assert((2n ** 65n) === 0x20000000000000000n)
|
||||
assert(((-2n) ** 64n) === 0x10000000000000000n)
|
||||
assert(((-2n) ** 65n) === -0x20000000000000000n)
|
||||
|
||||
assert((2n ** 190n) === 0x400000000000000000000000000000000000000000000000n)
|
||||
assert((2n ** 191n) === 0x800000000000000000000000000000000000000000000000n)
|
||||
assert(((-2n) ** 190n) === 0x400000000000000000000000000000000000000000000000n)
|
||||
assert(((-2n) ** 191n) === -0x800000000000000000000000000000000000000000000000n)
|
||||
|
||||
assert((103n ** 32n) === 25750827556851106532658069028441289322166445432839581773436522241n)
|
||||
assert((103n ** 31n) === 250008034532535014880175427460595041962781023619801764790645847n)
|
||||
assert(((-79n) ** 32n) === 5297450670659957549009604563595170759963655420038456036451841n)
|
||||
assert(((-79n) ** 31n) === -67056337603290601886197526121457857721058929367575392866479n)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
assert(Number(0n) === 0)
|
||||
assert(Number(1n) === 1)
|
||||
assert(Number(2n) === 2)
|
||||
assert(Number(0x100n) === 256)
|
||||
assert(Number(-1n) === -1)
|
||||
assert(Number(-2n) === -2)
|
||||
assert(Number(-0x100n) === -256)
|
||||
|
||||
assert(Number(0x1fffffffffffffn) === 0x1fffffffffffff)
|
||||
assert(Number(-0x3fffffffffffffn) === -0x40000000000000)
|
||||
assert(Number(1000n ** 1000n) === Number.POSITIVE_INFINITY)
|
||||
assert(Number((-1000n) ** 1001n) === Number.NEGATIVE_INFINITY)
|
||||
|
||||
// Rounding to even
|
||||
|
||||
assert(Number(0x80000000000004n) === 0x80000000000000)
|
||||
assert(Number(0x80000000000008n) === 0x80000000000008)
|
||||
assert(Number(0x8000000000000cn) === 0x80000000000010)
|
||||
assert(Number(0x80000000000004n) === 0x80000000000000)
|
||||
assert(Number(0x80000000000006n) === 0x80000000000008)
|
||||
assert(Number(0x800000000000f400000000000000000000000000000000n) === 0x800000000000f000000000000000000000000000000000)
|
||||
assert(Number(0x800000000000f400000000000000000000000000000001n) === 0x800000000000f800000000000000000000000000000000)
|
||||
|
||||
// Construct
|
||||
|
||||
assert((new Number(0n)).valueOf() === 0)
|
||||
assert((new Number(-3256n)).valueOf() == -3256)
|
||||
@@ -0,0 +1,52 @@
|
||||
/* 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 checkSyntaxError (str) {
|
||||
try {
|
||||
eval(str);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError);
|
||||
}
|
||||
}
|
||||
|
||||
// Test with invalid literals
|
||||
checkSyntaxError("0c");
|
||||
checkSyntaxError("0b");
|
||||
checkSyntaxError("0b0123456");
|
||||
checkSyntaxError("0b2");
|
||||
|
||||
checkSyntaxError("0C");
|
||||
checkSyntaxError("0B");
|
||||
checkSyntaxError("0B2");
|
||||
|
||||
checkSyntaxError("000b01010101");
|
||||
checkSyntaxError("010b01010101");
|
||||
checkSyntaxError("11 0b01010101");
|
||||
|
||||
// Test with valid literals
|
||||
assert(0b111 === 7);
|
||||
assert(0b111110111 === 503);
|
||||
assert(0b111101010101 === 3925);
|
||||
assert(0b00000000000001 === 1);
|
||||
assert(0b00000000000000 === 0);
|
||||
assert(0b1101001 === parseInt ("1101001", 2));
|
||||
|
||||
assert(0B111 === 7);
|
||||
assert(0B111110111 === 503);
|
||||
assert(0B111101010101 === 3925);
|
||||
assert(0B00000000000001 === 1);
|
||||
assert(0B00000000000000 === 0);
|
||||
assert(0B1101001 === parseInt ("1101001", 2));
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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 check_syntax_error (script)
|
||||
{
|
||||
try
|
||||
{
|
||||
eval (script);
|
||||
assert (false);
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
assert (e instanceof SyntaxError);
|
||||
}
|
||||
}
|
||||
|
||||
eval("function f(){}; var f;");
|
||||
eval("var f; function f(){};");
|
||||
|
||||
eval("function f(){}; { var f; }")
|
||||
eval("{ var f; } function f(){};")
|
||||
|
||||
eval("{ function f(){}; } var f;")
|
||||
eval("var f; { function f(){}; }")
|
||||
|
||||
check_syntax_error ("{ function f(){}; var f; }");
|
||||
check_syntax_error ("{ var f; function f(){}; }");
|
||||
|
||||
eval("{ { function f(){}; } var f; }")
|
||||
eval("{ var f; { function f(){}; } }")
|
||||
|
||||
check_syntax_error ("{ function f(){}; { var f; } }")
|
||||
check_syntax_error ("{ { var f; } function f(){}; }")
|
||||
|
||||
eval("{ { function f(){}; } { var f; } }")
|
||||
eval("{ { var f; } { function f(){}; } }")
|
||||
|
||||
eval("function g(){ function f(){}; var f; }")
|
||||
eval("function g(){ var f; function f(){}; }")
|
||||
|
||||
eval("function g(){ function f(){}; { var f; } }")
|
||||
eval("function g(){ { var f; } function f(){}; }")
|
||||
|
||||
eval("function g(){ { function f(){}; } var f; }")
|
||||
eval("function g(){ var f; { function f(){}; } }")
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
// 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 test_failed = false;
|
||||
|
||||
function verifyConfigurableAccessor (obj, name, string) {
|
||||
let prop = Object.getOwnPropertyDescriptor (obj, name);
|
||||
if (prop.get && !prop.configurable) {
|
||||
print (string + " should be configurable, but wasn't");
|
||||
test_failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (let builtin_name of Reflect.ownKeys (this)) {
|
||||
let builtin_obj = this[builtin_name];
|
||||
if (builtin_name[0] === builtin_name[0].toUpperCase () && typeof builtin_obj == "function") {
|
||||
for (let prop of Reflect.ownKeys (builtin_obj)) {
|
||||
verifyConfigurableAccessor (builtin_obj, prop, builtin_name + "." + prop.toString ());
|
||||
}
|
||||
|
||||
let builtin_proto = builtin_obj.prototype;
|
||||
if (builtin_proto) {
|
||||
for (let prop of Reflect.ownKeys (builtin_proto)) {
|
||||
verifyConfigurableAccessor (builtin_proto, prop, builtin_name + ".prototype." + prop.toString ());
|
||||
}
|
||||
}
|
||||
|
||||
builtin_proto = Reflect.getPrototypeOf (builtin_obj);
|
||||
if (builtin_proto !== Function.prototype) {
|
||||
for (let prop of Reflect.ownKeys (builtin_proto.prototype)) {
|
||||
verifyConfigurableAccessor (builtin_proto.prototype, prop, builtin_name + ".[[Prototype]].prototype." + prop.toString ());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert (!test_failed);
|
||||
@@ -0,0 +1,60 @@
|
||||
/* 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 prototypes = [
|
||||
Date.prototype,
|
||||
RegExp.prototype,
|
||||
Error.prototype,
|
||||
EvalError.prototype,
|
||||
RangeError.prototype,
|
||||
ReferenceError.prototype,
|
||||
SyntaxError.prototype,
|
||||
TypeError.prototype,
|
||||
URIError.prototype
|
||||
]
|
||||
|
||||
for (proto of prototypes) {
|
||||
assert (Object.prototype.toString.call (proto) === '[object Object]');
|
||||
}
|
||||
|
||||
try {
|
||||
Date.prototype.valueOf();
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
RegExp.prototype.exec("");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
RegExp.prototype.compile("a", "u");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert (RegExp.prototype.source === '(?:)');
|
||||
assert (RegExp.prototype.global === undefined);
|
||||
assert (RegExp.prototype.ignoreCase === undefined);
|
||||
assert (RegExp.prototype.multiline === undefined);
|
||||
assert (RegExp.prototype.sticky === undefined);
|
||||
assert (RegExp.prototype.unicode === undefined);
|
||||
assert (RegExp.prototype.dotAll === undefined);
|
||||
assert (RegExp.prototype.flags === '');
|
||||
@@ -0,0 +1,182 @@
|
||||
// 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 check_syntax_error(code)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
function check_property(obj, name, value)
|
||||
{
|
||||
property = Object.getOwnPropertyDescriptor(obj, name)
|
||||
assert(typeof property === "object")
|
||||
assert(property.value === value)
|
||||
}
|
||||
|
||||
check_syntax_error("class C { get a = 5 }");
|
||||
check_syntax_error("class C { id1 id2 }");
|
||||
check_syntax_error("class C { a = 5,6 }");
|
||||
check_syntax_error("class C { set\na = 6 }");
|
||||
check_syntax_error("class C { constructor }");
|
||||
check_syntax_error("class C { static constructor }");
|
||||
check_syntax_error("class C { constructor = 1 }");
|
||||
check_syntax_error("class C { static constructor = 1 }");
|
||||
check_syntax_error("class C { f = arguments }");
|
||||
check_syntax_error("class C { static f = a\\u0072guments }");
|
||||
check_syntax_error("class C { f = () => arguments }");
|
||||
check_syntax_error("class C { f = arguments => 1 }");
|
||||
check_syntax_error("class C { f = ([arguments]) => 1 }");
|
||||
check_syntax_error("new class { f = eval('arguments') }");
|
||||
check_syntax_error("new class { f = eval('arguments => 1') }");
|
||||
|
||||
var res = 10
|
||||
var counter = 0
|
||||
|
||||
function f1() {
|
||||
counter++
|
||||
return 5
|
||||
}
|
||||
|
||||
var C1 = class {
|
||||
get = "a" + f1()
|
||||
static; set; a = () => Math.cos(0)
|
||||
v\u0061r
|
||||
f\u006fr = () => this
|
||||
arguments = this
|
||||
}
|
||||
|
||||
res = new C1
|
||||
check_property(res, "get", "a5")
|
||||
check_property(res, "static", undefined)
|
||||
check_property(res, "set", undefined)
|
||||
assert(res.a() === 1)
|
||||
check_property(res, "var", undefined)
|
||||
assert(res.for() === res)
|
||||
assert(res.arguments === res)
|
||||
|
||||
class C2 {
|
||||
constructor(a = this.x, b = this.y) {
|
||||
assert(a === undefined)
|
||||
assert(b === undefined)
|
||||
check_property(this, 'x', 11)
|
||||
check_property(this, 'y', "ab")
|
||||
}
|
||||
x = 5 + 6
|
||||
y = "a" + 'b'
|
||||
}
|
||||
|
||||
res = new C2
|
||||
|
||||
class C3 {
|
||||
constructor() {
|
||||
assert(this.x === 1)
|
||||
return { z:"zz" }
|
||||
}
|
||||
x = 1
|
||||
}
|
||||
|
||||
class C4 extends C3 {
|
||||
constructor() {
|
||||
super()
|
||||
assert(Object.getOwnPropertyDescriptor(this, "x") === undefined)
|
||||
check_property(this, "y", 2)
|
||||
check_property(this, "z", "zz")
|
||||
}
|
||||
y = 2
|
||||
}
|
||||
new C4
|
||||
|
||||
var o = {}
|
||||
class C5 extends C3 {
|
||||
'pr op' = o
|
||||
3 = true
|
||||
}
|
||||
res = new C5
|
||||
assert(Object.getOwnPropertyDescriptor(res, "x") === undefined)
|
||||
check_property(res, "pr op", o)
|
||||
check_property(res, "3", true)
|
||||
check_property(res, "z", "zz")
|
||||
|
||||
class C6 {
|
||||
a= () => this
|
||||
b= this
|
||||
}
|
||||
|
||||
class C7 extends C6 {
|
||||
c= () => this
|
||||
d= this
|
||||
}
|
||||
|
||||
count = 0
|
||||
class C8 extends C7 {
|
||||
constructor() {
|
||||
count++
|
||||
super()
|
||||
}
|
||||
|
||||
e= () => this
|
||||
f= this
|
||||
}
|
||||
|
||||
var res = new C8
|
||||
assert(res.a() === res)
|
||||
assert(res.b === res)
|
||||
assert(res.c() === res)
|
||||
assert(res.d === res)
|
||||
assert(res.e() === res)
|
||||
assert(res.f === res)
|
||||
|
||||
count = 0
|
||||
class C9 {
|
||||
a=assert(++count === 5)
|
||||
a=assert(++count === 6)
|
||||
a=assert(++count === 7)
|
||||
a=assert(++count === 8)
|
||||
static a=assert(++count === 1)
|
||||
static a=assert(++count === 2)
|
||||
static a=assert(++count === 3)
|
||||
static a=assert(++count === 4)
|
||||
}
|
||||
|
||||
assert(count === 4)
|
||||
new C9
|
||||
assert(count === 8)
|
||||
|
||||
count = 0
|
||||
class C10 {
|
||||
[(assert(++count == 1), "aa")] = assert(++count == 5);
|
||||
[(assert(++count == 2), "bb")] = assert(++count == 6);
|
||||
cc = assert(++count == 7);
|
||||
[(assert(++count == 3), "aa")] = assert(++count == 8);
|
||||
[(assert(++count == 4), "bb")] = assert(++count == 9);
|
||||
}
|
||||
|
||||
assert(count == 4)
|
||||
assert(Reflect.ownKeys(new C10).toString() === "aa,bb,cc");
|
||||
assert(count == 9)
|
||||
|
||||
res = "p"
|
||||
class C11 {
|
||||
p1 = assert(Reflect.ownKeys(this).toString() === "");
|
||||
[res + 2] = assert(Reflect.ownKeys(this).toString() === "p1");
|
||||
[res + 1] = assert(Reflect.ownKeys(this).toString() === "p1,p2");
|
||||
p3 = assert(Reflect.ownKeys(this).toString() === "p1,p2");
|
||||
[res + 4] = assert(Reflect.ownKeys(this).toString() === "p1,p2,p3");
|
||||
}
|
||||
new C11
|
||||
@@ -0,0 +1,139 @@
|
||||
// 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 count = 0
|
||||
class C1 {
|
||||
error = assert(++count === 1)
|
||||
error = function() { throw 40.5 }()
|
||||
}
|
||||
|
||||
try {
|
||||
new C1
|
||||
assert(false)
|
||||
} catch(e) {
|
||||
assert(e === 40.5)
|
||||
assert(count === 1)
|
||||
}
|
||||
|
||||
count = 0
|
||||
class C2 {
|
||||
constructor(a = assert(++count === 1)) {}
|
||||
error = function() { throw "Err" }()
|
||||
error = assert(false)
|
||||
}
|
||||
|
||||
try {
|
||||
new C2
|
||||
assert(false)
|
||||
} catch(e) {
|
||||
assert(e === "Err")
|
||||
assert(count === 1)
|
||||
}
|
||||
|
||||
count = 0
|
||||
var o = {}
|
||||
|
||||
class C3 extends class {
|
||||
error = function() { throw o }()
|
||||
} {
|
||||
constructor() {
|
||||
assert(++count === 1)
|
||||
super()
|
||||
assert(false)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
new C3
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e === o)
|
||||
assert(count === 1)
|
||||
}
|
||||
|
||||
count = 0
|
||||
class C4 {
|
||||
constructor() {
|
||||
assert(++count === 2)
|
||||
}
|
||||
a = assert(++count === 1)
|
||||
}
|
||||
|
||||
class C5 extends C4 {
|
||||
ok = assert(++count === 3)
|
||||
error = function() { assert(++count === 4); throw "Except" }()
|
||||
never = assert(false)
|
||||
}
|
||||
|
||||
try {
|
||||
new C5
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e === "Except")
|
||||
assert(count === 4)
|
||||
}
|
||||
|
||||
count = 0
|
||||
o = []
|
||||
class C6 {
|
||||
a = assert(++count === 2)
|
||||
}
|
||||
|
||||
class C7 extends C6 {
|
||||
constructor() {
|
||||
assert(++count === 1)
|
||||
eval('super()')
|
||||
assert(false)
|
||||
}
|
||||
ok = assert(++count === 3)
|
||||
error = function() { assert(++count === 4); throw o }()
|
||||
never = assert(false)
|
||||
}
|
||||
|
||||
try {
|
||||
new C7
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e === o)
|
||||
assert(count === 4)
|
||||
}
|
||||
|
||||
var res
|
||||
class C8 {
|
||||
/* Create a non-configurable accessor */
|
||||
a = (res = this, Object.defineProperty(this, "b", { get() {} }));
|
||||
b = 6
|
||||
}
|
||||
|
||||
try {
|
||||
new C8
|
||||
assert(false)
|
||||
} catch(e) {
|
||||
assert(e instanceof TypeError)
|
||||
assert(Reflect.ownKeys(res).toString() === "b,a")
|
||||
}
|
||||
|
||||
class C9 {
|
||||
["p" + 1]
|
||||
["p" + 2] = (res = this, Object.freeze(this));
|
||||
p3
|
||||
}
|
||||
|
||||
try {
|
||||
new C9
|
||||
assert(false)
|
||||
} catch(e) {
|
||||
assert(e instanceof TypeError)
|
||||
assert(Reflect.ownKeys(res).toString() === "p1")
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// 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 check_property(obj, name, value)
|
||||
{
|
||||
property = Object.getOwnPropertyDescriptor(obj, name)
|
||||
assert(typeof property === "object")
|
||||
assert(property.value === value)
|
||||
}
|
||||
|
||||
var o = {}
|
||||
var name = "Pro"
|
||||
var res = 0
|
||||
var counter = 0
|
||||
|
||||
function f1() {
|
||||
counter++
|
||||
}
|
||||
|
||||
class C1 {
|
||||
static
|
||||
v\u0061r
|
||||
static Prop =
|
||||
res
|
||||
=
|
||||
"msg"
|
||||
static
|
||||
Prop
|
||||
=
|
||||
f1()
|
||||
static [name + "p"] = (f1(), o)
|
||||
static 22 = 3 * 4 ;static 23 = 5 + 6
|
||||
static 'a b'
|
||||
}
|
||||
|
||||
check_property(C1, "var", undefined)
|
||||
check_property(C1, "Prop", o)
|
||||
check_property(C1, 22, 12)
|
||||
check_property(C1, 23, 11)
|
||||
check_property(C1, "a b", undefined)
|
||||
assert(res === "msg")
|
||||
assert(counter === 2)
|
||||
|
||||
counter = 0
|
||||
class C2 {
|
||||
static a = (assert(++counter === 6), "x")
|
||||
static [(assert(++counter === 1), "b")]
|
||||
static [(assert(++counter === 2), "f")]() {}
|
||||
static [(assert(++counter === 3), "c")] = (assert(++counter === 7), this);
|
||||
[(assert(++counter === 4), "a")]
|
||||
static [(assert(++counter === 5), "d")];static e = (assert(++counter === 8), C2)
|
||||
}
|
||||
|
||||
assert(counter === 8)
|
||||
check_property(C2, "a", "x")
|
||||
check_property(C2, "b", undefined)
|
||||
check_property(C2, "c", C2)
|
||||
check_property(C2, "d", undefined)
|
||||
check_property(C2, "e", C2)
|
||||
|
||||
res = new C2
|
||||
check_property(res, "a", undefined)
|
||||
|
||||
let C3 = class C4 {
|
||||
static f() {}
|
||||
static xx = C4
|
||||
static yy = this
|
||||
}
|
||||
|
||||
assert(Reflect.ownKeys(C3).toString() === "length,prototype,f,name,xx,yy")
|
||||
check_property(C3, "xx", C3)
|
||||
check_property(C3, "yy", C3)
|
||||
@@ -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.
|
||||
|
||||
try {
|
||||
{
|
||||
A;
|
||||
class A { }
|
||||
}
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
class A { [A] () {} }
|
||||
}
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
var a = class A { [A] () {} }
|
||||
}
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
|
||||
{
|
||||
class C {
|
||||
a = C
|
||||
static b = C
|
||||
}
|
||||
|
||||
var X = C
|
||||
C = 6
|
||||
var c = new X
|
||||
|
||||
assert(X.b === X)
|
||||
assert(c.a === X)
|
||||
}
|
||||
|
||||
{
|
||||
let a = 6
|
||||
let b = 7
|
||||
class C {
|
||||
p = a + b
|
||||
}
|
||||
assert((new C).p === 13)
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
class C { static a = C = 5 }
|
||||
}
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
|
||||
try {
|
||||
{
|
||||
class C { static [C = 5] = 6 }
|
||||
}
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// 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 count = 0
|
||||
|
||||
class C1 {
|
||||
constructor() {
|
||||
assert(++count === 3)
|
||||
}
|
||||
|
||||
a = (assert(++count === 2), 1.1)
|
||||
}
|
||||
|
||||
class C2 extends C1 {
|
||||
constructor() {
|
||||
var s = () => super()
|
||||
|
||||
function g() {
|
||||
assert(++count === 1)
|
||||
eval("s()");
|
||||
}
|
||||
|
||||
g();
|
||||
assert(++count === 5)
|
||||
}
|
||||
|
||||
b = (assert(++count === 4), "prop")
|
||||
}
|
||||
|
||||
var c = new C2
|
||||
assert(count === 5)
|
||||
assert(c.a === 1.1)
|
||||
assert(c.b === "prop")
|
||||
|
||||
var o = {}
|
||||
count = 0
|
||||
|
||||
class C3 extends C1 {
|
||||
constructor() {
|
||||
var s = () => () => eval("() => eval('super()')")
|
||||
|
||||
function g() {
|
||||
assert(++count === 1)
|
||||
s()()()
|
||||
}
|
||||
|
||||
g();
|
||||
assert(++count === 5)
|
||||
}
|
||||
|
||||
b = (assert(++count === 4), o)
|
||||
}
|
||||
|
||||
c = new C3
|
||||
assert(count === 5)
|
||||
assert(c.a === 1.1)
|
||||
assert(c.b === o)
|
||||
|
||||
var f
|
||||
class C4 extends Array {
|
||||
a = 6.6
|
||||
|
||||
constructor() {
|
||||
f = () => super()
|
||||
super()
|
||||
}
|
||||
}
|
||||
c = new C4
|
||||
assert(c.a === 6.6)
|
||||
|
||||
try {
|
||||
f()
|
||||
assert(false)
|
||||
} catch(e) {
|
||||
assert(e instanceof ReferenceError)
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// 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.
|
||||
|
||||
|
||||
class A {
|
||||
// Test skipping spaces
|
||||
get (a, b, c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
// Test skipping spaces
|
||||
static get(a, b, c) {
|
||||
return a - b - c;
|
||||
}
|
||||
|
||||
set (a, b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
static set (a, b) {
|
||||
return a / b;
|
||||
}
|
||||
}
|
||||
|
||||
assert(A.get(1, 2, 3) === -4);
|
||||
assert(A.set(2, 1) === 2);
|
||||
|
||||
var a = new A;
|
||||
|
||||
assert(a.get(1, 2, 3) === 6);
|
||||
assert(a.set(2, 2) === 4);
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/* 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 g = Array.bind (0, 1, 2, 3)
|
||||
g.prototype = Array.prototype;
|
||||
|
||||
class C extends g {}
|
||||
|
||||
class D extends C {
|
||||
constructor () {
|
||||
super (4, 5);
|
||||
}
|
||||
}
|
||||
|
||||
var d = new D;
|
||||
assert (Object.getPrototypeOf (d) == D.prototype);
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
/* 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 isInstanceofArray (instance) {
|
||||
assert (instance instanceof C);
|
||||
assert (instance instanceof B);
|
||||
assert (instance instanceof A);
|
||||
assert (instance instanceof Array);
|
||||
}
|
||||
|
||||
class A extends Array {
|
||||
f () {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
g () {
|
||||
return eval ("eval ('super.f ()')");
|
||||
}
|
||||
}
|
||||
|
||||
class C extends B {
|
||||
h () {
|
||||
return eval ('super.g ()');
|
||||
}
|
||||
}
|
||||
|
||||
var c = new C (1, 2, 3, 4, 5, 6);
|
||||
|
||||
isInstanceofArray (c);
|
||||
c.push (7);
|
||||
assert (c.length === 7);
|
||||
assert (c.f () === 5);
|
||||
assert (c.g () === 5);
|
||||
assert (c.h () === 5);
|
||||
|
||||
// Test built-in Array prototype methods
|
||||
var mapped = c.map ((x) => x * 2);
|
||||
isInstanceofArray (mapped);
|
||||
|
||||
for (var i = 0; i < mapped.length; i++) {
|
||||
assert (mapped[i] == c[i] * 2);
|
||||
}
|
||||
|
||||
var concated = c.concat (c);
|
||||
isInstanceofArray (concated);
|
||||
|
||||
for (var i = 0; i < concated.length; i++) {
|
||||
assert (concated[i] == c[i % (concated.length / 2)]);
|
||||
}
|
||||
|
||||
var sliced = c.slice (c);
|
||||
isInstanceofArray (sliced);
|
||||
|
||||
for (var i = 0; i < sliced.length; i++) {
|
||||
assert (sliced[i] == c[i]);
|
||||
}
|
||||
|
||||
var filtered = c.filter ((x) => x > 100);
|
||||
isInstanceofArray (sliced);
|
||||
assert (filtered.length === 0);
|
||||
|
||||
var spliced = c.splice (c.length - 1);
|
||||
isInstanceofArray (spliced);
|
||||
assert (spliced.length === 1);
|
||||
assert (spliced[0] === 7);
|
||||
|
||||
c.constructor = 5;
|
||||
|
||||
try {
|
||||
mapped = c.map ((x) => x * 2);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
concated = c.concat (c);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
sliced = c.slice (c);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
filtered = c.filter ((x) => x > 100);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
spliced = c.splice (0);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/* 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 isInstanceofTypedArray (instance) {
|
||||
assert (instance instanceof C);
|
||||
assert (instance instanceof B);
|
||||
assert (instance instanceof A);
|
||||
assert (instance instanceof Uint8Array);
|
||||
}
|
||||
|
||||
class A extends Uint8Array {
|
||||
f () {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
g () {
|
||||
return super.f ();
|
||||
}
|
||||
}
|
||||
|
||||
class C extends B {
|
||||
h () {
|
||||
return super.g ();
|
||||
}
|
||||
}
|
||||
|
||||
var c = new C ([1, 2, 3, 4, 5, 6]);
|
||||
|
||||
isInstanceofTypedArray (c);
|
||||
assert (c.length === 6);
|
||||
assert (c.f () === 5)
|
||||
assert (c.g () === 5)
|
||||
assert (c.h () === 5)
|
||||
|
||||
/* TODO: Enable these tests after Symbol has been implemented
|
||||
var mapped = c.map ((x) => x * 2);
|
||||
isInstanceofTypedArray (mapped);
|
||||
|
||||
for (var i = 0; i < mapped.length; i++) {
|
||||
assert (mapped[i] == c[i] * 2);
|
||||
}
|
||||
|
||||
var filtered = c.filter ((x) => x > 100);
|
||||
isInstanceofTypedArray (filtered);
|
||||
assert (filtered.length === 0);
|
||||
|
||||
c.constructor = 5;
|
||||
|
||||
try {
|
||||
mapped = c.map ((x) => x * 2);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
|
||||
try {
|
||||
filtered = c.filter ((x) => x > 100);
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError)
|
||||
}
|
||||
*/
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user