Skip to content

Commit

Permalink
Bug 1887939 - Update error message when the pref of "Symbols as WeakM…
Browse files Browse the repository at this point in the history
…ap keys" is off. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D205855
  • Loading branch information
allstarschh committed Mar 28, 2024
1 parent 8a4ae4e commit 8ce9b93
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
2 changes: 2 additions & 0 deletions js/public/friend/ErrorNumbers.msg
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions js/src/builtin/WeakMapObject-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
8 changes: 4 additions & 4 deletions js/src/builtin/WeakMapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 5 additions & 4 deletions js/src/builtin/WeakSetObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions js/src/jit-test/tests/collections/bug-1887939-1.js
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 7 additions & 0 deletions js/src/jit-test/tests/collections/bug-1887939-2.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8ce9b93

Please sign in to comment.