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