Bug 1754405 part 6 - Add JS::GetWellKnownSymbolKey. r=evilpie

It's very common to call JS::GetWellKnownSymbol and then creating a PropertyKey.
We can make this a bit more ergonomic with this API.

Differential Revision: https://phabricator.services.mozilla.com/D138277
This commit is contained in:
Jan de Mooij 2022-02-13 12:01:45 +00:00
Родитель e508008806
Коммит 126599c237
9 изменённых файлов: 32 добавлений и 19 удалений

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

@ -206,8 +206,8 @@ bool WindowNamedPropertiesHandler::ownPropNames(
if (!doc || !doc->IsHTMLOrXHTML()) {
// Define to @@toStringTag on this object to keep Object.prototype.toString
// backwards compatible.
JS::Rooted<jsid> toStringTagId(aCx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
aCx, JS::SymbolCode::toStringTag)));
JS::Rooted<jsid> toStringTagId(
aCx, JS::GetWellKnownSymbolKey(aCx, JS::SymbolCode::toStringTag));
return aProps.append(toStringTagId);
}
@ -221,8 +221,8 @@ bool WindowNamedPropertiesHandler::ownPropNames(
return false;
}
JS::Rooted<jsid> toStringTagId(aCx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
aCx, JS::SymbolCode::toStringTag)));
JS::Rooted<jsid> toStringTagId(
aCx, JS::GetWellKnownSymbolKey(aCx, JS::SymbolCode::toStringTag));
if (!docProps.append(toStringTagId)) {
return false;
}

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

@ -848,8 +848,8 @@ static JSObject* CreateInterfaceObject(
->wantsInterfaceHasInstance) {
if (isChrome ||
StaticPrefs::dom_webidl_crosscontext_hasinstance_enabled()) {
JS::Rooted<jsid> hasInstanceId(cx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
cx, JS::SymbolCode::hasInstance)));
JS::Rooted<jsid> hasInstanceId(
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::hasInstance));
if (!JS_DefineFunctionById(
cx, constructor, hasInstanceId, InterfaceHasInstance, 1,
// Flags match those of Function[Symbol.hasInstance]
@ -974,8 +974,8 @@ static JSObject* CreateInterfacePrototypeObject(
}
}
JS::Rooted<jsid> unscopableId(cx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
cx, JS::SymbolCode::unscopables)));
JS::Rooted<jsid> unscopableId(
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::unscopables));
// Readonly and non-enumerable to match Array.prototype.
if (!JS_DefinePropertyById(cx, ourProto, unscopableId, unscopableObj,
JSPROP_READONLY)) {
@ -983,8 +983,8 @@ static JSObject* CreateInterfacePrototypeObject(
}
}
JS::Rooted<jsid> toStringTagId(cx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
cx, JS::SymbolCode::toStringTag)));
JS::Rooted<jsid> toStringTagId(
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::toStringTag));
if (!JS_DefinePropertyById(cx, ourProto, toStringTagId, name,
JSPROP_READONLY)) {
return nullptr;

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

@ -3733,8 +3733,7 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
name = alias[2:]
symbolJSID = (
"SYMBOL_TO_JSID(JS::GetWellKnownSymbol(aCx, JS::SymbolCode::%s))"
% name
"JS::GetWellKnownSymbolKey(aCx, JS::SymbolCode::%s)" % name
)
prop = "%sId" % name
getSymbolJSID = CGGeneric(
@ -4391,7 +4390,7 @@ def InitUnforgeablePropertiesOnHolder(
fill(
"""
JS::RootedId toPrimitive(aCx,
SYMBOL_TO_JSID(JS::GetWellKnownSymbol(aCx, JS::SymbolCode::toPrimitive)));
JS::GetWellKnownSymbolKey(aCx, JS::SymbolCode::toPrimitive));
if (!JS_DefinePropertyById(aCx, ${holderName}, toPrimitive,
JS::UndefinedHandleValue,
JSPROP_READONLY | JSPROP_PERMANENT)) {

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

@ -240,6 +240,15 @@ MOZ_ALWAYS_INLINE void AssertIdIsNotGray(jsid id) {
}
#endif
/**
* Get one of the well-known symbols defined by ES6 as PropertyKey. This is
* equivalent to calling JS::GetWellKnownSymbol and then creating a PropertyKey.
*
* `which` must be in the range [0, WellKnownSymbolLimit).
*/
extern JS_PUBLIC_API PropertyKey GetWellKnownSymbolKey(JSContext* cx,
SymbolCode which);
} // namespace JS
namespace js {

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

@ -1871,8 +1871,8 @@ static bool DefineToStringTag(JSContext* cx, HandleObject obj,
return false;
}
RootedId toStringTagId(cx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(
cx, JS::SymbolCode::toStringTag)));
RootedId toStringTagId(
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::toStringTag));
return JS_DefinePropertyById(cx, obj, toStringTagId, toStringTagStr,
JSPROP_READONLY);
}

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

@ -14,7 +14,7 @@ FRAGMENT(jsid, simple) {
JS::Rooted<jsid> registry_symbol_id(
cx, SYMBOL_TO_JSID(JS::GetSymbolFor(cx, interned)));
JS::Rooted<jsid> well_known_symbol_id(
cx, SYMBOL_TO_JSID(JS::GetWellKnownSymbol(cx, JS::SymbolCode::iterator)));
cx, JS::GetWellKnownSymbolKey(cx, JS::SymbolCode::iterator));
jsid void_id = JSID_VOID;
breakpoint();

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

@ -3364,6 +3364,11 @@ JS_PUBLIC_API JS::Symbol* JS::GetWellKnownSymbol(JSContext* cx,
return cx->wellKnownSymbols().get(which);
}
JS_PUBLIC_API JS::PropertyKey JS::GetWellKnownSymbolKey(JSContext* cx,
JS::SymbolCode which) {
return SYMBOL_TO_JSID(cx->wellKnownSymbols().get(which));
}
#ifdef DEBUG
static bool PropertySpecNameIsDigits(JSPropertySpec::Name name) {
if (name.isSymbol()) {

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

@ -116,8 +116,8 @@ static JSObject* GetIDPrototype(JSContext* aCx, const JSClass* aClass) {
JS_NewObjectWithGivenProto(aCx, nullptr, idProto));
RootedObject cidProto(aCx,
JS_NewObjectWithGivenProto(aCx, nullptr, idProto));
RootedId hasInstance(
aCx, SYMBOL_TO_JSID(GetWellKnownSymbol(aCx, SymbolCode::hasInstance)));
RootedId hasInstance(aCx,
GetWellKnownSymbolKey(aCx, SymbolCode::hasInstance));
const uint32_t kFlags =
JSPROP_READONLY | JSPROP_ENUMERATE | JSPROP_PERMANENT;

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

@ -68,7 +68,7 @@ bool AppendCrossOriginWhitelistedPropNames(JSContext* cx,
}
for (auto code : sCrossOriginWhitelistedSymbolCodes) {
props.infallibleAppend(SYMBOL_TO_JSID(JS::GetWellKnownSymbol(cx, code)));
props.infallibleAppend(JS::GetWellKnownSymbolKey(cx, code));
}
return true;