[INTL] Implement Intl.PluralRules
[WebKit-https.git] / Source / JavaScriptCore / runtime / VM.cpp
1 /*
2  * Copyright (C) 2008-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "VM.h"
31
32 #include "ArgList.h"
33 #include "ArrayBufferNeuteringWatchpoint.h"
34 #include "BuiltinExecutables.h"
35 #include "BytecodeIntrinsicRegistry.h"
36 #include "CodeBlock.h"
37 #include "CodeCache.h"
38 #include "CommonIdentifiers.h"
39 #include "CommonSlowPaths.h"
40 #include "CustomGetterSetter.h"
41 #include "DFGWorklist.h"
42 #include "DirectEvalExecutable.h"
43 #include "Disassembler.h"
44 #include "Error.h"
45 #include "ErrorConstructor.h"
46 #include "ErrorInstance.h"
47 #include "EvalCodeBlock.h"
48 #include "Exception.h"
49 #include "ExecutableToCodeBlockEdge.h"
50 #include "FTLThunks.h"
51 #include "FastMallocAlignedMemoryAllocator.h"
52 #include "FunctionCodeBlock.h"
53 #include "FunctionConstructor.h"
54 #include "FunctionExecutable.h"
55 #include "GCActivityCallback.h"
56 #include "GetterSetter.h"
57 #include "GigacageAlignedMemoryAllocator.h"
58 #include "HasOwnPropertyCache.h"
59 #include "Heap.h"
60 #include "HeapIterationScope.h"
61 #include "HeapProfiler.h"
62 #include "HostCallReturnValue.h"
63 #include "Identifier.h"
64 #include "IncrementalSweeper.h"
65 #include "IndirectEvalExecutable.h"
66 #include "InferredTypeTable.h"
67 #include "InferredValue.h"
68 #include "Interpreter.h"
69 #include "IntlCollatorConstructor.h"
70 #include "IntlDateTimeFormatConstructor.h"
71 #include "IntlNumberFormatConstructor.h"
72 #include "IntlPluralRulesConstructor.h"
73 #include "JITCode.h"
74 #include "JITWorklist.h"
75 #include "JSAPIValueWrapper.h"
76 #include "JSArray.h"
77 #include "JSArrayBufferConstructor.h"
78 #include "JSAsyncFunction.h"
79 #include "JSBigInt.h"
80 #include "JSBoundFunction.h"
81 #include "JSCInlines.h"
82 #include "JSCallbackFunction.h"
83 #include "JSCustomGetterSetterFunction.h"
84 #include "JSDestructibleObjectHeapCellType.h"
85 #include "JSFixedArray.h"
86 #include "JSFunction.h"
87 #include "JSGlobalObjectFunctions.h"
88 #include "JSInternalPromiseDeferred.h"
89 #include "JSLock.h"
90 #include "JSMap.h"
91 #include "JSMapIterator.h"
92 #include "JSPromiseDeferred.h"
93 #include "JSPropertyNameEnumerator.h"
94 #include "JSSegmentedVariableObjectHeapCellType.h"
95 #include "JSScriptFetchParameters.h"
96 #include "JSScriptFetcher.h"
97 #include "JSSet.h"
98 #include "JSSetIterator.h"
99 #include "JSSourceCode.h"
100 #include "JSStringHeapCellType.h"
101 #include "JSTemplateObjectDescriptor.h"
102 #include "JSWeakMap.h"
103 #include "JSWeakSet.h"
104 #include "JSWebAssembly.h"
105 #include "JSWebAssemblyCodeBlock.h"
106 #include "JSWebAssemblyCodeBlockHeapCellType.h"
107 #include "JSWithScope.h"
108 #include "LLIntData.h"
109 #include "Lexer.h"
110 #include "Lookup.h"
111 #include "MinimumReservedZoneSize.h"
112 #include "ModuleProgramCodeBlock.h"
113 #include "ModuleProgramExecutable.h"
114 #include "NativeErrorConstructor.h"
115 #include "NativeExecutable.h"
116 #include "NativeStdFunctionCell.h"
117 #include "Nodes.h"
118 #include "ObjCCallbackFunction.h"
119 #include "Parser.h"
120 #include "ProfilerDatabase.h"
121 #include "ProgramCodeBlock.h"
122 #include "ProgramExecutable.h"
123 #include "PromiseDeferredTimer.h"
124 #include "PropertyMapHashTable.h"
125 #include "ProxyRevoke.h"
126 #include "RegExpCache.h"
127 #include "RegExpConstructor.h"
128 #include "RegExpObject.h"
129 #include "RegisterAtOffsetList.h"
130 #include "RuntimeType.h"
131 #include "SamplingProfiler.h"
132 #include "ShadowChicken.h"
133 #include "SimpleTypedArrayController.h"
134 #include "SourceProviderCache.h"
135 #include "StackVisitor.h"
136 #include "StrictEvalActivation.h"
137 #include "StrongInlines.h"
138 #include "StructureInlines.h"
139 #include "TestRunnerUtils.h"
140 #include "ThreadLocalCacheInlines.h"
141 #include "ThunkGenerators.h"
142 #include "TypeProfiler.h"
143 #include "TypeProfilerLog.h"
144 #include "UnlinkedCodeBlock.h"
145 #include "VMEntryScope.h"
146 #include "VMInspector.h"
147 #include "WasmWorklist.h"
148 #include "Watchdog.h"
149 #include "WeakGCMapInlines.h"
150 #include "WebAssemblyFunction.h"
151 #include "WebAssemblyWrapperFunction.h"
152 #include <wtf/ProcessID.h>
153 #include <wtf/ReadWriteLock.h>
154 #include <wtf/SimpleStats.h>
155 #include <wtf/StringPrintStream.h>
156 #include <wtf/Threading.h>
157 #include <wtf/text/AtomicStringTable.h>
158 #include <wtf/text/SymbolRegistry.h>
159
160 #if !ENABLE(JIT)
161 #include "CLoopStack.h"
162 #include "CLoopStackInlines.h"
163 #endif
164
165 #if ENABLE(DFG_JIT)
166 #include "ConservativeRoots.h"
167 #endif
168
169 #if ENABLE(REGEXP_TRACING)
170 #include "RegExp.h"
171 #endif
172
173 using namespace WTF;
174
175 namespace JSC {
176
177 // Note: Platform.h will enforce that ENABLE(ASSEMBLER) is true if either
178 // ENABLE(JIT) or ENABLE(YARR_JIT) or both are enabled. The code below
179 // just checks for ENABLE(JIT) or ENABLE(YARR_JIT) with this premise in mind.
180
181 #if ENABLE(ASSEMBLER)
182 static bool enableAssembler(ExecutableAllocator& executableAllocator)
183 {
184     if (!Options::useJIT() && !Options::useRegExpJIT())
185         return false;
186
187     if (!executableAllocator.isValid()) {
188         if (Options::crashIfCantAllocateJITMemory())
189             CRASH();
190         return false;
191     }
192
193     char* canUseJITString = getenv("JavaScriptCoreUseJIT");
194     return !canUseJITString || atoi(canUseJITString);
195 }
196 #endif // ENABLE(!ASSEMBLER)
197
198 bool VM::canUseAssembler()
199 {
200 #if ENABLE(ASSEMBLER)
201     static std::once_flag onceKey;
202     static bool enabled = false;
203     std::call_once(onceKey, [] {
204         enabled = enableAssembler(ExecutableAllocator::singleton());
205     });
206     return enabled;
207 #else
208     return false; // interpreter only
209 #endif
210 }
211
212 bool VM::canUseJIT()
213 {
214 #if ENABLE(JIT)
215     static std::once_flag onceKey;
216     static bool enabled = false;
217     std::call_once(onceKey, [] {
218         enabled = VM::canUseAssembler() && Options::useJIT();
219     });
220     return enabled;
221 #else
222     return false; // interpreter only
223 #endif
224 }
225
226 bool VM::canUseRegExpJIT()
227 {
228 #if ENABLE(YARR_JIT)
229     static std::once_flag onceKey;
230     static bool enabled = false;
231     std::call_once(onceKey, [] {
232         enabled = VM::canUseAssembler() && Options::useRegExpJIT();
233     });
234     return enabled;
235 #else
236     return false; // interpreter only
237 #endif
238 }
239
240 VM::VM(VMType vmType, HeapType heapType)
241     : m_apiLock(adoptRef(new JSLock(this)))
242 #if USE(CF)
243     , m_runLoop(CFRunLoopGetCurrent())
244 #endif // USE(CF)
245     , heap(this, heapType)
246     , fastMallocAllocator(std::make_unique<FastMallocAlignedMemoryAllocator>())
247     , primitiveGigacageAllocator(std::make_unique<GigacageAlignedMemoryAllocator>(Gigacage::Primitive))
248     , jsValueGigacageAllocator(std::make_unique<GigacageAlignedMemoryAllocator>(Gigacage::JSValue))
249     , auxiliaryHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::Auxiliary)))
250     , cellJSValueOOBHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::JSCell)))
251     , cellDangerousBitsHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::JSCell)))
252     , destructibleCellHeapCellType(std::make_unique<HeapCellType>(CellAttributes(NeedsDestruction, HeapCell::JSCell)))
253     , stringHeapCellType(std::make_unique<JSStringHeapCellType>())
254     , destructibleObjectHeapCellType(std::make_unique<JSDestructibleObjectHeapCellType>())
255     , segmentedVariableObjectHeapCellType(std::make_unique<JSSegmentedVariableObjectHeapCellType>())
256 #if ENABLE(WEBASSEMBLY)
257     , webAssemblyCodeBlockHeapCellType(std::make_unique<JSWebAssemblyCodeBlockHeapCellType>())
258 #endif
259     , primitiveGigacageAuxiliarySpace("Primitive Gigacage Auxiliary", heap, auxiliaryHeapCellType.get(), primitiveGigacageAllocator.get())
260     , jsValueGigacageAuxiliarySpace("JSValue Gigacage Auxiliary", heap, auxiliaryHeapCellType.get(), jsValueGigacageAllocator.get())
261     , cellJSValueOOBSpace("JSCell JSValueOOB", heap, cellJSValueOOBHeapCellType.get(), fastMallocAllocator.get())
262     , cellDangerousBitsSpace("JSCell DangerousBits", heap, cellDangerousBitsHeapCellType.get(), fastMallocAllocator.get())
263     , jsValueGigacageCellSpace("JSValue Gigacage JSCell", heap, cellJSValueOOBHeapCellType.get(), jsValueGigacageAllocator.get())
264     , destructibleCellSpace("Destructible JSCell", heap, destructibleCellHeapCellType.get(), fastMallocAllocator.get())
265     , stringSpace("JSString", heap, stringHeapCellType.get(), fastMallocAllocator.get())
266     , destructibleObjectSpace("JSDestructibleObject", heap, destructibleObjectHeapCellType.get(), fastMallocAllocator.get())
267     , eagerlySweptDestructibleObjectSpace("Eagerly Swept JSDestructibleObject", heap, destructibleObjectHeapCellType.get(), fastMallocAllocator.get())
268     , segmentedVariableObjectSpace("JSSegmentedVariableObjectSpace", heap, segmentedVariableObjectHeapCellType.get(), fastMallocAllocator.get())
269     , arrayBufferConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSArrayBufferConstructor)
270     , asyncFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSAsyncFunction)
271     , asyncGeneratorFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSAsyncGeneratorFunction)
272     , boundFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSBoundFunction)
273     , callbackFunctionSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSCallbackFunction)
274     , customGetterSetterFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSCustomGetterSetterFunction)
275     , directEvalExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), DirectEvalExecutable)
276     , errorConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), ErrorConstructor)
277     , executableToCodeBlockEdgeSpace ISO_SUBSPACE_INIT(heap, cellDangerousBitsHeapCellType.get(), ExecutableToCodeBlockEdge)
278     , functionExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), FunctionExecutable)
279     , functionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSFunction)
280     , generatorFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSGeneratorFunction)
281     , indirectEvalExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), IndirectEvalExecutable)
282     , inferredTypeSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), InferredType)
283     , inferredValueSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), InferredValue)
284     , internalFunctionSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), InternalFunction)
285 #if ENABLE(INTL)
286     , intlCollatorConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), IntlCollatorConstructor)
287     , intlDateTimeFormatConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), IntlDateTimeFormatConstructor)
288     , intlNumberFormatConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), IntlNumberFormatConstructor)
289     , intlPluralRulesConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), IntlPluralRulesConstructor)
290 #endif
291     , moduleProgramExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ModuleProgramExecutable)
292     , nativeErrorConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), NativeErrorConstructor)
293     , nativeExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), NativeExecutable)
294     , nativeStdFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSNativeStdFunction)
295 #if JSC_OBJC_API_ENABLED
296     , objCCallbackFunctionSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), ObjCCallbackFunction)
297 #endif
298     , programExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ProgramExecutable)
299     , propertyTableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), PropertyTable)
300     , proxyRevokeSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), ProxyRevoke)
301     , regExpConstructorSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), RegExpConstructor)
302     , strictModeTypeErrorFunctionSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), StrictModeTypeErrorFunction)
303     , structureRareDataSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), StructureRareData)
304     , structureSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), Structure)
305     , weakSetSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSWeakSet)
306     , weakMapSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSWeakMap)
307 #if ENABLE(WEBASSEMBLY)
308     , webAssemblyCodeBlockSpace ISO_SUBSPACE_INIT(heap, webAssemblyCodeBlockHeapCellType.get(), JSWebAssemblyCodeBlock)
309     , webAssemblyFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), WebAssemblyFunction)
310     , webAssemblyWrapperFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), WebAssemblyWrapperFunction)
311 #endif
312     , executableToCodeBlockEdgesWithConstraints(executableToCodeBlockEdgeSpace)
313     , executableToCodeBlockEdgesWithFinalizers(executableToCodeBlockEdgeSpace)
314     , inferredTypesWithFinalizers(inferredTypeSpace)
315     , inferredValuesWithFinalizers(inferredValueSpace)
316     , evalCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), EvalCodeBlock)
317     , functionCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), FunctionCodeBlock)
318     , moduleProgramCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ModuleProgramCodeBlock)
319     , programCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ProgramCodeBlock)
320     , vmType(vmType)
321     , clientData(0)
322     , topEntryFrame(nullptr)
323     , topCallFrame(CallFrame::noCaller())
324     , promiseDeferredTimer(std::make_unique<PromiseDeferredTimer>(*this))
325     , m_atomicStringTable(vmType == Default ? Thread::current().atomicStringTable() : new AtomicStringTable)
326     , propertyNames(nullptr)
327     , emptyList(new ArgList)
328     , machineCodeBytesPerBytecodeWordForBaselineJIT(std::make_unique<SimpleStats>())
329     , customGetterSetterFunctionMap(*this)
330     , stringCache(*this)
331     , symbolImplToSymbolMap(*this)
332     , structureCache(*this)
333     , interpreter(0)
334     , entryScope(0)
335     , m_regExpCache(new RegExpCache(this))
336 #if ENABLE(REGEXP_TRACING)
337     , m_rtTraceList(new RTTraceList())
338 #endif
339 #if ENABLE(GC_VALIDATION)
340     , m_initializingObjectClass(0)
341 #endif
342     , m_stackPointerAtVMEntry(0)
343     , m_codeCache(std::make_unique<CodeCache>())
344     , m_builtinExecutables(std::make_unique<BuiltinExecutables>(*this))
345     , m_typeProfilerEnabledCount(0)
346     , m_primitiveGigacageEnabled(IsWatched)
347     , m_controlFlowProfilerEnabledCount(0)
348     , m_shadowChicken(std::make_unique<ShadowChicken>())
349 {
350     interpreter = new Interpreter(*this);
351     StackBounds stack = Thread::current().stack();
352     updateSoftReservedZoneSize(Options::softReservedZoneSize());
353     setLastStackTop(stack.origin());
354
355     defaultThreadLocalCache = ThreadLocalCache::create(heap);
356     defaultThreadLocalCache->install(*this);
357
358     // Need to be careful to keep everything consistent here
359     JSLockHolder lock(this);
360     AtomicStringTable* existingEntryAtomicStringTable = Thread::current().setCurrentAtomicStringTable(m_atomicStringTable);
361     propertyNames = new CommonIdentifiers(this);
362     structureStructure.set(*this, Structure::createStructure(*this));
363     structureRareDataStructure.set(*this, StructureRareData::createStructure(*this, 0, jsNull()));
364     terminatedExecutionErrorStructure.set(*this, TerminatedExecutionError::createStructure(*this, 0, jsNull()));
365     stringStructure.set(*this, JSString::createStructure(*this, 0, jsNull()));
366     propertyNameEnumeratorStructure.set(*this, JSPropertyNameEnumerator::createStructure(*this, 0, jsNull()));
367     customGetterSetterStructure.set(*this, CustomGetterSetter::createStructure(*this, 0, jsNull()));
368     domAttributeGetterSetterStructure.set(*this, DOMAttributeGetterSetter::createStructure(*this, 0, jsNull()));
369     scopedArgumentsTableStructure.set(*this, ScopedArgumentsTable::createStructure(*this, 0, jsNull()));
370     apiWrapperStructure.set(*this, JSAPIValueWrapper::createStructure(*this, 0, jsNull()));
371     nativeExecutableStructure.set(*this, NativeExecutable::createStructure(*this, 0, jsNull()));
372     evalExecutableStructure.set(*this, EvalExecutable::createStructure(*this, 0, jsNull()));
373     programExecutableStructure.set(*this, ProgramExecutable::createStructure(*this, 0, jsNull()));
374     functionExecutableStructure.set(*this, FunctionExecutable::createStructure(*this, 0, jsNull()));
375 #if ENABLE(WEBASSEMBLY)
376     webAssemblyCodeBlockStructure.set(*this, JSWebAssemblyCodeBlock::createStructure(*this, 0, jsNull()));
377 #endif
378     moduleProgramExecutableStructure.set(*this, ModuleProgramExecutable::createStructure(*this, 0, jsNull()));
379     regExpStructure.set(*this, RegExp::createStructure(*this, 0, jsNull()));
380     symbolStructure.set(*this, Symbol::createStructure(*this, 0, jsNull()));
381     symbolTableStructure.set(*this, SymbolTable::createStructure(*this, 0, jsNull()));
382     fixedArrayStructure.set(*this, JSFixedArray::createStructure(*this, 0, jsNull()));
383     sourceCodeStructure.set(*this, JSSourceCode::createStructure(*this, 0, jsNull()));
384     scriptFetcherStructure.set(*this, JSScriptFetcher::createStructure(*this, 0, jsNull()));
385     scriptFetchParametersStructure.set(*this, JSScriptFetchParameters::createStructure(*this, 0, jsNull()));
386     structureChainStructure.set(*this, StructureChain::createStructure(*this, 0, jsNull()));
387     sparseArrayValueMapStructure.set(*this, SparseArrayValueMap::createStructure(*this, 0, jsNull()));
388     templateObjectDescriptorStructure.set(*this, JSTemplateObjectDescriptor::createStructure(*this, 0, jsNull()));
389     arrayBufferNeuteringWatchpointStructure.set(*this, ArrayBufferNeuteringWatchpoint::createStructure(*this));
390     unlinkedFunctionExecutableStructure.set(*this, UnlinkedFunctionExecutable::createStructure(*this, 0, jsNull()));
391     unlinkedProgramCodeBlockStructure.set(*this, UnlinkedProgramCodeBlock::createStructure(*this, 0, jsNull()));
392     unlinkedEvalCodeBlockStructure.set(*this, UnlinkedEvalCodeBlock::createStructure(*this, 0, jsNull()));
393     unlinkedFunctionCodeBlockStructure.set(*this, UnlinkedFunctionCodeBlock::createStructure(*this, 0, jsNull()));
394     unlinkedModuleProgramCodeBlockStructure.set(*this, UnlinkedModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
395     propertyTableStructure.set(*this, PropertyTable::createStructure(*this, 0, jsNull()));
396     inferredTypeStructure.set(*this, InferredType::createStructure(*this, 0, jsNull()));
397     inferredTypeTableStructure.set(*this, InferredTypeTable::createStructure(*this, 0, jsNull()));
398     inferredValueStructure.set(*this, InferredValue::createStructure(*this, 0, jsNull()));
399     functionRareDataStructure.set(*this, FunctionRareData::createStructure(*this, 0, jsNull()));
400     exceptionStructure.set(*this, Exception::createStructure(*this, 0, jsNull()));
401     promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
402     internalPromiseDeferredStructure.set(*this, JSInternalPromiseDeferred::createStructure(*this, 0, jsNull()));
403     programCodeBlockStructure.set(*this, ProgramCodeBlock::createStructure(*this, 0, jsNull()));
404     moduleProgramCodeBlockStructure.set(*this, ModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
405     evalCodeBlockStructure.set(*this, EvalCodeBlock::createStructure(*this, 0, jsNull()));
406     functionCodeBlockStructure.set(*this, FunctionCodeBlock::createStructure(*this, 0, jsNull()));
407     hashMapBucketSetStructure.set(*this, HashMapBucket<HashMapBucketDataKey>::createStructure(*this, 0, jsNull()));
408     hashMapBucketMapStructure.set(*this, HashMapBucket<HashMapBucketDataKeyValue>::createStructure(*this, 0, jsNull()));
409     setIteratorStructure.set(*this, JSSetIterator::createStructure(*this, 0, jsNull()));
410     mapIteratorStructure.set(*this, JSMapIterator::createStructure(*this, 0, jsNull()));
411     bigIntStructure.set(*this, JSBigInt::createStructure(*this, 0, jsNull()));
412     executableToCodeBlockEdgeStructure.set(*this, ExecutableToCodeBlockEdge::createStructure(*this, nullptr, jsNull()));
413
414     sentinelSetBucket.set(*this, JSSet::BucketType::createSentinel(*this));
415     sentinelMapBucket.set(*this, JSMap::BucketType::createSentinel(*this));
416
417     nativeStdFunctionCellStructure.set(*this, NativeStdFunctionCell::createStructure(*this, 0, jsNull()));
418     smallStrings.initializeCommonStrings(*this);
419
420     Thread::current().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
421
422 #if ENABLE(JIT)
423     jitStubs = std::make_unique<JITThunks>();
424 #endif
425
426 #if ENABLE(FTL_JIT)
427     ftlThunks = std::make_unique<FTL::Thunks>();
428 #endif // ENABLE(FTL_JIT)
429     
430 #if ENABLE(JIT)
431     initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
432 #endif
433     
434     Gigacage::addPrimitiveDisableCallback(primitiveGigacageDisabledCallback, this);
435
436     heap.notifyIsSafeToCollect();
437     
438     LLInt::Data::performAssertions(*this);
439     
440     if (UNLIKELY(Options::useProfiler())) {
441         m_perBytecodeProfiler = std::make_unique<Profiler::Database>(*this);
442
443         StringPrintStream pathOut;
444         const char* profilerPath = getenv("JSC_PROFILER_PATH");
445         if (profilerPath)
446             pathOut.print(profilerPath, "/");
447         pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
448         m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
449     }
450
451     callFrameForCatch = nullptr;
452
453     // Initialize this last, as a free way of asserting that VM initialization itself
454     // won't use this.
455     m_typedArrayController = adoptRef(new SimpleTypedArrayController());
456
457     m_bytecodeIntrinsicRegistry = std::make_unique<BytecodeIntrinsicRegistry>(*this);
458
459     if (Options::useTypeProfiler())
460         enableTypeProfiler();
461     if (Options::useControlFlowProfiler())
462         enableControlFlowProfiler();
463 #if ENABLE(SAMPLING_PROFILER)
464     if (Options::useSamplingProfiler()) {
465         setShouldBuildPCToCodeOriginMapping();
466         Ref<Stopwatch> stopwatch = Stopwatch::create();
467         stopwatch->start();
468         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
469         if (Options::samplingProfilerPath())
470             m_samplingProfiler->registerForReportAtExit();
471         m_samplingProfiler->start();
472     }
473 #endif // ENABLE(SAMPLING_PROFILER)
474
475     if (Options::alwaysGeneratePCToCodeOriginMap())
476         setShouldBuildPCToCodeOriginMapping();
477
478     if (Options::watchdog()) {
479         Watchdog& watchdog = ensureWatchdog();
480         watchdog.setTimeLimit(Seconds::fromMilliseconds(Options::watchdog()));
481     }
482
483 #if ENABLE(JIT)
484     // Make sure that any stubs that the JIT is going to use are initialized in non-compilation threads.
485     if (canUseJIT()) {
486         getCTIInternalFunctionTrampolineFor(CodeForCall);
487         getCTIInternalFunctionTrampolineFor(CodeForConstruct);
488     }
489 #endif
490
491     VMInspector::instance().add(this);
492 }
493
494 static ReadWriteLock s_destructionLock;
495
496 void waitForVMDestruction()
497 {
498     auto locker = holdLock(s_destructionLock.write());
499 }
500
501 VM::~VM()
502 {
503     auto destructionLocker = holdLock(s_destructionLock.read());
504     
505     Gigacage::removePrimitiveDisableCallback(primitiveGigacageDisabledCallback, this);
506     promiseDeferredTimer->stopRunningTasks();
507 #if ENABLE(WEBASSEMBLY)
508     if (Wasm::existingWorklistOrNull())
509         Wasm::ensureWorklist().stopAllPlansForContext(wasmContext);
510 #endif
511     if (UNLIKELY(m_watchdog))
512         m_watchdog->willDestroyVM(this);
513     m_traps.willDestroyVM();
514     VMInspector::instance().remove(this);
515
516     // Never GC, ever again.
517     heap.incrementDeferralDepth();
518
519 #if ENABLE(SAMPLING_PROFILER)
520     if (m_samplingProfiler) {
521         m_samplingProfiler->reportDataToOptionFile();
522         m_samplingProfiler->shutdown();
523     }
524 #endif // ENABLE(SAMPLING_PROFILER)
525     
526 #if ENABLE(JIT)
527     JITWorklist::instance()->completeAllForVM(*this);
528 #endif // ENABLE(JIT)
529
530 #if ENABLE(DFG_JIT)
531     // Make sure concurrent compilations are done, but don't install them, since there is
532     // no point to doing so.
533     for (unsigned i = DFG::numberOfWorklists(); i--;) {
534         if (DFG::Worklist* worklist = DFG::existingWorklistForIndexOrNull(i)) {
535             worklist->removeNonCompilingPlansForVM(*this);
536             worklist->waitUntilAllPlansForVMAreReady(*this);
537             worklist->removeAllReadyPlansForVM(*this);
538         }
539     }
540 #endif // ENABLE(DFG_JIT)
541     
542     waitForAsynchronousDisassembly();
543     
544     // Clear this first to ensure that nobody tries to remove themselves from it.
545     m_perBytecodeProfiler = nullptr;
546
547     ASSERT(currentThreadIsHoldingAPILock());
548     m_apiLock->willDestroyVM(this);
549     heap.lastChanceToFinalize();
550     
551 #if !USE(FAST_TLS_FOR_TLC)
552     ThreadLocalCache::destructor(threadLocalCacheData);
553 #endif
554
555     delete interpreter;
556 #ifndef NDEBUG
557     interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
558 #endif
559
560     delete emptyList;
561
562     delete propertyNames;
563     if (vmType != Default)
564         delete m_atomicStringTable;
565
566     delete clientData;
567     delete m_regExpCache;
568
569 #if ENABLE(REGEXP_TRACING)
570     delete m_rtTraceList;
571 #endif
572
573 #if ENABLE(DFG_JIT)
574     for (unsigned i = 0; i < m_scratchBuffers.size(); ++i)
575         fastFree(m_scratchBuffers[i]);
576 #endif
577 }
578
579 void VM::primitiveGigacageDisabledCallback(void* argument)
580 {
581     static_cast<VM*>(argument)->primitiveGigacageDisabled();
582 }
583
584 void VM::primitiveGigacageDisabled()
585 {
586     if (m_apiLock->currentThreadIsHoldingLock()) {
587         m_primitiveGigacageEnabled.fireAll(*this, "Primitive gigacage disabled");
588         return;
589     }
590  
591     // This is totally racy, and that's OK. The point is, it's up to the user to ensure that they pass the
592     // uncaged buffer in a nicely synchronized manner.
593     m_needToFirePrimitiveGigacageEnabled = true;
594 }
595
596 void VM::setLastStackTop(void* lastStackTop)
597
598     m_lastStackTop = lastStackTop;
599 }
600
601 Ref<VM> VM::createContextGroup(HeapType heapType)
602 {
603     return adoptRef(*new VM(APIContextGroup, heapType));
604 }
605
606 Ref<VM> VM::create(HeapType heapType)
607 {
608     return adoptRef(*new VM(Default, heapType));
609 }
610
611 bool VM::sharedInstanceExists()
612 {
613     return sharedInstanceInternal();
614 }
615
616 VM& VM::sharedInstance()
617 {
618     GlobalJSLock globalLock;
619     VM*& instance = sharedInstanceInternal();
620     if (!instance)
621         instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
622     return *instance;
623 }
624
625 VM*& VM::sharedInstanceInternal()
626 {
627     static VM* sharedInstance;
628     return sharedInstance;
629 }
630
631 Watchdog& VM::ensureWatchdog()
632 {
633     if (!m_watchdog)
634         m_watchdog = adoptRef(new Watchdog(this));
635     return *m_watchdog;
636 }
637
638 HeapProfiler& VM::ensureHeapProfiler()
639 {
640     if (!m_heapProfiler)
641         m_heapProfiler = std::make_unique<HeapProfiler>(*this);
642     return *m_heapProfiler;
643 }
644
645 #if ENABLE(SAMPLING_PROFILER)
646 SamplingProfiler& VM::ensureSamplingProfiler(RefPtr<Stopwatch>&& stopwatch)
647 {
648     if (!m_samplingProfiler)
649         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
650     return *m_samplingProfiler;
651 }
652 #endif // ENABLE(SAMPLING_PROFILER)
653
654 #if ENABLE(JIT)
655 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
656 {
657     switch (intrinsic) {
658     case CharCodeAtIntrinsic:
659         return charCodeAtThunkGenerator;
660     case CharAtIntrinsic:
661         return charAtThunkGenerator;
662     case Clz32Intrinsic:
663         return clz32ThunkGenerator;
664     case FromCharCodeIntrinsic:
665         return fromCharCodeThunkGenerator;
666     case SqrtIntrinsic:
667         return sqrtThunkGenerator;
668     case AbsIntrinsic:
669         return absThunkGenerator;
670     case FloorIntrinsic:
671         return floorThunkGenerator;
672     case CeilIntrinsic:
673         return ceilThunkGenerator;
674     case TruncIntrinsic:
675         return truncThunkGenerator;
676     case RoundIntrinsic:
677         return roundThunkGenerator;
678     case ExpIntrinsic:
679         return expThunkGenerator;
680     case LogIntrinsic:
681         return logThunkGenerator;
682     case IMulIntrinsic:
683         return imulThunkGenerator;
684     case RandomIntrinsic:
685         return randomThunkGenerator;
686     case BoundThisNoArgsFunctionCallIntrinsic:
687         return boundThisNoArgsFunctionCallGenerator;
688     default:
689         return nullptr;
690     }
691 }
692
693 #endif // ENABLE(JIT)
694
695 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor, const String& name)
696 {
697     return getHostFunction(function, NoIntrinsic, constructor, nullptr, name);
698 }
699
700 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic, NativeFunction constructor, const DOMJIT::Signature* signature, const String& name)
701 {
702 #if ENABLE(JIT)
703     if (canUseJIT()) {
704         return jitStubs->hostFunctionStub(
705             this, function, constructor,
706             intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0,
707             intrinsic, signature, name);
708     }
709 #else // ENABLE(JIT)
710     UNUSED_PARAM(intrinsic);
711 #endif // ENABLE(JIT)
712     return NativeExecutable::create(*this,
713         adoptRef(*new NativeJITCode(LLInt::getCodeRef<JSEntryPtrTag>(llint_native_call_trampoline), JITCode::HostCallThunk)), function,
714         adoptRef(*new NativeJITCode(LLInt::getCodeRef<JSEntryPtrTag>(llint_native_construct_trampoline), JITCode::HostCallThunk)), constructor,
715         NoIntrinsic, signature, name);
716 }
717
718 MacroAssemblerCodePtr<JSEntryPtrTag> VM::getCTIInternalFunctionTrampolineFor(CodeSpecializationKind kind)
719 {
720 #if ENABLE(JIT)
721     if (canUseJIT()) {
722         if (kind == CodeForCall)
723             return jitStubs->ctiInternalFunctionCall(this).retagged<JSEntryPtrTag>();
724         return jitStubs->ctiInternalFunctionConstruct(this).retagged<JSEntryPtrTag>();
725     }
726 #endif
727     if (kind == CodeForCall)
728         return LLInt::getCodePtr<JSEntryPtrTag>(llint_internal_function_call_trampoline);
729     return LLInt::getCodePtr<JSEntryPtrTag>(llint_internal_function_construct_trampoline);
730 }
731
732 VM::ClientData::~ClientData()
733 {
734 }
735
736 void VM::resetDateCache()
737 {
738     localTimeOffsetCache.reset();
739     cachedDateString = String();
740     cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
741     dateInstanceCache.reset();
742 }
743
744 void VM::whenIdle(std::function<void()> callback)
745 {
746     if (!entryScope) {
747         callback();
748         return;
749     }
750
751     entryScope->addDidPopListener(callback);
752 }
753
754 void VM::deleteAllLinkedCode(DeleteAllCodeEffort effort)
755 {
756     whenIdle([=] () {
757         heap.deleteAllCodeBlocks(effort);
758     });
759 }
760
761 void VM::deleteAllCode(DeleteAllCodeEffort effort)
762 {
763     whenIdle([=] () {
764         m_codeCache->clear();
765         m_regExpCache->deleteAllCode();
766         heap.deleteAllCodeBlocks(effort);
767         heap.deleteAllUnlinkedCodeBlocks(effort);
768         heap.reportAbandonedObjectGraph();
769     });
770 }
771
772 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
773 {
774     auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
775     if (addResult.isNewEntry)
776         addResult.iterator->value = adoptRef(new SourceProviderCache);
777     return addResult.iterator->value.get();
778 }
779
780 void VM::clearSourceProviderCaches()
781 {
782     sourceProviderCacheMap.clear();
783 }
784
785 void VM::throwException(ExecState* exec, Exception* exception)
786 {
787     if (Options::breakOnThrow()) {
788         CodeBlock* codeBlock = exec->codeBlock();
789         dataLog("Throwing exception in call frame ", RawPointer(exec), " for code block ", codeBlock, "\n");
790         CRASH();
791     }
792
793     ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
794
795     interpreter->notifyDebuggerOfExceptionToBeThrown(*this, exec, exception);
796
797     setException(exception);
798
799 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
800     m_nativeStackTraceOfLastThrow = StackTrace::captureStackTrace(Options::unexpectedExceptionStackTraceLimit());
801     m_throwingThread = &Thread::current();
802 #endif
803 }
804
805 JSValue VM::throwException(ExecState* exec, JSValue thrownValue)
806 {
807     VM& vm = *this;
808     Exception* exception = jsDynamicCast<Exception*>(vm, thrownValue);
809     if (!exception)
810         exception = Exception::create(*this, thrownValue);
811
812     throwException(exec, exception);
813     return JSValue(exception);
814 }
815
816 JSObject* VM::throwException(ExecState* exec, JSObject* error)
817 {
818     return asObject(throwException(exec, JSValue(error)));
819 }
820
821 void VM::setStackPointerAtVMEntry(void* sp)
822 {
823     m_stackPointerAtVMEntry = sp;
824     updateStackLimits();
825 }
826
827 size_t VM::updateSoftReservedZoneSize(size_t softReservedZoneSize)
828 {
829     size_t oldSoftReservedZoneSize = m_currentSoftReservedZoneSize;
830     m_currentSoftReservedZoneSize = softReservedZoneSize;
831 #if !ENABLE(JIT)
832     interpreter->cloopStack().setSoftReservedZoneSize(softReservedZoneSize);
833 #endif
834
835     updateStackLimits();
836
837     return oldSoftReservedZoneSize;
838 }
839
840 #if OS(WINDOWS)
841 // On Windows the reserved stack space consists of committed memory, a guard page, and uncommitted memory,
842 // where the guard page is a barrier between committed and uncommitted memory.
843 // When data from the guard page is read or written, the guard page is moved, and memory is committed.
844 // This is how the system grows the stack.
845 // When using the C stack on Windows we need to precommit the needed stack space.
846 // Otherwise we might crash later if we access uncommitted stack memory.
847 // This can happen if we allocate stack space larger than the page guard size (4K).
848 // The system does not get the chance to move the guard page, and commit more memory,
849 // and we crash if uncommitted memory is accessed.
850 // The MSVC compiler fixes this by inserting a call to the _chkstk() function,
851 // when needed, see http://support.microsoft.com/kb/100775.
852 // By touching every page up to the stack limit with a dummy operation,
853 // we force the system to move the guard page, and commit memory.
854
855 static void preCommitStackMemory(void* stackLimit)
856 {
857     const int pageSize = 4096;
858     for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
859         char ch = *p;
860         *p = ch;
861     }
862 }
863 #endif
864
865 inline void VM::updateStackLimits()
866 {
867 #if OS(WINDOWS)
868     void* lastSoftStackLimit = m_softStackLimit;
869 #endif
870
871     const StackBounds& stack = Thread::current().stack();
872     size_t reservedZoneSize = Options::reservedZoneSize();
873     // We should have already ensured that Options::reservedZoneSize() >= minimumReserveZoneSize at
874     // options initialization time, and the option value should not have been changed thereafter.
875     // We don't have the ability to assert here that it hasn't changed, but we can at least assert
876     // that the value is sane.
877     RELEASE_ASSERT(reservedZoneSize >= minimumReservedZoneSize);
878
879     if (m_stackPointerAtVMEntry) {
880         ASSERT(stack.isGrowingDownward());
881         char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
882         m_softStackLimit = stack.recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_currentSoftReservedZoneSize);
883         m_stackLimit = stack.recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), reservedZoneSize);
884     } else {
885         m_softStackLimit = stack.recursionLimit(m_currentSoftReservedZoneSize);
886         m_stackLimit = stack.recursionLimit(reservedZoneSize);
887     }
888
889 #if OS(WINDOWS)
890     // We only need to precommit stack memory dictated by the VM::m_softStackLimit limit.
891     // This is because VM::m_softStackLimit applies to stack usage by LLINT asm or JIT
892     // generated code which can allocate stack space that the C++ compiler does not know
893     // about. As such, we have to precommit that stack memory manually.
894     //
895     // In contrast, we do not need to worry about VM::m_stackLimit because that limit is
896     // used exclusively by C++ code, and the C++ compiler will automatically commit the
897     // needed stack pages.
898     if (lastSoftStackLimit != m_softStackLimit)
899         preCommitStackMemory(m_softStackLimit);
900 #endif
901 }
902
903 #if ENABLE(DFG_JIT)
904 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
905 {
906     auto lock = holdLock(m_scratchBufferLock);
907     for (auto* scratchBuffer : m_scratchBuffers) {
908         if (scratchBuffer->activeLength()) {
909             void* bufferStart = scratchBuffer->dataBuffer();
910             conservativeRoots.add(bufferStart, static_cast<void*>(static_cast<char*>(bufferStart) + scratchBuffer->activeLength()));
911         }
912     }
913 }
914 #endif
915
916 void logSanitizeStack(VM* vm)
917 {
918     if (Options::verboseSanitizeStack() && vm->topCallFrame) {
919         int dummy;
920         auto& stackBounds = Thread::current().stack();
921         dataLog(
922             "Sanitizing stack for VM = ", RawPointer(vm), " with top call frame at ", RawPointer(vm->topCallFrame),
923             ", current stack pointer at ", RawPointer(&dummy), ", in ",
924             pointerDump(vm->topCallFrame->codeBlock()), ", last code origin = ",
925             vm->topCallFrame->codeOrigin(), ", last stack top = ", RawPointer(vm->lastStackTop()), ", in stack range [", RawPointer(stackBounds.origin()), ", ", RawPointer(stackBounds.end()), "]\n");
926     }
927 }
928
929 #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
930 char* VM::acquireRegExpPatternContexBuffer()
931 {
932     m_regExpPatternContextLock.lock();
933     ASSERT(m_regExpPatternContextLock.isLocked());
934     if (!m_regExpPatternContexBuffer)
935         m_regExpPatternContexBuffer = makeUniqueArray<char>(VM::patternContextBufferSize);
936     return m_regExpPatternContexBuffer.get();
937 }
938
939 void VM::releaseRegExpPatternContexBuffer()
940 {
941     ASSERT(m_regExpPatternContextLock.isLocked());
942
943     m_regExpPatternContextLock.unlock();
944 }
945 #endif
946
947 #if ENABLE(REGEXP_TRACING)
948 void VM::addRegExpToTrace(RegExp* regExp)
949 {
950     gcProtect(regExp);
951     m_rtTraceList->add(regExp);
952 }
953
954 void VM::dumpRegExpTrace()
955 {
956     // The first RegExp object is ignored.  It is create by the RegExpPrototype ctor and not used.
957     RTTraceList::iterator iter = ++m_rtTraceList->begin();
958     
959     if (iter != m_rtTraceList->end()) {
960         dataLogF("\nRegExp Tracing\n");
961         dataLogF("Regular Expression                              8 Bit          16 Bit        match()    Matches    Average\n");
962         dataLogF(" <Match only / Match>                         JIT Addr      JIT Address       calls      found   String len\n");
963         dataLogF("----------------------------------------+----------------+----------------+----------+----------+-----------\n");
964     
965         unsigned reCount = 0;
966     
967         for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
968             (*iter)->printTraceData();
969             gcUnprotect(*iter);
970         }
971
972         dataLogF("%d Regular Expressions\n", reCount);
973     }
974     
975     m_rtTraceList->clear();
976 }
977 #else
978 void VM::dumpRegExpTrace()
979 {
980 }
981 #endif
982
983 WatchpointSet* VM::ensureWatchpointSetForImpureProperty(const Identifier& propertyName)
984 {
985     auto result = m_impurePropertyWatchpointSets.add(propertyName.string(), nullptr);
986     if (result.isNewEntry)
987         result.iterator->value = adoptRef(new WatchpointSet(IsWatched));
988     return result.iterator->value.get();
989 }
990
991 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
992 {
993     ensureWatchpointSetForImpureProperty(propertyName)->add(watchpoint);
994 }
995
996 void VM::addImpureProperty(const String& propertyName)
997 {
998     if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
999         watchpointSet->fireAll(*this, "Impure property added");
1000 }
1001
1002 static bool enableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doEnableWork)
1003 {
1004     bool needsToRecompile = false;
1005     if (!counter) {
1006         doEnableWork();
1007         needsToRecompile = true;
1008     }
1009     counter++;
1010
1011     return needsToRecompile;
1012 }
1013
1014 static bool disableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doDisableWork)
1015 {
1016     RELEASE_ASSERT(counter > 0);
1017     bool needsToRecompile = false;
1018     counter--;
1019     if (!counter) {
1020         doDisableWork();
1021         needsToRecompile = true;
1022     }
1023
1024     return needsToRecompile;
1025 }
1026
1027 bool VM::enableTypeProfiler()
1028 {
1029     auto enableTypeProfiler = [this] () {
1030         this->m_typeProfiler = std::make_unique<TypeProfiler>();
1031         this->m_typeProfilerLog = std::make_unique<TypeProfilerLog>();
1032     };
1033
1034     return enableProfilerWithRespectToCount(m_typeProfilerEnabledCount, enableTypeProfiler);
1035 }
1036
1037 bool VM::disableTypeProfiler()
1038 {
1039     auto disableTypeProfiler = [this] () {
1040         this->m_typeProfiler.reset(nullptr);
1041         this->m_typeProfilerLog.reset(nullptr);
1042     };
1043
1044     return disableProfilerWithRespectToCount(m_typeProfilerEnabledCount, disableTypeProfiler);
1045 }
1046
1047 bool VM::enableControlFlowProfiler()
1048 {
1049     auto enableControlFlowProfiler = [this] () {
1050         this->m_controlFlowProfiler = std::make_unique<ControlFlowProfiler>();
1051     };
1052
1053     return enableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, enableControlFlowProfiler);
1054 }
1055
1056 bool VM::disableControlFlowProfiler()
1057 {
1058     auto disableControlFlowProfiler = [this] () {
1059         this->m_controlFlowProfiler.reset(nullptr);
1060     };
1061
1062     return disableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, disableControlFlowProfiler);
1063 }
1064
1065 void VM::dumpTypeProfilerData()
1066 {
1067     if (!typeProfiler())
1068         return;
1069
1070     typeProfilerLog()->processLogEntries(ASCIILiteral("VM Dump Types"));
1071     typeProfiler()->dumpTypeProfilerData(*this);
1072 }
1073
1074 void VM::queueMicrotask(JSGlobalObject& globalObject, Ref<Microtask>&& task)
1075 {
1076     m_microtaskQueue.append(std::make_unique<QueuedTask>(*this, &globalObject, WTFMove(task)));
1077 }
1078
1079 void VM::drainMicrotasks()
1080 {
1081     while (!m_microtaskQueue.isEmpty())
1082         m_microtaskQueue.takeFirst()->run();
1083 }
1084
1085 void QueuedTask::run()
1086 {
1087     m_microtask->run(m_globalObject->globalExec());
1088 }
1089
1090 void sanitizeStackForVM(VM* vm)
1091 {
1092     logSanitizeStack(vm);
1093     if (vm->topCallFrame) {
1094         auto& stackBounds = Thread::current().stack();
1095         ASSERT(vm->currentThreadIsHoldingAPILock());
1096         ASSERT_UNUSED(stackBounds, stackBounds.contains(vm->lastStackTop()));
1097     }
1098 #if !ENABLE(JIT)
1099     vm->interpreter->cloopStack().sanitizeStack();
1100 #else
1101     sanitizeStackForVMImpl(vm);
1102 #endif
1103 }
1104
1105 size_t VM::committedStackByteCount()
1106 {
1107 #if ENABLE(JIT)
1108     // When using the C stack, we don't know how many stack pages are actually
1109     // committed. So, we use the current stack usage as an estimate.
1110     ASSERT(Thread::current().stack().isGrowingDownward());
1111     int8_t* current = reinterpret_cast<int8_t*>(&current);
1112     int8_t* high = reinterpret_cast<int8_t*>(Thread::current().stack().origin());
1113     return high - current;
1114 #else
1115     return CLoopStack::committedByteCount();
1116 #endif
1117 }
1118
1119 #if !ENABLE(JIT)
1120 bool VM::ensureStackCapacityForCLoop(Register* newTopOfStack)
1121 {
1122     return interpreter->cloopStack().ensureCapacityFor(newTopOfStack);
1123 }
1124
1125 bool VM::isSafeToRecurseSoftCLoop() const
1126 {
1127     return interpreter->cloopStack().isSafeToRecurse();
1128 }
1129 #endif // !ENABLE(JIT)
1130
1131 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
1132 void VM::verifyExceptionCheckNeedIsSatisfied(unsigned recursionDepth, ExceptionEventLocation& location)
1133 {
1134     if (!Options::validateExceptionChecks())
1135         return;
1136
1137     if (UNLIKELY(m_needExceptionCheck)) {
1138         auto throwDepth = m_simulatedThrowPointRecursionDepth;
1139         auto& throwLocation = m_simulatedThrowPointLocation;
1140
1141         dataLog(
1142             "ERROR: Unchecked JS exception:\n"
1143             "    This scope can throw a JS exception: ", throwLocation, "\n"
1144             "        (ExceptionScope::m_recursionDepth was ", throwDepth, ")\n"
1145             "    But the exception was unchecked as of this scope: ", location, "\n"
1146             "        (ExceptionScope::m_recursionDepth was ", recursionDepth, ")\n"
1147             "\n");
1148
1149         StringPrintStream out;
1150         std::unique_ptr<StackTrace> currentTrace = StackTrace::captureStackTrace(Options::unexpectedExceptionStackTraceLimit());
1151
1152         if (Options::dumpSimulatedThrows()) {
1153             out.println("The simulated exception was thrown at:");
1154             m_nativeStackTraceOfLastSimulatedThrow->dump(out, "    ");
1155             out.println();
1156         }
1157         out.println("Unchecked exception detected at:");
1158         currentTrace->dump(out, "    ");
1159         out.println();
1160
1161         dataLog(out.toCString());
1162         RELEASE_ASSERT(!m_needExceptionCheck);
1163     }
1164 }
1165 #endif
1166
1167 #if USE(CF)
1168 void VM::registerRunLoopTimer(JSRunLoopTimer* timer)
1169 {
1170     ASSERT(runLoop());
1171     ASSERT(!m_runLoopTimers.contains(timer));
1172     m_runLoopTimers.add(timer);
1173     timer->setRunLoop(runLoop());
1174 }
1175
1176 void VM::unregisterRunLoopTimer(JSRunLoopTimer* timer)
1177 {
1178     ASSERT(m_runLoopTimers.contains(timer));
1179     m_runLoopTimers.remove(timer);
1180     timer->setRunLoop(nullptr);
1181 }
1182
1183 void VM::setRunLoop(CFRunLoopRef runLoop)
1184 {
1185     ASSERT(runLoop);
1186     m_runLoop = runLoop;
1187     for (auto timer : m_runLoopTimers)
1188         timer->setRunLoop(runLoop);
1189 }
1190 #endif // USE(CF)
1191
1192 ScratchBuffer* VM::scratchBufferForSize(size_t size)
1193 {
1194     if (!size)
1195         return nullptr;
1196
1197     auto locker = holdLock(m_scratchBufferLock);
1198
1199     if (size > m_sizeOfLastScratchBuffer) {
1200         // Protect against a N^2 memory usage pathology by ensuring
1201         // that at worst, we get a geometric series, meaning that the
1202         // total memory usage is somewhere around
1203         // max(scratch buffer size) * 4.
1204         m_sizeOfLastScratchBuffer = size * 2;
1205
1206         ScratchBuffer* newBuffer = ScratchBuffer::create(m_sizeOfLastScratchBuffer);
1207         RELEASE_ASSERT(newBuffer);
1208         m_scratchBuffers.append(newBuffer);
1209     }
1210
1211     ScratchBuffer* result = m_scratchBuffers.last();
1212     return result;
1213 }
1214
1215 } // namespace JSC