[JSC] Drop direct references to Intl constructors by rewriting Intl JS builtins in C++
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSGlobalObject.h
1 /*
2  *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3  *  Copyright (C) 2007-2018 Apple Inc. All rights reserved.
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Library General Public License
16  *  along with this library; see the file COPYING.LIB.  If not, write to
17  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  *  Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #pragma once
23
24 #include "ArrayAllocationProfile.h"
25 #include "ArrayBufferSharingMode.h"
26 #include "BigIntPrototype.h"
27 #include "BooleanPrototype.h"
28 #include "ErrorType.h"
29 #include "ExceptionHelpers.h"
30 #include "InternalFunction.h"
31 #include "JSArray.h"
32 #include "JSArrayBufferPrototype.h"
33 #include "JSCPoison.h"
34 #include "JSClassRef.h"
35 #include "JSGlobalLexicalEnvironment.h"
36 #include "JSPromiseDeferred.h"
37 #include "JSSegmentedVariableObject.h"
38 #include "JSWeakObjectMapRefInternal.h"
39 #include "LazyProperty.h"
40 #include "LazyClassStructure.h"
41 #include "NumberPrototype.h"
42 #include "RegExpGlobalData.h"
43 #include "RuntimeFlags.h"
44 #include "SpecialPointer.h"
45 #include "StringPrototype.h"
46 #include "SymbolPrototype.h"
47 #include "VM.h"
48 #include "Watchpoint.h"
49 #include <JavaScriptCore/JSBase.h>
50 #include <array>
51 #include <wtf/HashSet.h>
52 #include <wtf/PoisonedUniquePtr.h>
53 #include <wtf/RetainPtr.h>
54
55 struct OpaqueJSClass;
56 struct OpaqueJSClassContextData;
57 OBJC_CLASS JSWrapperMap;
58
59 namespace Inspector {
60 class JSGlobalObjectInspectorController;
61 }
62
63 namespace JSC {
64 class ArrayConstructor;
65 class ArrayPrototype;
66 class AsyncIteratorPrototype;
67 class AsyncFunctionPrototype;
68 class AsyncGeneratorPrototype;
69 class AsyncGeneratorFunctionPrototype;
70 class BooleanPrototype;
71 class ConsoleClient;
72 class Debugger;
73 class ErrorConstructor;
74 class ErrorPrototype;
75 class EvalCodeBlock;
76 class EvalExecutable;
77 class FunctionConstructor;
78 class FunctionPrototype;
79 class GeneratorPrototype;
80 class GeneratorFunctionPrototype;
81 class GetterSetter;
82 class GlobalCodeBlock;
83 class IndirectEvalExecutable;
84 class InputCursor;
85 class IntlObject;
86 class IntlCollator;
87 class JSArrayBuffer;
88 class JSArrayBufferPrototype;
89 class JSCallee;
90 class JSGlobalObjectDebuggable;
91 class JSInternalPromise;
92 class JSModuleLoader;
93 class JSModuleRecord;
94 class JSPromise;
95 class JSPromiseConstructor;
96 class JSPromisePrototype;
97 class JSSharedArrayBuffer;
98 class JSSharedArrayBufferPrototype;
99 class JSTypedArrayViewConstructor;
100 class JSTypedArrayViewPrototype;
101 class DirectEvalExecutable;
102 class LLIntOffsetsExtractor;
103 class MapPrototype;
104 class Microtask;
105 class ModuleLoader;
106 class ModuleProgramExecutable;
107 class NativeErrorConstructorBase;
108 class NullGetterFunction;
109 class NullSetterFunction;
110 class ObjectConstructor;
111 class ProgramCodeBlock;
112 class ProgramExecutable;
113 class RegExpConstructor;
114 class RegExpPrototype;
115 class SetPrototype;
116 class SourceCode;
117 class SourceOrigin;
118 class UnlinkedModuleProgramCodeBlock;
119 class VariableEnvironment;
120 struct ActivationStackNode;
121 struct HashTable;
122
123 #ifdef JSC_GLIB_API_ENABLED
124 class WrapperMap;
125 #endif
126
127 template<typename Watchpoint> class ObjectPropertyChangeAdaptiveWatchpoint;
128
129 #define DEFINE_STANDARD_BUILTIN(macro, upperName, lowerName) macro(upperName, lowerName, lowerName, JS ## upperName, upperName, object)
130
131 #define FOR_EACH_SIMPLE_BUILTIN_TYPE_WITH_CONSTRUCTOR(macro) \
132     macro(String, string, stringObject, StringObject, String, object) \
133     macro(Error, error, error, ErrorInstance, Error, object) \
134     macro(Map, map, map, JSMap, Map, object) \
135     macro(Set, set, set, JSSet, Set, object) \
136     macro(JSPromise, promise, promise, JSPromise, Promise, object)
137
138 #define FOR_BIG_INT_BUILTIN_TYPE_WITH_CONSTRUCTOR(macro) \
139     macro(BigInt, bigInt, bigIntObject, BigIntObject, BigInt, object)
140
141 #define FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(macro) \
142     macro(StringIterator, stringIterator, stringIterator, JSStringIterator, StringIterator, iterator) \
143
144 #define FOR_EACH_SIMPLE_BUILTIN_TYPE(macro) \
145     FOR_EACH_SIMPLE_BUILTIN_TYPE_WITH_CONSTRUCTOR(macro) \
146     macro(JSInternalPromise, internalPromise, internalPromise, JSInternalPromise, InternalPromise, object) \
147
148 #define FOR_EACH_LAZY_BUILTIN_TYPE(macro) \
149     macro(Boolean, boolean, booleanObject, BooleanObject, Boolean, object) \
150     macro(Date, date, date, DateInstance, Date, object) \
151     macro(Number, number, numberObject, NumberObject, Number, object) \
152     macro(Symbol, symbol, symbolObject, SymbolObject, Symbol, object) \
153     DEFINE_STANDARD_BUILTIN(macro, WeakMap, weakMap) \
154     DEFINE_STANDARD_BUILTIN(macro, WeakSet, weakSet) \
155
156 #if ENABLE(WEBASSEMBLY)
157 #define FOR_EACH_WEBASSEMBLY_CONSTRUCTOR_TYPE(macro) \
158     macro(WebAssemblyCompileError, webAssemblyCompileError, WebAssemblyCompileError, WebAssemblyCompileError, CompileError, error) \
159     macro(WebAssemblyInstance,     webAssemblyInstance,     WebAssemblyInstance,     WebAssemblyInstance,     Instance,     object) \
160     macro(WebAssemblyLinkError,    webAssemblyLinkError,    WebAssemblyLinkError,    WebAssemblyLinkError,    LinkError,    error) \
161     macro(WebAssemblyMemory,       webAssemblyMemory,       WebAssemblyMemory,       WebAssemblyMemory,       Memory,       object) \
162     macro(WebAssemblyModule,       webAssemblyModule,       WebAssemblyModule,       WebAssemblyModule,       Module,       object) \
163     macro(WebAssemblyRuntimeError, webAssemblyRuntimeError, WebAssemblyRuntimeError, WebAssemblyRuntimeError, RuntimeError, error) \
164     macro(WebAssemblyTable,        webAssemblyTable,        WebAssemblyTable,        WebAssemblyTable,        Table,        object)
165 #else
166 #define FOR_EACH_WEBASSEMBLY_CONSTRUCTOR_TYPE(macro)
167 #endif // ENABLE(WEBASSEMBLY)
168
169 #define DECLARE_SIMPLE_BUILTIN_TYPE(capitalName, lowerName, properName, instanceType, jsName, prototypeBase) \
170     class JS ## capitalName; \
171     class capitalName ## Prototype; \
172     class capitalName ## Constructor;
173
174 class IteratorPrototype;
175 FOR_EACH_SIMPLE_BUILTIN_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
176 FOR_BIG_INT_BUILTIN_TYPE_WITH_CONSTRUCTOR(DECLARE_SIMPLE_BUILTIN_TYPE)
177 FOR_EACH_LAZY_BUILTIN_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
178 FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
179 FOR_EACH_WEBASSEMBLY_CONSTRUCTOR_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
180
181 #undef DECLARE_SIMPLE_BUILTIN_TYPE
182
183 enum class JSPromiseRejectionOperation : unsigned {
184     Reject, // When a promise is rejected without any handlers.
185     Handle, // When a handler is added to a rejected promise for the first time.
186 };
187
188 struct GlobalObjectMethodTable {
189     typedef bool (*SupportsRichSourceInfoFunctionPtr)(const JSGlobalObject*);
190     SupportsRichSourceInfoFunctionPtr supportsRichSourceInfo;
191
192     typedef bool (*ShouldInterruptScriptFunctionPtr)(const JSGlobalObject*);
193     ShouldInterruptScriptFunctionPtr shouldInterruptScript;
194
195     typedef RuntimeFlags (*JavaScriptRuntimeFlagsFunctionPtr)(const JSGlobalObject*);
196     JavaScriptRuntimeFlagsFunctionPtr javaScriptRuntimeFlags;
197
198     typedef void (*QueueTaskToEventLoopFunctionPtr)(JSGlobalObject&, Ref<Microtask>&&);
199     QueueTaskToEventLoopFunctionPtr queueTaskToEventLoop;
200
201     typedef bool (*ShouldInterruptScriptBeforeTimeoutPtr)(const JSGlobalObject*);
202     ShouldInterruptScriptBeforeTimeoutPtr shouldInterruptScriptBeforeTimeout;
203
204     typedef JSInternalPromise* (*ModuleLoaderImportModulePtr)(JSGlobalObject*, ExecState*, JSModuleLoader*, JSString*, JSValue, const SourceOrigin&);
205     ModuleLoaderImportModulePtr moduleLoaderImportModule;
206
207     typedef Identifier (*ModuleLoaderResolvePtr)(JSGlobalObject*, ExecState*, JSModuleLoader*, JSValue, JSValue, JSValue);
208     ModuleLoaderResolvePtr moduleLoaderResolve;
209
210     typedef JSInternalPromise* (*ModuleLoaderFetchPtr)(JSGlobalObject*, ExecState*, JSModuleLoader*, JSValue, JSValue, JSValue);
211     ModuleLoaderFetchPtr moduleLoaderFetch;
212
213     typedef JSObject* (*ModuleLoaderCreateImportMetaPropertiesPtr)(JSGlobalObject*, ExecState*, JSModuleLoader*, JSValue, JSModuleRecord*, JSValue);
214     ModuleLoaderCreateImportMetaPropertiesPtr moduleLoaderCreateImportMetaProperties;
215
216     typedef JSValue (*ModuleLoaderEvaluatePtr)(JSGlobalObject*, ExecState*, JSModuleLoader*, JSValue, JSValue, JSValue);
217     ModuleLoaderEvaluatePtr moduleLoaderEvaluate;
218
219     typedef void (*PromiseRejectionTrackerPtr)(JSGlobalObject*, ExecState*, JSPromise*, JSPromiseRejectionOperation);
220     PromiseRejectionTrackerPtr promiseRejectionTracker;
221
222     typedef String (*DefaultLanguageFunctionPtr)();
223     DefaultLanguageFunctionPtr defaultLanguage;
224
225     typedef void (*CompileStreamingPtr)(JSGlobalObject*, ExecState*, JSPromiseDeferred*, JSValue);
226     CompileStreamingPtr compileStreaming;
227
228     typedef void (*InstantiateStreamingPtr)(JSGlobalObject*, ExecState*, JSPromiseDeferred*, JSValue, JSObject*);
229     InstantiateStreamingPtr instantiateStreaming;
230 };
231
232 class JSGlobalObject : public JSSegmentedVariableObject {
233 private:
234     typedef HashSet<RefPtr<OpaqueJSWeakObjectMap>> WeakMapSet;
235     typedef HashMap<OpaqueJSClass*, std::unique_ptr<OpaqueJSClassContextData>> OpaqueJSClassDataMap;
236
237     struct JSGlobalObjectRareData {
238         WTF_MAKE_FAST_ALLOCATED;
239     public:
240         JSGlobalObjectRareData()
241             : profileGroup(0)
242         {
243         }
244
245         WeakMapSet weakMaps;
246         unsigned profileGroup;
247         
248         OpaqueJSClassDataMap opaqueJSClassData;
249     };
250
251 // Our hashtable code-generator tries to access these properties, so we make them public.
252 // However, we'd like it better if they could be protected.
253 public:
254     template<typename T> using Initializer = typename LazyProperty<JSGlobalObject, T>::Initializer;
255     
256     Register m_globalCallFrame[CallFrame::headerSizeInRegisters];
257
258     WriteBarrier<JSObject> m_globalThis;
259
260     WriteBarrier<JSGlobalLexicalEnvironment> m_globalLexicalEnvironment;
261     WriteBarrier<JSScope> m_globalScopeExtension;
262     WriteBarrier<JSCallee> m_globalCallee;
263     WriteBarrier<JSCallee> m_stackOverflowFrameCallee;
264
265     WriteBarrier<ErrorConstructor> m_errorConstructor;
266     LazyClassStructure m_evalErrorStructure;
267     LazyClassStructure m_rangeErrorStructure;
268     LazyClassStructure m_referenceErrorStructure;
269     LazyClassStructure m_syntaxErrorStructure;
270     LazyClassStructure m_typeErrorStructure;
271     LazyClassStructure m_URIErrorStructure;
272
273     WriteBarrier<ObjectConstructor> m_objectConstructor;
274     WriteBarrier<ArrayConstructor> m_arrayConstructor;
275     WriteBarrier<JSPromiseConstructor> m_promiseConstructor;
276     WriteBarrier<JSInternalPromiseConstructor> m_internalPromiseConstructor;
277
278 #if ENABLE(INTL)
279     WriteBarrier<IntlObject> m_intlObject;
280     WriteBarrier<IntlCollator> m_defaultCollator;
281 #endif
282     WriteBarrier<NullGetterFunction> m_nullGetterFunction;
283     WriteBarrier<NullSetterFunction> m_nullSetterFunction;
284
285     WriteBarrier<JSFunction> m_parseIntFunction;
286     WriteBarrier<JSFunction> m_parseFloatFunction;
287
288     WriteBarrier<JSFunction> m_evalFunction;
289     WriteBarrier<JSFunction> m_callFunction;
290     WriteBarrier<JSFunction> m_applyFunction;
291     WriteBarrier<JSFunction> m_throwTypeErrorFunction;
292     LazyProperty<JSGlobalObject, JSFunction> m_arrayProtoToStringFunction;
293     LazyProperty<JSGlobalObject, JSFunction> m_arrayProtoValuesFunction;
294     LazyProperty<JSGlobalObject, JSFunction> m_initializePromiseFunction;
295     LazyProperty<JSGlobalObject, JSFunction> m_iteratorProtocolFunction;
296     LazyProperty<JSGlobalObject, JSFunction> m_promiseResolveFunction;
297     WriteBarrier<JSFunction> m_objectProtoValueOfFunction;
298     WriteBarrier<JSFunction> m_numberProtoToStringFunction;
299     WriteBarrier<JSFunction> m_newPromiseCapabilityFunction;
300     WriteBarrier<JSFunction> m_functionProtoHasInstanceSymbolFunction;
301     LazyProperty<JSGlobalObject, GetterSetter> m_throwTypeErrorGetterSetter;
302     WriteBarrier<JSObject> m_regExpProtoExec;
303     WriteBarrier<JSObject> m_regExpProtoSymbolReplace;
304     WriteBarrier<JSObject> m_regExpProtoGlobalGetter;
305     WriteBarrier<JSObject> m_regExpProtoUnicodeGetter;
306     WriteBarrier<GetterSetter> m_throwTypeErrorArgumentsCalleeAndCallerGetterSetter;
307
308     LazyProperty<JSGlobalObject, JSModuleLoader> m_moduleLoader;
309
310     WriteBarrier<ObjectPrototype> m_objectPrototype;
311     WriteBarrier<FunctionPrototype> m_functionPrototype;
312     WriteBarrier<ArrayPrototype> m_arrayPrototype;
313     WriteBarrier<RegExpPrototype> m_regExpPrototype;
314     WriteBarrier<IteratorPrototype> m_iteratorPrototype;
315     WriteBarrier<AsyncIteratorPrototype> m_asyncIteratorPrototype;
316     WriteBarrier<GeneratorFunctionPrototype> m_generatorFunctionPrototype;
317     WriteBarrier<GeneratorPrototype> m_generatorPrototype;
318     WriteBarrier<AsyncGeneratorPrototype> m_asyncGeneratorPrototype;
319
320     LazyProperty<JSGlobalObject, Structure> m_debuggerScopeStructure;
321     LazyProperty<JSGlobalObject, Structure> m_withScopeStructure;
322     WriteBarrier<Structure> m_strictEvalActivationStructure;
323     WriteBarrier<Structure> m_lexicalEnvironmentStructure;
324     LazyProperty<JSGlobalObject, Structure> m_moduleEnvironmentStructure;
325     WriteBarrier<Structure> m_directArgumentsStructure;
326     WriteBarrier<Structure> m_scopedArgumentsStructure;
327     WriteBarrier<Structure> m_clonedArgumentsStructure;
328
329     WriteBarrier<Structure> m_objectStructureForObjectConstructor;
330         
331     // Lists the actual structures used for having these particular indexing shapes.
332     WriteBarrier<Structure> m_originalArrayStructureForIndexingShape[NumberOfArrayIndexingModes];
333     // Lists the structures we should use during allocation for these particular indexing shapes.
334     // These structures will differ from the originals list above when we are having a bad time.
335     WriteBarrier<Structure> m_arrayStructureForIndexingShapeDuringAllocation[NumberOfArrayIndexingModes];
336
337     LazyProperty<JSGlobalObject, Structure> m_callbackConstructorStructure;
338     LazyProperty<JSGlobalObject, Structure> m_callbackFunctionStructure;
339     LazyProperty<JSGlobalObject, Structure> m_callbackObjectStructure;
340 #if JSC_OBJC_API_ENABLED
341     LazyProperty<JSGlobalObject, Structure> m_objcCallbackFunctionStructure;
342     LazyProperty<JSGlobalObject, Structure> m_objcWrapperObjectStructure;
343 #endif
344 #ifdef JSC_GLIB_API_ENABLED
345     LazyProperty<JSGlobalObject, Structure> m_glibCallbackFunctionStructure;
346     LazyProperty<JSGlobalObject, Structure> m_glibWrapperObjectStructure;
347 #endif
348     WriteBarrier<Structure> m_nullPrototypeObjectStructure;
349     WriteBarrier<Structure> m_calleeStructure;
350
351     WriteBarrier<Structure> m_hostFunctionStructure;
352
353     struct FunctionStructures {
354         WriteBarrier<Structure> arrowFunctionStructure;
355         WriteBarrier<Structure> sloppyFunctionStructure;
356         WriteBarrier<Structure> strictFunctionStructure;
357     };
358     FunctionStructures m_builtinFunctions;
359     FunctionStructures m_ordinaryFunctions;
360
361     LazyProperty<JSGlobalObject, Structure> m_boundFunctionStructure;
362     LazyProperty<JSGlobalObject, Structure> m_customGetterSetterFunctionStructure;
363     WriteBarrier<Structure> m_getterSetterStructure;
364     LazyProperty<JSGlobalObject, Structure> m_nativeStdFunctionStructure;
365     PropertyOffset m_functionNameOffset;
366     WriteBarrier<Structure> m_regExpStructure;
367     WriteBarrier<AsyncFunctionPrototype> m_asyncFunctionPrototype;
368     WriteBarrier<AsyncGeneratorFunctionPrototype> m_asyncGeneratorFunctionPrototype;
369     WriteBarrier<Structure> m_asyncFunctionStructure;
370     WriteBarrier<Structure> m_asyncGeneratorFunctionStructure;
371     WriteBarrier<Structure> m_generatorFunctionStructure;
372     WriteBarrier<Structure> m_iteratorResultObjectStructure;
373     WriteBarrier<Structure> m_regExpMatchesArrayStructure;
374     WriteBarrier<Structure> m_regExpMatchesArrayWithGroupsStructure;
375     WriteBarrier<Structure> m_moduleRecordStructure;
376     WriteBarrier<Structure> m_moduleNamespaceObjectStructure;
377     WriteBarrier<Structure> m_proxyObjectStructure;
378     WriteBarrier<Structure> m_callableProxyObjectStructure;
379     WriteBarrier<Structure> m_proxyRevokeStructure;
380     WriteBarrier<JSArrayBufferPrototype> m_arrayBufferPrototype;
381     WriteBarrier<Structure> m_arrayBufferStructure;
382 #if ENABLE(SHARED_ARRAY_BUFFER)
383     WriteBarrier<JSArrayBufferPrototype> m_sharedArrayBufferPrototype;
384     WriteBarrier<Structure> m_sharedArrayBufferStructure;
385 #endif
386
387 #define DEFINE_STORAGE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName, prototypeBase) \
388     WriteBarrier<capitalName ## Prototype> m_ ## lowerName ## Prototype; \
389     WriteBarrier<Structure> m_ ## properName ## Structure;
390
391     FOR_EACH_SIMPLE_BUILTIN_TYPE(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
392     FOR_BIG_INT_BUILTIN_TYPE_WITH_CONSTRUCTOR(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
393     FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
394     
395 #if ENABLE(WEBASSEMBLY)
396     WriteBarrier<Structure> m_webAssemblyStructure;
397     WriteBarrier<Structure> m_webAssemblyModuleRecordStructure;
398     WriteBarrier<Structure> m_webAssemblyFunctionStructure;
399     WriteBarrier<Structure> m_webAssemblyWrapperFunctionStructure;
400     WriteBarrier<Structure> m_webAssemblyToJSCalleeStructure;
401     FOR_EACH_WEBASSEMBLY_CONSTRUCTOR_TYPE(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
402 #endif // ENABLE(WEBASSEMBLY)
403
404 #undef DEFINE_STORAGE_FOR_SIMPLE_TYPE
405
406 #define DEFINE_STORAGE_FOR_LAZY_TYPE(capitalName, lowerName, properName, instanceType, jsName, prototypeBase) \
407     LazyClassStructure m_ ## properName ## Structure;
408     FOR_EACH_LAZY_BUILTIN_TYPE(DEFINE_STORAGE_FOR_LAZY_TYPE)
409 #undef DEFINE_STORAGE_FOR_LAZY_TYPE
410
411     WriteBarrier<GetterSetter> m_speciesGetterSetter;
412     
413     LazyProperty<JSGlobalObject, JSTypedArrayViewPrototype> m_typedArrayProto;
414     LazyProperty<JSGlobalObject, JSTypedArrayViewConstructor> m_typedArraySuperConstructor;
415     
416 #define DECLARE_TYPED_ARRAY_TYPE_STRUCTURE(name) LazyClassStructure m_typedArray ## name;
417     FOR_EACH_TYPED_ARRAY_TYPE(DECLARE_TYPED_ARRAY_TYPE_STRUCTURE)
418 #undef DECLARE_TYPED_ARRAY_TYPE_STRUCTURE
419
420     JSCell* m_specialPointers[Special::TableSize]; // Special pointers used by the LLInt and JIT.
421     JSCell* m_linkTimeConstants[LinkTimeConstantCount];
422
423     String m_name;
424
425     Debugger* m_debugger;
426
427     VM& m_vm;
428
429     template<typename T> using PoisonedUniquePtr = WTF::PoisonedUniquePtr<JSGlobalObjectPoison, T>;
430
431 #if ENABLE(REMOTE_INSPECTOR)
432     PoisonedUniquePtr<Inspector::JSGlobalObjectInspectorController> m_inspectorController;
433     PoisonedUniquePtr<JSGlobalObjectDebuggable> m_inspectorDebuggable;
434 #endif
435
436 #if ENABLE(INTL)
437     HashSet<String> m_intlCollatorAvailableLocales;
438     HashSet<String> m_intlDateTimeFormatAvailableLocales;
439     HashSet<String> m_intlNumberFormatAvailableLocales;
440     HashSet<String> m_intlPluralRulesAvailableLocales;
441 #endif // ENABLE(INTL)
442
443     RefPtr<WatchpointSet> m_masqueradesAsUndefinedWatchpoint;
444     RefPtr<WatchpointSet> m_havingABadTimeWatchpoint;
445     RefPtr<WatchpointSet> m_varInjectionWatchpoint;
446
447     std::unique_ptr<JSGlobalObjectRareData> m_rareData;
448
449     WeakRandom m_weakRandom;
450     RegExpGlobalData m_regExpGlobalData;
451
452     JSCallee* stackOverflowFrameCallee() const { return m_stackOverflowFrameCallee.get(); }
453
454     InlineWatchpointSet& arrayIteratorProtocolWatchpoint() { return m_arrayIteratorProtocolWatchpoint; }
455     InlineWatchpointSet& mapIteratorProtocolWatchpoint() { return m_mapIteratorProtocolWatchpoint; }
456     InlineWatchpointSet& setIteratorProtocolWatchpoint() { return m_setIteratorProtocolWatchpoint; }
457     InlineWatchpointSet& stringIteratorProtocolWatchpoint() { return m_stringIteratorProtocolWatchpoint; }
458     InlineWatchpointSet& mapSetWatchpoint() { return m_mapSetWatchpoint; }
459     InlineWatchpointSet& setAddWatchpoint() { return m_setAddWatchpoint; }
460     InlineWatchpointSet& arraySpeciesWatchpoint() { return m_arraySpeciesWatchpoint; }
461     InlineWatchpointSet& numberToStringWatchpoint()
462     {
463         RELEASE_ASSERT(VM::canUseJIT());
464         return m_numberToStringWatchpoint;
465     }
466     // If this hasn't been invalidated, it means the array iterator protocol
467     // is not observable to user code yet.
468     InlineWatchpointSet m_arrayIteratorProtocolWatchpoint;
469     InlineWatchpointSet m_mapIteratorProtocolWatchpoint;
470     InlineWatchpointSet m_setIteratorProtocolWatchpoint;
471     InlineWatchpointSet m_stringIteratorProtocolWatchpoint;
472     InlineWatchpointSet m_mapSetWatchpoint;
473     InlineWatchpointSet m_setAddWatchpoint;
474     InlineWatchpointSet m_arraySpeciesWatchpoint;
475     InlineWatchpointSet m_numberToStringWatchpoint;
476     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_arrayPrototypeSymbolIteratorWatchpoint;
477     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_arrayIteratorPrototypeNext;
478     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_mapPrototypeSymbolIteratorWatchpoint;
479     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_mapIteratorPrototypeNextWatchpoint;
480     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_setPrototypeSymbolIteratorWatchpoint;
481     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_setIteratorPrototypeNextWatchpoint;
482     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_stringPrototypeSymbolIteratorWatchpoint;
483     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_stringIteratorPrototypeNextWatchpoint;
484     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_mapPrototypeSetWatchpoint;
485     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_setPrototypeAddWatchpoint;
486     PoisonedUniquePtr<ObjectPropertyChangeAdaptiveWatchpoint<InlineWatchpointSet>> m_numberPrototypeToStringWatchpoint;
487
488     bool isArrayPrototypeIteratorProtocolFastAndNonObservable();
489     bool isMapPrototypeIteratorProtocolFastAndNonObservable();
490     bool isSetPrototypeIteratorProtocolFastAndNonObservable();
491     bool isStringPrototypeIteratorProtocolFastAndNonObservable();
492     bool isMapPrototypeSetFastAndNonObservable();
493     bool isSetPrototypeAddFastAndNonObservable();
494
495 #if ENABLE(DFG_JIT)
496     using ReferencedGlobalPropertyWatchpointSets = HashMap<RefPtr<UniquedStringImpl>, Ref<WatchpointSet>, IdentifierRepHash>;
497     ReferencedGlobalPropertyWatchpointSets m_referencedGlobalPropertyWatchpointSets;
498     ConcurrentJSLock m_referencedGlobalPropertyWatchpointSetsLock;
499 #endif
500
501     bool m_evalEnabled { true };
502     bool m_webAssemblyEnabled { true };
503     unsigned m_globalLexicalBindingEpoch { 1 };
504     String m_evalDisabledErrorMessage;
505     String m_webAssemblyDisabledErrorMessage;
506     RuntimeFlags m_runtimeFlags;
507     ConsoleClient* m_consoleClient { nullptr };
508     Optional<unsigned> m_stackTraceLimit;
509
510 #if !ASSERT_DISABLED
511     const ExecState* m_callFrameAtDebuggerEntry { nullptr };
512 #endif
513
514     static JS_EXPORT_PRIVATE const GlobalObjectMethodTable s_globalObjectMethodTable;
515     const GlobalObjectMethodTable* m_globalObjectMethodTable;
516
517     void createRareDataIfNeeded()
518     {
519         if (m_rareData)
520             return;
521         m_rareData = std::make_unique<JSGlobalObjectRareData>();
522     }
523         
524 public:
525     typedef JSSegmentedVariableObject Base;
526     static const unsigned StructureFlags = Base::StructureFlags | HasStaticPropertyTable | OverridesGetOwnPropertySlot | OverridesGetPropertyNames | IsImmutablePrototypeExoticObject;
527
528     JS_EXPORT_PRIVATE static JSGlobalObject* create(VM&, Structure*);
529
530     DECLARE_EXPORT_INFO;
531
532     bool hasDebugger() const;
533     bool hasInteractiveDebugger() const;
534     const RuntimeFlags& runtimeFlags() const { return m_runtimeFlags; }
535
536 #if ENABLE(DFG_JIT)
537     WatchpointSet* getReferencedPropertyWatchpointSet(UniquedStringImpl*);
538     WatchpointSet& ensureReferencedPropertyWatchpointSet(UniquedStringImpl*);
539 #endif
540
541     Optional<unsigned> stackTraceLimit() const { return m_stackTraceLimit; }
542     void setStackTraceLimit(Optional<unsigned> value) { m_stackTraceLimit = value; }
543
544 protected:
545     JS_EXPORT_PRIVATE explicit JSGlobalObject(VM&, Structure*, const GlobalObjectMethodTable* = nullptr);
546
547     JS_EXPORT_PRIVATE void finishCreation(VM&);
548
549     JS_EXPORT_PRIVATE void finishCreation(VM&, JSObject*);
550
551     void addGlobalVar(const Identifier&);
552
553 public:
554     JS_EXPORT_PRIVATE ~JSGlobalObject();
555     JS_EXPORT_PRIVATE static void destroy(JSCell*);
556
557     JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
558
559     JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
560     JS_EXPORT_PRIVATE static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
561
562     JS_EXPORT_PRIVATE static void defineGetter(JSObject*, ExecState*, PropertyName, JSObject* getterFunc, unsigned attributes);
563     JS_EXPORT_PRIVATE static void defineSetter(JSObject*, ExecState*, PropertyName, JSObject* setterFunc, unsigned attributes);
564     JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
565
566     void addVar(ExecState* exec, const Identifier& propertyName)
567     {
568         if (!hasOwnProperty(exec, propertyName))
569             addGlobalVar(propertyName);
570     }
571     void addFunction(ExecState*, const Identifier&);
572
573     JSScope* globalScope() { return m_globalLexicalEnvironment.get(); }
574     JSGlobalLexicalEnvironment* globalLexicalEnvironment() { return m_globalLexicalEnvironment.get(); }
575
576     JSScope* globalScopeExtension() { return m_globalScopeExtension.get(); }
577     void setGlobalScopeExtension(JSScope*);
578     void clearGlobalScopeExtension();
579
580     // The following accessors return pristine values, even if a script 
581     // replaces the global object's associated property.
582
583     GetterSetter* speciesGetterSetter() const { return m_speciesGetterSetter.get(); }
584
585     ArrayConstructor* arrayConstructor() const { return m_arrayConstructor.get(); }
586     ObjectConstructor* objectConstructor() const { return m_objectConstructor.get(); }
587     JSPromiseConstructor* promiseConstructor() const { return m_promiseConstructor.get(); }
588     JSInternalPromiseConstructor* internalPromiseConstructor() const { return m_internalPromiseConstructor.get(); }
589
590 #if ENABLE(INTL)
591     IntlObject* intlObject() const { return m_intlObject.get(); }
592     IntlCollator* defaultCollator(ExecState*);
593 #endif
594
595     NullGetterFunction* nullGetterFunction() const { return m_nullGetterFunction.get(); }
596     NullSetterFunction* nullSetterFunction() const { return m_nullSetterFunction.get(); }
597
598     JSFunction* parseIntFunction() const { return m_parseIntFunction.get(); }
599     JSFunction* parseFloatFunction() const { return m_parseFloatFunction.get(); }
600
601     JSFunction* evalFunction() const { return m_evalFunction.get(); }
602     JSFunction* callFunction() const { return m_callFunction.get(); }
603     JSFunction* applyFunction() const { return m_applyFunction.get(); }
604     JSFunction* throwTypeErrorFunction() const { return m_throwTypeErrorFunction.get(); }
605     JSFunction* arrayProtoToStringFunction() const { return m_arrayProtoToStringFunction.get(this); }
606     JSFunction* arrayProtoValuesFunction() const { return m_arrayProtoValuesFunction.get(this); }
607     JSFunction* initializePromiseFunction() const { return m_initializePromiseFunction.get(this); }
608     JSFunction* iteratorProtocolFunction() const { return m_iteratorProtocolFunction.get(this); }
609     JSFunction* promiseResolveFunction() const { return m_promiseResolveFunction.get(this); }
610     JSFunction* objectProtoValueOfFunction() const { return m_objectProtoValueOfFunction.get(); }
611     JSFunction* numberProtoToStringFunction() const { return m_numberProtoToStringFunction.get(); }
612     JSFunction* newPromiseCapabilityFunction() const { return m_newPromiseCapabilityFunction.get(); }
613     JSFunction* functionProtoHasInstanceSymbolFunction() const { return m_functionProtoHasInstanceSymbolFunction.get(); }
614     JSObject* regExpProtoExecFunction() const { return m_regExpProtoExec.get(); }
615     JSObject* regExpProtoSymbolReplaceFunction() const { return m_regExpProtoSymbolReplace.get(); }
616     JSObject* regExpProtoGlobalGetter() const { return m_regExpProtoGlobalGetter.get(); }
617     JSObject* regExpProtoUnicodeGetter() const { return m_regExpProtoUnicodeGetter.get(); }
618     GetterSetter* throwTypeErrorArgumentsCalleeAndCallerGetterSetter()
619     {
620         return m_throwTypeErrorArgumentsCalleeAndCallerGetterSetter.get();
621     }
622     
623     JSModuleLoader* moduleLoader() const { return m_moduleLoader.get(this); }
624
625     ObjectPrototype* objectPrototype() const { return m_objectPrototype.get(); }
626     FunctionPrototype* functionPrototype() const { return m_functionPrototype.get(); }
627     ArrayPrototype* arrayPrototype() const { return m_arrayPrototype.get(); }
628     JSObject* booleanPrototype() const { return m_booleanObjectStructure.prototypeInitializedOnMainThread(this); }
629     StringPrototype* stringPrototype() const { return m_stringPrototype.get(); }
630     JSObject* numberPrototype() const { return m_numberObjectStructure.prototypeInitializedOnMainThread(this); }
631     BigIntPrototype* bigIntPrototype() const { return m_bigIntPrototype.get(); }
632     JSObject* datePrototype() const { return m_dateStructure.prototype(this); }
633     JSObject* symbolPrototype() const { return m_symbolObjectStructure.prototypeInitializedOnMainThread(this); }
634     RegExpPrototype* regExpPrototype() const { return m_regExpPrototype.get(); }
635     ErrorPrototype* errorPrototype() const { return m_errorPrototype.get(); }
636     IteratorPrototype* iteratorPrototype() const { return m_iteratorPrototype.get(); }
637     AsyncIteratorPrototype* asyncIteratorPrototype() const { return m_asyncIteratorPrototype.get(); }
638     GeneratorFunctionPrototype* generatorFunctionPrototype() const { return m_generatorFunctionPrototype.get(); }
639     GeneratorPrototype* generatorPrototype() const { return m_generatorPrototype.get(); }
640     AsyncFunctionPrototype* asyncFunctionPrototype() const { return m_asyncFunctionPrototype.get(); }
641     MapPrototype* mapPrototype() const { return m_mapPrototype.get(); }
642     // Workaround for the name conflict between JSCell::setPrototype.
643     SetPrototype* jsSetPrototype() const { return m_setPrototype.get(); }
644     JSPromisePrototype* promisePrototype() const { return m_promisePrototype.get(); }
645     AsyncGeneratorPrototype* asyncGeneratorPrototype() const { return m_asyncGeneratorPrototype.get(); }
646     AsyncGeneratorFunctionPrototype* asyncGeneratorFunctionPrototype() const { return m_asyncGeneratorFunctionPrototype.get(); }
647
648     Structure* debuggerScopeStructure() const { return m_debuggerScopeStructure.get(this); }
649     Structure* withScopeStructure() const { return m_withScopeStructure.get(this); }
650     Structure* strictEvalActivationStructure() const { return m_strictEvalActivationStructure.get(); }
651     Structure* activationStructure() const { return m_lexicalEnvironmentStructure.get(); }
652     Structure* moduleEnvironmentStructure() const { return m_moduleEnvironmentStructure.get(this); }
653     Structure* directArgumentsStructure() const { return m_directArgumentsStructure.get(); }
654     Structure* scopedArgumentsStructure() const { return m_scopedArgumentsStructure.get(); }
655     Structure* clonedArgumentsStructure() const { return m_clonedArgumentsStructure.get(); }
656     Structure* objectStructureForObjectConstructor() const { return m_objectStructureForObjectConstructor.get(); }
657     Structure* originalArrayStructureForIndexingType(IndexingType indexingType) const
658     {
659         ASSERT(indexingType & IsArray);
660         return m_originalArrayStructureForIndexingShape[arrayIndexFromIndexingType(indexingType)].get();
661     }
662     Structure* arrayStructureForIndexingTypeDuringAllocation(IndexingType indexingType) const
663     {
664         ASSERT(indexingType & IsArray);
665         return m_arrayStructureForIndexingShapeDuringAllocation[arrayIndexFromIndexingType(indexingType)].get();
666     }
667     Structure* arrayStructureForIndexingTypeDuringAllocation(ExecState* exec, IndexingType indexingType, JSValue newTarget) const
668     {
669         return InternalFunction::createSubclassStructure(exec, newTarget, arrayStructureForIndexingTypeDuringAllocation(indexingType));
670     }
671     Structure* arrayStructureForProfileDuringAllocation(ExecState* exec, ArrayAllocationProfile* profile, JSValue newTarget) const
672     {
673         return arrayStructureForIndexingTypeDuringAllocation(exec, ArrayAllocationProfile::selectIndexingTypeFor(profile), newTarget);
674     }
675         
676     bool isOriginalArrayStructure(Structure* structure)
677     {
678         return originalArrayStructureForIndexingType(structure->indexingMode() | IsArray) == structure;
679     }
680         
681     Structure* booleanObjectStructure() const { return m_booleanObjectStructure.get(this); }
682     Structure* callbackConstructorStructure() const { return m_callbackConstructorStructure.get(this); }
683     Structure* callbackFunctionStructure() const { return m_callbackFunctionStructure.get(this); }
684     Structure* callbackObjectStructure() const { return m_callbackObjectStructure.get(this); }
685 #if JSC_OBJC_API_ENABLED
686     Structure* objcCallbackFunctionStructure() const { return m_objcCallbackFunctionStructure.get(this); }
687     Structure* objcWrapperObjectStructure() const { return m_objcWrapperObjectStructure.get(this); }
688 #endif
689 #ifdef JSC_GLIB_API_ENABLED
690     Structure* glibCallbackFunctionStructure() const { return m_glibCallbackFunctionStructure.get(this); }
691     Structure* glibWrapperObjectStructure() const { return m_glibWrapperObjectStructure.get(this); }
692 #endif
693     Structure* dateStructure() const { return m_dateStructure.get(this); }
694     Structure* symbolObjectStructure() const { return m_symbolObjectStructure.get(this); }
695     Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
696     Structure* errorStructure() const { return m_errorStructure.get(); }
697     Structure* errorStructure(ErrorType errorType) const
698     {
699         switch (errorType) {
700         case ErrorType::Error:
701             return errorStructure();
702         case ErrorType::EvalError:
703             return m_evalErrorStructure.get(this);
704         case ErrorType::RangeError:
705             return m_rangeErrorStructure.get(this);
706         case ErrorType::ReferenceError:
707             return m_referenceErrorStructure.get(this);
708         case ErrorType::SyntaxError:
709             return m_syntaxErrorStructure.get(this);
710         case ErrorType::TypeError:
711             return m_typeErrorStructure.get(this);
712         case ErrorType::URIError:
713             return m_URIErrorStructure.get(this);
714         }
715         ASSERT_NOT_REACHED();
716         return nullptr;
717     }
718     Structure* calleeStructure() const { return m_calleeStructure.get(); }
719     Structure* hostFunctionStructure() const { return m_hostFunctionStructure.get(); }
720
721     Structure* arrowFunctionStructure(bool isBuiltin) const
722     {
723         if (isBuiltin)
724             return m_builtinFunctions.arrowFunctionStructure.get();
725         return m_ordinaryFunctions.arrowFunctionStructure.get();
726     }
727     Structure* sloppyFunctionStructure(bool isBuiltin) const
728     {
729         if (isBuiltin)
730             return m_builtinFunctions.sloppyFunctionStructure.get();
731         return m_ordinaryFunctions.sloppyFunctionStructure.get();
732     }
733     Structure* strictFunctionStructure(bool isBuiltin) const
734     {
735         if (isBuiltin)
736             return m_builtinFunctions.strictFunctionStructure.get();
737         return m_ordinaryFunctions.strictFunctionStructure.get();
738     }
739
740     Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(this); }
741     Structure* customGetterSetterFunctionStructure() const { return m_customGetterSetterFunctionStructure.get(this); }
742     Structure* getterSetterStructure() const { return m_getterSetterStructure.get(); }
743     Structure* nativeStdFunctionStructure() const { return m_nativeStdFunctionStructure.get(this); }
744     PropertyOffset functionNameOffset() const { return m_functionNameOffset; }
745     Structure* numberObjectStructure() const { return m_numberObjectStructure.get(this); }
746     Structure* mapStructure() const { return m_mapStructure.get(); }
747     Structure* regExpStructure() const { return m_regExpStructure.get(); }
748     Structure* generatorFunctionStructure() const { return m_generatorFunctionStructure.get(); }
749     Structure* asyncFunctionStructure() const { return m_asyncFunctionStructure.get(); }
750     Structure* asyncGeneratorFunctionStructure() const { return m_asyncGeneratorFunctionStructure.get(); }
751     Structure* stringObjectStructure() const { return m_stringObjectStructure.get(); }
752     Structure* bigIntObjectStructure() const { return m_bigIntObjectStructure.get(); }
753     Structure* iteratorResultObjectStructure() const { return m_iteratorResultObjectStructure.get(); }
754     Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
755     Structure* regExpMatchesArrayWithGroupsStructure() const { return m_regExpMatchesArrayWithGroupsStructure.get(); }
756     Structure* moduleRecordStructure() const { return m_moduleRecordStructure.get(); }
757     Structure* moduleNamespaceObjectStructure() const { return m_moduleNamespaceObjectStructure.get(); }
758     Structure* proxyObjectStructure() const { return m_proxyObjectStructure.get(); }
759     Structure* callableProxyObjectStructure() const { return m_callableProxyObjectStructure.get(); }
760     Structure* proxyRevokeStructure() const { return m_proxyRevokeStructure.get(); }
761     Structure* restParameterStructure() const { return arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous); }
762     Structure* originalRestParameterStructure() const { return originalArrayStructureForIndexingType(ArrayWithContiguous); }
763 #if ENABLE(WEBASSEMBLY)
764     Structure* webAssemblyModuleRecordStructure() const { return m_webAssemblyModuleRecordStructure.get(); }
765     Structure* webAssemblyFunctionStructure() const { return m_webAssemblyFunctionStructure.get(); }
766     Structure* webAssemblyWrapperFunctionStructure() const { return m_webAssemblyWrapperFunctionStructure.get(); }
767     Structure* webAssemblyToJSCalleeStructure() const { return m_webAssemblyToJSCalleeStructure.get(); }
768 #endif // ENABLE(WEBASSEMBLY)
769
770     JS_EXPORT_PRIVATE void setRemoteDebuggingEnabled(bool);
771     JS_EXPORT_PRIVATE bool remoteDebuggingEnabled() const;
772
773     RegExpGlobalData& regExpGlobalData() { return m_regExpGlobalData; }
774     static ptrdiff_t regExpGlobalDataOffset() { return OBJECT_OFFSETOF(JSGlobalObject, m_regExpGlobalData); }
775
776 #if ENABLE(REMOTE_INSPECTOR)
777     Inspector::JSGlobalObjectInspectorController& inspectorController() const { return *m_inspectorController.get(); }
778     JSGlobalObjectDebuggable& inspectorDebuggable() { return *m_inspectorDebuggable.get(); }
779 #endif
780
781 #if ENABLE(INTL)
782     const HashSet<String>& intlCollatorAvailableLocales();
783     const HashSet<String>& intlDateTimeFormatAvailableLocales();
784     const HashSet<String>& intlNumberFormatAvailableLocales();
785     const HashSet<String>& intlPluralRulesAvailableLocales();
786 #endif // ENABLE(INTL)
787
788     void bumpGlobalLexicalBindingEpoch(VM&);
789     unsigned globalLexicalBindingEpoch() const { return m_globalLexicalBindingEpoch; }
790     static ptrdiff_t globalLexicalBindingEpochOffset() { return OBJECT_OFFSETOF(JSGlobalObject, m_globalLexicalBindingEpoch); }
791     unsigned* addressOfGlobalLexicalBindingEpoch() { return &m_globalLexicalBindingEpoch; }
792
793     void setConsoleClient(ConsoleClient* consoleClient) { m_consoleClient = consoleClient; }
794     ConsoleClient* consoleClient() const { return m_consoleClient; }
795
796     void setName(const String&);
797     const String& name() const { return m_name; }
798
799     JSArrayBufferPrototype* arrayBufferPrototype(ArrayBufferSharingMode sharingMode) const
800     {
801         switch (sharingMode) {
802         case ArrayBufferSharingMode::Default:
803             return m_arrayBufferPrototype.get();
804 #if ENABLE(SHARED_ARRAY_BUFFER)
805         case ArrayBufferSharingMode::Shared:
806             return m_sharedArrayBufferPrototype.get();
807 #else
808         default:
809             return m_arrayBufferPrototype.get();
810 #endif
811         }
812     }
813     Structure* arrayBufferStructure(ArrayBufferSharingMode sharingMode) const
814     {
815         switch (sharingMode) {
816         case ArrayBufferSharingMode::Default:
817             return m_arrayBufferStructure.get();
818 #if ENABLE(SHARED_ARRAY_BUFFER)
819         case ArrayBufferSharingMode::Shared:
820             return m_sharedArrayBufferStructure.get();
821 #else
822         default:
823             return m_arrayBufferStructure.get();
824 #endif
825         }
826         RELEASE_ASSERT_NOT_REACHED();
827         return nullptr;
828     }
829
830 #define DEFINE_ACCESSORS_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName, prototypeBase) \
831     Structure* properName ## Structure() { return m_ ## properName ## Structure.get(); }
832
833     FOR_EACH_SIMPLE_BUILTIN_TYPE(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
834     FOR_BIG_INT_BUILTIN_TYPE_WITH_CONSTRUCTOR(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
835     FOR_EACH_WEBASSEMBLY_CONSTRUCTOR_TYPE(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
836     FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
837
838 #undef DEFINE_ACCESSORS_FOR_SIMPLE_TYPE
839
840 #define DEFINE_ACCESSORS_FOR_LAZY_TYPE(capitalName, lowerName, properName, instanceType, jsName, prototypeBase) \
841     Structure* properName ## Structure() { return m_ ## properName ## Structure.get(this); }
842
843     FOR_EACH_LAZY_BUILTIN_TYPE(DEFINE_ACCESSORS_FOR_LAZY_TYPE)
844
845 #undef DEFINE_ACCESSORS_FOR_LAZY_TYPE
846
847     LazyClassStructure& lazyTypedArrayStructure(TypedArrayType type)
848     {
849         switch (type) {
850         case NotTypedArray:
851             RELEASE_ASSERT_NOT_REACHED();
852             return m_typedArrayInt8;
853 #define TYPED_ARRAY_TYPE_CASE(name) case Type ## name: return m_typedArray ## name;
854             FOR_EACH_TYPED_ARRAY_TYPE(TYPED_ARRAY_TYPE_CASE)
855 #undef TYPED_ARRAY_TYPE_CASE
856         }
857         RELEASE_ASSERT_NOT_REACHED();
858         return m_typedArrayInt8;
859     }
860     const LazyClassStructure& lazyTypedArrayStructure(TypedArrayType type) const
861     {
862         return const_cast<const LazyClassStructure&>(const_cast<JSGlobalObject*>(this)->lazyTypedArrayStructure(type));
863     }
864     
865     Structure* typedArrayStructure(TypedArrayType type) const
866     {
867         return lazyTypedArrayStructure(type).get(this);
868     }
869     Structure* typedArrayStructureConcurrently(TypedArrayType type) const
870     {
871         return lazyTypedArrayStructure(type).getConcurrently();
872     }
873     bool isOriginalTypedArrayStructure(Structure* structure)
874     {
875         TypedArrayType type = structure->classInfo()->typedArrayStorageType;
876         if (type == NotTypedArray)
877             return false;
878         return typedArrayStructureConcurrently(type) == structure;
879     }
880
881     JSObject* typedArrayConstructor(TypedArrayType type) const
882     {
883         return lazyTypedArrayStructure(type).constructor(this);
884     }
885
886     JSCell* actualPointerFor(Special::Pointer pointer)
887     {
888         ASSERT(pointer < Special::TableSize);
889         return m_specialPointers[pointer];
890     }
891     JSCell* jsCellForLinkTimeConstant(LinkTimeConstant type)
892     {
893         unsigned index = static_cast<unsigned>(type);
894         ASSERT(index < LinkTimeConstantCount);
895         return m_linkTimeConstants[index];
896     }
897
898     WatchpointSet* masqueradesAsUndefinedWatchpoint() { return m_masqueradesAsUndefinedWatchpoint.get(); }
899     WatchpointSet* havingABadTimeWatchpoint() { return m_havingABadTimeWatchpoint.get(); }
900     WatchpointSet* varInjectionWatchpoint() { return m_varInjectionWatchpoint.get(); }
901         
902     bool isHavingABadTime() const
903     {
904         return m_havingABadTimeWatchpoint->hasBeenInvalidated();
905     }
906         
907     void haveABadTime(VM&);
908         
909     bool objectPrototypeIsSane();
910     bool arrayPrototypeChainIsSane();
911     bool stringPrototypeChainIsSane();
912
913     void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
914     unsigned profileGroup() const
915     { 
916         if (!m_rareData)
917             return 0;
918         return m_rareData->profileGroup;
919     }
920
921     Debugger* debugger() const { return m_debugger; }
922     void setDebugger(Debugger*);
923
924     const GlobalObjectMethodTable* globalObjectMethodTable() const { return m_globalObjectMethodTable; }
925
926     static bool supportsRichSourceInfo(const JSGlobalObject*) { return true; }
927
928     JS_EXPORT_PRIVATE ExecState* globalExec();
929
930     static bool shouldInterruptScript(const JSGlobalObject*) { return true; }
931     static bool shouldInterruptScriptBeforeTimeout(const JSGlobalObject*) { return false; }
932     static RuntimeFlags javaScriptRuntimeFlags(const JSGlobalObject*) { return RuntimeFlags(); }
933
934     JS_EXPORT_PRIVATE void queueMicrotask(Ref<Microtask>&&);
935
936     bool evalEnabled() const { return m_evalEnabled; }
937     bool webAssemblyEnabled() const { return m_webAssemblyEnabled; }
938     const String& evalDisabledErrorMessage() const { return m_evalDisabledErrorMessage; }
939     const String& webAssemblyDisabledErrorMessage() const { return m_webAssemblyDisabledErrorMessage; }
940     void setEvalEnabled(bool enabled, const String& errorMessage = String())
941     {
942         m_evalEnabled = enabled;
943         m_evalDisabledErrorMessage = errorMessage;
944     }
945     void setWebAssemblyEnabled(bool enabled, const String& errorMessage = String())
946     {
947         m_webAssemblyEnabled = enabled;
948         m_webAssemblyDisabledErrorMessage = errorMessage;
949     }
950
951 #if !ASSERT_DISABLED
952     const ExecState* callFrameAtDebuggerEntry() const { return m_callFrameAtDebuggerEntry; }
953     void setCallFrameAtDebuggerEntry(const ExecState* callFrame) { m_callFrameAtDebuggerEntry = callFrame; }
954 #endif
955
956     void resetPrototype(VM&, JSValue prototype);
957
958     VM& vm() const { return m_vm; }
959     JSObject* globalThis() const;
960     WriteBarrier<JSObject>* addressOfGlobalThis() { return &m_globalThis; }
961
962     static Structure* createStructure(VM& vm, JSValue prototype)
963     {
964         Structure* result = Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
965         result->setTransitionWatchpointIsLikelyToBeFired(true);
966         return result;
967     }
968
969     void registerWeakMap(OpaqueJSWeakObjectMap* map)
970     {
971         createRareDataIfNeeded();
972         m_rareData->weakMaps.add(map);
973     }
974
975     void unregisterWeakMap(OpaqueJSWeakObjectMap* map)
976     {
977         if (m_rareData)
978             m_rareData->weakMaps.remove(map);
979     }
980
981     OpaqueJSClassDataMap& opaqueJSClassData()
982     {
983         createRareDataIfNeeded();
984         return m_rareData->opaqueJSClassData;
985     }
986
987     static ptrdiff_t weakRandomOffset() { return OBJECT_OFFSETOF(JSGlobalObject, m_weakRandom); }
988     double weakRandomNumber() { return m_weakRandom.get(); }
989     unsigned weakRandomInteger() { return m_weakRandom.getUint32(); }
990     WeakRandom& weakRandom() { return m_weakRandom; }
991
992     bool needsSiteSpecificQuirks() const { return m_needsSiteSpecificQuirks; }
993     JS_EXPORT_PRIVATE void exposeDollarVM(VM&);
994
995 #if JSC_OBJC_API_ENABLED
996     JSWrapperMap* wrapperMap() const { return m_wrapperMap.get(); }
997     void setWrapperMap(JSWrapperMap* map) { m_wrapperMap = map; }
998 #endif
999 #ifdef JSC_GLIB_API_ENABLED
1000     WrapperMap* wrapperMap() const { return m_wrapperMap.get(); }
1001     void setWrapperMap(std::unique_ptr<WrapperMap>&&);
1002 #endif
1003
1004 protected:
1005     struct GlobalPropertyInfo {
1006         GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
1007             : identifier(i)
1008             , value(v)
1009             , attributes(a)
1010         {
1011         }
1012
1013         const Identifier identifier;
1014         JSValue value;
1015         unsigned attributes;
1016     };
1017     JS_EXPORT_PRIVATE void addStaticGlobals(GlobalPropertyInfo*, int count);
1018
1019     void setNeedsSiteSpecificQuirks(bool needQuirks) { m_needsSiteSpecificQuirks = needQuirks; }
1020
1021 private:
1022     friend class LLIntOffsetsExtractor;
1023
1024     void fireWatchpointAndMakeAllArrayStructuresSlowPut(VM&);
1025     void setGlobalThis(VM&, JSObject* globalThis);
1026
1027     template<ErrorType errorType>
1028     void initializeErrorConstructor(LazyClassStructure::Initializer&);
1029
1030     JS_EXPORT_PRIVATE void init(VM&);
1031
1032     JS_EXPORT_PRIVATE static void clearRareData(JSCell*);
1033
1034     bool m_needsSiteSpecificQuirks { false };
1035 #if JSC_OBJC_API_ENABLED
1036     RetainPtr<JSWrapperMap> m_wrapperMap;
1037 #endif
1038 #ifdef JSC_GLIB_API_ENABLED
1039     std::unique_ptr<WrapperMap> m_wrapperMap;
1040 #endif
1041 };
1042
1043 inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, unsigned initialLength = 0, JSValue newTarget = JSValue())
1044 {
1045     VM& vm = globalObject->vm();
1046     auto scope = DECLARE_THROW_SCOPE(vm);
1047     Structure* structure;
1048     if (initialLength >= MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH)
1049         structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(exec, ArrayWithArrayStorage, newTarget);
1050     else
1051         structure = globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget);
1052     RETURN_IF_EXCEPTION(scope, nullptr);
1053
1054     JSArray* result = JSArray::tryCreate(vm, structure, initialLength);
1055     if (UNLIKELY(!result)) {
1056         throwOutOfMemoryError(exec, scope);
1057         return nullptr;
1058     }
1059     return ArrayAllocationProfile::updateLastAllocationFor(profile, result);
1060 }
1061
1062 inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, unsigned initialLength = 0, JSValue newTarget = JSValue())
1063 {
1064     return constructEmptyArray(exec, profile, exec->lexicalGlobalObject(), initialLength, newTarget);
1065 }
1066  
1067 inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const ArgList& values, JSValue newTarget = JSValue())
1068 {
1069     VM& vm = globalObject->vm();
1070     auto scope = DECLARE_THROW_SCOPE(vm);
1071     Structure* structure = globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget);
1072     RETURN_IF_EXCEPTION(scope, nullptr);
1073     return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, structure, values));
1074 }
1075
1076 inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const ArgList& values, JSValue newTarget = JSValue())
1077 {
1078     return constructArray(exec, profile, exec->lexicalGlobalObject(), values, newTarget);
1079 }
1080
1081 inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
1082 {
1083     VM& vm = globalObject->vm();
1084     auto scope = DECLARE_THROW_SCOPE(vm);
1085     Structure* structure = globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget);
1086     RETURN_IF_EXCEPTION(scope, nullptr);
1087     return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, structure, values, length));
1088 }
1089
1090 inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
1091 {
1092     return constructArray(exec, profile, exec->lexicalGlobalObject(), values, length, newTarget);
1093 }
1094
1095 inline JSArray* constructArrayNegativeIndexed(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
1096 {
1097     VM& vm = globalObject->vm();
1098     auto scope = DECLARE_THROW_SCOPE(vm);
1099     Structure* structure = globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget);
1100     RETURN_IF_EXCEPTION(scope, nullptr);
1101     return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArrayNegativeIndexed(exec, structure, values, length));
1102 }
1103
1104 inline JSArray* constructArrayNegativeIndexed(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
1105 {
1106     return constructArrayNegativeIndexed(exec, profile, exec->lexicalGlobalObject(), values, length, newTarget);
1107 }
1108
1109 inline JSObject* ExecState::globalThisValue() const
1110 {
1111     return lexicalGlobalObject()->globalThis();
1112 }
1113
1114 inline JSObject* JSScope::globalThis()
1115
1116     return globalObject()->globalThis();
1117 }
1118
1119 inline JSObject* JSGlobalObject::globalThis() const
1120
1121     return m_globalThis.get();
1122 }
1123
1124 } // namespace JSC