| 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 |
/* JS script descriptor. */ |
| 8 |
|
| 9 |
#ifndef vm_JSScript_h |
| 10 |
#define vm_JSScript_h |
| 11 |
|
| 12 |
#include "mozilla/Atomics.h" |
| 13 |
#include "mozilla/Maybe.h" |
| 14 |
#include "mozilla/MaybeOneOf.h" |
| 15 |
#include "mozilla/MemoryReporting.h" |
| 16 |
#include "mozilla/RefPtr.h" |
| 17 |
#include "mozilla/Span.h" |
| 18 |
|
| 19 |
#include "mozilla/UniquePtr.h" |
| 20 |
#include "mozilla/Utf8.h" |
| 21 |
#include "mozilla/Variant.h" |
| 22 |
|
| 23 |
#include <type_traits> // std::is_same |
| 24 |
#include <utility> // std::move |
| 25 |
|
| 26 |
#include "jstypes.h" |
| 27 |
|
| 28 |
#include "frontend/ScriptIndex.h" // ScriptIndex |
| 29 |
#include "gc/Barrier.h" |
| 30 |
#include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin, JS::LimitedColumnNumberOneOrigin |
| 31 |
#include "js/CompileOptions.h" |
| 32 |
#include "js/Transcoding.h" |
| 33 |
#include "js/UbiNode.h" |
| 34 |
#include "js/UniquePtr.h" |
| 35 |
#include "js/Utility.h" |
| 36 |
#include "util/TrailingArray.h" |
| 37 |
#include "vm/BytecodeIterator.h" |
| 38 |
#include "vm/BytecodeLocation.h" |
| 39 |
#include "vm/BytecodeUtil.h" |
| 40 |
#include "vm/MutexIDs.h" // mutexid |
| 41 |
#include "vm/NativeObject.h" |
| 42 |
#include "vm/SharedImmutableStringsCache.h" |
| 43 |
#include "vm/SharedStencil.h" // js::GCThingIndex, js::SourceExtent, js::SharedImmutableScriptData, MemberInitializers |
| 44 |
#include "vm/StencilEnums.h" // SourceRetrievable |
| 45 |
|
| 46 |
namespace JS { |
| 47 |
struct ScriptSourceInfo; |
| 48 |
template <typename UnitT> |
| 49 |
class SourceText; |
| 50 |
} // namespace JS |
| 51 |
|
| 52 |
namespace js { |
| 53 |
|
| 54 |
class FrontendContext; |
| 55 |
class ScriptSource; |
| 56 |
|
| 57 |
class VarScope; |
| 58 |
class LexicalScope; |
| 59 |
|
| 60 |
class JS_PUBLIC_API Sprinter; |
| 61 |
|
| 62 |
namespace coverage { |
| 63 |
class LCovSource; |
| 64 |
} // namespace coverage |
| 65 |
|
| 66 |
namespace gc { |
| 67 |
class AllocSite; |
| 68 |
} // namespace gc |
| 69 |
|
| 70 |
namespace jit { |
| 71 |
class AutoKeepJitScripts; |
| 72 |
class BaselineScript; |
| 73 |
class IonScript; |
| 74 |
struct IonScriptCounts; |
| 75 |
class JitScript; |
| 76 |
} // namespace jit |
| 77 |
|
| 78 |
class ModuleObject; |
| 79 |
class RegExpObject; |
| 80 |
class SourceCompressionTask; |
| 81 |
class Shape; |
| 82 |
class SrcNote; |
| 83 |
class DebugScript; |
| 84 |
|
| 85 |
namespace frontend { |
| 86 |
struct CompilationStencil; |
| 87 |
struct ExtensibleCompilationStencil; |
| 88 |
struct CompilationGCOutput; |
| 89 |
struct InitialStencilAndDelazifications; |
| 90 |
struct CompilationStencilMerger; |
| 91 |
class StencilXDR; |
| 92 |
} // namespace frontend |
| 93 |
|
| 94 |
class ScriptCounts { |
| 95 |
public: |
| 96 |
using PCCountsVector = mozilla::Vector<PCCounts, 0, SystemAllocPolicy>; |
| 97 |
|
| 98 |
inline ScriptCounts(); |
| 99 |
inline explicit ScriptCounts(PCCountsVector&& jumpTargets); |
| 100 |
inline ScriptCounts(ScriptCounts&& src); |
| 101 |
inline ~ScriptCounts(); |
| 102 |
|
| 103 |
inline ScriptCounts& operator=(ScriptCounts&& src); |
| 104 |
|
| 105 |
// Return the counter used to count the number of visits. Returns null if |
| 106 |
// the element is not found. |
| 107 |
PCCounts* maybeGetPCCounts(size_t offset); |
| 108 |
const PCCounts* maybeGetPCCounts(size_t offset) const; |
| 109 |
|
| 110 |
// PCCounts are stored at jump-target offsets. This function looks for the |
| 111 |
// previous PCCount which is in the same basic block as the current offset. |
| 112 |
PCCounts* getImmediatePrecedingPCCounts(size_t offset); |
| 113 |
|
| 114 |
// Return the counter used to count the number of throws. Returns null if |
| 115 |
// the element is not found. |
| 116 |
const PCCounts* maybeGetThrowCounts(size_t offset) const; |
| 117 |
|
| 118 |
// Throw counts are stored at the location of each throwing |
| 119 |
// instruction. This function looks for the previous throw count. |
| 120 |
// |
| 121 |
// Note: if the offset of the returned count is higher than the offset of |
| 122 |
// the immediate preceding PCCount, then this throw happened in the same |
| 123 |
// basic block. |
| 124 |
const PCCounts* getImmediatePrecedingThrowCounts(size_t offset) const; |
| 125 |
|
| 126 |
// Return the counter used to count the number of throws. Allocate it if |
| 127 |
// none exists yet. Returns null if the allocation failed. |
| 128 |
PCCounts* getThrowCounts(size_t offset); |
| 129 |
|
| 130 |
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); |
| 131 |
|
| 132 |
bool traceWeak(JSTracer* trc) { return true; } |
| 133 |
|
| 134 |
private: |
| 135 |
friend class ::JSScript; |
| 136 |
friend struct ScriptAndCounts; |
| 137 |
|
| 138 |
// This sorted array is used to map an offset to the number of times a |
| 139 |
// branch got visited. |
| 140 |
PCCountsVector pcCounts_; |
| 141 |
|
| 142 |
// This sorted vector is used to map an offset to the number of times an |
| 143 |
// instruction throw. |
| 144 |
PCCountsVector throwCounts_; |
| 145 |
|
| 146 |
// Information about any Ion compilations for the script. |
| 147 |
jit::IonScriptCounts* ionCounts_; |
| 148 |
}; |
| 149 |
|
| 150 |
// The key of these side-table hash maps are intentionally not traced GC |
| 151 |
// references to JSScript. Instead, we use bare pointers and manually fix up |
| 152 |
// when objects could have moved (see Zone::fixupScriptMapsAfterMovingGC) and |
| 153 |
// remove when the realm is destroyed (see Zone::clearScriptCounts and |
| 154 |
// Zone::clearScriptNames). They essentially behave as weak references, except |
| 155 |
// that the references are not cleared early by the GC. They must be non-strong |
| 156 |
// references because the tables are kept at the Zone level and otherwise the |
| 157 |
// table keys would keep scripts alive, thus keeping Realms alive, beyond their |
| 158 |
// expected lifetimes. However, We do not use actual weak references (e.g. as |
| 159 |
// used by WeakMap tables provided in gc/WeakMap.h) because they would be |
| 160 |
// collected before the calls to the JSScript::finalize function which are used |
| 161 |
// to aggregate code coverage results on the realm. |
| 162 |
// |
| 163 |
// Note carefully, however, that there is an exceptional case for which we *do* |
| 164 |
// want the JSScripts to be strong references (and thus traced): when the |
| 165 |
// --dump-bytecode command line option or the PCCount JSFriend API is used, |
| 166 |
// then the scripts for all counts must remain alive. See |
| 167 |
// Zone::traceScriptTableRoots() for more details. |
| 168 |
// |
| 169 |
// TODO: Clean this up by either aggregating coverage results in some other |
| 170 |
// way, or by tweaking sweep ordering. |
| 171 |
using UniqueScriptCounts = js::UniquePtr<ScriptCounts>; |
| 172 |
using ScriptCountsMap = |
| 173 |
GCRekeyableHashMap<HeapPtr<BaseScript*>, UniqueScriptCounts, |
| 174 |
DefaultHasher<HeapPtr<BaseScript*>>, SystemAllocPolicy>; |
| 175 |
|
| 176 |
// The 'const char*' for the function name is a pointer within the LCovSource's |
| 177 |
// LifoAlloc and will be discarded at the same time. |
| 178 |
using ScriptLCovEntry = std::tuple<coverage::LCovSource*, const char*>; |
| 179 |
using ScriptLCovMap = |
| 180 |
GCRekeyableHashMap<HeapPtr<BaseScript*>, ScriptLCovEntry, |
| 181 |
DefaultHasher<HeapPtr<BaseScript*>>, SystemAllocPolicy>; |
| 182 |
|
| 183 |
#ifdef MOZ_VTUNE |
| 184 |
using ScriptVTuneIdMap = |
| 185 |
GCRekeyableHashMap<HeapPtr<BaseScript*>, uint32_t, |
| 186 |
DefaultHasher<HeapPtr<BaseScript*>>, SystemAllocPolicy>; |
| 187 |
#endif |
| 188 |
#ifdef JS_CACHEIR_SPEW |
| 189 |
using ScriptFinalWarmUpCountEntry = std::tuple<uint32_t, SharedImmutableString>; |
| 190 |
using ScriptFinalWarmUpCountMap = |
| 191 |
GCRekeyableHashMap<HeapPtr<BaseScript*>, ScriptFinalWarmUpCountEntry, |
| 192 |
DefaultHasher<HeapPtr<BaseScript*>>, SystemAllocPolicy>; |
| 193 |
#endif |
| 194 |
|
| 195 |
struct ScriptSourceChunk { |
| 196 |
ScriptSource* ss = nullptr; |
| 197 |
uint32_t chunk = 0; |
| 198 |
|
| 199 |
ScriptSourceChunk() = default; |
| 200 |
|
| 201 |
ScriptSourceChunk(ScriptSource* ss, uint32_t chunk) : ss(ss), chunk(chunk) { |
| 202 |
MOZ_ASSERT(valid()); |
| 203 |
} |
| 204 |
|
| 205 |
bool valid() const { return ss != nullptr; } |
| 206 |
|
| 207 |
bool operator==(const ScriptSourceChunk& other) const { |
| 208 |
return ss == other.ss && chunk == other.chunk; |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
struct ScriptSourceChunkHasher { |
| 213 |
using Lookup = ScriptSourceChunk; |
| 214 |
|
| 215 |
static HashNumber hash(const ScriptSourceChunk& ssc) { |
| 216 |
return mozilla::AddToHash(DefaultHasher<ScriptSource*>::hash(ssc.ss), |
| 217 |
ssc.chunk); |
| 218 |
} |
| 219 |
static bool match(const ScriptSourceChunk& c1, const ScriptSourceChunk& c2) { |
| 220 |
return c1 == c2; |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
template <typename Unit> |
| 225 |
using EntryUnits = mozilla::UniquePtr<Unit[], JS::FreePolicy>; |
| 226 |
|
| 227 |
// The uncompressed source cache contains *either* UTF-8 source data *or* |
| 228 |
// UTF-16 source data. ScriptSourceChunk implies a ScriptSource that |
| 229 |
// contains either UTF-8 data or UTF-16 data, so the nature of the key to |
| 230 |
// Map below indicates how each SourceData ought to be interpreted. |
| 231 |
using SourceData = mozilla::UniquePtr<void, JS::FreePolicy>; |
| 232 |
|
| 233 |
template <typename Unit> |
| 234 |
inline SourceData ToSourceData(EntryUnits<Unit> chars) { |
| 235 |
static_assert(std::is_same_v<SourceData::DeleterType, |
| 236 |
typename EntryUnits<Unit>::DeleterType>, |
| 237 |
"EntryUnits and SourceData must share the same deleter " |
| 238 |
"type, that need not know the type of the data being freed, " |
| 239 |
"for the upcast below to be safe"); |
| 240 |
return SourceData(chars.release()); |
| 241 |
} |
| 242 |
|
| 243 |
class UncompressedSourceCache { |
| 244 |
using Map = HashMap<ScriptSourceChunk, SourceData, ScriptSourceChunkHasher, |
| 245 |
SystemAllocPolicy>; |
| 246 |
|
| 247 |
public: |
| 248 |
// Hold an entry in the source data cache and prevent it from being purged on |
| 249 |
// GC. |
| 250 |
class AutoHoldEntry { |
| 251 |
UncompressedSourceCache* cache_ = nullptr; |
| 252 |
ScriptSourceChunk sourceChunk_ = {}; |
| 253 |
SourceData data_ = nullptr; |
| 254 |
|
| 255 |
public: |
| 256 |
explicit AutoHoldEntry() = default; |
| 257 |
|
| 258 |
~AutoHoldEntry() { |
| 259 |
if (cache_) { |
| 260 |
MOZ_ASSERT(sourceChunk_.valid()); |
| 261 |
cache_->releaseEntry(*this); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
template <typename Unit> |
| 266 |
void holdUnits(EntryUnits<Unit> units) { |
| 267 |
MOZ_ASSERT(!cache_); |
| 268 |
MOZ_ASSERT(!sourceChunk_.valid()); |
| 269 |
MOZ_ASSERT(!data_); |
| 270 |
|
| 271 |
data_ = ToSourceData(std::move(units)); |
| 272 |
} |
| 273 |
|
| 274 |
private: |
| 275 |
void holdEntry(UncompressedSourceCache* cache, |
| 276 |
const ScriptSourceChunk& sourceChunk) { |
| 277 |
// Initialise the holder for a specific cache and script source. |
| 278 |
// This will hold on to the cached source chars in the event that |
| 279 |
// the cache is purged. |
| 280 |
MOZ_ASSERT(!cache_); |
| 281 |
MOZ_ASSERT(!sourceChunk_.valid()); |
| 282 |
MOZ_ASSERT(!data_); |
| 283 |
|
| 284 |
cache_ = cache; |
| 285 |
sourceChunk_ = sourceChunk; |
| 286 |
} |
| 287 |
|
| 288 |
void deferDelete(SourceData data) { |
| 289 |
// Take ownership of source chars now the cache is being purged. Remove |
| 290 |
// our reference to the ScriptSource which might soon be destroyed. |
| 291 |
MOZ_ASSERT(cache_); |
| 292 |
MOZ_ASSERT(sourceChunk_.valid()); |
| 293 |
MOZ_ASSERT(!data_); |
| 294 |
|
| 295 |
cache_ = nullptr; |
| 296 |
sourceChunk_ = ScriptSourceChunk(); |
| 297 |
|
| 298 |
data_ = std::move(data); |
| 299 |
} |
| 300 |
|
| 301 |
const ScriptSourceChunk& sourceChunk() const { return sourceChunk_; } |
| 302 |
friend class UncompressedSourceCache; |
| 303 |
}; |
| 304 |
|
| 305 |
private: |
| 306 |
UniquePtr<Map> map_ = nullptr; |
| 307 |
AutoHoldEntry* holder_ = nullptr; |
| 308 |
|
| 309 |
public: |
| 310 |
UncompressedSourceCache() = default; |
| 311 |
|
| 312 |
template <typename Unit> |
| 313 |
const Unit* lookup(const ScriptSourceChunk& ssc, AutoHoldEntry& asp); |
| 314 |
|
| 315 |
bool put(const ScriptSourceChunk& ssc, SourceData data, AutoHoldEntry& asp); |
| 316 |
|
| 317 |
void purge(); |
| 318 |
|
| 319 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf); |
| 320 |
|
| 321 |
private: |
| 322 |
void holdEntry(AutoHoldEntry& holder, const ScriptSourceChunk& ssc); |
| 323 |
void releaseEntry(AutoHoldEntry& holder); |
| 324 |
}; |
| 325 |
|
| 326 |
template <typename Unit> |
| 327 |
struct SourceTypeTraits; |
| 328 |
|
| 329 |
template <> |
| 330 |
struct SourceTypeTraits<mozilla::Utf8Unit> { |
| 331 |
using CharT = char; |
| 332 |
using SharedImmutableString = js::SharedImmutableString; |
| 333 |
|
| 334 |
static const mozilla::Utf8Unit* units(const SharedImmutableString& string) { |
| 335 |
// Casting |char| data to |Utf8Unit| is safe because |Utf8Unit| |
| 336 |
// contains a |char|. See the long comment in |Utf8Unit|'s definition. |
| 337 |
return reinterpret_cast<const mozilla::Utf8Unit*>(string.chars()); |
| 338 |
} |
| 339 |
|
| 340 |
static char* toString(const mozilla::Utf8Unit* units) { |
| 341 |
auto asUnsigned = |
| 342 |
const_cast<unsigned char*>(mozilla::Utf8AsUnsignedChars(units)); |
| 343 |
return reinterpret_cast<char*>(asUnsigned); |
| 344 |
} |
| 345 |
|
| 346 |
static UniqueChars toCacheable(EntryUnits<mozilla::Utf8Unit> str) { |
| 347 |
// The cache only stores strings of |char| or |char16_t|, and right now |
| 348 |
// it seems best not to gunk up the cache with |Utf8Unit| too. So |
| 349 |
// cache |Utf8Unit| strings by interpreting them as |char| strings. |
| 350 |
char* chars = toString(str.release()); |
| 351 |
return UniqueChars(chars); |
| 352 |
} |
| 353 |
}; |
| 354 |
|
| 355 |
template <> |
| 356 |
struct SourceTypeTraits<char16_t> { |
| 357 |
using CharT = char16_t; |
| 358 |
using SharedImmutableString = js::SharedImmutableTwoByteString; |
| 359 |
|
| 360 |
static const char16_t* units(const SharedImmutableString& string) { |
| 361 |
return string.chars(); |
| 362 |
} |
| 363 |
|
| 364 |
static char16_t* toString(const char16_t* units) { |
| 365 |
return const_cast<char16_t*>(units); |
| 366 |
} |
| 367 |
|
| 368 |
static UniqueTwoByteChars toCacheable(EntryUnits<char16_t> str) { |
| 369 |
return UniqueTwoByteChars(std::move(str)); |
| 370 |
} |
| 371 |
}; |
| 372 |
|
| 373 |
// Synchronously compress the source of |script|, for testing purposes. |
| 374 |
[[nodiscard]] extern bool SynchronouslyCompressSource( |
| 375 |
JSContext* cx, JS::Handle<BaseScript*> script); |
| 376 |
|
| 377 |
// [SMDOC] ScriptSource |
| 378 |
// |
| 379 |
// This class abstracts over the source we used to compile from. The current |
| 380 |
// representation may transition to different modes in order to save memory. |
| 381 |
// Abstractly the source may be one of UTF-8 or UTF-16. The data itself may be |
| 382 |
// unavailable, retrieveable-using-source-hook, compressed, or uncompressed. If |
| 383 |
// source is retrieved or decompressed for use, we may update the ScriptSource |
| 384 |
// to hold the result. |
| 385 |
class ScriptSource { |
| 386 |
// NOTE: While ScriptSources may be compressed off thread, they are only |
| 387 |
// modified by the main thread, and all members are always safe to access |
| 388 |
// on the main thread. |
| 389 |
|
| 390 |
friend class SourceCompressionTask; |
| 391 |
friend bool SynchronouslyCompressSource(JSContext* cx, |
| 392 |
JS::Handle<BaseScript*> script); |
| 393 |
|
| 394 |
friend class frontend::StencilXDR; |
| 395 |
|
| 396 |
private: |
| 397 |
// Common base class of the templated variants of PinnedUnits<T>. |
| 398 |
class PinnedUnitsBase { |
| 399 |
protected: |
| 400 |
ScriptSource* source_; |
| 401 |
|
| 402 |
explicit PinnedUnitsBase(ScriptSource* source) : source_(source) {} |
| 403 |
|
| 404 |
void addReader(); |
| 405 |
|
| 406 |
template <typename Unit> |
| 407 |
void removeReader(); |
| 408 |
}; |
| 409 |
|
| 410 |
public: |
| 411 |
// Any users that wish to manipulate the char buffer of the ScriptSource |
| 412 |
// needs to do so via PinnedUnits for GC safety. A GC may compress |
| 413 |
// ScriptSources. If the source were initially uncompressed, then any raw |
| 414 |
// pointers to the char buffer would now point to the freed, uncompressed |
| 415 |
// chars. This is analogous to Rooted. |
| 416 |
template <typename Unit> |
| 417 |
class PinnedUnits : public PinnedUnitsBase { |
| 418 |
const Unit* units_; |
| 419 |
|
| 420 |
public: |
| 421 |
PinnedUnits(JSContext* cx, ScriptSource* source, |
| 422 |
UncompressedSourceCache::AutoHoldEntry& holder, size_t begin, |
| 423 |
size_t len); |
| 424 |
|
| 425 |
~PinnedUnits(); |
| 426 |
|
| 427 |
const Unit* get() const { return units_; } |
| 428 |
|
| 429 |
const typename SourceTypeTraits<Unit>::CharT* asChars() const { |
| 430 |
return SourceTypeTraits<Unit>::toString(get()); |
| 431 |
} |
| 432 |
}; |
| 433 |
|
| 434 |
template <typename Unit> |
| 435 |
class PinnedUnitsIfUncompressed : public PinnedUnitsBase { |
| 436 |
const Unit* units_; |
| 437 |
|
| 438 |
public: |
| 439 |
PinnedUnitsIfUncompressed(ScriptSource* source, size_t begin, size_t len); |
| 440 |
|
| 441 |
~PinnedUnitsIfUncompressed(); |
| 442 |
|
| 443 |
const Unit* get() const { return units_; } |
| 444 |
|
| 445 |
const typename SourceTypeTraits<Unit>::CharT* asChars() const { |
| 446 |
return SourceTypeTraits<Unit>::toString(get()); |
| 447 |
} |
| 448 |
}; |
| 449 |
|
| 450 |
class GenericReader : public PinnedUnitsBase { |
| 451 |
public: |
| 452 |
explicit GenericReader(ScriptSource* source); |
| 453 |
|
| 454 |
~GenericReader(); |
| 455 |
}; |
| 456 |
|
| 457 |
private: |
| 458 |
// Missing source text that isn't retrievable using the source hook. (All |
| 459 |
// ScriptSources initially begin in this state. Users that are compiling |
| 460 |
// source text will overwrite |data| to store a different state.) |
| 461 |
struct Missing {}; |
| 462 |
|
| 463 |
// Source that can be retrieved using the registered source hook. |Unit| |
| 464 |
// records the source type so that source-text coordinates in functions and |
| 465 |
// scripts that depend on this |ScriptSource| are correct. |
| 466 |
template <typename Unit> |
| 467 |
struct Retrievable { |
| 468 |
// The source hook and script URL required to retrieve source are stored |
| 469 |
// elsewhere, so nothing is needed here. It'd be better hygiene to store |
| 470 |
// something source-hook-like in each |ScriptSource| that needs it, but that |
| 471 |
// requires reimagining a source-hook API that currently depends on source |
| 472 |
// hooks being uniquely-owned pointers... |
| 473 |
}; |
| 474 |
|
| 475 |
// Uncompressed source text. Templates distinguish if we are interconvertable |
| 476 |
// to |Retrievable| or not. |
| 477 |
template <typename Unit> |
| 478 |
class UncompressedData { |
| 479 |
typename SourceTypeTraits<Unit>::SharedImmutableString string_; |
| 480 |
|
| 481 |
public: |
| 482 |
explicit UncompressedData( |
| 483 |
typename SourceTypeTraits<Unit>::SharedImmutableString str) |
| 484 |
: string_(std::move(str)) {} |
| 485 |
|
| 486 |
const Unit* units() const { return SourceTypeTraits<Unit>::units(string_); } |
| 487 |
|
| 488 |
size_t length() const { return string_.length(); } |
| 489 |
}; |
| 490 |
|
| 491 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 492 |
class Uncompressed : public UncompressedData<Unit> { |
| 493 |
using Base = UncompressedData<Unit>; |
| 494 |
|
| 495 |
public: |
| 496 |
using Base::Base; |
| 497 |
}; |
| 498 |
|
| 499 |
// Compressed source text. Templates distinguish if we are interconvertable |
| 500 |
// to |Retrievable| or not. |
| 501 |
template <typename Unit> |
| 502 |
struct CompressedData { |
| 503 |
// Single-byte compressed text, regardless whether the original text |
| 504 |
// was single-byte or two-byte. |
| 505 |
SharedImmutableString raw; |
| 506 |
size_t uncompressedLength; |
| 507 |
|
| 508 |
CompressedData(SharedImmutableString raw, size_t uncompressedLength) |
| 509 |
: raw(std::move(raw)), uncompressedLength(uncompressedLength) {} |
| 510 |
}; |
| 511 |
|
| 512 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 513 |
struct Compressed : public CompressedData<Unit> { |
| 514 |
using Base = CompressedData<Unit>; |
| 515 |
|
| 516 |
public: |
| 517 |
using Base::Base; |
| 518 |
}; |
| 519 |
|
| 520 |
// The set of currently allowed encoding modes. |
| 521 |
using SourceType = |
| 522 |
mozilla::Variant<Compressed<mozilla::Utf8Unit, SourceRetrievable::Yes>, |
| 523 |
Uncompressed<mozilla::Utf8Unit, SourceRetrievable::Yes>, |
| 524 |
Compressed<mozilla::Utf8Unit, SourceRetrievable::No>, |
| 525 |
Uncompressed<mozilla::Utf8Unit, SourceRetrievable::No>, |
| 526 |
Compressed<char16_t, SourceRetrievable::Yes>, |
| 527 |
Uncompressed<char16_t, SourceRetrievable::Yes>, |
| 528 |
Compressed<char16_t, SourceRetrievable::No>, |
| 529 |
Uncompressed<char16_t, SourceRetrievable::No>, |
| 530 |
Retrievable<mozilla::Utf8Unit>, Retrievable<char16_t>, |
| 531 |
Missing>; |
| 532 |
|
| 533 |
// |
| 534 |
// Start of fields. |
| 535 |
// |
| 536 |
|
| 537 |
mozilla::Atomic<uint32_t, mozilla::ReleaseAcquire> refs = {}; |
| 538 |
|
| 539 |
// An id for this source that is unique across the process. This can be used |
| 540 |
// to refer to this source from places that don't want to hold a strong |
| 541 |
// reference on the source itself. |
| 542 |
// |
| 543 |
// This is a 32 bit ID and could overflow, in which case the ID will not be |
| 544 |
// unique anymore. |
| 545 |
uint32_t id_ = 0; |
| 546 |
|
| 547 |
// Source data (as a mozilla::Variant). |
| 548 |
SourceType data = SourceType(Missing()); |
| 549 |
|
| 550 |
// If the GC calls triggerConvertToCompressedSource with PinnedUnits present, |
| 551 |
// the last PinnedUnits instance will install the compressed chars upon |
| 552 |
// destruction. |
| 553 |
// |
| 554 |
// Retrievability isn't part of the type here because uncompressed->compressed |
| 555 |
// transitions must preserve existing retrievability. |
| 556 |
struct ReaderInstances { |
| 557 |
size_t count = 0; |
| 558 |
mozilla::MaybeOneOf<CompressedData<mozilla::Utf8Unit>, |
| 559 |
CompressedData<char16_t>> |
| 560 |
pendingCompressed; |
| 561 |
}; |
| 562 |
ExclusiveData<ReaderInstances> readers_; |
| 563 |
|
| 564 |
// The UTF-8 encoded filename of this script. |
| 565 |
SharedImmutableString filename_; |
| 566 |
|
| 567 |
// Hash of the script filename; |
| 568 |
HashNumber filenameHash_ = 0; |
| 569 |
|
| 570 |
// If this ScriptSource was generated by a code-introduction mechanism such |
| 571 |
// as |eval| or |new Function|, the debugger needs access to the "raw" |
| 572 |
// filename of the top-level script that contains the eval-ing code. To |
| 573 |
// keep track of this, we must preserve the original outermost filename (of |
| 574 |
// the original introducer script), so that instead of a filename of |
| 575 |
// "foo.js line 30 > eval line 10 > Function", we can obtain the original |
| 576 |
// raw filename of "foo.js". |
| 577 |
// |
| 578 |
// In the case described above, this field will be set to to the original raw |
| 579 |
// UTF-8 encoded filename from above, otherwise it will be mozilla::Nothing. |
| 580 |
SharedImmutableString introducerFilename_; |
| 581 |
|
| 582 |
SharedImmutableTwoByteString displayURL_; |
| 583 |
SharedImmutableTwoByteString sourceMapURL_; |
| 584 |
|
| 585 |
// A string indicating how this source code was introduced into the system. |
| 586 |
// This is a constant, statically allocated C string, so does not need memory |
| 587 |
// management. |
| 588 |
// |
| 589 |
// TODO: Document the various additional introduction type constants. |
| 590 |
const char* introductionType_ = nullptr; |
| 591 |
|
| 592 |
// Bytecode offset in caller script that generated this code. This is |
| 593 |
// present for eval-ed code, as well as "new Function(...)"-introduced |
| 594 |
// scripts. |
| 595 |
mozilla::Maybe<uint32_t> introductionOffset_; |
| 596 |
|
| 597 |
// If this source is for Function constructor, the position of ")" after |
| 598 |
// parameter list in the source. This is used to get function body. |
| 599 |
// 0 for other cases. |
| 600 |
uint32_t parameterListEnd_ = 0; |
| 601 |
|
| 602 |
// Line number within the file where this source starts (1-origin). |
| 603 |
uint32_t startLine_ = 0; |
| 604 |
// Column number within the file where this source starts, |
| 605 |
// in UTF-16 code units. |
| 606 |
JS::LimitedColumnNumberOneOrigin startColumn_; |
| 607 |
|
| 608 |
// See: CompileOptions::mutedErrors. |
| 609 |
bool mutedErrors_ = false; |
| 610 |
|
| 611 |
// Carry the delazification mode per source. |
| 612 |
JS::DelazificationOption delazificationMode_ = |
| 613 |
JS::DelazificationOption::OnDemandOnly; |
| 614 |
|
| 615 |
// True if an associated SourceCompressionTask was ever created. |
| 616 |
bool hadCompressionTask_ = false; |
| 617 |
|
| 618 |
// |
| 619 |
// End of fields. |
| 620 |
// |
| 621 |
|
| 622 |
// How many ids have been handed out to sources. |
| 623 |
static mozilla::Atomic<uint32_t, mozilla::SequentiallyConsistent> idCount_; |
| 624 |
|
| 625 |
template <typename Unit> |
| 626 |
const Unit* chunkUnits(JSContext* cx, |
| 627 |
UncompressedSourceCache::AutoHoldEntry& holder, |
| 628 |
size_t chunk); |
| 629 |
|
| 630 |
// Return a string containing the chars starting at |begin| and ending at |
| 631 |
// |begin + len|. |
| 632 |
// |
| 633 |
// Warning: this is *not* GC-safe! Any chars to be handed out must use |
| 634 |
// PinnedUnits. See comment below. |
| 635 |
template <typename Unit> |
| 636 |
const Unit* units(JSContext* cx, UncompressedSourceCache::AutoHoldEntry& asp, |
| 637 |
size_t begin, size_t len); |
| 638 |
|
| 639 |
template <typename Unit> |
| 640 |
const Unit* uncompressedUnits(size_t begin, size_t len); |
| 641 |
|
| 642 |
public: |
| 643 |
// When creating a JSString* from TwoByte source characters, we don't try to |
| 644 |
// to deflate to Latin1 for longer strings, because this can be slow. |
| 645 |
static const size_t SourceDeflateLimit = 100; |
| 646 |
|
| 647 |
explicit ScriptSource() |
| 648 |
: id_(++idCount_), readers_(js::mutexid::SourceCompression) {} |
| 649 |
~ScriptSource() { MOZ_ASSERT(refs == 0); } |
| 650 |
|
| 651 |
void AddRef() { refs++; } |
| 652 |
void Release() { |
| 653 |
MOZ_ASSERT(refs != 0); |
| 654 |
if (--refs == 0) { |
| 655 |
js_delete(this); |
| 656 |
} |
| 657 |
} |
| 658 |
[[nodiscard]] bool initFromOptions(FrontendContext* fc, |
| 659 |
const JS::ReadOnlyCompileOptions& options); |
| 660 |
|
| 661 |
/** |
| 662 |
* The minimum script length (in code units) necessary for a script to be |
| 663 |
* eligible to be compressed. |
| 664 |
*/ |
| 665 |
static constexpr size_t MinimumCompressibleLength = 256; |
| 666 |
|
| 667 |
SharedImmutableString getOrCreateStringZ(FrontendContext* fc, |
| 668 |
UniqueChars&& str); |
| 669 |
SharedImmutableTwoByteString getOrCreateStringZ(FrontendContext* fc, |
| 670 |
UniqueTwoByteChars&& str); |
| 671 |
|
| 672 |
private: |
| 673 |
class LoadSourceMatcher; |
| 674 |
|
| 675 |
public: |
| 676 |
// Attempt to load usable source for |ss| -- source text on which substring |
| 677 |
// operations and the like can be performed. On success return true and set |
| 678 |
// |*loaded| to indicate whether usable source could be loaded; otherwise |
| 679 |
// return false. |
| 680 |
static bool loadSource(JSContext* cx, ScriptSource* ss, bool* loaded); |
| 681 |
|
| 682 |
// Assign source data from |srcBuf| to this recently-created |ScriptSource|. |
| 683 |
template <typename Unit> |
| 684 |
[[nodiscard]] bool assignSource(FrontendContext* fc, |
| 685 |
const JS::ReadOnlyCompileOptions& options, |
| 686 |
JS::SourceText<Unit>& srcBuf); |
| 687 |
|
| 688 |
bool hasSourceText() const { |
| 689 |
return hasUncompressedSource() || hasCompressedSource(); |
| 690 |
} |
| 691 |
|
| 692 |
private: |
| 693 |
template <typename Unit> |
| 694 |
struct UncompressedDataMatcher { |
| 695 |
template <SourceRetrievable CanRetrieve> |
| 696 |
const UncompressedData<Unit>* operator()( |
| 697 |
const Uncompressed<Unit, CanRetrieve>& u) { |
| 698 |
return &u; |
| 699 |
} |
| 700 |
|
| 701 |
template <typename T> |
| 702 |
const UncompressedData<Unit>* operator()(const T&) { |
| 703 |
MOZ_CRASH( |
| 704 |
"attempting to access uncompressed data in a ScriptSource not " |
| 705 |
"containing it"); |
| 706 |
return nullptr; |
| 707 |
} |
| 708 |
}; |
| 709 |
|
| 710 |
public: |
| 711 |
template <typename Unit> |
| 712 |
const UncompressedData<Unit>* uncompressedData() { |
| 713 |
return data.match(UncompressedDataMatcher<Unit>()); |
| 714 |
} |
| 715 |
|
| 716 |
private: |
| 717 |
template <typename Unit> |
| 718 |
struct CompressedDataMatcher { |
| 719 |
template <SourceRetrievable CanRetrieve> |
| 720 |
const CompressedData<Unit>* operator()( |
| 721 |
const Compressed<Unit, CanRetrieve>& c) { |
| 722 |
return &c; |
| 723 |
} |
| 724 |
|
| 725 |
template <typename T> |
| 726 |
const CompressedData<Unit>* operator()(const T&) { |
| 727 |
MOZ_CRASH( |
| 728 |
"attempting to access compressed data in a ScriptSource not " |
| 729 |
"containing it"); |
| 730 |
return nullptr; |
| 731 |
} |
| 732 |
}; |
| 733 |
|
| 734 |
public: |
| 735 |
template <typename Unit> |
| 736 |
const CompressedData<Unit>* compressedData() { |
| 737 |
return data.match(CompressedDataMatcher<Unit>()); |
| 738 |
} |
| 739 |
|
| 740 |
private: |
| 741 |
struct HasUncompressedSource { |
| 742 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 743 |
bool operator()(const Uncompressed<Unit, CanRetrieve>&) { |
| 744 |
return true; |
| 745 |
} |
| 746 |
|
| 747 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 748 |
bool operator()(const Compressed<Unit, CanRetrieve>&) { |
| 749 |
return false; |
| 750 |
} |
| 751 |
|
| 752 |
template <typename Unit> |
| 753 |
bool operator()(const Retrievable<Unit>&) { |
| 754 |
return false; |
| 755 |
} |
| 756 |
|
| 757 |
bool operator()(const Missing&) { return false; } |
| 758 |
}; |
| 759 |
|
| 760 |
public: |
| 761 |
bool hasUncompressedSource() const { |
| 762 |
return data.match(HasUncompressedSource()); |
| 763 |
} |
| 764 |
|
| 765 |
private: |
| 766 |
template <typename Unit> |
| 767 |
struct IsUncompressed { |
| 768 |
template <SourceRetrievable CanRetrieve> |
| 769 |
bool operator()(const Uncompressed<Unit, CanRetrieve>&) { |
| 770 |
return true; |
| 771 |
} |
| 772 |
|
| 773 |
template <typename T> |
| 774 |
bool operator()(const T&) { |
| 775 |
return false; |
| 776 |
} |
| 777 |
}; |
| 778 |
|
| 779 |
public: |
| 780 |
template <typename Unit> |
| 781 |
bool isUncompressed() const { |
| 782 |
return data.match(IsUncompressed<Unit>()); |
| 783 |
} |
| 784 |
|
| 785 |
private: |
| 786 |
struct HasCompressedSource { |
| 787 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 788 |
bool operator()(const Compressed<Unit, CanRetrieve>&) { |
| 789 |
return true; |
| 790 |
} |
| 791 |
|
| 792 |
template <typename T> |
| 793 |
bool operator()(const T&) { |
| 794 |
return false; |
| 795 |
} |
| 796 |
}; |
| 797 |
|
| 798 |
public: |
| 799 |
bool hasCompressedSource() const { return data.match(HasCompressedSource()); } |
| 800 |
|
| 801 |
private: |
| 802 |
template <typename Unit> |
| 803 |
struct IsCompressed { |
| 804 |
template <SourceRetrievable CanRetrieve> |
| 805 |
bool operator()(const Compressed<Unit, CanRetrieve>&) { |
| 806 |
return true; |
| 807 |
} |
| 808 |
|
| 809 |
template <typename T> |
| 810 |
bool operator()(const T&) { |
| 811 |
return false; |
| 812 |
} |
| 813 |
}; |
| 814 |
|
| 815 |
public: |
| 816 |
template <typename Unit> |
| 817 |
bool isCompressed() const { |
| 818 |
return data.match(IsCompressed<Unit>()); |
| 819 |
} |
| 820 |
|
| 821 |
private: |
| 822 |
template <typename Unit> |
| 823 |
struct SourceTypeMatcher { |
| 824 |
template <template <typename C, SourceRetrievable R> class Data, |
| 825 |
SourceRetrievable CanRetrieve> |
| 826 |
bool operator()(const Data<Unit, CanRetrieve>&) { |
| 827 |
return true; |
| 828 |
} |
| 829 |
|
| 830 |
template <template <typename C, SourceRetrievable R> class Data, |
| 831 |
typename NotUnit, SourceRetrievable CanRetrieve> |
| 832 |
bool operator()(const Data<NotUnit, CanRetrieve>&) { |
| 833 |
return false; |
| 834 |
} |
| 835 |
|
| 836 |
bool operator()(const Retrievable<Unit>&) { |
| 837 |
MOZ_CRASH("source type only applies where actual text is available"); |
| 838 |
return false; |
| 839 |
} |
| 840 |
|
| 841 |
template <typename NotUnit> |
| 842 |
bool operator()(const Retrievable<NotUnit>&) { |
| 843 |
return false; |
| 844 |
} |
| 845 |
|
| 846 |
bool operator()(const Missing&) { |
| 847 |
MOZ_CRASH("doesn't make sense to ask source type when missing"); |
| 848 |
return false; |
| 849 |
} |
| 850 |
}; |
| 851 |
|
| 852 |
public: |
| 853 |
template <typename Unit> |
| 854 |
bool hasSourceType() const { |
| 855 |
return data.match(SourceTypeMatcher<Unit>()); |
| 856 |
} |
| 857 |
|
| 858 |
private: |
| 859 |
struct UncompressedLengthMatcher { |
| 860 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 861 |
size_t operator()(const Uncompressed<Unit, CanRetrieve>& u) { |
| 862 |
return u.length(); |
| 863 |
} |
| 864 |
|
| 865 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 866 |
size_t operator()(const Compressed<Unit, CanRetrieve>& u) { |
| 867 |
return u.uncompressedLength; |
| 868 |
} |
| 869 |
|
| 870 |
template <typename Unit> |
| 871 |
size_t operator()(const Retrievable<Unit>&) { |
| 872 |
MOZ_CRASH("ScriptSource::length on a missing-but-retrievable source"); |
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
size_t operator()(const Missing& m) { |
| 877 |
MOZ_CRASH("ScriptSource::length on a missing source"); |
| 878 |
return 0; |
| 879 |
} |
| 880 |
}; |
| 881 |
|
| 882 |
public: |
| 883 |
size_t length() const { |
| 884 |
MOZ_ASSERT(hasSourceText()); |
| 885 |
return data.match(UncompressedLengthMatcher()); |
| 886 |
} |
| 887 |
|
| 888 |
JSLinearString* substring(JSContext* cx, size_t start, size_t stop); |
| 889 |
JSLinearString* substringDontDeflate(JSContext* cx, size_t start, |
| 890 |
size_t stop); |
| 891 |
|
| 892 |
[[nodiscard]] bool appendSubstring(JSContext* cx, js::StringBuilder& buf, |
| 893 |
size_t start, size_t stop); |
| 894 |
|
| 895 |
void setParameterListEnd(uint32_t parameterListEnd) { |
| 896 |
parameterListEnd_ = parameterListEnd; |
| 897 |
} |
| 898 |
|
| 899 |
bool isFunctionBody() { return parameterListEnd_ != 0; } |
| 900 |
JSLinearString* functionBodyString(JSContext* cx); |
| 901 |
|
| 902 |
void addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 903 |
JS::ScriptSourceInfo* info) const; |
| 904 |
|
| 905 |
private: |
| 906 |
// Overwrites |data| with the uncompressed data from |source|. |
| 907 |
// |
| 908 |
// This function asserts nothing about |data|. Users should use assertions to |
| 909 |
// double-check their own understandings of the |data| state transition being |
| 910 |
// performed. |
| 911 |
template <typename ContextT, typename Unit> |
| 912 |
[[nodiscard]] bool setUncompressedSourceHelper(ContextT* cx, |
| 913 |
EntryUnits<Unit>&& source, |
| 914 |
size_t length, |
| 915 |
SourceRetrievable retrievable); |
| 916 |
|
| 917 |
public: |
| 918 |
// Initialize a fresh |ScriptSource| with unretrievable, uncompressed source. |
| 919 |
template <typename Unit> |
| 920 |
[[nodiscard]] bool initializeUnretrievableUncompressedSource( |
| 921 |
FrontendContext* fc, EntryUnits<Unit>&& source, size_t length); |
| 922 |
|
| 923 |
// Set the retrieved source for a |ScriptSource| whose source was recorded as |
| 924 |
// missing but retrievable. |
| 925 |
template <typename Unit> |
| 926 |
[[nodiscard]] bool setRetrievedSource(JSContext* cx, |
| 927 |
EntryUnits<Unit>&& source, |
| 928 |
size_t length); |
| 929 |
|
| 930 |
[[nodiscard]] bool tryCompressOffThread(JSContext* cx); |
| 931 |
|
| 932 |
// Called by the SourceCompressionTask constructor to indicate such a task was |
| 933 |
// ever created. |
| 934 |
void noteSourceCompressionTask() { hadCompressionTask_ = true; } |
| 935 |
|
| 936 |
// *Trigger* the conversion of this ScriptSource from containing uncompressed |
| 937 |
// |Unit|-encoded source to containing compressed source. Conversion may not |
| 938 |
// be complete when this function returns: it'll be delayed if there's ongoing |
| 939 |
// use of the uncompressed source via |PinnedUnits|, in which case conversion |
| 940 |
// won't occur until the outermost |PinnedUnits| is destroyed. |
| 941 |
// |
| 942 |
// Compressed source is in bytes, no matter that |Unit| might be |char16_t|. |
| 943 |
// |sourceLength| is the length in code units (not bytes) of the uncompressed |
| 944 |
// source. |
| 945 |
template <typename Unit> |
| 946 |
void triggerConvertToCompressedSource(SharedImmutableString compressed, |
| 947 |
size_t sourceLength); |
| 948 |
|
| 949 |
// Initialize a fresh ScriptSource as containing unretrievable compressed |
| 950 |
// source of the indicated original encoding. |
| 951 |
template <typename Unit> |
| 952 |
[[nodiscard]] bool initializeWithUnretrievableCompressedSource( |
| 953 |
FrontendContext* fc, UniqueChars&& raw, size_t rawLength, |
| 954 |
size_t sourceLength); |
| 955 |
|
| 956 |
private: |
| 957 |
void performTaskWork(SourceCompressionTask* task); |
| 958 |
|
| 959 |
struct TriggerConvertToCompressedSourceFromTask { |
| 960 |
ScriptSource* const source_; |
| 961 |
SharedImmutableString& compressed_; |
| 962 |
|
| 963 |
TriggerConvertToCompressedSourceFromTask(ScriptSource* source, |
| 964 |
SharedImmutableString& compressed) |
| 965 |
: source_(source), compressed_(compressed) {} |
| 966 |
|
| 967 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 968 |
void operator()(const Uncompressed<Unit, CanRetrieve>&) { |
| 969 |
source_->triggerConvertToCompressedSource<Unit>(std::move(compressed_), |
| 970 |
source_->length()); |
| 971 |
} |
| 972 |
|
| 973 |
template <typename Unit, SourceRetrievable CanRetrieve> |
| 974 |
void operator()(const Compressed<Unit, CanRetrieve>&) { |
| 975 |
MOZ_CRASH( |
| 976 |
"can't set compressed source when source is already compressed -- " |
| 977 |
"ScriptSource::tryCompressOffThread shouldn't have queued up this " |
| 978 |
"task?"); |
| 979 |
} |
| 980 |
|
| 981 |
template <typename Unit> |
| 982 |
void operator()(const Retrievable<Unit>&) { |
| 983 |
MOZ_CRASH("shouldn't compressing unloaded-but-retrievable source"); |
| 984 |
} |
| 985 |
|
| 986 |
void operator()(const Missing&) { |
| 987 |
MOZ_CRASH( |
| 988 |
"doesn't make sense to set compressed source for missing source -- " |
| 989 |
"ScriptSource::tryCompressOffThread shouldn't have queued up this " |
| 990 |
"task?"); |
| 991 |
} |
| 992 |
}; |
| 993 |
|
| 994 |
template <typename Unit> |
| 995 |
void convertToCompressedSource(SharedImmutableString compressed, |
| 996 |
size_t uncompressedLength); |
| 997 |
|
| 998 |
template <typename Unit> |
| 999 |
void performDelayedConvertToCompressedSource( |
| 1000 |
ExclusiveData<ReaderInstances>::Guard& g); |
| 1001 |
|
| 1002 |
void triggerConvertToCompressedSourceFromTask( |
| 1003 |
SharedImmutableString compressed); |
| 1004 |
|
| 1005 |
public: |
| 1006 |
HashNumber filenameHash() const { return filenameHash_; } |
| 1007 |
const char* filename() const { |
| 1008 |
return filename_ ? filename_.chars() : nullptr; |
| 1009 |
} |
| 1010 |
[[nodiscard]] bool setFilename(FrontendContext* fc, const char* filename); |
| 1011 |
[[nodiscard]] bool setFilename(FrontendContext* fc, UniqueChars&& filename); |
| 1012 |
|
| 1013 |
bool hasIntroducerFilename() const { |
| 1014 |
return introducerFilename_ ? true : false; |
| 1015 |
} |
| 1016 |
const char* introducerFilename() const { |
| 1017 |
return introducerFilename_ ? introducerFilename_.chars() : filename(); |
| 1018 |
} |
| 1019 |
[[nodiscard]] bool setIntroducerFilename(FrontendContext* fc, |
| 1020 |
const char* filename); |
| 1021 |
[[nodiscard]] bool setIntroducerFilename(FrontendContext* fc, |
| 1022 |
UniqueChars&& filename); |
| 1023 |
|
| 1024 |
bool hasIntroductionType() const { return introductionType_; } |
| 1025 |
const char* introductionType() const { |
| 1026 |
MOZ_ASSERT(hasIntroductionType()); |
| 1027 |
return introductionType_; |
| 1028 |
} |
| 1029 |
|
| 1030 |
uint32_t id() const { return id_; } |
| 1031 |
|
| 1032 |
// Display URLs |
| 1033 |
[[nodiscard]] bool setDisplayURL(FrontendContext* fc, const char16_t* url); |
| 1034 |
[[nodiscard]] bool setDisplayURL(FrontendContext* fc, |
| 1035 |
UniqueTwoByteChars&& url); |
| 1036 |
bool hasDisplayURL() const { return bool(displayURL_); } |
| 1037 |
const char16_t* displayURL() { return displayURL_.chars(); } |
| 1038 |
|
| 1039 |
// Source maps |
| 1040 |
[[nodiscard]] bool setSourceMapURL(FrontendContext* fc, const char16_t* url); |
| 1041 |
[[nodiscard]] bool setSourceMapURL(FrontendContext* fc, |
| 1042 |
UniqueTwoByteChars&& url); |
| 1043 |
bool hasSourceMapURL() const { return bool(sourceMapURL_); } |
| 1044 |
const char16_t* sourceMapURL() { return sourceMapURL_.chars(); } |
| 1045 |
|
| 1046 |
bool mutedErrors() const { return mutedErrors_; } |
| 1047 |
|
| 1048 |
uint32_t startLine() const { return startLine_; } |
| 1049 |
JS::LimitedColumnNumberOneOrigin startColumn() const { return startColumn_; } |
| 1050 |
|
| 1051 |
JS::DelazificationOption delazificationMode() const { |
| 1052 |
return delazificationMode_; |
| 1053 |
} |
| 1054 |
|
| 1055 |
bool hasIntroductionOffset() const { return introductionOffset_.isSome(); } |
| 1056 |
uint32_t introductionOffset() const { return introductionOffset_.value(); } |
| 1057 |
void setIntroductionOffset(uint32_t offset) { |
| 1058 |
MOZ_ASSERT(!hasIntroductionOffset()); |
| 1059 |
MOZ_ASSERT(offset <= (uint32_t)INT32_MAX); |
| 1060 |
introductionOffset_.emplace(offset); |
| 1061 |
} |
| 1062 |
}; |
| 1063 |
|
| 1064 |
// [SMDOC] ScriptSourceObject |
| 1065 |
// |
| 1066 |
// ScriptSourceObject stores the ScriptSource and GC pointers related to it. |
| 1067 |
class ScriptSourceObject : public NativeObject { |
| 1068 |
static const JSClassOps classOps_; |
| 1069 |
|
| 1070 |
public: |
| 1071 |
static const JSClass class_; |
| 1072 |
|
| 1073 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 1074 |
|
| 1075 |
static ScriptSourceObject* create(JSContext* cx, ScriptSource* source); |
| 1076 |
|
| 1077 |
// Initialize those properties of this ScriptSourceObject whose values |
| 1078 |
// are provided by |options|, re-wrapping as necessary. |
| 1079 |
static bool initFromOptions(JSContext* cx, |
| 1080 |
JS::Handle<ScriptSourceObject*> source, |
| 1081 |
const JS::InstantiateOptions& options); |
| 1082 |
|
| 1083 |
static bool initElementProperties(JSContext* cx, |
| 1084 |
JS::Handle<ScriptSourceObject*> source, |
| 1085 |
HandleString elementAttrName); |
| 1086 |
|
| 1087 |
bool hasSource() const { return !getReservedSlot(SOURCE_SLOT).isUndefined(); } |
| 1088 |
ScriptSource* source() const { |
| 1089 |
return static_cast<ScriptSource*>(getReservedSlot(SOURCE_SLOT).toPrivate()); |
| 1090 |
} |
| 1091 |
|
| 1092 |
JSObject* unwrappedElement(JSContext* cx) const; |
| 1093 |
|
| 1094 |
const Value& unwrappedElementAttributeName() const { |
| 1095 |
MOZ_ASSERT(isInitialized()); |
| 1096 |
const Value& v = getReservedSlot(ELEMENT_PROPERTY_SLOT); |
| 1097 |
MOZ_ASSERT(!v.isMagic()); |
| 1098 |
return v; |
| 1099 |
} |
| 1100 |
BaseScript* unwrappedIntroductionScript() const { |
| 1101 |
MOZ_ASSERT(isInitialized()); |
| 1102 |
Value value = getReservedSlot(INTRODUCTION_SCRIPT_SLOT); |
| 1103 |
if (value.isUndefined()) { |
| 1104 |
return nullptr; |
| 1105 |
} |
| 1106 |
return value.toGCThing()->as<BaseScript>(); |
| 1107 |
} |
| 1108 |
|
| 1109 |
void setPrivate(JSRuntime* rt, const Value& value); |
| 1110 |
void clearPrivate(JSRuntime* rt); |
| 1111 |
|
| 1112 |
void setIntroductionScript(const Value& introductionScript) { |
| 1113 |
setReservedSlot(INTRODUCTION_SCRIPT_SLOT, introductionScript); |
| 1114 |
} |
| 1115 |
|
| 1116 |
Value getPrivate() const { |
| 1117 |
MOZ_ASSERT(isInitialized()); |
| 1118 |
Value value = getReservedSlot(PRIVATE_SLOT); |
| 1119 |
return value; |
| 1120 |
} |
| 1121 |
|
| 1122 |
private: |
| 1123 |
#ifdef DEBUG |
| 1124 |
bool isInitialized() const { |
| 1125 |
Value element = getReservedSlot(ELEMENT_PROPERTY_SLOT); |
| 1126 |
if (element.isMagic(JS_GENERIC_MAGIC)) { |
| 1127 |
return false; |
| 1128 |
} |
| 1129 |
return !getReservedSlot(INTRODUCTION_SCRIPT_SLOT).isMagic(JS_GENERIC_MAGIC); |
| 1130 |
} |
| 1131 |
#endif |
| 1132 |
|
| 1133 |
enum { |
| 1134 |
SOURCE_SLOT = 0, |
| 1135 |
ELEMENT_PROPERTY_SLOT, |
| 1136 |
INTRODUCTION_SCRIPT_SLOT, |
| 1137 |
PRIVATE_SLOT, |
| 1138 |
STENCILS_SLOT, |
| 1139 |
RESERVED_SLOTS |
| 1140 |
}; |
| 1141 |
|
| 1142 |
// Delazification stencils can be aggregated in |
| 1143 |
// InitialStencilAndDelazification, this structure might be used for |
| 1144 |
// different purposes. |
| 1145 |
// - Collecting: The goal is to aggregate all delazified functions in order |
| 1146 |
// to aggregate them for serialization. |
| 1147 |
// - Sharing: The goal is to use the InitialStencilAndDelazification as a way |
| 1148 |
// to share multiple threads efforts towards parsing a Script Source |
| 1149 |
// content. |
| 1150 |
// |
| 1151 |
// See setCollectingDelazifications and setSharingDelazifications for details. |
| 1152 |
static constexpr uintptr_t STENCILS_COLLECTING_DELAZIFICATIONS_FLAG = 0x1; |
| 1153 |
static constexpr uintptr_t STENCILS_SHARING_DELAZIFICATIONS_FLAG = 0x2; |
| 1154 |
static constexpr uintptr_t STENCILS_MASK = 0x3; |
| 1155 |
|
| 1156 |
void clearStencils(); |
| 1157 |
|
| 1158 |
template <uintptr_t flag> |
| 1159 |
void setStencilsFlag(); |
| 1160 |
|
| 1161 |
template <uintptr_t flag> |
| 1162 |
void unsetStencilsFlag(); |
| 1163 |
|
| 1164 |
template <uintptr_t flag> |
| 1165 |
bool isStencilsFlagSet() const; |
| 1166 |
|
| 1167 |
public: |
| 1168 |
// Associate stencils to this ScriptSourceObject. |
| 1169 |
// The consumer should call setCollectingDelazifications or |
| 1170 |
// setSharingDelazifications after this. |
| 1171 |
void setStencils( |
| 1172 |
already_AddRefed<frontend::InitialStencilAndDelazifications> stencils); |
| 1173 |
|
| 1174 |
// Start collecting delazifications. |
| 1175 |
// This is a temporary state until unsetCollectingDelazifications is called, |
| 1176 |
// and this expects a pair of set/unset call. |
| 1177 |
// |
| 1178 |
// The caller should check isCollectingDelazifications before calling this. |
| 1179 |
void setCollectingDelazifications(); |
| 1180 |
|
| 1181 |
// Clear the flag for collecting delazifications. |
| 1182 |
// |
| 1183 |
// If setSharingDelazifications wasn't called, this clears the association |
| 1184 |
// with the stencils. |
| 1185 |
void unsetCollectingDelazifications(); |
| 1186 |
|
| 1187 |
// Returns true if setCollectingDelazifications was called and |
| 1188 |
// unsetCollectingDelazifications is not yet called. |
| 1189 |
bool isCollectingDelazifications() const; |
| 1190 |
|
| 1191 |
// Start sharing delazifications with others. |
| 1192 |
// This is a permanent state. |
| 1193 |
// |
| 1194 |
// The flag is orthogonal to setCollectingDelazifications. |
| 1195 |
void setSharingDelazifications(); |
| 1196 |
|
| 1197 |
// Returns true if setSharingDelazifications was called. |
| 1198 |
bool isSharingDelazifications() const; |
| 1199 |
|
| 1200 |
// Return the associated stencils if any. |
| 1201 |
// Returns nullptr if stencils is not associated |
| 1202 |
frontend::InitialStencilAndDelazifications* maybeGetStencils(); |
| 1203 |
}; |
| 1204 |
|
| 1205 |
// ScriptWarmUpData represents a pointer-sized field in BaseScript that stores |
| 1206 |
// one of the following using low-bit tags: |
| 1207 |
// |
| 1208 |
// * The enclosing BaseScript. This is only used while this script is lazy and |
| 1209 |
// its containing script is also lazy. This outer script must be compiled |
| 1210 |
// before the current script can in order to correctly build the scope chain. |
| 1211 |
// |
| 1212 |
// * The enclosing Scope. This is only used while this script is lazy and its |
| 1213 |
// containing script is compiled. This is the outer scope chain that will be |
| 1214 |
// used to compile this scipt. |
| 1215 |
// |
| 1216 |
// * The script's warm-up count. This is only used until the script has a |
| 1217 |
// JitScript. The Baseline Interpreter and JITs use the warm-up count stored |
| 1218 |
// in JitScript. |
| 1219 |
// |
| 1220 |
// * A pointer to the JitScript, when the script is warm enough for the Baseline |
| 1221 |
// Interpreter. |
| 1222 |
// |
| 1223 |
class ScriptWarmUpData { |
| 1224 |
uintptr_t data_ = ResetState(); |
| 1225 |
|
| 1226 |
private: |
| 1227 |
static constexpr uintptr_t NumTagBits = 2; |
| 1228 |
static constexpr uint32_t MaxWarmUpCount = UINT32_MAX >> NumTagBits; |
| 1229 |
|
| 1230 |
public: |
| 1231 |
// Public only for the JITs. |
| 1232 |
static constexpr uintptr_t TagMask = (1 << NumTagBits) - 1; |
| 1233 |
static constexpr uintptr_t JitScriptTag = 0; |
| 1234 |
static constexpr uintptr_t EnclosingScriptTag = 1; |
| 1235 |
static constexpr uintptr_t EnclosingScopeTag = 2; |
| 1236 |
static constexpr uintptr_t WarmUpCountTag = 3; |
| 1237 |
|
| 1238 |
private: |
| 1239 |
// A gc-safe value to clear to. |
| 1240 |
constexpr uintptr_t ResetState() { return 0 | WarmUpCountTag; } |
| 1241 |
|
| 1242 |
template <uintptr_t Tag> |
| 1243 |
inline void setTaggedPtr(void* ptr) { |
| 1244 |
static_assert(Tag <= TagMask, "Tag must fit in TagMask"); |
| 1245 |
MOZ_ASSERT((uintptr_t(ptr) & TagMask) == 0); |
| 1246 |
data_ = uintptr_t(ptr) | Tag; |
| 1247 |
} |
| 1248 |
|
| 1249 |
template <typename T, uintptr_t Tag> |
| 1250 |
inline T getTaggedPtr() const { |
| 1251 |
static_assert(Tag <= TagMask, "Tag must fit in TagMask"); |
| 1252 |
MOZ_ASSERT((data_ & TagMask) == Tag); |
| 1253 |
return reinterpret_cast<T>(data_ & ~TagMask); |
| 1254 |
} |
| 1255 |
|
| 1256 |
void setWarmUpCount(uint32_t count) { |
| 1257 |
if (count > MaxWarmUpCount) { |
| 1258 |
count = MaxWarmUpCount; |
| 1259 |
} |
| 1260 |
data_ = (uintptr_t(count) << NumTagBits) | WarmUpCountTag; |
| 1261 |
} |
| 1262 |
|
| 1263 |
public: |
| 1264 |
void trace(JSTracer* trc); |
| 1265 |
|
| 1266 |
bool isEnclosingScript() const { |
| 1267 |
return (data_ & TagMask) == EnclosingScriptTag; |
| 1268 |
} |
| 1269 |
bool isEnclosingScope() const { |
| 1270 |
return (data_ & TagMask) == EnclosingScopeTag; |
| 1271 |
} |
| 1272 |
bool isWarmUpCount() const { return (data_ & TagMask) == WarmUpCountTag; } |
| 1273 |
bool isJitScript() const { return (data_ & TagMask) == JitScriptTag; } |
| 1274 |
|
| 1275 |
// NOTE: To change type safely, 'clear' the old tagged value and then 'init' |
| 1276 |
// the new one. This will notify the GC appropriately. |
| 1277 |
|
| 1278 |
BaseScript* toEnclosingScript() const { |
| 1279 |
return getTaggedPtr<BaseScript*, EnclosingScriptTag>(); |
| 1280 |
} |
| 1281 |
inline void initEnclosingScript(BaseScript* enclosingScript); |
| 1282 |
inline void clearEnclosingScript(); |
| 1283 |
|
| 1284 |
Scope* toEnclosingScope() const { |
| 1285 |
return getTaggedPtr<Scope*, EnclosingScopeTag>(); |
| 1286 |
} |
| 1287 |
inline void initEnclosingScope(Scope* enclosingScope); |
| 1288 |
inline void clearEnclosingScope(); |
| 1289 |
|
| 1290 |
uint32_t toWarmUpCount() const { |
| 1291 |
MOZ_ASSERT(isWarmUpCount()); |
| 1292 |
return data_ >> NumTagBits; |
| 1293 |
} |
| 1294 |
void resetWarmUpCount(uint32_t count) { |
| 1295 |
MOZ_ASSERT(isWarmUpCount()); |
| 1296 |
setWarmUpCount(count); |
| 1297 |
} |
| 1298 |
void incWarmUpCount() { |
| 1299 |
MOZ_ASSERT(isWarmUpCount()); |
| 1300 |
data_ += uintptr_t(1) << NumTagBits; |
| 1301 |
} |
| 1302 |
|
| 1303 |
jit::JitScript* toJitScript() const { |
| 1304 |
return getTaggedPtr<jit::JitScript*, JitScriptTag>(); |
| 1305 |
} |
| 1306 |
void initJitScript(jit::JitScript* jitScript) { |
| 1307 |
MOZ_ASSERT(isWarmUpCount()); |
| 1308 |
setTaggedPtr<JitScriptTag>(jitScript); |
| 1309 |
} |
| 1310 |
void clearJitScript() { |
| 1311 |
MOZ_ASSERT(isJitScript()); |
| 1312 |
data_ = ResetState(); |
| 1313 |
} |
| 1314 |
} JS_HAZ_GC_POINTER; |
| 1315 |
|
| 1316 |
static_assert(sizeof(ScriptWarmUpData) == sizeof(uintptr_t), |
| 1317 |
"JIT code depends on ScriptWarmUpData being pointer-sized"); |
| 1318 |
|
| 1319 |
// [SMDOC] - JSScript data layout (unshared) |
| 1320 |
// |
| 1321 |
// PrivateScriptData stores variable-length data associated with a script. |
| 1322 |
// Abstractly a PrivateScriptData consists of the following: |
| 1323 |
// |
| 1324 |
// * A non-empty array of GCCellPtr in gcthings() |
| 1325 |
// |
| 1326 |
// Accessing this array just requires calling the appropriate public |
| 1327 |
// Span-computing function. |
| 1328 |
// |
| 1329 |
// This class doesn't use the GC barrier wrapper classes. BaseScript::swapData |
| 1330 |
// performs a manual pre-write barrier when detaching PrivateScriptData from a |
| 1331 |
// script. |
| 1332 |
class alignas(uintptr_t) PrivateScriptData final |
| 1333 |
: public TrailingArray<PrivateScriptData> { |
| 1334 |
private: |
| 1335 |
uint32_t ngcthings = 0; |
| 1336 |
|
| 1337 |
// Note: This is only defined for scripts with an enclosing scope. This |
| 1338 |
// excludes lazy scripts with lazy parents. |
| 1339 |
js::MemberInitializers memberInitializers_ = |
| 1340 |
js::MemberInitializers::Invalid(); |
| 1341 |
|
| 1342 |
// End of fields. |
| 1343 |
|
| 1344 |
private: |
| 1345 |
// Layout helpers |
| 1346 |
Offset gcThingsOffset() { return offsetOfGCThings(); } |
| 1347 |
Offset endOffset() const { |
| 1348 |
uintptr_t size = ngcthings * sizeof(JS::GCCellPtr); |
| 1349 |
return offsetOfGCThings() + size; |
| 1350 |
} |
| 1351 |
|
| 1352 |
// Initialize header and PackedSpans |
| 1353 |
explicit PrivateScriptData(uint32_t ngcthings); |
| 1354 |
|
| 1355 |
public: |
| 1356 |
static constexpr size_t offsetOfGCThings() { |
| 1357 |
return sizeof(PrivateScriptData); |
| 1358 |
} |
| 1359 |
|
| 1360 |
// Accessors for typed array spans. |
| 1361 |
mozilla::Span<JS::GCCellPtr> gcthings() { |
| 1362 |
Offset offset = offsetOfGCThings(); |
| 1363 |
return mozilla::Span{offsetToPointer<JS::GCCellPtr>(offset), ngcthings}; |
| 1364 |
} |
| 1365 |
|
| 1366 |
void setMemberInitializers(MemberInitializers memberInitializers) { |
| 1367 |
MOZ_ASSERT(memberInitializers_.valid == false, |
| 1368 |
"Only init MemberInitializers once"); |
| 1369 |
memberInitializers_ = memberInitializers; |
| 1370 |
} |
| 1371 |
const MemberInitializers& getMemberInitializers() { |
| 1372 |
return memberInitializers_; |
| 1373 |
} |
| 1374 |
|
| 1375 |
// Allocate a new PrivateScriptData. Headers and GCCellPtrs are initialized. |
| 1376 |
static PrivateScriptData* new_(JSContext* cx, uint32_t ngcthings); |
| 1377 |
|
| 1378 |
static bool InitFromStencil( |
| 1379 |
JSContext* cx, js::HandleScript script, |
| 1380 |
const js::frontend::CompilationAtomCache& atomCache, |
| 1381 |
const js::frontend::CompilationStencil& stencil, |
| 1382 |
js::frontend::CompilationGCOutput& gcOutput, |
| 1383 |
const js::frontend::ScriptIndex scriptIndex); |
| 1384 |
|
| 1385 |
void trace(JSTracer* trc); |
| 1386 |
|
| 1387 |
size_t allocationSize() const; |
| 1388 |
|
| 1389 |
// PrivateScriptData has trailing data so isn't copyable or movable. |
| 1390 |
PrivateScriptData(const PrivateScriptData&) = delete; |
| 1391 |
PrivateScriptData& operator=(const PrivateScriptData&) = delete; |
| 1392 |
}; |
| 1393 |
|
| 1394 |
// [SMDOC] Script Representation (js::BaseScript) |
| 1395 |
// |
| 1396 |
// A "script" corresponds to a JavaScript function or a top-level (global, eval, |
| 1397 |
// module) body that will be executed using SpiderMonkey bytecode. Note that |
| 1398 |
// special forms such as asm.js do not use bytecode or the BaseScript type. |
| 1399 |
// |
| 1400 |
// BaseScript may be generated directly from the parser/emitter, or by cloning |
| 1401 |
// or deserializing another script. Cloning is typically used when a script is |
| 1402 |
// needed in multiple realms and we would like to avoid re-compiling. |
| 1403 |
// |
| 1404 |
// A single script may be shared by multiple JSFunctions in a realm when those |
| 1405 |
// function objects are used as closure. In this case, a single JSFunction is |
| 1406 |
// considered canonical (and often does not escape to script directly). |
| 1407 |
// |
| 1408 |
// A BaseScript may be in "lazy" form where the parser performs a syntax-only |
| 1409 |
// parse and saves minimal information. These lazy scripts must be recompiled |
| 1410 |
// from the source (generating bytecode) before they can execute in a process |
| 1411 |
// called "delazification". On GC memory pressure, a fully-compiled script may |
| 1412 |
// be converted back into lazy form by "relazification". |
| 1413 |
// |
| 1414 |
// A fully-initialized BaseScript can be identified with `hasBytecode()` and |
| 1415 |
// will have bytecode and set of GC-things such as scopes, inner-functions, and |
| 1416 |
// object/string literals. This is referred to as a "non-lazy" script. |
| 1417 |
// |
| 1418 |
// A lazy script has either an enclosing script or scope. Each script needs to |
| 1419 |
// know its enclosing scope in order to be fully compiled. If the parent is |
| 1420 |
// still lazy we track that script and will need to compile it first to know our |
| 1421 |
// own enclosing scope. This is because scope objects are not created until full |
| 1422 |
// compilation and bytecode generation. |
| 1423 |
// |
| 1424 |
// |
| 1425 |
// # Script Warm-Up # |
| 1426 |
// |
| 1427 |
// A script evolves its representation over time. As it becomes "hotter" we |
| 1428 |
// attach a stack of additional data-structures generated by the JITs to |
| 1429 |
// speed-up execution. This evolution may also be run in reverse, in order to |
| 1430 |
// reduce memory usage. |
| 1431 |
// |
| 1432 |
// +-------------------------------------+ |
| 1433 |
// | ScriptSource | |
| 1434 |
// | Provides: Source | |
| 1435 |
// | Engine: Parser | |
| 1436 |
// +-------------------------------------+ |
| 1437 |
// v |
| 1438 |
// +-----------------------------------------------+ |
| 1439 |
// | BaseScript | |
| 1440 |
// | Provides: SourceExtent/Bindings | |
| 1441 |
// | Engine: CompileLazyFunctionToStencil | |
| 1442 |
// | /InstantiateStencilsForDelazify | |
| 1443 |
// +-----------------------------------------------+ |
| 1444 |
// v |
| 1445 |
// +-------------------------------------+ |
| 1446 |
// | ImmutableScriptData | |
| 1447 |
// | Provides: Bytecode | |
| 1448 |
// | Engine: Interpreter | |
| 1449 |
// +-------------------------------------+ |
| 1450 |
// v |
| 1451 |
// +-------------------------------------+ |
| 1452 |
// | JitScript | |
| 1453 |
// | Provides: Inline Caches (ICs) | |
| 1454 |
// | Engine: BaselineInterpreter | |
| 1455 |
// +-------------------------------------+ |
| 1456 |
// v |
| 1457 |
// +-------------------------------------+ |
| 1458 |
// | BaselineScript | |
| 1459 |
// | Provides: Native Code | |
| 1460 |
// | Engine: Baseline | |
| 1461 |
// +-------------------------------------+ |
| 1462 |
// v |
| 1463 |
// +-------------------------------------+ |
| 1464 |
// | IonScript | |
| 1465 |
// | Provides: Optimized Native Code | |
| 1466 |
// | Engine: IonMonkey | |
| 1467 |
// +-------------------------------------+ |
| 1468 |
// |
| 1469 |
// NOTE: Scripts may be directly created with bytecode and skip the lazy script |
| 1470 |
// form. This is always the case for top-level scripts. |
| 1471 |
class BaseScript : public gc::TenuredCellWithNonGCPointer<uint8_t> { |
| 1472 |
friend class js::gc::CellAllocator; |
| 1473 |
|
| 1474 |
public: |
| 1475 |
// Pointer to baseline->method()->raw(), ion->method()->raw(), a wasm jit |
| 1476 |
// entry, the JIT's EnterInterpreter stub, or the lazy link stub. Must be |
| 1477 |
// non-null (except on no-jit builds). This is stored in the cell header. |
| 1478 |
uint8_t* jitCodeRaw() const { return headerPtr(); } |
| 1479 |
|
| 1480 |
protected: |
| 1481 |
// Multi-purpose value that changes type as the script warms up from lazy form |
| 1482 |
// to interpreted-bytecode to JITs. See: ScriptWarmUpData type for more info. |
| 1483 |
ScriptWarmUpData warmUpData_ = {}; |
| 1484 |
|
| 1485 |
// For function scripts this is the canonical function, otherwise nullptr. |
| 1486 |
const GCPtr<JSFunction*> function_ = {}; |
| 1487 |
|
| 1488 |
// The ScriptSourceObject for this script. This is always same-compartment and |
| 1489 |
// same-realm with this script. |
| 1490 |
const GCPtr<ScriptSourceObject*> sourceObject_ = {}; |
| 1491 |
|
| 1492 |
// Position of the function in the source buffer. Both in terms of line/column |
| 1493 |
// and code-unit offset. |
| 1494 |
const SourceExtent extent_ = {}; |
| 1495 |
|
| 1496 |
// Immutable flags are a combination of parser options and bytecode |
| 1497 |
// characteristics. These flags are preserved when serializing or copying this |
| 1498 |
// script. |
| 1499 |
const ImmutableScriptFlags immutableFlags_ = {}; |
| 1500 |
|
| 1501 |
// Mutable flags store transient information used by subsystems such as the |
| 1502 |
// debugger and the JITs. These flags are *not* preserved when serializing or |
| 1503 |
// cloning since they are based on runtime state. |
| 1504 |
MutableScriptFlags mutableFlags_ = {}; |
| 1505 |
|
| 1506 |
// Variable-length data owned by this script. This stores one of: |
| 1507 |
// - GC pointers that bytecode references. |
| 1508 |
// - Inner-functions and bindings generated by syntax parse. |
| 1509 |
// - Nullptr, if no bytecode or inner functions. |
| 1510 |
// This is updated as script is delazified and relazified. |
| 1511 |
GCStructPtr<PrivateScriptData*> data_; |
| 1512 |
|
| 1513 |
// Shareable script data. This includes runtime-wide atom pointers, bytecode, |
| 1514 |
// and various script note structures. If the script is currently lazy, this |
| 1515 |
// will be nullptr. |
| 1516 |
RefPtr<js::SharedImmutableScriptData> sharedData_ = {}; |
| 1517 |
|
| 1518 |
// End of fields. |
| 1519 |
|
| 1520 |
BaseScript(uint8_t* stubEntry, JSFunction* function, |
| 1521 |
ScriptSourceObject* sourceObject, const SourceExtent& extent, |
| 1522 |
uint32_t immutableFlags); |
| 1523 |
|
| 1524 |
void setJitCodeRaw(uint8_t* code) { setHeaderPtr(code); } |
| 1525 |
|
| 1526 |
public: |
| 1527 |
static BaseScript* New(JSContext* cx, JS::Handle<JSFunction*> function, |
| 1528 |
JS::Handle<js::ScriptSourceObject*> sourceObject, |
| 1529 |
const js::SourceExtent& extent, |
| 1530 |
uint32_t immutableFlags); |
| 1531 |
|
| 1532 |
// Create a lazy BaseScript without initializing any gc-things. |
| 1533 |
static BaseScript* CreateRawLazy(JSContext* cx, uint32_t ngcthings, |
| 1534 |
HandleFunction fun, |
| 1535 |
JS::Handle<ScriptSourceObject*> sourceObject, |
| 1536 |
const SourceExtent& extent, |
| 1537 |
uint32_t immutableFlags); |
| 1538 |
|
| 1539 |
bool isUsingInterpreterTrampoline(JSRuntime* rt) const; |
| 1540 |
|
| 1541 |
// Canonical function for the script, if it has a function. For top-level |
| 1542 |
// scripts this is nullptr. |
| 1543 |
JSFunction* function() const { return function_; } |
| 1544 |
|
| 1545 |
JS::Realm* realm() const { return sourceObject()->realm(); } |
| 1546 |
JS::Compartment* compartment() const { return sourceObject()->compartment(); } |
| 1547 |
JS::Compartment* maybeCompartment() const { return compartment(); } |
| 1548 |
inline JSPrincipals* principals() const; |
| 1549 |
|
| 1550 |
ScriptSourceObject* sourceObject() const { return sourceObject_; } |
| 1551 |
ScriptSource* scriptSource() const { return sourceObject()->source(); } |
| 1552 |
ScriptSource* maybeForwardedScriptSource() const; |
| 1553 |
|
| 1554 |
bool mutedErrors() const { return scriptSource()->mutedErrors(); } |
| 1555 |
|
| 1556 |
const char* filename() const { return scriptSource()->filename(); } |
| 1557 |
HashNumber filenameHash() const { return scriptSource()->filenameHash(); } |
| 1558 |
const char* maybeForwardedFilename() const { |
| 1559 |
return maybeForwardedScriptSource()->filename(); |
| 1560 |
} |
| 1561 |
|
| 1562 |
uint32_t sourceStart() const { return extent_.sourceStart; } |
| 1563 |
uint32_t sourceEnd() const { return extent_.sourceEnd; } |
| 1564 |
uint32_t sourceLength() const { |
| 1565 |
return extent_.sourceEnd - extent_.sourceStart; |
| 1566 |
} |
| 1567 |
uint32_t toStringStart() const { return extent_.toStringStart; } |
| 1568 |
uint32_t toStringEnd() const { return extent_.toStringEnd; } |
| 1569 |
SourceExtent extent() const { return extent_; } |
| 1570 |
|
| 1571 |
[[nodiscard]] bool appendSourceDataForToString(JSContext* cx, |
| 1572 |
js::StringBuilder& buf); |
| 1573 |
|
| 1574 |
// Line number (1-origin) |
| 1575 |
uint32_t lineno() const { return extent_.lineno; } |
| 1576 |
// Column number in UTF-16 code units |
| 1577 |
JS::LimitedColumnNumberOneOrigin column() const { return extent_.column; } |
| 1578 |
|
| 1579 |
JS::DelazificationOption delazificationMode() const { |
| 1580 |
return scriptSource()->delazificationMode(); |
| 1581 |
} |
| 1582 |
|
| 1583 |
public: |
| 1584 |
ImmutableScriptFlags immutableFlags() const { return immutableFlags_; } |
| 1585 |
RO_IMMUTABLE_SCRIPT_FLAGS(immutableFlags_) |
| 1586 |
RW_MUTABLE_SCRIPT_FLAGS(mutableFlags_) |
| 1587 |
|
| 1588 |
bool hasEnclosingScript() const { return warmUpData_.isEnclosingScript(); } |
| 1589 |
BaseScript* enclosingScript() const { |
| 1590 |
return warmUpData_.toEnclosingScript(); |
| 1591 |
} |
| 1592 |
void setEnclosingScript(BaseScript* enclosingScript); |
| 1593 |
|
| 1594 |
// Returns true is the script has an enclosing scope but no bytecode. It is |
| 1595 |
// ready for delazification. |
| 1596 |
// NOTE: The enclosing script must have been successfully compiled at some |
| 1597 |
// point for the enclosing scope to exist. That script may have since been |
| 1598 |
// GC'd, but we kept the scope live so we can still compile ourselves. |
| 1599 |
bool isReadyForDelazification() const { |
| 1600 |
return warmUpData_.isEnclosingScope(); |
| 1601 |
} |
| 1602 |
|
| 1603 |
Scope* enclosingScope() const; |
| 1604 |
void setEnclosingScope(Scope* enclosingScope); |
| 1605 |
Scope* releaseEnclosingScope(); |
| 1606 |
|
| 1607 |
bool hasJitScript() const { return warmUpData_.isJitScript(); } |
| 1608 |
jit::JitScript* jitScript() const { |
| 1609 |
MOZ_ASSERT(hasJitScript()); |
| 1610 |
return warmUpData_.toJitScript(); |
| 1611 |
} |
| 1612 |
jit::JitScript* maybeJitScript() const { |
| 1613 |
return hasJitScript() ? jitScript() : nullptr; |
| 1614 |
} |
| 1615 |
|
| 1616 |
inline bool hasBaselineScript() const; |
| 1617 |
inline bool hasIonScript() const; |
| 1618 |
|
| 1619 |
bool hasPrivateScriptData() const { return data_ != nullptr; } |
| 1620 |
|
| 1621 |
// Update data_ pointer while also informing GC MemoryUse tracking. |
| 1622 |
void swapData(UniquePtr<PrivateScriptData>& other); |
| 1623 |
|
| 1624 |
mozilla::Span<const JS::GCCellPtr> gcthings() const { |
| 1625 |
return data_ ? data_->gcthings() : mozilla::Span<JS::GCCellPtr>(); |
| 1626 |
} |
| 1627 |
|
| 1628 |
// NOTE: This is only used to initialize a fresh script. |
| 1629 |
mozilla::Span<JS::GCCellPtr> gcthingsForInit() { |
| 1630 |
MOZ_ASSERT(!hasBytecode()); |
| 1631 |
return data_ ? data_->gcthings() : mozilla::Span<JS::GCCellPtr>(); |
| 1632 |
} |
| 1633 |
|
| 1634 |
void setMemberInitializers(MemberInitializers memberInitializers) { |
| 1635 |
MOZ_ASSERT(useMemberInitializers()); |
| 1636 |
MOZ_ASSERT(data_); |
| 1637 |
data_->setMemberInitializers(memberInitializers); |
| 1638 |
} |
| 1639 |
const MemberInitializers& getMemberInitializers() const { |
| 1640 |
MOZ_ASSERT(data_); |
| 1641 |
return data_->getMemberInitializers(); |
| 1642 |
} |
| 1643 |
|
| 1644 |
SharedImmutableScriptData* sharedData() const { return sharedData_; } |
| 1645 |
void initSharedData(SharedImmutableScriptData* data) { |
| 1646 |
MOZ_ASSERT(sharedData_ == nullptr); |
| 1647 |
sharedData_ = data; |
| 1648 |
} |
| 1649 |
void freeSharedData() { sharedData_ = nullptr; } |
| 1650 |
|
| 1651 |
// NOTE: Script only has bytecode if JSScript::fullyInitFromStencil completes |
| 1652 |
// successfully. |
| 1653 |
bool hasBytecode() const { |
| 1654 |
if (sharedData_) { |
| 1655 |
MOZ_ASSERT(data_); |
| 1656 |
MOZ_ASSERT(warmUpData_.isWarmUpCount() || warmUpData_.isJitScript()); |
| 1657 |
return true; |
| 1658 |
} |
| 1659 |
return false; |
| 1660 |
} |
| 1661 |
|
| 1662 |
public: |
| 1663 |
static const JS::TraceKind TraceKind = JS::TraceKind::Script; |
| 1664 |
|
| 1665 |
void traceChildren(JSTracer* trc); |
| 1666 |
void finalize(JS::GCContext* gcx); |
| 1667 |
|
| 1668 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { |
| 1669 |
return mallocSizeOf(data_); |
| 1670 |
} |
| 1671 |
|
| 1672 |
inline JSScript* asJSScript(); |
| 1673 |
|
| 1674 |
// JIT accessors |
| 1675 |
static constexpr size_t offsetOfJitCodeRaw() { return offsetOfHeaderPtr(); } |
| 1676 |
static constexpr size_t offsetOfPrivateData() { |
| 1677 |
return offsetof(BaseScript, data_); |
| 1678 |
} |
| 1679 |
static constexpr size_t offsetOfSharedData() { |
| 1680 |
return offsetof(BaseScript, sharedData_); |
| 1681 |
} |
| 1682 |
static size_t offsetOfImmutableFlags() { |
| 1683 |
static_assert(sizeof(ImmutableScriptFlags) == sizeof(uint32_t)); |
| 1684 |
return offsetof(BaseScript, immutableFlags_); |
| 1685 |
} |
| 1686 |
static constexpr size_t offsetOfMutableFlags() { |
| 1687 |
static_assert(sizeof(MutableScriptFlags) == sizeof(uint32_t)); |
| 1688 |
return offsetof(BaseScript, mutableFlags_); |
| 1689 |
} |
| 1690 |
static constexpr size_t offsetOfWarmUpData() { |
| 1691 |
return offsetof(BaseScript, warmUpData_); |
| 1692 |
} |
| 1693 |
|
| 1694 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1695 |
void dumpStringContent(js::GenericPrinter& out) const; |
| 1696 |
#endif |
| 1697 |
}; |
| 1698 |
|
| 1699 |
extern void SweepScriptData(JSRuntime* rt); |
| 1700 |
|
| 1701 |
} /* namespace js */ |
| 1702 |
|
| 1703 |
class JSScript : public js::BaseScript { |
| 1704 |
private: |
| 1705 |
friend bool js::PrivateScriptData::InitFromStencil( |
| 1706 |
JSContext* cx, js::HandleScript script, |
| 1707 |
const js::frontend::CompilationAtomCache& atomCache, |
| 1708 |
const js::frontend::CompilationStencil& stencil, |
| 1709 |
js::frontend::CompilationGCOutput& gcOutput, |
| 1710 |
const js::frontend::ScriptIndex scriptIndex); |
| 1711 |
|
| 1712 |
private: |
| 1713 |
using js::BaseScript::BaseScript; |
| 1714 |
|
| 1715 |
public: |
| 1716 |
static JSScript* Create(JSContext* cx, JS::Handle<JSFunction*> function, |
| 1717 |
JS::Handle<js::ScriptSourceObject*> sourceObject, |
| 1718 |
const js::SourceExtent& extent, |
| 1719 |
js::ImmutableScriptFlags flags); |
| 1720 |
|
| 1721 |
// NOTE: This should only be used while delazifying. |
| 1722 |
static JSScript* CastFromLazy(js::BaseScript* lazy) { |
| 1723 |
return static_cast<JSScript*>(lazy); |
| 1724 |
} |
| 1725 |
|
| 1726 |
// NOTE: If you use createPrivateScriptData directly instead of via |
| 1727 |
// fullyInitFromStencil, you are responsible for notifying the debugger |
| 1728 |
// after successfully creating the script. |
| 1729 |
static bool createPrivateScriptData(JSContext* cx, |
| 1730 |
JS::Handle<JSScript*> script, |
| 1731 |
uint32_t ngcthings); |
| 1732 |
|
| 1733 |
public: |
| 1734 |
static bool fullyInitFromStencil( |
| 1735 |
JSContext* cx, const js::frontend::CompilationAtomCache& atomCache, |
| 1736 |
const js::frontend::CompilationStencil& stencil, |
| 1737 |
js::frontend::CompilationGCOutput& gcOutput, js::HandleScript script, |
| 1738 |
const js::frontend::ScriptIndex scriptIndex); |
| 1739 |
|
| 1740 |
// Allocate a JSScript and initialize it with bytecode. This consumes |
| 1741 |
// allocations within the stencil. |
| 1742 |
static JSScript* fromStencil(JSContext* cx, |
| 1743 |
js::frontend::CompilationAtomCache& atomCache, |
| 1744 |
const js::frontend::CompilationStencil& stencil, |
| 1745 |
js::frontend::CompilationGCOutput& gcOutput, |
| 1746 |
js::frontend::ScriptIndex scriptIndex); |
| 1747 |
|
| 1748 |
#ifdef DEBUG |
| 1749 |
private: |
| 1750 |
// Assert that jump targets are within the code array of the script. |
| 1751 |
void assertValidJumpTargets() const; |
| 1752 |
#endif |
| 1753 |
|
| 1754 |
public: |
| 1755 |
js::ImmutableScriptData* immutableScriptData() const { |
| 1756 |
return sharedData_->get(); |
| 1757 |
} |
| 1758 |
|
| 1759 |
// Script bytecode is immutable after creation. |
| 1760 |
jsbytecode* code() const { |
| 1761 |
if (!sharedData_) { |
| 1762 |
return nullptr; |
| 1763 |
} |
| 1764 |
return immutableScriptData()->code(); |
| 1765 |
} |
| 1766 |
|
| 1767 |
bool hasForceInterpreterOp() const { |
| 1768 |
// JSOp::ForceInterpreter, if present, must be the first op. |
| 1769 |
MOZ_ASSERT(length() >= 1); |
| 1770 |
return JSOp(*code()) == JSOp::ForceInterpreter; |
| 1771 |
} |
| 1772 |
|
| 1773 |
js::AllBytecodesIterable allLocations() { |
| 1774 |
return js::AllBytecodesIterable(this); |
| 1775 |
} |
| 1776 |
|
| 1777 |
js::BytecodeLocation location() { return js::BytecodeLocation(this, code()); } |
| 1778 |
|
| 1779 |
size_t length() const { |
| 1780 |
MOZ_ASSERT(sharedData_); |
| 1781 |
return immutableScriptData()->codeLength(); |
| 1782 |
} |
| 1783 |
|
| 1784 |
jsbytecode* codeEnd() const { return code() + length(); } |
| 1785 |
|
| 1786 |
jsbytecode* lastPC() const { |
| 1787 |
jsbytecode* pc = codeEnd() - js::JSOpLength_RetRval; |
| 1788 |
MOZ_ASSERT(JSOp(*pc) == JSOp::RetRval || JSOp(*pc) == JSOp::Return); |
| 1789 |
return pc; |
| 1790 |
} |
| 1791 |
|
| 1792 |
// Note: ArgBytes is optional, but if specified then containsPC will also |
| 1793 |
// check that the opcode arguments are in bounds. |
| 1794 |
template <size_t ArgBytes = 0> |
| 1795 |
bool containsPC(const jsbytecode* pc) const { |
| 1796 |
MOZ_ASSERT_IF(ArgBytes, |
| 1797 |
js::GetBytecodeLength(pc) == sizeof(jsbytecode) + ArgBytes); |
| 1798 |
const jsbytecode* lastByte = pc + ArgBytes; |
| 1799 |
return pc >= code() && lastByte < codeEnd(); |
| 1800 |
} |
| 1801 |
template <typename ArgType> |
| 1802 |
bool containsPC(const jsbytecode* pc) const { |
| 1803 |
return containsPC<sizeof(ArgType)>(pc); |
| 1804 |
} |
| 1805 |
|
| 1806 |
bool contains(const js::BytecodeLocation& loc) const { |
| 1807 |
return containsPC(loc.toRawBytecode()); |
| 1808 |
} |
| 1809 |
|
| 1810 |
size_t pcToOffset(const jsbytecode* pc) const { |
| 1811 |
MOZ_ASSERT(containsPC(pc)); |
| 1812 |
return size_t(pc - code()); |
| 1813 |
} |
| 1814 |
|
| 1815 |
jsbytecode* offsetToPC(size_t offset) const { |
| 1816 |
MOZ_ASSERT(offset < length()); |
| 1817 |
return code() + offset; |
| 1818 |
} |
| 1819 |
|
| 1820 |
size_t mainOffset() const { return immutableScriptData()->mainOffset; } |
| 1821 |
|
| 1822 |
// The fixed part of a stack frame is comprised of vars (in function and |
| 1823 |
// module code) and block-scoped locals (in all kinds of code). |
| 1824 |
size_t nfixed() const { return immutableScriptData()->nfixed; } |
| 1825 |
|
| 1826 |
// Number of fixed slots reserved for slots that are always live. Only |
| 1827 |
// nonzero for function or module code. |
| 1828 |
size_t numAlwaysLiveFixedSlots() const; |
| 1829 |
|
| 1830 |
// Calculate the number of fixed slots that are live at a particular bytecode. |
| 1831 |
size_t calculateLiveFixed(jsbytecode* pc); |
| 1832 |
|
| 1833 |
size_t nslots() const { return immutableScriptData()->nslots; } |
| 1834 |
|
| 1835 |
unsigned numArgs() const; |
| 1836 |
|
| 1837 |
inline js::Shape* initialEnvironmentShape() const; |
| 1838 |
|
| 1839 |
bool functionHasParameterExprs() const; |
| 1840 |
|
| 1841 |
bool functionAllowsParameterRedeclaration() const { |
| 1842 |
// Parameter redeclaration is only allowed for non-strict functions with |
| 1843 |
// simple parameter lists, which are neither arrow nor method functions. We |
| 1844 |
// don't have a flag at hand to test the function kind, but we can still |
| 1845 |
// test if the function is non-strict and has a simple parameter list by |
| 1846 |
// checking |hasMappedArgsObj()|. (Mapped arguments objects are only |
| 1847 |
// created for non-strict functions with simple parameter lists.) |
| 1848 |
return hasMappedArgsObj(); |
| 1849 |
} |
| 1850 |
|
| 1851 |
size_t numICEntries() const { return immutableScriptData()->numICEntries; } |
| 1852 |
|
| 1853 |
size_t funLength() const { return immutableScriptData()->funLength; } |
| 1854 |
|
| 1855 |
void cacheForEval() { |
| 1856 |
MOZ_ASSERT(isForEval()); |
| 1857 |
// IsEvalCacheCandidate will make sure that there's nothing in this |
| 1858 |
// script that would prevent reexecution even if isRunOnce is |
| 1859 |
// true. So just pretend like we never ran this script. |
| 1860 |
clearFlag(MutableFlags::HasRunOnce); |
| 1861 |
} |
| 1862 |
|
| 1863 |
/* |
| 1864 |
* Arguments access (via JSOp::*Arg* opcodes) must access the canonical |
| 1865 |
* location for the argument. If an arguments object exists AND it's mapped |
| 1866 |
* ('arguments' aliases formals), then all access must go through the |
| 1867 |
* arguments object. Otherwise, the local slot is the canonical location for |
| 1868 |
* the arguments. Note: if a formal is aliased through the scope chain, then |
| 1869 |
* script->formalIsAliased and JSOp::*Arg* opcodes won't be emitted at all. |
| 1870 |
*/ |
| 1871 |
bool argsObjAliasesFormals() const { |
| 1872 |
return needsArgsObj() && hasMappedArgsObj(); |
| 1873 |
} |
| 1874 |
|
| 1875 |
void updateJitCodeRaw(JSRuntime* rt); |
| 1876 |
|
| 1877 |
bool isModule() const; |
| 1878 |
js::ModuleObject* module() const; |
| 1879 |
|
| 1880 |
bool isGlobalCode() const; |
| 1881 |
|
| 1882 |
// Returns true if the script may read formal arguments on the stack |
| 1883 |
// directly, via lazy arguments or a rest parameter. |
| 1884 |
bool mayReadFrameArgsDirectly(); |
| 1885 |
|
| 1886 |
static JSLinearString* sourceData(JSContext* cx, JS::HandleScript script); |
| 1887 |
|
| 1888 |
#ifdef MOZ_VTUNE |
| 1889 |
// Unique Method ID passed to the VTune profiler. Allows attribution of |
| 1890 |
// different jitcode to the same source script. |
| 1891 |
uint32_t vtuneMethodID(); |
| 1892 |
#endif |
| 1893 |
|
| 1894 |
public: |
| 1895 |
/* Return whether this is a 'direct eval' script in a function scope. */ |
| 1896 |
bool isDirectEvalInFunction() const; |
| 1897 |
|
| 1898 |
/* |
| 1899 |
* Return whether this script is a top-level script. |
| 1900 |
* |
| 1901 |
* If we evaluate some code which contains a syntax error, then we might |
| 1902 |
* produce a JSScript which has no associated bytecode. Testing with |
| 1903 |
* |code()| filters out this kind of scripts. |
| 1904 |
* |
| 1905 |
* If this script has a function associated to it, then it is not the |
| 1906 |
* top-level of a file. |
| 1907 |
*/ |
| 1908 |
bool isTopLevel() { return code() && !isFunction(); } |
| 1909 |
|
| 1910 |
/* Ensure the script has a JitScript. */ |
| 1911 |
inline bool ensureHasJitScript(JSContext* cx, js::jit::AutoKeepJitScripts&); |
| 1912 |
|
| 1913 |
void maybeReleaseJitScript(JS::GCContext* gcx); |
| 1914 |
void releaseJitScript(JS::GCContext* gcx); |
| 1915 |
void releaseJitScriptOnFinalize(JS::GCContext* gcx); |
| 1916 |
|
| 1917 |
inline js::jit::BaselineScript* baselineScript() const; |
| 1918 |
inline js::jit::IonScript* ionScript() const; |
| 1919 |
|
| 1920 |
inline bool isIonCompilingOffThread() const; |
| 1921 |
inline bool canIonCompile() const; |
| 1922 |
inline void disableIon(); |
| 1923 |
|
| 1924 |
inline bool isBaselineCompilingOffThread() const; |
| 1925 |
inline bool canBaselineCompile() const; |
| 1926 |
inline void disableBaselineCompile(); |
| 1927 |
|
| 1928 |
inline js::GlobalObject& global() const; |
| 1929 |
inline bool hasGlobal(const js::GlobalObject* global) const; |
| 1930 |
js::GlobalObject& uninlinedGlobal() const; |
| 1931 |
|
| 1932 |
js::GCThingIndex bodyScopeIndex() const { |
| 1933 |
return immutableScriptData()->bodyScopeIndex; |
| 1934 |
} |
| 1935 |
|
| 1936 |
js::Scope* bodyScope() const { return getScope(bodyScopeIndex()); } |
| 1937 |
|
| 1938 |
js::Scope* outermostScope() const { |
| 1939 |
// The body scope may not be the outermost scope in the script when |
| 1940 |
// the decl env scope is present. |
| 1941 |
return getScope(js::GCThingIndex::outermostScopeIndex()); |
| 1942 |
} |
| 1943 |
|
| 1944 |
bool functionHasExtraBodyVarScope() const { |
| 1945 |
bool res = BaseScript::functionHasExtraBodyVarScope(); |
| 1946 |
MOZ_ASSERT_IF(res, functionHasParameterExprs()); |
| 1947 |
return res; |
| 1948 |
} |
| 1949 |
|
| 1950 |
js::VarScope* functionExtraBodyVarScope() const; |
| 1951 |
|
| 1952 |
bool needsBodyEnvironment() const; |
| 1953 |
|
| 1954 |
inline js::LexicalScope* maybeNamedLambdaScope() const; |
| 1955 |
|
| 1956 |
// Drop script data and reset warmUpData to reference enclosing scope. |
| 1957 |
void relazify(JSRuntime* rt); |
| 1958 |
|
| 1959 |
private: |
| 1960 |
bool createJitScript(JSContext* cx); |
| 1961 |
|
| 1962 |
bool shareScriptData(JSContext* cx); |
| 1963 |
|
| 1964 |
public: |
| 1965 |
inline uint32_t getWarmUpCount() const; |
| 1966 |
inline void incWarmUpCounter(); |
| 1967 |
inline void resetWarmUpCounterForGC(); |
| 1968 |
|
| 1969 |
inline void updateLastICStubCounter(); |
| 1970 |
inline uint32_t warmUpCountAtLastICStub() const; |
| 1971 |
|
| 1972 |
void resetWarmUpCounterToDelayIonCompilation(); |
| 1973 |
|
| 1974 |
unsigned getWarmUpResetCount() const { |
| 1975 |
constexpr uint32_t MASK = uint32_t(MutableFlags::WarmupResets_MASK); |
| 1976 |
return mutableFlags_ & MASK; |
| 1977 |
} |
| 1978 |
void incWarmUpResetCounter() { |
| 1979 |
constexpr uint32_t MASK = uint32_t(MutableFlags::WarmupResets_MASK); |
| 1980 |
uint32_t newCount = getWarmUpResetCount() + 1; |
| 1981 |
if (newCount <= MASK) { |
| 1982 |
mutableFlags_ &= ~MASK; |
| 1983 |
mutableFlags_ |= newCount; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
void resetWarmUpResetCounter() { |
| 1987 |
constexpr uint32_t MASK = uint32_t(MutableFlags::WarmupResets_MASK); |
| 1988 |
mutableFlags_ &= ~MASK; |
| 1989 |
} |
| 1990 |
|
| 1991 |
public: |
| 1992 |
bool initScriptCounts(JSContext* cx); |
| 1993 |
js::ScriptCounts& getScriptCounts(); |
| 1994 |
js::PCCounts* maybeGetPCCounts(jsbytecode* pc); |
| 1995 |
const js::PCCounts* maybeGetThrowCounts(jsbytecode* pc); |
| 1996 |
js::PCCounts* getThrowCounts(jsbytecode* pc); |
| 1997 |
uint64_t getHitCount(jsbytecode* pc); |
| 1998 |
void addIonCounts(js::jit::IonScriptCounts* ionCounts); |
| 1999 |
js::jit::IonScriptCounts* getIonCounts(); |
| 2000 |
void releaseScriptCounts(js::ScriptCounts* counts); |
| 2001 |
void destroyScriptCounts(); |
| 2002 |
void resetScriptCounts(); |
| 2003 |
|
| 2004 |
jsbytecode* main() const { return code() + mainOffset(); } |
| 2005 |
|
| 2006 |
js::BytecodeLocation mainLocation() const { |
| 2007 |
return js::BytecodeLocation(this, main()); |
| 2008 |
} |
| 2009 |
|
| 2010 |
js::BytecodeLocation endLocation() const { |
| 2011 |
return js::BytecodeLocation(this, codeEnd()); |
| 2012 |
} |
| 2013 |
|
| 2014 |
js::BytecodeLocation offsetToLocation(uint32_t offset) const { |
| 2015 |
return js::BytecodeLocation(this, offsetToPC(offset)); |
| 2016 |
} |
| 2017 |
|
| 2018 |
void addSizeOfJitScript(mozilla::MallocSizeOf mallocSizeOf, |
| 2019 |
size_t* sizeOfJitScript, |
| 2020 |
size_t* sizeOfAllocSites) const; |
| 2021 |
|
| 2022 |
mozilla::Span<const js::TryNote> trynotes() const { |
| 2023 |
return immutableScriptData()->tryNotes(); |
| 2024 |
} |
| 2025 |
|
| 2026 |
mozilla::Span<const js::ScopeNote> scopeNotes() const { |
| 2027 |
return immutableScriptData()->scopeNotes(); |
| 2028 |
} |
| 2029 |
|
| 2030 |
mozilla::Span<const uint32_t> resumeOffsets() const { |
| 2031 |
return immutableScriptData()->resumeOffsets(); |
| 2032 |
} |
| 2033 |
|
| 2034 |
uint32_t tableSwitchCaseOffset(jsbytecode* pc, uint32_t caseIndex) const { |
| 2035 |
MOZ_ASSERT(containsPC(pc)); |
| 2036 |
MOZ_ASSERT(JSOp(*pc) == JSOp::TableSwitch); |
| 2037 |
uint32_t firstResumeIndex = GET_RESUMEINDEX(pc + 3 * JUMP_OFFSET_LEN); |
| 2038 |
return resumeOffsets()[firstResumeIndex + caseIndex]; |
| 2039 |
} |
| 2040 |
jsbytecode* tableSwitchCasePC(jsbytecode* pc, uint32_t caseIndex) const { |
| 2041 |
return offsetToPC(tableSwitchCaseOffset(pc, caseIndex)); |
| 2042 |
} |
| 2043 |
|
| 2044 |
bool hasLoops(); |
| 2045 |
|
| 2046 |
uint32_t numNotes() const { |
| 2047 |
MOZ_ASSERT(sharedData_); |
| 2048 |
return immutableScriptData()->noteLength(); |
| 2049 |
} |
| 2050 |
js::SrcNote* notes() const { |
| 2051 |
MOZ_ASSERT(sharedData_); |
| 2052 |
return immutableScriptData()->notes(); |
| 2053 |
} |
| 2054 |
js::SrcNote* notesEnd() const { |
| 2055 |
MOZ_ASSERT(sharedData_); |
| 2056 |
return immutableScriptData()->notes() + numNotes(); |
| 2057 |
} |
| 2058 |
|
| 2059 |
JSString* getString(js::GCThingIndex index) const { |
| 2060 |
return &gcthings()[index].as<JSString>(); |
| 2061 |
} |
| 2062 |
|
| 2063 |
JSString* getString(jsbytecode* pc) const { |
| 2064 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2065 |
MOZ_ASSERT(js::JOF_OPTYPE((JSOp)*pc) == JOF_STRING); |
| 2066 |
return getString(GET_GCTHING_INDEX(pc)); |
| 2067 |
} |
| 2068 |
|
| 2069 |
bool atomizeString(JSContext* cx, jsbytecode* pc) { |
| 2070 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2071 |
MOZ_ASSERT(js::JOF_OPTYPE((JSOp)*pc) == JOF_STRING); |
| 2072 |
js::GCThingIndex index = GET_GCTHING_INDEX(pc); |
| 2073 |
JSString* str = getString(index); |
| 2074 |
if (str->isAtom()) { |
| 2075 |
return true; |
| 2076 |
} |
| 2077 |
JSAtom* atom = js::AtomizeString(cx, str); |
| 2078 |
if (!atom) { |
| 2079 |
return false; |
| 2080 |
} |
| 2081 |
js::gc::CellPtrPreWriteBarrier(data_->gcthings()[index]); |
| 2082 |
data_->gcthings()[index] = JS::GCCellPtr(atom); |
| 2083 |
return true; |
| 2084 |
} |
| 2085 |
|
| 2086 |
JSAtom* getAtom(js::GCThingIndex index) const { |
| 2087 |
return &gcthings()[index].as<JSString>().asAtom(); |
| 2088 |
} |
| 2089 |
|
| 2090 |
JSAtom* getAtom(jsbytecode* pc) const { |
| 2091 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2092 |
MOZ_ASSERT(js::JOF_OPTYPE((JSOp)*pc) == JOF_ATOM); |
| 2093 |
return getAtom(GET_GCTHING_INDEX(pc)); |
| 2094 |
} |
| 2095 |
|
| 2096 |
js::PropertyName* getName(js::GCThingIndex index) { |
| 2097 |
return getAtom(index)->asPropertyName(); |
| 2098 |
} |
| 2099 |
|
| 2100 |
js::PropertyName* getName(jsbytecode* pc) const { |
| 2101 |
return getAtom(pc)->asPropertyName(); |
| 2102 |
} |
| 2103 |
|
| 2104 |
JSObject* getObject(js::GCThingIndex index) const { |
| 2105 |
MOZ_ASSERT(gcthings()[index].asCell()->isTenured()); |
| 2106 |
return &gcthings()[index].as<JSObject>(); |
| 2107 |
} |
| 2108 |
|
| 2109 |
JSObject* getObject(const jsbytecode* pc) const { |
| 2110 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2111 |
return getObject(GET_GCTHING_INDEX(pc)); |
| 2112 |
} |
| 2113 |
|
| 2114 |
js::SharedShape* getShape(js::GCThingIndex index) const { |
| 2115 |
return &gcthings()[index].as<js::Shape>().asShared(); |
| 2116 |
} |
| 2117 |
|
| 2118 |
js::SharedShape* getShape(const jsbytecode* pc) const { |
| 2119 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2120 |
return getShape(GET_GCTHING_INDEX(pc)); |
| 2121 |
} |
| 2122 |
|
| 2123 |
js::Scope* getScope(js::GCThingIndex index) const { |
| 2124 |
return &gcthings()[index].as<js::Scope>(); |
| 2125 |
} |
| 2126 |
|
| 2127 |
js::Scope* getScope(jsbytecode* pc) const { |
| 2128 |
// This method is used to get a scope directly using a JSOp with an |
| 2129 |
// index. To search through ScopeNotes to look for a Scope using pc, |
| 2130 |
// use lookupScope. |
| 2131 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2132 |
MOZ_ASSERT(js::JOF_OPTYPE(JSOp(*pc)) == JOF_SCOPE, |
| 2133 |
"Did you mean to use lookupScope(pc)?"); |
| 2134 |
return getScope(GET_GCTHING_INDEX(pc)); |
| 2135 |
} |
| 2136 |
|
| 2137 |
inline JSFunction* getFunction(js::GCThingIndex index) const; |
| 2138 |
inline JSFunction* getFunction(jsbytecode* pc) const; |
| 2139 |
|
| 2140 |
inline js::RegExpObject* getRegExp(js::GCThingIndex index) const; |
| 2141 |
inline js::RegExpObject* getRegExp(jsbytecode* pc) const; |
| 2142 |
|
| 2143 |
js::BigInt* getBigInt(js::GCThingIndex index) const { |
| 2144 |
MOZ_ASSERT(gcthings()[index].asCell()->isTenured()); |
| 2145 |
return &gcthings()[index].as<js::BigInt>(); |
| 2146 |
} |
| 2147 |
|
| 2148 |
js::BigInt* getBigInt(jsbytecode* pc) const { |
| 2149 |
MOZ_ASSERT(containsPC<js::GCThingIndex>(pc)); |
| 2150 |
MOZ_ASSERT(js::JOF_OPTYPE(JSOp(*pc)) == JOF_BIGINT); |
| 2151 |
return getBigInt(GET_GCTHING_INDEX(pc)); |
| 2152 |
} |
| 2153 |
|
| 2154 |
// The following 3 functions find the static scope just before the |
| 2155 |
// execution of the instruction pointed to by pc. |
| 2156 |
|
| 2157 |
js::Scope* lookupScope(const jsbytecode* pc) const; |
| 2158 |
|
| 2159 |
js::Scope* innermostScope(const jsbytecode* pc) const; |
| 2160 |
js::Scope* innermostScope() const { return innermostScope(main()); } |
| 2161 |
|
| 2162 |
/* |
| 2163 |
* The isEmpty method tells whether this script has code that computes any |
| 2164 |
* result (not return value, result AKA normal completion value) other than |
| 2165 |
* JSVAL_VOID, or any other effects. |
| 2166 |
*/ |
| 2167 |
bool isEmpty() const { |
| 2168 |
if (length() > 3) { |
| 2169 |
return false; |
| 2170 |
} |
| 2171 |
|
| 2172 |
jsbytecode* pc = code(); |
| 2173 |
if (noScriptRval() && JSOp(*pc) == JSOp::False) { |
| 2174 |
++pc; |
| 2175 |
} |
| 2176 |
return JSOp(*pc) == JSOp::RetRval; |
| 2177 |
} |
| 2178 |
|
| 2179 |
bool formalIsAliased(unsigned argSlot); |
| 2180 |
bool anyFormalIsForwarded(); |
| 2181 |
bool formalLivesInArgumentsObject(unsigned argSlot); |
| 2182 |
|
| 2183 |
// See comment above 'debugMode' in Realm.h for explanation of |
| 2184 |
// invariants of debuggee compartments, scripts, and frames. |
| 2185 |
inline bool isDebuggee() const; |
| 2186 |
|
| 2187 |
// A helper class to prevent relazification of the given function's script |
| 2188 |
// while it's holding on to it. This class automatically roots the script. |
| 2189 |
class AutoDelazify; |
| 2190 |
friend class AutoDelazify; |
| 2191 |
|
| 2192 |
class AutoDelazify { |
| 2193 |
JS::RootedScript script_; |
| 2194 |
JSContext* cx_; |
| 2195 |
bool oldAllowRelazify_ = false; |
| 2196 |
|
| 2197 |
public: |
| 2198 |
explicit AutoDelazify(JSContext* cx, JS::HandleFunction fun = nullptr) |
| 2199 |
: script_(cx), cx_(cx) { |
| 2200 |
holdScript(fun); |
| 2201 |
} |
| 2202 |
|
| 2203 |
~AutoDelazify() { dropScript(); } |
| 2204 |
|
| 2205 |
void operator=(JS::HandleFunction fun) { |
| 2206 |
dropScript(); |
| 2207 |
holdScript(fun); |
| 2208 |
} |
| 2209 |
|
| 2210 |
operator JS::HandleScript() const { return script_; } |
| 2211 |
explicit operator bool() const { return script_; } |
| 2212 |
|
| 2213 |
private: |
| 2214 |
void holdScript(JS::HandleFunction fun); |
| 2215 |
void dropScript(); |
| 2216 |
}; |
| 2217 |
|
| 2218 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 2219 |
public: |
| 2220 |
struct DumpOptions { |
| 2221 |
bool recursive = false; |
| 2222 |
bool runtimeData = false; |
| 2223 |
}; |
| 2224 |
|
| 2225 |
void dump(JSContext* cx); |
| 2226 |
void dumpRecursive(JSContext* cx); |
| 2227 |
|
| 2228 |
static bool dump(JSContext* cx, JS::Handle<JSScript*> script, |
| 2229 |
DumpOptions& options, js::StringPrinter* sp); |
| 2230 |
static bool dumpSrcNotes(JSContext* cx, JS::Handle<JSScript*> script, |
| 2231 |
js::GenericPrinter* sp); |
| 2232 |
static bool dumpTryNotes(JSContext* cx, JS::Handle<JSScript*> script, |
| 2233 |
js::GenericPrinter* sp); |
| 2234 |
static bool dumpScopeNotes(JSContext* cx, JS::Handle<JSScript*> script, |
| 2235 |
js::GenericPrinter* sp); |
| 2236 |
static bool dumpGCThings(JSContext* cx, JS::Handle<JSScript*> script, |
| 2237 |
js::GenericPrinter* sp); |
| 2238 |
#endif |
| 2239 |
}; |
| 2240 |
|
| 2241 |
namespace js { |
| 2242 |
|
| 2243 |
struct ScriptAndCounts { |
| 2244 |
/* This structure is stored and marked from the JSRuntime. */ |
| 2245 |
JSScript* script; |
| 2246 |
ScriptCounts scriptCounts; |
| 2247 |
|
| 2248 |
inline explicit ScriptAndCounts(JSScript* script); |
| 2249 |
inline ScriptAndCounts(ScriptAndCounts&& sac); |
| 2250 |
|
| 2251 |
const PCCounts* maybeGetPCCounts(jsbytecode* pc) const { |
| 2252 |
return scriptCounts.maybeGetPCCounts(script->pcToOffset(pc)); |
| 2253 |
} |
| 2254 |
const PCCounts* maybeGetThrowCounts(jsbytecode* pc) const { |
| 2255 |
return scriptCounts.maybeGetThrowCounts(script->pcToOffset(pc)); |
| 2256 |
} |
| 2257 |
|
| 2258 |
jit::IonScriptCounts* getIonCounts() const { return scriptCounts.ionCounts_; } |
| 2259 |
|
| 2260 |
void trace(JSTracer* trc) { |
| 2261 |
TraceRoot(trc, &script, "ScriptAndCounts::script"); |
| 2262 |
} |
| 2263 |
}; |
| 2264 |
|
| 2265 |
extern JS::UniqueChars FormatIntroducedFilename(const char* filename, |
| 2266 |
uint32_t lineno, |
| 2267 |
const char* introducer); |
| 2268 |
|
| 2269 |
extern jsbytecode* LineNumberToPC(JSScript* script, unsigned lineno); |
| 2270 |
|
| 2271 |
extern JS_PUBLIC_API unsigned GetScriptLineExtent( |
| 2272 |
JSScript* script, JS::LimitedColumnNumberOneOrigin* columnp = nullptr); |
| 2273 |
|
| 2274 |
#ifdef JS_CACHEIR_SPEW |
| 2275 |
void maybeUpdateWarmUpCount(JSScript* script); |
| 2276 |
void maybeSpewScriptFinalWarmUpCount(JSScript* script); |
| 2277 |
#endif |
| 2278 |
|
| 2279 |
} /* namespace js */ |
| 2280 |
|
| 2281 |
namespace js { |
| 2282 |
|
| 2283 |
extern unsigned PCToLineNumber( |
| 2284 |
JSScript* script, jsbytecode* pc, |
| 2285 |
JS::LimitedColumnNumberOneOrigin* columnp = nullptr); |
| 2286 |
|
| 2287 |
extern unsigned PCToLineNumber( |
| 2288 |
unsigned startLine, JS::LimitedColumnNumberOneOrigin startCol, |
| 2289 |
SrcNote* notes, SrcNote* notesEnd, jsbytecode* code, jsbytecode* pc, |
| 2290 |
JS::LimitedColumnNumberOneOrigin* columnp = nullptr); |
| 2291 |
|
| 2292 |
/* |
| 2293 |
* This function returns the file and line number of the script currently |
| 2294 |
* executing on cx. If there is no current script executing on cx (e.g., a |
| 2295 |
* native called directly through JSAPI (e.g., by setTimeout)), nullptr and 0 |
| 2296 |
* are returned as the file and line. |
| 2297 |
*/ |
| 2298 |
extern void DescribeScriptedCallerForCompilation( |
| 2299 |
JSContext* cx, MutableHandleScript maybeScript, const char** file, |
| 2300 |
uint32_t* linenop, uint32_t* pcOffset, bool* mutedErrors); |
| 2301 |
|
| 2302 |
/* |
| 2303 |
* Like DescribeScriptedCallerForCompilation, but this function avoids looking |
| 2304 |
* up the script/pc and the full linear scan to compute line number. |
| 2305 |
*/ |
| 2306 |
extern void DescribeScriptedCallerForDirectEval( |
| 2307 |
JSContext* cx, HandleScript script, jsbytecode* pc, const char** file, |
| 2308 |
uint32_t* linenop, uint32_t* pcOffset, bool* mutedErrors); |
| 2309 |
|
| 2310 |
bool CheckCompileOptionsMatch(const JS::ReadOnlyCompileOptions& options, |
| 2311 |
js::ImmutableScriptFlags flags); |
| 2312 |
|
| 2313 |
void FillImmutableFlagsFromCompileOptionsForTopLevel( |
| 2314 |
const JS::ReadOnlyCompileOptions& options, js::ImmutableScriptFlags& flags); |
| 2315 |
|
| 2316 |
void FillImmutableFlagsFromCompileOptionsForFunction( |
| 2317 |
const JS::ReadOnlyCompileOptions& options, js::ImmutableScriptFlags& flags); |
| 2318 |
|
| 2319 |
} /* namespace js */ |
| 2320 |
|
| 2321 |
namespace JS { |
| 2322 |
|
| 2323 |
template <> |
| 2324 |
struct GCPolicy<js::ScriptLCovEntry> |
| 2325 |
: public IgnoreGCPolicy<js::ScriptLCovEntry> {}; |
| 2326 |
|
| 2327 |
#ifdef JS_CACHEIR_SPEW |
| 2328 |
template <> |
| 2329 |
struct GCPolicy<js::ScriptFinalWarmUpCountEntry> |
| 2330 |
: public IgnoreGCPolicy<js::ScriptFinalWarmUpCountEntry> {}; |
| 2331 |
#endif |
| 2332 |
|
| 2333 |
namespace ubi { |
| 2334 |
|
| 2335 |
template <> |
| 2336 |
class Concrete<JSScript> : public Concrete<js::BaseScript> {}; |
| 2337 |
|
| 2338 |
} // namespace ubi |
| 2339 |
} // namespace JS |
| 2340 |
|
| 2341 |
#endif /* vm_JSScript_h */ |