michael@0: // Test that SavedFrame.prototype.parent gives the next older frame whose michael@0: // principals are subsumed by the caller's principals. 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: while (stack.length && expected.length) { michael@0: assertEq(stack.shift(), expected[0]); michael@0: expected = expected.slice(1); michael@0: } michael@0: michael@0: if (expected.length > 0) { michael@0: throw new Error("Missing frames for: " + expected); michael@0: } michael@0: if (stack.length > 0 && !stack.every(s => s === null)) { michael@0: throw new Error("Unexpected extra frame(s):\n" + stack); michael@0: } michael@0: } michael@0: michael@0: // Go from a SavedFrame linked list to an array of function display names. michael@0: function extract(stack) { michael@0: const results = []; michael@0: while (stack) { michael@0: results.push(stack.functionDisplayName); michael@0: stack = stack.parent; michael@0: } michael@0: return results; michael@0: } michael@0: michael@0: const low = newGlobal({ principal: 0 }); michael@0: const mid = newGlobal({ principal: 0xffff }); michael@0: const high = newGlobal({ principal: 0xfffff }); michael@0: michael@0: var count = 0; michael@0: michael@0: eval('function a() { check("a", extract(saveStack())); b(); }'); michael@0: low .eval('function b() { check("b", extract(saveStack())); c(); }'); michael@0: mid .eval('function c() { check("cba", extract(saveStack())); d(); }'); michael@0: high.eval('function d() { check("dcba", extract(saveStack())); e(); }'); michael@0: eval('function e() { check("edcba", extract(saveStack())); f(); }'); // no principal, so checks skipped michael@0: low .eval('function f() { check("fb", extract(saveStack())); g(); }'); michael@0: mid .eval('function g() { check("gfecba", extract(saveStack())); h(); }'); michael@0: high.eval('function h() { check("hgfedcba", extract(saveStack())); }'); 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: // They each must have their own extract so that CCWs don't mess up the michael@0: // principals when we ask for the parent property. michael@0: low. eval("" + extract); michael@0: mid. eval("" + extract); michael@0: high.eval("" + extract); michael@0: michael@0: // Kick the whole process off. michael@0: a(); michael@0: michael@0: assertEq(count, 8);