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 "ArrayConstructor.h"
35 #include "ArrayPrototype.h"
36 #include "BooleanConstructor.h"
37 #include "BooleanPrototype.h"
38 #include "CodeBlock.h"
39 #include "CodeCache.h"
40 #include "DateConstructor.h"
41 #include "DatePrototype.h"
44 #include "ErrorConstructor.h"
45 #include "ErrorPrototype.h"
46 #include "FunctionConstructor.h"
47 #include "FunctionPrototype.h"
48 #include "GetterSetter.h"
49 #include "HeapIterationScope.h"
50 #include "Interpreter.h"
51 #include "JSAPIWrapperObject.h"
52 #include "JSActivation.h"
53 #include "JSArrayBuffer.h"
54 #include "JSArrayBufferConstructor.h"
55 #include "JSArrayBufferPrototype.h"
56 #include "JSBoundFunction.h"
57 #include "JSCallbackConstructor.h"
58 #include "JSCallbackFunction.h"
59 #include "JSCallbackObject.h"
60 #include "JSDataView.h"
61 #include "JSDataViewPrototype.h"
62 #include "JSFunction.h"
63 #include "JSGenericTypedArrayViewConstructorInlines.h"
64 #include "JSGenericTypedArrayViewInlines.h"
65 #include "JSGenericTypedArrayViewPrototypeInlines.h"
66 #include "JSGlobalObjectFunctions.h"
69 #include "JSNameScope.h"
70 #include "JSONObject.h"
72 #include "JSTypedArrayConstructors.h"
73 #include "JSTypedArrayPrototypes.h"
74 #include "JSTypedArrays.h"
75 #include "JSWeakMap.h"
76 #include "JSWithScope.h"
77 #include "LegacyProfiler.h"
79 #include "MapConstructor.h"
80 #include "MapPrototype.h"
81 #include "MathObject.h"
82 #include "NameConstructor.h"
83 #include "NameInstance.h"
84 #include "NamePrototype.h"
85 #include "NativeErrorConstructor.h"
86 #include "NativeErrorPrototype.h"
87 #include "NumberConstructor.h"
88 #include "NumberPrototype.h"
89 #include "ObjCCallbackFunction.h"
90 #include "ObjectConstructor.h"
91 #include "ObjectPrototype.h"
92 #include "Operations.h"
93 #include "ParserError.h"
94 #include "RegExpConstructor.h"
95 #include "RegExpMatchesArray.h"
96 #include "RegExpObject.h"
97 #include "RegExpPrototype.h"
98 #include "SetConstructor.h"
99 #include "SetPrototype.h"
100 #include "StrictEvalActivation.h"
101 #include "StringConstructor.h"
102 #include "StringPrototype.h"
103 #include "WeakMapConstructor.h"
104 #include "WeakMapPrototype.h"
107 #include "JSPromise.h"
108 #include "JSPromiseCallback.h"
109 #include "JSPromiseConstructor.h"
110 #include "JSPromisePrototype.h"
111 #include "JSPromiseResolver.h"
112 #include "JSPromiseResolverConstructor.h"
113 #include "JSPromiseResolverPrototype.h"
114 #endif // ENABLE(PROMISES)
116 #include "JSGlobalObject.lut.h"
120 const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
122 const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled, 0 };
124 /* Source for JSGlobalObject.lut.h
125 @begin globalObjectTable
126 parseInt globalFuncParseInt DontEnum|Function 2
127 parseFloat globalFuncParseFloat DontEnum|Function 1
128 isNaN globalFuncIsNaN DontEnum|Function 1
129 isFinite globalFuncIsFinite DontEnum|Function 1
130 escape globalFuncEscape DontEnum|Function 1
131 unescape globalFuncUnescape DontEnum|Function 1
132 decodeURI globalFuncDecodeURI DontEnum|Function 1
133 decodeURIComponent globalFuncDecodeURIComponent DontEnum|Function 1
134 encodeURI globalFuncEncodeURI DontEnum|Function 1
135 encodeURIComponent globalFuncEncodeURIComponent DontEnum|Function 1
139 JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
140 : Base(vm, structure, 0)
141 , m_masqueradesAsUndefinedWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
142 , m_havingABadTimeWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
143 , m_varInjectionWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
144 , m_weakRandom(Options::forceWeakRandomSeed() ? Options::forcedWeakRandomSeed() : static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
145 , m_evalEnabled(true)
146 , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
150 JSGlobalObject::~JSGlobalObject()
153 m_debugger->detach(this);
155 if (LegacyProfiler* profiler = vm().enabledProfiler())
156 profiler->stopProfiling(this);
159 void JSGlobalObject::destroy(JSCell* cell)
161 static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
164 void JSGlobalObject::setGlobalThis(VM& vm, JSObject* globalThis)
166 m_globalThis.set(vm, this, globalThis);
169 void JSGlobalObject::init(JSObject* thisValue)
171 ASSERT(vm().currentThreadIsHoldingAPILock());
173 setGlobalThis(vm(), thisValue);
174 JSGlobalObject::globalExec()->init(0, 0, this, CallFrame::noCaller(), 0, 0);
181 void JSGlobalObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
183 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
184 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
186 if (symbolTablePut(thisObject, exec, propertyName, value, slot.isStrictMode()))
188 Base::put(thisObject, exec, propertyName, value, slot);
191 bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
193 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
194 PropertySlot slot(thisObject);
195 // silently ignore attempts to add accessors aliasing vars.
196 if (descriptor.isAccessorDescriptor() && symbolTableGet(thisObject, propertyName, slot))
198 return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
201 int JSGlobalObject::addGlobalVar(const Identifier& ident, ConstantMode constantMode, FunctionMode functionMode)
203 ConcurrentJITLocker locker(symbolTable()->m_lock);
204 int index = symbolTable()->size(locker);
205 SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
206 if (functionMode == IsFunctionToSpecialize)
207 newEntry.attemptToWatch();
208 SymbolTable::Map::AddResult result = symbolTable()->add(locker, ident.impl(), newEntry);
209 if (result.isNewEntry)
212 result.iterator->value.notifyWrite();
213 index = result.iterator->value.getIndex();
218 static inline JSObject* lastInPrototypeChain(JSObject* object)
220 JSObject* o = object;
221 while (o->prototype().isObject())
222 o = asObject(o->prototype());
226 void JSGlobalObject::reset(JSValue prototype)
228 ExecState* exec = JSGlobalObject::globalExec();
231 m_functionPrototype.set(vm, this, FunctionPrototype::create(exec, this, FunctionPrototype::createStructure(vm, this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
232 m_functionStructure.set(vm, this, JSFunction::createStructure(vm, this, m_functionPrototype.get()));
233 m_boundFunctionStructure.set(vm, this, JSBoundFunction::createStructure(vm, this, m_functionPrototype.get()));
234 m_namedFunctionStructure.set(vm, this, Structure::addPropertyTransition(vm, m_functionStructure.get(), vm.propertyNames->name, DontDelete | ReadOnly | DontEnum, 0, m_functionNameOffset));
235 m_internalFunctionStructure.set(vm, this, InternalFunction::createStructure(vm, this, m_functionPrototype.get()));
236 JSFunction* callFunction = 0;
237 JSFunction* applyFunction = 0;
238 m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
239 m_callFunction.set(vm, this, callFunction);
240 m_applyFunction.set(vm, this, applyFunction);
241 m_objectPrototype.set(vm, this, ObjectPrototype::create(exec, this, ObjectPrototype::createStructure(vm, this, jsNull())));
242 GetterSetter* protoAccessor = GetterSetter::create(vm);
243 protoAccessor->setGetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoGetter));
244 protoAccessor->setSetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoSetter));
245 m_objectPrototype->putDirectNonIndexAccessor(vm, vm.propertyNames->underscoreProto, protoAccessor, Accessor | DontEnum);
246 m_functionPrototype->structure()->setPrototypeWithoutTransition(vm, m_objectPrototype.get());
248 m_typedArrays[toIndex(TypeInt8)].prototype.set(vm, this, JSInt8ArrayPrototype::create(exec, this, JSInt8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
249 m_typedArrays[toIndex(TypeInt16)].prototype.set(vm, this, JSInt16ArrayPrototype::create(exec, this, JSInt16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
250 m_typedArrays[toIndex(TypeInt32)].prototype.set(vm, this, JSInt32ArrayPrototype::create(exec, this, JSInt32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
251 m_typedArrays[toIndex(TypeUint8)].prototype.set(vm, this, JSUint8ArrayPrototype::create(exec, this, JSUint8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
252 m_typedArrays[toIndex(TypeUint8Clamped)].prototype.set(vm, this, JSUint8ClampedArrayPrototype::create(exec, this, JSUint8ClampedArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
253 m_typedArrays[toIndex(TypeUint16)].prototype.set(vm, this, JSUint16ArrayPrototype::create(exec, this, JSUint16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
254 m_typedArrays[toIndex(TypeUint32)].prototype.set(vm, this, JSUint32ArrayPrototype::create(exec, this, JSUint32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
255 m_typedArrays[toIndex(TypeFloat32)].prototype.set(vm, this, JSFloat32ArrayPrototype::create(exec, this, JSFloat32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
256 m_typedArrays[toIndex(TypeFloat64)].prototype.set(vm, this, JSFloat64ArrayPrototype::create(exec, this, JSFloat64ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
257 m_typedArrays[toIndex(TypeDataView)].prototype.set(vm, this, JSDataViewPrototype::create(vm, JSDataViewPrototype::createStructure(vm, this, m_objectPrototype.get())));
259 m_typedArrays[toIndex(TypeInt8)].structure.set(vm, this, JSInt8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt8)].prototype.get()));
260 m_typedArrays[toIndex(TypeInt16)].structure.set(vm, this, JSInt16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt16)].prototype.get()));
261 m_typedArrays[toIndex(TypeInt32)].structure.set(vm, this, JSInt32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt32)].prototype.get()));
262 m_typedArrays[toIndex(TypeUint8)].structure.set(vm, this, JSUint8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8)].prototype.get()));
263 m_typedArrays[toIndex(TypeUint8Clamped)].structure.set(vm, this, JSUint8ClampedArray::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get()));
264 m_typedArrays[toIndex(TypeUint16)].structure.set(vm, this, JSUint16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint16)].prototype.get()));
265 m_typedArrays[toIndex(TypeUint32)].structure.set(vm, this, JSUint32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint32)].prototype.get()));
266 m_typedArrays[toIndex(TypeFloat32)].structure.set(vm, this, JSFloat32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat32)].prototype.get()));
267 m_typedArrays[toIndex(TypeFloat64)].structure.set(vm, this, JSFloat64Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat64)].prototype.get()));
268 m_typedArrays[toIndex(TypeDataView)].structure.set(vm, this, JSDataView::createStructure(vm, this, m_typedArrays[toIndex(TypeDataView)].prototype.get()));
270 m_nameScopeStructure.set(vm, this, JSNameScope::createStructure(vm, this, jsNull()));
271 m_activationStructure.set(vm, this, JSActivation::createStructure(vm, this, jsNull()));
272 m_strictEvalActivationStructure.set(vm, this, StrictEvalActivation::createStructure(vm, this, jsNull()));
273 m_withScopeStructure.set(vm, this, JSWithScope::createStructure(vm, this, jsNull()));
275 m_nullPrototypeObjectStructure.set(vm, this, JSFinalObject::createStructure(vm, this, jsNull(), JSFinalObject::defaultInlineCapacity()));
277 m_callbackFunctionStructure.set(vm, this, JSCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
278 m_argumentsStructure.set(vm, this, Arguments::createStructure(vm, this, m_objectPrototype.get()));
279 m_callbackConstructorStructure.set(vm, this, JSCallbackConstructor::createStructure(vm, this, m_objectPrototype.get()));
280 m_callbackObjectStructure.set(vm, this, JSCallbackObject<JSDestructibleObject>::createStructure(vm, this, m_objectPrototype.get()));
281 #if JSC_OBJC_API_ENABLED
282 m_objcCallbackFunctionStructure.set(vm, this, ObjCCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
283 m_objcWrapperObjectStructure.set(vm, this, JSCallbackObject<JSAPIWrapperObject>::createStructure(vm, this, m_objectPrototype.get()));
286 m_arrayPrototype.set(vm, this, ArrayPrototype::create(vm, ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
288 m_originalArrayStructureForIndexingShape[UndecidedShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithUndecided));
289 m_originalArrayStructureForIndexingShape[Int32Shape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithInt32));
290 m_originalArrayStructureForIndexingShape[DoubleShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithDouble));
291 m_originalArrayStructureForIndexingShape[ContiguousShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithContiguous));
292 m_originalArrayStructureForIndexingShape[ArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithArrayStorage));
293 m_originalArrayStructureForIndexingShape[SlowPutArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithSlowPutArrayStorage));
294 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
295 m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
297 m_regExpMatchesArrayStructure.set(vm, this, RegExpMatchesArray::createStructure(vm, this, m_arrayPrototype.get()));
299 RegExp* emptyRegex = RegExp::create(vm, "", NoFlags);
301 m_regExpPrototype.set(vm, this, RegExpPrototype::create(exec, this, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()), emptyRegex));
302 m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get()));
305 m_promisePrototype.set(vm, this, JSPromisePrototype::create(exec, this, JSPromisePrototype::createStructure(vm, this, m_objectPrototype.get())));
306 m_promiseStructure.set(vm, this, JSPromise::createStructure(vm, this, m_promisePrototype.get()));
308 m_promiseResolverPrototype.set(vm, this, JSPromiseResolverPrototype::create(exec, this, JSPromiseResolverPrototype::createStructure(vm, this, m_objectPrototype.get())));
309 m_promiseResolverStructure.set(vm, this, JSPromiseResolver::createStructure(vm, this, m_promiseResolverPrototype.get()));
310 m_promiseCallbackStructure.set(vm, this, JSPromiseCallback::createStructure(vm, this, m_functionPrototype.get()));
311 m_promiseWrapperCallbackStructure.set(vm, this, JSPromiseWrapperCallback::createStructure(vm, this, m_functionPrototype.get()));
312 #endif // ENABLE(PROMISES)
314 #define CREATE_PROTOTYPE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
315 m_ ## lowerName ## Prototype.set(vm, this, capitalName##Prototype::create(vm, this, capitalName##Prototype::createStructure(vm, this, m_objectPrototype.get()))); \
316 m_ ## properName ## Structure.set(vm, this, instanceType::createStructure(vm, this, m_ ## lowerName ## Prototype.get()));
318 FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_PROTOTYPE_FOR_SIMPLE_TYPE)
320 #undef CREATE_PROTOTYPE_FOR_SIMPLE_TYPE
324 JSCell* objectConstructor = ObjectConstructor::create(exec, this, ObjectConstructor::createStructure(vm, this, m_functionPrototype.get()), m_objectPrototype.get());
325 JSCell* functionConstructor = FunctionConstructor::create(exec, this, FunctionConstructor::createStructure(vm, this, m_functionPrototype.get()), m_functionPrototype.get());
326 JSCell* arrayConstructor = ArrayConstructor::create(exec, this, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get());
329 JSCell* promiseConstructor = JSPromiseConstructor::create(exec, this, JSPromiseConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promisePrototype.get());
330 JSCell* promiseResolverConstructor = JSPromiseResolverConstructor::create(exec, this, JSPromiseResolverConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promiseResolverPrototype.get());
331 #endif // ENABLE(PROMISES)
333 m_regExpConstructor.set(vm, this, RegExpConstructor::create(exec, this, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get()));
335 #define CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
336 capitalName ## Constructor* lowerName ## Constructor = capitalName ## Constructor::create(exec, this, capitalName ## Constructor::createStructure(vm, this, m_functionPrototype.get()), m_ ## lowerName ## Prototype.get()); \
337 m_ ## lowerName ## Prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, lowerName ## Constructor, DontEnum); \
339 FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE)
341 #undef CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE
343 m_errorConstructor.set(vm, this, errorConstructor);
345 Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(vm, this, m_errorPrototype.get());
346 Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(vm, this, m_functionPrototype.get());
347 m_evalErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("EvalError")));
348 m_rangeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("RangeError")));
349 m_referenceErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("ReferenceError")));
350 m_syntaxErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("SyntaxError")));
351 m_typeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("TypeError")));
352 m_URIErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("URIError")));
354 m_objectPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, objectConstructor, DontEnum);
355 m_functionPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, functionConstructor, DontEnum);
356 m_arrayPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, arrayConstructor, DontEnum);
357 m_regExpPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_regExpConstructor.get(), DontEnum);
359 m_promisePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseConstructor, DontEnum);
360 m_promiseResolverPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseResolverConstructor, DontEnum);
363 putDirectWithoutTransition(vm, vm.propertyNames->Object, objectConstructor, DontEnum);
364 putDirectWithoutTransition(vm, vm.propertyNames->Function, functionConstructor, DontEnum);
365 putDirectWithoutTransition(vm, vm.propertyNames->Array, arrayConstructor, DontEnum);
366 putDirectWithoutTransition(vm, vm.propertyNames->RegExp, m_regExpConstructor.get(), DontEnum);
367 putDirectWithoutTransition(vm, vm.propertyNames->EvalError, m_evalErrorConstructor.get(), DontEnum);
368 putDirectWithoutTransition(vm, vm.propertyNames->RangeError, m_rangeErrorConstructor.get(), DontEnum);
369 putDirectWithoutTransition(vm, vm.propertyNames->ReferenceError, m_referenceErrorConstructor.get(), DontEnum);
370 putDirectWithoutTransition(vm, vm.propertyNames->SyntaxError, m_syntaxErrorConstructor.get(), DontEnum);
371 putDirectWithoutTransition(vm, vm.propertyNames->TypeError, m_typeErrorConstructor.get(), DontEnum);
372 putDirectWithoutTransition(vm, vm.propertyNames->URIError, m_URIErrorConstructor.get(), DontEnum);
374 putDirectWithoutTransition(vm, vm.propertyNames->Promise, promiseConstructor, DontEnum);
375 putDirectWithoutTransition(vm, vm.propertyNames->PromiseResolver, promiseResolverConstructor, DontEnum);
379 #define PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
380 putDirectWithoutTransition(vm, vm.propertyNames-> jsName, lowerName ## Constructor, DontEnum); \
382 FOR_EACH_SIMPLE_BUILTIN_TYPE(PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE)
384 #undef PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE
387 m_evalFunction.set(vm, this, JSFunction::create(vm, this, 1, vm.propertyNames->eval.string(), globalFuncEval));
388 putDirectWithoutTransition(vm, vm.propertyNames->eval, m_evalFunction.get(), DontEnum);
390 putDirectWithoutTransition(vm, vm.propertyNames->JSON, JSONObject::create(exec, this, JSONObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
391 putDirectWithoutTransition(vm, vm.propertyNames->Math, MathObject::create(exec, this, MathObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
393 FixedArray<InternalFunction*, NUMBER_OF_TYPED_ARRAY_TYPES> typedArrayConstructors;
394 typedArrayConstructors[toIndex(TypeInt8)] = JSInt8ArrayConstructor::create(this, JSInt8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt8)].prototype.get(), "Int8Array");
395 typedArrayConstructors[toIndex(TypeInt16)] = JSInt16ArrayConstructor::create(this, JSInt16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt16)].prototype.get(), "Int16Array");
396 typedArrayConstructors[toIndex(TypeInt32)] = JSInt32ArrayConstructor::create(this, JSInt32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt32)].prototype.get(), "Int32Array");
397 typedArrayConstructors[toIndex(TypeUint8)] = JSUint8ArrayConstructor::create(this, JSUint8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8)].prototype.get(), "Uint8Array");
398 typedArrayConstructors[toIndex(TypeUint8Clamped)] = JSUint8ClampedArrayConstructor::create(this, JSUint8ClampedArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get(), "Uint8ClampedArray");
399 typedArrayConstructors[toIndex(TypeUint16)] = JSUint16ArrayConstructor::create(this, JSUint16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint16)].prototype.get(), "Uint16Array");
400 typedArrayConstructors[toIndex(TypeUint32)] = JSUint32ArrayConstructor::create(this, JSUint32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint32)].prototype.get(), "Uint32Array");
401 typedArrayConstructors[toIndex(TypeFloat32)] = JSFloat32ArrayConstructor::create(this, JSFloat32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat32)].prototype.get(), "Float32Array");
402 typedArrayConstructors[toIndex(TypeFloat64)] = JSFloat64ArrayConstructor::create(this, JSFloat64ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat64)].prototype.get(), "Float64Array");
403 typedArrayConstructors[toIndex(TypeDataView)] = JSDataViewConstructor::create(this, JSDataViewConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeDataView)].prototype.get(), "DataView");
405 for (unsigned typedArrayIndex = NUMBER_OF_TYPED_ARRAY_TYPES; typedArrayIndex--;) {
406 m_typedArrays[typedArrayIndex].prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, typedArrayConstructors[typedArrayIndex], DontEnum);
407 putDirectWithoutTransition(vm, Identifier(exec, typedArrayConstructors[typedArrayIndex]->name(exec)), typedArrayConstructors[typedArrayIndex], DontEnum);
410 GlobalPropertyInfo staticGlobals[] = {
411 GlobalPropertyInfo(vm.propertyNames->NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
412 GlobalPropertyInfo(vm.propertyNames->Infinity, jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
413 GlobalPropertyInfo(vm.propertyNames->undefinedKeyword, jsUndefined(), DontEnum | DontDelete | ReadOnly)
415 addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
417 m_specialPointers[Special::CallFunction] = m_callFunction.get();
418 m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
419 m_specialPointers[Special::ObjectConstructor] = objectConstructor;
420 m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
422 if (m_experimentsEnabled) {
423 NamePrototype* privateNamePrototype = NamePrototype::create(exec, NamePrototype::createStructure(vm, this, m_objectPrototype.get()));
424 m_privateNameStructure.set(vm, this, NameInstance::createStructure(vm, this, privateNamePrototype));
426 JSCell* privateNameConstructor = NameConstructor::create(exec, this, NameConstructor::createStructure(vm, this, m_functionPrototype.get()), privateNamePrototype);
427 privateNamePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, privateNameConstructor, DontEnum);
428 putDirectWithoutTransition(vm, Identifier(exec, "Name"), privateNameConstructor, DontEnum);
431 resetPrototype(vm, prototype);
434 // Private namespace for helpers for JSGlobalObject::haveABadTime()
437 class ObjectsWithBrokenIndexingFinder : public MarkedBlock::VoidFunctor {
439 ObjectsWithBrokenIndexingFinder(MarkedArgumentBuffer&, JSGlobalObject*);
440 void operator()(JSCell*);
443 MarkedArgumentBuffer& m_foundObjects;
444 JSGlobalObject* m_globalObject;
447 ObjectsWithBrokenIndexingFinder::ObjectsWithBrokenIndexingFinder(
448 MarkedArgumentBuffer& foundObjects, JSGlobalObject* globalObject)
449 : m_foundObjects(foundObjects)
450 , m_globalObject(globalObject)
454 inline bool hasBrokenIndexing(JSObject* object)
456 // This will change if we have more indexing types.
457 IndexingType type = object->structure()->indexingType();
458 // This could be made obviously more efficient, but isn't made so right now, because
459 // we expect this to be an unlikely slow path anyway.
460 return hasUndecided(type) || hasInt32(type) || hasDouble(type) || hasContiguous(type) || hasFastArrayStorage(type);
463 void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
465 if (!cell->isObject())
468 JSObject* object = asObject(cell);
470 // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
471 if (!hasBrokenIndexing(object))
474 // We only want to have a bad time in the affected global object, not in the entire
475 // VM. But we have to be careful, since there may be objects that claim to belong to
476 // a different global object that have prototypes from our global object.
477 bool foundGlobalObject = false;
478 for (JSObject* current = object; ;) {
479 if (current->globalObject() == m_globalObject) {
480 foundGlobalObject = true;
484 JSValue prototypeValue = current->prototype();
485 if (prototypeValue.isNull())
487 current = asObject(prototypeValue);
489 if (!foundGlobalObject)
492 m_foundObjects.append(object);
495 } // end private namespace for helpers for JSGlobalObject::haveABadTime()
497 void JSGlobalObject::haveABadTime(VM& vm)
499 ASSERT(&vm == &this->vm());
501 if (isHavingABadTime())
504 // Make sure that all allocations or indexed storage transitions that are inlining
505 // the assumption that it's safe to transition to a non-SlowPut array storage don't
507 m_havingABadTimeWatchpoint->notifyWrite();
508 ASSERT(isHavingABadTime()); // The watchpoint is what tells us that we're having a bad time.
510 // Make sure that all JSArray allocations that load the appropriate structure from
511 // this object now load a structure that uses SlowPut.
512 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
513 m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
515 // Make sure that all objects that have indexed storage switch to the slow kind of
517 MarkedArgumentBuffer foundObjects; // Use MarkedArgumentBuffer because switchToSlowPutArrayStorage() may GC.
518 ObjectsWithBrokenIndexingFinder finder(foundObjects, this);
520 HeapIterationScope iterationScope(vm.heap);
521 vm.heap.objectSpace().forEachLiveCell(iterationScope, finder);
523 while (!foundObjects.isEmpty()) {
524 JSObject* object = asObject(foundObjects.last());
525 foundObjects.removeLast();
526 ASSERT(hasBrokenIndexing(object));
527 object->switchToSlowPutArrayStorage(vm);
531 bool JSGlobalObject::objectPrototypeIsSane()
533 return !hasIndexedProperties(m_objectPrototype->structure()->indexingType())
534 && m_objectPrototype->prototype().isNull();
537 bool JSGlobalObject::arrayPrototypeChainIsSane()
539 return !hasIndexedProperties(m_arrayPrototype->structure()->indexingType())
540 && m_arrayPrototype->prototype() == m_objectPrototype.get()
541 && objectPrototypeIsSane();
544 bool JSGlobalObject::stringPrototypeChainIsSane()
546 return !hasIndexedProperties(m_stringPrototype->structure()->indexingType())
547 && m_stringPrototype->prototype() == m_objectPrototype.get()
548 && objectPrototypeIsSane();
551 void JSGlobalObject::createThrowTypeError(VM& vm)
553 JSFunction* thrower = JSFunction::create(vm, this, 0, String(), globalFuncThrowTypeError);
554 GetterSetter* getterSetter = GetterSetter::create(vm);
555 getterSetter->setGetter(vm, thrower);
556 getterSetter->setSetter(vm, thrower);
557 m_throwTypeErrorGetterSetter.set(vm, this, getterSetter);
560 // Set prototype, and also insert the object prototype at the end of the chain.
561 void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
563 setPrototype(vm, prototype);
565 JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
566 JSObject* objectPrototype = m_objectPrototype.get();
567 if (oldLastInPrototypeChain != objectPrototype)
568 oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
571 void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
573 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
574 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
575 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
576 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
577 Base::visitChildren(thisObject, visitor);
579 visitor.append(&thisObject->m_globalThis);
581 visitor.append(&thisObject->m_regExpConstructor);
582 visitor.append(&thisObject->m_errorConstructor);
583 visitor.append(&thisObject->m_evalErrorConstructor);
584 visitor.append(&thisObject->m_rangeErrorConstructor);
585 visitor.append(&thisObject->m_referenceErrorConstructor);
586 visitor.append(&thisObject->m_syntaxErrorConstructor);
587 visitor.append(&thisObject->m_typeErrorConstructor);
588 visitor.append(&thisObject->m_URIErrorConstructor);
590 visitor.append(&thisObject->m_evalFunction);
591 visitor.append(&thisObject->m_callFunction);
592 visitor.append(&thisObject->m_applyFunction);
593 visitor.append(&thisObject->m_throwTypeErrorGetterSetter);
595 visitor.append(&thisObject->m_objectPrototype);
596 visitor.append(&thisObject->m_functionPrototype);
597 visitor.append(&thisObject->m_arrayPrototype);
598 visitor.append(&thisObject->m_errorPrototype);
600 visitor.append(&thisObject->m_promisePrototype);
601 visitor.append(&thisObject->m_promiseResolverPrototype);
604 visitor.append(&thisObject->m_withScopeStructure);
605 visitor.append(&thisObject->m_strictEvalActivationStructure);
606 visitor.append(&thisObject->m_activationStructure);
607 visitor.append(&thisObject->m_nameScopeStructure);
608 visitor.append(&thisObject->m_argumentsStructure);
609 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
610 visitor.append(&thisObject->m_originalArrayStructureForIndexingShape[i]);
611 for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
612 visitor.append(&thisObject->m_arrayStructureForIndexingShapeDuringAllocation[i]);
613 visitor.append(&thisObject->m_booleanObjectStructure);
614 visitor.append(&thisObject->m_callbackConstructorStructure);
615 visitor.append(&thisObject->m_callbackFunctionStructure);
616 visitor.append(&thisObject->m_callbackObjectStructure);
617 #if JSC_OBJC_API_ENABLED
618 visitor.append(&thisObject->m_objcCallbackFunctionStructure);
619 visitor.append(&thisObject->m_objcWrapperObjectStructure);
621 visitor.append(&thisObject->m_nullPrototypeObjectStructure);
622 visitor.append(&thisObject->m_errorStructure);
623 visitor.append(&thisObject->m_functionStructure);
624 visitor.append(&thisObject->m_boundFunctionStructure);
625 visitor.append(&thisObject->m_namedFunctionStructure);
626 visitor.append(&thisObject->m_privateNameStructure);
627 visitor.append(&thisObject->m_regExpMatchesArrayStructure);
628 visitor.append(&thisObject->m_regExpStructure);
629 visitor.append(&thisObject->m_internalFunctionStructure);
632 visitor.append(&thisObject->m_promiseStructure);
633 visitor.append(&thisObject->m_promiseResolverStructure);
634 visitor.append(&thisObject->m_promiseCallbackStructure);
635 visitor.append(&thisObject->m_promiseWrapperCallbackStructure);
636 #endif // ENABLE(PROMISES)
638 #define VISIT_SIMPLE_TYPE(CapitalName, lowerName, properName, instanceType, jsName) \
639 visitor.append(&thisObject->m_ ## lowerName ## Prototype); \
640 visitor.append(&thisObject->m_ ## properName ## Structure); \
642 FOR_EACH_SIMPLE_BUILTIN_TYPE(VISIT_SIMPLE_TYPE)
644 #undef VISIT_SIMPLE_TYPE
646 for (unsigned i = NUMBER_OF_TYPED_ARRAY_TYPES; i--;) {
647 visitor.append(&thisObject->m_typedArrays[i].prototype);
648 visitor.append(&thisObject->m_typedArrays[i].structure);
652 JSValue JSGlobalObject::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
654 if (ecmaMode == StrictMode)
655 return jsUndefined();
656 return exec->globalThisValue();
659 ExecState* JSGlobalObject::globalExec()
661 return CallFrame::create(m_globalCallFrame);
664 void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
668 for (int i = 0; i < count; ++i) {
669 GlobalPropertyInfo& global = globals[i];
670 ASSERT(global.attributes & DontDelete);
672 int index = symbolTable()->size();
673 SymbolTableEntry newEntry(index, global.attributes);
674 symbolTable()->add(global.identifier.impl(), newEntry);
675 registerAt(index).set(vm(), this, global.value);
679 bool JSGlobalObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
681 JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
682 if (getStaticFunctionSlot<Base>(exec, ExecState::globalObjectTable(exec), thisObject, propertyName, slot))
684 return symbolTableGet(thisObject, propertyName, slot);
687 void JSGlobalObject::clearRareData(JSCell* cell)
689 jsCast<JSGlobalObject*>(cell)->m_rareData.clear();
692 DynamicGlobalObjectScope::DynamicGlobalObjectScope(VM& vm, JSGlobalObject* dynamicGlobalObject)
693 : m_dynamicGlobalObjectSlot(vm.dynamicGlobalObject)
694 , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
696 if (!m_dynamicGlobalObjectSlot) {
697 #if ENABLE(ASSEMBLER)
698 if (ExecutableAllocator::underMemoryPressure())
699 vm.heap.deleteAllCompiledCode();
702 m_dynamicGlobalObjectSlot = dynamicGlobalObject;
704 // Reset the date cache between JS invocations to force the VM
705 // to observe time zone changes.
708 // Clear the exception stack between entries
709 vm.clearExceptionStack();
712 void slowValidateCell(JSGlobalObject* globalObject)
714 RELEASE_ASSERT(globalObject->isGlobalObject());
715 ASSERT_GC_OBJECT_INHERITS(globalObject, JSGlobalObject::info());
718 UnlinkedProgramCodeBlock* JSGlobalObject::createProgramCodeBlock(CallFrame* callFrame, ProgramExecutable* executable, JSObject** exception)
721 JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
722 DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
723 ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
724 UnlinkedProgramCodeBlock* unlinkedCode = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
727 debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
729 if (error.m_type != ParserError::ErrorNone) {
730 *exception = error.toErrorObject(this, executable->source());
737 UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CallFrame* callFrame, EvalExecutable* executable)
740 JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
741 DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
742 ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
743 UnlinkedEvalCodeBlock* unlinkedCode = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
746 debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
748 if (error.m_type != ParserError::ErrorNone) {
749 throwVMError(callFrame, error.toErrorObject(this, executable->source()));