Skip to content

Instantly share code, notes, and snippets.

@bunnybones1
Created September 17, 2014 19:45
Show Gist options
  • Save bunnybones1/a0e446f32e50fb629e29 to your computer and use it in GitHub Desktop.
Save bunnybones1/a0e446f32e50fb629e29 to your computer and use it in GitHub Desktop.
helper to debug when you don't know who or what is changing a value. Can log sets and gets, or even provide stacktraces to the offenders. Meant as a last resort debugging tool.
var defaultMax = 1000;
function GetterSetterValue(initVal, logGet, logSet, traceSet, traceGet, max) {
max = max === undefined ? defaultMax : max;
this.originalMax = max;
this._value = initVal;
this.__logGet = logGet !== false;
this.__logSet = logSet !== false;
this.__traceGet = traceGet !== false;
this.__traceSet = traceSet !== false;
this.__countLogGet = max;
this.__countLogSet = max;
this.__countTraceGet = max;
this.__countTraceSet = max;
}
GetterSetterValue.prototype = {
_value: 0,
__count: defaultMax,
get value() {
if(this.__logGet && this.__countLogGet >= 0) {
console.log("getting", this._value);
if(this.__countLogGet == 0) {
console.log("max number of get logs reached (", this.originalMax, ")")
}
this.__countLogGet--;
}
if(this.__traceGet && this.__countTraceGet >= 0) {
try {
throw Error("Who read this value?");
} catch(err) {
console.log(err.message);
console.log(err.stack);
}
if(this.__countTraceGet == 0) {
console.log("max number of get traces reached (", this.originalMax, ")")
}
this.__countTraceGet--;
}
return this._value;
},
set value(val) {
if(this.__logSet && this.__countLogSet >= 0) {
console.log("setting", val);
if(this.__countLogSet == 0) {
console.log("max number of set logs reached (", this.originalMax, ")")
}
this.__countLogSet--;
}
if(this.__traceSet && this.__countTraceSet >= 0) {
try {
throw Error("Who wrote this value?");
} catch(err) {
console.log(err.message);
console.log(err.stack);
}
if(this.__countTraceSet == 0) {
console.log("max number of set traces reached (", this.originalMax, ")")
}
this.__countTraceSet--;
}
this._value = val;
}
}
LibHelpers = {
GetterSetterValue: GetterSetterValue
}
@bunnybones1
Copy link
Author

This is mostly meant to debug pesky in-place vars defined and manipulated in a function. It was born of a need to figure out what was wrong with geometry I was trying to parse with Three.js. It's a bit hacky because you do need to replace all instances of offendingVar with offendingVar.value, but in large libraries where its almost impossible to keep track of everything that manipulates a particular value, this can be a lifesaver.
For example:

function stuffDoer() {
    var offendingVar = 0;
    function cleverStuff(really) {
        if(really) offendingVar++;
    }
    cleverStuff(true);
    cleverStuff(false);
    cleverStuff(0);
    offendingVar = 0;
    cleverStuff(true);
}

To use this helper, include it in the html:

    <script type="text/javascript" src="../../lib/LibHelpers.js"></script>

and modify the suspicious code:

function stuffDoer() {
    var offendingVar = new LibHelpers.GetterSetterValue(0, true, true, true, true, 10);
    function cleverStuff(really) {
        if(really) offendingVar++;
    }
    cleverStuff(true);
    cleverStuff(false);
    cleverStuff(0);
    offendingVar = 0;
    cleverStuff(true);
}

And you should get a flood of logs pertaining to anything that reads or writes to offendingVar.value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment