Bug 1887939 - Update error message when the pref of "Symbols as WeakMap keys" is off. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D205855
This commit is contained in:
Yoshi Cheng-Hao Huang 2024-03-28 14:05:21 +00:00
parent 8a4ae4e137
commit 8ce9b930e0
6 changed files with 40 additions and 8 deletions

View File

@ -80,6 +80,8 @@ MSG_DEF(JSMSG_SPREAD_TOO_LARGE, 0, JSEXN_RANGEERR, "array too large due t
MSG_DEF(JSMSG_BAD_WEAKMAP_KEY, 0, JSEXN_TYPEERR, "cannot use the given object as a weak map key")
MSG_DEF(JSMSG_WEAKMAP_KEY_CANT_BE_HELD_WEAKLY, 1, JSEXN_TYPEERR, "WeakMap key {0} must be an object or an unregistered symbol")
MSG_DEF(JSMSG_WEAKSET_VAL_CANT_BE_HELD_WEAKLY, 1, JSEXN_TYPEERR, "WeakSet value {0} must be an object or an unregistered symbol")
MSG_DEF(JSMSG_WEAKMAP_KEY_MUST_BE_AN_OBJECT, 1, JSEXN_TYPEERR, "WeakMap key {0} must be an object")
MSG_DEF(JSMSG_WEAKSET_VAL_MUST_BE_AN_OBJECT, 1, JSEXN_TYPEERR, "WeakSet value {0} must be an object")
MSG_DEF(JSMSG_BAD_GETTER_OR_SETTER, 1, JSEXN_TYPEERR, "invalid {0} usage")
MSG_DEF(JSMSG_BAD_ARRAY_LENGTH, 0, JSEXN_RANGEERR, "invalid array length")
MSG_DEF(JSMSG_SOURCE_ARRAY_TOO_LONG, 0, JSEXN_RANGEERR, "source array is too long")

View File

@ -92,6 +92,21 @@ static MOZ_ALWAYS_INLINE bool CanBeHeldWeakly(JSContext* cx,
return false;
}
static unsigned GetErrorNumber(bool isWeakMap) {
#ifdef NIGHTLY_BUILD
bool symbolsAsWeakMapKeysEnabled =
JS::Prefs::experimental_symbols_as_weakmap_keys();
if (symbolsAsWeakMapKeysEnabled) {
return isWeakMap ? JSMSG_WEAKMAP_KEY_CANT_BE_HELD_WEAKLY
: JSMSG_WEAKSET_VAL_CANT_BE_HELD_WEAKLY;
}
#endif
return isWeakMap ? JSMSG_WEAKMAP_KEY_MUST_BE_AN_OBJECT
: JSMSG_WEAKSET_VAL_MUST_BE_AN_OBJECT;
}
} // namespace js
#endif /* builtin_WeakMapObject_inl_h */

View File

@ -122,8 +122,8 @@ bool WeakMapObject::delete_(JSContext* cx, unsigned argc, Value* vp) {
MOZ_ASSERT(WeakMapObject::is(args.thisv()));
if (!CanBeHeldWeakly(cx, args.get(0))) {
ReportValueError(cx, JSMSG_WEAKMAP_KEY_CANT_BE_HELD_WEAKLY,
JSDVG_IGNORE_STACK, args.get(0), nullptr);
unsigned errorNum = GetErrorNumber(true);
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, args.get(0), nullptr);
return false;
}
@ -238,8 +238,8 @@ JS_PUBLIC_API bool JS::SetWeakMapEntry(JSContext* cx, HandleObject mapObj,
CHECK_THREAD(cx);
cx->check(key, val);
if (!CanBeHeldWeakly(cx, key)) {
ReportValueError(cx, JSMSG_WEAKMAP_KEY_CANT_BE_HELD_WEAKLY,
JSDVG_IGNORE_STACK, key, nullptr);
unsigned errorNum = GetErrorNumber(true);
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, key, nullptr);
return false;
}

View File

@ -31,8 +31,8 @@ using namespace js;
// Step 4.
if (!CanBeHeldWeakly(cx, args.get(0))) {
ReportValueError(cx, JSMSG_WEAKSET_VAL_CANT_BE_HELD_WEAKLY,
JSDVG_IGNORE_STACK, args.get(0), nullptr);
unsigned errorNum = GetErrorNumber(false);
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, args.get(0), nullptr);
return false;
}
@ -198,8 +198,9 @@ bool WeakSetObject::construct(JSContext* cx, unsigned argc, Value* vp) {
MOZ_ASSERT(!keyVal.isMagic(JS_ELEMENTS_HOLE));
if (!CanBeHeldWeakly(cx, keyVal)) {
ReportValueError(cx, JSMSG_WEAKSET_VAL_CANT_BE_HELD_WEAKLY,
JSDVG_IGNORE_STACK, keyVal, nullptr);
unsigned errorNum = GetErrorNumber(false);
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, args.get(0),
nullptr);
return false;
}

View File

@ -0,0 +1,7 @@
var map = new WeakMap();
var sym = Symbol();
try {
map.set(sym, 1);
} catch (e) {
assertEq(!!e.message.match(/an unregistered symbol/), false);
}

View File

@ -0,0 +1,7 @@
// |jit-test| --enable-symbols-as-weakmap-keys; skip-if: getBuildConfiguration("release_or_beta")
var map = new WeakMap();
try {
map.set(1, 1);
} catch (e) {
assertEq(!!e.message.match(/an unregistered symbol/), true);
}