| 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 |
#include "frontend/ParserAtom.h" |
| 8 |
|
| 9 |
#include "mozilla/TextUtils.h" // mozilla::IsAscii |
| 10 |
|
| 11 |
#include <memory> // std::uninitialized_fill_n |
| 12 |
|
| 13 |
#include "jsnum.h" // CharsToNumber |
| 14 |
|
| 15 |
#include "frontend/CompilationStencil.h" |
| 16 |
#include "js/GCAPI.h" // JS::AutoSuppressGCAnalysis |
| 17 |
#include "js/Printer.h" // Sprinter, QuoteString |
| 18 |
#include "util/Identifier.h" // IsIdentifier |
| 19 |
#include "util/StringBuilder.h" // StringBuilder |
| 20 |
#include "util/Text.h" // AsciiDigitToNumber |
| 21 |
#include "util/Unicode.h" |
| 22 |
#include "vm/JSContext.h" |
| 23 |
#include "vm/Runtime.h" |
| 24 |
#include "vm/SelfHosting.h" // ExtendedUnclonedSelfHostedFunctionNamePrefix |
| 25 |
#include "vm/StaticStrings.h" |
| 26 |
#include "vm/StringType.h" |
| 27 |
|
| 28 |
using namespace js; |
| 29 |
using namespace js::frontend; |
| 30 |
|
| 31 |
namespace js { |
| 32 |
namespace frontend { |
| 33 |
|
| 34 |
JSAtom* GetWellKnownAtom(JSContext* cx, WellKnownAtomId atomId) { |
| 35 |
#define ASSERT_OFFSET_(NAME, _) \ |
| 36 |
static_assert(offsetof(JSAtomState, NAME) == \ |
| 37 |
int32_t(WellKnownAtomId::NAME) * \ |
| 38 |
sizeof(js::ImmutableTenuredPtr<PropertyName*>)); |
| 39 |
FOR_EACH_COMMON_PROPERTYNAME(ASSERT_OFFSET_); |
| 40 |
#undef ASSERT_OFFSET_ |
| 41 |
|
| 42 |
#define ASSERT_OFFSET_(NAME, _) \ |
| 43 |
static_assert(offsetof(JSAtomState, NAME) == \ |
| 44 |
int32_t(WellKnownAtomId::NAME) * \ |
| 45 |
sizeof(js::ImmutableTenuredPtr<PropertyName*>)); |
| 46 |
JS_FOR_EACH_PROTOTYPE(ASSERT_OFFSET_); |
| 47 |
#undef ASSERT_OFFSET_ |
| 48 |
|
| 49 |
#define ASSERT_OFFSET_(NAME) \ |
| 50 |
static_assert(offsetof(JSAtomState, NAME) == \ |
| 51 |
int32_t(WellKnownAtomId::NAME) * \ |
| 52 |
sizeof(js::ImmutableTenuredPtr<PropertyName*>)); |
| 53 |
JS_FOR_EACH_WELL_KNOWN_SYMBOL(ASSERT_OFFSET_); |
| 54 |
#undef ASSERT_OFFSET_ |
| 55 |
|
| 56 |
static_assert(int32_t(WellKnownAtomId::abort) == 0, |
| 57 |
"Unexpected order of WellKnownAtom"); |
| 58 |
|
| 59 |
return (&cx->names().abort)[int32_t(atomId)]; |
| 60 |
} |
| 61 |
|
| 62 |
#ifdef DEBUG |
| 63 |
void TaggedParserAtomIndex::validateRaw() { |
| 64 |
if (isParserAtomIndex()) { |
| 65 |
MOZ_ASSERT(toParserAtomIndex().index < IndexLimit); |
| 66 |
} else if (isWellKnownAtomId()) { |
| 67 |
MOZ_ASSERT(uint32_t(toWellKnownAtomId()) < |
| 68 |
uint32_t(WellKnownAtomId::Limit)); |
| 69 |
} else if (isLength1StaticParserString()) { |
| 70 |
// always valid |
| 71 |
} else if (isLength2StaticParserString()) { |
| 72 |
MOZ_ASSERT(size_t(toLength2StaticParserString()) < Length2StaticLimit); |
| 73 |
} else if (isLength3StaticParserString()) { |
| 74 |
// always valid |
| 75 |
} else { |
| 76 |
MOZ_ASSERT(isNull()); |
| 77 |
} |
| 78 |
} |
| 79 |
#endif |
| 80 |
|
| 81 |
HashNumber TaggedParserAtomIndex::staticOrWellKnownHash() const { |
| 82 |
MOZ_ASSERT(!isParserAtomIndex()); |
| 83 |
|
| 84 |
if (isWellKnownAtomId()) { |
| 85 |
const auto& info = GetWellKnownAtomInfo(toWellKnownAtomId()); |
| 86 |
return info.hash; |
| 87 |
} |
| 88 |
|
| 89 |
if (isLength1StaticParserString()) { |
| 90 |
Latin1Char content[1]; |
| 91 |
ParserAtomsTable::getLength1Content(toLength1StaticParserString(), content); |
| 92 |
return mozilla::HashString(content, 1); |
| 93 |
} |
| 94 |
|
| 95 |
if (isLength2StaticParserString()) { |
| 96 |
char content[2]; |
| 97 |
ParserAtomsTable::getLength2Content(toLength2StaticParserString(), content); |
| 98 |
return mozilla::HashString(reinterpret_cast<const Latin1Char*>(content), 2); |
| 99 |
} |
| 100 |
|
| 101 |
MOZ_ASSERT(isLength3StaticParserString()); |
| 102 |
char content[3]; |
| 103 |
ParserAtomsTable::getLength3Content(toLength3StaticParserString(), content); |
| 104 |
return mozilla::HashString(reinterpret_cast<const Latin1Char*>(content), 3); |
| 105 |
} |
| 106 |
|
| 107 |
template <typename CharT, typename SeqCharT> |
| 108 |
/* static */ ParserAtom* ParserAtom::allocate( |
| 109 |
FrontendContext* fc, LifoAlloc& alloc, InflatedChar16Sequence<SeqCharT> seq, |
| 110 |
uint32_t length, HashNumber hash) { |
| 111 |
if (length > JSString::MAX_LENGTH) { |
| 112 |
ReportAllocationOverflow(fc); |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
constexpr size_t HeaderSize = sizeof(ParserAtom); |
| 117 |
void* raw = alloc.alloc(HeaderSize + (sizeof(CharT) * length)); |
| 118 |
if (!raw) { |
| 119 |
js::ReportOutOfMemory(fc); |
| 120 |
return nullptr; |
| 121 |
} |
| 122 |
|
| 123 |
constexpr bool hasTwoByteChars = (sizeof(CharT) == 2); |
| 124 |
static_assert(sizeof(CharT) == 1 || sizeof(CharT) == 2, |
| 125 |
"CharT should be 1 or 2 byte type"); |
| 126 |
ParserAtom* entry = new (raw) ParserAtom(length, hash, hasTwoByteChars); |
| 127 |
CharT* entryBuf = entry->chars<CharT>(); |
| 128 |
drainChar16Seq(entryBuf, seq, length); |
| 129 |
return entry; |
| 130 |
} |
| 131 |
|
| 132 |
bool ParserAtom::isInstantiatedAsJSAtom() const { |
| 133 |
if (isMarkedAtomize()) { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
// Always use JSAtom for short strings. |
| 138 |
if (length() < MinimumLengthForNonAtom) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
JSString* ParserAtom::instantiateString(JSContext* cx, FrontendContext* fc, |
| 146 |
ParserAtomIndex index, |
| 147 |
CompilationAtomCache& atomCache) const { |
| 148 |
MOZ_ASSERT(!isInstantiatedAsJSAtom()); |
| 149 |
|
| 150 |
JSString* str; |
| 151 |
if (hasLatin1Chars()) { |
| 152 |
str = NewStringCopyNDontDeflateNonStaticValidLength<CanGC>( |
| 153 |
cx, latin1Chars(), length(), gc::Heap::Tenured); |
| 154 |
} else { |
| 155 |
str = NewStringCopyNDontDeflateNonStaticValidLength<CanGC>( |
| 156 |
cx, twoByteChars(), length(), gc::Heap::Tenured); |
| 157 |
} |
| 158 |
if (!str) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
if (!atomCache.setAtomAt(fc, index, str)) { |
| 162 |
return nullptr; |
| 163 |
} |
| 164 |
|
| 165 |
return str; |
| 166 |
} |
| 167 |
|
| 168 |
JSAtom* ParserAtom::instantiateAtom(JSContext* cx, FrontendContext* fc, |
| 169 |
ParserAtomIndex index, |
| 170 |
CompilationAtomCache& atomCache) const { |
| 171 |
MOZ_ASSERT(isInstantiatedAsJSAtom()); |
| 172 |
|
| 173 |
JSAtom* atom; |
| 174 |
if (hasLatin1Chars()) { |
| 175 |
atom = |
| 176 |
AtomizeCharsNonStaticValidLength(cx, hash(), latin1Chars(), length()); |
| 177 |
} else { |
| 178 |
atom = |
| 179 |
AtomizeCharsNonStaticValidLength(cx, hash(), twoByteChars(), length()); |
| 180 |
} |
| 181 |
if (!atom) { |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
if (!atomCache.setAtomAt(fc, index, atom)) { |
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
return atom; |
| 188 |
} |
| 189 |
|
| 190 |
JSAtom* ParserAtom::instantiatePermanentAtom( |
| 191 |
JSContext* cx, FrontendContext* fc, AtomSet& atomSet, ParserAtomIndex index, |
| 192 |
CompilationAtomCache& atomCache) const { |
| 193 |
MOZ_ASSERT(!cx->zone()); |
| 194 |
|
| 195 |
MOZ_ASSERT(hasLatin1Chars()); |
| 196 |
MOZ_ASSERT(length() <= JSString::MAX_LENGTH); |
| 197 |
JSAtom* atom = PermanentlyAtomizeCharsNonStaticValidLength( |
| 198 |
cx, atomSet, hash(), latin1Chars(), length()); |
| 199 |
if (!atom) { |
| 200 |
return nullptr; |
| 201 |
} |
| 202 |
if (!atomCache.setAtomAt(fc, index, atom)) { |
| 203 |
return nullptr; |
| 204 |
} |
| 205 |
return atom; |
| 206 |
} |
| 207 |
|
| 208 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 209 |
void ParserAtom::dump() const { |
| 210 |
js::Fprinter out(stderr); |
| 211 |
out.put("\""); |
| 212 |
dumpCharsNoQuote(out); |
| 213 |
out.put("\"\n"); |
| 214 |
} |
| 215 |
|
| 216 |
void ParserAtom::dumpCharsNoQuote(js::GenericPrinter& out) const { |
| 217 |
if (hasLatin1Chars()) { |
| 218 |
JSString::dumpCharsNoQuote<Latin1Char>(latin1Chars(), length(), out); |
| 219 |
} else { |
| 220 |
JSString::dumpCharsNoQuote<char16_t>(twoByteChars(), length(), out); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void ParserAtomsTable::dump(TaggedParserAtomIndex index) const { |
| 225 |
if (index.isParserAtomIndex()) { |
| 226 |
getParserAtom(index.toParserAtomIndex())->dump(); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
if (index.isWellKnownAtomId()) { |
| 231 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 232 |
js::Fprinter out(stderr); |
| 233 |
out.put("\""); |
| 234 |
out.put(info.content, info.length); |
| 235 |
out.put("\""); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
if (index.isLength1StaticParserString()) { |
| 240 |
js::Fprinter out(stderr); |
| 241 |
out.put("\""); |
| 242 |
dumpCharsNoQuote(out, index.toLength1StaticParserString()); |
| 243 |
out.put("\"\n"); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
if (index.isLength2StaticParserString()) { |
| 248 |
js::Fprinter out(stderr); |
| 249 |
out.put("\""); |
| 250 |
dumpCharsNoQuote(out, index.toLength2StaticParserString()); |
| 251 |
out.put("\"\n"); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
if (index.isLength3StaticParserString()) { |
| 256 |
js::Fprinter out(stderr); |
| 257 |
out.put("\""); |
| 258 |
dumpCharsNoQuote(out, index.toLength3StaticParserString()); |
| 259 |
out.put("\"\n"); |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
MOZ_ASSERT(index.isNull()); |
| 264 |
js::Fprinter out(stderr); |
| 265 |
out.put("#<null>"); |
| 266 |
} |
| 267 |
|
| 268 |
void ParserAtomsTable::dumpCharsNoQuote(js::GenericPrinter& out, |
| 269 |
TaggedParserAtomIndex index) const { |
| 270 |
if (index.isParserAtomIndex()) { |
| 271 |
getParserAtom(index.toParserAtomIndex())->dumpCharsNoQuote(out); |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
if (index.isWellKnownAtomId()) { |
| 276 |
dumpCharsNoQuote(out, index.toWellKnownAtomId()); |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
if (index.isLength1StaticParserString()) { |
| 281 |
dumpCharsNoQuote(out, index.toLength1StaticParserString()); |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
if (index.isLength2StaticParserString()) { |
| 286 |
dumpCharsNoQuote(out, index.toLength2StaticParserString()); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
if (index.isLength3StaticParserString()) { |
| 291 |
dumpCharsNoQuote(out, index.toLength3StaticParserString()); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
MOZ_ASSERT(index.isNull()); |
| 296 |
out.put("#<null>"); |
| 297 |
} |
| 298 |
|
| 299 |
/* static */ |
| 300 |
void ParserAtomsTable::dumpCharsNoQuote(js::GenericPrinter& out, |
| 301 |
WellKnownAtomId id) { |
| 302 |
const auto& info = GetWellKnownAtomInfo(id); |
| 303 |
out.put(info.content, info.length); |
| 304 |
} |
| 305 |
|
| 306 |
/* static */ |
| 307 |
void ParserAtomsTable::dumpCharsNoQuote(js::GenericPrinter& out, |
| 308 |
Length1StaticParserString index) { |
| 309 |
Latin1Char content[1]; |
| 310 |
getLength1Content(index, content); |
| 311 |
out.putChar(content[0]); |
| 312 |
} |
| 313 |
|
| 314 |
/* static */ |
| 315 |
void ParserAtomsTable::dumpCharsNoQuote(js::GenericPrinter& out, |
| 316 |
Length2StaticParserString index) { |
| 317 |
char content[2]; |
| 318 |
getLength2Content(index, content); |
| 319 |
out.putChar(content[0]); |
| 320 |
out.putChar(content[1]); |
| 321 |
} |
| 322 |
|
| 323 |
/* static */ |
| 324 |
void ParserAtomsTable::dumpCharsNoQuote(js::GenericPrinter& out, |
| 325 |
Length3StaticParserString index) { |
| 326 |
char content[3]; |
| 327 |
getLength3Content(index, content); |
| 328 |
out.putChar(content[0]); |
| 329 |
out.putChar(content[1]); |
| 330 |
out.putChar(content[2]); |
| 331 |
} |
| 332 |
#endif |
| 333 |
|
| 334 |
ParserAtomsTable::ParserAtomsTable(LifoAlloc& alloc) : alloc_(&alloc) {} |
| 335 |
|
| 336 |
TaggedParserAtomIndex ParserAtomsTable::addEntry(FrontendContext* fc, |
| 337 |
EntryMap::AddPtr& addPtr, |
| 338 |
ParserAtom* entry) { |
| 339 |
MOZ_ASSERT(!addPtr); |
| 340 |
ParserAtomIndex index = ParserAtomIndex(entries_.length()); |
| 341 |
if (size_t(index) >= TaggedParserAtomIndex::IndexLimit) { |
| 342 |
ReportAllocationOverflow(fc); |
| 343 |
return TaggedParserAtomIndex::null(); |
| 344 |
} |
| 345 |
if (!entries_.append(entry)) { |
| 346 |
js::ReportOutOfMemory(fc); |
| 347 |
return TaggedParserAtomIndex::null(); |
| 348 |
} |
| 349 |
auto taggedIndex = TaggedParserAtomIndex(index); |
| 350 |
if (!entryMap_.add(addPtr, entry, taggedIndex)) { |
| 351 |
js::ReportOutOfMemory(fc); |
| 352 |
return TaggedParserAtomIndex::null(); |
| 353 |
} |
| 354 |
return taggedIndex; |
| 355 |
} |
| 356 |
|
| 357 |
template <typename AtomCharT, typename SeqCharT> |
| 358 |
TaggedParserAtomIndex ParserAtomsTable::internChar16Seq( |
| 359 |
FrontendContext* fc, EntryMap::AddPtr& addPtr, HashNumber hash, |
| 360 |
InflatedChar16Sequence<SeqCharT> seq, uint32_t length) { |
| 361 |
MOZ_ASSERT(!addPtr); |
| 362 |
|
| 363 |
ParserAtom* entry = |
| 364 |
ParserAtom::allocate<AtomCharT>(fc, *alloc_, seq, length, hash); |
| 365 |
if (!entry) { |
| 366 |
return TaggedParserAtomIndex::null(); |
| 367 |
} |
| 368 |
return addEntry(fc, addPtr, entry); |
| 369 |
} |
| 370 |
|
| 371 |
static const uint16_t MAX_LATIN1_CHAR = 0xff; |
| 372 |
|
| 373 |
TaggedParserAtomIndex ParserAtomsTable::internAscii(FrontendContext* fc, |
| 374 |
const char* asciiPtr, |
| 375 |
uint32_t length) { |
| 376 |
// ASCII strings are strict subsets of Latin1 strings. |
| 377 |
const Latin1Char* latin1Ptr = reinterpret_cast<const Latin1Char*>(asciiPtr); |
| 378 |
return internLatin1(fc, latin1Ptr, length); |
| 379 |
} |
| 380 |
|
| 381 |
TaggedParserAtomIndex ParserAtomsTable::internLatin1( |
| 382 |
FrontendContext* fc, const Latin1Char* latin1Ptr, uint32_t length) { |
| 383 |
// Check for tiny strings which are abundant in minified code. |
| 384 |
if (auto tiny = WellKnownParserAtoms::getSingleton().lookupTinyIndex( |
| 385 |
latin1Ptr, length)) { |
| 386 |
return tiny; |
| 387 |
} |
| 388 |
|
| 389 |
// Check for well-known atom. |
| 390 |
InflatedChar16Sequence<Latin1Char> seq(latin1Ptr, length); |
| 391 |
SpecificParserAtomLookup<Latin1Char> lookup(seq); |
| 392 |
if (auto wk = WellKnownParserAtoms::getSingleton().lookupChar16Seq(lookup)) { |
| 393 |
return wk; |
| 394 |
} |
| 395 |
|
| 396 |
// Check for existing atom. |
| 397 |
auto addPtr = entryMap_.lookupForAdd(lookup); |
| 398 |
if (addPtr) { |
| 399 |
return addPtr->value(); |
| 400 |
} |
| 401 |
|
| 402 |
return internChar16Seq<Latin1Char>(fc, addPtr, lookup.hash(), seq, length); |
| 403 |
} |
| 404 |
|
| 405 |
bool IsWide(const InflatedChar16Sequence<char16_t>& seq) { |
| 406 |
InflatedChar16Sequence<char16_t> seqCopy = seq; |
| 407 |
while (seqCopy.hasMore()) { |
| 408 |
char16_t ch = seqCopy.next(); |
| 409 |
if (ch > MAX_LATIN1_CHAR) { |
| 410 |
return true; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
template <typename AtomCharT> |
| 418 |
TaggedParserAtomIndex ParserAtomsTable::internExternalParserAtomImpl( |
| 419 |
FrontendContext* fc, const ParserAtom* atom) { |
| 420 |
InflatedChar16Sequence<AtomCharT> seq(atom->chars<AtomCharT>(), |
| 421 |
atom->length()); |
| 422 |
SpecificParserAtomLookup<AtomCharT> lookup(seq, atom->hash()); |
| 423 |
|
| 424 |
// Check for existing atom. |
| 425 |
auto addPtr = entryMap_.lookupForAdd(lookup); |
| 426 |
if (addPtr) { |
| 427 |
auto index = addPtr->value(); |
| 428 |
|
| 429 |
// Copy UsedByStencilFlag and AtomizeFlag. |
| 430 |
MOZ_ASSERT(entries_[index.toParserAtomIndex()]->hasTwoByteChars() == |
| 431 |
atom->hasTwoByteChars()); |
| 432 |
entries_[index.toParserAtomIndex()]->flags_ |= atom->flags_; |
| 433 |
return index; |
| 434 |
} |
| 435 |
|
| 436 |
auto index = |
| 437 |
internChar16Seq<AtomCharT>(fc, addPtr, atom->hash(), seq, atom->length()); |
| 438 |
if (!index) { |
| 439 |
return TaggedParserAtomIndex::null(); |
| 440 |
} |
| 441 |
|
| 442 |
// Copy UsedByStencilFlag and AtomizeFlag. |
| 443 |
MOZ_ASSERT(entries_[index.toParserAtomIndex()]->hasTwoByteChars() == |
| 444 |
atom->hasTwoByteChars()); |
| 445 |
entries_[index.toParserAtomIndex()]->flags_ |= atom->flags_; |
| 446 |
return index; |
| 447 |
} |
| 448 |
|
| 449 |
TaggedParserAtomIndex ParserAtomsTable::internExternalParserAtom( |
| 450 |
FrontendContext* fc, const ParserAtom* atom) { |
| 451 |
if (atom->hasLatin1Chars()) { |
| 452 |
return internExternalParserAtomImpl<JS::Latin1Char>(fc, atom); |
| 453 |
} |
| 454 |
return internExternalParserAtomImpl<char16_t>(fc, atom); |
| 455 |
} |
| 456 |
|
| 457 |
bool ParserAtomsTable::addPlaceholder(FrontendContext* fc) { |
| 458 |
ParserAtomIndex index = ParserAtomIndex(entries_.length()); |
| 459 |
if (size_t(index) >= TaggedParserAtomIndex::IndexLimit) { |
| 460 |
ReportAllocationOverflow(fc); |
| 461 |
return false; |
| 462 |
} |
| 463 |
if (!entries_.append(nullptr)) { |
| 464 |
js::ReportOutOfMemory(fc); |
| 465 |
return false; |
| 466 |
} |
| 467 |
return true; |
| 468 |
} |
| 469 |
|
| 470 |
TaggedParserAtomIndex ParserAtomsTable::internExternalParserAtomIndex( |
| 471 |
FrontendContext* fc, const CompilationStencil& context, |
| 472 |
TaggedParserAtomIndex atom) { |
| 473 |
// When the atom is not a parser atom index, the value represent the atom |
| 474 |
// without the need for a ParserAtom, and thus we can skip interning it. |
| 475 |
if (!atom.isParserAtomIndex()) { |
| 476 |
return atom; |
| 477 |
} |
| 478 |
auto index = atom.toParserAtomIndex(); |
| 479 |
return internExternalParserAtom(fc, context.parserAtomData[index]); |
| 480 |
} |
| 481 |
|
| 482 |
bool ParserAtomsTable::isEqualToExternalParserAtomIndex( |
| 483 |
TaggedParserAtomIndex internal, const CompilationStencil& context, |
| 484 |
TaggedParserAtomIndex external) const { |
| 485 |
// If one is null, well-known or static, then testing the equality of the bits |
| 486 |
// of the TaggedParserAtomIndex is sufficient. |
| 487 |
if (!internal.isParserAtomIndex() || !external.isParserAtomIndex()) { |
| 488 |
return internal == external; |
| 489 |
} |
| 490 |
|
| 491 |
// Otherwise we have to compare 2 atom-indexes from different ParserAtomTable. |
| 492 |
ParserAtom* internalAtom = getParserAtom(internal.toParserAtomIndex()); |
| 493 |
ParserAtom* externalAtom = |
| 494 |
context.parserAtomData[external.toParserAtomIndex()]; |
| 495 |
|
| 496 |
if (internalAtom->hash() != externalAtom->hash()) { |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
HashNumber hash = internalAtom->hash(); |
| 501 |
size_t length = internalAtom->length(); |
| 502 |
if (internalAtom->hasLatin1Chars()) { |
| 503 |
const Latin1Char* chars = internalAtom->latin1Chars(); |
| 504 |
InflatedChar16Sequence<Latin1Char> seq(chars, length); |
| 505 |
return externalAtom->equalsSeq(hash, seq); |
| 506 |
} |
| 507 |
|
| 508 |
const char16_t* chars = internalAtom->twoByteChars(); |
| 509 |
InflatedChar16Sequence<char16_t> seq(chars, length); |
| 510 |
return externalAtom->equalsSeq(hash, seq); |
| 511 |
} |
| 512 |
|
| 513 |
bool ParserAtomSpanBuilder::allocate(FrontendContext* fc, LifoAlloc& alloc, |
| 514 |
size_t count) { |
| 515 |
if (count >= TaggedParserAtomIndex::IndexLimit) { |
| 516 |
ReportAllocationOverflow(fc); |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
auto* p = alloc.newArrayUninitialized<ParserAtom*>(count); |
| 521 |
if (!p) { |
| 522 |
js::ReportOutOfMemory(fc); |
| 523 |
return false; |
| 524 |
} |
| 525 |
std::uninitialized_fill_n(p, count, nullptr); |
| 526 |
|
| 527 |
entries_ = mozilla::Span(p, count); |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
static inline bool IsLatin1(mozilla::Utf8Unit c1, mozilla::Utf8Unit c2) { |
| 532 |
auto u1 = c1.toUint8(); |
| 533 |
auto u2 = c2.toUint8(); |
| 534 |
|
| 535 |
// 0x80-0xBF |
| 536 |
if (u1 == 0xC2 && 0x80 <= u2 && u2 <= 0xBF) { |
| 537 |
return true; |
| 538 |
} |
| 539 |
|
| 540 |
// 0xC0-0xFF |
| 541 |
if (u1 == 0xC3 && 0x80 <= u2 && u2 <= 0xBF) { |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
return false; |
| 546 |
} |
| 547 |
|
| 548 |
TaggedParserAtomIndex ParserAtomsTable::internUtf8( |
| 549 |
FrontendContext* fc, const mozilla::Utf8Unit* utf8Ptr, uint32_t nbyte) { |
| 550 |
if (auto tiny = WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8( |
| 551 |
utf8Ptr, nbyte)) { |
| 552 |
return tiny; |
| 553 |
} |
| 554 |
|
| 555 |
// If source text is ASCII, then the length of the target char buffer |
| 556 |
// is the same as the length of the UTF8 input. Convert it to a Latin1 |
| 557 |
// encoded string on the heap. |
| 558 |
JS::UTF8Chars utf8(utf8Ptr, nbyte); |
| 559 |
JS::SmallestEncoding minEncoding = FindSmallestEncoding(utf8); |
| 560 |
if (minEncoding == JS::SmallestEncoding::ASCII) { |
| 561 |
// As ascii strings are a subset of Latin1 strings, and each encoding |
| 562 |
// unit is the same size, we can reliably cast this `Utf8Unit*` |
| 563 |
// to a `Latin1Char*`. |
| 564 |
const Latin1Char* latin1Ptr = reinterpret_cast<const Latin1Char*>(utf8Ptr); |
| 565 |
return internLatin1(fc, latin1Ptr, nbyte); |
| 566 |
} |
| 567 |
|
| 568 |
// Check for existing. |
| 569 |
// NOTE: Well-known are all ASCII so have been handled above. |
| 570 |
InflatedChar16Sequence<mozilla::Utf8Unit> seq(utf8Ptr, nbyte); |
| 571 |
SpecificParserAtomLookup<mozilla::Utf8Unit> lookup(seq); |
| 572 |
MOZ_ASSERT(!WellKnownParserAtoms::getSingleton().lookupChar16Seq(lookup)); |
| 573 |
EntryMap::AddPtr addPtr = entryMap_.lookupForAdd(lookup); |
| 574 |
if (addPtr) { |
| 575 |
return addPtr->value(); |
| 576 |
} |
| 577 |
|
| 578 |
// Compute length in code-points. |
| 579 |
uint32_t length = 0; |
| 580 |
InflatedChar16Sequence<mozilla::Utf8Unit> seqCopy = seq; |
| 581 |
while (seqCopy.hasMore()) { |
| 582 |
(void)seqCopy.next(); |
| 583 |
length += 1; |
| 584 |
} |
| 585 |
|
| 586 |
// Otherwise, add new entry. |
| 587 |
bool wide = (minEncoding == JS::SmallestEncoding::UTF16); |
| 588 |
return wide |
| 589 |
? internChar16Seq<char16_t>(fc, addPtr, lookup.hash(), seq, length) |
| 590 |
: internChar16Seq<Latin1Char>(fc, addPtr, lookup.hash(), seq, |
| 591 |
length); |
| 592 |
} |
| 593 |
|
| 594 |
TaggedParserAtomIndex ParserAtomsTable::internChar16(FrontendContext* fc, |
| 595 |
const char16_t* char16Ptr, |
| 596 |
uint32_t length) { |
| 597 |
// Check for tiny strings which are abundant in minified code. |
| 598 |
if (auto tiny = WellKnownParserAtoms::getSingleton().lookupTinyIndex( |
| 599 |
char16Ptr, length)) { |
| 600 |
return tiny; |
| 601 |
} |
| 602 |
|
| 603 |
// Check against well-known. |
| 604 |
InflatedChar16Sequence<char16_t> seq(char16Ptr, length); |
| 605 |
SpecificParserAtomLookup<char16_t> lookup(seq); |
| 606 |
if (auto wk = WellKnownParserAtoms::getSingleton().lookupChar16Seq(lookup)) { |
| 607 |
return wk; |
| 608 |
} |
| 609 |
|
| 610 |
// Check for existing atom. |
| 611 |
EntryMap::AddPtr addPtr = entryMap_.lookupForAdd(lookup); |
| 612 |
if (addPtr) { |
| 613 |
return addPtr->value(); |
| 614 |
} |
| 615 |
|
| 616 |
// Otherwise, add new entry. |
| 617 |
return IsWide(seq) |
| 618 |
? internChar16Seq<char16_t>(fc, addPtr, lookup.hash(), seq, length) |
| 619 |
: internChar16Seq<Latin1Char>(fc, addPtr, lookup.hash(), seq, |
| 620 |
length); |
| 621 |
} |
| 622 |
|
| 623 |
TaggedParserAtomIndex ParserAtomsTable::internJSAtom( |
| 624 |
FrontendContext* fc, CompilationAtomCache& atomCache, JSAtom* atom) { |
| 625 |
TaggedParserAtomIndex parserAtom; |
| 626 |
{ |
| 627 |
JS::AutoCheckCannotGC nogc; |
| 628 |
|
| 629 |
parserAtom = |
| 630 |
atom->hasLatin1Chars() |
| 631 |
? internLatin1(fc, atom->latin1Chars(nogc), atom->length()) |
| 632 |
: internChar16(fc, atom->twoByteChars(nogc), atom->length()); |
| 633 |
if (!parserAtom) { |
| 634 |
return TaggedParserAtomIndex::null(); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
if (parserAtom.isParserAtomIndex()) { |
| 639 |
ParserAtomIndex index = parserAtom.toParserAtomIndex(); |
| 640 |
if (!atomCache.hasAtomAt(index)) { |
| 641 |
if (!atomCache.setAtomAt(fc, index, atom)) { |
| 642 |
return TaggedParserAtomIndex::null(); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
// We should (infallibly) map back to the same JSAtom. |
| 648 |
#ifdef DEBUG |
| 649 |
if (JSContext* cx = fc->maybeCurrentJSContext()) { |
| 650 |
JS::AutoSuppressGCAnalysis suppress(cx); |
| 651 |
MOZ_ASSERT(toJSAtom(cx, fc, parserAtom, atomCache) == atom); |
| 652 |
} |
| 653 |
#endif |
| 654 |
|
| 655 |
return parserAtom; |
| 656 |
} |
| 657 |
|
| 658 |
ParserAtom* ParserAtomsTable::getParserAtom(ParserAtomIndex index) const { |
| 659 |
return entries_[index]; |
| 660 |
} |
| 661 |
|
| 662 |
void ParserAtomsTable::markUsedByStencil(TaggedParserAtomIndex index, |
| 663 |
ParserAtom::Atomize atomize) const { |
| 664 |
if (!index.isParserAtomIndex()) { |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
getParserAtom(index.toParserAtomIndex())->markUsedByStencil(atomize); |
| 669 |
} |
| 670 |
|
| 671 |
void ParserAtomsTable::markAtomize(TaggedParserAtomIndex index, |
| 672 |
ParserAtom::Atomize atomize) const { |
| 673 |
if (!index.isParserAtomIndex()) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
getParserAtom(index.toParserAtomIndex())->markAtomize(atomize); |
| 678 |
} |
| 679 |
|
| 680 |
bool ParserAtomsTable::isIdentifier(TaggedParserAtomIndex index) const { |
| 681 |
if (index.isParserAtomIndex()) { |
| 682 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 683 |
return atom->hasLatin1Chars() |
| 684 |
? IsIdentifier(atom->latin1Chars(), atom->length()) |
| 685 |
: IsIdentifier(atom->twoByteChars(), atom->length()); |
| 686 |
} |
| 687 |
|
| 688 |
if (index.isWellKnownAtomId()) { |
| 689 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 690 |
return IsIdentifier(reinterpret_cast<const Latin1Char*>(info.content), |
| 691 |
info.length); |
| 692 |
} |
| 693 |
|
| 694 |
if (index.isLength1StaticParserString()) { |
| 695 |
Latin1Char content[1]; |
| 696 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 697 |
if (MOZ_UNLIKELY(content[0] > 127)) { |
| 698 |
return IsIdentifier(content, 1); |
| 699 |
} |
| 700 |
return IsIdentifierASCII(char(content[0])); |
| 701 |
} |
| 702 |
|
| 703 |
if (index.isLength2StaticParserString()) { |
| 704 |
char content[2]; |
| 705 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 706 |
return IsIdentifierASCII(content[0], content[1]); |
| 707 |
} |
| 708 |
|
| 709 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 710 |
#ifdef DEBUG |
| 711 |
char content[3]; |
| 712 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 713 |
MOZ_ASSERT(!reinterpret_cast<const Latin1Char*>( |
| 714 |
IsIdentifier(reinterpret_cast<const Latin1Char*>(content), 3))); |
| 715 |
#endif |
| 716 |
return false; |
| 717 |
} |
| 718 |
|
| 719 |
bool ParserAtomsTable::isPrivateName(TaggedParserAtomIndex index) const { |
| 720 |
if (!index.isParserAtomIndex()) { |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 725 |
return atom->isPrivateName(); |
| 726 |
} |
| 727 |
|
| 728 |
bool ParserAtomsTable::isExtendedUnclonedSelfHostedFunctionName( |
| 729 |
TaggedParserAtomIndex index) const { |
| 730 |
if (index.isParserAtomIndex()) { |
| 731 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 732 |
if (atom->length() < 2) { |
| 733 |
return false; |
| 734 |
} |
| 735 |
|
| 736 |
return atom->charAt(0) == ExtendedUnclonedSelfHostedFunctionNamePrefix; |
| 737 |
} |
| 738 |
|
| 739 |
if (index.isWellKnownAtomId()) { |
| 740 |
switch (index.toWellKnownAtomId()) { |
| 741 |
case WellKnownAtomId::dollar_ArrayBufferSpecies_: |
| 742 |
case WellKnownAtomId::dollar_ArraySpecies_: |
| 743 |
case WellKnownAtomId::dollar_ArrayValues_: |
| 744 |
case WellKnownAtomId::dollar_RegExpFlagsGetter_: |
| 745 |
case WellKnownAtomId::dollar_RegExpToString_: |
| 746 |
case WellKnownAtomId::dollar_SharedArrayBufferSpecies_: { |
| 747 |
#ifdef DEBUG |
| 748 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 749 |
MOZ_ASSERT(info.content[0] == |
| 750 |
ExtendedUnclonedSelfHostedFunctionNamePrefix); |
| 751 |
#endif |
| 752 |
return true; |
| 753 |
} |
| 754 |
default: { |
| 755 |
#ifdef DEBUG |
| 756 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 757 |
MOZ_ASSERT(info.length == 0 || |
| 758 |
info.content[0] != |
| 759 |
ExtendedUnclonedSelfHostedFunctionNamePrefix); |
| 760 |
#endif |
| 761 |
break; |
| 762 |
} |
| 763 |
} |
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
// Length-1/2/3 shouldn't be used for extented uncloned self-hosted |
| 768 |
// function name, and this query shouldn't be used for them. |
| 769 |
#ifdef DEBUG |
| 770 |
if (index.isLength1StaticParserString()) { |
| 771 |
Latin1Char content[1]; |
| 772 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 773 |
MOZ_ASSERT(content[0] != ExtendedUnclonedSelfHostedFunctionNamePrefix); |
| 774 |
} else if (index.isLength2StaticParserString()) { |
| 775 |
char content[2]; |
| 776 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 777 |
MOZ_ASSERT(content[0] != ExtendedUnclonedSelfHostedFunctionNamePrefix); |
| 778 |
} else { |
| 779 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 780 |
char content[3]; |
| 781 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 782 |
MOZ_ASSERT(content[0] != ExtendedUnclonedSelfHostedFunctionNamePrefix); |
| 783 |
} |
| 784 |
#endif |
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
static bool HasUnpairedSurrogate(mozilla::Range<const char16_t> chars) { |
| 789 |
for (auto ptr = chars.begin(); ptr < chars.end();) { |
| 790 |
char16_t ch = *ptr++; |
| 791 |
if (unicode::IsLeadSurrogate(ch)) { |
| 792 |
if (ptr == chars.end() || !unicode::IsTrailSurrogate(*ptr++)) { |
| 793 |
return true; |
| 794 |
} |
| 795 |
} else if (unicode::IsTrailSurrogate(ch)) { |
| 796 |
return true; |
| 797 |
} |
| 798 |
} |
| 799 |
return false; |
| 800 |
} |
| 801 |
|
| 802 |
bool ParserAtomsTable::isModuleExportName(TaggedParserAtomIndex index) const { |
| 803 |
if (index.isParserAtomIndex()) { |
| 804 |
const ParserAtom* name = getParserAtom(index.toParserAtomIndex()); |
| 805 |
return name->hasLatin1Chars() || |
| 806 |
!HasUnpairedSurrogate(name->twoByteRange()); |
| 807 |
} |
| 808 |
|
| 809 |
// Well-known/length-2 are ASCII. |
| 810 |
// length-1 are Latin1. |
| 811 |
return true; |
| 812 |
} |
| 813 |
|
| 814 |
bool ParserAtomsTable::isIndex(TaggedParserAtomIndex index, |
| 815 |
uint32_t* indexp) const { |
| 816 |
if (index.isParserAtomIndex()) { |
| 817 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 818 |
size_t len = atom->length(); |
| 819 |
if (len == 0 || len > UINT32_CHAR_BUFFER_LENGTH) { |
| 820 |
return false; |
| 821 |
} |
| 822 |
if (atom->hasLatin1Chars()) { |
| 823 |
return mozilla::IsAsciiDigit(*atom->latin1Chars()) && |
| 824 |
js::CheckStringIsIndex(atom->latin1Chars(), len, indexp); |
| 825 |
} |
| 826 |
return mozilla::IsAsciiDigit(*atom->twoByteChars()) && |
| 827 |
js::CheckStringIsIndex(atom->twoByteChars(), len, indexp); |
| 828 |
} |
| 829 |
|
| 830 |
if (index.isWellKnownAtomId()) { |
| 831 |
#ifdef DEBUG |
| 832 |
// Well-known atom shouldn't start with digit. |
| 833 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 834 |
MOZ_ASSERT(info.length == 0 || !mozilla::IsAsciiDigit(info.content[0])); |
| 835 |
#endif |
| 836 |
return false; |
| 837 |
} |
| 838 |
|
| 839 |
if (index.isLength1StaticParserString()) { |
| 840 |
Latin1Char content[1]; |
| 841 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 842 |
if (mozilla::IsAsciiDigit(content[0])) { |
| 843 |
*indexp = AsciiDigitToNumber(content[0]); |
| 844 |
return true; |
| 845 |
} |
| 846 |
return false; |
| 847 |
} |
| 848 |
|
| 849 |
if (index.isLength2StaticParserString()) { |
| 850 |
char content[2]; |
| 851 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 852 |
// Leading '0' isn't allowed. |
| 853 |
// See CheckStringIsIndex comment. |
| 854 |
if (content[0] != '0' && mozilla::IsAsciiDigit(content[0]) && |
| 855 |
mozilla::IsAsciiDigit(content[1])) { |
| 856 |
*indexp = |
| 857 |
AsciiDigitToNumber(content[0]) * 10 + AsciiDigitToNumber(content[1]); |
| 858 |
return true; |
| 859 |
} |
| 860 |
return false; |
| 861 |
} |
| 862 |
|
| 863 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 864 |
*indexp = uint32_t(index.toLength3StaticParserString()); |
| 865 |
#ifdef DEBUG |
| 866 |
char content[3]; |
| 867 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 868 |
MOZ_ASSERT(uint32_t(AsciiDigitToNumber(content[0])) * 100 + |
| 869 |
uint32_t(AsciiDigitToNumber(content[1])) * 10 + |
| 870 |
uint32_t(AsciiDigitToNumber(content[2])) == |
| 871 |
*indexp); |
| 872 |
MOZ_ASSERT(100 <= *indexp); |
| 873 |
#endif |
| 874 |
return true; |
| 875 |
} |
| 876 |
|
| 877 |
bool ParserAtomsTable::isInstantiatedAsJSAtom( |
| 878 |
TaggedParserAtomIndex index) const { |
| 879 |
if (index.isParserAtomIndex()) { |
| 880 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 881 |
return atom->isInstantiatedAsJSAtom(); |
| 882 |
} |
| 883 |
|
| 884 |
// Everything else are always JSAtom. |
| 885 |
return true; |
| 886 |
} |
| 887 |
|
| 888 |
uint32_t ParserAtomsTable::length(TaggedParserAtomIndex index) const { |
| 889 |
if (index.isParserAtomIndex()) { |
| 890 |
return getParserAtom(index.toParserAtomIndex())->length(); |
| 891 |
} |
| 892 |
|
| 893 |
if (index.isWellKnownAtomId()) { |
| 894 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 895 |
return info.length; |
| 896 |
} |
| 897 |
|
| 898 |
if (index.isLength1StaticParserString()) { |
| 899 |
return 1; |
| 900 |
} |
| 901 |
|
| 902 |
if (index.isLength2StaticParserString()) { |
| 903 |
return 2; |
| 904 |
} |
| 905 |
|
| 906 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 907 |
return 3; |
| 908 |
} |
| 909 |
|
| 910 |
HashNumber ParserAtomsTable::hash(TaggedParserAtomIndex index) const { |
| 911 |
if (index.isParserAtomIndex()) { |
| 912 |
return getParserAtom(index.toParserAtomIndex())->hash(); |
| 913 |
} |
| 914 |
|
| 915 |
return index.staticOrWellKnownHash(); |
| 916 |
} |
| 917 |
|
| 918 |
double ParserAtomsTable::toNumber(TaggedParserAtomIndex index) const { |
| 919 |
if (index.isParserAtomIndex()) { |
| 920 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 921 |
size_t len = atom->length(); |
| 922 |
return atom->hasLatin1Chars() ? CharsToNumber(atom->latin1Chars(), len) |
| 923 |
: CharsToNumber(atom->twoByteChars(), len); |
| 924 |
} |
| 925 |
|
| 926 |
if (index.isWellKnownAtomId()) { |
| 927 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 928 |
return CharsToNumber(reinterpret_cast<const Latin1Char*>(info.content), |
| 929 |
info.length); |
| 930 |
} |
| 931 |
|
| 932 |
if (index.isLength1StaticParserString()) { |
| 933 |
Latin1Char content[1]; |
| 934 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 935 |
return CharsToNumber(content, 1); |
| 936 |
} |
| 937 |
|
| 938 |
if (index.isLength2StaticParserString()) { |
| 939 |
char content[2]; |
| 940 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 941 |
return CharsToNumber(reinterpret_cast<const Latin1Char*>(content), 2); |
| 942 |
} |
| 943 |
|
| 944 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 945 |
double result = double(index.toLength3StaticParserString()); |
| 946 |
#ifdef DEBUG |
| 947 |
char content[3]; |
| 948 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 949 |
double tmp = CharsToNumber(reinterpret_cast<const Latin1Char*>(content), 3); |
| 950 |
MOZ_ASSERT(tmp == result); |
| 951 |
#endif |
| 952 |
return result; |
| 953 |
} |
| 954 |
|
| 955 |
UniqueChars ParserAtomsTable::toNewUTF8CharsZ( |
| 956 |
FrontendContext* fc, TaggedParserAtomIndex index) const { |
| 957 |
auto* alloc = fc->getAllocator(); |
| 958 |
|
| 959 |
if (index.isParserAtomIndex()) { |
| 960 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 961 |
return UniqueChars( |
| 962 |
atom->hasLatin1Chars() |
| 963 |
? JS::CharsToNewUTF8CharsZ(alloc, atom->latin1Range()).c_str() |
| 964 |
: JS::CharsToNewUTF8CharsZ(alloc, atom->twoByteRange()).c_str()); |
| 965 |
} |
| 966 |
|
| 967 |
if (index.isWellKnownAtomId()) { |
| 968 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 969 |
return UniqueChars( |
| 970 |
JS::CharsToNewUTF8CharsZ( |
| 971 |
alloc, |
| 972 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(info.content), |
| 973 |
info.length)) |
| 974 |
.c_str()); |
| 975 |
} |
| 976 |
|
| 977 |
if (index.isLength1StaticParserString()) { |
| 978 |
Latin1Char content[1]; |
| 979 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 980 |
return UniqueChars( |
| 981 |
JS::CharsToNewUTF8CharsZ(alloc, mozilla::Range(content, 1)).c_str()); |
| 982 |
} |
| 983 |
|
| 984 |
if (index.isLength2StaticParserString()) { |
| 985 |
char content[2]; |
| 986 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 987 |
return UniqueChars( |
| 988 |
JS::CharsToNewUTF8CharsZ( |
| 989 |
alloc, |
| 990 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 2)) |
| 991 |
.c_str()); |
| 992 |
} |
| 993 |
|
| 994 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 995 |
char content[3]; |
| 996 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 997 |
return UniqueChars( |
| 998 |
JS::CharsToNewUTF8CharsZ( |
| 999 |
alloc, |
| 1000 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 3)) |
| 1001 |
.c_str()); |
| 1002 |
} |
| 1003 |
|
| 1004 |
template <typename CharT> |
| 1005 |
UniqueChars ToPrintableStringImpl(mozilla::Range<CharT> str, |
| 1006 |
char quote = '\0') { |
| 1007 |
// Pass nullptr as JSContext, given we don't use JSString. |
| 1008 |
// OOM should be handled by caller. |
| 1009 |
Sprinter sprinter(nullptr); |
| 1010 |
if (!sprinter.init()) { |
| 1011 |
return nullptr; |
| 1012 |
} |
| 1013 |
QuoteString<QuoteTarget::String>(&sprinter, str, quote); |
| 1014 |
return sprinter.release(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
UniqueChars ParserAtomsTable::toPrintableString( |
| 1018 |
TaggedParserAtomIndex index) const { |
| 1019 |
if (index.isParserAtomIndex()) { |
| 1020 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 1021 |
return atom->hasLatin1Chars() ? ToPrintableStringImpl(atom->latin1Range()) |
| 1022 |
: ToPrintableStringImpl(atom->twoByteRange()); |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (index.isWellKnownAtomId()) { |
| 1026 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 1027 |
return ToPrintableStringImpl(mozilla::Range( |
| 1028 |
reinterpret_cast<const Latin1Char*>(info.content), info.length)); |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (index.isLength1StaticParserString()) { |
| 1032 |
Latin1Char content[1]; |
| 1033 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 1034 |
return ToPrintableStringImpl(mozilla::Range<const Latin1Char>(content, 1)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (index.isLength2StaticParserString()) { |
| 1038 |
char content[2]; |
| 1039 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 1040 |
return ToPrintableStringImpl( |
| 1041 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 2)); |
| 1042 |
} |
| 1043 |
|
| 1044 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 1045 |
char content[3]; |
| 1046 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 1047 |
return ToPrintableStringImpl( |
| 1048 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 3)); |
| 1049 |
} |
| 1050 |
|
| 1051 |
UniqueChars ParserAtomsTable::toQuotedString( |
| 1052 |
TaggedParserAtomIndex index) const { |
| 1053 |
if (index.isParserAtomIndex()) { |
| 1054 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 1055 |
return atom->hasLatin1Chars() |
| 1056 |
? ToPrintableStringImpl(atom->latin1Range(), '\"') |
| 1057 |
: ToPrintableStringImpl(atom->twoByteRange(), '\"'); |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (index.isWellKnownAtomId()) { |
| 1061 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 1062 |
return ToPrintableStringImpl( |
| 1063 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(info.content), |
| 1064 |
info.length), |
| 1065 |
'\"'); |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (index.isLength1StaticParserString()) { |
| 1069 |
Latin1Char content[1]; |
| 1070 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 1071 |
return ToPrintableStringImpl(mozilla::Range<const Latin1Char>(content, 1), |
| 1072 |
'\"'); |
| 1073 |
} |
| 1074 |
|
| 1075 |
if (index.isLength2StaticParserString()) { |
| 1076 |
char content[2]; |
| 1077 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 1078 |
return ToPrintableStringImpl( |
| 1079 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 2), '\"'); |
| 1080 |
} |
| 1081 |
|
| 1082 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 1083 |
char content[3]; |
| 1084 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 1085 |
return ToPrintableStringImpl( |
| 1086 |
mozilla::Range(reinterpret_cast<const Latin1Char*>(content), 3), '\"'); |
| 1087 |
} |
| 1088 |
|
| 1089 |
JSAtom* ParserAtomsTable::toJSAtom(JSContext* cx, FrontendContext* fc, |
| 1090 |
TaggedParserAtomIndex index, |
| 1091 |
CompilationAtomCache& atomCache) const { |
| 1092 |
// This function can be called before we instantiate atoms based on |
| 1093 |
// AtomizeFlag. |
| 1094 |
|
| 1095 |
if (index.isParserAtomIndex()) { |
| 1096 |
auto atomIndex = index.toParserAtomIndex(); |
| 1097 |
|
| 1098 |
// If we already instantiated this parser atom, it should always be JSAtom. |
| 1099 |
// `asAtom()` called in getAtomAt asserts that. |
| 1100 |
JSAtom* atom = atomCache.getAtomAt(atomIndex); |
| 1101 |
if (atom) { |
| 1102 |
return atom; |
| 1103 |
} |
| 1104 |
|
| 1105 |
// For consistency, mark atomize. |
| 1106 |
ParserAtom* parserAtom = getParserAtom(atomIndex); |
| 1107 |
parserAtom->markAtomize(ParserAtom::Atomize::Yes); |
| 1108 |
return parserAtom->instantiateAtom(cx, fc, atomIndex, atomCache); |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (index.isWellKnownAtomId()) { |
| 1112 |
return GetWellKnownAtom(cx, index.toWellKnownAtomId()); |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (index.isLength1StaticParserString()) { |
| 1116 |
char16_t ch = static_cast<char16_t>(index.toLength1StaticParserString()); |
| 1117 |
return cx->staticStrings().getUnit(ch); |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (index.isLength2StaticParserString()) { |
| 1121 |
size_t s = static_cast<size_t>(index.toLength2StaticParserString()); |
| 1122 |
return cx->staticStrings().getLength2FromIndex(s); |
| 1123 |
} |
| 1124 |
|
| 1125 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 1126 |
uint32_t s = uint32_t(index.toLength3StaticParserString()); |
| 1127 |
return cx->staticStrings().getUint(s); |
| 1128 |
} |
| 1129 |
|
| 1130 |
bool ParserAtomsTable::appendTo(StringBuilder& sb, |
| 1131 |
TaggedParserAtomIndex index) const { |
| 1132 |
if (index.isParserAtomIndex()) { |
| 1133 |
const auto* atom = getParserAtom(index.toParserAtomIndex()); |
| 1134 |
size_t length = atom->length(); |
| 1135 |
return atom->hasLatin1Chars() ? sb.append(atom->latin1Chars(), length) |
| 1136 |
: sb.append(atom->twoByteChars(), length); |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (index.isWellKnownAtomId()) { |
| 1140 |
const auto& info = GetWellKnownAtomInfo(index.toWellKnownAtomId()); |
| 1141 |
return sb.append(info.content, info.length); |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (index.isLength1StaticParserString()) { |
| 1145 |
Latin1Char content[1]; |
| 1146 |
getLength1Content(index.toLength1StaticParserString(), content); |
| 1147 |
return sb.append(content[0]); |
| 1148 |
} |
| 1149 |
|
| 1150 |
if (index.isLength2StaticParserString()) { |
| 1151 |
char content[2]; |
| 1152 |
getLength2Content(index.toLength2StaticParserString(), content); |
| 1153 |
return sb.append(content, 2); |
| 1154 |
} |
| 1155 |
|
| 1156 |
MOZ_ASSERT(index.isLength3StaticParserString()); |
| 1157 |
char content[3]; |
| 1158 |
getLength3Content(index.toLength3StaticParserString(), content); |
| 1159 |
return sb.append(content, 3); |
| 1160 |
} |
| 1161 |
|
| 1162 |
bool InstantiateMarkedAtoms(JSContext* cx, FrontendContext* fc, |
| 1163 |
const ParserAtomSpan& entries, |
| 1164 |
CompilationAtomCache& atomCache) { |
| 1165 |
MOZ_ASSERT(cx->zone()); |
| 1166 |
|
| 1167 |
for (size_t i = 0; i < entries.size(); i++) { |
| 1168 |
const auto& entry = entries[i]; |
| 1169 |
if (!entry) { |
| 1170 |
continue; |
| 1171 |
} |
| 1172 |
if (!entry->isUsedByStencil()) { |
| 1173 |
continue; |
| 1174 |
} |
| 1175 |
|
| 1176 |
auto index = ParserAtomIndex(i); |
| 1177 |
if (atomCache.hasAtomAt(index)) { |
| 1178 |
continue; |
| 1179 |
} |
| 1180 |
|
| 1181 |
if (!entry->isInstantiatedAsJSAtom()) { |
| 1182 |
if (!entry->instantiateString(cx, fc, index, atomCache)) { |
| 1183 |
return false; |
| 1184 |
} |
| 1185 |
} else { |
| 1186 |
if (!entry->instantiateAtom(cx, fc, index, atomCache)) { |
| 1187 |
return false; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
} |
| 1191 |
return true; |
| 1192 |
} |
| 1193 |
|
| 1194 |
bool InstantiateMarkedAtomsAsPermanent(JSContext* cx, FrontendContext* fc, |
| 1195 |
AtomSet& atomSet, |
| 1196 |
const ParserAtomSpan& entries, |
| 1197 |
CompilationAtomCache& atomCache) { |
| 1198 |
MOZ_ASSERT(!cx->zone()); |
| 1199 |
|
| 1200 |
for (size_t i = 0; i < entries.size(); i++) { |
| 1201 |
const auto& entry = entries[i]; |
| 1202 |
if (!entry) { |
| 1203 |
continue; |
| 1204 |
} |
| 1205 |
if (!entry->isUsedByStencil()) { |
| 1206 |
continue; |
| 1207 |
} |
| 1208 |
|
| 1209 |
auto index = ParserAtomIndex(i); |
| 1210 |
if (atomCache.hasAtomAt(index)) { |
| 1211 |
MOZ_ASSERT(atomCache.getAtomAt(index)->isPermanentAtom()); |
| 1212 |
continue; |
| 1213 |
} |
| 1214 |
|
| 1215 |
if (!entry->instantiatePermanentAtom(cx, fc, atomSet, index, atomCache)) { |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
return true; |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* static */ |
| 1223 |
MOZ_RUNINIT WellKnownParserAtoms WellKnownParserAtoms::singleton_; |
| 1224 |
|
| 1225 |
template <typename CharT> |
| 1226 |
TaggedParserAtomIndex WellKnownParserAtoms::lookupChar16Seq( |
| 1227 |
const SpecificParserAtomLookup<CharT>& lookup) const { |
| 1228 |
EntryMap::Ptr ptr = wellKnownMap_.readonlyThreadsafeLookup(lookup); |
| 1229 |
if (ptr) { |
| 1230 |
return ptr->value(); |
| 1231 |
} |
| 1232 |
return TaggedParserAtomIndex::null(); |
| 1233 |
} |
| 1234 |
|
| 1235 |
TaggedParserAtomIndex WellKnownParserAtoms::lookupTinyIndexUTF8( |
| 1236 |
const mozilla::Utf8Unit* utf8Ptr, size_t nbyte) const { |
| 1237 |
// Check for tiny strings which are abundant in minified code. |
| 1238 |
if (nbyte == 2 && IsLatin1(utf8Ptr[0], utf8Ptr[1])) { |
| 1239 |
// Special case the length-1 non-ASCII range. |
| 1240 |
InflatedChar16Sequence<mozilla::Utf8Unit> seq(utf8Ptr, 2); |
| 1241 |
char16_t u = seq.next(); |
| 1242 |
const Latin1Char c = u; |
| 1243 |
MOZ_ASSERT(!seq.hasMore()); |
| 1244 |
auto tiny = lookupTinyIndex(&c, 1); |
| 1245 |
MOZ_ASSERT(tiny); |
| 1246 |
return tiny; |
| 1247 |
} |
| 1248 |
|
| 1249 |
// NOTE: Other than length-1 non-ASCII range, the tiny atoms are all |
| 1250 |
// ASCII-only so we can directly look at the UTF-8 data without |
| 1251 |
// worrying about surrogates. |
| 1252 |
return lookupTinyIndex(reinterpret_cast<const Latin1Char*>(utf8Ptr), nbyte); |
| 1253 |
} |
| 1254 |
|
| 1255 |
bool WellKnownParserAtoms::initSingle(const WellKnownAtomInfo& info, |
| 1256 |
TaggedParserAtomIndex index) { |
| 1257 |
unsigned int len = info.length; |
| 1258 |
const Latin1Char* str = reinterpret_cast<const Latin1Char*>(info.content); |
| 1259 |
|
| 1260 |
// Well-known atoms are all currently ASCII with length <= MaxWellKnownLength. |
| 1261 |
MOZ_ASSERT(len <= MaxWellKnownLength); |
| 1262 |
MOZ_ASSERT(mozilla::IsAscii(mozilla::Span(info.content, len))); |
| 1263 |
|
| 1264 |
// Strings matched by lookupTinyIndex are stored in static table and aliases |
| 1265 |
// should be initialized directly in WellKnownParserAtoms::init. |
| 1266 |
MOZ_ASSERT(lookupTinyIndex(str, len) == TaggedParserAtomIndex::null(), |
| 1267 |
"Well-known atom matches a tiny StaticString. Did you add it to " |
| 1268 |
"the wrong CommonPropertyNames.h list?"); |
| 1269 |
|
| 1270 |
InflatedChar16Sequence<Latin1Char> seq(str, len); |
| 1271 |
SpecificParserAtomLookup<Latin1Char> lookup(seq, info.hash); |
| 1272 |
|
| 1273 |
// Save name for returning after moving entry into set. |
| 1274 |
if (!wellKnownMap_.putNew(lookup, &info, index)) { |
| 1275 |
return false; |
| 1276 |
} |
| 1277 |
|
| 1278 |
return true; |
| 1279 |
} |
| 1280 |
|
| 1281 |
bool WellKnownParserAtoms::init() { |
| 1282 |
MOZ_ASSERT(wellKnownMap_.empty()); |
| 1283 |
|
| 1284 |
// Add well-known strings to the HashMap. The HashMap is used for dynamic |
| 1285 |
// lookups later and does not change once this init method is complete. |
| 1286 |
#define COMMON_NAME_INIT_(NAME, _) \ |
| 1287 |
if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ |
| 1288 |
TaggedParserAtomIndex::WellKnown::NAME())) { \ |
| 1289 |
return false; \ |
| 1290 |
} |
| 1291 |
FOR_EACH_NONTINY_COMMON_PROPERTYNAME(COMMON_NAME_INIT_) |
| 1292 |
#undef COMMON_NAME_INIT_ |
| 1293 |
#define COMMON_NAME_INIT_(NAME, _) \ |
| 1294 |
if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ |
| 1295 |
TaggedParserAtomIndex::WellKnown::NAME())) { \ |
| 1296 |
return false; \ |
| 1297 |
} |
| 1298 |
JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INIT_) |
| 1299 |
#undef COMMON_NAME_INIT_ |
| 1300 |
#define COMMON_NAME_INIT_(NAME) \ |
| 1301 |
if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ |
| 1302 |
TaggedParserAtomIndex::WellKnown::NAME())) { \ |
| 1303 |
return false; \ |
| 1304 |
} |
| 1305 |
JS_FOR_EACH_WELL_KNOWN_SYMBOL(COMMON_NAME_INIT_) |
| 1306 |
#undef COMMON_NAME_INIT_ |
| 1307 |
|
| 1308 |
return true; |
| 1309 |
} |
| 1310 |
|
| 1311 |
void WellKnownParserAtoms::free() { wellKnownMap_.clear(); } |
| 1312 |
|
| 1313 |
/* static */ bool WellKnownParserAtoms::initSingleton() { |
| 1314 |
return singleton_.init(); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/* static */ void WellKnownParserAtoms::freeSingleton() { singleton_.free(); } |
| 1318 |
|
| 1319 |
} /* namespace frontend */ |
| 1320 |
} /* namespace js */ |