Re-target for ES.Next (#3901)

A list of changes:
- 'es2015-subset' profile is deprecated, and an 'es.next' profile is added.
- The default profile is changed to 'es.next'
- Renamed the JERRY_ES2015 guard to JERRY_ESNEXT
- Renamed JERRY_ES2015_BUILTIN_* guards to JERRY_BUILTIN_*
- Moved es2015 specific tests to a new 'es.next' subdirectory
- Updated docs, targets, and test runners to reflect these changes

Resolves #3737.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2020-06-12 17:55:00 +02:00
committed by GitHub
parent c0270c4887
commit fde0d556ac
832 changed files with 3053 additions and 3046 deletions
+111
View File
@@ -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]);
+50
View File
@@ -0,0 +1,50 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var 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);
+344
View File
@@ -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());
+48
View File
@@ -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);
@@ -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");
}
+78
View File
@@ -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);
+313
View File
@@ -0,0 +1,313 @@
// 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]");
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);
}) ();
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 ReferenceError);
}
@@ -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 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]);
@@ -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
View File
@@ -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]);
@@ -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
View File
@@ -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);
@@ -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]");
@@ -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,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]");
+184
View File
@@ -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);
+79
View File
@@ -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);
+37
View File
@@ -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);
+46
View File
@@ -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)
+41
View File
@@ -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()
+186
View File
@@ -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.
*/
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,) => 0");
must_throw ("(x==6) => 0");
must_throw ("(x y) => 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);
+40
View File
@@ -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);
+52
View File
@@ -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,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);
+117
View File
@@ -0,0 +1,117 @@
/* 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 = [
String.prototype,
Boolean.prototype,
Number.prototype,
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 {
String.prototype.toString();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Boolean.prototype.valueOf();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
Number.prototype.valueOf();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
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);
}
try {
RegExp.prototype.source;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
RegExp.prototype.global;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
RegExp.prototype.ignoreCase;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
RegExp.prototype.multiline;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
RegExp.prototype.sticky;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
try {
RegExp.prototype.unicode;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
@@ -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);
@@ -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);
@@ -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);
}
@@ -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)
}
*/
@@ -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.
*/
class Animal {
constructor (name) {
this.name = name;
}
hello () {
return "Hello I am " + this.name;
}
static speak () {
return "Animals roar.";
}
static explain () {
return "I can walk,";
}
whoAmI () {
return "I am an Animal.";
}
breath () {
return "I am breathing.";
}
get myName () {
return this.name;
}
set rename (name) {
this.name = name;
}
}
class Dog extends Animal {
constructor (name, barks) {
super (name);
this.barks = barks;
}
hello () {
return super.hello () + " and I can " + (this.barks ? "bark" : "not bark");
}
whoAmI () {
return "I am a Dog.";
}
static speak () {
return "Dogs bark.";
}
static explain () {
return super.explain () + " jump,";
}
bark () {
return this.barks ? "Woof" : "----";
}
}
class Doge extends Dog {
constructor (name, barks, awesomeness) {
super (name, barks);
this.awesomeness = awesomeness;
}
hello () {
return super.hello () + " and I'm " + (this.awesomeness > 9000 ? "super awesome" : "awesome") + ".";
}
whoAmI ( ) {
return "I am a Doge.";
}
static speak () {
return "Doges wow.";
}
static explain () {
return super.explain () + " dance.";
}
}
var doge = new Doge ("doggoe", true, 10000);
assert (doge.name === "doggoe");
doge.rename = "doggo";
assert (doge.myName === "doggo");
assert (doge.barks === true);
assert (doge.awesomeness === 10000);
assert (doge.hello () === "Hello I am doggo and I can bark and I'm super awesome.");
assert (doge.whoAmI () === "I am a Doge.");
assert (doge.breath () === "I am breathing.");
assert (doge.bark () === "Woof");
assert (Doge.speak () === "Doges wow.");
assert (Doge.explain () === "I can walk, jump, dance.");
assert (doge instanceof Animal);
assert (doge instanceof Dog);
assert (doge instanceof Doge);
assert (Dog.prototype.constructor === Dog)
assert (Doge.prototype.constructor === Doge)
@@ -0,0 +1,29 @@
/* 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 extends Array {
constructor () {
return null;
}
}
class B extends A { }
try {
new B;
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
@@ -0,0 +1,30 @@
/* 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 extends Array {
constructor () {
assert (false);
return null;
}
}
class B extends A {
constructor () {
return { o : 10 };
}
}
var b = new B;
assert (b.o === 10);
@@ -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.
*/
class A extends Array {
constructor (a, b, c) {
eval ("eval ('super (a, b, c)')");
eval ("eval ('this.f = 5;')");
assert (this.h ()()() === 5);
return { o : 4 };
}
g () {
return function () {
return 5;
}
}
}
class B extends A {
constructor (a, b, c) {
eval ("eval ('super (a, b, c)')");
assert (this.f === undefined)
assert (this.o === 4)
this.k = 5;
return { o : 7 };
}
h () {
return super["g"];
}
}
var b = new B (1, 2, 3, 4);
assert (b.k === undefined);
assert (b.o === 7);
assert (b.h === undefined);
assert (b.g === undefined);
@@ -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.
*/
class C extends Array {
constructor (a, b) {
var a = eval ('super (arguments);');
}
}
class D extends C {
constructor () {
var a = eval ("eval ('super (1, 2);')");
return
}
}
var d = new D;
assert (JSON.stringify (d) === '[{"0":1,"1":2}]');
assert (d + "" === "[object Arguments]");
assert (d.length === 1);
@@ -0,0 +1,53 @@
/* 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 {
constructor () {
this.a = 5;
}
f () {
return 10;
}
super () {
this.super = 10;
return 15;
}
}
class B extends A {
constructor () {
super ();
assert (super.f === A.prototype.f);
super.f = 8;
assert (this.f === 8);
assert (super.f === A.prototype.f);
assert (this.a === 5);
super.a = 10;
assert (this.a === 10);
assert (super.super () === 15);
assert (this.super === 10);
super.super = 20;
assert (this.super === 20);
assert (super.super () === 15);
}
}
var b = new B;
assert (b.f === 8);
assert (b.a === 10);
@@ -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.
*/
class A extends Array {
constructor () {
super ();
return;
}
};
var a = new A;
assert (a.length === 0);
assert (a instanceof Array);
assert (a instanceof A);
assert (JSON.stringify (a) === "[]");
@@ -0,0 +1,38 @@
/* 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 C {
static a () {
return 5;
}
}
class D extends C {
constructor () {
super ();
}
}
assert (D.a () === 5);
C.a = function () {
return 6;
}
assert (D.a () === 6);
C = 5;
assert (D.a () === 6);
@@ -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.
*/
class A extends Array {
constructor (a, b, c) {
super (a, b);
this.f = 5;
}
}
class B extends A { }
var b = new B (1, 2, 3, 4);
assert (b.f === 5);
assert (b.length === 2);
@@ -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.
*/
class A extends Array {
constructor (a, b, c) {
super (a, b);
this.f = 5;
}
}
class B extends A {
constructor (a, b, c) {
super (a, b);
this.g = super.f;
this.h = this.f;
}
}
var b = new B (1, 2, 3, 4);
assert (b.g === undefined);
assert (b.h === 5);
assert (b.length === 2);
@@ -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.
*/
class A extends Array {
constructor (a, b, c) {
eval ("eval ('super (a, b, c)')");
eval ("eval ('this.f = 5;')");
}
}
class B extends A { }
var b = new B (1, 2, 3, 4);
assert (b.f === 5);
assert (b.length === 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.
*/
class A extends Array {
constructor (a, b, c, d, e) {
eval ("eval ('super (a, b, c)')");
this.a = 6;
return new String ("foo");
}
f () {
return 5;
}
}
class B extends A {
constructor (a, b, c, d) {
eval ("eval ('super (a, b, c, d)')");
assert (super.f () === 5);
}
}
var a = new B (1, 2, 3, 4, 5, 6);
assert (a.a === undefined);
assert (a[0] + a[1] + a[2] === "foo");
@@ -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.
*/
class C extends Array {
constructor () {
var a = eval ('super (1, 2); 5');
assert (a === 5);
}
}
class D extends C {
constructor () {
var a = eval ("eval ('super (1, 2); 3')");
assert (a === 3);
}
}
var d = new D;
assert (JSON.stringify (d) === "[1,2]");
@@ -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.
*/
var A = Array;
var order = 0;
function f () {
order++;
return function () {
return A;
}
}
var B = class extends f ()() {
constructor () {
assert (++order === 2);
eval ("eval ('super (1, 2, 3, 4)')");
try {
super (1, 2, 3, 4, 5)
assert (false);
} catch (e) {
assert (e instanceof ReferenceError)
}
assert (this.g ()()()() === 10);
assert (eval ("eval ('this.g ()')()")()() === 10);
assert (eval ("eval ('this.g ()')")()()() === 10);
assert (eval ("eval ('this.g ()()()')")() === 10);
assert (eval ("eval ('this.g')")()()()() === 10);
this.push (5);
assert (this.length === 5)
eval ('this.push (6)');
assert (this.length === 6);
eval ("eval ('this.push (7)')");
this.j = 6;
return;
}
}
var C = class extends B {
g () {
return function () {
return () => {
return 10;
}
}
}
}
var D = class D extends C {
constructor () {
super ();
this.k = 5;
return
}
g () {
return eval ('super["g"]');
}
}
assert (order === 1);
var d = new D;
assert (d.length === 7);
assert (d.k === 5);
assert (d.j === 6);
assert (d instanceof D);
assert (d instanceof C);
assert (d instanceof B);
assert (d instanceof f ()());
assert (JSON.stringify (d) === "[1,2,3,4,5,6,7]");
@@ -0,0 +1,29 @@
/* 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 order = 0;
try {
var A = class extends null {
constructor () {
order++;
}
}
new A;
} catch (e) {
assert (order === 1);
assert (e instanceof ReferenceError);
}
@@ -0,0 +1,141 @@
/* 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) { }
try {
eval ("'use strict'; switch (1) { default: " + str + "}");
assert (false);
} catch (e) { }
try {
eval ("'use strict'; " + str);
assert (false);
} catch (e) { }
}
class A {
constructor (a) {
this.a = a;
}
f () {
return 5;
}
}
must_throw ("class B extends 5 + 6 + 5 { constructor (a, b) { super (a) } }");
must_throw ("class B extends null { constructor () { super () } }; new B");
must_throw ("var o = { a : 5 }; \
class B extends Object.keys (o)[0] { constructor (a, b) { super (a) } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { this.b = b} } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { super.f () } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { eval ('this.b = b') } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { eval ('super.f ()') } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { super (a); super (a); } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { eval ('super (a)'); eval ('super (a)'); } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { super (a) } g () { super (a) } } \
var b = new B (1, 2);");
must_throw ("class B extends A { constructor (a, b) { super (a) } g () { eval ('super (a)') } } \
var b = new B (1, 2); \
b.g ();");
must_throw ("class B extends A { constructor (a, b) { super (a) } g () { return function () { return super.f () } } } \
var b = new B (1, 2); \
b.g ()();");
must_throw ("class B extends A { constructor (a, b) { super (a) } \
g () { return function () { return eval ('super.f ()') } } } \
var b = new B (1, 2); \
b.g ()();");
must_throw ("class B extends A { constructor (a, b) { super (a) } \
g () { return function () { return eval (\"eval ('super.f ();')\") } } } \
var b = new B (1, 2); \
b.g ()();");
must_throw ("class A extends Array { constructor () { return 5; } }; new A");
must_throw ("class A extends Array { constructor () { return undefined; } }; new A");
must_throw ("class B extends undefined { }; new B;");
must_throw ("var A = class extends Array { . }");
must_throw ("class Array extends Array { }");
must_throw ("class A extends A { }");
must_throw ("class A extends { constructor () { super () } }");
must_throw ("class A extends a * b {}");
must_throw ("class A extends a = b {}");
must_throw ("class A extends a++ {}");
must_throw ("class A extends -a {}");
class B extends A {
constructor (a, b) {
super (a);
assert (super.f () === 5);
}
g () {
return () => {
return super.f ();
}
}
h () {
return () => {
return () => {
return eval ('super.f ()');
}
}
}
}
var b = new B (1, 2);
assert (b.g ()() === 5);
assert (b.h ()()() === 5);
@@ -0,0 +1,47 @@
/* 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.
*/
/* Create bound implicit class constructor */
class myArray extends Array { };
var array = new myArray (1);
array.push (2);
assert (array.length === 2);
assert (array instanceof myArray);
assert (array instanceof Array);
assert (!([] instanceof myArray));
/* Add a new element to the bound function chain */
class mySecretArray extends myArray { };
var secretArray = new mySecretArray (1, 2);
secretArray.push (3);
assert (secretArray.length === 3);
assert (secretArray instanceof mySecretArray);
assert (secretArray instanceof myArray);
assert (secretArray instanceof Array);
assert (!([] instanceof mySecretArray));
/* Add a new element to the bound function chain */
class myEpicSecretArray extends mySecretArray { };
var epicSecretArray = new myEpicSecretArray (1, 2, 3);
epicSecretArray.push (4);
assert (epicSecretArray.length === 4);
assert (epicSecretArray instanceof myEpicSecretArray);
assert (epicSecretArray instanceof mySecretArray);
assert (epicSecretArray instanceof myArray);
assert (epicSecretArray instanceof Array);
assert (!([] instanceof myEpicSecretArray));
@@ -0,0 +1,62 @@
/* 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.
*/
// TODO: enable this test when super keyword is supported for normal functions
/*
var console = { assert : assert };
class C1 {
f () {
return 5;
}
}
class C2 extends C1 {
f () {
assert (super.f () === 5);
class G {
g () {
assert (super.f === undefined);
assert (super.toString () === "[object Object]");
var a = super.valueOf ();
try {
a ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
}
constructor () {
// Test to overwrite the current lit-object
console.assert (Object.getPrototypeOf (this) === G.prototype);
try {
eval ("super ()");
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
}
}
var g = new G ();
g.g ();
}
}
(new C2).f ();
*/
@@ -0,0 +1,38 @@
/* 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 calculatorMixin = Base => class extends Base {
f () {
return 1;
}
};
var randomizerMixin = Base => class extends Base {
g () {
return 2;
}
};
class A {
constructor () { }
}
class B extends calculatorMixin (randomizerMixin (A)) {
}
var b = new B ();
assert (b.f () === 1)
assert (b.g () === 2);
@@ -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.
*/
order = 0;
var Mixin1 = (superclass) => class extends superclass {
foo () {
assert (order++ == 1)
if (super.foo) {
super.foo ();
}
}
};
var Mixin2 = (superclass) => class extends superclass {
foo () {
assert (order++ == 2)
if (super.foo) {
assert (super.foo () === 5);
}
}
};
class S {
foo () {
assert (order++ == 3)
return 5;
}
}
class C extends Mixin1 (Mixin2 (S)) {
foo () {
assert (order++ == 0)
super.foo ();
}
}
new C ().foo ()
@@ -0,0 +1,102 @@
// 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 Base {
constructor () {
this.parent_value = 100;
}
parent_value () {
return this.parent_value;
}
parent_value_arg (a, b, c) {
if (c) {
return this.parent_value + a + b + c;
} else if (b) {
return this.parent_value + a + b;
} else {
return this.parent_value + a;
}
}
method () {
return {
method: function (a, b, c, d, e) { return -50 + a + b + c + d + e; }
}
}
}
class Target extends Base {
constructor () {
super ();
this.parent_value = -10;
}
parent_value () {
throw new Error ('(parent_value)');
}
parent_value_direct () {
return super.parent_value ();
}
parent_value_direct_arg (a, b, c) {
if (c) {
return super.parent_value_arg (a, b, c);
} else if (b) {
return super.parent_value_arg (a, b);
} else {
return super.parent_value_arg (a);
}
}
method () {
throw new Error ("(method)");
}
parent_method_dot () {
return super.method ().method (1, 2, 3, 4, 5)
}
parent_method_index () {
return super['method']()['method'](1, 2, 3, 4, 5);
}
}
var obj = new Target ();
assert (obj.parent_value_direct () === -10);
assert (obj.parent_value_direct_arg (1) === -9);
assert (obj.parent_value_direct_arg (1, 2) === -7);
assert (obj.parent_value_direct_arg (1, 2, 3) === -4);
try {
obj.parent_value();
assert (false)
} catch (ex) {
/* 'obj.parent_value is a number! */
assert (ex instanceof TypeError);
}
assert (obj.parent_method_dot () === -35);
assert (obj.parent_method_index () === -35);
try {
obj.method ();
assert (false);
} catch (ex) {
assert (ex.message === '(method)');
}
@@ -0,0 +1,114 @@
// 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 Base {
constructor () {
this.parent_value = 100;
}
parent_value () {
return this.parent_value;
}
parent_value_arg (a, b, c) {
if (c) {
return this.parent_value + a + b + c;
} else if (b) {
return this.parent_value + a + b;
} else {
return this.parent_value + a;
}
}
method () {
return {
method: function (a, b, c, d, e) { return -50 + a + b + c + d + e; }
}
}
}
class Target extends Base {
constructor () {
super ();
this.parent_value = -10;
}
parent_value () {
throw new Error ('(parent_value)');
}
parent_value_indirect () {
return super.parent_value.call (this);
}
parent_value_indirect_arg (a, b, c) {
if (c) {
return super.parent_value_arg.call (this, a, b, c);
} else if (b) {
return super.parent_value_arg.call (this, a, b);
} else {
return super.parent_value_arg.call (this, a);
}
}
method () {
throw new Error ("(method)");
}
parent_method_dot () {
return super.method.call (this).method (1, 2, 3, 4, 5)
}
parent_method_index() {
return super['method'].call (this)['method'] (1, 2, 3, 4, 5);
}
}
var obj = new Target();
assert (obj.parent_value_indirect () === -10);
assert (obj.parent_value_indirect_arg (1) === -9);
assert (obj.parent_value_indirect_arg (1, 2) === -7);
assert (obj.parent_value_indirect_arg (1, 2, 3) === -4);
try {
obj.parent_value ();
assert (false);
} catch (ex) {
/* 'obj.parent_value is a number! */
assert (ex instanceof TypeError);
}
assert (obj.parent_method_dot () === -35);
assert (obj.parent_method_index () === -35);
try {
obj.method();
assert (false);
} catch (ex) {
assert (ex.message === '(method)');
}
var demo_object = {
parent_value: 1000,
method: function () {
throw new Error ('Very bad!');
}
}
assert (obj.parent_value_indirect_arg.call (demo_object, 1) === 1001);
assert (obj.parent_value_indirect_arg.call (demo_object, 1, 2) === 1003);
assert (obj.parent_method_dot.call (demo_object) === -35);
+334
View File
@@ -0,0 +1,334 @@
/* 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) {
}
must_throw_strict(str);
}
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) {
}
}
must_throw("class {}");
must_throw("class class {}");
must_throw("class A { constructor() {} this.a = 5 }");
must_throw("class A { constructor() {} constructor() {} }");
must_throw("class A { static prototype() {} }");
must_throw("class A { get constructor() {} }");
must_throw("class A { set constructor() {} }");
must_throw("class A {}; A()");
must_throw("class X {}; var o = {}; Object.defineProperty(o, 'p', { get: X, set: X }); o.p;");
must_throw("var a = new A; class A {};");
must_throw("class A { g\\u0065t e() {} }");
must_throw('class A { "static" e() {} }');
must_throw('class A { *constructor() {} }');
assert(eval("class A {}") === undefined);
assert(eval("var a = class A {}") === undefined);
assert(eval("var a = class {}") === undefined);
assert(eval("class A { ; ; ; ;;;;;;;;;;;; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;; }") === undefined);
assert(eval('class A {"constructor"() {} }') === undefined);
assert(isNaN (eval('switch(1) { default: (class A{} % 1) }')));
class A1 {
["constructor"]() {
return 5;
}
}
assert ((new A1).constructor() === 5);
class A2 {
*["constructor"]() {
yield 5;
}
}
assert ((new A2).constructor().next().value === 5);
class B {
}
var b = new B;
assert (typeof B === "function");
assert (typeof b === "object");
assert (b.constructor === B);
class C {
c1() {
return 5;
}
c2() {
return this._c;
}
3() {
return 3;
}
super() {
return 42;
}
return() {
return 43;
}
static *constructor() {
return 44;
}
}
var c = new C;
assert (c.c1() === 5);
assert (c.c2() === undefined);
assert (c["3"]() === 3);
assert (c.super() === 42);
assert (c.return() === 43);
assert (c.constructor === C);
assert (C.constructor().next().value === 44);
class D {
constructor(d) {
this._d = d;
}
d1() {
return this._d;
}
}
var d = new D(5);
assert (d.d1() === 5);
assert (d.constructor === D);
class E {
constructor(e) {
this._e = e;
}
get e() {
return this._e;
}
set e(e) {
this._e = e;
}
get () {
return 11;
}
set () {
return 12;
}
}
var e = new E (5);
assert (e.e === 5);
e.e = 10;
assert (e.e === 10);
assert (e.get() === 11);
assert (e.set() === 12);
assert (e.constructor === E);
var F = class ClassF {
constructor(f) {
this._f = f;
}
static f1() {
return this;
}
static f2() {
return this._f;
}
static f3(a, b) {
return a + b;
}
static constructor(a) {
return a;
}
static static(a) {
return a;
}
static 2 (a) {
return 2 * a;
}
static function(a) {
return 3 * a;
}
}
var f = new F(5);
assert (f.f1 === undefined);
assert (f.f2 === undefined);
assert (F.f1() === F);
assert (F.f2() === undefined);
assert (F.f3(1, 1) === 2);
assert (F.constructor(5) === 5);
assert (F.static(5) === 5);
assert (F["2"](5) === 10);
assert (F.function(5) === 15);
assert (f.constructor === F);
var G = class {
static set a(a) {
this._a = a;
}
static get a() {
return this._a;
}
static set 1(a) {
this._a = a;
}
static get 1() {
return this._a;
}
static get() {
return 11;
}
static set() {
return 12;
}
static set constructor(a) {
this._a = a;
}
static get constructor() {
return this._a;
}
static g1() {
return 5;
}
static g1() {
return 10;
}
}
G.a = 10;
assert (G.a === 10);
assert (G.g1() === 10);
G["1"] = 20;
assert (G["1"] === 20);
assert (G.get() == 11);
assert (G.set() == 12);
G.constructor = 30;
assert (G.constructor === 30);
class H {
method() { assert (typeof H === 'function'); return H; }
}
let H_original = H;
var H_method = H.prototype.method;
C = undefined;
assert(C === undefined);
C = H_method();
assert(C === H_original);
var I = class C {
method() { assert(typeof C === 'function'); return C; }
}
let I_original = I;
var I_method = I.prototype.method;
I = undefined;
assert(I === undefined);
I = I_method();
assert(I == I_original);
var J_method;
class J {
static [(J_method = eval('(function() { return J; })'), "X")]() {}
}
var J_original = J;
J = 6;
assert (J_method() == J_original);
var K_method;
class K {
constructor () {
K_method = function() { return K; }
}
}
var K_original = K;
new K;
K = 6;
assert (K_method() == K_original);
var L_method;
class L extends (L_method = function() { return L; }) {
}
var L_original = L;
L = 6;
assert (L_method() == L_original);
/* Test cleanup class environment */
try {
class A {
[d]() {}
}
let d;
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
try {
class A extends d {}
let d;
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
try {
var a = 1 + 2 * 3 >> class A extends d {};
let d;
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
+74
View File
@@ -0,0 +1,74 @@
/* 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.
*/
const a = 6
assert(!delete a)
assert(a === 6)
try {
eval("a = 7")
assert(false)
} catch (e) {
assert(e instanceof TypeError)
}
function check_type_error(code) {
try {
eval(code)
assert(false)
} catch (e) {
assert(e instanceof TypeError)
}
}
// Register cases
check_type_error("{ const a = 0; a = 1 }");
check_type_error("{ const a = 0; a += 1 }");
check_type_error("{ const a = 0; a -= 1 }");
check_type_error("{ const a = 0; a *= 1 }");
check_type_error("{ const a = 0; a %= 1 }");
check_type_error("{ const a = 0; a /= 1 }");
check_type_error("{ const a = 0; a <<= 1 }");
check_type_error("{ const a = 0; a >>= 1 }");
check_type_error("{ const a = 0; a >>>= 1 }");
check_type_error("{ const a = 0; a &= 1 }");
check_type_error("{ const a = 0; a |= 1 }");
check_type_error("{ const a = 0; a ^= 1 }");
check_type_error("{ const a = 0; a++ }");
check_type_error("{ const a = 0; a-- }");
check_type_error("{ const a = 0; ++a }");
check_type_error("{ const a = 0; --a }");
check_type_error("{ const a = 0; [a] = [1] }");
check_type_error("{ const a = 0; ({a} = { a:1 }) }");
// Non-register cases
check_type_error("{ const a = 0; (function (){ a = 1 })() }");
check_type_error("{ const a = 0; (function (){ a += 1 })() }");
check_type_error("{ const a = 0; (function (){ a -= 1 })() }");
check_type_error("{ const a = 0; (function (){ a *= 1 })() }");
check_type_error("{ const a = 0; (function (){ a %= 1 })() }");
check_type_error("{ const a = 0; (function (){ a /= 1 })() }");
check_type_error("{ const a = 0; (function (){ a <<= 1 })() }");
check_type_error("{ const a = 0; (function (){ a >>= 1 })() }");
check_type_error("{ const a = 0; (function (){ a >>>= 1 })() }");
check_type_error("{ const a = 0; (function (){ a &= 1 })() }");
check_type_error("{ const a = 0; (function (){ a |= 1 })() }");
check_type_error("{ const a = 0; (function (){ a ^= 1 })() }");
check_type_error("{ const a = 0; (function (){ a++ })() }");
check_type_error("{ const a = 0; (function (){ a-- })() }");
check_type_error("{ const a = 0; (function (){ ++a })() }");
check_type_error("{ const a = 0; (function (){ --a })() }");
check_type_error("{ const a = 0; (function (){ [a] = [1] })() }");
check_type_error("{ const a = 0; (function (){ ({a} = { a:1 }) })() }");
+44
View File
@@ -0,0 +1,44 @@
/* 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.
*/
for (var i = 0; i < 1000; i++)
{
switch (1)
{
default:
/* This block must not be enclosed in braces. */
let j = eval();
continue;
}
}
next:
for (var i = 0; i < 1000; i++)
{
for (let j = eval(); true; )
{
continue next;
}
}
next:
for (var i = 0; i < 1000; i++)
{
for (let j in {a:1})
{
eval()
continue next;
}
}
+228
View File
@@ -0,0 +1,228 @@
/* 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.
*/
/* ES2015 24.2.2.1.1 */
try {
DataView ();
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
/* ES2015 24.2.2.1.2 */
try {
new DataView (5);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
/* ES2015 24.2.2.1.3 */
try {
new DataView ({});
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var buffer = new ArrayBuffer (16);
/* ES2015 24.2.2.1.6 */
try {
new DataView (buffer, { toString: function () { throw new ReferenceError ('foo') } });
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message === 'foo');
}
/* ES2015 24.2.2.1.7 (numberOffset != offset)*/
try {
new DataView (buffer, 1.5);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* ES2015 24.2.2.1.7 (offset < 0) */
try {
new DataView (buffer, -1);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* ES2015 24.2.2.1.10 */
try {
new DataView (buffer, 17);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* ES2015 24.2.2.1.12.b */
try {
new DataView (buffer, 0, { toString: function () { throw new ReferenceError ('bar') } });
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message === 'bar');
}
/* ES2015 24.2.2.1.12.b */
try {
new DataView (buffer, 0, Infinity);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* ES2015 24.2.2.1.12.c */
try {
new DataView (buffer, 4, 13);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* Tests accessors: ES2015 24.2.2.1 - 24.2.2.3 */
var accessorList = ['buffer', 'byteOffset', 'byteLength'];
accessorList.forEach (function (prop) {
/* ES2015 24.2.4.{1, 2, 3}.{2, 3} */
try {
var obj = {};
Object.setPrototypeOf (obj, DataView.prototype);
obj[prop];
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
});
buffer = new ArrayBuffer (32);
var view1 = new DataView (buffer);
assert (view1.buffer === buffer);
assert (view1.byteOffset === 0);
assert (view1.byteLength === buffer.byteLength);
var view2 = new DataView (buffer, 8);
assert (view2.buffer === buffer);
assert (view2.byteOffset === 8);
assert (view2.byteLength === buffer.byteLength - view2.byteOffset);
var view3 = new DataView (buffer, 8, 16);
assert (view3.buffer === buffer);
assert (view3.byteOffset === 8);
assert (view3.byteLength === 16);
/* Test get/set routines */
var getters = ['getInt8', 'getUint8', 'getInt16', 'getUint16', 'getInt32', 'getUint32', 'getFloat32', 'getFloat64'];
var setters = ['setInt8', 'setUint8', 'setInt16', 'setUint16', 'setInt32', 'setUint32', 'setFloat32', 'setFloat64'];
var gettersSetters = getters.concat (setters);
gettersSetters.forEach (function (propName) {
/* ES2015 24.2.1.{1, 2}.1 */
var routine = DataView.prototype[propName];
try {
DataView.prototype[propName].call (5);
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
/* ES2015 24.2.1.{1, 2}.2 */
try {
DataView.prototype[propName].call ({});
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
/* ES2015 24.2.1.{1, 2}.5 */
try {
var buffer = new ArrayBuffer (16);
var view = new DataView (buffer)
view[propName] ({ toString : function () { throw new ReferenceError ('fooBar') } });
assert (false);
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message == 'fooBar');
}
var buffer = new ArrayBuffer (16);
var view = new DataView (buffer)
/* ES2015 24.2.1.{1, 2}.6 (numberIndex != getIndex) */
if (propName.indexOf("get") !== -1) {
assert(view[propName] (1.5) === 0);
} else {
assert(view[propName] (1.5) === undefined);
}
/* ES2015 24.2.1.{1, 2}.6 (getIndex < 0) */
try {
view[propName] (-1);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
/* ES2015 24.2.1.{1, 2}.13 */
try {
view[propName] (20);
assert (false);
} catch (e) {
assert (e instanceof RangeError);
}
});
/* Test the endianness */
function validateResult (view, offset, isLitteEndian, results) {
for (var i = 0; i < getters.length; i++) {
assert (results[i] === view[getters[i]](offset, isLitteEndian));
}
}
buffer = new ArrayBuffer (24);
view1 = new DataView (buffer);
view2 = new DataView (buffer, 4, 12);
view3 = new DataView (buffer, 6, 18);
view1.setUint8 (0, 255);
validateResult(view1, 0, false, [-1, 255, -256, 65280, -16777216, 4278190080, -1.7014118346046924e+38, -5.486124068793689e+303]);
validateResult(view1, 0, true, [-1, 255, 255, 255, 255, 255, 3.5733110840282835e-43, 1.26e-321]);
validateResult(view1, 2, false, [0, 0, 0, 0, 0, 0, 0, 0]);
validateResult(view1, 2, true, [0, 0, 0, 0, 0, 0, 0, 0]);
view1.setInt16 (4, 40000);
validateResult(view1, 4, false, [-100, 156, -25536, 40000, -1673527296, 2621440000, -6.352747104407253e-22, -1.2938158758247024e-172]);
validateResult(view2, 0, false, [-100, 156, -25536, 40000, -1673527296, 2621440000, -6.352747104407253e-22, -1.2938158758247024e-172]);
validateResult(view1, 4, true, [-100, 156, 16540, 16540, 16540, 16540, 2.3177476599932474e-41, 8.172e-320]);
validateResult(view2, 0, true, [-100, 156, 16540, 16540, 16540, 16540, 2.3177476599932474e-41, 8.172e-320]);
view2.setUint32 (2, 3000000000, true);
validateResult(view2, 2, false, [0, 0, 94, 94, 6213810, 6213810, 8.707402410606192e-39, 6.856613170926581e-307]);
validateResult(view3, 0, false, [0, 0, 94, 94, 6213810, 6213810, 8.707402410606192e-39, 6.856613170926581e-307]);
validateResult(view2, 2, true, [0, 0, 24064, 24064, -1294967296, 3000000000, -2.425713319098577e-8, 1.4821969375e-314]);
validateResult(view3, 0, true, [0, 0, 24064, 24064, -1294967296, 3000000000, -2.425713319098577e-8, 1.4821969375e-314]);
view3.setFloat32 (4, 8.5);
validateResult(view3, 4, false, [65, 65, 16648, 16648, 1091043328, 1091043328, 8.5, 196608]);
validateResult(view3, 4, true, [65, 65, 2113, 2113, 2113, 2113, 2.9609436551183385e-42, 1.044e-320]);
validateResult(view2, 4, false, [-48, 208, -12110, 53426, -793624312, 3501342984, -23924850688, -5.411000515087672e+80]);
validateResult(view2, 4, true, [-48, 208, -19760, 45776, 138523344, 138523344, 5.828901796903399e-34, 6.84396254e-316]);
@@ -0,0 +1,51 @@
/* 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 dateObj = new Date("1997-04-10");
var dateNaN = new Date(NaN);
// Test with default hint
var result = dateObj[Symbol.toPrimitive]("default");
assert(result.toString().substring(0,15) === "Thu Apr 10 1997");
result = dateNaN[Symbol.toPrimitive]("default");
assert(dateNaN == "Invalid Date");
// Test with number hint
result = dateObj[Symbol.toPrimitive]("number");
assert(result.toString() === "860630400000");
result = dateNaN[Symbol.toPrimitive]("number");
assert(isNaN(result) === true);
// Test with string hint
result = dateObj[Symbol.toPrimitive]("string");
assert(result.toString().substring(0,15) === "Thu Apr 10 1997");
result = dateNaN[Symbol.toPrimitive]("string");
assert(result == "Invalid Date");
// Test with invalid hint
try {
result = dateObj[Symbol.toPrimitive](90);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// Test when unable to call toPrimitive
try {
Date.prototype[Symbol.toPrimitive].call(undefined);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
+35
View File
@@ -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_syntax_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
eval("function f(a, b = 4) { }")
check_syntax_error ("function f(a, b = 4) { 'use strict' }")
eval('function f(...a) { }')
check_syntax_error ('function f(...a) { "use strict" }')
eval("({ f([a,b]) { } })")
check_syntax_error ("({ f([a,b]) { 'use strict' } })")
eval("function f(a, b = 4) { 'directive1'\n'directive2'\n }")
check_syntax_error ("function f(a, b = 4) { 'directive1'\n'directive2'\n'use strict' }")
+21
View File
@@ -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.
/* Prototype of NativeErrors should be Error */
assert(Object.getPrototypeOf(EvalError) === Error);
assert(Object.getPrototypeOf(RangeError) === Error);
assert(Object.getPrototypeOf(ReferenceError) === Error);
assert(Object.getPrototypeOf(SyntaxError) === Error);
assert(Object.getPrototypeOf(TypeError) === Error);
assert(Object.getPrototypeOf(URIError) === Error);
+73
View File
@@ -0,0 +1,73 @@
/* 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 two = 2
var three = 3
// Precedence (right-to-left) tests
assert(2 ** 3 ** 2 === 512)
assert((2 ** 3) ** 2 === 64)
assert(2 ** (3 ** 2) === 512)
assert(two ** three ** two === 512)
assert((two ** three) ** two === 64)
assert(two ** (three ** two) === 512)
assert(2 ** 2 * 3 ** 3 === 4 * 27)
assert(two ** two * three ** three === 4 * 27)
var a = 3
assert((a **= 3) === 27)
assert(a === 27)
a = 2
assert((a **= a **= 2) === 16)
assert(a === 16)
function assertThrows(src) {
try {
eval(src)
assert(false)
} catch (e) {
assert(e instanceof SyntaxError)
}
}
assertThrows("-2 ** 2")
assertThrows("+a ** 2")
assertThrows("!a ** 2")
assertThrows("~a ** 2")
assertThrows("void a ** 2")
assertThrows("typeof a ** 2")
assertThrows("delete a ** 2")
assertThrows("!(-a) ** 2")
assert((-2) ** 2 === 4)
a = 3
assert((-+-a) ** 3 === 27)
a = 0
assert((!a) ** 2 === 1)
a = 0
assert(isNaN((void a) ** 3))
a = -4
assert(++a ** 2 === 9)
assert(a === -3)
a = -2
assert(a-- ** 2 === 4)
assert(a === -3)
@@ -0,0 +1,30 @@
// 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_reference_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof ReferenceError)
}
}
check_reference_error("for (let x of [x]) {}")
check_reference_error("for (const x of [x]) {}")
check_reference_error("for (let x in {x}) {}")
check_reference_error("for (const x in {x}) {}")
+105
View File
@@ -0,0 +1,105 @@
// 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 arr = [];
for (let i = 0, j = 0; i < 10; j++)
{
arr[i] = function() { return i + j; }
i++;
}
for (let i = 0; i < 10; i++)
assert(arr[i]() === (i * 2 + 1));
var j = 0, k = 0;
for (let i = 0; j = i, i < 10; i++)
{
let i = -3;
assert(i === -3);
assert(j === k);
k++;
}
var j = 0, k = 0;
for (let i = 0; eval("j = i"), i < 10; i++)
{
let i = -3;
assert(i === -3);
assert(j === k);
k++;
}
var arr = [];
for (let i in { x:1, y:1, z:1 })
{
let str = "P";
arr.push(function () { return str + i; });
}
assert(arr[0]() === "Px");
assert(arr[1]() === "Py");
assert(arr[2]() === "Pz");
try {
for (let i in (function() { return i; })()) {}
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
try {
for (let i = 0, j = 0; i < 5; i++, j++)
{
if (i === 3)
{
eval("throw -42")
}
}
assert(false);
} catch (e) {
assert(e === -42);
}
exit: {
for (let i = 0, j = 0; i < 5; i++, j++)
{
if (eval("i === 3")) {
assert(i === 3);
break exit;
}
}
assert(false);
}
var f = null, g = null, h = null;
for (let i = 0;
f = function() { return i }, i < 1;
i++, g = function() { return i })
{
h = function() { return i };
}
assert(f() === 1);
assert(g() === 1);
assert(h() === 0);
var arr = [];
for (const i in { aa:4, bb:5, cc:6 })
{
arr.push(function () { return i });
}
assert(arr[0]() === "aa");
assert(arr[1]() === "bb");
assert(arr[2]() === "cc");
@@ -0,0 +1,244 @@
// 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 createIterable(arr, methods = {}) {
let iterable = function *() {
let idx = 0;
while (idx < arr.length) {
yield arr[idx];
idx++;
}
}();
iterable['return'] = methods['return'];
iterable['throw'] = methods['throw'];
return iterable;
};
function close1() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; return {}; }
});
for (var it of iter) break;
return closed;
}
assert(close1());
function close2() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; return {}; }
});
try {
for (var it of iter) throw 0;
assert(false);
} catch(e){
assert(e === 0);
}
return closed;
}
assert(close2());
function close3() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; return {}; }
});
for (var it of iter) continue;
return closed;
}
assert(!close3());
function close4() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; throw 6; }
});
try {
for (var it of iter) throw 5;
assert(false);
} catch(e) {
assert(e === 5);
}
return closed;
}
assert(close4());
function close5() {
var closed_called = 0;
var iter = createIterable([1, 2, 3], {
'return': function() { closed_called++; throw 6; }
});
try {
for (var it of iter) {
for (var it of iter) {
throw 5;
}
assert(false);
}
assert(false);
} catch(e) {
assert(e === 5);
}
return closed_called === 2;
}
assert(close5());
function close6() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; return {}; }
});
for (var it of iter) {};
return closed;
}
assert(!close6());
var close7_result = false;
function close7() {
var iter = createIterable([1, 2, 3], {
'return': function() { close7_result = true; throw "5"; }
});
for (var it of iter) {
return "foo";
}
}
try {
close7();
assert(false);
} catch (e) {
assert(close7_result);
assert(e === "5");
}
function close8() {
var iter = createIterable([1, 2, 3], {
'return': function() { close8_result = true; throw "5"; }
});
for (var it of iter) {
throw "foo";
}
}
var close8_result = false;
try {
close8();
assert(false);
} catch (e) {
assert(e === "foo");
assert(close8_result);
}
function close9() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; throw "5"; }
});
try {
for (var it of iter) {
break;
}
} finally {
assert(closed);
throw "foo"
}
}
try {
close9();
assert(false);
} catch (e) {
assert(e === "foo");
}
function close10() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; return {}; }
});
try {
for (var it of iter) {
return "foo";
}
} finally {
assert(closed);
throw "bar";
}
}
try {
close10();
assert(false);
} catch (e) {
assert(e === "bar");
}
function close11() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; throw "5"; }
});
try {
for (var it of iter) {
return "foo";
}
} finally {
assert(closed);
throw "bar";
}
}
try {
close11();
assert(false);
} catch (e) {
assert(e === "bar");
}
function close12() {
var closed = false;
var iter = createIterable([1, 2, 3], {
'return': function() { closed = true; throw "5"; }
});
try {
for (var it of iter) {
throw "foo";
}
} finally {
assert(closed);
throw "bar";
}
}
try {
close12();
assert(false);
} catch (e) {
assert(e === "bar");
}
+121
View File
@@ -0,0 +1,121 @@
// 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 parse (txt) {
try {
eval (txt)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
function checkError (obj) {
try {
for (var a of obj);
assert (false)
} catch (e) {
assert (e instanceof TypeError)
}
}
var arr = [1,2,3,4]
var forOf =
"for var prop of obj" +
" obj [prop] += 4"
parse (forOf)
var forOf =
"for [var prop of obj]" +
" obj[prop] += 4;"
parse (forOf)
var forOf =
"for (var prop obj)" +
" obj[prop] += 4;"
parse (forOf)
var forOf =
"foreach (var prop of obj)" +
" obj[prop] += 4;"
parse (forOf)
var forOf =
"for (var a \"of\" []) {}"
parse (forOf)
checkError(5)
var obj = {}
Object.defineProperty(obj, Symbol.iterator, { get : function () { throw TypeError ('foo');}});
checkError (obj);
var obj = {
[Symbol.iterator] : 5
}
checkError (obj);
var obj = {
[Symbol.iterator] () {
return 5
}
}
checkError(obj);
var obj = {
[Symbol.iterator] () {
return {}
}
}
checkError(obj);
var obj = {
[Symbol.iterator] () {
return {
next() {
return 5;
}
}
}
}
checkError(obj);
var array = [0, 1, 2, 3, 4, 5];
var i = 0;
for (var a of array) {
assert (a === i++);
}
var obj = {
[Symbol.iterator]() {
return {
counter : 0,
next () {
if (this.counter == 10) {
return { done : true, value : undefined };
}
return { done: false, value: this.counter++ };
}
}
}
}
var i = 0;
for (var a of obj) {
assert (a === i++);
}
+77
View File
@@ -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.
var idx = 0;
for (var [a,b] of [[1,2], [3,4]])
{
if (idx == 0)
{
assert(a === 1);
assert(b === 2);
idx = 1;
}
else
{
assert(a === 3);
assert(b === 4);
}
}
assert(a === 3);
assert(b === 4);
idx = 0;
for (let [a,b] of [[5,6], [7,8]])
{
if (idx == 0)
{
assert(a === 5);
assert(b === 6);
idx = 1;
}
else
{
assert(a === 7);
assert(b === 8);
}
}
assert(a === 3);
assert(b === 4);
idx = 0;
for (let [a,b] of [[11,12], [13,14]])
{
if (idx == 0)
{
eval("assert(a === 11)");
eval("assert(b === 12)");
idx = 1;
}
else
{
eval("assert(a === 13)");
eval("assert(b === 14)");
}
}
assert(a === 3);
assert(b === 4);
try {
eval("for (let [a,b] = [1,2] of [[3,4]]) {}");
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}
@@ -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.
*/
'use strict';
try {
eval('for (var i = 0 in {}) {}');
assert(false);
} catch(e) {
assert(e instanceof SyntaxError);
}
try {
eval('for (var = i of {}) {}');
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}
var reached = false;
for (var i in {}) {
reached = true;
}
assert(!reached);
for (var i of []) {
reached = true;
}
assert(!reached);
for (let i in {}) {
reached = true;
}
assert(!reached);
for (let i of []) {
reached = true;
}
assert(!reached);
for (const i in {}) {
reached = true;
}
assert(!reached);
for (const i of []) {
reached = true;
}
assert(!reached);
+31
View File
@@ -0,0 +1,31 @@
// 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 = (Object.getOwnPropertyDescriptor({get a(){}}, 'a')).get;
try {
new g;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var s = (Object.getOwnPropertyDescriptor({set a(value){}}, 'a')).set;
try {
new s;
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
@@ -0,0 +1,47 @@
// 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 props = ['arguments', 'caller'];
function f_simple () {
}
function f_strict () {
"use strict";
}
for (let prop of props) {
try {
Function.prototype[prop];
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
assert(f_simple[prop] === null);
try {
f_strict[prop];
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
let desc = Object.getOwnPropertyDescriptor(f_simple, prop);
assert(desc.value === null);
assert(desc.writable === false);
assert(desc.enumerable === false);
assert(desc.configurable === false);
assert(Object.getOwnPropertyDescriptor(f_strict, prop) === undefined);
}
+146
View File
@@ -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.
/* This test checks async modifiers (nothing else). */
function check_promise(p, value)
{
assert(p instanceof Promise)
p.then(function(v) {
assert(v === value)
})
}
/* Async functions */
async function f(a) {
return a
}
check_promise(f(1), 1)
f = async function (a) { return a }
check_promise(f(2), 2)
f = (async function (a) { return a })
check_promise(f(3), 3)
f = [async function (a) { return a }]
check_promise(f[0](4), 4)
/* These four are parser tests. */
async => {}
async async => {}
(async => {})
(async async => {})
f = async => async;
assert(f(5) === 5)
f = async async => async;
check_promise(f(6), 6)
f = (async => async)
assert(f(7) === 7)
f = (async async => async)
check_promise(f(8), 8)
f = [async => async]
assert(f[0](9) === 9)
f = [async async => async]
check_promise(f[0](10), 10)
f = async (a, b) => a + b;
check_promise(f(10, 1), 11)
f = (async (a, b) => a + b);
check_promise(f(10, 2), 12)
f = [async (a, b) => a + b];
check_promise(f[0](10, 3), 13)
f = true ? async () => 14 : 0;
check_promise(f(), 14)
f = (1, async async => async)
check_promise(f(15), 15)
/* Functions contain async references */
function f1() {
var async = 1;
/* The arrow function after the newline should be ignored. */
var v1 = async
async => async
/* The function statement after the newline should not be an async function. */
var v2 = async
function g() { return 2 }
async
function h() { return 3 }
assert(v1 === 1)
assert(v2 === 1)
assert(g() === 2)
assert(h() === 3)
}
f1();
function f2() {
var async = 1;
function g() { async = 2; }
g();
assert(async == 2);
}
f2();
function f3() {
var v = 3;
var async = () => v = 4;
function g() { async(); }
g();
assert(v === 4);
}
f3();
function f4() {
var v = 5;
var async = (a, b) => v = a + b;
function g() { async(((v)), ((v))); }
g();
assert(v === 10);
}
f4();
function f5() {
var v = 0;
var async = (a, b) => v = a + b;
function g() { async((async(1,2)), ((async(3,4)))); }
g();
assert(v === 10);
}
f5();
+69
View File
@@ -0,0 +1,69 @@
// 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 checks async modifiers (nothing else). */
function check_syntax_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
check_syntax_error("function async f() {}")
check_syntax_error("(a,b) async => 1")
/* SyntaxError because arrow declaration is an assignment expression. */
check_syntax_error("async * (a,b) => 1")
check_syntax_error("({ *async f() {} })")
check_syntax_error("class C { async static f() {} }")
check_syntax_error("class C { * async f() {} }")
check_syntax_error("class C { static * async f() {} }")
function check_promise(p, value)
{
assert(p instanceof Promise)
p.then(function(v) {
assert(v === value)
})
}
var o = {
async f() { return 1 },
async() { return 2 },
async *x() {}, /* Parser test, async iterators are needed to work. */
}
check_promise(o.f(), 1)
assert(o.async() === 2)
class C {
async f() { return 3 }
async *x() {} /* Parser test, async iterators are needed to work. */
static async f() { return 4 }
static async *y() {} /* Parser test, async iterators are needed to work. */
async() { return 5 }
static async() { return 6 }
}
var c = new C
check_promise(c.f(), 3)
check_promise(C.f(), 4)
assert(c.async() === 5)
assert(C.async() === 6)
+138
View File
@@ -0,0 +1,138 @@
// 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 checks await expressions (nothing else). */
function check_syntax_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
check_syntax_error("(async function await() {})")
check_syntax_error("(async function *await() {})")
check_syntax_error("async function f(await) {}")
check_syntax_error("(async function f(await) {})")
check_syntax_error("async function f(a = await new Promise) {}")
check_syntax_error("async function f() { function await() {} }")
check_syntax_error("async await => 0");
check_syntax_error("async (await) => 0");
check_syntax_error("async function f() { await () => 0 }");
check_syntax_error("async (a) => a\\u0077ait a");
check_syntax_error("async (a) => { () => 0\na\\u0077ait a }");
// Valid uses of await
async a => await a
async a => { await a }
async (a) => await a
async(a) => { await a }
// Nested async and non-async functions
async (a) => {
() => await
await a
}
(a) => {
await
async (a) => await a
await
async (a) => await a
a\u0077ait
}
async function f1(a) {
await a
(function () { await ? async function(a) { await a } : await })
await a
}
async (a) => {
await a;
() => await ? async (a) => await a : await
await a
}
async (a) => {
(a = () => await, [b] = (c))
await a
(a, b = () => await)
await a
}
// Object initializers
var o = {
async await(a) {
await a;
() => await
await a
},
f(a) {
await
async (a) => await a
await
async (a) => await a
a\u0077ait
},
async ["g"] () {
await a;
() => await
await a
}
}
async function f2(a) {
var o = {
[await a]() { await % await }
}
await a;
}
class C {
async await(a) {
await a;
() => await
await a
}
f(a) {
await
async (a) => await a
await
async (a) => await a
a\u0077ait
}
async ["g"] () {
await a;
() => await
await a
}
}
async function f3(a) {
class C {
[await a]() { await % await }
}
await a;
}
+38
View File
@@ -0,0 +1,38 @@
// 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 f(...a)
{
return a.length
}
check_syntax_error ("f(,)")
check_syntax_error ("f(,1)")
check_syntax_error ("f(1,,)")
check_syntax_error ("f(1,,2)")
assert(f(10) === 1)
assert(f(10,) === 1)
assert(f(10,11) === 2)
assert(f(10,11,) === 2)
+119
View File
@@ -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.
*/
function check_syntax_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
check_syntax_error ("'use strict'; if (true) function f() {}")
check_syntax_error ("'use strict'; if (true) ; else function f() {}")
check_syntax_error ("'use strict'; a: function f() {}")
check_syntax_error ("if (true) async function f() {}")
check_syntax_error ("if (true) ; else async function f() {}")
check_syntax_error ("if (true) a: function f() {}")
check_syntax_error ("if (true) ; else a: function f() {}")
var g = 1
var h = 1
function f1(p)
{
assert(g === undefined)
assert(h === undefined)
if (p)
// Same as: { function g() { return 3 } }
function g() { return 3 }
else
// Same as: { function h() { return 4 } }
function h() { return 4 }
if (p) {
assert(g() === 3)
assert(h === undefined)
} else {
assert(g === undefined)
assert(h() === 4)
}
}
f1(true)
f1(false)
function f2()
{
assert(g() === 2)
a: b: c: d: function g() { return 2 }
assert(h === undefined)
{
assert(h() === 3)
a: b: c: d: function h() { return 3 }
}
assert(h() === 3)
try {
assert(h() === 4)
a: b: c: d: function h() { return 4 }
throw 1
} catch (e) {
assert(h() === 5)
a: b: c: d: function h() { return 5 }
} finally {
assert(h() === 6)
a: b: c: d: function h() { return 6 }
}
assert(h() === 6)
switch (1) {
default:
assert(h() === 7)
a: b: c: d: function h() { return 7 }
}
assert(h() === 7)
}
f2()
function f3()
{
assert(h === undefined)
{
let a = eval("1")
assert(a === 1)
assert(h() === 1)
a: b: c: d: function h() { return 1 }
}
assert(h() === 1)
{
eval("a: b: c: d: function h() { return 2 }")
assert(h() === 2)
}
assert(h() === 2)
}
f3()
+297
View File
@@ -0,0 +1,297 @@
// 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 assertNameExists(func) {
assert(func.hasOwnProperty('name') === true);
}
function assertNameNotExists(func) {
assert(func.hasOwnProperty('name') === false);
assert(Function.prototype.name === '');
assert(func.name === '');
}
function assertConfigurableOnlyMethod(func) {
let desc = Object.getOwnPropertyDescriptor(func, 'name');
assert(desc.configurable === true);
assert(desc.writable === false);
assert(desc.enumerable === false);
delete func.name;
assertNameNotExists(func);
Object.defineProperty(func, 'name', {value: 'newName', configurable: true});
assert (Object.getOwnPropertyDescriptor(func, 'name').value === 'newName');
assertNameExists(func);
delete func.name;
assertNameNotExists(func);
Object.defineProperty(func, 'name', desc);
}
function assertConfigurableOnlyAccessor(func, name, method) {
let accessor = Object.getOwnPropertyDescriptor(func, name)[method];
assertConfigurableOnlyMethod(accessor);
}
function assertMethodName(func, name, functionName = name) {
assertNameExists(func);
assertConfigurableOnlyMethod(func)
assert(Object.getOwnPropertyDescriptor(func, 'name').value === functionName)
}
function assertGetterName(obj, name, functionName = name) {
assertConfigurableOnlyAccessor(obj, name, 'get');
assert(Object.getOwnPropertyDescriptor(obj, name).get['name'] === 'get ' + functionName)
}
function assertSetterName(obj, name, functionName = name) {
assertConfigurableOnlyAccessor(obj, name, 'set');
assert(Object.getOwnPropertyDescriptor(obj, name).set['name'] === 'set ' + functionName)
}
var func1 = function () {};
assertMethodName(func1, 'func1');
var func2 = function bar() {};
assertMethodName(func2, 'bar');
var func3 = (function () {}).prototype.constructor;
assert(typeof func3 === 'function');
assertNameNotExists(func3);
var func4;
func4 = function () {}
assertMethodName(func4, 'func4');
var func5;
func5 = function bar () {}
assertMethodName(func5, 'bar');
var func6;
(func6) = function () {}
assertNameNotExists(func6);
var func7;
(func7) = function bar () {}
assertMethodName(func7, 'bar');
let emptySymbolMethod = Symbol();
let namedSymbolMethod = Symbol('foo');
let emptySymbolGetter = Symbol();
let namedSymbolGetter = Symbol('foo');
let emptySymbolSetter = Symbol();
let namedSymbolSetter = Symbol('foo');
var o = {
func1() {},
func2: function () {},
func3: function bar() {},
func4: () => {},
func5: class {},
func6: class A {},
func7: class name { static name () {} },
['func' + '8']() {},
['func' + '9']: function () {},
['func' + '10']: function bar() {},
['func' + '11']: () => {},
['func' + '12']: class {},
['func' + '13']: class A {},
['func' + '14']: class name { static name () {} },
get func15() {},
get ['func' + '16']() {},
set func17(a) {},
set ['func' + '18'](a) {},
[emptySymbolMethod]() {},
[namedSymbolMethod]() {},
get [emptySymbolGetter]() {},
get [namedSymbolGetter]() {},
set [emptySymbolSetter](a) {},
set [namedSymbolSetter](a) {},
}
assertMethodName(o.func1, 'func1');
assertMethodName(o.func2, 'func2');
assertMethodName(o.func3, 'bar');
assertMethodName(o.func4, 'func4');
assertMethodName(o.func5, 'func5');
assertMethodName(o.func6, 'A');
assert(typeof o.func7 === 'function');
assertMethodName(o.func8, 'func8');
assertMethodName(o.func9, 'func9');
assertMethodName(o.func10, 'bar');
assertMethodName(o.func11, 'func11');
assertMethodName(o.func12, 'func12');
assertMethodName(o.func13, 'A');
assert(typeof o.func14 === 'function');
assertGetterName(o, 'func15');
assertGetterName(o, 'func16');
assertSetterName(o, 'func17');
assertSetterName(o, 'func17');
assertMethodName(o[emptySymbolMethod], '');
assertMethodName(o[namedSymbolMethod], '[foo]');
assertGetterName(o, emptySymbolGetter, '');
assertGetterName(o, namedSymbolGetter, '[foo]');
assertSetterName(o, emptySymbolSetter, '');
assertSetterName(o, namedSymbolSetter, '[foo]');
class A {
constructor () {}
func1() {}
get func2() {}
set func3(a) {}
static func4() {}
static get func5() {}
static set func6(a) {}
['func' + '7']() {}
get ['func' + '8']() {}
set ['func' + '9'](a) {}
static ['func' + '10']() {}
static get ['func' + '11']() {}
static set ['func' + '12'](a) {}
[emptySymbolMethod]() {}
[namedSymbolMethod]() {}
get [emptySymbolGetter]() {}
get [namedSymbolGetter]() {}
set [emptySymbolSetter](a) {}
set [namedSymbolSetter](a) {}
static [emptySymbolMethod]() {}
static [namedSymbolMethod]() {}
static get [emptySymbolGetter]() {}
static get [namedSymbolGetter]() {}
static set [emptySymbolSetter](a) {}
static set [namedSymbolSetter](a) {}
}
assertMethodName(A.prototype.func1, 'func1');
assertGetterName(A.prototype, 'func2');
assertSetterName(A.prototype, 'func3');
assertMethodName(A.func4, 'func4');
assertGetterName(A, 'func5');
assertSetterName(A, 'func6');
assertMethodName(A.prototype.func7, 'func7');
assertGetterName(A.prototype, 'func8');
assertSetterName(A.prototype, 'func9');
assertMethodName(A.func10, 'func10');
assertGetterName(A, 'func11');
assertSetterName(A, 'func12');
assertMethodName(A[emptySymbolMethod], '');
assertMethodName(A[namedSymbolMethod], '[foo]');
assertGetterName(A, emptySymbolGetter, '');
assertGetterName(A, namedSymbolGetter, '[foo]');
assertSetterName(A, emptySymbolSetter, '');
assertSetterName(A, namedSymbolSetter, '[foo]');
assertMethodName(A.prototype[emptySymbolMethod], '');
assertMethodName(A.prototype[namedSymbolMethod], '[foo]');
assertGetterName(A.prototype, emptySymbolGetter, '');
assertGetterName(A.prototype, namedSymbolGetter, '[foo]');
assertSetterName(A.prototype, emptySymbolSetter, '');
assertSetterName(A.prototype, namedSymbolSetter, '[foo]');
class B {
func1() {}
get func2() {}
set func3(a) {}
static func4() {}
static get func5() {}
static set func6(a) {}
['func' + '7']() {}
get ['func' + '8']() {}
set ['func' + '9'](a) {}
static ['func' + '10']() {}
static get ['func' + '11']() {}
static set ['func' + '12'](a) {}
[emptySymbolMethod]() {}
[namedSymbolMethod]() {}
get [emptySymbolGetter]() {}
get [namedSymbolGetter]() {}
set [emptySymbolSetter](a) {}
set [namedSymbolSetter](a) {}
static [emptySymbolMethod]() {}
static [namedSymbolMethod]() {}
static get [emptySymbolGetter]() {}
static get [namedSymbolGetter]() {}
static set [emptySymbolSetter](a) {}
static set [namedSymbolSetter](a) {}
}
assertMethodName(B.prototype.func1, 'func1');
assertGetterName(B.prototype, 'func2');
assertSetterName(B.prototype, 'func3');
assertMethodName(B.func4, 'func4');
assertGetterName(B, 'func5');
assertSetterName(B, 'func6');
assertMethodName(B.prototype.func7, 'func7');
assertGetterName(B.prototype, 'func8');
assertSetterName(B.prototype, 'func9');
assertMethodName(B.func10, 'func10');
assertGetterName(B, 'func11');
assertSetterName(B, 'func12');
assertMethodName(B[emptySymbolMethod], '');
assertMethodName(B[namedSymbolMethod], '[foo]');
assertGetterName(B, emptySymbolGetter, '');
assertGetterName(B, namedSymbolGetter, '[foo]');
assertSetterName(B, emptySymbolSetter, '');
assertSetterName(B, namedSymbolSetter, '[foo]');
assertMethodName(B.prototype[emptySymbolMethod], '');
assertMethodName(B.prototype[namedSymbolMethod], '[foo]');
assertGetterName(B.prototype, emptySymbolGetter, '');
assertGetterName(B.prototype, namedSymbolGetter, '[foo]');
assertSetterName(B.prototype, emptySymbolSetter, '');
assertSetterName(B.prototype, namedSymbolSetter, '[foo]');
let names = ['push', 'pop', 'reduce', 'reduceRight'];
for (let n of names) {
assert(Array.prototype[n].name === n);
}
assert(Array.prototype[Symbol.iterator].name === 'values');
assert(Array.prototype.values.name === 'values');
assert(Object.getOwnPropertyDescriptor(Array, Symbol.species).get.name === 'get [Symbol.species]');
assert(Object.getOwnPropertyDescriptor(String.prototype, Symbol.iterator).value.name === '[Symbol.iterator]');
assert(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__').get.name === 'get __proto__');
assert(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__').set.name === 'set __proto__');
let arFunc;
let array = [];
array['original'] = array;
array['original'][arFunc = ()=>{ }]=function(){}
assertNameNotExists(array[arFunc]);
var o = { 0 : class {} };
assertMethodName(o['0'], '0');
@@ -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.
var get = [];
var p = new Proxy(Function, { get: function(o, k) { get.push(k); return o[k]; }});
new p;
assert(get + '' === "prototype");
var func = function() {}
var reflect = Reflect.construct(Function, ['a','b','return a+b'], func);
assert(Object.getPrototypeOf(reflect) == func.prototype);
var o = new Proxy (function f () {}, { get(t,p,r) { if (p == "prototype") { throw 42 }}})
try {
Reflect.construct(Function, [], o);
assert(false);
} catch (e) {
assert(e === 42);
}
@@ -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 local = 0;
switch(0) { /* This switch forces a pre-scanner run. */
default:
function f(a = (5, local = 6),
b = ((5 + function(a = 6) { return a }() * 3)),
c,
d = true ? 1 : 2)
{
return "" + a + ", " + b + ", " + c + ", " + d;
}
assert(f() === "6, 23, undefined, 1");
assert(local === 6);
var obj = {
f: function(a = [10,,20],
b,
c = Math.cos(0),
d)
{
return "" + a + ", " + b + ", " + c + ", " + d;
}
};
assert(obj.f() === "10,,20, undefined, 1, undefined");
function g(a, b = (local = 7)) { }
local = 0;
g();
assert(local === 7);
local = 0;
g(0);
assert(local === 7);
local = 0;
g(0, undefined);
assert(local === 7);
local = 0;
g(0, null);
assert(local === 0);
local = 0;
g(0, false);
assert(local === 0);
break;
}
function CheckSyntaxError(str)
{
try {
eval(str);
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}
}
CheckSyntaxError('function x(a += 5) {}');
CheckSyntaxError('function x(a =, b) {}');
CheckSyntaxError('function x(a = (b) {}');
CheckSyntaxError('function x(a, a = 5) {}');
CheckSyntaxError('function x(a = 5, a) {}');
// Pre-scanner tests.
var str = "a = 5, b, c = function() { for (var a = 0; a < 4; a++) ; return a; } ()"
var f = new Function (str, str);
f();
var f = new Function (str, "return (a + c) * (b == undefined ? 1 : 0)");
assert (f() == 9);
function duplicatedArg (a = c, b = d, c) {
assert(a === 1);
assert(b === 2);
assert(c === 3);
}
duplicatedArg(1, 2, 3);
+100
View File
@@ -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.
function f(a, b = a)
{
function a() { return 2; }
assert(a() === 2);
assert(b === 1)
}
f(1);
function g(a, b = a)
{
function a() { return 2; }
eval("assert(a() === 2)");
eval("assert(b === 1)");
}
g(1);
var x = 1;
function h(a = x) {
assert(x === undefined);
var x = 2;
assert(a === 1);
assert(x === 2);
}
h();
x = function() { return 4; }
let y = 6;
function i(a = x() / 2, b = (y) + 2, c = typeof z) {
let y = 10;
let z = 11;
function x() { return 5; }
assert(a === 2);
assert(x() === 5);
assert(b === 8);
assert(c === "undefined");
assert(y === 10);
assert(z === 11);
}
i();
var arguments = 10;
function j(a = arguments[1])
{
assert(a === 2);
a = 3;
assert(arguments[0] === undefined)
}
j(undefined,2);
function k(a = 2)
{
let d = 5;
assert(d === 5);
eval("assert(a === 2)");
}
k();
function l(a = 3)
{
const d = 6;
assert(d === 6);
eval("assert(a === 3)");
}
l();
function m(a, b = 2)
{
assert(a === 1);
assert(arguments[0] === 1);
assert(b === 2);
assert(arguments[1] === undefined);
a = 8;
b = 9;
assert(a === 8);
assert(arguments[0] === 1);
assert(b === 9);
assert(arguments[1] === undefined);
}
m(1);
@@ -0,0 +1,68 @@
// 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 = 1
function f(a = function () { return d })
{
var d = 2
assert(d === 2)
assert(a() === 1)
}
f()
var g = (a = () => d) => {
var d = 2
assert(d === 2)
assert(a() === 1)
}
g()
var h = ([{a}] = [{a: function () { return d }}]) => {
var d = 2
assert(d === 2)
assert(a() === 1)
}
h()
function i(a = ((eval))("(function () { return d })"))
{
var d = 2
assert(d === 2)
assert(a() === 1)
}
i()
function j(a = (([1, ((() => d))])[1]))
{
var d = 2
assert(d === 2)
assert(a() === 1)
}
j()
var m = 0
function l(a)
{
m = a
return m
}
function k(a = l(() => d))
{
var d = 2
assert(d === 2)
assert(a() === 1)
assert(m() === 1)
}
k()
+164
View File
@@ -0,0 +1,164 @@
// 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_reference_error(code)
{
try {
eval(code);
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
}
function f1(a = a)
{
assert(a === 1)
}
f1(1)
check_reference_error("f1()");
function f2([a] = 1 + a)
{
assert(a === 2)
}
f2([2])
check_reference_error("f2()");
function f3([a = !a])
{
assert(a === 2)
}
f3([2])
check_reference_error("f3([])");
function f4([[a]] = a)
{
assert(a === 3)
}
f4([[3]])
check_reference_error("f4()");
function f5([[a], b = a] = a)
{
assert(a === 4 && b === 4)
}
f5([[4]])
check_reference_error("f5()")
function f6(a = 3 - ((b)), b)
{
assert(a === 1 && b === 2)
}
f6(1, 2)
check_reference_error("f6(undefined, 2)");
function f7(a = b(), [b])
{
assert(a === 3 && b === 4)
}
f7(3, [4])
check_reference_error("f7(undefined, [4])");
function f8(a = (function () { return a * 2 })())
{
assert(a === 1)
}
f8(1)
check_reference_error("f8()");
function f9({a = b, b:{b}})
{
assert(a === 2 && b === 3)
}
f9({a:2, b:{b:3}})
check_reference_error("f9({b:{b:3}})");
function f10(a = eval("a"))
{
assert(a === 1)
}
f10(1)
check_reference_error("f10()");
function f11([a] = eval("a"))
{
assert(a === 2)
}
f11([2])
check_reference_error("f11()");
function f12({a} = eval("a"))
{
assert(a === 3)
}
f12({a:3})
check_reference_error("f12()");
function f13(a = arguments)
{
assert(a[0] === undefined)
assert(a[1] === 4)
arguments[0] = 5
assert(a[0] === 5)
}
f13(undefined, 4)
function f14(a, b = function() { return a; }(), c = (() => a)())
{
assert(a === 6 && b === 6 && c === 6)
}
f14(6)
function f15(a = (() => b)(), b)
{
assert(a === 1 && b === 2)
}
f15(1, 2)
check_reference_error("f15(undefined, 2)");
var f16 = (a = a) =>
{
assert(a === 1)
}
f16(1)
check_reference_error("f16()");
var f17 = ([[a]] = a) =>
{
assert(a === 2)
}
f17([[2]])
check_reference_error("f17()");
var f18 = ({a = b, b:{b}}) =>
{
assert(a === 3 && b === 4)
}
f18({a:3, b:{b:4}})
check_reference_error("f18({b:{b:4}})");
var f19 = (a = eval("a")) =>
{
assert(a === 5)
}
f19(5)
check_reference_error("f19()");
var f20 = ([a] = eval("a")) =>
{
assert(a === 6)
}
f20([6])
check_reference_error("f20()");
+94
View File
@@ -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.
function must_throw (str, type = SyntaxError)
{
try
{
eval (str);
assert (false);
}
catch (e)
{
assert (e instanceof type)
}
}
must_throw ("function f(a, [a]) {}");
must_throw ("function f([a], a) {}");
must_throw ("function f(a = b, [b]) {}; f()", ReferenceError);
must_throw ("function f([a+b]) {}");
must_throw ("function f([a().b]) {}");
must_throw ("function f(...[a] = [1]) {}");
function a1([a,b]) {
var a, b;
assert(a === 1);
assert(b === undefined);
var a = 3;
assert(a === 3);
}
a1([1]);
function a2([a,b]) {
eval("var a, b");
assert(a === 1);
assert(b === undefined);
eval("var a = 3");
assert(a === 3);
}
a2([1]);
function f1(a, [b], c, [d], e)
{
assert (a === 1);
assert (b === 2);
assert (c === 3);
assert (d === 4);
assert (e === 5);
}
f1(1, [2], 3, [4], 5)
function f2(a, [b], c, [d], e)
{
eval("");
assert (a === 1);
assert (b === 2);
assert (c === 3);
assert (d === 4);
assert (e === 5);
}
f2(1, [2], 3, [4], 5)
var g1 = (a, [b], c, [d], e) => {
assert (a === 1);
assert (b === 2);
assert (c === 3);
assert (d === 4);
assert (e === 5);
}
g1(1, [2], 3, [4], 5)
var g2 = (a, [b], c, [d], e) => {
eval("");
assert (a === 1);
assert (b === 2);
assert (c === 3);
assert (d === 4);
assert (e === 5);
}
g2(1, [2], 3, [4], 5)
+93
View File
@@ -0,0 +1,93 @@
// 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((function([a], b, {c}) {}).length === 3);
function f([a = "x", b = "y", c = "z"])
{
assert(a === "a");
assert(b === "b");
assert(c === "z");
}
f("ab")
function g({ ["x" + "y"]: { a = 4, b = 5 }, })
{
assert(a === 1);
assert(b === 5);
}
g({ xy: { a:1 } });
function h([,,a,,b,,])
{
assert(a === 3);
assert(b === 5);
}
h([1,2,3,4,5,6,7,8])
function i([a] = [42], b = a)
{
assert(a === 42);
assert(b === 42);
}
i();
function j(a, [[b = a, [c] = [b], { d } = { d:eval("c") }], e = d + 1] = [[]])
{
assert(a === 8);
assert(b === 8);
assert(c === 8);
assert(d === 8);
assert(e === 9);
}
j(8);
function k([a = function() { return a; }])
{
assert(typeof a === "function");
assert(a() === a);
}
k([]);
function l(a = 0, ...[b, c = 1, d = 4])
{
assert(a === 1);
assert(b === 2);
assert(c === 3);
assert(d === 4);
}
l(1,2,3);
Function("{a, x:b}","[c]", "{ 'dd':d, e = Math.cos(0)}",
"assert(a === 1);" +
"assert(b === undefined);" +
"assert(c === 3);" +
"assert(d === 4);" +
"assert(e === 1);"
)({a:1, b:3}, [3], {a:1, b:2, dd:4});
function m()
{
var prop_name = "x";
var def_val = 123;
function g({[prop_name]: a, b = def_val })
{
assert(a === 12);
assert(b === 123);
}
g({ x:12 })
}
m();
@@ -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.
function getProperties(obj)
{
var str = "";
for (name in obj)
{
if (str)
{
str += " " + name;
}
else
{
str = name;
}
}
return str;
}
var prototype_obj = { dummy:1, length:1, caller:null,
arguments:null, prototype:null };
var func = function() {};
Object.setPrototypeOf(func, prototype_obj);
assert(getProperties(func) == "dummy");
var bound_func = (function() {}).bind(null);
Object.setPrototypeOf(bound_func, prototype_obj);
assert(getProperties(bound_func) == "dummy prototype");
// 'print' is an external function
Object.setPrototypeOf(print, prototype_obj);
assert(getProperties(print) == "dummy length caller arguments");
@@ -0,0 +1,69 @@
// 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.
/* extended class */
(function() {
class C extends Function {}
var c = new C("x", "y", "return this.foo + x + y;").bind({foo : 1}, 2);
assert(c(3) === 6);
assert(c instanceof C);
})();
function boundPrototypeChecker(f, proto) {
Object.setPrototypeOf(f, proto);
var boundFunc = Function.prototype.bind.call(f, null);
assert(Object.getPrototypeOf(boundFunc) === proto);
}
/* generator function */
(function() {
var f = function*(){};
boundPrototypeChecker(f, Function.prototype)
boundPrototypeChecker(f, {})
boundPrototypeChecker(f, null);
})();
/* arrow function */
(function() {
var f = () => 5;
boundPrototypeChecker(f, Function.prototype)
boundPrototypeChecker(f, {})
boundPrototypeChecker(f, null);
})();
/* simple class */
(function() {
class C {};
boundPrototypeChecker(C, Function.prototype)
boundPrototypeChecker(C, {})
boundPrototypeChecker(C, null);
})();
/* subclasses */
(function() {
function boundPrototypeChecker(superclass) {
class C extends superclass {
constructor() {
return Object.create(null);
}
}
var boundF = Function.prototype.bind.call(C, null);
assert(Object.getPrototypeOf(boundF) === Object.getPrototypeOf(C));
}
boundPrototypeChecker(function(){});
boundPrototypeChecker(Array);
boundPrototypeChecker(null);
})();
@@ -0,0 +1,53 @@
// 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 base {
constructor (value) {
this.member = value;
}
method () {
return this.member;
}
}
class sub {
constructor (value) {
this.member = value;
}
}
var obj_base = new base (3);
var obj_sub = new sub (4);
assert (base[Symbol.hasInstance](obj_base) === true);
assert (base[Symbol.hasInstance](obj_sub) === false);
assert (sub[Symbol.hasInstance](obj_base) === false);
assert (sub[Symbol.hasInstance](obj_sub) === true);
class sub_c extends base {
constructor (value) {
super(value);
this.member = value;
}
}
var obj_sub_c = new sub_c (5);
assert (base[Symbol.hasInstance](obj_sub_c) === true);
assert (sub_c[Symbol.hasInstance](obj_base) === false);
assert (sub_c[Symbol.hasInstance](obj_sub_c) === true);
@@ -0,0 +1,81 @@
// 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 base (value) {
this.member = value;
}
base.prototype.method = function () { return this.member; }
function sub (value) {
this.member = value;
}
sub.prototype = base;
var obj_base = new base (3);
var obj_sub = new sub (4);
assert (base[Symbol.hasInstance] (obj_base) === true);
assert (base[Symbol.hasInstance] (obj_sub) === false);
assert (Object[Symbol.hasInstance] (obj_base) === true);
assert (Object[Symbol.hasInstance] (obj_sub) === true);
assert (obj_base.method () === 3);
assert (sub[Symbol.hasInstance] (obj_base) === false);
assert (sub[Symbol.hasInstance] (obj_sub) === true);
assert (obj_sub.method === undefined);
function sub_c (value) {
this.member = value;
}
sub_c.prototype = Object.create (base.prototype)
sub_c.prototype.constructor = sub_c
var obj_sub_c = new sub_c (5);
assert (base[Symbol.hasInstance] (obj_sub_c) === true);
assert (sub_c[Symbol.hasInstance] (obj_base) === false);
assert (sub_c[Symbol.hasInstance] (obj_sub_c) === true);
assert (Object[Symbol.hasInstance] (obj_sub_c) === true);
assert (Function.prototype[Symbol.hasInstance].call (sub_c, obj_sub_c) === true);
assert (obj_sub_c.method () === 5);
assert (base[Symbol.hasInstance] (3) === false);
assert (Number[Symbol.hasInstance] (33) === false);
assert (Number[Symbol.hasInstance] (new Number (33)) === true);
assert (Object[Symbol.hasInstance] (44) === false);
assert (Object[Symbol.hasInstance] (new Number (22)) === true);
assert (base[Symbol.hasInstance] ('demo') === false);
assert (String[Symbol.hasInstance] ('demo') === false);
assert (String[Symbol.hasInstance] (new String ('demo')) === true);
assert (Object[Symbol.hasInstance] ('demo') === false);
assert (Object[Symbol.hasInstance] (new String ('demo')) === true);
assert (base[Symbol.hasInstance] ([]) === false);
assert (base[Symbol.hasInstance] ([1, 2]) === false);
assert (Array[Symbol.hasInstance] ([1, 2]) === true);
assert (Array[Symbol.hasInstance] (new Array(1, 2)) === true);
assert (Object[Symbol.hasInstance] ([]) === true);
assert (Object[Symbol.hasInstance] (new Array()) === true);
assert (base[Symbol.hasInstance] (new RegExp('abc')) === false);
assert (RegExp[Symbol.hasInstance] (/abc/) === true);
assert (RegExp[Symbol.hasInstance] (new RegExp('abc')) === true);
assert (Object[Symbol.hasInstance] (/abc/) === true);
@@ -0,0 +1,96 @@
// 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);
}
/* force the pre-scanner */
try {
eval ('switch (1) { default: ' + str + '}');
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}
}
CheckSyntaxError ('function x (a, b, ...c, d) {}');
CheckSyntaxError ('function x (... c = 5) {}');
CheckSyntaxError ('function x (...) {}');
CheckSyntaxError ('function x (a, a, ...a) {}');
CheckSyntaxError ('"use strict" function x (...arguments) {}');
CheckSyntaxError ('var o = { set e (...args) { } }');
rest_params = ['hello', true, 7, {}, [], function () {}];
function f (x, y, ...a) {
for (var i = 0; i < a.length; i++) {
assert (a[i] == rest_params[i]);
}
return (x + y) * a.length;
}
assert (f (1, 2, rest_params[0], rest_params[1], rest_params[2]) === 9);
assert (f.length === 2);
function g (...a) {
return a.reduce (function (accumulator, currentValue) { return accumulator + currentValue });
}
assert (g (1, 2, 3, 4) === 10);
function h (...arguments) {
return arguments.length;
}
assert (h (1, 2, 3, 4) === 4);
function f2 (a = 1, b = 1, c = 1, ...d) {
assert (JSON.stringify (d) === '[]');
return a + b + c;
}
assert (f2 () === 3);
assert (f2 (2) === 4);
assert (f2 (2, 3) === 6);
assert (f2 (2, 3, 4) === 9);
function g2 (a = 5, b = a + 1, ...c) {
return a + b + c.length;
}
assert (g2 () === 11);
assert (g2 (1) === 3);
assert (g2 (1, 2) === 3);
assert (g2 (1, 2, 3) === 4);
function args(a, ...b)
{
assert(a === 1);
assert(arguments[0] === 1);
a = 5;
assert(a === 5);
assert(arguments[0] === 1);
assert(arguments[1] === 2);
assert(b[0] === 2)
}
args(1, 2);
+67
View File
@@ -0,0 +1,67 @@
// 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)
{
assert(a === 2)
{
assert(a() === 1)
function a() { return 1 }
}
assert(a === 2)
}
f1(2)
function f2([a])
{
assert(a === 4)
{
assert(a() === 3)
function a() { return 3 }
}
assert(a === 4)
}
f2([4])
function f3(a)
{
assert(a() === 5)
{
assert(a() === 6)
function a() { return 6 }
}
assert(a() === 5)
function a() { return 5 }
}
f3(7)
function f4(a)
{
assert(a === 8)
{
eval("function a() { return 9 }")
assert(a() === 9)
}
assert(a() === 9)
}
f4(8)
function f5(a, b = function() { return a }) {
function a() { return 9 }
assert(a() === 9)
assert(b() === 10)
}
f5(10)
+111
View File
@@ -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.
var a = 1;
var b = 2;
function f(x = eval("eval('var a = 3; function b() { return 4 } () => a')"), y = b) {
eval("eval('var a = 5; function b() { return 6 }')");
assert(a === 5);
assert(b() === 6);
assert(x() === 3);
assert(y() === 4);
delete a;
delete b;
assert(a === 3);
assert(b() === 4);
assert(x() === 3);
assert(y() === 4);
delete a;
delete b;
assert(a === 1);
assert(b === 2);
assert(x() === 1);
assert(y() === 4);
}
f()
function g() {
'use strict'
function h(x, y = function() { return x }) {
var x = 2;
/* This should not overwrite y. */
eval("var y = 3; assert (y === 3)");
assert(x === 2);
assert(typeof y === "function");
assert(y() === 1);
}
h(1);
}
g();
function h(a, get = () => a, set = (v) => a = v) {
assert(a === 1);
var a = 2;
assert(a === 2);
assert(get() === 1);
set(3)
a = 4;
assert(a === 4);
assert(get() === 3);
}
h(1);
function i([a], get = () => a, set = (v) => a = v) {
assert(a === 1);
var a;
assert(a === 1);
a = 2;
assert(a === 2);
assert(get() === 1);
set(3)
a = 4;
assert(a === 4);
assert(get() === 3);
}
i([1]);
function j(a = eval()) {
var a = 3.14;
try {
eval("throw 1; function a() { return 8; }")
assert(false)
} catch (e) {
assert(e === 1)
}
assert(a() === 8)
}
j()
+97
View File
@@ -0,0 +1,97 @@
// 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 %GeneratorPrototype%
(function () {
function* generatorFn(){}
var ownProto = Object.getPrototypeOf(generatorFn());
var sharedProto = Object.getPrototypeOf(ownProto);
assert(ownProto === generatorFn.prototype);
assert(sharedProto !== Object.prototype);
assert(sharedProto === Object.getPrototypeOf(function*(){}.prototype));
assert(sharedProto.hasOwnProperty('next'));
})();
// Test %GeneratorPrototype% prototype chain
(function () {
function* generatorFn(){}
var g = generatorFn();
var ownProto = Object.getPrototypeOf(g);
var sharedProto = Object.getPrototypeOf(ownProto);
var iterProto = Object.getPrototypeOf(sharedProto);
assert(ownProto === generatorFn.prototype);
assert(iterProto.hasOwnProperty(Symbol.iterator));
assert(!sharedProto.hasOwnProperty(Symbol.iterator));
assert(!ownProto.hasOwnProperty(Symbol.iterator));
assert(g[Symbol.iterator]() === g);
})();
// Test %GeneratorPrototype% prototype chain
(function () {
function* g(){}
var iterator = new g.constructor("a","b","c","() => yield\n yield a; yield b; yield c;")(1,2,3);
var item = iterator.next();
assert(item.value === 1);
assert(item.done === false);
item = iterator.next();
assert(item.value === 2);
assert(item.done === false);
item = iterator.next();
assert(item.value === 3);
assert(item.done === false);
item = iterator.next();
assert(item.value === undefined);
assert(item.done === true);
assert(g.constructor === (function*(){}).constructor);
})();
// Test GeneratorFunction parsing
(function () {
function *f() {};
try {
Object.getPrototypeOf(f).constructor("yield", "");
} catch (e) {
assert(e instanceof SyntaxError);
}
})();
// Test correct property membership
(function () {
function *f() {};
Object.getPrototypeOf(f).xx = 5;
assert(Object.getPrototypeOf(f).prototype.constructor.xx === 5);
})();
// Test GetPrototypeFromConstructor
(function () {
function *f() {};
var originalProto = f.prototype;
f.prototype = 5;
assert(Object.getPrototypeOf(f()) === Object.getPrototypeOf(originalProto));
var fakeProto = { x : 6 };
f.prototype = fakeProto;
assert(Object.getPrototypeOf(f()) === fakeProto);
assert(f.next === undefined);
})();
@@ -0,0 +1,138 @@
/* 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 file checks core generator operations. */
function check_syntax_error (code)
{
try {
eval (code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
check_syntax_error ("({ * })")
check_syntax_error ("({ *, b:4 })")
check_syntax_error ("({ *a:4 })")
check_syntax_error ("({ *['a']:4 })")
check_syntax_error ("({ *a(yield) {} })")
check_syntax_error ("({ get *a() {} })")
check_syntax_error ("({ set *b(v) {} })")
check_syntax_error ("class C { * }")
check_syntax_error ("class C { static * }")
check_syntax_error ("class C { *() {} }")
check_syntax_error ("class C { static * () {} }")
check_syntax_error ("class C { *['a'] {} }")
function check_result(result, value, done)
{
assert(result.value === value)
assert(result.done === done)
}
function postfix(a) { return a + "b" }
var o = {
* a () {
yield 1
return 2
},
*2(x) {
yield x + 1
return x + 2
},
*[postfix("a")]() {
var o = { get yield() { return 3 + 2 } }
yield o.yield
return 6
},
*yield() {
var o = { yield:7 }
yield o.yield
return 8
}
}
var f = o.a()
check_result(f.next(), 1, false)
check_result(f.next(), 2, true)
var f = o[2](2)
check_result(f.next(), 3, false)
check_result(f.next(), 4, true)
var f = o.ab()
check_result(f.next(), 5, false)
check_result(f.next(), 6, true)
var f = o.yield()
check_result(f.next(), 7, false)
check_result(f.next(), 8, true)
class C {
* a () {
yield 1
return 2
}
*3(x) {
yield x + 1
return x + 2
}
*[postfix("a")]() {
var o = { get yield() { return 3 + 2 } }
yield o.yield
return 6
}
static *yield() {
var o = { yield:7 }
yield o.yield
return 8
}
static * [postfix("b") ] (v = 9) {
return v
}
}
var c = new C
var f = c.a()
check_result(f.next(), 1, false)
check_result(f.next(), 2, true)
var f = c[3](2)
check_result(f.next(), 3, false)
check_result(f.next(), 4, true)
var f = c.ab()
check_result(f.next(), 5, false)
check_result(f.next(), 6, true)
var f = C.yield()
check_result(f.next(), 7, false)
check_result(f.next(), 8, true)
var f = C.bb()
check_result(f.next(), 9, true)
+74
View File
@@ -0,0 +1,74 @@
/* 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(result, value, done)
{
assert(result.value === value)
assert(result.done === done)
}
function * gen1(a) {
return "a: " + (yield a.p)
}
var f = gen1({})
check_result(f.return(4), 4, true)
check_result(f.next(), undefined, true)
f = gen1({ p:"x" })
check_result(f.next(), "x", false)
check_result(f.return(10), 10, true)
check_result(f.next(), undefined, true)
f = gen1({ p:"b" })
check_result(f.next(), "b", false)
check_result(f.next(), "a: undefined", true)
check_result(f.next(), undefined, true)
function*gen2() {
try {
for (let i in { x:1, y:2 })
{
assert((yield i) === "33")
}
assert(false)
} catch (e) {
assert(false)
} finally {
yield "z"
}
}
f = gen2()
check_result(f.return("ret"), "ret", true)
check_result(f.next(), undefined, true)
f = gen2()
check_result(f.next(), "x", false)
check_result(f.return("ret"), "z", false)
check_result(f.next(), "ret", true)
check_result(f.next(), undefined, true)
function* gen3() {
try {
return 8
} finally {
yield 1
}
}
f = gen3()
check_result(f.next(), 1, false)
check_result(f.return(2), 2, true)
+82
View File
@@ -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.
*/
function check_result(result, value, done)
{
assert(result.value === value)
assert(result.done === done)
}
function check_throw(str, expected)
{
try {
eval(str)
assert(false);
} catch (e) {
assert(e === expected);
}
}
function * gen1(a) {
return "a: " + (yield a.p)
}
var f = gen1({})
check_throw("f.throw(4)", 4)
check_result(f.next(), undefined, true)
f = gen1({ p:"x" })
check_result(f.next(), "x", false)
check_throw("f.throw(10)", 10)
check_result(f.next(), undefined, true)
f = gen1({ p:"b" })
check_result(f.next(), "b", false)
check_result(f.next(), "a: undefined", true)
check_result(f.next(), undefined, true)
function*gen2() {
try {
for (let i in { x:1, y:2 })
{
assert((yield i) === "33")
}
assert(false)
} finally {
yield "z"
}
}
f = gen2()
check_throw("f.throw('throw')", "throw")
check_result(f.next(), undefined, true)
f = gen2()
check_result(f.next(), "x", false)
check_result(f.throw("throw"), "z", false)
check_throw("f.next()", "throw")
check_result(f.next(), undefined, true)
function* gen3() {
try {
return 8
} finally {
yield 1
}
}
f = gen3()
check_result(f.next(), 1, false)
check_throw("f.throw(2)", 2)
@@ -0,0 +1,121 @@
/* 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(result, value, done)
{
assert(result.value === value)
assert(result.done === done)
}
function *gen1() {
yield 1
yield *[2,3,4]
yield 5
}
var g = gen1()
check_result(g.next(), 1, false)
check_result(g.next(), 2, false)
check_result(g.next(), 3, false)
check_result(g.next(), 4, false)
check_result(g.next(), 5, false)
check_result(g.next(), undefined, true)
function *gen2() {
yield * true
}
try {
g = gen2()
g.next()
assert(false)
} catch (e) {
assert(e instanceof TypeError)
}
var t0 = 0, t1 = 0
function *gen3() {
function *f() {
try {
yield 5
} finally {
t0 = 1
}
}
try {
yield *f()
} finally {
t1 = 1
}
}
g = gen3()
check_result(g.next(), 5, false)
check_result(g.return(13), 13, true)
assert(t0 === 1)
assert(t1 === 1)
t0 = -1
t1 = 0
function *gen4() {
function next(arg)
{
t0++;
if (t0 === 0)
{
assert(arg === undefined);
return { value:2, done:false }
}
if (t0 === 1)
{
assert(arg === -3);
return { value:3, done:false }
}
assert(arg === -4);
return { value:4, done:true }
}
var o = { [Symbol.iterator]() { return { next } } }
assert((yield *o) === 4)
return 5;
}
g = gen4()
check_result(g.next(-2), 2, false)
check_result(g.next(-3), 3, false)
check_result(g.next(-4), 5, true)
function *gen5() {
function *f() {
try {
yield 1
assert(false)
} catch (e) {
assert(e === 10)
}
return 2
}
assert((yield *f()) === 2)
yield 3
}
g = gen5()
check_result(g.next(), 1, false)
check_result(g.throw(10), 3, false)
+86
View File
@@ -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.
*/
/* This file checks yield syntax errors. */
function check_syntax_error(str)
{
try {
eval(str);
assert(false);
} catch (e) {
assert(e instanceof SyntaxError);
}
}
function * gen()
{
yield , yield
yield
, yield
(yield
)
yield[
1]
}
function*gen2()
{
1 ?
yield
:
yield
}
var gen3 = function*(){
(yield)/[yield]
}
check_syntax_error("function *gen(){ yield % yield }");
check_syntax_error("function *gen(){ (yield) % yield }");
check_syntax_error("function *gen(){ yield % (yield) }");
check_syntax_error("function *gen(){ (yield\n1) }");
check_syntax_error("function *gen(){ function yield() {} }");
check_syntax_error("function *gen(){ (yield)=>1 }");
check_syntax_error("function *gen(){ yield => 1 }");
check_syntax_error("function *gen(){ yi\\u0065ld 1 }");
function *gen4() {
var f = function yield(i) {
if (i = 0)
return yield(i + 1)
return 39
}
return f(0)
}
assert(gen4().next().value === 39);
(function *() {
() => yield
yield 1;
})
function *gen5() {
var o = {
["f"]() { yield % yield }
}
yield 1;
}
+199
View File
@@ -0,0 +1,199 @@
/* 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 file checks core generator operations. */
function check_result(result, value, done)
{
assert(result.value === value)
assert(result.done === done)
}
function * gen1(a = (t = 8)) {
var o = { p: 2 }
var x = 3.25
assert((o.p + (yield 10)) === 23)
assert((o.p + (yield 11)) === 24)
return x
}
/* Cannot be invoked with new. */
try {
new gen1
assert(false)
} catch (e) {
assert(e instanceof TypeError)
}
/* Fully read values. */
var t = 0
var g = gen1()
assert(t === 8)
check_result(g.next(20), 10, false)
check_result(g.next(21), 11, false)
check_result(g.next(22), 3.25, true)
check_result(g.next(23), undefined, true)
/* Partly read values (gc needs to free a suspended generator). */
t = 0
g = gen1()
assert(t === 8)
check_result(g.next(20), 10, false)
function * gen2() {
for (i in { x:0, y:1, z:2 })
{
let a = eval("'s'")
var b = yield a + i
assert (b === ++t)
}
}
/* Fully read values. */
t = 0
f = gen2()
check_result(f.next(0), "sx", false)
check_result(f.next(1), "sy", false)
check_result(f.next(2), "sz", false)
check_result(f.next(3), undefined, true)
check_result(f.next(4), undefined, true)
/* Partly read values (gc needs to free a suspended generator). */
f = gen2()
t = 0
check_result(f.next(0), "sx", false)
function *gen3() {
function f(yield) {
return -yield * 2
}
var g = (v) => {
assert(v === 6)
}
g(yield yield f(++t))
return 77
}
/* Fully read values. */
t = 0
f = gen3()
check_result(f.next(0), -2, false)
check_result(f.next(88), 88, false)
check_result(f.next(6), 77, true)
/* Partly read values (gc needs to free a suspended generator). */
t = 0
f = gen3()
check_result(f.next(0), -2, false)
function
/* generator: */ *
/* name: */ gen4() {
let a = eval("5")
with ({a})
{
let a = eval("6")
for (let a = 10; a < 11; a++)
{
let a = eval("7")
yield (a)
}
yield a, !assert(a === 6)
}
assert((yield a) === undefined)
}
/* Fully read values. */
f = gen4()
check_result(f.next(), 7, false)
check_result(f.next(), 6, false)
check_result(f.next(), 5, false)
check_result(f.next(), undefined, true)
/* Partly read values (gc needs to free a suspended generator). */
f = gen4()
check_result(f.next(), 7, false)
function*gen5(a,b,c,d) {
yield a
yield b
yield c
yield d
}
/* Fully read values. */
t = []
for(let i of gen5(1,3,5,7)) {
t.push(i)
}
assert(t.length === 4)
assert(t[0] === 1)
assert(t[1] === 3)
assert(t[2] === 5)
assert(t[3] === 7)
/* Partly read values (gc needs to free a suspended generator). */
t = []
for(let i of gen5(1,3,5,7)) {
t.push(i)
if (i === 3) {
break
}
}
assert(t.length === 2)
assert(t[0] === 1)
assert(t[1] === 3)
/* Recursive generator call. */
function* gen6(a,b,c,d) {
yield f.next()
}
f = gen6()
try {
f.next()
assert(false)
} catch (e) {
assert(e instanceof TypeError)
}
/* Parameterless yield. */
function* gen7() {
yield
}
f = gen7()
check_result(f.next(), undefined, false)
check_result(f.next(), undefined, true)
+36
View File
@@ -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 check_syntax_error (code) {
try {
eval(code)
assert (false)
} catch (e) {
assert (e instanceof SyntaxError)
}
}
eval("\u{000010C80}: break \ud803\udc80")
eval("\\u{10C80}: break \ud803\udc80")
eval("$\u{000010C80}$: break $\ud803\udc80$")
eval("$\\u{10C82}$: break $\ud803\udc82$")
assert("\u{000010C80}".length === 2)
assert("x\u{010C80}y".length === 4)
assert("\u{10C80}" === "\ud803\u{dc80}")
assert("\u{0}\x01" === "\u0000\u0001")
/* Surrogate pairs are not combined if they passed as \u sequences. */
check_syntax_error("\\u{10C80}: break \\ud803\\udc80");
@@ -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.
*/
class NoParent {
static [Symbol.hasInstance] (arg) {
return false;
}
}
var obj = new NoParent ();
assert ((obj instanceof NoParent) === false);
class PositiveNumber {
static [Symbol.hasInstance] (arg) {
return (arg instanceof Number) && (arg >= 0);
}
}
var num_a = new Number (33);
var num_b = new Number (-50);
assert ((num_a instanceof PositiveNumber) === true);
assert ((num_b instanceof PositiveNumber) === false);
class ErrorAlways {
static [Symbol.hasInstance] (arg) {
throw new URIError("ErrorAlways");
}
}
try {
(new Object ()) instanceof ErrorAlways;
assert (false);
} catch (ex) {
assert (ex instanceof URIError);
}
@@ -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.
*/
function NoParent () { }
Object.defineProperty (NoParent, Symbol.hasInstance, {
value: function (arg) { return false; }
});
var obj = new NoParent ();
assert ((obj instanceof NoParent) === false);
try {
Object.defineProperty (NoParent, Symbol.hasInstance, {
value: function (arg) { return true; }
});
assert (false)
} catch (ex) {
assert (ex instanceof TypeError);
}
function PositiveNumber () { }
Object.defineProperty (PositiveNumber, Symbol.hasInstance, {
value: function (arg) { return (arg instanceof Number) && (arg >= 0); }
})
var num_a = new Number (33);
var num_b = new Number (-50);
assert ((num_a instanceof PositiveNumber) === true);
assert ((num_b instanceof PositiveNumber) === false);
function ErrorAlways () { }
Object.defineProperty (ErrorAlways, Symbol.hasInstance, {
value: function (arg) { throw new URIError ("ErrorAlways"); }
})
try {
(new Object ()) instanceof ErrorAlways;
assert (false);
} catch (ex) {
assert (ex instanceof URIError);
}
function NonCallable () { }
Object.defineProperty (NonCallable, Symbol.hasInstance, { value: 11 });
try {
(new Object ()) instanceof NonCallable;
assert (false);
} catch (ex) {
assert (ex instanceof TypeError);
}
function ErrorGenerator () { }
Object.defineProperty (ErrorGenerator, Symbol.hasInstance, {
get: function () { throw new URIError ("ErrorGenerator"); }
});
try {
(new Object ()) instanceof ErrorGenerator;
assert (false);
} catch (ex) {
assert (ex instanceof URIError);
}
+21
View File
@@ -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 array = [];
var array_iterator = array[Symbol.iterator]();
var array_iterator_prototype = Object.getPrototypeOf (array_iterator);
var iterator_prototype = Object.getPrototypeOf (array_iterator_prototype);
var iterator_prototype_iterator = iterator_prototype[Symbol.iterator]();
assert (iterator_prototype === iterator_prototype_iterator);
+48
View File
@@ -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.
// Test with proxy
assert(JSON.stringify(new Proxy(['foo'], {})) === '["foo"]');
assert(JSON.stringify(new Proxy({0:"foo"}, {})) === '{"0":"foo"}');
var target = [1,2,3];
var handler = {
get(target, prop) {
if (prop == "length")
{
throw 42;
}
}
}
try {
JSON.stringify(new Proxy(target,handler));
assert(false);
} catch (e) {
assert(e === 42);
}
var revocable = Proxy.revocable (target, { get (t, p , r) {
if (p == "toJSON") {
revocable.revoke();
}
}});
var proxy = revocable.proxy;
try {
JSON.stringify(proxy);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
+146
View File
@@ -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.
*/
var builtin_objects = [
Array,
ArrayBuffer,
Boolean,
DataView,
Date,
Error,
EvalError,
Function,
Map,
Number,
Object,
Promise,
RangeError,
ReferenceError,
RegExp,
Set,
String,
Symbol,
SyntaxError,
TypeError,
URIError,
WeakMap,
WeakSet,
];
var builtin_typedArrays = [
Float32Array,
Float64Array,
Int16Array,
Int32Array,
Int8Array,
Uint16Array,
Uint32Array,
Uint8Array,
Uint8ClampedArray,
];
(function () {
/* each length property is configurable */
var desc;
for (obj of builtin_objects) {
desc = Object.getOwnPropertyDescriptor(obj, 'length');
assert(desc.writable === false);
assert(desc.enumerable === false);
assert(desc.configurable === true);
}
for (ta of builtin_typedArrays) {
desc = Object.getOwnPropertyDescriptor(ta, 'length');
assert(desc.writable === false);
assert(desc.enumerable === false);
assert(desc.configurable === true);
}
})();
(function () {
/* each length property can be deleted */
for (obj of builtin_objects) {
assert(obj.hasOwnProperty('length') === true);
assert(delete obj.length);
assert(obj.hasOwnProperty('length') === false);
}
for (ta of builtin_typedArrays) {
assert(ta.hasOwnProperty('length') === true);
assert(delete ta.length);
assert(ta.hasOwnProperty('length') === false);
}
})();
(function () {
/* test length property of builtin function */
for (obj of builtin_objects) {
var property_names = Object.getOwnPropertyNames(obj);
for (var name of property_names) {
if (typeof obj[name] == 'function') {
var func = obj[name];
var desc = Object.getOwnPropertyDescriptor(func, 'length');
assert(desc.writable === false);
assert(desc.enumerable === false);
assert(desc.configurable === true);
assert(func.hasOwnProperty('length') === true);
assert(delete func.length);
assert(func.hasOwnProperty('length') === false);
}
}
}
})();
(function () {
/* test length property of function objects */
var normal_func = function () {};
var arrow_func = () => {};
var bound_func = normal_func.bind({});
var nested_bound_func = arrow_func.bind().bind(1);
var functions = [normal_func, arrow_func, bound_func, nested_bound_func];
for (func of functions) {
var desc = Object.getOwnPropertyDescriptor(func, 'length');
assert(desc.writable === false);
assert(desc.enumerable === false);
assert(desc.configurable === true);
}
for (func of functions) {
assert(func.hasOwnProperty('length') === true);
assert(delete func.length);
assert(func.hasOwnProperty('length') === false);
}
})();
(function() {
/* changing the length of f affects the bound function */
function f(a,b,c) {}
Object.defineProperty(f, "length", { value: 30 });
var g = f.bind(1,2)
assert(g.length === 29);
})();
(function() {
/* changing the length of f does not affect the bound function */
function f(a,b,c) {}
var g = f.bind(1,2)
Object.defineProperty(f, "length", { value: 30 });
assert(g.length === 2);
})();

Some files were not shown because too many files have changed in this diff Show More