1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Flags: --harmony-modules
30 // Test basic module linking and initialization.
35 // At this point, only functions and modules are initialized.
36 assertEquals(undefined, v)
37 assertEquals(undefined, vv)
38 assertEquals(undefined, R.v)
39 assertEquals(undefined, M.v)
40 assertEquals(undefined, MM.v)
41 assertEquals(undefined, F.v)
42 assertEquals(undefined, G.v)
43 assertThrows(function() { l }, ReferenceError)
44 assertThrows(function() { ll }, ReferenceError)
45 assertThrows(function() { R.l }, ReferenceError)
46 assertThrows(function() { M.l }, ReferenceError)
47 assertThrows(function() { MM.l }, ReferenceError)
48 assertThrows(function() { F.l }, ReferenceError)
49 assertThrows(function() { G.l }, ReferenceError)
50 assertThrows(function() { c }, ReferenceError)
51 assertThrows(function() { cc }, ReferenceError)
52 assertThrows(function() { R.c }, ReferenceError)
53 assertThrows(function() { M.c }, ReferenceError)
54 assertThrows(function() { MM.c }, ReferenceError)
55 assertThrows(function() { F.c }, ReferenceError)
56 assertThrows(function() { G.c }, ReferenceError)
58 assertEquals(24, ff())
59 assertEquals(4, R.f())
60 assertEquals(14, M.f())
61 assertEquals(34, MM.f())
62 assertEquals(44, F.f())
63 assertEquals(14, G.f())
65 // All properties should already exist on the instance objects, though.
93 assertTrue("RR" in RR)
95 // And aliases should be identical.
101 // We can only assign to var.
102 assertEquals(-1, v = -1)
103 assertEquals(-2, R.v = -2)
105 assertEquals(-2, R.v)
107 assertThrows(function() { l = -1 }, ReferenceError)
108 assertThrows(function() { R.l = -2 }, ReferenceError)
109 assertThrows(function() { l }, ReferenceError)
110 assertThrows(function() { R.l }, ReferenceError)
112 assertThrows(function() { eval("c = -1") }, TypeError)
113 assertThrows(function() { R.c = -2 }, TypeError)
115 // Initialize first bunch of variables.
119 export function f() { return 4 }
128 assertEquals(-3, v = -3)
129 assertEquals(-4, R.v = -4)
130 assertEquals(-3, l = -3)
131 assertEquals(-4, R.l = -4)
132 assertThrows(function() { eval("c = -3") }, TypeError)
133 assertThrows(function() { R.c = -4 }, TypeError)
136 assertEquals(-4, R.v)
138 assertEquals(-4, R.l)
142 // Initialize nested module.
147 export function f() { return 14 }
150 assertEquals(11, M.v)
151 assertEquals(11, G.v)
152 assertEquals(12, M.l)
153 assertEquals(12, G.l)
154 assertEquals(13, M.c)
155 assertEquals(13, G.c)
157 // Initialize non-exported variables.
161 function ff() { return 24 }
167 // Initialize non-exported module.
172 export function f() { return 34 }
175 assertEquals(31, MM.v)
176 assertEquals(32, MM.l)
177 assertEquals(33, MM.c)
179 // Recursive self reference.
183 // Initialize sibling module that was forward-used.
185 assertEquals(undefined, v)
186 assertEquals(undefined, F.v)
187 assertThrows(function() { l }, ReferenceError)
188 assertThrows(function() { F.l }, ReferenceError)
189 assertThrows(function() { c }, ReferenceError)
190 assertThrows(function() { F.c }, ReferenceError)
195 export function f() { return 44 }
198 assertEquals(41, F.v)
200 assertEquals(42, F.l)
202 assertEquals(43, F.c)
205 // Define recursive module alias.
210 // Second test with side effects and more module nesting.
214 export let x = (log += "1");
216 export module B = A.B
219 export let x = (log += "2");
220 let y = (log += "3");
221 export function f() { log += "5" };
225 let x = (log += "4");
227 let y = (log += "6");
229 export let z = (log += "7");
231 export let z = (log += "8");
233 export module C = A.C
239 export module A2 = M2;
240 export let x = (log += "9");
243 export module A1 = M1;
244 export let x = (log += "0");
247 assertEquals("object", typeof A);
248 assertTrue('x' in A);
249 assertFalse('y' in A);
250 assertTrue('f' in A);
251 assertTrue('B' in A);
252 assertTrue('z' in A);
253 assertTrue('C' in A);
254 assertFalse('D' in A);
256 assertEquals("object", typeof B);
257 assertTrue('BB' in B);
258 assertTrue('x' in B);
259 assertFalse('y' in B);
261 assertEquals("object", typeof A.B);
262 assertTrue('BB' in A.B);
263 assertTrue('x' in A.B);
264 assertFalse('y' in A.B);
266 assertEquals("object", typeof A.B.BB);
267 assertTrue('BB' in A.B.BB);
268 assertTrue('x' in A.B.BB);
269 assertFalse('y' in A.B.BB);
271 assertEquals("object", typeof A.C);
272 assertTrue('z' in A.C);
273 assertTrue('D' in A.C);
274 assertTrue('C' in A.C);
276 assertEquals("object", typeof M1);
277 assertEquals("object", typeof M2);
278 assertTrue('A2' in M1);
279 assertTrue('A1' in M2);
280 assertEquals("object", typeof M1.A2);
281 assertEquals("object", typeof M2.A1);
282 assertTrue('A1' in M1.A2);
283 assertTrue('A2' in M2.A1);
284 assertEquals("object", typeof M1.A2.A1);
285 assertEquals("object", typeof M2.A1.A2);
289 assertSame(B, A.C.D);
290 assertSame(A.C, A.C.C);
291 assertFalse(A.D === A.C.D);
293 assertSame(M1, M2.A1);
294 assertSame(M2, M1.A2);
295 assertSame(M1, M1.A2.A1);
296 assertSame(M2, M2.A1.A2);
298 assertEquals("1234567890", log);