| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* |
| 8 |
* JS atom table. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "vm/JSAtomUtils-inl.h" |
| 12 |
|
| 13 |
#include "mozilla/HashFunctions.h" // mozilla::HashStringKnownLength |
| 14 |
#include "mozilla/RangedPtr.h" |
| 15 |
|
| 16 |
#include <charconv> |
| 17 |
#include <iterator> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "jstypes.h" |
| 21 |
|
| 22 |
#include "frontend/CompilationStencil.h" |
| 23 |
#include "gc/GC.h" |
| 24 |
#include "gc/Marking.h" |
| 25 |
#include "gc/MaybeRooted.h" |
| 26 |
#include "js/CharacterEncoding.h" |
| 27 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 28 |
#include "js/Symbol.h" |
| 29 |
#include "util/Text.h" |
| 30 |
#include "vm/JSContext.h" |
| 31 |
#include "vm/JSObject.h" |
| 32 |
#include "vm/StaticStrings.h" |
| 33 |
#include "vm/StringType.h" |
| 34 |
#include "vm/SymbolType.h" |
| 35 |
#include "vm/WellKnownAtom.h" // WellKnownAtomInfo, WellKnownAtomId, wellKnownAtomInfos |
| 36 |
#include "gc/AtomMarking-inl.h" |
| 37 |
#include "vm/JSContext-inl.h" |
| 38 |
#include "vm/Realm-inl.h" |
| 39 |
#include "vm/StringType-inl.h" |
| 40 |
|
| 41 |
using namespace js; |
| 42 |
|
| 43 |
using mozilla::Maybe; |
| 44 |
using mozilla::Nothing; |
| 45 |
using mozilla::RangedPtr; |
| 46 |
|
| 47 |
template <typename CharT> |
| 48 |
extern void InflateUTF8CharsToBuffer(const JS::UTF8Chars& src, CharT* dst, |
| 49 |
size_t dstLen, |
| 50 |
JS::SmallestEncoding encoding); |
| 51 |
|
| 52 |
template <typename CharT> |
| 53 |
extern bool UTF8EqualsChars(const JS::UTF8Chars& utf8, const CharT* chars); |
| 54 |
|
| 55 |
extern bool GetUTF8AtomizationData(JSContext* cx, const JS::UTF8Chars& utf8, |
| 56 |
size_t* outlen, |
| 57 |
JS::SmallestEncoding* encoding, |
| 58 |
HashNumber* hashNum); |
| 59 |
|
| 60 |
MOZ_ALWAYS_INLINE bool js::AtomHasher::Lookup::StringsMatch( |
| 61 |
const JSAtom& atom) const { |
| 62 |
if (atom.hasLatin1Chars()) { |
| 63 |
const Latin1Char* keyChars = atom.latin1Chars(nogc); |
| 64 |
switch (type) { |
| 65 |
case Lookup::Latin1: |
| 66 |
return EqualChars(keyChars, latin1Chars, length); |
| 67 |
case Lookup::TwoByteChar: |
| 68 |
return EqualChars(keyChars, twoByteChars, length); |
| 69 |
case Lookup::UTF8: { |
| 70 |
JS::UTF8Chars utf8(utf8Bytes, byteLength); |
| 71 |
return UTF8EqualsChars(utf8, keyChars); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
const char16_t* keyChars = atom.twoByteChars(nogc); |
| 77 |
switch (type) { |
| 78 |
case Lookup::Latin1: |
| 79 |
return EqualChars(latin1Chars, keyChars, length); |
| 80 |
case Lookup::TwoByteChar: |
| 81 |
return EqualChars(keyChars, twoByteChars, length); |
| 82 |
case Lookup::UTF8: { |
| 83 |
JS::UTF8Chars utf8(utf8Bytes, byteLength); |
| 84 |
return UTF8EqualsChars(utf8, keyChars); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
MOZ_ASSERT_UNREACHABLE("AtomHasher::match unknown type"); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
inline HashNumber js::AtomHasher::hash(const Lookup& l) { return l.hash; } |
| 93 |
|
| 94 |
MOZ_ALWAYS_INLINE bool js::AtomHasher::match(const WeakHeapPtr<JSAtom*>& entry, |
| 95 |
const Lookup& lookup) { |
| 96 |
JSAtom* key = entry.unbarrieredGet(); |
| 97 |
if (lookup.atom) { |
| 98 |
return lookup.atom == key; |
| 99 |
} |
| 100 |
if (key->length() != lookup.length || key->hash() != lookup.hash) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
return lookup.StringsMatch(*key); |
| 105 |
} |
| 106 |
|
| 107 |
UniqueChars js::AtomToPrintableString(JSContext* cx, JSAtom* atom) { |
| 108 |
return QuoteString(cx, atom); |
| 109 |
} |
| 110 |
|
| 111 |
// Use a low initial capacity for the permanent atoms table to avoid penalizing |
| 112 |
// runtimes that create a small number of atoms. |
| 113 |
static const uint32_t JS_PERMANENT_ATOM_SIZE = 64; |
| 114 |
|
| 115 |
MOZ_ALWAYS_INLINE AtomSet::Ptr js::FrozenAtomSet::readonlyThreadsafeLookup( |
| 116 |
const AtomSet::Lookup& l) const { |
| 117 |
return mSet->readonlyThreadsafeLookup(l); |
| 118 |
} |
| 119 |
|
| 120 |
static JSAtom* PermanentlyAtomizeCharsValidLength(JSContext* cx, |
| 121 |
AtomSet& atomSet, |
| 122 |
mozilla::HashNumber hash, |
| 123 |
const Latin1Char* chars, |
| 124 |
size_t length); |
| 125 |
|
| 126 |
bool JSRuntime::initializeAtoms(JSContext* cx) { |
| 127 |
JS::AutoAssertNoGC nogc; |
| 128 |
|
| 129 |
MOZ_ASSERT(!atoms_); |
| 130 |
MOZ_ASSERT(!permanentAtoms_); |
| 131 |
|
| 132 |
if (parentRuntime) { |
| 133 |
permanentAtoms_ = parentRuntime->permanentAtoms_; |
| 134 |
|
| 135 |
staticStrings = parentRuntime->staticStrings; |
| 136 |
commonNames = parentRuntime->commonNames; |
| 137 |
emptyString = parentRuntime->emptyString; |
| 138 |
wellKnownSymbols = parentRuntime->wellKnownSymbols; |
| 139 |
|
| 140 |
atoms_ = js_new<AtomsTable>(); |
| 141 |
return bool(atoms_); |
| 142 |
} |
| 143 |
|
| 144 |
// NOTE: There's no GC, but `gc.freezePermanentSharedThings` below contains |
| 145 |
// a function call that's marked as "Can GC". |
| 146 |
Rooted<UniquePtr<AtomSet>> atomSet(cx, |
| 147 |
cx->new_<AtomSet>(JS_PERMANENT_ATOM_SIZE)); |
| 148 |
if (!atomSet) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
staticStrings = js_new<StaticStrings>(); |
| 153 |
if (!staticStrings || !staticStrings->init(cx)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
// The bare symbol names are already part of the well-known set, but their |
| 158 |
// descriptions are not, so enumerate them here and add them to the initial |
| 159 |
// permanent atoms set below. |
| 160 |
static const WellKnownAtomInfo symbolDescInfo[] = { |
| 161 |
#define COMMON_NAME_INFO(NAME) \ |
| 162 |
{uint32_t(sizeof("Symbol." #NAME) - 1), \ |
| 163 |
mozilla::HashStringKnownLength("Symbol." #NAME, \ |
| 164 |
sizeof("Symbol." #NAME) - 1), \ |
| 165 |
"Symbol." #NAME}, |
| 166 |
JS_FOR_EACH_WELL_KNOWN_SYMBOL(COMMON_NAME_INFO) |
| 167 |
#undef COMMON_NAME_INFO |
| 168 |
}; |
| 169 |
|
| 170 |
commonNames = js_new<JSAtomState>(); |
| 171 |
if (!commonNames) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
ImmutableTenuredPtr<PropertyName*>* names = |
| 176 |
reinterpret_cast<ImmutableTenuredPtr<PropertyName*>*>(commonNames.ref()); |
| 177 |
for (size_t i = 0; i < uint32_t(WellKnownAtomId::Limit); i++) { |
| 178 |
const auto& info = wellKnownAtomInfos[i]; |
| 179 |
JSAtom* atom = PermanentlyAtomizeCharsValidLength( |
| 180 |
cx, *atomSet, info.hash, |
| 181 |
reinterpret_cast<const Latin1Char*>(info.content), info.length); |
| 182 |
if (!atom) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
names->init(atom->asPropertyName()); |
| 186 |
names++; |
| 187 |
} |
| 188 |
|
| 189 |
for (const auto& info : symbolDescInfo) { |
| 190 |
JSAtom* atom = PermanentlyAtomizeCharsNonStaticValidLength( |
| 191 |
cx, *atomSet, info.hash, |
| 192 |
reinterpret_cast<const Latin1Char*>(info.content), info.length); |
| 193 |
if (!atom) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
names->init(atom->asPropertyName()); |
| 197 |
names++; |
| 198 |
} |
| 199 |
MOZ_ASSERT(uintptr_t(names) == uintptr_t(commonNames + 1)); |
| 200 |
|
| 201 |
emptyString = commonNames->empty_; |
| 202 |
|
| 203 |
// The self-hosted atoms are those that exist in a self-hosted JS source file, |
| 204 |
// but are not defined in any of the well-known atom collections. |
| 205 |
if (!cx->runtime()->selfHostStencil_->instantiateSelfHostedAtoms( |
| 206 |
cx, *atomSet, cx->runtime()->selfHostStencilInput_->atomCache)) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
// Create the well-known symbols. |
| 211 |
auto wks = js_new<WellKnownSymbols>(); |
| 212 |
if (!wks) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
{ |
| 217 |
// Prevent GC until we have fully initialized the well known symbols table. |
| 218 |
// Faster than zeroing the array and null checking during every GC. |
| 219 |
gc::AutoSuppressGC nogc(cx); |
| 220 |
|
| 221 |
ImmutableTenuredPtr<PropertyName*>* descriptions = |
| 222 |
commonNames->wellKnownSymbolDescriptions(); |
| 223 |
ImmutableTenuredPtr<JS::Symbol*>* symbols = |
| 224 |
reinterpret_cast<ImmutableTenuredPtr<JS::Symbol*>*>(wks); |
| 225 |
for (size_t i = 0; i < JS::WellKnownSymbolLimit; i++) { |
| 226 |
JS::Symbol* symbol = |
| 227 |
JS::Symbol::newWellKnown(cx, JS::SymbolCode(i), descriptions[i]); |
| 228 |
if (!symbol) { |
| 229 |
ReportOutOfMemory(cx); |
| 230 |
return false; |
| 231 |
} |
| 232 |
symbols[i].init(symbol); |
| 233 |
} |
| 234 |
|
| 235 |
wellKnownSymbols = wks; |
| 236 |
} |
| 237 |
|
| 238 |
if (!gc.freezeSharedAtomsZone()) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
// The permanent atoms table has now been populated. |
| 243 |
permanentAtoms_ = |
| 244 |
js_new<FrozenAtomSet>(atomSet.release()); // Takes ownership. |
| 245 |
if (!permanentAtoms_) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
// Initialize the main atoms table. |
| 250 |
atoms_ = js_new<AtomsTable>(); |
| 251 |
if (!atoms_) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
void JSRuntime::finishAtoms() { |
| 259 |
js_delete(atoms_.ref()); |
| 260 |
|
| 261 |
if (!parentRuntime) { |
| 262 |
js_delete(permanentAtoms_.ref()); |
| 263 |
js_delete(staticStrings.ref()); |
| 264 |
js_delete(commonNames.ref()); |
| 265 |
js_delete(wellKnownSymbols.ref()); |
| 266 |
} |
| 267 |
|
| 268 |
atoms_ = nullptr; |
| 269 |
permanentAtoms_ = nullptr; |
| 270 |
staticStrings = nullptr; |
| 271 |
commonNames = nullptr; |
| 272 |
wellKnownSymbols = nullptr; |
| 273 |
emptyString = nullptr; |
| 274 |
} |
| 275 |
|
| 276 |
AtomsTable::AtomsTable() |
| 277 |
: atoms(InitialTableSize), atomsAddedWhileSweeping(nullptr) {} |
| 278 |
|
| 279 |
AtomsTable::~AtomsTable() { MOZ_ASSERT(!atomsAddedWhileSweeping); } |
| 280 |
|
| 281 |
void AtomsTable::tracePinnedAtoms(JSTracer* trc) { |
| 282 |
for (JSAtom* atom : pinnedAtoms) { |
| 283 |
TraceRoot(trc, &atom, "pinned atom"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
void js::TraceAtoms(JSTracer* trc) { |
| 288 |
JSRuntime* rt = trc->runtime(); |
| 289 |
if (rt->permanentAtomsPopulated()) { |
| 290 |
rt->atoms().tracePinnedAtoms(trc); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
void AtomsTable::traceWeak(JSTracer* trc) { |
| 295 |
for (AtomSet::Enum e(atoms); !e.empty(); e.popFront()) { |
| 296 |
JSAtom* atom = e.front().unbarrieredGet(); |
| 297 |
MOZ_DIAGNOSTIC_ASSERT(atom); |
| 298 |
if (!TraceManuallyBarrieredWeakEdge(trc, &atom, "AtomsTable::atoms")) { |
| 299 |
e.removeFront(); |
| 300 |
} else { |
| 301 |
MOZ_ASSERT(atom == e.front().unbarrieredGet()); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
bool AtomsTable::startIncrementalSweep(Maybe<SweepIterator>& atomsToSweepOut) { |
| 307 |
MOZ_ASSERT(JS::RuntimeHeapIsCollecting()); |
| 308 |
MOZ_ASSERT(atomsToSweepOut.isNothing()); |
| 309 |
MOZ_ASSERT(!atomsAddedWhileSweeping); |
| 310 |
|
| 311 |
atomsAddedWhileSweeping = js_new<AtomSet>(); |
| 312 |
if (!atomsAddedWhileSweeping) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
atomsToSweepOut.emplace(atoms); |
| 317 |
|
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
void AtomsTable::mergeAtomsAddedWhileSweeping() { |
| 322 |
// Add atoms that were added to the secondary table while we were sweeping |
| 323 |
// the main table. |
| 324 |
|
| 325 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 326 |
|
| 327 |
auto newAtoms = atomsAddedWhileSweeping; |
| 328 |
atomsAddedWhileSweeping = nullptr; |
| 329 |
|
| 330 |
for (auto r = newAtoms->all(); !r.empty(); r.popFront()) { |
| 331 |
if (!atoms.putNew(AtomHasher::Lookup(r.front().unbarrieredGet()), |
| 332 |
r.front())) { |
| 333 |
oomUnsafe.crash("Adding atom from secondary table after sweep"); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
js_delete(newAtoms); |
| 338 |
} |
| 339 |
|
| 340 |
bool AtomsTable::sweepIncrementally(SweepIterator& atomsToSweep, |
| 341 |
JS::SliceBudget& budget) { |
| 342 |
// Sweep the table incrementally until we run out of work or budget. |
| 343 |
while (!atomsToSweep.empty()) { |
| 344 |
budget.step(); |
| 345 |
if (budget.isOverBudget()) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
JSAtom* atom = atomsToSweep.front().unbarrieredGet(); |
| 350 |
MOZ_DIAGNOSTIC_ASSERT(atom); |
| 351 |
if (IsAboutToBeFinalizedUnbarriered(atom)) { |
| 352 |
MOZ_ASSERT(!atom->isPinned()); |
| 353 |
atomsToSweep.removeFront(); |
| 354 |
} else { |
| 355 |
MOZ_ASSERT(atom == atomsToSweep.front().unbarrieredGet()); |
| 356 |
} |
| 357 |
atomsToSweep.popFront(); |
| 358 |
} |
| 359 |
|
| 360 |
mergeAtomsAddedWhileSweeping(); |
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
size_t AtomsTable::sizeOfIncludingThis( |
| 365 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 366 |
size_t size = sizeof(AtomsTable); |
| 367 |
size += atoms.shallowSizeOfExcludingThis(mallocSizeOf); |
| 368 |
if (atomsAddedWhileSweeping) { |
| 369 |
size += atomsAddedWhileSweeping->shallowSizeOfExcludingThis(mallocSizeOf); |
| 370 |
} |
| 371 |
size += pinnedAtoms.sizeOfExcludingThis(mallocSizeOf); |
| 372 |
return size; |
| 373 |
} |
| 374 |
|
| 375 |
template <typename CharT> |
| 376 |
static MOZ_ALWAYS_INLINE JSAtom* |
| 377 |
AtomizeAndCopyCharsNonStaticValidLengthFromLookup( |
| 378 |
JSContext* cx, const CharT* chars, size_t length, |
| 379 |
const AtomHasher::Lookup& lookup, const Maybe<uint32_t>& indexValue) { |
| 380 |
Zone* zone = cx->zone(); |
| 381 |
MOZ_ASSERT(zone); |
| 382 |
|
| 383 |
AtomCacheHashTable* atomCache = zone->atomCache(); |
| 384 |
|
| 385 |
// Try the per-Zone cache first. If we find the atom there we can avoid the |
| 386 |
// markAtom call, and the multiple HashSet lookups below. |
| 387 |
if (MOZ_LIKELY(atomCache)) { |
| 388 |
JSAtom* const cachedAtom = atomCache->lookupForAdd(lookup); |
| 389 |
if (cachedAtom) { |
| 390 |
// The cache is purged on GC so if we're in the middle of an incremental |
| 391 |
// GC we should have barriered the atom when we put it in the cache. |
| 392 |
MOZ_ASSERT(AtomIsMarked(zone, cachedAtom)); |
| 393 |
return cachedAtom; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
MOZ_ASSERT(cx->permanentAtomsPopulated()); |
| 398 |
|
| 399 |
AtomSet::Ptr pp = cx->permanentAtoms().readonlyThreadsafeLookup(lookup); |
| 400 |
if (pp) { |
| 401 |
JSAtom* atom = pp->get(); |
| 402 |
if (MOZ_LIKELY(atomCache)) { |
| 403 |
atomCache->add(lookup.hash, atom); |
| 404 |
} |
| 405 |
return atom; |
| 406 |
} |
| 407 |
|
| 408 |
JSAtom* atom = cx->atoms().atomizeAndCopyCharsNonStaticValidLength( |
| 409 |
cx, chars, length, indexValue, lookup); |
| 410 |
if (!atom) { |
| 411 |
return nullptr; |
| 412 |
} |
| 413 |
|
| 414 |
if (MOZ_UNLIKELY(!cx->atomMarking().inlinedMarkAtomFallible(cx, atom))) { |
| 415 |
ReportOutOfMemory(cx); |
| 416 |
return nullptr; |
| 417 |
} |
| 418 |
|
| 419 |
if (MOZ_LIKELY(atomCache)) { |
| 420 |
atomCache->add(lookup.hash, atom); |
| 421 |
} |
| 422 |
return atom; |
| 423 |
} |
| 424 |
|
| 425 |
template <typename CharT> |
| 426 |
static MOZ_ALWAYS_INLINE JSAtom* AllocateNewAtomNonStaticValidLength( |
| 427 |
JSContext* cx, const CharT* chars, size_t length, |
| 428 |
const Maybe<uint32_t>& indexValue, const AtomHasher::Lookup& lookup); |
| 429 |
|
| 430 |
template <typename CharT> |
| 431 |
static MOZ_ALWAYS_INLINE JSAtom* AllocateNewPermanentAtomNonStaticValidLength( |
| 432 |
JSContext* cx, const CharT* chars, size_t length, |
| 433 |
const AtomHasher::Lookup& lookup); |
| 434 |
|
| 435 |
template <typename CharT> |
| 436 |
MOZ_ALWAYS_INLINE JSAtom* AtomsTable::atomizeAndCopyCharsNonStaticValidLength( |
| 437 |
JSContext* cx, const CharT* chars, size_t length, |
| 438 |
const Maybe<uint32_t>& indexValue, const AtomHasher::Lookup& lookup) { |
| 439 |
AtomSet::AddPtr p; |
| 440 |
|
| 441 |
if (!atomsAddedWhileSweeping) { |
| 442 |
p = atoms.lookupForAdd(lookup); |
| 443 |
} else { |
| 444 |
// We're currently sweeping the main atoms table and all new atoms will |
| 445 |
// be added to a secondary table. Check this first. |
| 446 |
p = atomsAddedWhileSweeping->lookupForAdd(lookup); |
| 447 |
|
| 448 |
// If that fails check the main table but check if any atom found there |
| 449 |
// is dead. |
| 450 |
if (!p) { |
| 451 |
if (AtomSet::AddPtr p2 = atoms.lookupForAdd(lookup)) { |
| 452 |
JSAtom* atom = p2->unbarrieredGet(); |
| 453 |
if (!IsAboutToBeFinalizedUnbarriered(atom)) { |
| 454 |
p = p2; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if (p) { |
| 461 |
return p->get(); |
| 462 |
} |
| 463 |
|
| 464 |
JSAtom* atom = AllocateNewAtomNonStaticValidLength(cx, chars, length, |
| 465 |
indexValue, lookup); |
| 466 |
if (!atom) { |
| 467 |
return nullptr; |
| 468 |
} |
| 469 |
|
| 470 |
// The operations above can't GC; therefore the atoms table has not been |
| 471 |
// modified and p is still valid. |
| 472 |
AtomSet* addSet = atomsAddedWhileSweeping ? atomsAddedWhileSweeping : &atoms; |
| 473 |
if (MOZ_UNLIKELY(!addSet->add(p, atom))) { |
| 474 |
ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */ |
| 475 |
return nullptr; |
| 476 |
} |
| 477 |
|
| 478 |
return atom; |
| 479 |
} |
| 480 |
|
| 481 |
/* |chars| must not point into an inline or short string. */ |
| 482 |
template <typename CharT> |
| 483 |
static MOZ_ALWAYS_INLINE JSAtom* AtomizeAndCopyChars( |
| 484 |
JSContext* cx, const CharT* chars, size_t length, |
| 485 |
const Maybe<uint32_t>& indexValue, const Maybe<js::HashNumber>& hash) { |
| 486 |
if (JSAtom* s = cx->staticStrings().lookup(chars, length)) { |
| 487 |
return s; |
| 488 |
} |
| 489 |
|
| 490 |
if (MOZ_UNLIKELY(!JSString::validateLength(cx, length))) { |
| 491 |
return nullptr; |
| 492 |
} |
| 493 |
|
| 494 |
if (hash.isSome()) { |
| 495 |
AtomHasher::Lookup lookup(hash.value(), chars, length); |
| 496 |
return AtomizeAndCopyCharsNonStaticValidLengthFromLookup( |
| 497 |
cx, chars, length, lookup, indexValue); |
| 498 |
} |
| 499 |
|
| 500 |
AtomHasher::Lookup lookup(chars, length); |
| 501 |
return AtomizeAndCopyCharsNonStaticValidLengthFromLookup(cx, chars, length, |
| 502 |
lookup, indexValue); |
| 503 |
} |
| 504 |
|
| 505 |
template <typename CharT> |
| 506 |
static MOZ_NEVER_INLINE JSAtom* |
| 507 |
PermanentlyAtomizeAndCopyCharsNonStaticValidLength( |
| 508 |
JSContext* cx, AtomSet& atomSet, const CharT* chars, size_t length, |
| 509 |
const AtomHasher::Lookup& lookup) { |
| 510 |
MOZ_ASSERT(!cx->permanentAtomsPopulated()); |
| 511 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime())); |
| 512 |
|
| 513 |
AtomSet::AddPtr p = atomSet.lookupForAdd(lookup); |
| 514 |
if (p) { |
| 515 |
return p->get(); |
| 516 |
} |
| 517 |
|
| 518 |
JSAtom* atom = |
| 519 |
AllocateNewPermanentAtomNonStaticValidLength(cx, chars, length, lookup); |
| 520 |
if (!atom) { |
| 521 |
return nullptr; |
| 522 |
} |
| 523 |
|
| 524 |
// We are single threaded at this point, and the operations we've done since |
| 525 |
// then can't GC; therefore the atoms table has not been modified and p is |
| 526 |
// still valid. |
| 527 |
if (!atomSet.add(p, atom)) { |
| 528 |
ReportOutOfMemory(cx); /* SystemAllocPolicy does not report OOM. */ |
| 529 |
return nullptr; |
| 530 |
} |
| 531 |
|
| 532 |
return atom; |
| 533 |
} |
| 534 |
|
| 535 |
struct AtomizeUTF8CharsWrapper { |
| 536 |
JS::UTF8Chars utf8; |
| 537 |
JS::SmallestEncoding encoding; |
| 538 |
|
| 539 |
AtomizeUTF8CharsWrapper(const JS::UTF8Chars& chars, |
| 540 |
JS::SmallestEncoding minEncode) |
| 541 |
: utf8(chars), encoding(minEncode) {} |
| 542 |
}; |
| 543 |
|
| 544 |
// NewAtomNonStaticValidLength has 3 variants. |
| 545 |
// This is used by Latin1Char and char16_t. |
| 546 |
template <typename CharT> |
| 547 |
static MOZ_ALWAYS_INLINE JSAtom* NewAtomNonStaticValidLength( |
| 548 |
JSContext* cx, const CharT* chars, size_t length, js::HashNumber hash) { |
| 549 |
return NewAtomCopyNMaybeDeflateValidLength(cx, chars, length, hash); |
| 550 |
} |
| 551 |
|
| 552 |
template <typename CharT> |
| 553 |
static MOZ_ALWAYS_INLINE JSAtom* MakeUTF8AtomHelperNonStaticValidLength( |
| 554 |
JSContext* cx, const AtomizeUTF8CharsWrapper* chars, size_t length, |
| 555 |
js::HashNumber hash) { |
| 556 |
if (JSAtom::lengthFitsInline<CharT>(length)) { |
| 557 |
CharT* storage; |
| 558 |
JSAtom* str = AllocateInlineAtom(cx, length, &storage, hash); |
| 559 |
if (!str) { |
| 560 |
return nullptr; |
| 561 |
} |
| 562 |
|
| 563 |
InflateUTF8CharsToBuffer(chars->utf8, storage, length, chars->encoding); |
| 564 |
return str; |
| 565 |
} |
| 566 |
|
| 567 |
// MakeAtomUTF8Helper is called from deep in the Atomization path, which |
| 568 |
// expects functions to fail gracefully with nullptr on OOM, without throwing. |
| 569 |
JSString::OwnedChars<CharT> newChars( |
| 570 |
AllocAtomCharsValidLength<CharT>(cx, length)); |
| 571 |
if (!newChars) { |
| 572 |
return nullptr; |
| 573 |
} |
| 574 |
|
| 575 |
InflateUTF8CharsToBuffer(chars->utf8, newChars.data(), length, |
| 576 |
chars->encoding); |
| 577 |
|
| 578 |
return JSAtom::newValidLength<CharT>(cx, newChars, hash); |
| 579 |
} |
| 580 |
|
| 581 |
// Another variant of NewAtomNonStaticValidLength. |
| 582 |
static MOZ_ALWAYS_INLINE JSAtom* NewAtomNonStaticValidLength( |
| 583 |
JSContext* cx, const AtomizeUTF8CharsWrapper* chars, size_t length, |
| 584 |
js::HashNumber hash) { |
| 585 |
if (length == 0) { |
| 586 |
return cx->emptyString(); |
| 587 |
} |
| 588 |
|
| 589 |
if (chars->encoding == JS::SmallestEncoding::UTF16) { |
| 590 |
return MakeUTF8AtomHelperNonStaticValidLength<char16_t>(cx, chars, length, |
| 591 |
hash); |
| 592 |
} |
| 593 |
return MakeUTF8AtomHelperNonStaticValidLength<JS::Latin1Char>(cx, chars, |
| 594 |
length, hash); |
| 595 |
} |
| 596 |
|
| 597 |
template <typename CharT> |
| 598 |
static MOZ_ALWAYS_INLINE JSAtom* AllocateNewAtomNonStaticValidLength( |
| 599 |
JSContext* cx, const CharT* chars, size_t length, |
| 600 |
const Maybe<uint32_t>& indexValue, const AtomHasher::Lookup& lookup) { |
| 601 |
AutoAllocInAtomsZone ac(cx); |
| 602 |
|
| 603 |
JSAtom* atom = NewAtomNonStaticValidLength(cx, chars, length, lookup.hash); |
| 604 |
if (!atom) { |
| 605 |
// Grudgingly forgo last-ditch GC. The alternative would be to manually GC |
| 606 |
// here, and retry from the top. |
| 607 |
ReportOutOfMemory(cx); |
| 608 |
return nullptr; |
| 609 |
} |
| 610 |
|
| 611 |
MOZ_ASSERT(atom->hash() == lookup.hash); |
| 612 |
|
| 613 |
if (indexValue) { |
| 614 |
atom->setIsIndex(*indexValue); |
| 615 |
} else { |
| 616 |
// We need to call isIndexSlow directly to avoid the flag check in isIndex, |
| 617 |
// because we still have to initialize that flag. |
| 618 |
uint32_t index; |
| 619 |
if (atom->isIndexSlow(&index)) { |
| 620 |
atom->setIsIndex(index); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
return atom; |
| 625 |
} |
| 626 |
|
| 627 |
template <typename CharT> |
| 628 |
static MOZ_ALWAYS_INLINE JSAtom* AllocateNewPermanentAtomNonStaticValidLength( |
| 629 |
JSContext* cx, const CharT* chars, size_t length, |
| 630 |
const AtomHasher::Lookup& lookup) { |
| 631 |
AutoAllocInAtomsZone ac(cx); |
| 632 |
|
| 633 |
#ifdef DEBUG |
| 634 |
if constexpr (std::is_same_v<CharT, char16_t>) { |
| 635 |
// Can call DontDeflate variant. |
| 636 |
MOZ_ASSERT(!CanStoreCharsAsLatin1(chars, length)); |
| 637 |
} |
| 638 |
#endif |
| 639 |
|
| 640 |
JSAtom* atom = |
| 641 |
NewAtomCopyNDontDeflateValidLength(cx, chars, length, lookup.hash); |
| 642 |
if (!atom) { |
| 643 |
// Do not bother with a last-ditch GC here since we are very early in |
| 644 |
// startup and there is no potential garbage to collect. |
| 645 |
ReportOutOfMemory(cx); |
| 646 |
return nullptr; |
| 647 |
} |
| 648 |
atom->makePermanent(); |
| 649 |
|
| 650 |
MOZ_ASSERT(atom->hash() == lookup.hash); |
| 651 |
|
| 652 |
uint32_t index; |
| 653 |
if (atom->isIndexSlow(&index)) { |
| 654 |
atom->setIsIndex(index); |
| 655 |
} |
| 656 |
|
| 657 |
return atom; |
| 658 |
} |
| 659 |
|
| 660 |
JSAtom* js::AtomizeStringSlow(JSContext* cx, JSString* str) { |
| 661 |
MOZ_ASSERT(!str->isAtom()); |
| 662 |
|
| 663 |
if (str->isAtomRef()) { |
| 664 |
return str->atom(); |
| 665 |
} |
| 666 |
|
| 667 |
if (JSAtom* atom = cx->caches().stringToAtomCache.lookup(str)) { |
| 668 |
return atom; |
| 669 |
} |
| 670 |
|
| 671 |
JS::Latin1Char flattenRope[StringToAtomCache::MinStringLength]; |
| 672 |
mozilla::Maybe<StringToAtomCache::AtomTableKey> key; |
| 673 |
size_t length = str->length(); |
| 674 |
if (str->isRope() && length < StringToAtomCache::MinStringLength && |
| 675 |
str->hasLatin1Chars()) { |
| 676 |
StringSegmentRange<StringToAtomCache::MinStringLength> iter(cx); |
| 677 |
if (iter.init(str)) { |
| 678 |
size_t index = 0; |
| 679 |
do { |
| 680 |
const JSLinearString* s = iter.front(); |
| 681 |
CopyChars(flattenRope + index, *s); |
| 682 |
index += s->length(); |
| 683 |
} while (iter.popFront() && !iter.empty()); |
| 684 |
|
| 685 |
if (JSAtom* atom = cx->caches().stringToAtomCache.lookupWithRopeChars( |
| 686 |
flattenRope, length, key)) { |
| 687 |
// Since this cache lookup is based on a string comparison, not object |
| 688 |
// identity, need to mark atom explicitly in this case. And this is |
| 689 |
// not done in lookup() itself, because #including JSContext.h there |
| 690 |
// causes some non-trivial #include ordering issues. |
| 691 |
cx->markAtom(atom); |
| 692 |
str->tryReplaceWithAtomRef(atom); |
| 693 |
return atom; |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
Maybe<uint32_t> indexValue; |
| 699 |
if (str->hasIndexValue()) { |
| 700 |
indexValue.emplace(str->getIndexValue()); |
| 701 |
} |
| 702 |
|
| 703 |
JSAtom* atom = nullptr; |
| 704 |
if (key.isSome()) { |
| 705 |
atom = AtomizeAndCopyChars(cx, key.value().string_, key.value().length_, |
| 706 |
indexValue, mozilla::Some(key.value().hash_)); |
| 707 |
} else { |
| 708 |
JSLinearString* linear = str->ensureLinear(cx); |
| 709 |
if (!linear) { |
| 710 |
return nullptr; |
| 711 |
} |
| 712 |
|
| 713 |
JS::AutoCheckCannotGC nogc; |
| 714 |
atom = linear->hasLatin1Chars() |
| 715 |
? AtomizeAndCopyChars(cx, linear->latin1Chars(nogc), |
| 716 |
linear->length(), indexValue, Nothing()) |
| 717 |
: AtomizeAndCopyChars(cx, linear->twoByteChars(nogc), |
| 718 |
linear->length(), indexValue, Nothing()); |
| 719 |
} |
| 720 |
|
| 721 |
if (!atom) { |
| 722 |
return nullptr; |
| 723 |
} |
| 724 |
|
| 725 |
if (!str->tryReplaceWithAtomRef(atom)) { |
| 726 |
cx->caches().stringToAtomCache.maybePut(str, atom, key); |
| 727 |
} |
| 728 |
|
| 729 |
return atom; |
| 730 |
} |
| 731 |
|
| 732 |
bool js::AtomIsPinned(JSContext* cx, JSAtom* atom) { return atom->isPinned(); } |
| 733 |
|
| 734 |
bool js::PinAtom(JSContext* cx, JSAtom* atom) { |
| 735 |
JS::AutoCheckCannotGC nogc; |
| 736 |
return cx->runtime()->atoms().maybePinExistingAtom(cx, atom); |
| 737 |
} |
| 738 |
|
| 739 |
bool AtomsTable::maybePinExistingAtom(JSContext* cx, JSAtom* atom) { |
| 740 |
MOZ_ASSERT(atom); |
| 741 |
|
| 742 |
if (atom->isPinned()) { |
| 743 |
return true; |
| 744 |
} |
| 745 |
|
| 746 |
if (!pinnedAtoms.append(atom)) { |
| 747 |
return false; |
| 748 |
} |
| 749 |
|
| 750 |
atom->setPinned(); |
| 751 |
return true; |
| 752 |
} |
| 753 |
|
| 754 |
JSAtom* js::Atomize(JSContext* cx, const char* bytes, size_t length, |
| 755 |
const Maybe<uint32_t>& indexValue) { |
| 756 |
const Latin1Char* chars = reinterpret_cast<const Latin1Char*>(bytes); |
| 757 |
return AtomizeAndCopyChars(cx, chars, length, indexValue, Nothing()); |
| 758 |
} |
| 759 |
|
| 760 |
template <typename CharT> |
| 761 |
JSAtom* js::AtomizeChars(JSContext* cx, const CharT* chars, size_t length) { |
| 762 |
return AtomizeAndCopyChars(cx, chars, length, Nothing(), Nothing()); |
| 763 |
} |
| 764 |
|
| 765 |
template JSAtom* js::AtomizeChars(JSContext* cx, const Latin1Char* chars, |
| 766 |
size_t length); |
| 767 |
|
| 768 |
template JSAtom* js::AtomizeChars(JSContext* cx, const char16_t* chars, |
| 769 |
size_t length); |
| 770 |
|
| 771 |
JSAtom* js::AtomizeWithoutActiveZone(JSContext* cx, const char* bytes, |
| 772 |
size_t length) { |
| 773 |
// This is used to implement JS_AtomizeAndPinString{N} when called without an |
| 774 |
// active zone. This simplifies the normal atomization code because it can |
| 775 |
// assume a non-null cx->zone(). |
| 776 |
|
| 777 |
MOZ_ASSERT(!cx->zone()); |
| 778 |
MOZ_ASSERT(cx->permanentAtomsPopulated()); |
| 779 |
|
| 780 |
const Latin1Char* chars = reinterpret_cast<const Latin1Char*>(bytes); |
| 781 |
|
| 782 |
if (JSAtom* s = cx->staticStrings().lookup(chars, length)) { |
| 783 |
return s; |
| 784 |
} |
| 785 |
|
| 786 |
if (MOZ_UNLIKELY(!JSString::validateLength(cx, length))) { |
| 787 |
return nullptr; |
| 788 |
} |
| 789 |
|
| 790 |
AtomHasher::Lookup lookup(chars, length); |
| 791 |
if (AtomSet::Ptr pp = cx->permanentAtoms().readonlyThreadsafeLookup(lookup)) { |
| 792 |
return pp->get(); |
| 793 |
} |
| 794 |
|
| 795 |
return cx->atoms().atomizeAndCopyCharsNonStaticValidLength(cx, chars, length, |
| 796 |
Nothing(), lookup); |
| 797 |
} |
| 798 |
|
| 799 |
/* |chars| must not point into an inline or short string. */ |
| 800 |
template <typename CharT> |
| 801 |
JSAtom* js::AtomizeCharsNonStaticValidLength(JSContext* cx, HashNumber hash, |
| 802 |
const CharT* chars, |
| 803 |
size_t length) { |
| 804 |
MOZ_ASSERT(!cx->staticStrings().lookup(chars, length)); |
| 805 |
|
| 806 |
AtomHasher::Lookup lookup(hash, chars, length); |
| 807 |
return AtomizeAndCopyCharsNonStaticValidLengthFromLookup(cx, chars, length, |
| 808 |
lookup, Nothing()); |
| 809 |
} |
| 810 |
|
| 811 |
template JSAtom* js::AtomizeCharsNonStaticValidLength(JSContext* cx, |
| 812 |
HashNumber hash, |
| 813 |
const Latin1Char* chars, |
| 814 |
size_t length); |
| 815 |
|
| 816 |
template JSAtom* js::AtomizeCharsNonStaticValidLength(JSContext* cx, |
| 817 |
HashNumber hash, |
| 818 |
const char16_t* chars, |
| 819 |
size_t length); |
| 820 |
|
| 821 |
static JSAtom* PermanentlyAtomizeCharsValidLength(JSContext* cx, |
| 822 |
AtomSet& atomSet, |
| 823 |
HashNumber hash, |
| 824 |
const Latin1Char* chars, |
| 825 |
size_t length) { |
| 826 |
if (JSAtom* s = cx->staticStrings().lookup(chars, length)) { |
| 827 |
return s; |
| 828 |
} |
| 829 |
|
| 830 |
return PermanentlyAtomizeCharsNonStaticValidLength(cx, atomSet, hash, chars, |
| 831 |
length); |
| 832 |
} |
| 833 |
|
| 834 |
JSAtom* js::PermanentlyAtomizeCharsNonStaticValidLength(JSContext* cx, |
| 835 |
AtomSet& atomSet, |
| 836 |
HashNumber hash, |
| 837 |
const Latin1Char* chars, |
| 838 |
size_t length) { |
| 839 |
MOZ_ASSERT(!cx->staticStrings().lookup(chars, length)); |
| 840 |
MOZ_ASSERT(length <= JSString::MAX_LENGTH); |
| 841 |
|
| 842 |
AtomHasher::Lookup lookup(hash, chars, length); |
| 843 |
return PermanentlyAtomizeAndCopyCharsNonStaticValidLength(cx, atomSet, chars, |
| 844 |
length, lookup); |
| 845 |
} |
| 846 |
|
| 847 |
JSAtom* js::AtomizeUTF8Chars(JSContext* cx, const char* utf8Chars, |
| 848 |
size_t utf8ByteLength) { |
| 849 |
{ |
| 850 |
StaticStrings& statics = cx->staticStrings(); |
| 851 |
|
| 852 |
// Handle all pure-ASCII UTF-8 static strings. |
| 853 |
if (JSAtom* s = statics.lookup(utf8Chars, utf8ByteLength)) { |
| 854 |
return s; |
| 855 |
} |
| 856 |
|
| 857 |
// The only non-ASCII static strings are the single-code point strings |
| 858 |
// U+0080 through U+00FF, encoded as |
| 859 |
// |
| 860 |
// 0b1100'00xx 0b10xx'xxxx |
| 861 |
// |
| 862 |
// where the encoded code point is the concatenation of the 'x' bits -- and |
| 863 |
// where the highest 'x' bit is necessarily 1 (because U+0080 through U+00FF |
| 864 |
// all contain an 0x80 bit). |
| 865 |
if (utf8ByteLength == 2) { |
| 866 |
auto first = static_cast<uint8_t>(utf8Chars[0]); |
| 867 |
if ((first & 0b1111'1110) == 0b1100'0010) { |
| 868 |
auto second = static_cast<uint8_t>(utf8Chars[1]); |
| 869 |
if (mozilla::IsTrailingUnit(mozilla::Utf8Unit(second))) { |
| 870 |
uint8_t unit = |
| 871 |
static_cast<uint8_t>(first << 6) | (second & 0b0011'1111); |
| 872 |
|
| 873 |
MOZ_ASSERT(StaticStrings::hasUnit(unit)); |
| 874 |
return statics.getUnit(unit); |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
// Fallthrough code handles the cases where the two units aren't a Latin-1 |
| 879 |
// code point or are invalid. |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
size_t length; |
| 884 |
HashNumber hash; |
| 885 |
JS::SmallestEncoding forCopy; |
| 886 |
JS::UTF8Chars utf8(utf8Chars, utf8ByteLength); |
| 887 |
if (!GetUTF8AtomizationData(cx, utf8, &length, &forCopy, &hash)) { |
| 888 |
return nullptr; |
| 889 |
} |
| 890 |
|
| 891 |
if (MOZ_UNLIKELY(!JSString::validateLength(cx, length))) { |
| 892 |
return nullptr; |
| 893 |
} |
| 894 |
|
| 895 |
AtomizeUTF8CharsWrapper chars(utf8, forCopy); |
| 896 |
AtomHasher::Lookup lookup(utf8Chars, utf8ByteLength, length, hash); |
| 897 |
return AtomizeAndCopyCharsNonStaticValidLengthFromLookup(cx, &chars, length, |
| 898 |
lookup, Nothing()); |
| 899 |
} |
| 900 |
|
| 901 |
bool js::IndexToIdSlow(JSContext* cx, uint32_t index, MutableHandleId idp) { |
| 902 |
MOZ_ASSERT(index > JS::PropertyKey::IntMax); |
| 903 |
|
| 904 |
char buf[UINT32_CHAR_BUFFER_LENGTH]; |
| 905 |
|
| 906 |
auto result = std::to_chars(buf, buf + std::size(buf), index, 10); |
| 907 |
MOZ_ASSERT(result.ec == std::errc()); |
| 908 |
|
| 909 |
size_t length = result.ptr - buf; |
| 910 |
JSAtom* atom = Atomize(cx, buf, length); |
| 911 |
if (!atom) { |
| 912 |
return false; |
| 913 |
} |
| 914 |
|
| 915 |
idp.set(JS::PropertyKey::NonIntAtom(atom)); |
| 916 |
return true; |
| 917 |
} |
| 918 |
|
| 919 |
template <AllowGC allowGC> |
| 920 |
static MOZ_ALWAYS_INLINE JSAtom* PrimitiveToAtom(JSContext* cx, |
| 921 |
const Value& v) { |
| 922 |
MOZ_ASSERT(v.isPrimitive()); |
| 923 |
switch (v.type()) { |
| 924 |
case ValueType::String: { |
| 925 |
JSAtom* atom = AtomizeString(cx, v.toString()); |
| 926 |
if (!allowGC && !atom) { |
| 927 |
cx->recoverFromOutOfMemory(); |
| 928 |
} |
| 929 |
return atom; |
| 930 |
} |
| 931 |
case ValueType::Int32: { |
| 932 |
JSAtom* atom = Int32ToAtom(cx, v.toInt32()); |
| 933 |
if (!allowGC && !atom) { |
| 934 |
cx->recoverFromOutOfMemory(); |
| 935 |
} |
| 936 |
return atom; |
| 937 |
} |
| 938 |
case ValueType::Double: { |
| 939 |
JSAtom* atom = NumberToAtom(cx, v.toDouble()); |
| 940 |
if (!allowGC && !atom) { |
| 941 |
cx->recoverFromOutOfMemory(); |
| 942 |
} |
| 943 |
return atom; |
| 944 |
} |
| 945 |
case ValueType::Boolean: |
| 946 |
return v.toBoolean() ? cx->names().true_ : cx->names().false_; |
| 947 |
case ValueType::Null: |
| 948 |
return cx->names().null; |
| 949 |
case ValueType::Undefined: |
| 950 |
return cx->names().undefined; |
| 951 |
case ValueType::Symbol: |
| 952 |
if constexpr (allowGC) { |
| 953 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 954 |
JSMSG_SYMBOL_TO_STRING); |
| 955 |
} |
| 956 |
return nullptr; |
| 957 |
case ValueType::BigInt: { |
| 958 |
RootedBigInt i(cx, v.toBigInt()); |
| 959 |
return BigIntToAtom<allowGC>(cx, i); |
| 960 |
} |
| 961 |
case ValueType::Object: |
| 962 |
case ValueType::Magic: |
| 963 |
case ValueType::PrivateGCThing: |
| 964 |
break; |
| 965 |
} |
| 966 |
MOZ_CRASH("Unexpected type"); |
| 967 |
} |
| 968 |
|
| 969 |
template <AllowGC allowGC> |
| 970 |
static JSAtom* ToAtomSlow( |
| 971 |
JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType arg) { |
| 972 |
MOZ_ASSERT(!arg.isString()); |
| 973 |
|
| 974 |
Value v = arg; |
| 975 |
if (!v.isPrimitive()) { |
| 976 |
if (!allowGC) { |
| 977 |
return nullptr; |
| 978 |
} |
| 979 |
RootedValue v2(cx, v); |
| 980 |
if (!ToPrimitive(cx, JSTYPE_STRING, &v2)) { |
| 981 |
return nullptr; |
| 982 |
} |
| 983 |
v = v2; |
| 984 |
} |
| 985 |
|
| 986 |
return PrimitiveToAtom<allowGC>(cx, v); |
| 987 |
} |
| 988 |
|
| 989 |
template <AllowGC allowGC> |
| 990 |
JSAtom* js::ToAtom(JSContext* cx, |
| 991 |
typename MaybeRooted<Value, allowGC>::HandleType v) { |
| 992 |
if (!v.isString()) { |
| 993 |
return ToAtomSlow<allowGC>(cx, v); |
| 994 |
} |
| 995 |
|
| 996 |
JSAtom* atom = AtomizeString(cx, v.toString()); |
| 997 |
if (!atom && !allowGC) { |
| 998 |
MOZ_ASSERT(cx->isThrowingOutOfMemory()); |
| 999 |
cx->recoverFromOutOfMemory(); |
| 1000 |
} |
| 1001 |
return atom; |
| 1002 |
} |
| 1003 |
|
| 1004 |
template JSAtom* js::ToAtom<CanGC>(JSContext* cx, HandleValue v); |
| 1005 |
template JSAtom* js::ToAtom<NoGC>(JSContext* cx, const Value& v); |
| 1006 |
|
| 1007 |
template <AllowGC allowGC> |
| 1008 |
bool js::PrimitiveValueToIdSlow( |
| 1009 |
JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType v, |
| 1010 |
typename MaybeRooted<jsid, allowGC>::MutableHandleType idp) { |
| 1011 |
MOZ_ASSERT(v.isPrimitive()); |
| 1012 |
MOZ_ASSERT(!v.isString()); |
| 1013 |
MOZ_ASSERT(!v.isSymbol()); |
| 1014 |
MOZ_ASSERT_IF(v.isInt32(), !PropertyKey::fitsInInt(v.toInt32())); |
| 1015 |
|
| 1016 |
int32_t i; |
| 1017 |
if (v.isDouble() && mozilla::NumberEqualsInt32(v.toDouble(), &i) && |
| 1018 |
PropertyKey::fitsInInt(i)) { |
| 1019 |
idp.set(PropertyKey::Int(i)); |
| 1020 |
return true; |
| 1021 |
} |
| 1022 |
|
| 1023 |
JSAtom* atom = PrimitiveToAtom<allowGC>(cx, v); |
| 1024 |
if (!atom) { |
| 1025 |
return false; |
| 1026 |
} |
| 1027 |
|
| 1028 |
idp.set(AtomToId(atom)); |
| 1029 |
return true; |
| 1030 |
} |
| 1031 |
|
| 1032 |
template bool js::PrimitiveValueToIdSlow<CanGC>(JSContext* cx, HandleValue v, |
| 1033 |
MutableHandleId idp); |
| 1034 |
template bool js::PrimitiveValueToIdSlow<NoGC>(JSContext* cx, const Value& v, |
| 1035 |
FakeMutableHandle<jsid> idp); |
| 1036 |
|
| 1037 |
Handle<PropertyName*> js::ClassName(JSProtoKey key, JSContext* cx) { |
| 1038 |
return ClassName(key, cx->names()); |
| 1039 |
} |