Bug 1134865 - Part 1: Add JSObject::constructorDisplayAtom; r=djvj

This commit is contained in:
Nick Fitzgerald 2015-04-27 10:57:06 -07:00
Родитель 744bbc11a4
Коммит 669fee1c38
4 изменённых файлов: 102 добавлений и 0 удалений

Просмотреть файл

@ -2554,6 +2554,32 @@ SetDiscardSource(JSContext* cx, unsigned argc, Value* vp)
return true; return true;
} }
static bool
GetConstructorName(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
if (!args.requireAtLeast(cx, "getConstructorName", 1))
return false;
if (!args[0].isObject()) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE,
"getConstructorName", "Object",
InformalValueTypeName(args[0]));
return false;
}
RootedAtom name(cx);
if (!args[0].toObject().constructorDisplayAtom(cx, &name))
return false;
if (name) {
args.rval().setString(name);
} else {
args.rval().setNull();
}
return true;
}
static const JSFunctionSpecWithHelp TestingFunctions[] = { static const JSFunctionSpecWithHelp TestingFunctions[] = {
JS_FN_HELP("gc", ::GC, 0, 0, JS_FN_HELP("gc", ::GC, 0, 0,
"gc([obj] | 'compartment' [, 'shrinking'])", "gc([obj] | 'compartment' [, 'shrinking'])",
@ -2960,6 +2986,11 @@ gc::ZealModeHelpText),
" Explicitly enable source discarding in the current compartment. The default is that " " Explicitly enable source discarding in the current compartment. The default is that "
" source discarding is not explicitly enabled."), " source discarding is not explicitly enabled."),
JS_FN_HELP("getConstructorName", GetConstructorName, 1, 0,
"getConstructorName(object)",
" If the given object was created with `new Ctor`, return the constructor's display name. "
" Otherwise, return null."),
JS_FS_HELP_END JS_FS_HELP_END
}; };

Просмотреть файл

@ -0,0 +1,28 @@
function Ctor() {}
var nested = {};
nested.Ctor = function () {};
nested.object = {};
function makeInstance() {
let LexicalCtor = function () {};
return new LexicalCtor;
}
function makeObject() {
let object = {};
return object;
}
let tests = [
{ name: "Ctor", object: new Ctor },
{ name: "nested.Ctor", object: new nested.Ctor },
{ name: "makeInstance/LexicalCtor", object: makeInstance() },
{ name: null, object: {} },
{ name: null, object: nested.object },
{ name: null, object: makeObject() },
];
for (let { name, object } of tests) {
assertEq(getConstructorName(object), name);
}

Просмотреть файл

@ -4106,3 +4106,32 @@ JSObject::traceChildren(JSTracer* trc)
} while (false); } while (false);
} }
} }
static JSAtom*
displayAtomFromObjectGroup(ObjectGroup& group)
{
TypeNewScript* script = group.newScript();
if (!script)
return nullptr;
return script->function()->displayAtom();
}
bool
JSObject::constructorDisplayAtom(JSContext* cx, js::MutableHandleAtom name)
{
ObjectGroup *g = getGroup(cx);
if (!g)
return false;
name.set(displayAtomFromObjectGroup(*g));
return true;
}
JSAtom*
JSObject::maybeConstructorDisplayAtom() const
{
if (hasLazyGroup())
return nullptr;
return displayAtomFromObjectGroup(*group());
}

Просмотреть файл

@ -254,6 +254,20 @@ class JSObject : public js::gc::Cell
*/ */
inline bool isIndexed() const; inline bool isIndexed() const;
/*
* If this object was instantiated with `new Ctor`, return the constructor's
* display atom. Otherwise, return nullptr.
*/
bool constructorDisplayAtom(JSContext* cx, js::MutableHandleAtom name);
/*
* The same as constructorDisplayAtom above, however if this object has a
* lazy group, nullptr is returned. This allows for use in situations that
* cannot GC and where having some information, even if it is inconsistently
* available, is better than no information.
*/
JSAtom* maybeConstructorDisplayAtom() const;
/* GC support. */ /* GC support. */
void traceChildren(JSTracer* trc); void traceChildren(JSTracer* trc);