Implement ES2015 class feature (part II.) (#2439)
This patch is the second milestone of the implementation of this new language element. Supported: - Single class inheritance - Functionality of 'super' keyword - Implicit constructor in class heritage - Specific behaviour while extending with the built-in 'Array' or '%TypedArray%' object - Abstract subclasses (Mix-ins) JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
committed by
Akos Kiss
parent
e0e6363f85
commit
d1860d0e34
@@ -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,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 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)
|
||||
|
||||
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,133 @@
|
||||
/* 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 () } }");
|
||||
|
||||
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,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 ()
|
||||
@@ -223,7 +223,7 @@ main (void)
|
||||
/* Check the snapshot data. Unused bytes should be filled with zeroes */
|
||||
const uint8_t expected_data[] =
|
||||
{
|
||||
0x4A, 0x52, 0x52, 0x59, 0x12, 0x00, 0x00, 0x00,
|
||||
0x4A, 0x52, 0x52, 0x59, 0x13, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
|
||||
0x03, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
|
||||
|
||||
Reference in New Issue
Block a user