Bug 433529: Part 4 - Add DebuggerObject_displayName for a function's displayAtom attribute. r=jimb
authorAlex Crichton <acrichton@mozilla.com>
Sat, 18 Aug 2012 21:42:17 -0700
changeset 105149 234d99170fbf1079c4561900e836d60f9e50f4df
parent 105148 8a751b65df8b19861b94fc6ce3303a05ce1d03de
child 105150 d26a8e451fa2526e011de1ac6b77cd396bc81814
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersjimb
bugs433529
milestone17.0a1
Bug 433529: Part 4 - Add DebuggerObject_displayName for a function's displayAtom attribute. r=jimb
js/src/frontend/NameFunctions.cpp
js/src/jit-test/tests/debug/Object-displayName-01.js
js/src/vm/Debugger.cpp
--- a/js/src/frontend/NameFunctions.cpp
+++ b/js/src/frontend/NameFunctions.cpp
@@ -211,16 +211,18 @@ class NameResolver
 
         /*
          * functions which are "genuinely anonymous" but are contained in some
          * other namespace are rather considered as "contributing" to the outer
          * function, so give them a contribution symbol here.
          */
         if (!buf.empty() && *(buf.end() - 1) == '/' && !buf.append("<"))
             return NULL;
+        if (buf.empty())
+            return NULL;
 
         fun->setGuessedAtom(buf.finishAtom());
         return fun->displayAtom();
     }
 
     /*
      * Tests whether parents[pos] is a function call whose callee is cur.
      * This is the case for functions which do things like simply create a scope
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/debug/Object-displayName-01.js
@@ -0,0 +1,17 @@
+// Debugger.Object.prototype.displayName
+
+var g = newGlobal('new-compartment');
+var dbg = Debugger(g);
+var name;
+dbg.onDebuggerStatement = function (frame) { name = frame.callee.displayName; };
+
+g.eval("(function f() { debugger; })();");
+assertEq(name, "f");
+g.eval("(function () { debugger; })();");
+assertEq(name, undefined);
+g.eval("Function('debugger;')();");
+assertEq(name, "anonymous");
+g.eval("var f = function() { debugger; }; f()");
+assertEq(name, "f");
+g.eval("var a = {}; a.f = function() { debugger; }; a.f()");
+assertEq(name, "a.f");
--- a/js/src/vm/Debugger.cpp
+++ b/js/src/vm/Debugger.cpp
@@ -3717,16 +3717,38 @@ DebuggerObject_getName(JSContext *cx, un
     Value namev = StringValue(name);
     if (!dbg->wrapDebuggeeValue(cx, &namev))
         return false;
     args.rval().set(namev);
     return true;
 }
 
 static JSBool
+DebuggerObject_getDisplayName(JSContext *cx, unsigned argc, Value *vp)
+{
+    THIS_DEBUGOBJECT_OWNER_REFERENT(cx, argc, vp, "get display name", args, dbg, obj);
+    if (!obj->isFunction()) {
+        args.rval().setUndefined();
+        return true;
+    }
+
+    JSString *name = obj->toFunction()->displayAtom();
+    if (!name) {
+        args.rval().setUndefined();
+        return true;
+    }
+
+    Value namev = StringValue(name);
+    if (!dbg->wrapDebuggeeValue(cx, &namev))
+        return false;
+    args.rval().set(namev);
+    return true;
+}
+
+static JSBool
 DebuggerObject_getParameterNames(JSContext *cx, unsigned argc, Value *vp)
 {
     THIS_DEBUGOBJECT_REFERENT(cx, argc, vp, "get parameterNames", args, obj);
     if (!obj->isFunction()) {
         args.rval().setUndefined();
         return true;
     }
 
@@ -4217,16 +4239,17 @@ DebuggerObject_makeDebuggeeValue(JSConte
     return true;
 }
 
 static JSPropertySpec DebuggerObject_properties[] = {
     JS_PSG("proto", DebuggerObject_getProto, 0),
     JS_PSG("class", DebuggerObject_getClass, 0),
     JS_PSG("callable", DebuggerObject_getCallable, 0),
     JS_PSG("name", DebuggerObject_getName, 0),
+    JS_PSG("displayName", DebuggerObject_getDisplayName, 0),
     JS_PSG("parameterNames", DebuggerObject_getParameterNames, 0),
     JS_PSG("script", DebuggerObject_getScript, 0),
     JS_PSG("environment", DebuggerObject_getEnvironment, 0),
     JS_PS_END
 };
 
 static JSFunctionSpec DebuggerObject_methods[] = {
     JS_FN("getOwnPropertyDescriptor", DebuggerObject_getOwnPropertyDescriptor, 1, 0),