bb75492c5683ac65bbb9979dc0d885ae1c1608d8
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSGlobalObject.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2014 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "JSGlobalObject.h"
32
33 #include "Arguments.h"
34 #include "ArgumentsIteratorConstructor.h"
35 #include "ArgumentsIteratorPrototype.h"
36 #include "ArrayConstructor.h"
37 #include "ArrayIteratorConstructor.h"
38 #include "ArrayIteratorPrototype.h"
39 #include "ArrayPrototype.h"
40 #include "BooleanConstructor.h"
41 #include "BooleanPrototype.h"
42 #include "CodeBlock.h"
43 #include "CodeCache.h"
44 #include "ConsolePrototype.h"
45 #include "DateConstructor.h"
46 #include "DatePrototype.h"
47 #include "Debugger.h"
48 #include "DebuggerScope.h"
49 #include "Error.h"
50 #include "ErrorConstructor.h"
51 #include "ErrorPrototype.h"
52 #include "FunctionConstructor.h"
53 #include "FunctionPrototype.h"
54 #include "GetterSetter.h"
55 #include "HeapIterationScope.h"
56 #include "Interpreter.h"
57 #include "JSAPIWrapperObject.h"
58 #include "JSArgumentsIterator.h"
59 #include "JSArrayBuffer.h"
60 #include "JSArrayBufferConstructor.h"
61 #include "JSArrayBufferPrototype.h"
62 #include "JSArrayIterator.h"
63 #include "JSBoundFunction.h"
64 #include "JSCInlines.h"
65 #include "JSCallbackConstructor.h"
66 #include "JSCallbackFunction.h"
67 #include "JSCallbackObject.h"
68 #include "JSCatchScope.h"
69 #include "JSConsole.h"
70 #include "JSDataView.h"
71 #include "JSDataViewPrototype.h"
72 #include "JSFunction.h"
73 #include "JSFunctionNameScope.h"
74 #include "JSGenericTypedArrayViewConstructorInlines.h"
75 #include "JSGenericTypedArrayViewInlines.h"
76 #include "JSGenericTypedArrayViewPrototypeInlines.h"
77 #include "JSGlobalObjectFunctions.h"
78 #include "JSLexicalEnvironment.h"
79 #include "JSLock.h"
80 #include "JSMap.h"
81 #include "JSMapIterator.h"
82 #include "JSONObject.h"
83 #include "JSSet.h"
84 #include "JSSetIterator.h"
85 #include "JSTypedArrayConstructors.h"
86 #include "JSTypedArrayPrototypes.h"
87 #include "JSTypedArrays.h"
88 #include "JSWeakMap.h"
89 #include "JSWithScope.h"
90 #include "LegacyProfiler.h"
91 #include "Lookup.h"
92 #include "MapConstructor.h"
93 #include "MapIteratorConstructor.h"
94 #include "MapIteratorPrototype.h"
95 #include "MapPrototype.h"
96 #include "MathObject.h"
97 #include "Microtask.h"
98 #include "NativeErrorConstructor.h"
99 #include "NativeErrorPrototype.h"
100 #include "NullGetterFunction.h"
101 #include "NullSetterFunction.h"
102 #include "NumberConstructor.h"
103 #include "NumberPrototype.h"
104 #include "ObjCCallbackFunction.h"
105 #include "ObjectConstructor.h"
106 #include "ObjectPrototype.h"
107 #include "ParserError.h"
108 #include "RegExpConstructor.h"
109 #include "RegExpMatchesArray.h"
110 #include "RegExpObject.h"
111 #include "RegExpPrototype.h"
112 #include "SetConstructor.h"
113 #include "SetIteratorConstructor.h"
114 #include "SetIteratorPrototype.h"
115 #include "SetPrototype.h"
116 #include "StrictEvalActivation.h"
117 #include "StringConstructor.h"
118 #include "StringPrototype.h"
119 #include "Symbol.h"
120 #include "SymbolConstructor.h"
121 #include "SymbolPrototype.h"
122 #include "VariableWatchpointSetInlines.h"
123 #include "WeakMapConstructor.h"
124 #include "WeakMapPrototype.h"
125
126 #if ENABLE(PROMISES)
127 #include "JSPromise.h"
128 #include "JSPromiseConstructor.h"
129 #include "JSPromisePrototype.h"
130 #endif // ENABLE(PROMISES)
131
132 #if ENABLE(REMOTE_INSPECTOR)
133 #include "JSGlobalObjectDebuggable.h"
134 #include "JSGlobalObjectInspectorController.h"
135 #endif
136
137 #if ENABLE(WEB_REPLAY)
138 #include "EmptyInputCursor.h"
139 #include "JSReplayInputs.h"
140 #endif
141
142 #include "JSGlobalObject.lut.h"
143
144 namespace JSC {
145
146 const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, &globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
147
148 const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled, 0, &shouldInterruptScriptBeforeTimeout };
149
150 /* Source for JSGlobalObject.lut.h
151 @begin globalObjectTable
152   parseInt              globalFuncParseInt              DontEnum|Function 2
153   parseFloat            globalFuncParseFloat            DontEnum|Function 1
154   isNaN                 globalFuncIsNaN                 DontEnum|Function 1
155   isFinite              globalFuncIsFinite              DontEnum|Function 1
156   escape                globalFuncEscape                DontEnum|Function 1
157   unescape              globalFuncUnescape              DontEnum|Function 1
158   decodeURI             globalFuncDecodeURI             DontEnum|Function 1
159   decodeURIComponent    globalFuncDecodeURIComponent    DontEnum|Function 1
160   encodeURI             globalFuncEncodeURI             DontEnum|Function 1
161   encodeURIComponent    globalFuncEncodeURIComponent    DontEnum|Function 1
162 @end
163 */
164
165 JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
166     : Base(vm, structure, 0)
167     , m_vm(vm)
168 #if ENABLE(WEB_REPLAY)
169     , m_inputCursor(EmptyInputCursor::create())
170 #endif
171     , m_masqueradesAsUndefinedWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
172     , m_havingABadTimeWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
173     , m_varInjectionWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
174     , m_weakRandom(Options::forceWeakRandomSeed() ? Options::forcedWeakRandomSeed() : static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
175     , m_evalEnabled(true)
176     , m_experimentsEnabled(false)
177     , m_consoleClient(nullptr)
178     , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
179 {
180 }
181
182 JSGlobalObject::~JSGlobalObject()
183 {
184 #if ENABLE(REMOTE_INSPECTOR)
185     m_inspectorController->globalObjectDestroyed();
186 #endif
187
188     if (m_debugger)
189         m_debugger->detach(this, Debugger::GlobalObjectIsDestructing);
190
191     if (LegacyProfiler* profiler = vm().enabledProfiler())
192         profiler->stopProfiling(this);
193 }
194
195 void JSGlobalObject::destroy(JSCell* cell)
196 {
197     static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
198 }
199
200 void JSGlobalObject::setGlobalThis(VM& vm, JSObject* globalThis)
201 {
202     m_globalThis.set(vm, this, globalThis);
203 }
204
205 void JSGlobalObject::init(VM& vm)
206 {
207     ASSERT(vm.currentThreadIsHoldingAPILock());
208
209     JSGlobalObject::globalExec()->init(0, 0, CallFrame::noCaller(), 0, 0);
210
211     m_debugger = 0;
212
213 #if ENABLE(REMOTE_INSPECTOR)
214     m_inspectorController = std::make_unique<Inspector::JSGlobalObjectInspectorController>(*this);
215     m_inspectorDebuggable = std::make_unique<JSGlobalObjectDebuggable>(*this);
216     m_inspectorDebuggable->init();
217     m_consoleClient = m_inspectorController->consoleClient();
218 #endif
219
220     ExecState* exec = JSGlobalObject::globalExec();
221
222     m_functionPrototype.set(vm, this, FunctionPrototype::create(vm, FunctionPrototype::createStructure(vm, this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
223     m_calleeStructure.set(vm, this, JSCallee::createStructure(vm, this, jsNull()));
224
225     // Need to create the callee structure (above) before creating the callee.
226     m_globalCallee.set(vm, this, JSCallee::create(vm, this, this));
227     exec->setCallee(m_globalCallee.get());
228
229     m_functionStructure.set(vm, this, JSFunction::createStructure(vm, this, m_functionPrototype.get()));
230     m_boundFunctionStructure.set(vm, this, JSBoundFunction::createStructure(vm, this, m_functionPrototype.get()));
231     m_namedFunctionStructure.set(vm, this, Structure::addPropertyTransition(vm, m_functionStructure.get(), vm.propertyNames->name, DontDelete | ReadOnly | DontEnum, m_functionNameOffset));
232     m_internalFunctionStructure.set(vm, this, InternalFunction::createStructure(vm, this, m_functionPrototype.get()));
233     JSFunction* callFunction = 0;
234     JSFunction* applyFunction = 0;
235     m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
236     m_callFunction.set(vm, this, callFunction);
237     m_applyFunction.set(vm, this, applyFunction);
238     m_nullGetterFunction.set(vm, this, NullGetterFunction::create(vm, NullGetterFunction::createStructure(vm, this, m_functionPrototype.get())));
239     m_nullSetterFunction.set(vm, this, NullSetterFunction::create(vm, NullSetterFunction::createStructure(vm, this, m_functionPrototype.get())));
240     m_objectPrototype.set(vm, this, ObjectPrototype::create(vm, this, ObjectPrototype::createStructure(vm, this, jsNull())));
241     GetterSetter* protoAccessor = GetterSetter::create(vm, this);
242     protoAccessor->setGetter(vm, this, JSFunction::create(vm, this, 0, String(), globalFuncProtoGetter));
243     protoAccessor->setSetter(vm, this, JSFunction::create(vm, this, 0, String(), globalFuncProtoSetter));
244     m_objectPrototype->putDirectNonIndexAccessor(vm, vm.propertyNames->underscoreProto, protoAccessor, Accessor | DontEnum);
245     m_functionPrototype->structure()->setPrototypeWithoutTransition(vm, m_objectPrototype.get());
246     
247     m_typedArrays[toIndex(TypeInt8)].prototype.set(vm, this, JSInt8ArrayPrototype::create(vm, this, JSInt8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
248     m_typedArrays[toIndex(TypeInt16)].prototype.set(vm, this, JSInt16ArrayPrototype::create(vm, this, JSInt16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
249     m_typedArrays[toIndex(TypeInt32)].prototype.set(vm, this, JSInt32ArrayPrototype::create(vm, this, JSInt32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
250     m_typedArrays[toIndex(TypeUint8)].prototype.set(vm, this, JSUint8ArrayPrototype::create(vm, this, JSUint8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
251     m_typedArrays[toIndex(TypeUint8Clamped)].prototype.set(vm, this, JSUint8ClampedArrayPrototype::create(vm, this, JSUint8ClampedArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
252     m_typedArrays[toIndex(TypeUint16)].prototype.set(vm, this, JSUint16ArrayPrototype::create(vm, this, JSUint16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
253     m_typedArrays[toIndex(TypeUint32)].prototype.set(vm, this, JSUint32ArrayPrototype::create(vm, this, JSUint32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
254     m_typedArrays[toIndex(TypeFloat32)].prototype.set(vm, this, JSFloat32ArrayPrototype::create(vm, this, JSFloat32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
255     m_typedArrays[toIndex(TypeFloat64)].prototype.set(vm, this, JSFloat64ArrayPrototype::create(vm, this, JSFloat64ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
256     m_typedArrays[toIndex(TypeDataView)].prototype.set(vm, this, JSDataViewPrototype::create(vm, JSDataViewPrototype::createStructure(vm, this, m_objectPrototype.get())));
257     
258     m_typedArrays[toIndex(TypeInt8)].structure.set(vm, this, JSInt8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt8)].prototype.get()));
259     m_typedArrays[toIndex(TypeInt16)].structure.set(vm, this, JSInt16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt16)].prototype.get()));
260     m_typedArrays[toIndex(TypeInt32)].structure.set(vm, this, JSInt32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt32)].prototype.get()));
261     m_typedArrays[toIndex(TypeUint8)].structure.set(vm, this, JSUint8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8)].prototype.get()));
262     m_typedArrays[toIndex(TypeUint8Clamped)].structure.set(vm, this, JSUint8ClampedArray::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get()));
263     m_typedArrays[toIndex(TypeUint16)].structure.set(vm, this, JSUint16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint16)].prototype.get()));
264     m_typedArrays[toIndex(TypeUint32)].structure.set(vm, this, JSUint32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint32)].prototype.get()));
265     m_typedArrays[toIndex(TypeFloat32)].structure.set(vm, this, JSFloat32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat32)].prototype.get()));
266     m_typedArrays[toIndex(TypeFloat64)].structure.set(vm, this, JSFloat64Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat64)].prototype.get()));
267     m_typedArrays[toIndex(TypeDataView)].structure.set(vm, this, JSDataView::createStructure(vm, this, m_typedArrays[toIndex(TypeDataView)].prototype.get()));
268     
269     m_catchScopeStructure.set(vm, this, JSCatchScope::createStructure(vm, this, jsNull()));
270     m_functionNameScopeStructure.set(vm, this, JSFunctionNameScope::createStructure(vm, this, jsNull()));
271     m_lexicalEnvironmentStructure.set(vm, this, JSLexicalEnvironment::createStructure(vm, this));
272     m_strictEvalActivationStructure.set(vm, this, StrictEvalActivation::createStructure(vm, this, jsNull()));
273     m_debuggerScopeStructure.set(m_vm, this, DebuggerScope::createStructure(m_vm, this));
274     m_withScopeStructure.set(vm, this, JSWithScope::createStructure(vm, this, jsNull()));
275     
276     m_nullPrototypeObjectStructure.set(vm, this, JSFinalObject::createStructure(vm, this, jsNull(), JSFinalObject::defaultInlineCapacity()));
277     
278     m_callbackFunctionStructure.set(vm, this, JSCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
279     m_argumentsStructure.set(vm, this, Arguments::createStructure(vm, this, m_objectPrototype.get()));
280     m_callbackConstructorStructure.set(vm, this, JSCallbackConstructor::createStructure(vm, this, m_objectPrototype.get()));
281     m_callbackObjectStructure.set(vm, this, JSCallbackObject<JSDestructibleObject>::createStructure(vm, this, m_objectPrototype.get()));
282 #if JSC_OBJC_API_ENABLED
283     m_objcCallbackFunctionStructure.set(vm, this, ObjCCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
284     m_objcWrapperObjectStructure.set(vm, this, JSCallbackObject<JSAPIWrapperObject>::createStructure(vm, this, m_objectPrototype.get()));
285 #endif
286     
287     m_arrayPrototype.set(vm, this, ArrayPrototype::create(vm, this, ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
288     
289     m_originalArrayStructureForIndexingShape[UndecidedShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithUndecided));
290     m_originalArrayStructureForIndexingShape[Int32Shape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithInt32));
291     m_originalArrayStructureForIndexingShape[DoubleShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithDouble));
292     m_originalArrayStructureForIndexingShape[ContiguousShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithContiguous));
293     m_originalArrayStructureForIndexingShape[ArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithArrayStorage));
294     m_originalArrayStructureForIndexingShape[SlowPutArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithSlowPutArrayStorage));
295     for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
296         m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
297
298     m_regExpMatchesArrayStructure.set(vm, this, Structure::create(vm, this, m_arrayPrototype.get(), TypeInfo(ObjectType, StructureFlags), JSArray::info(), ArrayWithSlowPutArrayStorage));
299     
300     RegExp* emptyRegex = RegExp::create(vm, "", NoFlags);
301     
302     m_regExpPrototype.set(vm, this, RegExpPrototype::create(vm, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()), emptyRegex));
303     m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get()));
304     
305 #if ENABLE(PROMISES)
306     m_promisePrototype.set(vm, this, JSPromisePrototype::create(exec, this, JSPromisePrototype::createStructure(vm, this, m_objectPrototype.get())));
307     m_promiseStructure.set(vm, this, JSPromise::createStructure(vm, this, m_promisePrototype.get()));
308 #endif // ENABLE(PROMISES)
309     
310 #define CREATE_PROTOTYPE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
311 m_ ## lowerName ## Prototype.set(vm, this, capitalName##Prototype::create(vm, this, capitalName##Prototype::createStructure(vm, this, m_objectPrototype.get()))); \
312 m_ ## properName ## Structure.set(vm, this, instanceType::createStructure(vm, this, m_ ## lowerName ## Prototype.get()));
313     
314     FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_PROTOTYPE_FOR_SIMPLE_TYPE)
315     
316 #undef CREATE_PROTOTYPE_FOR_SIMPLE_TYPE
317     
318     // Constructors
319     
320     ObjectConstructor* objectConstructor = ObjectConstructor::create(vm, ObjectConstructor::createStructure(vm, this, m_functionPrototype.get()), m_objectPrototype.get());
321     m_objectConstructor.set(vm, this, objectConstructor);
322     JSCell* functionConstructor = FunctionConstructor::create(vm, FunctionConstructor::createStructure(vm, this, m_functionPrototype.get()), m_functionPrototype.get());
323     JSCell* arrayConstructor = ArrayConstructor::create(vm, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get());
324     
325     m_regExpConstructor.set(vm, this, RegExpConstructor::create(vm, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get()));
326     
327 #define CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
328 capitalName ## Constructor* lowerName ## Constructor = capitalName ## Constructor::create(vm, capitalName ## Constructor::createStructure(vm, this, m_functionPrototype.get()), m_ ## lowerName ## Prototype.get()); \
329 m_ ## lowerName ## Prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, lowerName ## Constructor, DontEnum); \
330
331     FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE)
332     
333 #undef CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE
334     
335     m_errorConstructor.set(vm, this, errorConstructor);
336     
337     Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(vm, this, m_errorPrototype.get());
338     Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(vm, this, m_functionPrototype.get());
339     m_evalErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("EvalError")));
340     m_rangeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("RangeError")));
341     m_referenceErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("ReferenceError")));
342     m_syntaxErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("SyntaxError")));
343     m_typeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("TypeError")));
344     m_URIErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("URIError")));
345 #if ENABLE(PROMISES)
346     m_promiseConstructor.set(vm, this, JSPromiseConstructor::create(vm, JSPromiseConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promisePrototype.get()));
347 #endif
348     
349     m_objectPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, objectConstructor, DontEnum);
350     m_functionPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, functionConstructor, DontEnum);
351     m_arrayPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, arrayConstructor, DontEnum);
352     m_regExpPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_regExpConstructor.get(), DontEnum);
353 #if ENABLE(PROMISES)
354     m_promisePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_promiseConstructor.get(), DontEnum);
355 #endif
356     
357     putDirectWithoutTransition(vm, vm.propertyNames->Object, objectConstructor, DontEnum);
358     putDirectWithoutTransition(vm, vm.propertyNames->Function, functionConstructor, DontEnum);
359     putDirectWithoutTransition(vm, vm.propertyNames->Array, arrayConstructor, DontEnum);
360     putDirectWithoutTransition(vm, vm.propertyNames->RegExp, m_regExpConstructor.get(), DontEnum);
361     putDirectWithoutTransition(vm, vm.propertyNames->EvalError, m_evalErrorConstructor.get(), DontEnum);
362     putDirectWithoutTransition(vm, vm.propertyNames->RangeError, m_rangeErrorConstructor.get(), DontEnum);
363     putDirectWithoutTransition(vm, vm.propertyNames->ReferenceError, m_referenceErrorConstructor.get(), DontEnum);
364     putDirectWithoutTransition(vm, vm.propertyNames->SyntaxError, m_syntaxErrorConstructor.get(), DontEnum);
365     putDirectWithoutTransition(vm, vm.propertyNames->TypeError, m_typeErrorConstructor.get(), DontEnum);
366     putDirectWithoutTransition(vm, vm.propertyNames->URIError, m_URIErrorConstructor.get(), DontEnum);
367 #if ENABLE(PROMISES)
368     putDirectWithoutTransition(vm, vm.propertyNames->Promise, m_promiseConstructor.get(), DontEnum);
369 #endif
370     
371     
372 #define PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
373 putDirectWithoutTransition(vm, vm.propertyNames-> jsName, lowerName ## Constructor, DontEnum); \
374
375     FOR_EACH_SIMPLE_BUILTIN_TYPE_WITH_CONSTRUCTOR(PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE)
376     
377 #undef PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE
378     PrototypeMap& prototypeMap = vm.prototypeMap;
379     Structure* iteratorResultStructure = prototypeMap.emptyObjectStructureForPrototype(m_objectPrototype.get(), JSFinalObject::defaultInlineCapacity());
380     PropertyOffset offset;
381     iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->done, 0, offset);
382     iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->value, 0, offset);
383     m_iteratorResultStructure.set(vm, this, iteratorResultStructure);
384     
385     m_evalFunction.set(vm, this, JSFunction::create(vm, this, 1, vm.propertyNames->eval.string(), globalFuncEval));
386     putDirectWithoutTransition(vm, vm.propertyNames->eval, m_evalFunction.get(), DontEnum);
387     
388     putDirectWithoutTransition(vm, vm.propertyNames->JSON, JSONObject::create(vm, JSONObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
389     putDirectWithoutTransition(vm, vm.propertyNames->Math, MathObject::create(vm, this, MathObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
390     
391     std::array<InternalFunction*, NUMBER_OF_TYPED_ARRAY_TYPES> typedArrayConstructors;
392     typedArrayConstructors[toIndex(TypeInt8)] = JSInt8ArrayConstructor::create(vm, JSInt8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt8)].prototype.get(), ASCIILiteral("Int8Array"));
393     typedArrayConstructors[toIndex(TypeInt16)] = JSInt16ArrayConstructor::create(vm, JSInt16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt16)].prototype.get(), ASCIILiteral("Int16Array"));
394     typedArrayConstructors[toIndex(TypeInt32)] = JSInt32ArrayConstructor::create(vm, JSInt32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt32)].prototype.get(), ASCIILiteral("Int32Array"));
395     typedArrayConstructors[toIndex(TypeUint8)] = JSUint8ArrayConstructor::create(vm, JSUint8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8)].prototype.get(), ASCIILiteral("Uint8Array"));
396     typedArrayConstructors[toIndex(TypeUint8Clamped)] = JSUint8ClampedArrayConstructor::create(vm, JSUint8ClampedArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get(), ASCIILiteral("Uint8ClampedArray"));
397     typedArrayConstructors[toIndex(TypeUint16)] = JSUint16ArrayConstructor::create(vm, JSUint16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint16)].prototype.get(), ASCIILiteral("Uint16Array"));
398     typedArrayConstructors[toIndex(TypeUint32)] = JSUint32ArrayConstructor::create(vm, JSUint32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint32)].prototype.get(), ASCIILiteral("Uint32Array"));
399     typedArrayConstructors[toIndex(TypeFloat32)] = JSFloat32ArrayConstructor::create(vm, JSFloat32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat32)].prototype.get(), ASCIILiteral("Float32Array"));
400     typedArrayConstructors[toIndex(TypeFloat64)] = JSFloat64ArrayConstructor::create(vm, JSFloat64ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat64)].prototype.get(), ASCIILiteral("Float64Array"));
401     typedArrayConstructors[toIndex(TypeDataView)] = JSDataViewConstructor::create(vm, JSDataViewConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeDataView)].prototype.get(), ASCIILiteral("DataView"));
402     
403     for (unsigned typedArrayIndex = NUMBER_OF_TYPED_ARRAY_TYPES; typedArrayIndex--;) {
404         m_typedArrays[typedArrayIndex].prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, typedArrayConstructors[typedArrayIndex], DontEnum);
405         putDirectWithoutTransition(vm, Identifier(exec, typedArrayConstructors[typedArrayIndex]->name(exec)), typedArrayConstructors[typedArrayIndex], DontEnum);
406     }
407     
408     JSFunction* builtinLog = JSFunction::create(vm, this, 1, vm.propertyNames->emptyIdentifier.string(), globalFuncBuiltinLog);
409
410     JSFunction* privateFuncAbs = JSFunction::create(vm, this, 0, String(), globalPrivateFuncAbs);
411     JSFunction* privateFuncFloor = JSFunction::create(vm, this, 0, String(), globalPrivateFuncFloor);
412     JSFunction* privateFuncIsFinite = JSFunction::create(vm, this, 0, String(), globalFuncIsFinite);
413
414     GlobalPropertyInfo staticGlobals[] = {
415         GlobalPropertyInfo(vm.propertyNames->NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
416         GlobalPropertyInfo(vm.propertyNames->Infinity, jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
417         GlobalPropertyInfo(vm.propertyNames->undefinedKeyword, jsUndefined(), DontEnum | DontDelete | ReadOnly),
418         GlobalPropertyInfo(vm.propertyNames->undefinedPrivateName, jsUndefined(), DontEnum | DontDelete | ReadOnly),
419         GlobalPropertyInfo(vm.propertyNames->ObjectPrivateName, objectConstructor, DontEnum | DontDelete | ReadOnly),
420         GlobalPropertyInfo(vm.propertyNames->TypeErrorPrivateName, m_typeErrorConstructor.get(), DontEnum | DontDelete | ReadOnly),
421         GlobalPropertyInfo(vm.propertyNames->BuiltinLogPrivateName, builtinLog, DontEnum | DontDelete | ReadOnly),
422         GlobalPropertyInfo(vm.propertyNames->ArrayPrivateName, arrayConstructor, DontEnum | DontDelete | ReadOnly),
423         GlobalPropertyInfo(vm.propertyNames->NumberPrivateName, numberConstructor, DontEnum | DontDelete | ReadOnly),
424         GlobalPropertyInfo(vm.propertyNames->absPrivateName, privateFuncAbs, DontEnum | DontDelete | ReadOnly),
425         GlobalPropertyInfo(vm.propertyNames->floorPrivateName, privateFuncFloor, DontEnum | DontDelete | ReadOnly),
426         GlobalPropertyInfo(vm.propertyNames->isFinitePrivateName, privateFuncIsFinite, DontEnum | DontDelete | ReadOnly),
427     };
428     addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
429     
430     m_specialPointers[Special::CallFunction] = m_callFunction.get();
431     m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
432     m_specialPointers[Special::ObjectConstructor] = objectConstructor;
433     m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
434     
435     ConsolePrototype* consolePrototype = ConsolePrototype::create(vm, this, ConsolePrototype::createStructure(vm, this, m_objectPrototype.get()));
436     m_consoleStructure.set(vm, this, JSConsole::createStructure(vm, this, consolePrototype));
437     JSConsole* consoleObject = JSConsole::create(vm, m_consoleStructure.get());
438     putDirectWithoutTransition(vm, Identifier(exec, "console"), consoleObject, DontEnum);
439     
440     resetPrototype(vm, prototype());
441 }
442
443 void JSGlobalObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
444 {
445     JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
446     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
447
448     if (symbolTablePut(thisObject, exec, propertyName, value, slot.isStrictMode()))
449         return;
450     Base::put(thisObject, exec, propertyName, value, slot);
451 }
452
453 bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
454 {
455     JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
456     PropertySlot slot(thisObject);
457     // silently ignore attempts to add accessors aliasing vars.
458     if (descriptor.isAccessorDescriptor() && symbolTableGet(thisObject, propertyName, slot))
459         return false;
460     return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
461 }
462
463 JSGlobalObject::NewGlobalVar JSGlobalObject::addGlobalVar(const Identifier& ident, ConstantMode constantMode)
464 {
465     ConcurrentJITLocker locker(symbolTable()->m_lock);
466     int index = symbolTable()->size(locker);
467     SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
468     if (constantMode == IsVariable)
469         newEntry.prepareToWatch(symbolTable());
470     SymbolTable::Map::AddResult result = symbolTable()->add(locker, ident.impl(), newEntry);
471     if (result.isNewEntry)
472         addRegisters(1);
473     else
474         index = result.iterator->value.getIndex();
475     NewGlobalVar var;
476     var.registerNumber = index;
477     var.set = result.iterator->value.watchpointSet();
478     return var;
479 }
480
481 void JSGlobalObject::addFunction(ExecState* exec, const Identifier& propertyName, JSValue value)
482 {
483     VM& vm = exec->vm();
484     removeDirect(vm, propertyName); // Newly declared functions overwrite existing properties.
485     NewGlobalVar var = addGlobalVar(propertyName, IsVariable);
486     registerAt(var.registerNumber).set(exec->vm(), this, value);
487     if (var.set)
488         var.set->notifyWrite(vm, value, VariableWriteFireDetail(this, propertyName));
489 }
490
491 static inline JSObject* lastInPrototypeChain(JSObject* object)
492 {
493     JSObject* o = object;
494     while (o->prototype().isObject())
495         o = asObject(o->prototype());
496     return o;
497 }
498
499 // Private namespace for helpers for JSGlobalObject::haveABadTime()
500 namespace {
501
502 class ObjectsWithBrokenIndexingFinder : public MarkedBlock::VoidFunctor {
503 public:
504     ObjectsWithBrokenIndexingFinder(MarkedArgumentBuffer&, JSGlobalObject*);
505     void operator()(JSCell*);
506
507 private:
508     MarkedArgumentBuffer& m_foundObjects;
509     JSGlobalObject* m_globalObject;
510 };
511
512 ObjectsWithBrokenIndexingFinder::ObjectsWithBrokenIndexingFinder(
513     MarkedArgumentBuffer& foundObjects, JSGlobalObject* globalObject)
514     : m_foundObjects(foundObjects)
515     , m_globalObject(globalObject)
516 {
517 }
518
519 inline bool hasBrokenIndexing(JSObject* object)
520 {
521     // This will change if we have more indexing types.
522     IndexingType type = object->indexingType();
523     // This could be made obviously more efficient, but isn't made so right now, because
524     // we expect this to be an unlikely slow path anyway.
525     return hasUndecided(type) || hasInt32(type) || hasDouble(type) || hasContiguous(type) || hasArrayStorage(type);
526 }
527
528 void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
529 {
530     if (!cell->isObject())
531         return;
532     
533     JSObject* object = asObject(cell);
534
535     // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
536     if (!hasBrokenIndexing(object))
537         return;
538     
539     // We only want to have a bad time in the affected global object, not in the entire
540     // VM. But we have to be careful, since there may be objects that claim to belong to
541     // a different global object that have prototypes from our global object.
542     bool foundGlobalObject = false;
543     for (JSObject* current = object; ;) {
544         if (current->globalObject() == m_globalObject) {
545             foundGlobalObject = true;
546             break;
547         }
548         
549         JSValue prototypeValue = current->prototype();
550         if (prototypeValue.isNull())
551             break;
552         current = asObject(prototypeValue);
553     }
554     if (!foundGlobalObject)
555         return;
556     
557     m_foundObjects.append(object);
558 }
559
560 } // end private namespace for helpers for JSGlobalObject::haveABadTime()
561
562 void JSGlobalObject::haveABadTime(VM& vm)
563 {
564     ASSERT(&vm == &this->vm());
565     
566     if (isHavingABadTime())
567         return;
568     
569     // Make sure that all allocations or indexed storage transitions that are inlining
570     // the assumption that it's safe to transition to a non-SlowPut array storage don't
571     // do so anymore.
572     m_havingABadTimeWatchpoint->fireAll("Having a bad time");
573     ASSERT(isHavingABadTime()); // The watchpoint is what tells us that we're having a bad time.
574     
575     // Make sure that all JSArray allocations that load the appropriate structure from
576     // this object now load a structure that uses SlowPut.
577     for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
578         m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
579     
580     // Make sure that all objects that have indexed storage switch to the slow kind of
581     // indexed storage.
582     MarkedArgumentBuffer foundObjects; // Use MarkedArgumentBuffer because switchToSlowPutArrayStorage() may GC.
583     ObjectsWithBrokenIndexingFinder finder(foundObjects, this);
584     {
585         HeapIterationScope iterationScope(vm.heap);
586         vm.heap.objectSpace().forEachLiveCell(iterationScope, finder);
587     }
588     while (!foundObjects.isEmpty()) {
589         JSObject* object = asObject(foundObjects.last());
590         foundObjects.removeLast();
591         ASSERT(hasBrokenIndexing(object));
592         object->switchToSlowPutArrayStorage(vm);
593     }
594 }
595
596 bool JSGlobalObject::objectPrototypeIsSane()
597 {
598     return !hasIndexedProperties(m_objectPrototype->indexingType())
599         && m_objectPrototype->prototype().isNull();
600 }
601
602 bool JSGlobalObject::arrayPrototypeChainIsSane()
603 {
604     return !hasIndexedProperties(m_arrayPrototype->indexingType())
605         && m_arrayPrototype->prototype() == m_objectPrototype.get()
606         && objectPrototypeIsSane();
607 }
608
609 bool JSGlobalObject::stringPrototypeChainIsSane()
610 {
611     return !hasIndexedProperties(m_stringPrototype->indexingType())
612         && m_stringPrototype->prototype() == m_objectPrototype.get()
613         && objectPrototypeIsSane();
614 }
615
616 void JSGlobalObject::createThrowTypeError(VM& vm)
617 {
618     JSFunction* thrower = JSFunction::create(vm, this, 0, String(), globalFuncThrowTypeError);
619     GetterSetter* getterSetter = GetterSetter::create(vm, this);
620     getterSetter->setGetter(vm, this, thrower);
621     getterSetter->setSetter(vm, this, thrower);
622     m_throwTypeErrorGetterSetter.set(vm, this, getterSetter);
623 }
624
625 // Set prototype, and also insert the object prototype at the end of the chain.
626 void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
627 {
628     setPrototype(vm, prototype);
629
630     JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
631     JSObject* objectPrototype = m_objectPrototype.get();
632     if (oldLastInPrototypeChain != objectPrototype)
633         oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
634
635     // Whenever we change the prototype of the global object, we need to create a new JSProxy with the correct prototype.
636     setGlobalThis(vm, JSProxy::create(vm, JSProxy::createStructure(vm, this, prototype, PureForwardingProxyType), this));
637 }
638
639 void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
640
641     JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
642     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
643     Base::visitChildren(thisObject, visitor);
644
645     visitor.append(&thisObject->m_globalThis);
646
647     visitor.append(&thisObject->m_globalCallee);
648     visitor.append(&thisObject->m_regExpConstructor);
649     visitor.append(&thisObject->m_errorConstructor);
650     visitor.append(&thisObject->m_evalErrorConstructor);
651     visitor.append(&thisObject->m_rangeErrorConstructor);
652     visitor.append(&thisObject->m_referenceErrorConstructor);
653     visitor.append(&thisObject->m_syntaxErrorConstructor);
654     visitor.append(&thisObject->m_typeErrorConstructor);
655     visitor.append(&thisObject->m_URIErrorConstructor);
656     visitor.append(&thisObject->m_objectConstructor);
657 #if ENABLE(PROMISES)
658     visitor.append(&thisObject->m_promiseConstructor);
659 #endif
660
661     visitor.append(&thisObject->m_nullGetterFunction);
662     visitor.append(&thisObject->m_nullSetterFunction);
663
664     visitor.append(&thisObject->m_evalFunction);
665     visitor.append(&thisObject->m_callFunction);
666     visitor.append(&thisObject->m_applyFunction);
667     visitor.append(&thisObject->m_throwTypeErrorGetterSetter);
668
669     visitor.append(&thisObject->m_objectPrototype);
670     visitor.append(&thisObject->m_functionPrototype);
671     visitor.append(&thisObject->m_arrayPrototype);
672     visitor.append(&thisObject->m_errorPrototype);
673 #if ENABLE(PROMISES)
674     visitor.append(&thisObject->m_promisePrototype);
675 #endif
676
677     visitor.append(&thisObject->m_debuggerScopeStructure);
678     visitor.append(&thisObject->m_withScopeStructure);
679     visitor.append(&thisObject->m_strictEvalActivationStructure);
680     visitor.append(&thisObject->m_lexicalEnvironmentStructure);
681     visitor.append(&thisObject->m_catchScopeStructure);
682     visitor.append(&thisObject->m_functionNameScopeStructure);
683     visitor.append(&thisObject->m_argumentsStructure);
684     for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
685         visitor.append(&thisObject->m_originalArrayStructureForIndexingShape[i]);
686     for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
687         visitor.append(&thisObject->m_arrayStructureForIndexingShapeDuringAllocation[i]);
688     visitor.append(&thisObject->m_booleanObjectStructure);
689     visitor.append(&thisObject->m_callbackConstructorStructure);
690     visitor.append(&thisObject->m_callbackFunctionStructure);
691     visitor.append(&thisObject->m_callbackObjectStructure);
692 #if JSC_OBJC_API_ENABLED
693     visitor.append(&thisObject->m_objcCallbackFunctionStructure);
694     visitor.append(&thisObject->m_objcWrapperObjectStructure);
695 #endif
696     visitor.append(&thisObject->m_nullPrototypeObjectStructure);
697     visitor.append(&thisObject->m_errorStructure);
698     visitor.append(&thisObject->m_calleeStructure);
699     visitor.append(&thisObject->m_functionStructure);
700     visitor.append(&thisObject->m_boundFunctionStructure);
701     visitor.append(&thisObject->m_namedFunctionStructure);
702     visitor.append(&thisObject->m_symbolObjectStructure);
703     visitor.append(&thisObject->m_regExpMatchesArrayStructure);
704     visitor.append(&thisObject->m_regExpStructure);
705     visitor.append(&thisObject->m_consoleStructure);
706     visitor.append(&thisObject->m_internalFunctionStructure);
707
708 #if ENABLE(PROMISES)
709     visitor.append(&thisObject->m_promiseStructure);
710 #endif // ENABLE(PROMISES)
711
712 #define VISIT_SIMPLE_TYPE(CapitalName, lowerName, properName, instanceType, jsName) \
713     visitor.append(&thisObject->m_ ## lowerName ## Prototype); \
714     visitor.append(&thisObject->m_ ## properName ## Structure); \
715
716     FOR_EACH_SIMPLE_BUILTIN_TYPE(VISIT_SIMPLE_TYPE)
717
718 #undef VISIT_SIMPLE_TYPE
719
720     for (unsigned i = NUMBER_OF_TYPED_ARRAY_TYPES; i--;) {
721         visitor.append(&thisObject->m_typedArrays[i].prototype);
722         visitor.append(&thisObject->m_typedArrays[i].structure);
723     }
724 }
725
726 JSValue JSGlobalObject::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
727 {
728     if (ecmaMode == StrictMode)
729         return jsUndefined();
730     return exec->globalThisValue();
731 }
732
733 ExecState* JSGlobalObject::globalExec()
734 {
735     return CallFrame::create(m_globalCallFrame);
736 }
737
738 void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
739 {
740     addRegisters(count);
741
742     for (int i = 0; i < count; ++i) {
743         GlobalPropertyInfo& global = globals[i];
744         ASSERT(global.attributes & DontDelete);
745         
746         int index = symbolTable()->size();
747         SymbolTableEntry newEntry(index, global.attributes);
748         symbolTable()->add(global.identifier.impl(), newEntry);
749         registerAt(index).set(vm(), this, global.value);
750     }
751 }
752
753 bool JSGlobalObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
754 {
755     JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
756     if (getStaticFunctionSlot<Base>(exec, globalObjectTable, thisObject, propertyName, slot))
757         return true;
758     return symbolTableGet(thisObject, propertyName, slot);
759 }
760
761 void JSGlobalObject::clearRareData(JSCell* cell)
762 {
763     jsCast<JSGlobalObject*>(cell)->m_rareData = nullptr;
764 }
765
766 void slowValidateCell(JSGlobalObject* globalObject)
767 {
768     RELEASE_ASSERT(globalObject->isGlobalObject());
769     ASSERT_GC_OBJECT_INHERITS(globalObject, JSGlobalObject::info());
770 }
771
772 UnlinkedProgramCodeBlock* JSGlobalObject::createProgramCodeBlock(CallFrame* callFrame, ProgramExecutable* executable, JSObject** exception)
773 {
774     ParserError error;
775     JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
776     DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
777     ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
778     UnlinkedProgramCodeBlock* unlinkedCodeBlock = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
779
780     if (hasDebugger())
781         debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
782
783     if (error.m_type != ParserError::ErrorNone) {
784         *exception = error.toErrorObject(this, executable->source());
785         return 0;
786     }
787     
788     return unlinkedCodeBlock;
789 }
790
791 UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CallFrame* callFrame, EvalExecutable* executable)
792 {
793     ParserError error;
794     JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
795     DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
796     ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
797     UnlinkedEvalCodeBlock* unlinkedCodeBlock = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
798
799     if (hasDebugger())
800         debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
801
802     if (error.m_type != ParserError::ErrorNone) {
803         throwVMError(callFrame, error.toErrorObject(this, executable->source()));
804         return 0;
805     }
806
807     return unlinkedCodeBlock;
808 }
809
810 void JSGlobalObject::setRemoteDebuggingEnabled(bool enabled)
811 {
812 #if ENABLE(REMOTE_INSPECTOR)
813     m_inspectorDebuggable->setRemoteDebuggingAllowed(enabled);
814 #else
815     UNUSED_PARAM(enabled);
816 #endif
817 }
818
819 bool JSGlobalObject::remoteDebuggingEnabled() const
820 {
821 #if ENABLE(REMOTE_INSPECTOR)
822     return m_inspectorDebuggable->remoteDebuggingAllowed();
823 #else
824     return false;
825 #endif
826 }
827
828 #if ENABLE(WEB_REPLAY)
829 void JSGlobalObject::setInputCursor(PassRefPtr<InputCursor> prpCursor)
830 {
831     m_inputCursor = prpCursor;
832     ASSERT(m_inputCursor);
833
834     InputCursor& cursor = inputCursor();
835     // Save or set the random seed. This performed here rather than the constructor
836     // to avoid threading the input cursor through all the abstraction layers.
837     if (cursor.isCapturing())
838         cursor.appendInput<SetRandomSeed>(m_weakRandom.seedUnsafe());
839     else if (cursor.isReplaying()) {
840         if (SetRandomSeed* input = cursor.fetchInput<SetRandomSeed>())
841             m_weakRandom.initializeSeed(static_cast<unsigned>(input->randomSeed()));
842     }
843 }
844 #endif
845
846 void JSGlobalObject::setName(const String& name)
847 {
848     m_name = name;
849
850 #if ENABLE(REMOTE_INSPECTOR)
851     m_inspectorDebuggable->update();
852 #endif
853 }
854
855 void JSGlobalObject::queueMicrotask(PassRefPtr<Microtask> task)
856 {
857     if (globalObjectMethodTable()->queueTaskToEventLoop)
858         globalObjectMethodTable()->queueTaskToEventLoop(this, task);
859     else
860         WTFLogAlways("ERROR: Event loop not supported.");
861 }
862
863 } // namespace JSC