js/src/jit-test/tests/basic/shell-principals.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/basic/shell-principals.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,52 @@
     1.4 +// Test the JS shell's toy principals.
     1.5 +
     1.6 +var count = 0;
     1.7 +
     1.8 +// Given a string of letters |expected|, say "abc", assert that the stack
     1.9 +// contains calls to a series of functions named by the next letter from
    1.10 +// the string, say a, b, and then c. Younger frames appear earlier in
    1.11 +// |expected| than older frames.
    1.12 +function check(expected, stack) {
    1.13 +  print("check(" + uneval(expected) + ") against:\n" + stack);
    1.14 +  count++;
    1.15 +
    1.16 +  // Extract only the function names from the stack trace. Omit the frames
    1.17 +  // for the top-level evaluation, if it is present.
    1.18 +  var split = stack.split(/(.)?@.*\n/).slice(0, -1);
    1.19 +  if (split[split.length - 1] === undefined)
    1.20 +    split = split.slice(0, -2);
    1.21 +
    1.22 +  // Check the function names against the expected sequence.
    1.23 +  assertEq(split.length, expected.length * 2);
    1.24 +  for (var i = 0; i < expected.length; i++)
    1.25 +    assertEq(split[i * 2 + 1], expected[i]);
    1.26 +}
    1.27 +
    1.28 +var low = newGlobal({ principal: 0 });
    1.29 +var mid = newGlobal({ principal: 0xffff });
    1.30 +var high = newGlobal({ principal: 0xfffff });
    1.31 +
    1.32 +     eval('function a() { check("a",        Error().stack); b(); }');
    1.33 +low .eval('function b() { check("b",        Error().stack); c(); }');
    1.34 +mid .eval('function c() { check("cba",      Error().stack); d(); }');
    1.35 +high.eval('function d() { check("dcba",     Error().stack); e(); }');
    1.36 +     eval('function e() { check("edcba",    Error().stack); f(); }'); // no principal, so checks skipped
    1.37 +low .eval('function f() { check("fb",       Error().stack); g(); }');
    1.38 +mid .eval('function g() { check("gfecba",   Error().stack); h(); }');
    1.39 +high.eval('function h() { check("hgfedcba", Error().stack);      }');
    1.40 +
    1.41 +// Make everyone's functions visible to each other, as needed.
    1.42 +     b = low .b;
    1.43 +low .c = mid .c;
    1.44 +mid .d = high.d;
    1.45 +high.e =      e;
    1.46 +     f = low .f;
    1.47 +low .g = mid .g;
    1.48 +mid .h = high.h;
    1.49 +
    1.50 +low.check = mid.check = high.check = check;
    1.51 +
    1.52 +// Kick the whole process off.
    1.53 +a();
    1.54 +
    1.55 +assertEq(count, 8);

mercurial