|
1 // Test the JS shell's toy principals. |
|
2 |
|
3 var count = 0; |
|
4 |
|
5 // Given a string of letters |expected|, say "abc", assert that the stack |
|
6 // contains calls to a series of functions named by the next letter from |
|
7 // the string, say a, b, and then c. Younger frames appear earlier in |
|
8 // |expected| than older frames. |
|
9 function check(expected, stack) { |
|
10 print("check(" + uneval(expected) + ") against:\n" + stack); |
|
11 count++; |
|
12 |
|
13 // Extract only the function names from the stack trace. Omit the frames |
|
14 // for the top-level evaluation, if it is present. |
|
15 var split = stack.split(/(.)?@.*\n/).slice(0, -1); |
|
16 if (split[split.length - 1] === undefined) |
|
17 split = split.slice(0, -2); |
|
18 |
|
19 // Check the function names against the expected sequence. |
|
20 assertEq(split.length, expected.length * 2); |
|
21 for (var i = 0; i < expected.length; i++) |
|
22 assertEq(split[i * 2 + 1], expected[i]); |
|
23 } |
|
24 |
|
25 var low = newGlobal({ principal: 0 }); |
|
26 var mid = newGlobal({ principal: 0xffff }); |
|
27 var high = newGlobal({ principal: 0xfffff }); |
|
28 |
|
29 eval('function a() { check("a", Error().stack); b(); }'); |
|
30 low .eval('function b() { check("b", Error().stack); c(); }'); |
|
31 mid .eval('function c() { check("cba", Error().stack); d(); }'); |
|
32 high.eval('function d() { check("dcba", Error().stack); e(); }'); |
|
33 eval('function e() { check("edcba", Error().stack); f(); }'); // no principal, so checks skipped |
|
34 low .eval('function f() { check("fb", Error().stack); g(); }'); |
|
35 mid .eval('function g() { check("gfecba", Error().stack); h(); }'); |
|
36 high.eval('function h() { check("hgfedcba", Error().stack); }'); |
|
37 |
|
38 // Make everyone's functions visible to each other, as needed. |
|
39 b = low .b; |
|
40 low .c = mid .c; |
|
41 mid .d = high.d; |
|
42 high.e = e; |
|
43 f = low .f; |
|
44 low .g = mid .g; |
|
45 mid .h = high.h; |
|
46 |
|
47 low.check = mid.check = high.check = check; |
|
48 |
|
49 // Kick the whole process off. |
|
50 a(); |
|
51 |
|
52 assertEq(count, 8); |