view tcb.js @ 24:777839fbafeb

Added some documentation to the TCB.
author Atul Varma <varmaa@toolness.com>
date Mon, 22 Jun 2009 08:56:31 -0700
parents cd5faa0bb46c
children 69622f55fcf6
line wrap: on
line source

// This script represents the Trusted Code Base (TCB) of the
// playground; it alone has access to all privileged functionality and
// can load SecurableModules as needed, exporting capabilities to them
// as necessary.

// 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);
}

// This function uses the Python-inspired traceback functionality of the
// playground to print a stack trace that looks much like Python's.

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'));
}

// An example of some of the Python-inspired traceback functionality of
// the playground.

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]);
}

// Load a sample SecurableModule and run some code in it.

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

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

// Some unit tests.

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!");