forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathJSCellInlines.h
More file actions
561 lines (513 loc) · 24.8 KB
/
Copy pathJSCellInlines.h
File metadata and controls
561 lines (513 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* Copyright (C) 2012-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/Compiler.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#include "AllocatorForMode.h"
#include "AllocatorInlines.h"
#include "CPU.h"
#include "CallFrameInlines.h"
#include "CompleteSubspaceInlines.h"
#include "DeferGCInlines.h"
#include "FreeListInlines.h"
#include "Handle.h"
#include "HeapInlines.h"
#include "IsoSubspaceInlines.h"
#include "JSBigInt.h"
#include "JSCast.h"
#include "JSDestructibleObject.h"
#include "JSFunction.h"
#include "JSObject.h"
#include "JSString.h"
#include "LocalAllocatorInlines.h"
#include "MarkedBlock.h"
#include "SlotVisitorInlines.h"
#include "Structure.h"
#include "Symbol.h"
#include <wtf/CompilationThread.h>
namespace JSC {
inline JSCell::JSCell(CreatingEarlyCellTag)
{
// V7 (TSAN, cell-header family): ctor header init can race with foreign
// cellHeaderConcurrentLoad readers on a recycled cell (the cell address
// was previously published; stale-header readers re-dispatch per OM
// §3.0/GT#2). Store relaxed so the pair is atomic/atomic under TSAN;
// non-TSAN this is the identical plain store the init-list produced.
cellHeaderConcurrentStore(m_cellState, CellState::DefinitelyWhite);
ASSERT(!isCompilationThread());
}
inline JSCell::JSCell(VM&, Structure* structure)
: JSCell(CreatingWellDefinedBuiltinCell, structure->id(), structure->typeInfoBlob())
{
ASSERT(!isCompilationThread());
ASSERT(m_cellState == CellState::DefinitelyWhite);
// Note that in the constructor initializer list above, we are only using values
// inside structure but not necessarily the structure pointer itself. All these
// values are contained inside Structure::m_blob. Note also that this constructor
// is an inline function. Hence, the compiler may choose to pre-compute the address
// of structure->m_blob and discard the structure pointer itself. There's a chance
// that the GC may run while allocating this cell. In the event that the structure
// is newly instantiated just before calling this constructor, there may not be any
// other references to it. As a result, the structure may get collected before this
// cell is even constructed. To avoid this possibility, we need to ensure that the
// structure pointer is still alive at this point.
ensureStillAliveHere(structure);
static_assert(JSCell::atomSize >= MarkedBlock::atomSize);
}
// This constructor should not be used directly. Exceptions are for quite few well-defined builtin objects, e.g. JSString, empty JSFinalObject etc.
// Structure must be kept alive somehow (e.g. by JSGlobalObject, or ensureStillAliveHere).
#if TSAN_ENABLED && USE(JSVALUE64) && CPU(LITTLE_ENDIAN)
ALWAYS_INLINE JSCell::JSCell(CreatingWellDefinedBuiltinCellTag, StructureID structureID, uint32_t blob)
{
// V7 (TSAN, cell-header family): on a recycled cell this init races with
// foreign cellHeaderConcurrentLoad readers holding a stale reference
// (blessed reader side, OM §3.0/GT#2 — stale headers re-dispatch). Make
// the writer side atomic too: assemble the full 8-byte header
// {m_structureID, m_blob} and publish it with a single 64-bit relaxed
// store. TSAN-build-only path; the non-TSAN ctor below is the original
// member-init-list (zero codegen delta flag-off).
//
// Recorded residual: m_structureID is omitted from the mem-init-list, so
// its NSDMI default-init still emits a plain 4-byte zero store before
// this atomic store. It is dead (unconditionally overwritten by the
// covering atomic store) and DSE runs before the TSan instrumentation
// pass, so it is expected to vanish; if ctor frames persist in the next
// snapshot, that store is the cause and StructureID needs a no-init tag
// constructor (next wave).
static_assert(OBJECT_OFFSETOF(JSCell, m_structureID) + sizeof(StructureID) == OBJECT_OFFSETOF(JSCell, m_blob));
static_assert(sizeof(StructureID) == sizeof(uint32_t));
uint64_t header = static_cast<uint64_t>(structureID.bits()) | (static_cast<uint64_t>(blob) << 32);
__atomic_store_n(reinterpret_cast<uint64_t*>(&m_structureID), header, __ATOMIC_RELAXED);
}
#else
ALWAYS_INLINE JSCell::JSCell(CreatingWellDefinedBuiltinCellTag, StructureID structureID, uint32_t blob)
: m_structureID(structureID)
#if CPU(LITTLE_ENDIAN)
, m_blob(blob)
#else
, m_indexingTypeAndMisc(static_cast<uint8_t>(blob >> 24))
, m_type(std::bit_cast<JSType>(static_cast<uint8_t>(blob >> 16)))
, m_flags(std::bit_cast<TypeInfo::InlineTypeFlags>(static_cast<uint8_t>(blob >> 8)))
, m_cellState(std::bit_cast<CellState>(static_cast<uint8_t>(blob >> 0)))
#endif
{
}
#endif
inline void JSCell::finishCreation(VM& vm)
{
// This object is ready to be escaped so the concurrent GC may see it at any time. We have
// to make sure that none of our stores sink below here.
vm.mutatorFence();
#if ENABLE(GC_VALIDATION)
ASSERT(vm.isInitializingObject());
vm.setInitializingObjectClass(0);
#else
UNUSED_PARAM(vm);
#endif
ASSERT(m_structureID);
}
inline void JSCell::finishCreation(VM& vm, Structure* structure, CreatingEarlyCellTag)
{
#if ENABLE(GC_VALIDATION)
ASSERT(vm.isInitializingObject());
vm.setInitializingObjectClass(0);
if (structure) {
#endif
// V7 (TSAN, cell-header family): relaxed header stores pairing with
// cellHeaderConcurrentLoad readers; plain stores non-TSAN.
cellHeaderConcurrentStore(m_structureID, structure->id());
cellHeaderConcurrentStore(m_indexingTypeAndMisc, structure->indexingModeIncludingHistory());
cellHeaderConcurrentStore(m_type, structure->typeInfo().type());
cellHeaderConcurrentStore(m_flags, structure->typeInfo().inlineTypeFlags());
#if ENABLE(GC_VALIDATION)
}
#else
UNUSED_PARAM(vm);
#endif
// Very first set of allocations won't have a real structure.
ASSERT(m_structureID || !vm.structureStructure);
}
template<typename Visitor>
void JSCell::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
visitor.appendUnbarriered(cell->structure());
}
DEFINE_VISIT_CHILDREN_WITH_MODIFIER(inline, JSCell);
template<typename Visitor>
ALWAYS_INLINE void JSCell::visitOutputConstraintsImpl(JSCell*, Visitor&)
{
}
DEFINE_VISIT_OUTPUT_CONSTRAINTS_WITH_MODIFIER(inline, JSCell);
template<typename Type>
inline Allocator allocatorForConcurrently(VM& vm, size_t allocationSize, AllocatorForMode mode)
{
// IT-9 consumer (SPEC-ungil §B / I4, JIT-codegen leg; see
// Heap::allocationClientForJITCodegen's declaration comment, Heap.h):
// this is the single funnel every JIT emitter bakes a
// JITAllocator::constant from (DFG createOSREntries/NewObject/MakeRope,
// FTL allocateObject/MakeRope, AssemblyHelpers emitAllocateJSObject
// templates). GIL-off there is NO client whose LocalAllocator may be
// baked into an artifact: the artifact is executed by EVERY lite of the
// VM, and a baked per-client iso LocalAllocator makes N threads pop ONE
// FreeList unlocked (observed: JIT inline-allocation segfault under
// races/counter-lock.js — scrambled-head pop returned null past the
// empty check). CompleteSubspace already returns an empty Allocator
// under useSharedGCHeap (§5.5 server arrays never populated); this gate
// extends the same rule to the per-client ISO subspaces. Baking an
// empty Allocator makes every inline allocation take the slow path,
// which re-dispatches per-thread through allocationClientForCurrentThread
// at run time. Interim until U-T7 §B.4 item (1) (lite-relative TLC/iso
// emission) lands. GIL-on/flag-off: one predicted-false compiler-side
// branch; baked artifacts are byte-identical.
if (vm.gilOff()) [[unlikely]]
return { };
if (auto* subspace = subspaceForConcurrently<Type>(vm))
return subspace->allocatorFor(allocationSize, mode);
return { };
}
// H-VMLITE-TLCPTR (SPEC-heap §5.3/§B.4; GILOFF-TAX #1/#2): resolve the
// per-thread TLC flat-table SLOT a JIT inline-allocate emitter bakes for
// `allocationSize` in `subspace`. tlcIndexBase is per-subspace WRITE-ONCE
// (under directoryLock), so the slot is a process-wide constant once
// non-nullopt; the per-thread part is the lite-relative
// `tlcTable[slot * sizeof(Allocator)]` load the emitter generates against the
// VMLite mirror (offsetOfTlcTable / offsetOfTlcTableBound). Returns nullopt
// for: iso subspaces (never enter m_table — m_perDirectory lookup-only,
// §5.3); a subspace whose tlcIndexBase has not been reserved yet (first
// directory not created — the runtime slow path will reserve it, and a later
// recompile will see it); sizes past largeCutoff (precise path). GIL-off
// callers only (the call is reached through a vm.gilOff() codegen gate, so
// flag-off emission never evaluates it — flag-off byte-identity).
inline std::optional<unsigned> tlcSlotForSubspace(Subspace* subspace, size_t allocationSize)
{
if (!subspace || subspace->isIsoSubspace())
return std::nullopt;
if (allocationSize > MarkedSpace::largeCutoff)
return std::nullopt;
unsigned base = static_cast<CompleteSubspace*>(subspace)->tlcIndexBase();
if (base == BlockDirectory::invalidTlcIndex)
return std::nullopt;
return base + static_cast<unsigned>(MarkedSpace::sizeClassToIndex(allocationSize));
}
template<typename Type>
inline std::optional<unsigned> tlcSlotForConcurrently(VM& vm, size_t allocationSize)
{
auto* subspace = subspaceForConcurrently<Type>(vm);
// subspaceForConcurrently<Type> may yield CompleteSubspace*, IsoSubspace*,
// or GCClient::IsoSubspace* (NOT a JSC::Subspace) — the latter is the
// per-client iso allocator the IT-9 comment above forbids baking; it is
// never table-addressable.
if constexpr (std::is_convertible_v<decltype(subspace), Subspace*>)
return tlcSlotForSubspace(subspace, allocationSize);
else {
UNUSED_PARAM(subspace);
UNUSED_PARAM(allocationSize);
return std::nullopt;
}
}
template<typename T, AllocationFailureMode failureMode>
ALWAYS_INLINE void* tryAllocateCellHelper(VM& vm, size_t size, GCDeferralContext* deferralContext)
{
ASSERT(deferralContext || vm.heap.isDeferred() || !AssertNoGC::isInEffectOnCurrentThread());
ASSERT(size >= sizeof(T));
JSCell* result = static_cast<JSCell*>(subspaceFor<T>(vm)->allocate(vm, WTF::roundUpToMultipleOf<T::atomSize>(size), deferralContext, failureMode));
if constexpr (failureMode == AllocationFailureMode::ReturnNull) {
if (!result)
return nullptr;
}
#if ENABLE(GC_VALIDATION)
ASSERT_WITH_MESSAGE(
!vm.isInitializingObject(),
"Allocating JSCell while initializing an object is not allowed. Currently initializing '%s'\n"
"This means you either forgot `Base::finishCreation(...)` or you are actually allocating.",
vm.initializingObjectClass()->className.characters());
vm.setInitializingObjectClass(T::info());
#endif
result->clearStructure();
return result;
}
template<typename T>
void* allocateCell(VM& vm, size_t size)
{
return tryAllocateCellHelper<T, AllocationFailureMode::Assert>(vm, size, nullptr);
}
template<typename T>
void* tryAllocateCell(VM& vm, size_t size)
{
return tryAllocateCellHelper<T, AllocationFailureMode::ReturnNull>(vm, size, nullptr);
}
template<typename T>
void* allocateCell(VM& vm, GCDeferralContext* deferralContext, size_t size)
{
return tryAllocateCellHelper<T, AllocationFailureMode::Assert>(vm, size, deferralContext);
}
template<typename T>
void* tryAllocateCell(VM& vm, GCDeferralContext* deferralContext, size_t size)
{
return tryAllocateCellHelper<T, AllocationFailureMode::ReturnNull>(vm, size, deferralContext);
}
// H-CALLSITE-LASTSIZE-LA: WITHDRAWN (amender, post-refute). The mechanism is
// strictly dominated by H-TLS-TABLE already in this tree
// (CompleteSubspaceInlines.h): that change collapses the identical 3-hop
// (allocationClientForCurrentThread -> allocatorForSizeStep -> allocate) to
// two IE-TLS loads + one indexed load for EVERY CompleteSubspace::allocate
// call — a strict superset of the call sites this routed (JSCellButterfly,
// JSLexicalEnvironment, 5 Butterfly aux funnels all reach
// CompleteSubspace::allocate via tryAllocateCellHelper /
// vm.auxiliarySpace().allocate). The "subspace disambiguation" premise was
// wrong: H-TLS-TABLE keys on per-subspace tlcIndexBase(), so cellSpace sz=64
// and auxiliarySpace sz=64 resolve to distinct table slots. Against the
// combined tree the marginal hit-path win was {sizeStep TLS + compare} vs
// {tlcIndexBase load + bound IE-TLS + table[slot]}: a few cycles/cell, not
// the ~250ms attributed. Correctness defects that would survive amendment:
// (a) per-callsite static thread_local has NO invalidation hook at
// setCurrentThreadClient (A36C carrier swap / attach-detach,
// GCThreadLocalCache.cpp) — a same-OS-thread client swap leaves a stale
// LocalAllocator* → wrong-client pop / UAF after the prior client's
// GCThreadLocalCache teardown; adding a client-generation guard makes the hit
// path 3 TLS loads + 2 compares, worse than H-TLS-TABLE's IE-TLS path;
// (b) function-local static thread_local in an ALWAYS_INLINE template gets
// COMDAT/weak linkage with no tls_model attribute — risk of general-dynamic
// __tls_get_addr per allocation (the M2-alloc-tax-residual failure mode
// Heap.h documents), i.e. SLOWER than the path it replaces; (c) the hit path
// dropped the I2 hasHeapAccess() tripwire that H-TLS-TABLE was already
// required to restore (no-weakened-asserts gate). IsoSubspace types
// (JSRopeString 1.37%, JSArray/JSString/JSFunction 0.53% of the §41 T1
// bucket) were structurally unreachable regardless. No amendment yields a
// mechanism that independently removes T1 tax not already removed by
// H-TLS-TABLE; the routed call sites are reverted to their unmodified
// allocateCell / vm.auxiliarySpace().allocate forms so flag-off is
// byte-identical and GIL-off takes the H-TLS-TABLE fast path.
// FIXME: Consider making getCallData concurrency-safe once NPAPI support is removed.
// https://bugs.webkit.org/show_bug.cgi?id=215801
template<Concurrency concurrency>
ALWAYS_INLINE TriState JSCell::isCallableWithConcurrency()
{
if (!isObject())
return TriState::False;
// JSFunction and InternalFunction assert during construction that derived classes don't override getCallData,
// which guarantees that CallData::Type::None is never returned.
if (type() == JSFunctionType || type() == InternalFunctionType)
return TriState::True;
if (inlineTypeFlags() & OverridesGetCallData) {
if constexpr (concurrency == Concurrency::MainThread)
return (methodTable()->getCallData(this).type != CallData::Type::None) ? TriState::True : TriState::False;
return TriState::Indeterminate;
}
return TriState::False;
}
template<Concurrency concurrency>
inline TriState JSCell::isConstructorWithConcurrency()
{
if (!isObject())
return TriState::False;
if constexpr (concurrency == Concurrency::MainThread)
return (methodTable()->getConstructData(this).type != CallData::Type::None) ? TriState::True : TriState::False;
// We know that both getConstructData of both types are concurrency aware. Plus, derived classes of JSFunction and InternalFunction
// never override getConstructData (this is ensured by ASSERT in JSFunction and InternalFunction).
if (type() == JSFunctionType || type() == InternalFunctionType)
return (methodTable()->getConstructData(this).type != CallData::Type::None) ? TriState::True : TriState::False;
return TriState::Indeterminate;
}
ALWAYS_INLINE bool JSCell::isCallable()
{
auto result = isCallableWithConcurrency<Concurrency::MainThread>();
ASSERT(result != TriState::Indeterminate);
return result == TriState::True;
}
ALWAYS_INLINE bool JSCell::isConstructor()
{
auto result = isConstructorWithConcurrency<Concurrency::MainThread>();
ASSERT(result != TriState::Indeterminate);
return result == TriState::True;
}
ALWAYS_INLINE void JSCell::setStructure(VM& vm, Structure* structure)
{
ASSERT(structure->classInfoForCells() == this->structure()->classInfoForCells());
ASSERT(!this->structure()
|| this->structure()->transitionWatchpointSetHasBeenInvalidated()
|| structure->id().decode() == structure);
// V7 (TSAN, cell-header family): header-byte writers store relaxed so
// they pair with the (blessed, OM §3.0/GT#2) cellHeaderConcurrentLoad
// readers; identical plain stores non-TSAN. The m_indexingTypeAndMisc
// reads feeding the CAS loop below load relaxed for the same reason —
// the CAS itself was already atomic.
cellHeaderConcurrentStore(m_structureID, structure->id());
cellHeaderConcurrentStore(m_flags, TypeInfo::mergeInlineTypeFlags(structure->typeInfo().inlineTypeFlags(), cellHeaderConcurrentLoad(m_flags)));
cellHeaderConcurrentStore(m_type, structure->typeInfo().type());
IndexingType newIndexingType = structure->indexingModeIncludingHistory();
if (cellHeaderConcurrentLoad(m_indexingTypeAndMisc) != newIndexingType) {
ASSERT(!(newIndexingType & ~AllArrayTypesAndHistory));
for (;;) {
IndexingType oldValue = cellHeaderConcurrentLoad(m_indexingTypeAndMisc);
IndexingType newValue = (oldValue & ~AllArrayTypesAndHistory) | structure->indexingModeIncludingHistory();
if (WTF::atomicCompareExchangeWeakRelaxed(&m_indexingTypeAndMisc, oldValue, newValue))
break;
}
}
vm.writeBarrier(this, structure);
}
inline const MethodTable* JSCell::methodTable() const
{
Structure* structure = this->structure();
#if ASSERT_ENABLED
if (Structure* rootStructure = structure->structure())
ASSERT(rootStructure == rootStructure->structure());
#endif
return &structure->classInfoForCells()->methodTable;
}
// SPEC-objectmodel Task 2 audit note (M5/M7): JSCell::structure() masks the
// StructureID nuke bit before decoding (see JSCell.h — StructureID::decode()
// decontaminates, and the contract is now explicit there), while structureID()
// stays RAW for GC didRace/isNuked tests. Offset-bearing reads like
// fastGetOwnProperty below are M7/I24-conforming because the out-of-line leg of
// JSObject::locationForOffset() performs the §Q dispatch internally
// (M7(d) loadLoadFence + I33 bound) when Options::useJSThreads() is on.
ALWAYS_INLINE JSValue JSCell::fastGetOwnProperty(VM& vm, Structure& structure, PropertyName name)
{
ASSERT(canUseFastGetOwnProperty(structure));
PropertyOffset offset = structure.get(vm, name);
if (offset != invalidOffset)
return asObject(this)->locationForOffset(offset)->get();
return JSValue();
}
inline bool JSCell::canUseFastGetOwnProperty(const Structure& structure)
{
return !structure.hasAnyKindOfGetterSetterProperties()
&& !structure.typeInfo().overridesGetOwnPropertySlot();
}
inline bool JSCell::toBoolean(JSGlobalObject* globalObject) const
{
if (isString())
return static_cast<const JSString*>(this)->toBoolean();
if (isHeapBigInt())
return static_cast<const JSBigInt*>(this)->toBoolean();
return !structure()->masqueradesAsUndefined(globalObject);
}
inline TriState JSCell::pureToBoolean() const
{
if (isString())
return static_cast<const JSString*>(this)->toBoolean() ? TriState::True : TriState::False;
if (isHeapBigInt())
return static_cast<const JSBigInt*>(this)->toBoolean() ? TriState::True : TriState::False;
if (isSymbol())
return TriState::True;
return TriState::Indeterminate;
}
inline void JSCellLock::lock()
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
if (!IndexingTypeLockAlgorithm::lockFast(*lock)) [[unlikely]]
lockSlow();
}
inline bool JSCellLock::tryLock()
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
return IndexingTypeLockAlgorithm::tryLock(*lock);
}
inline void JSCellLock::unlock()
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
if (!IndexingTypeLockAlgorithm::unlockFast(*lock)) [[unlikely]]
unlockSlow();
}
inline bool JSCellLock::isLocked() const
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
return IndexingTypeLockAlgorithm::isLocked(*lock);
}
inline bool JSCell::perCellBit() const
{
return TypeInfo::perCellBit(inlineTypeFlags());
}
inline void JSCell::setPerCellBit(bool value)
{
if (value == perCellBit())
return;
#if USE(JSVALUE64)
// SPEC-objectmodel §3.0 (review round 4): flag-on, m_flags' per-cell-bit
// lane is one of the header's VOLATILE lanes (cellHeaderVolatileMask,
// ConcurrentButterfly.h) - it can be mutated with no cell lock while a
// transition's header CAS/DCAS is in flight, and a plain |=/&= RMW here
// could both (a) write back a stale flags byte that undoes a concurrent
// publication's mergeInlineTypeFlags store, and (b) lose this flip to a
// racing header CAS. Byte-sized CAS closes both directions: the transition
// side CAS-merges this lane from the freshest read (taxonomy (a)), and our
// CAS retries across any concurrent flags-byte movement. Flag-off the
// plain RMW below is byte-identical to today (I22).
if (Options::useJSThreads()) [[unlikely]] {
auto* flagsByte = std::bit_cast<Atomic<TypeInfo::InlineTypeFlags>*>(&m_flags);
while (true) {
TypeInfo::InlineTypeFlags oldFlags = flagsByte->load(std::memory_order_relaxed);
TypeInfo::InlineTypeFlags newFlags = value
? static_cast<TypeInfo::InlineTypeFlags>(oldFlags | TypeInfoPerCellBit)
: static_cast<TypeInfo::InlineTypeFlags>(oldFlags & ~TypeInfoPerCellBit);
if (oldFlags == newFlags || flagsByte->compareExchangeWeak(oldFlags, newFlags, std::memory_order_seq_cst))
return;
}
}
#endif
if (value)
m_flags |= static_cast<TypeInfo::InlineTypeFlags>(TypeInfoPerCellBit);
else
m_flags &= ~static_cast<TypeInfo::InlineTypeFlags>(TypeInfoPerCellBit);
}
inline JSObject* JSCell::toObject(JSGlobalObject* globalObject) const
{
if (isObject())
return uncheckedDowncast<JSObject>(const_cast<JSCell*>(this));
return toObjectSlow(globalObject);
}
ALWAYS_INLINE JSString* JSCell::toStringInline(JSGlobalObject* globalObject) const
{
Structure* structure = this->structure();
if (structure->hasRareData()) {
auto* rareData = structure->rareData();
if (rareData->cachedSpecialProperty(CachedSpecialPropertyKey::ToPrimitive).isUndefinedOrNull()) {
if (rareData->cachedSpecialProperty(CachedSpecialPropertyKey::ToString) == globalObject->objectProtoToStringFunction()) {
if (auto result = rareData->cachedSpecialProperty(CachedSpecialPropertyKey::ToStringTag))
return asString(result);
}
}
}
if (isObject())
return asObject(this)->toString(globalObject);
if (isString())
return asString(this);
return toStringSlowCase(globalObject);
}
ALWAYS_INLINE bool JSCell::putInline(JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
{
Structure* structure = this->structure();
if (!structure->typeInfo().overridesPut()) [[likely]]
return JSObject::putInlineForJSObject(asObject(this), globalObject, propertyName, value, slot);
return structure->methodTable()->put(this, globalObject, propertyName, value, slot);
}
inline bool isWebAssemblyInstance(const JSCell* cell)
{
return cell->type() == WebAssemblyInstanceType;
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END