view tcb.js @ 21:cd5faa0bb46c

Added more tests for unwrap()/getWrapper().
author Atul Varma <varmaa@toolness.com>
date Mon, 22 Jun 2009 08:31:23 -0700
parents 802ab1d478c6
children 777839fbafeb
line wrap: on
line source

// This security function is called by the platform whenever a
// particular property needs to be accessed on a particular object.
function checkAccess(obj, id) {
  var frame = stack().caller;
  var isSuspicious = false;
  if (!(frame.filename == null ||
        frame.filename == "tcb.js")) {
    isSuspicious = true;
    print("access request from " + frame.filename + " on property '" + id +
          "' of " + obj);
  }
  if (id == 'caller') {
    if (frame.caller &&
        frame.caller.functionObject &&
        !(isSuspicious &&
          (functionInfo(frame.caller.functionObject).filename !=
           frame.filename))) {
      return frame.caller.functionObject;
    } else
      return null;
  }
  return lookupProperty(obj, id);
}

// This function is called by the platform whenever an uncaught exception
// occurs.
function handleError() {
  printTraceback(lastExceptionTraceback);
  print(lastException);
}

function printTraceback(frame) {
  print("Traceback (most recent call last):");
  if (frame === undefined)
    frame = stack();
  var lines = [];
  while (frame) {
    var line = ('  File "' + frame.filename + '", line ' +
                frame.lineNo + ', in ' + frame.functionName);
    lines.splice(0, 0, line);
    frame = frame.caller;
  }
  print(lines.join('\n'));
}

function throwError() {
  function innerThrowError() {
    var x = 1;
    throw new Error("hi");
  }
  innerThrowError();
}

try {
  throwError();
} catch (e) {
  print("caught an intentional error. local variables in scope chain: ");
  var scopeChain = lastExceptionTraceback.scopeChain;
  for (name in scopeChain)
    print("  " + name + ": " + scopeChain[name]);
}

var module = require("sample-module.js", {blop: "hello", print: print});

(function() {
   print("module.foo() is " + module.foo());
   })();

var wrapper = {};
var wrappee = {};
var wrapped = wrap(wrappee, wrapper);

if (unwrap({}) !== null)
  throw new Error("Unwrapping a non-wrapped object should return null!");

if (getWrapper({}) !== null)
  throw new Error("Getting the wrapper of a non-wrapped object should " +
                  "return null!");

if (unwrap(wrapped) !== wrappee ||
    unwrap(unwrap(wrapped)) !== null)
  throw new Error("Unwrapping doesn't work!");

if (getWrapper(wrapped) !== wrapper ||
    getWrapper(getWrapper(wrapped)) !== null)
  throw new Error("Getting the wrapper doesn't work!");