debug.js (2359B)
1 /* debug.js - debugging-support for SPOCK runtime */ 2 3 4 SPOCK.debug = true; 5 SPOCK.toString = function() { return "#<SPOCK>"; }; 6 SPOCK.restartCount = 0; 7 SPOCK.traceBuffer = []; 8 SPOCK.traceOutput = false; 9 SPOCK.traceHook = []; 10 SPOCK.hasAlert = SPOCK.inBrowser; 11 12 // Overrides SPOCK.error 13 SPOCK.error = function(msg) { // "msg" may be a string or an error object 14 var args = Array.prototype.splice.call(arguments, 1); 15 var err; 16 var text; 17 18 if(typeof msg !== "string") { // an object? 19 err = msg; 20 msg = err.message; 21 } 22 23 text = msg; 24 25 if(args.length > 1) 26 text += ":\n " + SPOCK.map(SPOCK.stringify, args).join("\n "); 27 28 if(SPOCK.traceBuffer.length > 0) 29 text += "\n\nCall trace:\n\n" + SPOCK.getTrace(); 30 31 if(SPOCK.hasAlert) { 32 alert("Error: " + text); 33 SPOCK.hasAlert = false; // disable to avoid endless repetition of alerts 34 } 35 else if(err) throw new (err.constructor)(text); 36 else throw new Error(text); 37 }; 38 39 // Overrides SPOCK.count 40 SPOCK.count = function(args, loc) { 41 if(--SPOCK.stack <= 0) { 42 ++SPOCK.restartCount; 43 return new SPOCK.Continuation(args.callee, Array.prototype.slice.call(args)); 44 } 45 46 if(loc) SPOCK.trace(loc, args); 47 48 return false; 49 }; 50 51 SPOCK.trace = function(name, args) { 52 var tb = SPOCK.traceBuffer; 53 54 for(var i in SPOCK.traceHook) 55 (SPOCK.traceHook[ i ])(name, args); 56 57 if(SPOCK.traceOutput) 58 SPOCK.log("[" + SPOCK.stack + "] " + name); 59 60 if(tb.length >= SPOCK.TRACELENGTH) tb.shift(); 61 62 tb.push([name, Array.prototype.slice.call(args, 1)]); // skip continuation argument 63 }; 64 65 SPOCK.getTrace = function() { 66 var tb = SPOCK.traceBuffer; 67 var trace = []; 68 69 for(var i in tb) { 70 var e = tb[ i ]; 71 trace.push(" (" + e[ 0 ] + " " + 72 SPOCK.map(SPOCK.stringify, e[ 1 ]).join(" ") + ")"); 73 } 74 75 SPOCK.traceBuffer = []; 76 return trace.join("\n") + " <---"; 77 }; 78 79 // Overrides empty SPOCK.statistics 80 SPOCK.statistics = function() { 81 //if(SPOCK.restartCount > 0) 82 // SPOCK.log("restarts: ", SPOCK.restartCount); 83 84 SPOCK.traceBuffer = []; 85 }; 86 87 // Overrides SPOCK.callback 88 (function() { 89 var old = SPOCK.callback; 90 91 SPOCK.callback = function(proc) { 92 var cb = old(proc); 93 return function() { 94 var args = Array.prototype.slice.call(arguments); 95 SPOCK.trace("<callback>", args); 96 return cb.apply(this, args); 97 }; 98 }; 99 })();