michael@0: // Test the JS shell's toy principals. michael@0: michael@0: var count = 0; michael@0: michael@0: // Given a string of letters |expected|, say "abc", assert that the stack michael@0: // contains calls to a series of functions named by the next letter from michael@0: // the string, say a, b, and then c. Younger frames appear earlier in michael@0: // |expected| than older frames. michael@0: function check(expected, stack) { michael@0: print("check(" + uneval(expected) + ") against:\n" + stack); michael@0: count++; michael@0: michael@0: // Extract only the function names from the stack trace. Omit the frames michael@0: // for the top-level evaluation, if it is present. michael@0: var split = stack.split(/(.)?@.*\n/).slice(0, -1); michael@0: if (split[split.length - 1] === undefined) michael@0: split = split.slice(0, -2); michael@0: michael@0: // Check the function names against the expected sequence. michael@0: assertEq(split.length, expected.length * 2); michael@0: for (var i = 0; i < expected.length; i++) michael@0: assertEq(split[i * 2 + 1], expected[i]); michael@0: } michael@0: michael@0: var low = newGlobal({ principal: 0 }); michael@0: var mid = newGlobal({ principal: 0xffff }); michael@0: var high = newGlobal({ principal: 0xfffff }); michael@0: michael@0: eval('function a() { check("a", Error().stack); b(); }'); michael@0: low .eval('function b() { check("b", Error().stack); c(); }'); michael@0: mid .eval('function c() { check("cba", Error().stack); d(); }'); michael@0: high.eval('function d() { check("dcba", Error().stack); e(); }'); michael@0: eval('function e() { check("edcba", Error().stack); f(); }'); // no principal, so checks skipped michael@0: low .eval('function f() { check("fb", Error().stack); g(); }'); michael@0: mid .eval('function g() { check("gfecba", Error().stack); h(); }'); michael@0: high.eval('function h() { check("hgfedcba", Error().stack); }'); michael@0: michael@0: // Make everyone's functions visible to each other, as needed. michael@0: b = low .b; michael@0: low .c = mid .c; michael@0: mid .d = high.d; michael@0: high.e = e; michael@0: f = low .f; michael@0: low .g = mid .g; michael@0: mid .h = high.h; michael@0: michael@0: low.check = mid.check = high.check = check; michael@0: michael@0: // Kick the whole process off. michael@0: a(); michael@0: michael@0: assertEq(count, 8);