1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/saved-stacks/principals-01.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +// Test that SavedFrame.prototype.parent gives the next older frame whose 1.5 +// principals are subsumed by the caller's principals. 1.6 + 1.7 +// Given a string of letters |expected|, say "abc", assert that the stack 1.8 +// contains calls to a series of functions named by the next letter from 1.9 +// the string, say a, b, and then c. Younger frames appear earlier in 1.10 +// |expected| than older frames. 1.11 +function check(expected, stack) { 1.12 + print("check(" + uneval(expected) + ") against:\n" + stack); 1.13 + count++; 1.14 + 1.15 + while (stack.length && expected.length) { 1.16 + assertEq(stack.shift(), expected[0]); 1.17 + expected = expected.slice(1); 1.18 + } 1.19 + 1.20 + if (expected.length > 0) { 1.21 + throw new Error("Missing frames for: " + expected); 1.22 + } 1.23 + if (stack.length > 0 && !stack.every(s => s === null)) { 1.24 + throw new Error("Unexpected extra frame(s):\n" + stack); 1.25 + } 1.26 +} 1.27 + 1.28 +// Go from a SavedFrame linked list to an array of function display names. 1.29 +function extract(stack) { 1.30 + const results = []; 1.31 + while (stack) { 1.32 + results.push(stack.functionDisplayName); 1.33 + stack = stack.parent; 1.34 + } 1.35 + return results; 1.36 +} 1.37 + 1.38 +const low = newGlobal({ principal: 0 }); 1.39 +const mid = newGlobal({ principal: 0xffff }); 1.40 +const high = newGlobal({ principal: 0xfffff }); 1.41 + 1.42 +var count = 0; 1.43 + 1.44 + eval('function a() { check("a", extract(saveStack())); b(); }'); 1.45 +low .eval('function b() { check("b", extract(saveStack())); c(); }'); 1.46 +mid .eval('function c() { check("cba", extract(saveStack())); d(); }'); 1.47 +high.eval('function d() { check("dcba", extract(saveStack())); e(); }'); 1.48 + eval('function e() { check("edcba", extract(saveStack())); f(); }'); // no principal, so checks skipped 1.49 +low .eval('function f() { check("fb", extract(saveStack())); g(); }'); 1.50 +mid .eval('function g() { check("gfecba", extract(saveStack())); h(); }'); 1.51 +high.eval('function h() { check("hgfedcba", extract(saveStack())); }'); 1.52 + 1.53 +// Make everyone's functions visible to each other, as needed. 1.54 + b = low .b; 1.55 +low .c = mid .c; 1.56 +mid .d = high.d; 1.57 +high.e = e; 1.58 + f = low .f; 1.59 +low .g = mid .g; 1.60 +mid .h = high.h; 1.61 + 1.62 +low.check = mid.check = high.check = check; 1.63 + 1.64 +// They each must have their own extract so that CCWs don't mess up the 1.65 +// principals when we ask for the parent property. 1.66 +low. eval("" + extract); 1.67 +mid. eval("" + extract); 1.68 +high.eval("" + extract); 1.69 + 1.70 +// Kick the whole process off. 1.71 +a(); 1.72 + 1.73 +assertEq(count, 8);