Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created May 22, 2012 13:32
Show Gist options
  • Save jorendorff/2769076 to your computer and use it in GitHub Desktop.
Save jorendorff/2769076 to your computer and use it in GitHub Desktop.
Debugger.Breakpoint object
Debugger.Breakpoint = function Breakpoint(dbg, url, line, handler) {
this._dbg = dbg;
this.url = url;
this.line = line;
this.onHit = handler;
this._scripts = null;
this.enabled = true;
};
Debugger.Breakpoint.prototype.hit = function hit(frame) {
return this.onHit(frame);
};
Object.defineProperty(Debugger.Breakpoint.prototype, "enabled", {
configurable: true,
get: function enabled() {
return this._scripts !== null;
},
set: function enabled(state) {
if (state) {
if (this._scripts === null) {
this._scripts = this._dbg.findScripts({url: this.url, line: this.line});
for (let script of this._scripts) {
for (let offset of script.getLineOffsets(this.line))
script.setBreakpoint(offset, this);
}
}
} else {
if (this._scripts !== null) {
for (let script of this._scripts)
script.clearBreakpoint(this);
this._scripts = null;
}
}
}
});
Debugger.prototype.breakAt = function breakAt(url, line, handler) {
return new Debugger.Breakpoint(this, url, line, handler);
};
var g = newGlobal('new-compartment');
g.evaluate('function f(x)\n { \n return x * 2;\n }\n', {fileName: 'amazing.js', lineNumber: 1});
var dbg = Debugger(g);
var bp = dbg.breakAt('amazing.js', 3, function (frame) {
print("DEBUG: " + frame.eval('x').return);
});
/*
js> g.f(33)
DEBUG: 33
66
js> bp.enabled
true
js> bp.enabled = false // disable the breakpoint
false
js> bp.enabled
false
js> g.f(22)
44
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment