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