Skip to content

Commit

Permalink
Bug 1806994 - Define @@toStringTag on Xrays for namespace objects. r=…
Browse files Browse the repository at this point in the history
…smaug

Differential Revision: https://phabricator.services.mozilla.com/D165459
  • Loading branch information
petervanderbeken committed Jan 9, 2023
1 parent c4a3af2 commit 68c4f24
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
16 changes: 14 additions & 2 deletions dom/bindings/BindingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ static bool XrayResolveProperty(
switch (propertyInfo.type) {
case eStaticMethod:
case eStaticAttribute:
if (type != eInterface) {
if (type != eInterface && type != eNamespace) {
return true;
}
break;
Expand Down Expand Up @@ -1814,6 +1814,18 @@ static bool ResolvePrototypeOrConstructor(
return true;
}
}
} else if (type == eNamespace) {
if (id.isWellKnownSymbol(JS::SymbolCode::toStringTag)) {
JS::Rooted<JSString*> nameStr(
cx, JS_AtomizeString(cx, JS::GetClass(obj)->name));
if (!nameStr) {
return false;
}

desc.set(Some(JS::PropertyDescriptor::Data(
JS::StringValue(nameStr), {JS::PropertyAttribute::Configurable})));
return true;
}
} else {
MOZ_ASSERT(IsInterfacePrototype(type));

Expand Down Expand Up @@ -1954,7 +1966,7 @@ bool XrayOwnPropertyKeys(JSContext* cx, JS::Handle<JSObject*> wrapper,
}
} else {
MOZ_ASSERT(type != eGlobalInterfacePrototype);
if (type == eInterface) {
if (type == eInterface || type == eNamespace) {
ADD_KEYS_IF_DEFINED(StaticMethod);
ADD_KEYS_IF_DEFINED(StaticAttribute);
} else {
Expand Down
7 changes: 5 additions & 2 deletions dom/bindings/Codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ def define(self):
if self.descriptor.interface.isNamespace():
classString = self.descriptor.interface.getExtendedAttribute("ClassString")
if classString is None:
classString = "Object"
classString = self.descriptor.interface.identifier.name
else:
classString = classString[0]
funToString = "nullptr"
Expand All @@ -999,7 +999,7 @@ def define(self):
JS_NULL_CLASS_EXT,
${objectOps}
},
eInterface,
${type},
${needsHasInstance},
${prototypeID},
${depth},
Expand All @@ -1013,6 +1013,9 @@ def define(self):
classOpsPtr=classOpsPtr,
hooks=NativePropertyHooks(self.descriptor),
objectOps=objectOps,
type="eNamespace"
if self.descriptor.interface.isNamespace()
else "eInterface",
needsHasInstance=toStringBool(needsHasInstance),
prototypeID=prototypeID,
depth=depth,
Expand Down
5 changes: 3 additions & 2 deletions dom/bindings/DOMJSClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ enum DOMObjectType : uint8_t {
eInterface,
eInterfacePrototype,
eGlobalInterfacePrototype,
eNamespace,
eNamedPropertiesObject
};

Expand Down Expand Up @@ -549,8 +550,8 @@ struct DOMIfaceAndProtoJSClass {
// initialization for aggregate/POD types.
const JSClass mBase;

// Either eInterface, eInterfacePrototype, eGlobalInterfacePrototype or
// eNamedPropertiesObject.
// Either eInterface, eNamespace, eInterfacePrototype,
// eGlobalInterfacePrototype or eNamedPropertiesObject.
DOMObjectType mType; // uint8_t

// Boolean indicating whether this object wants a @@hasInstance property
Expand Down
9 changes: 9 additions & 0 deletions dom/bindings/test/test_dom_xrays.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@
checkXrayProperty(win.Image, Symbol.hasInstance,
[undefined, win.Function.prototype[Symbol.hasInstance]]);

// toString/@@toStringTag
let imageConstructor = win.Image;
is(win.Function.prototype.toString.apply(imageConstructor),
Function.prototype.toString.apply(Image),
"Applying Function.prototype.toString through an Xray should give the same result as applying it directly");
isDeeply(Object.getOwnPropertyDescriptor(win.CSS, Symbol.toStringTag),
Object.getOwnPropertyDescriptor(CSS, Symbol.toStringTag),
"Getting @@toStringTag on a namespace object through an Xray should give the same result as getting it directly");

SimpleTest.finish();
}

Expand Down

0 comments on commit 68c4f24

Please sign in to comment.