Bug 743311: Implement Debugger.prototype.findAllGlobals. r=jorendorff

This commit is contained in:
Jim Blandy 2012-10-13 16:04:41 -07:00
parent 2935cf9083
commit 0a2a9c19f9
4 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// Debugger.prototype.findAllGlobals surface.
load(libdir + 'asserts.js');
var dbg = new Debugger;
var d = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(dbg), 'findAllGlobals');
assertEq(d.configurable, true);
assertEq(d.enumerable, false);
assertEq(d.writable, true);
assertEq(typeof d.value, 'function');
assertEq(dbg.findAllGlobals.length, 0);
assertEq(dbg.findAllGlobals.name, 'findAllGlobals');
// findAllGlobals can only be applied to real Debugger instances.
assertThrowsInstanceOf(function() {
Debugger.prototype.findAllGlobals.call(Debugger.prototype);
},
TypeError);
var a = dbg.findAllGlobals();
assertEq(a instanceof Array, true);
assertEq(a.length > 0, true);
for (g of a) {
assertEq(g instanceof Debugger.Object, true);
}

View File

@ -0,0 +1,27 @@
// Debugger.prototype.findAllGlobals finds ALL the globals!
var g1 = newGlobal(); // Created before the Debugger; debuggee.
var g2 = newGlobal(); // Created before the Debugger; not debuggee.
var dbg = new Debugger;
var g3 = newGlobal(); // Created after the Debugger; debuggee.
var g4 = newGlobal(); // Created after the Debugger; not debuggee.
var g1w = dbg.addDebuggee(g1);
var g3w = dbg.addDebuggee(g3);
var a = dbg.findAllGlobals();
// Get Debugger.Objects viewing the globals from their own compartments;
// this is the sort that findAllGlobals and addDebuggee return.
var g2w = g1w.makeDebuggeeValue(g2).unwrap();
var g4w = g1w.makeDebuggeeValue(g4).unwrap();
var thisw = g1w.makeDebuggeeValue(this).unwrap();
// Check that they're all there.
assertEq(a.indexOf(g1w) != -1, true);
assertEq(a.indexOf(g2w) != -1, true);
assertEq(a.indexOf(g3w) != -1, true);
assertEq(a.indexOf(g4w) != -1, true);
assertEq(a.indexOf(thisw) != -1, true);

View File

@ -16,6 +16,7 @@
#include "jsinterpinlines.h"
#include "jsobjinlines.h"
#include "jsopcodeinlines.h"
#include "jscompartment.h"
#include "frontend/BytecodeCompiler.h"
#include "frontend/BytecodeEmitter.h"
@ -2484,6 +2485,30 @@ Debugger::findScripts(JSContext *cx, unsigned argc, Value *vp)
return true;
}
JSBool
Debugger::findAllGlobals(JSContext *cx, unsigned argc, Value *vp)
{
THIS_DEBUGGER(cx, argc, vp, "findAllGlobals", args, dbg);
RootedObject result(cx, NewDenseEmptyArray(cx));
if (!result)
return false;
for (CompartmentsIter c(cx->runtime); !c.done(); c.next()) {
GlobalObject *global = c->maybeGlobal();
if (global) {
Value globalValue(ObjectValue(*global));
if (!dbg->wrapDebuggeeValue(cx, &globalValue))
return false;
if (!js_NewbornArrayPush(cx, result, globalValue))
return false;
}
}
args.rval().setObject(*result);
return true;
}
JSPropertySpec Debugger::properties[] = {
JS_PSGS("enabled", Debugger::getEnabled, Debugger::setEnabled, 0),
JS_PSGS("onDebuggerStatement", Debugger::getOnDebuggerStatement,
@ -2506,6 +2531,7 @@ JSFunctionSpec Debugger::methods[] = {
JS_FN("getNewestFrame", Debugger::getNewestFrame, 0, 0),
JS_FN("clearAllBreakpoints", Debugger::clearAllBreakpoints, 1, 0),
JS_FN("findScripts", Debugger::findScripts, 1, 0),
JS_FN("findAllGlobals", Debugger::findAllGlobals, 0, 0),
JS_FS_END
};

View File

@ -181,6 +181,7 @@ class Debugger {
static JSBool getNewestFrame(JSContext *cx, unsigned argc, Value *vp);
static JSBool clearAllBreakpoints(JSContext *cx, unsigned argc, Value *vp);
static JSBool findScripts(JSContext *cx, unsigned argc, Value *vp);
static JSBool findAllGlobals(JSContext *cx, unsigned argc, Value *vp);
static JSBool wrap(JSContext *cx, unsigned argc, Value *vp);
static JSBool construct(JSContext *cx, unsigned argc, Value *vp);
static JSPropertySpec properties[];