1/*
2 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3 * Copyright (C) 2007, 2008, 2009, 2014, 2015 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#ifndef JSGlobalObject_h
23#define JSGlobalObject_h
24
25#include "ArrayAllocationProfile.h"
26#include "InternalFunction.h"
27#include "JSArray.h"
28#include "JSArrayBufferPrototype.h"
29#include "JSClassRef.h"
30#include "JSGlobalLexicalEnvironment.h"
31#include "JSProxy.h"
32#include "JSSegmentedVariableObject.h"
33#include "JSWeakObjectMapRefInternal.h"
34#include "NumberPrototype.h"
35#include "RuntimeFlags.h"
36#include "SpecialPointer.h"
37#include "StringPrototype.h"
38#include "SymbolPrototype.h"
39#include "TemplateRegistry.h"
40#include "VM.h"
41#include "Watchpoint.h"
42#include <JavaScriptCore/JSBase.h>
43#include <array>
44#include <wtf/HashSet.h>
45#include <wtf/PassRefPtr.h>
46
47struct OpaqueJSClass;
48struct OpaqueJSClassContextData;
49
50namespace Inspector {
51class JSGlobalObjectInspectorController;
52}
53
54namespace JSC {
55
56class ArrayPrototype;
57class BooleanPrototype;
58class ConsoleClient;
59class Debugger;
60class ErrorConstructor;
61class ErrorPrototype;
62class EvalCodeBlock;
63class EvalExecutable;
64class FunctionCodeBlock;
65class FunctionExecutable;
66class FunctionPrototype;
67class GeneratorPrototype;
68class GeneratorFunctionPrototype;
69class GetterSetter;
70class GlobalCodeBlock;
71class InputCursor;
72class JSGlobalObjectDebuggable;
73class JSInternalPromise;
74class JSPromise;
75class JSPromiseConstructor;
76class JSPromisePrototype;
77class JSStack;
78class LLIntOffsetsExtractor;
79class Microtask;
80class ModuleLoaderObject;
81class ModuleProgramExecutable;
82class NativeErrorConstructor;
83class NullGetterFunction;
84class NullSetterFunction;
85class ObjectConstructor;
86class ProgramCodeBlock;
87class ProgramExecutable;
88class RegExpConstructor;
89class RegExpPrototype;
90class SourceCode;
91class UnlinkedModuleProgramCodeBlock;
92class VariableEnvironment;
93enum class ThisTDZMode;
94struct ActivationStackNode;
95struct HashTable;
96
97#define DEFINE_STANDARD_BUILTIN(macro, upperName, lowerName) macro(upperName, lowerName, lowerName, JS ## upperName, upperName)
98
99#define FOR_EACH_SIMPLE_BUILTIN_TYPE_WITH_CONSTRUCTOR(macro) \
100 macro(Set, set, set, JSSet, Set) \
101 macro(Map, map, map, JSMap, Map) \
102 macro(Date, date, date, DateInstance, Date) \
103 macro(String, string, stringObject, StringObject, String) \
104 macro(Symbol, symbol, symbolObject, SymbolObject, Symbol) \
105 macro(Boolean, boolean, booleanObject, BooleanObject, Boolean) \
106 macro(Number, number, numberObject, NumberObject, Number) \
107 macro(Error, error, error, ErrorInstance, Error) \
108 macro(JSPromise, promise, promise, JSPromise, Promise) \
109 macro(JSArrayBuffer, arrayBuffer, arrayBuffer, JSArrayBuffer, ArrayBuffer) \
110 DEFINE_STANDARD_BUILTIN(macro, WeakMap, weakMap) \
111 DEFINE_STANDARD_BUILTIN(macro, WeakSet, weakSet) \
112
113#define FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(macro) \
114 DEFINE_STANDARD_BUILTIN(macro, ArrayIterator, arrayIterator) \
115 DEFINE_STANDARD_BUILTIN(macro, MapIterator, mapIterator) \
116 DEFINE_STANDARD_BUILTIN(macro, SetIterator, setIterator) \
117 DEFINE_STANDARD_BUILTIN(macro, StringIterator, stringIterator) \
118
119#define FOR_EACH_BUILTIN_ITERATOR_TYPE(macro) \
120 DEFINE_STANDARD_BUILTIN(macro, Iterator, iterator) \
121 FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(macro) \
122
123#define FOR_EACH_SIMPLE_BUILTIN_TYPE(macro) \
124 FOR_EACH_SIMPLE_BUILTIN_TYPE_WITH_CONSTRUCTOR(macro) \
125 macro(JSInternalPromise, internalPromise, internalPromise, JSInternalPromise, InternalPromise) \
126
127#define DECLARE_SIMPLE_BUILTIN_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
128 class JS ## capitalName; \
129 class capitalName ## Prototype; \
130 class capitalName ## Constructor;
131
132class IteratorPrototype;
133FOR_EACH_SIMPLE_BUILTIN_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
134FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DECLARE_SIMPLE_BUILTIN_TYPE)
135
136#undef DECLARE_SIMPLE_BUILTIN_TYPE
137
138typedef Vector<ExecState*, 16> ExecStateStack;
139
140struct GlobalObjectMethodTable {
141 typedef bool (*AllowsAccessFromFunctionPtr)(const JSGlobalObject*, ExecState*);
142 AllowsAccessFromFunctionPtr allowsAccessFrom;
143
144 typedef bool (*SupportsLegacyProfilingFunctionPtr)(const JSGlobalObject*);
145 SupportsLegacyProfilingFunctionPtr supportsLegacyProfiling;
146
147 typedef bool (*SupportsRichSourceInfoFunctionPtr)(const JSGlobalObject*);
148 SupportsRichSourceInfoFunctionPtr supportsRichSourceInfo;
149
150 typedef bool (*ShouldInterruptScriptFunctionPtr)(const JSGlobalObject*);
151 ShouldInterruptScriptFunctionPtr shouldInterruptScript;
152
153 typedef RuntimeFlags (*JavaScriptRuntimeFlagsFunctionPtr)(const JSGlobalObject*);
154 JavaScriptRuntimeFlagsFunctionPtr javaScriptRuntimeFlags;
155
156 typedef void (*QueueTaskToEventLoopFunctionPtr)(const JSGlobalObject*, PassRefPtr<Microtask>);
157 QueueTaskToEventLoopFunctionPtr queueTaskToEventLoop;
158
159 typedef bool (*ShouldInterruptScriptBeforeTimeoutPtr)(const JSGlobalObject*);
160 ShouldInterruptScriptBeforeTimeoutPtr shouldInterruptScriptBeforeTimeout;
161
162 typedef JSInternalPromise* (*ModuleLoaderResolvePtr)(JSGlobalObject*, ExecState*, JSValue, JSValue);
163 ModuleLoaderResolvePtr moduleLoaderResolve;
164
165 typedef JSInternalPromise* (*ModuleLoaderFetchPtr)(JSGlobalObject*, ExecState*, JSValue);
166 ModuleLoaderFetchPtr moduleLoaderFetch;
167
168 typedef JSInternalPromise* (*ModuleLoaderTranslatePtr)(JSGlobalObject*, ExecState*, JSValue, JSValue);
169 ModuleLoaderTranslatePtr moduleLoaderTranslate;
170
171 typedef JSInternalPromise* (*ModuleLoaderInstantiatePtr)(JSGlobalObject*, ExecState*, JSValue, JSValue);
172 ModuleLoaderInstantiatePtr moduleLoaderInstantiate;
173
174 typedef JSValue (*ModuleLoaderEvaluatePtr)(JSGlobalObject*, ExecState*, JSValue, JSValue);
175 ModuleLoaderEvaluatePtr moduleLoaderEvaluate;
176};
177
178class JSGlobalObject : public JSSegmentedVariableObject {
179private:
180 typedef HashSet<RefPtr<OpaqueJSWeakObjectMap>> WeakMapSet;
181 typedef HashMap<OpaqueJSClass*, std::unique_ptr<OpaqueJSClassContextData>> OpaqueJSClassDataMap;
182
183 struct JSGlobalObjectRareData {
184 WTF_MAKE_FAST_ALLOCATED;
185 public:
186 JSGlobalObjectRareData()
187 : profileGroup(0)
188 {
189 }
190
191 WeakMapSet weakMaps;
192 unsigned profileGroup;
193
194 OpaqueJSClassDataMap opaqueJSClassData;
195 };
196
197protected:
198 Register m_globalCallFrame[JSStack::CallFrameHeaderSize];
199
200 WriteBarrier<JSObject> m_globalThis;
201
202 WriteBarrier<JSGlobalLexicalEnvironment> m_globalLexicalEnvironment;
203 WriteBarrier<JSObject> m_globalCallee;
204 WriteBarrier<RegExpConstructor> m_regExpConstructor;
205 WriteBarrier<ErrorConstructor> m_errorConstructor;
206 WriteBarrier<NativeErrorConstructor> m_evalErrorConstructor;
207 WriteBarrier<NativeErrorConstructor> m_rangeErrorConstructor;
208 WriteBarrier<NativeErrorConstructor> m_referenceErrorConstructor;
209 WriteBarrier<NativeErrorConstructor> m_syntaxErrorConstructor;
210 WriteBarrier<NativeErrorConstructor> m_typeErrorConstructor;
211 WriteBarrier<NativeErrorConstructor> m_URIErrorConstructor;
212 WriteBarrier<ObjectConstructor> m_objectConstructor;
213 WriteBarrier<JSPromiseConstructor> m_promiseConstructor;
214 WriteBarrier<JSInternalPromiseConstructor> m_internalPromiseConstructor;
215
216 WriteBarrier<NullGetterFunction> m_nullGetterFunction;
217 WriteBarrier<NullSetterFunction> m_nullSetterFunction;
218
219 WriteBarrier<JSFunction> m_parseIntFunction;
220
221 WriteBarrier<JSFunction> m_evalFunction;
222 WriteBarrier<JSFunction> m_callFunction;
223 WriteBarrier<JSFunction> m_applyFunction;
224 WriteBarrier<JSFunction> m_definePropertyFunction;
225 WriteBarrier<JSFunction> m_arrayProtoValuesFunction;
226 WriteBarrier<JSFunction> m_initializePromiseFunction;
227 WriteBarrier<JSFunction> m_newPromiseCapabilityFunction;
228 WriteBarrier<JSFunction> m_functionProtoHasInstanceSymbolFunction;
229 WriteBarrier<GetterSetter> m_throwTypeErrorGetterSetter;
230
231 WriteBarrier<ModuleLoaderObject> m_moduleLoader;
232
233 WriteBarrier<ObjectPrototype> m_objectPrototype;
234 WriteBarrier<FunctionPrototype> m_functionPrototype;
235 WriteBarrier<ArrayPrototype> m_arrayPrototype;
236 WriteBarrier<RegExpPrototype> m_regExpPrototype;
237 WriteBarrier<IteratorPrototype> m_iteratorPrototype;
238 WriteBarrier<GeneratorFunctionPrototype> m_generatorFunctionPrototype;
239 WriteBarrier<GeneratorPrototype> m_generatorPrototype;
240
241 WriteBarrier<Structure> m_debuggerScopeStructure;
242 WriteBarrier<Structure> m_withScopeStructure;
243 WriteBarrier<Structure> m_strictEvalActivationStructure;
244 WriteBarrier<Structure> m_lexicalEnvironmentStructure;
245 WriteBarrier<Structure> m_moduleEnvironmentStructure;
246 WriteBarrier<Structure> m_directArgumentsStructure;
247 WriteBarrier<Structure> m_scopedArgumentsStructure;
248 WriteBarrier<Structure> m_outOfBandArgumentsStructure;
249
250 // Lists the actual structures used for having these particular indexing shapes.
251 WriteBarrier<Structure> m_originalArrayStructureForIndexingShape[NumberOfIndexingShapes];
252 // Lists the structures we should use during allocation for these particular indexing shapes.
253 // These structures will differ from the originals list above when we are having a bad time.
254 WriteBarrier<Structure> m_arrayStructureForIndexingShapeDuringAllocation[NumberOfIndexingShapes];
255
256 WriteBarrier<Structure> m_callbackConstructorStructure;
257 WriteBarrier<Structure> m_callbackFunctionStructure;
258 WriteBarrier<Structure> m_callbackObjectStructure;
259 WriteBarrier<Structure> m_propertyNameIteratorStructure;
260#if JSC_OBJC_API_ENABLED
261 WriteBarrier<Structure> m_objcCallbackFunctionStructure;
262 WriteBarrier<Structure> m_objcWrapperObjectStructure;
263#endif
264 WriteBarrier<Structure> m_nullPrototypeObjectStructure;
265 WriteBarrier<Structure> m_calleeStructure;
266 WriteBarrier<Structure> m_functionStructure;
267 WriteBarrier<Structure> m_boundFunctionStructure;
268 WriteBarrier<Structure> m_boundSlotBaseFunctionStructure;
269 WriteBarrier<Structure> m_nativeStdFunctionStructure;
270 WriteBarrier<Structure> m_namedFunctionStructure;
271 PropertyOffset m_functionNameOffset;
272 WriteBarrier<Structure> m_privateNameStructure;
273 WriteBarrier<Structure> m_regExpStructure;
274 WriteBarrier<Structure> m_generatorFunctionStructure;
275 WriteBarrier<Structure> m_consoleStructure;
276 WriteBarrier<Structure> m_dollarVMStructure;
277 WriteBarrier<Structure> m_internalFunctionStructure;
278 WriteBarrier<Structure> m_iteratorResultObjectStructure;
279 WriteBarrier<Structure> m_regExpMatchesArrayStructure;
280 WriteBarrier<Structure> m_moduleRecordStructure;
281 WriteBarrier<Structure> m_moduleNamespaceObjectStructure;
282 WriteBarrier<Structure> m_proxyObjectStructure;
283#if ENABLE(WEBASSEMBLY)
284 WriteBarrier<Structure> m_wasmModuleStructure;
285#endif
286
287#define DEFINE_STORAGE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
288 WriteBarrier<capitalName ## Prototype> m_ ## lowerName ## Prototype; \
289 WriteBarrier<Structure> m_ ## properName ## Structure;
290
291 FOR_EACH_SIMPLE_BUILTIN_TYPE(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
292 FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DEFINE_STORAGE_FOR_SIMPLE_TYPE)
293
294#undef DEFINE_STORAGE_FOR_SIMPLE_TYPE
295
296 struct TypedArrayData {
297 WriteBarrier<JSObject> prototype;
298 WriteBarrier<Structure> structure;
299 };
300
301 std::array<TypedArrayData, NUMBER_OF_TYPED_ARRAY_TYPES> m_typedArrays;
302
303 JSCell* m_specialPointers[Special::TableSize]; // Special pointers used by the LLInt and JIT.
304 JSCell* m_linkTimeConstants[LinkTimeConstantCount];
305
306 String m_name;
307
308 Debugger* m_debugger;
309
310 VM& m_vm;
311
312#if ENABLE(WEB_REPLAY)
313 RefPtr<InputCursor> m_inputCursor;
314#endif
315
316#if ENABLE(REMOTE_INSPECTOR)
317 std::unique_ptr<Inspector::JSGlobalObjectInspectorController> m_inspectorController;
318 std::unique_ptr<JSGlobalObjectDebuggable> m_inspectorDebuggable;
319#endif
320
321#if ENABLE(INTL)
322 HashSet<String> m_intlCollatorAvailableLocales;
323 HashSet<String> m_intlDateTimeFormatAvailableLocales;
324 HashSet<String> m_intlNumberFormatAvailableLocales;
325#endif // ENABLE(INTL)
326
327 RefPtr<WatchpointSet> m_masqueradesAsUndefinedWatchpoint;
328 RefPtr<WatchpointSet> m_havingABadTimeWatchpoint;
329 RefPtr<WatchpointSet> m_varInjectionWatchpoint;
330
331 std::unique_ptr<JSGlobalObjectRareData> m_rareData;
332
333 WeakRandom m_weakRandom;
334
335 TemplateRegistry m_templateRegistry;
336
337 bool m_evalEnabled;
338 String m_evalDisabledErrorMessage;
339 RuntimeFlags m_runtimeFlags;
340 ConsoleClient* m_consoleClient;
341
342 static JS_EXPORTDATA const GlobalObjectMethodTable s_globalObjectMethodTable;
343 const GlobalObjectMethodTable* m_globalObjectMethodTable;
344
345 void createRareDataIfNeeded()
346 {
347 if (m_rareData)
348 return;
349 m_rareData = std::make_unique<JSGlobalObjectRareData>();
350 }
351
352public:
353 typedef JSSegmentedVariableObject Base;
354 static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames;
355
356 static JSGlobalObject* create(VM& vm, Structure* structure)
357 {
358 JSGlobalObject* globalObject = new (NotNull, allocateCell<JSGlobalObject>(vm.heap)) JSGlobalObject(vm, structure);
359 globalObject->finishCreation(vm);
360 vm.heap.addFinalizer(globalObject, destroy);
361 return globalObject;
362 }
363
364 DECLARE_EXPORT_INFO;
365
366 bool hasDebugger() const { return m_debugger; }
367 bool hasLegacyProfiler() const;
368 const RuntimeFlags& runtimeFlags() const { return m_runtimeFlags; }
369
370protected:
371 JS_EXPORT_PRIVATE explicit JSGlobalObject(VM&, Structure*, const GlobalObjectMethodTable* = 0);
372
373 void finishCreation(VM& vm)
374 {
375 Base::finishCreation(vm);
376 structure()->setGlobalObject(vm, this);
377 m_runtimeFlags = m_globalObjectMethodTable->javaScriptRuntimeFlags(this);
378 init(vm);
379 setGlobalThis(vm, JSProxy::create(vm, JSProxy::createStructure(vm, this, prototype(), PureForwardingProxyType), this));
380 }
381
382 void finishCreation(VM& vm, JSObject* thisValue)
383 {
384 Base::finishCreation(vm);
385 structure()->setGlobalObject(vm, this);
386 m_runtimeFlags = m_globalObjectMethodTable->javaScriptRuntimeFlags(this);
387 init(vm);
388 setGlobalThis(vm, thisValue);
389 }
390
391 void addGlobalVar(const Identifier&);
392
393public:
394 JS_EXPORT_PRIVATE ~JSGlobalObject();
395 JS_EXPORT_PRIVATE static void destroy(JSCell*);
396 // We don't need a destructor because we use a finalizer instead.
397 static const bool needsDestruction = false;
398
399 JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
400
401 JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
402 JS_EXPORT_PRIVATE static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
403
404 JS_EXPORT_PRIVATE static void defineGetter(JSObject*, ExecState*, PropertyName, JSObject* getterFunc, unsigned attributes);
405 JS_EXPORT_PRIVATE static void defineSetter(JSObject*, ExecState*, PropertyName, JSObject* setterFunc, unsigned attributes);
406 JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
407
408 void addVar(ExecState* exec, const Identifier& propertyName)
409 {
410 if (!hasProperty(exec, propertyName))
411 addGlobalVar(propertyName);
412 }
413 void addFunction(ExecState*, const Identifier&);
414
415 JSScope* globalScope() { return m_globalLexicalEnvironment.get(); }
416 JSGlobalLexicalEnvironment* globalLexicalEnvironment() { return m_globalLexicalEnvironment.get(); }
417
418 // The following accessors return pristine values, even if a script
419 // replaces the global object's associated property.
420
421 RegExpConstructor* regExpConstructor() const { return m_regExpConstructor.get(); }
422
423 ErrorConstructor* errorConstructor() const { return m_errorConstructor.get(); }
424 ObjectConstructor* objectConstructor() const { return m_objectConstructor.get(); }
425 JSPromiseConstructor* promiseConstructor() const { return m_promiseConstructor.get(); }
426 JSInternalPromiseConstructor* internalPromiseConstructor() const { return m_internalPromiseConstructor.get(); }
427 NativeErrorConstructor* evalErrorConstructor() const { return m_evalErrorConstructor.get(); }
428 NativeErrorConstructor* rangeErrorConstructor() const { return m_rangeErrorConstructor.get(); }
429 NativeErrorConstructor* referenceErrorConstructor() const { return m_referenceErrorConstructor.get(); }
430 NativeErrorConstructor* syntaxErrorConstructor() const { return m_syntaxErrorConstructor.get(); }
431 NativeErrorConstructor* typeErrorConstructor() const { return m_typeErrorConstructor.get(); }
432 NativeErrorConstructor* URIErrorConstructor() const { return m_URIErrorConstructor.get(); }
433
434 NullGetterFunction* nullGetterFunction() const { return m_nullGetterFunction.get(); }
435 NullSetterFunction* nullSetterFunction() const { return m_nullSetterFunction.get(); }
436
437 JSFunction* parseIntFunction() const { return m_parseIntFunction.get(); }
438
439 JSFunction* evalFunction() const { return m_evalFunction.get(); }
440 JSFunction* callFunction() const { return m_callFunction.get(); }
441 JSFunction* applyFunction() const { return m_applyFunction.get(); }
442 JSFunction* definePropertyFunction() const { return m_definePropertyFunction.get(); }
443 JSFunction* arrayProtoValuesFunction() const { return m_arrayProtoValuesFunction.get(); }
444 JSFunction* initializePromiseFunction() const { return m_initializePromiseFunction.get(); }
445 JSFunction* newPromiseCapabilityFunction() const { return m_newPromiseCapabilityFunction.get(); }
446 JSFunction* functionProtoHasInstanceSymbolFunction() const { return m_functionProtoHasInstanceSymbolFunction.get(); }
447 GetterSetter* throwTypeErrorGetterSetter(VM& vm)
448 {
449 if (!m_throwTypeErrorGetterSetter)
450 createThrowTypeError(vm);
451 return m_throwTypeErrorGetterSetter.get();
452 }
453
454 ModuleLoaderObject* moduleLoader() const { return m_moduleLoader.get(); }
455
456 ObjectPrototype* objectPrototype() const { return m_objectPrototype.get(); }
457 FunctionPrototype* functionPrototype() const { return m_functionPrototype.get(); }
458 ArrayPrototype* arrayPrototype() const { return m_arrayPrototype.get(); }
459 BooleanPrototype* booleanPrototype() const { return m_booleanPrototype.get(); }
460 StringPrototype* stringPrototype() const { return m_stringPrototype.get(); }
461 SymbolPrototype* symbolPrototype() const { return m_symbolPrototype.get(); }
462 NumberPrototype* numberPrototype() const { return m_numberPrototype.get(); }
463 DatePrototype* datePrototype() const { return m_datePrototype.get(); }
464 RegExpPrototype* regExpPrototype() const { return m_regExpPrototype.get(); }
465 ErrorPrototype* errorPrototype() const { return m_errorPrototype.get(); }
466 IteratorPrototype* iteratorPrototype() const { return m_iteratorPrototype.get(); }
467 GeneratorFunctionPrototype* generatorFunctionPrototype() const { return m_generatorFunctionPrototype.get(); }
468 GeneratorPrototype* generatorPrototype() const { return m_generatorPrototype.get(); }
469
470 Structure* debuggerScopeStructure() const { return m_debuggerScopeStructure.get(); }
471 Structure* withScopeStructure() const { return m_withScopeStructure.get(); }
472 Structure* strictEvalActivationStructure() const { return m_strictEvalActivationStructure.get(); }
473 Structure* activationStructure() const { return m_lexicalEnvironmentStructure.get(); }
474 Structure* moduleEnvironmentStructure() const { return m_moduleEnvironmentStructure.get(); }
475 Structure* directArgumentsStructure() const { return m_directArgumentsStructure.get(); }
476 Structure* scopedArgumentsStructure() const { return m_scopedArgumentsStructure.get(); }
477 Structure* outOfBandArgumentsStructure() const { return m_outOfBandArgumentsStructure.get(); }
478 Structure* originalArrayStructureForIndexingType(IndexingType indexingType) const
479 {
480 ASSERT(indexingType & IsArray);
481 return m_originalArrayStructureForIndexingShape[(indexingType & IndexingShapeMask) >> IndexingShapeShift].get();
482 }
483 Structure* arrayStructureForIndexingTypeDuringAllocation(IndexingType indexingType) const
484 {
485 ASSERT(indexingType & IsArray);
486 return m_arrayStructureForIndexingShapeDuringAllocation[(indexingType & IndexingShapeMask) >> IndexingShapeShift].get();
487 }
488 Structure* arrayStructureForIndexingTypeDuringAllocation(ExecState* exec, IndexingType indexingType, JSValue newTarget) const
489 {
490 return InternalFunction::createSubclassStructure(exec, newTarget, arrayStructureForIndexingTypeDuringAllocation(indexingType));
491 }
492 Structure* arrayStructureForProfileDuringAllocation(ExecState* exec, ArrayAllocationProfile* profile, JSValue newTarget) const
493 {
494 return arrayStructureForIndexingTypeDuringAllocation(exec, ArrayAllocationProfile::selectIndexingTypeFor(profile), newTarget);
495 }
496
497 bool isOriginalArrayStructure(Structure* structure)
498 {
499 return originalArrayStructureForIndexingType(structure->indexingType() | IsArray) == structure;
500 }
501
502 Structure* booleanObjectStructure() const { return m_booleanObjectStructure.get(); }
503 Structure* callbackConstructorStructure() const { return m_callbackConstructorStructure.get(); }
504 Structure* callbackFunctionStructure() const { return m_callbackFunctionStructure.get(); }
505 Structure* callbackObjectStructure() const { return m_callbackObjectStructure.get(); }
506 Structure* propertyNameIteratorStructure() const { return m_propertyNameIteratorStructure.get(); }
507#if JSC_OBJC_API_ENABLED
508 Structure* objcCallbackFunctionStructure() const { return m_objcCallbackFunctionStructure.get(); }
509 Structure* objcWrapperObjectStructure() const { return m_objcWrapperObjectStructure.get(); }
510#endif
511 Structure* dateStructure() const { return m_dateStructure.get(); }
512 Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
513 Structure* errorStructure() const { return m_errorStructure.get(); }
514 Structure* calleeStructure() const { return m_calleeStructure.get(); }
515 Structure* functionStructure() const { return m_functionStructure.get(); }
516 Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(); }
517 Structure* boundSlotBaseFunctionStructure() const { return m_boundSlotBaseFunctionStructure.get(); }
518 Structure* nativeStdFunctionStructure() const { return m_nativeStdFunctionStructure.get(); }
519 Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
520 PropertyOffset functionNameOffset() const { return m_functionNameOffset; }
521 Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
522 Structure* privateNameStructure() const { return m_privateNameStructure.get(); }
523 Structure* internalFunctionStructure() const { return m_internalFunctionStructure.get(); }
524 Structure* mapStructure() const { return m_mapStructure.get(); }
525 Structure* regExpStructure() const { return m_regExpStructure.get(); }
526 Structure* generatorFunctionStructure() const { return m_generatorFunctionStructure.get(); }
527 Structure* setStructure() const { return m_setStructure.get(); }
528 Structure* stringObjectStructure() const { return m_stringObjectStructure.get(); }
529 Structure* symbolObjectStructure() const { return m_symbolObjectStructure.get(); }
530 Structure* iteratorResultObjectStructure() const { return m_iteratorResultObjectStructure.get(); }
531 Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
532 Structure* moduleRecordStructure() const { return m_moduleRecordStructure.get(); }
533 Structure* moduleNamespaceObjectStructure() const { return m_moduleNamespaceObjectStructure.get(); }
534 Structure* proxyObjectStructure() const { return m_proxyObjectStructure.get(); }
535#if ENABLE(WEBASSEMBLY)
536 Structure* wasmModuleStructure() const { return m_wasmModuleStructure.get(); }
537#endif
538
539 JS_EXPORT_PRIVATE void setRemoteDebuggingEnabled(bool);
540 JS_EXPORT_PRIVATE bool remoteDebuggingEnabled() const;
541
542#if ENABLE(WEB_REPLAY)
543 JS_EXPORT_PRIVATE void setInputCursor(PassRefPtr<InputCursor>);
544 InputCursor& inputCursor() const { return *m_inputCursor; }
545#endif
546
547#if ENABLE(REMOTE_INSPECTOR)
548 Inspector::JSGlobalObjectInspectorController& inspectorController() const { return *m_inspectorController.get(); }
549 JSGlobalObjectDebuggable& inspectorDebuggable() { return *m_inspectorDebuggable.get(); }
550#endif
551
552#if ENABLE(INTL)
553 const HashSet<String>& intlCollatorAvailableLocales();
554 const HashSet<String>& intlDateTimeFormatAvailableLocales();
555 const HashSet<String>& intlNumberFormatAvailableLocales();
556#endif // ENABLE(INTL)
557
558 void setConsoleClient(ConsoleClient* consoleClient) { m_consoleClient = consoleClient; }
559 ConsoleClient* consoleClient() const { return m_consoleClient; }
560
561 void setName(const String&);
562 const String& name() const { return m_name; }
563
564 JSArrayBufferPrototype* arrayBufferPrototype() const { return m_arrayBufferPrototype.get(); }
565
566#define DEFINE_ACCESSORS_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
567 Structure* properName ## Structure() { return m_ ## properName ## Structure.get(); }
568
569 FOR_EACH_SIMPLE_BUILTIN_TYPE(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
570 FOR_EACH_BUILTIN_DERIVED_ITERATOR_TYPE(DEFINE_ACCESSORS_FOR_SIMPLE_TYPE)
571
572#undef DEFINE_ACCESSORS_FOR_SIMPLE_TYPE
573
574 Structure* typedArrayStructure(TypedArrayType type) const
575 {
576 return m_typedArrays[toIndex(type)].structure.get();
577 }
578 bool isOriginalTypedArrayStructure(Structure* structure)
579 {
580 TypedArrayType type = structure->classInfo()->typedArrayStorageType;
581 if (type == NotTypedArray)
582 return false;
583 return typedArrayStructure(type) == structure;
584 }
585
586 JSCell* actualPointerFor(Special::Pointer pointer)
587 {
588 ASSERT(pointer < Special::TableSize);
589 return m_specialPointers[pointer];
590 }
591 JSCell* jsCellForLinkTimeConstant(LinkTimeConstant type)
592 {
593 unsigned index = static_cast<unsigned>(type);
594 ASSERT(index < LinkTimeConstantCount);
595 return m_linkTimeConstants[index];
596 }
597
598 WatchpointSet* masqueradesAsUndefinedWatchpoint() { return m_masqueradesAsUndefinedWatchpoint.get(); }
599 WatchpointSet* havingABadTimeWatchpoint() { return m_havingABadTimeWatchpoint.get(); }
600 WatchpointSet* varInjectionWatchpoint() { return m_varInjectionWatchpoint.get(); }
601
602 bool isHavingABadTime() const
603 {
604 return m_havingABadTimeWatchpoint->hasBeenInvalidated();
605 }
606
607 void haveABadTime(VM&);
608
609 bool objectPrototypeIsSane();
610 bool arrayPrototypeChainIsSane();
611 bool stringPrototypeChainIsSane();
612
613 void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
614 unsigned profileGroup() const
615 {
616 if (!m_rareData)
617 return 0;
618 return m_rareData->profileGroup;
619 }
620
621 Debugger* debugger() const { return m_debugger; }
622 void setDebugger(Debugger* debugger) { m_debugger = debugger; }
623
624 const GlobalObjectMethodTable* globalObjectMethodTable() const { return m_globalObjectMethodTable; }
625
626 static bool allowsAccessFrom(const JSGlobalObject*, ExecState*) { return true; }
627 static bool supportsLegacyProfiling(const JSGlobalObject*) { return false; }
628 static bool supportsRichSourceInfo(const JSGlobalObject*) { return true; }
629
630 JS_EXPORT_PRIVATE ExecState* globalExec();
631
632 static bool shouldInterruptScript(const JSGlobalObject*) { return true; }
633 static bool shouldInterruptScriptBeforeTimeout(const JSGlobalObject*) { return false; }
634 static RuntimeFlags javaScriptRuntimeFlags(const JSGlobalObject*) { return RuntimeFlags(); }
635
636 void queueMicrotask(PassRefPtr<Microtask>);
637
638 bool evalEnabled() const { return m_evalEnabled; }
639 const String& evalDisabledErrorMessage() const { return m_evalDisabledErrorMessage; }
640 void setEvalEnabled(bool enabled, const String& errorMessage = String())
641 {
642 m_evalEnabled = enabled;
643 m_evalDisabledErrorMessage = errorMessage;
644 }
645
646 void resetPrototype(VM&, JSValue prototype);
647
648 VM& vm() const { return m_vm; }
649 JSObject* globalThis() const;
650
651 static Structure* createStructure(VM& vm, JSValue prototype)
652 {
653 Structure* result = Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
654 result->setTransitionWatchpointIsLikelyToBeFired(true);
655 return result;
656 }
657
658 void registerWeakMap(OpaqueJSWeakObjectMap* map)
659 {
660 createRareDataIfNeeded();
661 m_rareData->weakMaps.add(map);
662 }
663
664 void unregisterWeakMap(OpaqueJSWeakObjectMap* map)
665 {
666 if (m_rareData)
667 m_rareData->weakMaps.remove(map);
668 }
669
670 OpaqueJSClassDataMap& opaqueJSClassData()
671 {
672 createRareDataIfNeeded();
673 return m_rareData->opaqueJSClassData;
674 }
675
676 TemplateRegistry& templateRegistry() { return m_templateRegistry; }
677
678 static ptrdiff_t weakRandomOffset() { return OBJECT_OFFSETOF(JSGlobalObject, m_weakRandom); }
679 double weakRandomNumber() { return m_weakRandom.get(); }
680 unsigned weakRandomInteger() { return m_weakRandom.getUint32(); }
681
682 UnlinkedProgramCodeBlock* createProgramCodeBlock(CallFrame*, ProgramExecutable*, JSObject** exception);
683 UnlinkedEvalCodeBlock* createEvalCodeBlock(CallFrame*, EvalExecutable*, ThisTDZMode, bool isArrowFunctionContext, const VariableEnvironment*);
684 UnlinkedModuleProgramCodeBlock* createModuleProgramCodeBlock(CallFrame*, ModuleProgramExecutable*);
685
686protected:
687 struct GlobalPropertyInfo {
688 GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
689 : identifier(i)
690 , value(v)
691 , attributes(a)
692 {
693 }
694
695 const Identifier identifier;
696 JSValue value;
697 unsigned attributes;
698 };
699 JS_EXPORT_PRIVATE void addStaticGlobals(GlobalPropertyInfo*, int count);
700
701 JS_EXPORT_PRIVATE static JSC::JSValue toThis(JSC::JSCell*, JSC::ExecState*, ECMAMode);
702
703private:
704 friend class LLIntOffsetsExtractor;
705
706 JS_EXPORT_PRIVATE void setGlobalThis(VM&, JSObject* globalThis);
707
708 JS_EXPORT_PRIVATE void init(VM&);
709
710 void createThrowTypeError(VM&);
711
712 JS_EXPORT_PRIVATE static void clearRareData(JSCell*);
713};
714
715JSGlobalObject* asGlobalObject(JSValue);
716
717inline JSGlobalObject* asGlobalObject(JSValue value)
718{
719 ASSERT(asObject(value)->isGlobalObject());
720 return jsCast<JSGlobalObject*>(asObject(value));
721}
722
723inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, unsigned initialLength = 0, JSValue newTarget = JSValue())
724{
725 Structure* structure;
726 if (initialLength >= MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH)
727 structure = globalObject->arrayStructureForIndexingTypeDuringAllocation(exec, ArrayWithArrayStorage, newTarget);
728 else
729 structure = globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget);
730
731 return ArrayAllocationProfile::updateLastAllocationFor(profile, JSArray::create(exec->vm(), structure, initialLength));
732}
733
734inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, unsigned initialLength = 0, JSValue newTarget = JSValue())
735{
736 return constructEmptyArray(exec, profile, exec->lexicalGlobalObject(), initialLength, newTarget);
737}
738
739inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const ArgList& values, JSValue newTarget = JSValue())
740{
741 return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget), values));
742}
743
744inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const ArgList& values, JSValue newTarget = JSValue())
745{
746 return constructArray(exec, profile, exec->lexicalGlobalObject(), values, newTarget);
747}
748
749inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
750{
751 return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget), values, length));
752}
753
754inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
755{
756 return constructArray(exec, profile, exec->lexicalGlobalObject(), values, length, newTarget);
757}
758
759inline JSArray* constructArrayNegativeIndexed(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
760{
761 return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArrayNegativeIndexed(exec, globalObject->arrayStructureForProfileDuringAllocation(exec, profile, newTarget), values, length));
762}
763
764inline JSArray* constructArrayNegativeIndexed(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, unsigned length, JSValue newTarget = JSValue())
765{
766 return constructArrayNegativeIndexed(exec, profile, exec->lexicalGlobalObject(), values, length, newTarget);
767}
768
769inline JSObject* ExecState::globalThisValue() const
770{
771 return lexicalGlobalObject()->globalThis();
772}
773
774inline JSObject* JSScope::globalThis()
775{
776 return globalObject()->globalThis();
777}
778
779inline JSObject* JSGlobalObject::globalThis() const
780{
781 return m_globalThis.get();
782}
783
784} // namespace JSC
785
786#endif // JSGlobalObject_h
787