1 
  2 ////////////////////////////////////////////////////////////////////////////////
  3 /**
  4  * @fileOverview Copyright (C) 2009 www.webis.de<br/>
  5  * Set of assertion routines used for type checks and common pitfalls
  6  * @author Martin Potthast martin.potthast@uni-weimar.de
  7  * @author Christof Braeutigam christof.braeutigam@uni-weimar.de
  8  */
  9 
 10 function CHECK(condition, errorMessage) {
 11   if (!errorMessage) { errorMessage = ""; }
 12   if (!condition) { throw new Error("Check failed! " + errorMessage); }
 13 }
 14 
 15 function CHECK_TRUE(condition, errorMessage) {
 16   if (!errorMessage) { errorMessage = ""; }
 17   if (!condition) { throw new Error("Check true failed! " + errorMessage); }
 18 }
 19 
 20 function CHECK_FALSE(condition, errorMessage) {
 21   if (!errorMessage) { errorMessage = ""; }
 22   if (condition) { throw new Error("Check false failed! " + errorMessage); }
 23 }
 24 
 25 function CHECK_TYPE(arg, type, errorMessage) {
 26   if (!errorMessage) { errorMessage = ""; }
 27   if (typeof(arg) !== type) { 
 28     throw new Error(
 29       "Check failed: got " + typeof(arg) + ", expected " + type + ". " + 
 30       errorMessage
 31     );
 32   }
 33 }
 34 
 35 function CHECK_NOT_NULL(arg, errorMessage) {
 36   if (!errorMessage) { errorMessage = ""; }
 37   if (arg === null) {
 38     throw new Error("Check failed: arg is null. "  + errorMessage);
 39   }
 40 }
 41 
 42 function CHECK_NOT_UNDEFINED(arg, errorMessage) {
 43   if (!errorMessage) { errorMessage = ""; }
 44   if (typeof(arg) === "undefined") {
 45     throw new Error("Check failed: arg is undefined. "  + errorMessage);
 46   }
 47 }
 48 
 49 function getStackTrace() {
 50   var callstack = [];
 51   var isCallstackPopulated = false;
 52   try {
 53     i.dont.exist+=0; //doesn't exist- that's the point
 54   } catch(e) {
 55     if (e.stack) { //Firefox
 56       var lines = e.stack.split("\n");
 57       for (var i=0, len=lines.length; i<len; i++) {
 58         if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
 59           callstack.push(lines[i]);
 60         }
 61       }
 62       //Remove call to printStackTrace()
 63       callstack.shift();
 64       isCallstackPopulated = true;
 65     }
 66     else if (window.opera && e.message) { //Opera
 67       var lines = e.message.split("\n");
 68       for (var i=0, len=lines.length; i<len; i++) {
 69         if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
 70           var entry = lines[i];
 71           //Append next line also since it has the file info
 72           if (lines[i+1]) {
 73             entry += " at " + lines[i+1];
 74             i++;
 75           }
 76           callstack.push(entry);
 77         }
 78       }
 79       //Remove call to printStackTrace()
 80       callstack.shift();
 81       isCallstackPopulated = true;
 82     }
 83   }
 84   if (!isCallstackPopulated) { //IE and Safari
 85     var currentFunction = arguments.callee.caller;
 86     while (currentFunction) {
 87       var fn = currentFunction.toString();
 88       var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
 89       callstack.push(fname);
 90       currentFunction = currentFunction.caller;
 91     }
 92   }
 93   return callstack.join("\n");
 94 }
 95 
 96 // Check.js
 97 ////////////////////////////////////////////////////////////////////////////////
 98