• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 function isInstanceofArray (instance) {
17   assert (instance instanceof C);
18   assert (instance instanceof B);
19   assert (instance instanceof A);
20   assert (instance instanceof Array);
21 }
22
23 class A extends Array {
24   f () {
25     return 5;
26   }
27 }
28
29 class B extends A {
30   g () {
31     return eval ("eval ('super.f ()')");
32   }
33 }
34
35 class C extends B {
36   h () {
37     return eval ('super.g ()');
38   }
39 }
40
41 var c = new C (1, 2, 3, 4, 5, 6);
42
43 isInstanceofArray (c);
44 c.push (7);
45 assert (c.length === 7);
46 assert (c.f () === 5);
47 assert (c.g () === 5);
48 assert (c.h () === 5);
49
50 // Test built-in Array prototype methods
51 var mapped = c.map ((x) => x * 2);
52 isInstanceofArray (mapped);
53
54 for (var i = 0; i < mapped.length; i++) {
55   assert (mapped[i] == c[i] * 2);
56 }
57
58 var concated = c.concat (c);
59 isInstanceofArray (concated);
60
61 for (var i = 0; i < concated.length; i++) {
62   assert (concated[i] == c[i % (concated.length / 2)]);
63 }
64
65 var sliced = c.slice (c);
66 isInstanceofArray (sliced);
67
68 for (var i = 0; i < sliced.length; i++) {
69   assert (sliced[i] == c[i]);
70 }
71
72 var filtered = c.filter ((x) => x > 100);
73 isInstanceofArray (sliced);
74 assert (filtered.length === 0);
75
76 var spliced = c.splice (c.length - 1);
77 isInstanceofArray (spliced);
78 assert (spliced.length === 1);
79 assert (spliced[0] === 7);
80
81 c.constructor = 5;
82
83 try {
84   mapped = c.map ((x) => x * 2);
85   assert (false);
86 } catch (e) {
87   assert (e instanceof TypeError);
88 }
89
90 try {
91   concated = c.concat (c);
92   assert (false);
93 } catch (e) {
94   assert (e instanceof TypeError);
95 }
96
97 try {
98   sliced = c.slice (c);
99   assert (false);
100 } catch (e) {
101   assert (e instanceof TypeError);
102 }
103
104 try {
105   filtered = c.filter ((x) => x > 100);
106   assert (false);
107 } catch (e) {
108   assert (e instanceof TypeError);
109 }
110
111 try {
112   spliced = c.splice (0);
113   assert (false);
114 } catch (e) {
115   assert (e instanceof TypeError);
116 }
117