2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 Computer, 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.
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.
31 #include "JSGlobalObject.h"
33 #include "Arguments.h"
34 #include "ArgumentsIteratorConstructor.h"
35 #include "ArgumentsIteratorPrototype.h"
36 #include "ArrayConstructor.h"
37 #include "ArrayIteratorConstructor.h"
38 #include "ArrayIteratorPrototype.h"
39 #include "ArrayPrototype.h"
40 #include "BooleanConstructor.h"
41 #include "BooleanPrototype.h"
42 #include "CodeBlock.h"
43 #include "CodeCache.h"
44 #include "DateConstructor.h"
45 #include "DatePrototype.h"
48 #include "ErrorConstructor.h"
49 #include "ErrorPrototype.h"
50 #include "FunctionConstructor.h"
51 #include "FunctionPrototype.h"
52 #include "GetterSetter.h"
53 #include "HeapIterationScope.h"
54 #include "Interpreter.h"
55 #include "JSAPIWrapperObject.h"
56 #include "JSActivation.h"
57 #include "JSArgumentsIterator.h"
58 #include "JSArrayBuffer.h"
59 #include "JSArrayBufferConstructor.h"
60 #include "JSArrayBufferPrototype.h"
61 #include "JSArrayIterator.h"
62 #include "JSBoundFunction.h"
63 #include "JSCallbackConstructor.h"
64 #include "JSCallbackFunction.h"
65 #include "JSCallbackObject.h"
66 #include "JSDataView.h"
67 #include "JSDataViewPrototype.h"
68 #include "JSFunction.h"
69 #include "JSGenericTypedArrayViewConstructorInlines.h"
70 #include "JSGenericTypedArrayViewInlines.h"
71 #include "JSGenericTypedArrayViewPrototypeInlines.h"
72 #include "JSGlobalObjectFunctions.h"
75 #include "JSMapIterator.h"
76 #include "JSNameScope.h"
77 #include "JSONObject.h"
79 #include "JSSetIterator.h"
80 #include "JSTypedArrayConstructors.h"
81 #include "JSTypedArrayPrototypes.h"
82 #include "JSTypedArrays.h"
83 #include "JSWeakMap.h"
84 #include "JSWithScope.h"
85 #include "LegacyProfiler.h"
87 #include "MapConstructor.h"
88 #include "MapIteratorConstructor.h"
89 #include "MapIteratorPrototype.h"
90 #include "MapPrototype.h"
91 #include "MathObject.h"
92 #include "Microtask.h"
93 #include "NameConstructor.h"
94 #include "NameInstance.h"
95 #include "NamePrototype.h"
96 #include "NativeErrorConstructor.h"
97 #include "NativeErrorPrototype.h"
98 #include "NumberConstructor.h"
99 #include "NumberPrototype.h"
100 #include "ObjCCallbackFunction.h"
101 #include "ObjectConstructor.h"
102 #include "ObjectPrototype.h"
103 #include "Operations.h"
104 #include "ParserError.h"
105 #include "RegExpConstructor.h"
106 #include "RegExpMatchesArray.h"
107 #include "RegExpObject.h"
108 #include "RegExpPrototype.h"
109 #include "SetConstructor.h"
110 #include "SetIteratorConstructor.h"
111 #include "SetIteratorPrototype.h"
112 #include "SetPrototype.h"
113 #include "StrictEvalActivation.h"
114 #include "StringConstructor.h"
115 #include "StringPrototype.h"
116 #include "WeakMapConstructor.h"
117 #include "WeakMapPrototype.h"
120 #include "JSPromise.h"
121 #include "JSPromiseConstructor.h"
122 #include "JSPromisePrototype.h"
123 #endif // ENABLE(PROMISES)
125 #if ENABLE(REMOTE_INSPECTOR)
126 #include "JSGlobalObjectDebuggable.h"
127 #include "RemoteInspector.h"
130 #include "JSGlobalObject.lut.h"
134 const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
136 const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled, 0, &shouldInterruptScriptBeforeTimeout };
138 /* Source for JSGlobalObject.lut.h
139 @begin globalObjectTable
140 parseInt globalFuncParseInt DontEnum|Function 2
141 parseFloat globalFuncParseFloat DontEnum|Function 1
142 isNaN globalFuncIsNaN DontEnum|Function 1
143 isFinite globalFuncIsFinite DontEnum|Function 1
144 escape globalFuncEscape DontEnum|Function 1
145 unescape globalFuncUnescape DontEnum|Function 1
146 decodeURI globalFuncDecodeURI DontEnum|Function 1
147 decodeURIComponent globalFuncDecodeURIComponent DontEnum|Function 1
148 encodeURI globalFuncEncodeURI DontEnum|Function 1
149 encodeURIComponent globalFuncEncodeURIComponent DontEnum|Function 1
153 JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
154 : Base(vm, structure, 0)
155 , m_masqueradesAsUndefinedWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
156 , m_havingABadTimeWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
157 , m_varInjectionWatchpoint(adoptRef(new WatchpointSet(IsWatched)))
158 , m_weakRandom(Options::forceWeakRandomSeed() ? Options::forcedWeakRandomSeed() : static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
159 , m_evalEnabled(true)
160 , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
164 JSGlobalObject::~JSGlobalObject()
167 m_debugger->detach(this, Debugger::GlobalObjectIsDestructing);
169 if (LegacyProfiler* profiler = vm().enabledProfiler())
170 profiler->stopProfiling(this);
173 void JSGlobalObject::destroy(JSCell* cell)
175 static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
178 void JSGlobalObject::setGlobalThis(VM& vm, JSObject* globalThis)
180 m_globalThis.set(vm, this, globalThis);
183 void JSGlobalObject::init(JSObject* thisValue)
185 ASSERT(vm().currentThreadIsHoldingAPILock());
187 setGlobalThis(vm(), thisValue);
188 JSGlobalObject::globalExec()->init(0, 0, this, CallFrame::noCaller(), 0, 0);
192 #if ENABLE(REMOTE_INSPECTOR)
193 m_inspectorDebuggable = std::make_unique<JSGlobalObjectDebuggable>(*this);
194 m_inspectorDebuggable->init();
195 m_inspectorDebuggable->setRemoteDebuggingAllowed(true);
201 void JSGlobalObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
203 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
204 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
206 if (symbolTablePut(thisObject, exec, propertyName, value, slot.isStrictMode()))
208 Base::put(thisObject, exec, propertyName, value, slot);
211 bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
213 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
214 PropertySlot slot(thisObject);
215 // silently ignore attempts to add accessors aliasing vars.
216 if (descriptor.isAccessorDescriptor() && symbolTableGet(thisObject, propertyName, slot))
218 return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
221 JSGlobalObject::NewGlobalVar JSGlobalObject::addGlobalVar(const Identifier& ident, ConstantMode constantMode)
223 ConcurrentJITLocker locker(symbolTable()->m_lock);
224 int index = symbolTable()->size(locker);
225 SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
226 if (constantMode == IsVariable)
227 newEntry.prepareToWatch();
228 SymbolTable::Map::AddResult result = symbolTable()->add(locker, ident.impl(), newEntry);
229 if (result.isNewEntry)
232 index = result.iterator->value.getIndex();
234 var.registerNumber = index;
235 var.set = result.iterator->value.watchpointSet();
239 void JSGlobalObject::addFunction(ExecState* exec, const Identifier& propertyName, JSValue value)
241 removeDirect(exec->vm(), propertyName); // Newly declared functions overwrite existing properties.
242 NewGlobalVar var = addGlobalVar(propertyName, IsVariable);
243 registerAt(var.registerNumber).set(exec->vm(), this, value);
245 var.set->notifyWrite(value);
248 static inline JSObject* lastInPrototypeChain(JSObject* object)
250 JSObject* o = object;
251 while (o->prototype().isObject())
252 o = asObject(o->prototype());
256 void JSGlobalObject::reset(JSValue prototype)
258 ExecState* exec = JSGlobalObject::globalExec();
261 m_functionPrototype.set(vm, this, FunctionPrototype::create(vm, FunctionPrototype::createStructure(vm, this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
262 m_functionStructure.set(vm, this, JSFunction::createStructure(vm, this, m_functionPrototype.get()));
263 m_boundFunctionStructure.set(vm, this, JSBoundFunction::createStructure(vm, this, m_functionPrototype.get()));
264 m_namedFunctionStructure.set(vm, this, Structure::addPropertyTransition(vm, m_functionStructure.get(), vm.propertyNames->name, DontDelete | ReadOnly | DontEnum, 0, m_functionNameOffset));
265 m_internalFunctionStructure.set(vm, this, InternalFunction::createStructure(vm, this, m_functionPrototype.get()));
266 JSFunction* callFunction = 0;
267 JSFunction* applyFunction = 0;
268 m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
269 m_callFunction.set(vm, this, callFunction);
270 m_applyFunction.set(vm, this, applyFunction);
271 m_objectPrototype.set(vm, this, ObjectPrototype::create(vm, this, ObjectPrototype::createStructure(vm, this, jsNull())));
272 GetterSetter* protoAccessor = GetterSetter::create(vm);
273 protoAccessor->setGetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoGetter));
274 protoAccessor->setSetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoSetter));
275 m_objectPrototype->putDirectNonIndexAccessor(vm, vm.propertyNames->underscoreProto, protoAccessor, Accessor | DontEnum);
276 m_functionPrototype->structure()->setPrototypeWithoutTransition(vm, m_objectPrototype.get());
278 m_typedArrays[toIndex(TypeInt8)].prototype.set(vm, this, JSInt8ArrayPrototype::create(vm, this, JSInt8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
279 m_typedArrays[toIndex(TypeInt16)].prototype.set(vm, this, JSInt16ArrayPrototype::create(vm, this, JSInt16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
280 m_typedArrays[toIndex(TypeInt32)].prototype.set(vm, this, JSInt32ArrayPrototype::create(vm, this, JSInt32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
281 m_typedArrays[toIndex(TypeUint8)].prototype.set(vm, this, JSUint8ArrayPrototype::create(vm, this, JSUint8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
282 m_typedArrays[toIndex(TypeUint8Clamped)].prototype.set(vm, this, JSUint8ClampedArrayPrototype::create(vm, this, JSUint8ClampedArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
283 m_typedArrays[toIndex(TypeUint16)].prototype.set(vm, this, JSUint16ArrayPrototype::create(vm, this, JSUint16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
284 m_typedArrays[toIndex(TypeUint32)].prototype.set(vm, this, JSUint32ArrayPrototype::create(vm, this, JSUint32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
285 m_typedArrays[toIndex(TypeFloat32)].prototype.set(vm, this, JSFloat32ArrayPrototype::create(vm, this, JSFloat32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
286 m_typedArrays[toIndex(TypeFloat64)].prototype.set(vm, this, JSFloat64ArrayPrototype::create(vm, this, JSFloat64ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
287 m_typedArrays[toIndex(TypeDataView)].prototype.set(vm, this, JSDataViewPrototype::create(vm, JSDataViewPrototype::createStructure(vm, this, m_objectPrototype.get())));
289 m_typedArrays[toIndex(TypeInt8)].structure.set(vm, this, JSInt8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt8)].prototype.get()));
290 m_typedArrays[toIndex(TypeInt16)].structure.set(vm, this, JSInt16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt16)].prototype.get()));
291 m_typedArrays[toIndex(TypeInt32)].structure.set(vm, this, JSInt32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt32)].prototype.get()));
292 m_typedArrays[toIndex(TypeUint8)].structure.set(vm, this, JSUint8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8)].prototype.get()));
293 m_typedArrays[toIndex(TypeUint8Clamped)].structure.set(vm, this, JSUint8ClampedArray::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get()));
294 m_typedArrays[toIndex(TypeUint16)].structure.set(vm, this, JSUint16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint16)].prototype.get()));
295 m_typedArrays[toIndex(TypeUint32)].structure.set(vm, this, JSUint32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint32)].prototype.get()));
296 m_typedArrays[toIndex(TypeFloat32)].structure.set(vm, this, JSFloat32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat32)].prototype.get()));
297 m_typedArrays[toIndex(TypeFloat64)].structure.set(vm, this, JSFloat64Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat64)].prototype.get()));
298 m_typedArrays[toIndex(TypeDataView)].structure.set(vm, this, JSDataView::createStructure(vm, this, m_typedArrays[toIndex(TypeDataView)].prototype.get()));
300 m_nameScopeStructure.set(vm, this, JSNameScope::createStructure(vm, this, jsNull()));
301 m_activationStructure.set(vm, this, JSActivation::createStructure(vm, this, jsNull()));
302 m_strictEvalActivationStructure.set(vm, this, StrictEvalActivation::createStructure(vm, this, jsNull()));
303 m_withScopeStructure.set(vm, this, JSWithScope::createStructure(vm, this, jsNull()));
305 m_nullPrototypeObjectStructure.set(vm, this, JSFinalObject::createStructure(vm, this, jsNull(), JSFinalObject::defaultInlineCapacity()));
307 m_callbackFunctionStructure.set(vm, this, JSCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
308 m_argumentsStructure.set(vm, this, Arguments::createStructure(vm, this, m_objectPrototype.get()));
309 m_callbackConstructorStructure.set(vm, this, JSCallbackConstructor::createStructure(vm, this, m_objectPrototype.get()));
310 m_callbackObjectStructure.set(vm, this, JSCallbackObject<JSDestructibleObject>::createStructure(vm, this, m_objectPrototype.get()));
311 #if JSC_OBJC_API_ENABLED
312 m_objcCallbackFunctionStructure.set(vm, this, ObjCCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
313 m_objcWrapperObjectStructure.set(vm, this, JSCallbackObject<JSAPIWrapperObject>::createStructure(vm, this, m_objectPrototype.get()));
316 m_arrayPrototype.set(vm, this, ArrayPrototype::create(vm, this, ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
318 m_originalArrayStructureForIndexingShape[UndecidedShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithUndecided));
319 m_originalArrayStructureForIndexingShape[Int32Shape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithInt32));
320 m_originalArrayStructureForIndexingShape[DoubleShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithDouble));
321 m_originalArrayStructureForIndexingShape[ContiguousShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithContiguous));
322 m_originalArrayStructureForIndexingShape[ArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithArrayStorage));
323 m_originalArrayStructureForIndexingShape[SlowPutArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithSlowPutArrayStorage));
324 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
325 m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
327 m_regExpMatchesArrayStructure.set(vm, this, RegExpMatchesArray::createStructure(vm, this, m_arrayPrototype.get()));
329 RegExp* emptyRegex = RegExp::create(vm, "", NoFlags);
331 m_regExpPrototype.set(vm, this, RegExpPrototype::create(vm, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()), emptyRegex));
332 m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get()));
335 m_promisePrototype.set(vm, this, JSPromisePrototype::create(exec, this, JSPromisePrototype::createStructure(vm, this, m_objectPrototype.get())));
336 m_promiseStructure.set(vm, this, JSPromise::createStructure(vm, this, m_promisePrototype.get()));
337 #endif // ENABLE(PROMISES)
339 #define CREATE_PROTOTYPE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
340 m_ ## lowerName ## Prototype.set(vm, this, capitalName##Prototype::create(vm, this, capitalName##Prototype::createStructure(vm, this, m_objectPrototype.get()))); \
341 m_ ## properName ## Structure.set(vm, this, instanceType::createStructure(vm, this, m_ ## lowerName ## Prototype.get()));
343 FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_PROTOTYPE_FOR_SIMPLE_TYPE)
345 #undef CREATE_PROTOTYPE_FOR_SIMPLE_TYPE
349 JSCell* objectConstructor = ObjectConstructor::create(vm, ObjectConstructor::createStructure(vm, this, m_functionPrototype.get()), m_objectPrototype.get());
350 JSCell* functionConstructor = FunctionConstructor::create(vm, FunctionConstructor::createStructure(vm, this, m_functionPrototype.get()), m_functionPrototype.get());
351 JSCell* arrayConstructor = ArrayConstructor::create(vm, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get());
353 m_regExpConstructor.set(vm, this, RegExpConstructor::create(vm, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get()));
355 #define CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
356 capitalName ## Constructor* lowerName ## Constructor = capitalName ## Constructor::create(vm, capitalName ## Constructor::createStructure(vm, this, m_functionPrototype.get()), m_ ## lowerName ## Prototype.get()); \
357 m_ ## lowerName ## Prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, lowerName ## Constructor, DontEnum); \
359 FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE)
361 #undef CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE
363 m_errorConstructor.set(vm, this, errorConstructor);
365 Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(vm, this, m_errorPrototype.get());
366 Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(vm, this, m_functionPrototype.get());
367 m_evalErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("EvalError")));
368 m_rangeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("RangeError")));
369 m_referenceErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("ReferenceError")));
370 m_syntaxErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("SyntaxError")));
371 m_typeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("TypeError")));
372 m_URIErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("URIError")));
373 m_promiseConstructor.set(vm, this, JSPromiseConstructor::create(vm, JSPromiseConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promisePrototype.get()));
375 m_objectPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, objectConstructor, DontEnum);
376 m_functionPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, functionConstructor, DontEnum);
377 m_arrayPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, arrayConstructor, DontEnum);
378 m_regExpPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_regExpConstructor.get(), DontEnum);
380 m_promisePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_promiseConstructor.get(), DontEnum);
383 putDirectWithoutTransition(vm, vm.propertyNames->Object, objectConstructor, DontEnum);
384 putDirectWithoutTransition(vm, vm.propertyNames->Function, functionConstructor, DontEnum);
385 putDirectWithoutTransition(vm, vm.propertyNames->Array, arrayConstructor, DontEnum);
386 putDirectWithoutTransition(vm, vm.propertyNames->RegExp, m_regExpConstructor.get(), DontEnum);
387 putDirectWithoutTransition(vm, vm.propertyNames->EvalError, m_evalErrorConstructor.get(), DontEnum);
388 putDirectWithoutTransition(vm, vm.propertyNames->RangeError, m_rangeErrorConstructor.get(), DontEnum);
389 putDirectWithoutTransition(vm, vm.propertyNames->ReferenceError, m_referenceErrorConstructor.get(), DontEnum);
390 putDirectWithoutTransition(vm, vm.propertyNames->SyntaxError, m_syntaxErrorConstructor.get(), DontEnum);
391 putDirectWithoutTransition(vm, vm.propertyNames->TypeError, m_typeErrorConstructor.get(), DontEnum);
392 putDirectWithoutTransition(vm, vm.propertyNames->URIError, m_URIErrorConstructor.get(), DontEnum);
393 putDirectWithoutTransition(vm, vm.propertyNames->Promise, m_promiseConstructor.get(), DontEnum);
396 #define PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
397 putDirectWithoutTransition(vm, vm.propertyNames-> jsName, lowerName ## Constructor, DontEnum); \
399 FOR_EACH_SIMPLE_BUILTIN_TYPE(PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE)
401 #undef PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE
402 PrototypeMap& prototypeMap = vm.prototypeMap;
403 Structure* iteratorResultStructure = prototypeMap.emptyObjectStructureForPrototype(m_objectPrototype.get(), JSFinalObject::defaultInlineCapacity());
404 PropertyOffset offset;
405 iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->done, 0, 0, offset);
406 iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->value, 0, 0, offset);
407 m_iteratorResultStructure.set(vm, this, iteratorResultStructure);
409 m_evalFunction.set(vm, this, JSFunction::create(vm, this, 1, vm.propertyNames->eval.string(), globalFuncEval));
410 putDirectWithoutTransition(vm, vm.propertyNames->eval, m_evalFunction.get(), DontEnum);
412 putDirectWithoutTransition(vm, vm.propertyNames->JSON, JSONObject::create(vm, JSONObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
413 putDirectWithoutTransition(vm, vm.propertyNames->Math, MathObject::create(vm, this, MathObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
415 std::array<InternalFunction*, NUMBER_OF_TYPED_ARRAY_TYPES> typedArrayConstructors;
416 typedArrayConstructors[toIndex(TypeInt8)] = JSInt8ArrayConstructor::create(vm, JSInt8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt8)].prototype.get(), "Int8Array");
417 typedArrayConstructors[toIndex(TypeInt16)] = JSInt16ArrayConstructor::create(vm, JSInt16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt16)].prototype.get(), "Int16Array");
418 typedArrayConstructors[toIndex(TypeInt32)] = JSInt32ArrayConstructor::create(vm, JSInt32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt32)].prototype.get(), "Int32Array");
419 typedArrayConstructors[toIndex(TypeUint8)] = JSUint8ArrayConstructor::create(vm, JSUint8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8)].prototype.get(), "Uint8Array");
420 typedArrayConstructors[toIndex(TypeUint8Clamped)] = JSUint8ClampedArrayConstructor::create(vm, JSUint8ClampedArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get(), "Uint8ClampedArray");
421 typedArrayConstructors[toIndex(TypeUint16)] = JSUint16ArrayConstructor::create(vm, JSUint16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint16)].prototype.get(), "Uint16Array");
422 typedArrayConstructors[toIndex(TypeUint32)] = JSUint32ArrayConstructor::create(vm, JSUint32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint32)].prototype.get(), "Uint32Array");
423 typedArrayConstructors[toIndex(TypeFloat32)] = JSFloat32ArrayConstructor::create(vm, JSFloat32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat32)].prototype.get(), "Float32Array");
424 typedArrayConstructors[toIndex(TypeFloat64)] = JSFloat64ArrayConstructor::create(vm, JSFloat64ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat64)].prototype.get(), "Float64Array");
425 typedArrayConstructors[toIndex(TypeDataView)] = JSDataViewConstructor::create(vm, JSDataViewConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeDataView)].prototype.get(), "DataView");
427 for (unsigned typedArrayIndex = NUMBER_OF_TYPED_ARRAY_TYPES; typedArrayIndex--;) {
428 m_typedArrays[typedArrayIndex].prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, typedArrayConstructors[typedArrayIndex], DontEnum);
429 putDirectWithoutTransition(vm, Identifier(exec, typedArrayConstructors[typedArrayIndex]->name(exec)), typedArrayConstructors[typedArrayIndex], DontEnum);
432 GlobalPropertyInfo staticGlobals[] = {
433 GlobalPropertyInfo(vm.propertyNames->NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
434 GlobalPropertyInfo(vm.propertyNames->Infinity, jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
435 GlobalPropertyInfo(vm.propertyNames->undefinedKeyword, jsUndefined(), DontEnum | DontDelete | ReadOnly)
437 addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
439 m_specialPointers[Special::CallFunction] = m_callFunction.get();
440 m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
441 m_specialPointers[Special::ObjectConstructor] = objectConstructor;
442 m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
444 if (m_experimentsEnabled) {
445 NamePrototype* privateNamePrototype = NamePrototype::create(exec, NamePrototype::createStructure(vm, this, m_objectPrototype.get()));
446 m_privateNameStructure.set(vm, this, NameInstance::createStructure(vm, this, privateNamePrototype));
448 JSCell* privateNameConstructor = NameConstructor::create(vm, NameConstructor::createStructure(vm, this, m_functionPrototype.get()), privateNamePrototype);
449 privateNamePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, privateNameConstructor, DontEnum);
450 putDirectWithoutTransition(vm, Identifier(exec, "Name"), privateNameConstructor, DontEnum);
453 resetPrototype(vm, prototype);
456 // Private namespace for helpers for JSGlobalObject::haveABadTime()
459 class ObjectsWithBrokenIndexingFinder : public MarkedBlock::VoidFunctor {
461 ObjectsWithBrokenIndexingFinder(MarkedArgumentBuffer&, JSGlobalObject*);
462 void operator()(JSCell*);
465 MarkedArgumentBuffer& m_foundObjects;
466 JSGlobalObject* m_globalObject;
469 ObjectsWithBrokenIndexingFinder::ObjectsWithBrokenIndexingFinder(
470 MarkedArgumentBuffer& foundObjects, JSGlobalObject* globalObject)
471 : m_foundObjects(foundObjects)
472 , m_globalObject(globalObject)
476 inline bool hasBrokenIndexing(JSObject* object)
478 // This will change if we have more indexing types.
479 IndexingType type = object->structure()->indexingType();
480 // This could be made obviously more efficient, but isn't made so right now, because
481 // we expect this to be an unlikely slow path anyway.
482 return hasUndecided(type) || hasInt32(type) || hasDouble(type) || hasContiguous(type) || hasFastArrayStorage(type);
485 void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
487 if (!cell->isObject())
490 JSObject* object = asObject(cell);
492 // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
493 if (!hasBrokenIndexing(object))
496 // We only want to have a bad time in the affected global object, not in the entire
497 // VM. But we have to be careful, since there may be objects that claim to belong to
498 // a different global object that have prototypes from our global object.
499 bool foundGlobalObject = false;
500 for (JSObject* current = object; ;) {
501 if (current->globalObject() == m_globalObject) {
502 foundGlobalObject = true;
506 JSValue prototypeValue = current->prototype();
507 if (prototypeValue.isNull())
509 current = asObject(prototypeValue);
511 if (!foundGlobalObject)
514 m_foundObjects.append(object);
517 } // end private namespace for helpers for JSGlobalObject::haveABadTime()
519 void JSGlobalObject::haveABadTime(VM& vm)
521 ASSERT(&vm == &this->vm());
523 if (isHavingABadTime())
526 // Make sure that all allocations or indexed storage transitions that are inlining
527 // the assumption that it's safe to transition to a non-SlowPut array storage don't
529 m_havingABadTimeWatchpoint->fireAll();
530 ASSERT(isHavingABadTime()); // The watchpoint is what tells us that we're having a bad time.
532 // Make sure that all JSArray allocations that load the appropriate structure from
533 // this object now load a structure that uses SlowPut.
534 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
535 m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
537 // Make sure that all objects that have indexed storage switch to the slow kind of
539 MarkedArgumentBuffer foundObjects; // Use MarkedArgumentBuffer because switchToSlowPutArrayStorage() may GC.
540 ObjectsWithBrokenIndexingFinder finder(foundObjects, this);
542 HeapIterationScope iterationScope(vm.heap);
543 vm.heap.objectSpace().forEachLiveCell(iterationScope, finder);
545 while (!foundObjects.isEmpty()) {
546 JSObject* object = asObject(foundObjects.last());
547 foundObjects.removeLast();
548 ASSERT(hasBrokenIndexing(object));
549 object->switchToSlowPutArrayStorage(vm);
553 bool JSGlobalObject::objectPrototypeIsSane()
555 return !hasIndexedProperties(m_objectPrototype->structure()->indexingType())
556 && m_objectPrototype->prototype().isNull();
559 bool JSGlobalObject::arrayPrototypeChainIsSane()
561 return !hasIndexedProperties(m_arrayPrototype->structure()->indexingType())
562 && m_arrayPrototype->prototype() == m_objectPrototype.get()
563 && objectPrototypeIsSane();
566 bool JSGlobalObject::stringPrototypeChainIsSane()
568 return !hasIndexedProperties(m_stringPrototype->structure()->indexingType())
569 && m_stringPrototype->prototype() == m_objectPrototype.get()
570 && objectPrototypeIsSane();
573 void JSGlobalObject::createThrowTypeError(VM& vm)
575 JSFunction* thrower = JSFunction::create(vm, this, 0, String(), globalFuncThrowTypeError);
576 GetterSetter* getterSetter = GetterSetter::create(vm);
577 getterSetter->setGetter(vm, thrower);
578 getterSetter->setSetter(vm, thrower);
579 m_throwTypeErrorGetterSetter.set(vm, this, getterSetter);
582 // Set prototype, and also insert the object prototype at the end of the chain.
583 void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
585 setPrototype(vm, prototype);
587 JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
588 JSObject* objectPrototype = m_objectPrototype.get();
589 if (oldLastInPrototypeChain != objectPrototype)
590 oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
593 void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
595 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
596 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
597 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
598 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
599 Base::visitChildren(thisObject, visitor);
601 visitor.append(&thisObject->m_globalThis);
603 visitor.append(&thisObject->m_regExpConstructor);
604 visitor.append(&thisObject->m_errorConstructor);
605 visitor.append(&thisObject->m_evalErrorConstructor);
606 visitor.append(&thisObject->m_rangeErrorConstructor);
607 visitor.append(&thisObject->m_referenceErrorConstructor);
608 visitor.append(&thisObject->m_syntaxErrorConstructor);
609 visitor.append(&thisObject->m_typeErrorConstructor);
610 visitor.append(&thisObject->m_URIErrorConstructor);
611 visitor.append(&thisObject->m_promiseConstructor);
613 visitor.append(&thisObject->m_evalFunction);
614 visitor.append(&thisObject->m_callFunction);
615 visitor.append(&thisObject->m_applyFunction);
616 visitor.append(&thisObject->m_throwTypeErrorGetterSetter);
618 visitor.append(&thisObject->m_objectPrototype);
619 visitor.append(&thisObject->m_functionPrototype);
620 visitor.append(&thisObject->m_arrayPrototype);
621 visitor.append(&thisObject->m_errorPrototype);
623 visitor.append(&thisObject->m_promisePrototype);
626 visitor.append(&thisObject->m_withScopeStructure);
627 visitor.append(&thisObject->m_strictEvalActivationStructure);
628 visitor.append(&thisObject->m_activationStructure);
629 visitor.append(&thisObject->m_nameScopeStructure);
630 visitor.append(&thisObject->m_argumentsStructure);
631 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
632 visitor.append(&thisObject->m_originalArrayStructureForIndexingShape[i]);
633 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
634 visitor.append(&thisObject->m_arrayStructureForIndexingShapeDuringAllocation[i]);
635 visitor.append(&thisObject->m_booleanObjectStructure);
636 visitor.append(&thisObject->m_callbackConstructorStructure);
637 visitor.append(&thisObject->m_callbackFunctionStructure);
638 visitor.append(&thisObject->m_callbackObjectStructure);
639 #if JSC_OBJC_API_ENABLED
640 visitor.append(&thisObject->m_objcCallbackFunctionStructure);
641 visitor.append(&thisObject->m_objcWrapperObjectStructure);
643 visitor.append(&thisObject->m_nullPrototypeObjectStructure);
644 visitor.append(&thisObject->m_errorStructure);
645 visitor.append(&thisObject->m_functionStructure);
646 visitor.append(&thisObject->m_boundFunctionStructure);
647 visitor.append(&thisObject->m_namedFunctionStructure);
648 visitor.append(&thisObject->m_privateNameStructure);
649 visitor.append(&thisObject->m_regExpMatchesArrayStructure);
650 visitor.append(&thisObject->m_regExpStructure);
651 visitor.append(&thisObject->m_internalFunctionStructure);
654 visitor.append(&thisObject->m_promiseStructure);
655 #endif // ENABLE(PROMISES)
657 #define VISIT_SIMPLE_TYPE(CapitalName, lowerName, properName, instanceType, jsName) \
658 visitor.append(&thisObject->m_ ## lowerName ## Prototype); \
659 visitor.append(&thisObject->m_ ## properName ## Structure); \
661 FOR_EACH_SIMPLE_BUILTIN_TYPE(VISIT_SIMPLE_TYPE)
663 #undef VISIT_SIMPLE_TYPE
665 for (unsigned i = NUMBER_OF_TYPED_ARRAY_TYPES; i--;) {
666 visitor.append(&thisObject->m_typedArrays[i].prototype);
667 visitor.append(&thisObject->m_typedArrays[i].structure);
671 JSValue JSGlobalObject::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
673 if (ecmaMode == StrictMode)
674 return jsUndefined();
675 return exec->globalThisValue();
678 ExecState* JSGlobalObject::globalExec()
680 return CallFrame::create(m_globalCallFrame);
683 void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
687 for (int i = 0; i < count; ++i) {
688 GlobalPropertyInfo& global = globals[i];
689 ASSERT(global.attributes & DontDelete);
691 int index = symbolTable()->size();
692 SymbolTableEntry newEntry(index, global.attributes);
693 symbolTable()->add(global.identifier.impl(), newEntry);
694 registerAt(index).set(vm(), this, global.value);
698 bool JSGlobalObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
700 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
701 if (getStaticFunctionSlot<Base>(exec, ExecState::globalObjectTable(exec->vm()), thisObject, propertyName, slot))
703 return symbolTableGet(thisObject, propertyName, slot);
706 void JSGlobalObject::clearRareData(JSCell* cell)
708 jsCast<JSGlobalObject*>(cell)->m_rareData.clear();
711 void slowValidateCell(JSGlobalObject* globalObject)
713 RELEASE_ASSERT(globalObject->isGlobalObject());
714 ASSERT_GC_OBJECT_INHERITS(globalObject, JSGlobalObject::info());
717 UnlinkedProgramCodeBlock* JSGlobalObject::createProgramCodeBlock(CallFrame* callFrame, ProgramExecutable* executable, JSObject** exception)
720 JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
721 DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
722 ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
723 UnlinkedProgramCodeBlock* unlinkedCodeBlock = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
726 debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
728 if (error.m_type != ParserError::ErrorNone) {
729 *exception = error.toErrorObject(this, executable->source());
733 return unlinkedCodeBlock;
736 UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CallFrame* callFrame, EvalExecutable* executable)
739 JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
740 DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
741 ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
742 UnlinkedEvalCodeBlock* unlinkedCodeBlock = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
745 debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
747 if (error.m_type != ParserError::ErrorNone) {
748 throwVMError(callFrame, error.toErrorObject(this, executable->source()));
752 return unlinkedCodeBlock;
755 void JSGlobalObject::setRemoteDebuggingEnabled(bool enabled)
757 #if ENABLE(REMOTE_INSPECTOR)
758 m_inspectorDebuggable->setRemoteDebuggingAllowed(enabled);
760 UNUSED_PARAM(enabled);
764 bool JSGlobalObject::remoteDebuggingEnabled() const
766 #if ENABLE(REMOTE_INSPECTOR)
767 return m_inspectorDebuggable->remoteDebuggingAllowed();
773 void JSGlobalObject::setName(const String& name)
777 #if ENABLE(REMOTE_INSPECTOR)
778 m_inspectorDebuggable->update();
782 void JSGlobalObject::queueMicrotask(PassRefPtr<Microtask> task)
784 if (globalObjectMethodTable()->queueTaskToEventLoop)
785 globalObjectMethodTable()->queueTaskToEventLoop(this, task);
787 WTFLogAlways("ERROR: Event loop not supported.");