diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-04-20 18:13:03 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-04-20 18:13:03 -0700 |
commit | 45bcf3ad360c950765db789bdd54d111d044ffc5 (patch) | |
tree | 13ca2b7b7c88165e9f191cde549f883f420d633f | |
parent | 410a632a1888bde9b78d574aa682f4dc48fb488f (diff) |
make library and gl debug configurable at runtime, and share the pretty printing code
-rw-r--r-- | src/compiler.js | 2 | ||||
-rw-r--r-- | src/jsifier.js | 4 | ||||
-rw-r--r-- | src/library_browser.js | 49 | ||||
-rw-r--r-- | src/runtime.js | 52 | ||||
-rw-r--r-- | src/settings.js | 8 |
5 files changed, 64 insertions, 51 deletions
diff --git a/src/compiler.js b/src/compiler.js index 4442e38e..89da32d5 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -146,6 +146,8 @@ if (PGO) { // by default, correct everything during PGO EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); EXPORTED_GLOBALS = set(EXPORTED_GLOBALS); +RUNTIME_DEBUG = LIBRARY_DEBUG || GL_DEBUG; + // Settings sanity checks assert(!(USE_TYPED_ARRAYS === 2 && QUANTUM_SIZE !== 4), 'For USE_TYPED_ARRAYS == 2, must have normal QUANTUM_SIZE of 4'); diff --git a/src/jsifier.js b/src/jsifier.js index 904517e1..70fe0991 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -408,8 +408,8 @@ function JSify(data, functionsOnly, givenFunctions) { // name the function; overwrite if it's already named snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function _' + ident + '('); if (LIBRARY_DEBUG) { - snippet = snippet.replace('{', '{ var ret = (function() {Module.printErr("[library call:' + ident + ': " + Array.prototype.slice.call(arguments) + "]"); '); - snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); Module.printErr(" [ return:" + ret); return ret; }'; + snippet = snippet.replace('{', '{ var ret = (function() { if (Runtime.debug) Module.printErr("[library call:' + ident + ': " + Array.prototype.slice.call(arguments).map(Runtime.prettyPrint) + "]"); '); + snippet = snippet.substr(0, snippet.length-1) + '}).apply(this, arguments); if (Runtime.debug && typeof ret !== "undefined") Module.printErr(" [ return:" + Runtime.prettyPrint(ret)); return ret; }'; } } diff --git a/src/library_browser.js b/src/library_browser.js index 3a59d751..a6a420fa 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -60,63 +60,18 @@ mergeInto(LibraryManager.library, { // Useful to debug native webgl apps: var Module = { printErr: function(x) { console.log(x) } }; var tempCtx = ctx; var wrapper = {}; - wrapper.objectMap = new WeakMap(); - wrapper.objectCounter = 1; - function prettify(arg) { - if (typeof arg == 'undefined') return '!UNDEFINED!'; - if (!arg) return arg; - if (wrapper.objectMap[arg]) return '<' + arg + '|' + wrapper.objectMap[arg] + '>'; - if (arg.toString() == '[object HTMLImageElement]') { - return arg + '\n\n'; - } - if (arg.byteLength) { - return '{' + Array.prototype.slice.call(arg, 0, Math.min(arg.length, 40)) + '}'; // Useful for correct arrays, less so for compiled arrays, see the code below for that - var buf = new ArrayBuffer(32); - var i8buf = new Int8Array(buf); - var i16buf = new Int16Array(buf); - var f32buf = new Float32Array(buf); - switch(arg.toString()) { - case '[object Uint8Array]': - i8buf.set(arg.subarray(0, 32)); - break; - case '[object Float32Array]': - f32buf.set(arg.subarray(0, 5)); - break; - case '[object Uint16Array]': - i16buf.set(arg.subarray(0, 16)); - break; - default: - alert('unknown array for debugging: ' + arg); - throw 'see alert'; - } - var ret = '{' + arg.byteLength + ':\n'; - var arr = Array.prototype.slice.call(i8buf); - ret += 'i8:' + arr.toString().replace(/,/g, ',') + '\n'; - arr = Array.prototype.slice.call(f32buf, 0, 8); - ret += 'f32:' + arr.toString().replace(/,/g, ',') + '}'; - return ret; - } - if (typeof arg == 'object') { - wrapper.objectMap[arg] = wrapper.objectCounter++; - return '<' + arg + '|' + wrapper.objectMap[arg] + '>'; - } - if (typeof arg == 'number') { - return '0x' + arg.toString(16); - } - return arg; - } for (var prop in tempCtx) { (function(prop) { switch (typeof tempCtx[prop]) { case 'function': { wrapper[prop] = function() { if (GL.debug) { - var printArgs = Array.prototype.slice.call(arguments).map(prettify) + var printArgs = Array.prototype.slice.call(arguments).map(Runtime.prettyPrint); console.log('[gl_f:' + prop + ':' + printArgs + ']'); } var ret = tempCtx[prop].apply(tempCtx, arguments); if (GL.debug && typeof ret != 'undefined') { - console.log('[ gl:' + prop + ':return:' + prettify(ret) + ']'); + console.log('[ gl:' + prop + ':return:' + Runtime.prettyPrint(ret) + ']'); } return ret; } diff --git a/src/runtime.js b/src/runtime.js index 0e4b7b2d..fe6481f5 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -313,7 +313,59 @@ var Runtime = { FUNCTION_TABLE.push(func); FUNCTION_TABLE.push(0); return ret; + }, + +#if RUNTIME_DEBUG + debug: false, // Switch to true at runtime to enable logging at the right times + + printObjectMap: null, + printObjectCounter: 1, + + prettyPrint: function(arg) { + if (!Runtime.printObjectMap) Runtime.printObjectMap = new WeakMap(); + if (typeof arg == 'undefined') return '!UNDEFINED!'; + if (!arg) return arg; + if (Runtime.printObjectMap[arg]) return '<' + arg + '|' + Runtime.printObjectMap[arg] + '>'; + if (arg.toString() == '[object HTMLImageElement]') { + return arg + '\n\n'; + } + if (arg.byteLength) { + return '{' + Array.prototype.slice.call(arg, 0, Math.min(arg.length, 40)) + '}'; // Useful for correct arrays, less so for compiled arrays, see the code below for that + var buf = new ArrayBuffer(32); + var i8buf = new Int8Array(buf); + var i16buf = new Int16Array(buf); + var f32buf = new Float32Array(buf); + switch(arg.toString()) { + case '[object Uint8Array]': + i8buf.set(arg.subarray(0, 32)); + break; + case '[object Float32Array]': + f32buf.set(arg.subarray(0, 5)); + break; + case '[object Uint16Array]': + i16buf.set(arg.subarray(0, 16)); + break; + default: + alert('unknown array for debugging: ' + arg); + throw 'see alert'; + } + var ret = '{' + arg.byteLength + ':\n'; + var arr = Array.prototype.slice.call(i8buf); + ret += 'i8:' + arr.toString().replace(/,/g, ',') + '\n'; + arr = Array.prototype.slice.call(f32buf, 0, 8); + ret += 'f32:' + arr.toString().replace(/,/g, ',') + '}'; + return ret; + } + if (typeof arg == 'object') { + Runtime.printObjectMap[arg] = Runtime.printObjectCounter++; + return '<' + arg + '|' + Runtime.printObjectMap[arg] + '>'; + } + if (typeof arg == 'number') { + return '0x' + arg.toString(16); + } + return arg; } +#endif }; Runtime.stackAlloc = unInline('stackAlloc', ['size']); diff --git a/src/settings.js b/src/settings.js index 24c52d59..f630e5c3 100644 --- a/src/settings.js +++ b/src/settings.js @@ -101,9 +101,13 @@ var SAFE_HEAP_LOG = 0; // Log out all SAFE_HEAP operations var LABEL_DEBUG = 0; // Print out labels and functions as we enter them var EXCEPTION_DEBUG = 1; // Print out exceptions in emscriptened code -var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js) -var GL_DEBUG = 0; // Print out all calls into WebGL +var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js). You must also set + // Runtime.debug at runtime for logging to happen, and can unset it when you + // no longer want logging. A simple way to set it in C++ is + // emscripten_run_script("Runtime.debug = 1;"); +var GL_DEBUG = 0; // Print out all calls into WebGL. As with LIBRARY_DEBUG, you must set a runtime + // option, in this case GL.debug. var DISABLE_EXCEPTION_CATCHING = 0; // Disables generating code to actually catch exceptions. If the code you // are compiling does not actually rely on catching exceptions (but the |