JSGlobalObject.cpp 40.7 KB
Newer Older
ggaren@apple.com's avatar
ggaren@apple.com committed
1
/*
2
 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
mjs@apple.com's avatar
mjs@apple.com committed
3
 * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
ggaren@apple.com's avatar
ggaren@apple.com committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer. 
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution. 
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "JSGlobalObject.h"

33
#include "Arguments.h"
34
#include "ArrayConstructor.h"
35 36
#include "ArrayIteratorConstructor.h"
#include "ArrayIteratorPrototype.h"
weinig@apple.com's avatar
weinig@apple.com committed
37
#include "ArrayPrototype.h"
38
#include "BooleanConstructor.h"
weinig@apple.com's avatar
weinig@apple.com committed
39 40
#include "BooleanPrototype.h"
#include "CodeBlock.h"
41
#include "CodeCache.h"
42 43
#include "DateConstructor.h"
#include "DatePrototype.h"
44
#include "Debugger.h"
45
#include "Error.h"
46 47
#include "ErrorConstructor.h"
#include "ErrorPrototype.h"
weinig@apple.com's avatar
weinig@apple.com committed
48
#include "FunctionConstructor.h"
darin@apple.com's avatar
darin@apple.com committed
49
#include "FunctionPrototype.h"
50
#include "GetterSetter.h"
51
#include "HeapIterationScope.h"
52
#include "Interpreter.h"
53
#include "JSAPIWrapperObject.h"
54
#include "JSActivation.h"
55 56 57
#include "JSArrayBuffer.h"
#include "JSArrayBufferConstructor.h"
#include "JSArrayBufferPrototype.h"
58
#include "JSArrayIterator.h"
59
#include "JSBoundFunction.h"
60 61 62
#include "JSCallbackConstructor.h"
#include "JSCallbackFunction.h"
#include "JSCallbackObject.h"
63 64
#include "JSDataView.h"
#include "JSDataViewPrototype.h"
65
#include "JSFunction.h"
66 67 68
#include "JSGenericTypedArrayViewConstructorInlines.h"
#include "JSGenericTypedArrayViewInlines.h"
#include "JSGenericTypedArrayViewPrototypeInlines.h"
weinig@apple.com's avatar
weinig@apple.com committed
69
#include "JSGlobalObjectFunctions.h"
ap@webkit.org's avatar
ap@webkit.org committed
70
#include "JSLock.h"
oliver@apple.com's avatar
oliver@apple.com committed
71
#include "JSMap.h"
72
#include "JSNameScope.h"
73
#include "JSONObject.h"
oliver@apple.com's avatar
oliver@apple.com committed
74
#include "JSSet.h"
75 76 77
#include "JSTypedArrayConstructors.h"
#include "JSTypedArrayPrototypes.h"
#include "JSTypedArrays.h"
oliver@apple.com's avatar
oliver@apple.com committed
78
#include "JSWeakMap.h"
79
#include "JSWithScope.h"
80
#include "LegacyProfiler.h"
81
#include "Lookup.h"
oliver@apple.com's avatar
oliver@apple.com committed
82 83
#include "MapConstructor.h"
#include "MapPrototype.h"
darin@apple.com's avatar
darin@apple.com committed
84
#include "MathObject.h"
85 86 87
#include "NameConstructor.h"
#include "NameInstance.h"
#include "NamePrototype.h"
88 89
#include "NativeErrorConstructor.h"
#include "NativeErrorPrototype.h"
weinig@apple.com's avatar
weinig@apple.com committed
90 91
#include "NumberConstructor.h"
#include "NumberPrototype.h"
92
#include "ObjCCallbackFunction.h"
weinig@apple.com's avatar
weinig@apple.com committed
93 94
#include "ObjectConstructor.h"
#include "ObjectPrototype.h"
95 96
#include "Operations.h"
#include "ParserError.h"
97
#include "RegExpConstructor.h"
mjs@apple.com's avatar
mjs@apple.com committed
98
#include "RegExpMatchesArray.h"
99
#include "RegExpObject.h"
100
#include "RegExpPrototype.h"
oliver@apple.com's avatar
oliver@apple.com committed
101 102
#include "SetConstructor.h"
#include "SetPrototype.h"
103
#include "StrictEvalActivation.h"
weinig@apple.com's avatar
weinig@apple.com committed
104 105
#include "StringConstructor.h"
#include "StringPrototype.h"
oliver@apple.com's avatar
oliver@apple.com committed
106 107
#include "WeakMapConstructor.h"
#include "WeakMapPrototype.h"
108

109 110 111 112 113 114 115 116 117 118
#if ENABLE(PROMISES)
#include "JSPromise.h"
#include "JSPromiseCallback.h"
#include "JSPromiseConstructor.h"
#include "JSPromisePrototype.h"
#include "JSPromiseResolver.h"
#include "JSPromiseResolverConstructor.h"
#include "JSPromiseResolverPrototype.h"
#endif // ENABLE(PROMISES)

119 120
#include "JSGlobalObject.lut.h"

121
namespace JSC {
ggaren@apple.com's avatar
ggaren@apple.com committed
122

123
const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
124

weinig@apple.com's avatar
weinig@apple.com committed
125
const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled, 0 };
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* Source for JSGlobalObject.lut.h
@begin globalObjectTable
  parseInt              globalFuncParseInt              DontEnum|Function 2
  parseFloat            globalFuncParseFloat            DontEnum|Function 1
  isNaN                 globalFuncIsNaN                 DontEnum|Function 1
  isFinite              globalFuncIsFinite              DontEnum|Function 1
  escape                globalFuncEscape                DontEnum|Function 1
  unescape              globalFuncUnescape              DontEnum|Function 1
  decodeURI             globalFuncDecodeURI             DontEnum|Function 1
  decodeURIComponent    globalFuncDecodeURIComponent    DontEnum|Function 1
  encodeURI             globalFuncEncodeURI             DontEnum|Function 1
  encodeURIComponent    globalFuncEncodeURIComponent    DontEnum|Function 1
@end
*/

ggaren@apple.com's avatar
ggaren@apple.com committed
142 143
JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
    : Base(vm, structure, 0)
144
    , m_masqueradesAsUndefinedWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
145
    , m_havingABadTimeWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
146
    , m_varInjectionWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
147
    , m_weakRandom(Options::forceWeakRandomSeed() ? Options::forcedWeakRandomSeed() : static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
148 149 150 151 152
    , m_evalEnabled(true)
    , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
{
}

ggaren@apple.com's avatar
ggaren@apple.com committed
153
JSGlobalObject::~JSGlobalObject()
ggaren@apple.com's avatar
ggaren@apple.com committed
154
{
155 156
    if (m_debugger)
        m_debugger->detach(this);
ggaren@apple.com's avatar
ggaren@apple.com committed
157

ggaren@apple.com's avatar
ggaren@apple.com committed
158
    if (LegacyProfiler* profiler = vm().enabledProfiler())
159
        profiler->stopProfiling(this);
ggaren@apple.com's avatar
ggaren@apple.com committed
160 161
}

162 163
void JSGlobalObject::destroy(JSCell* cell)
{
164
    static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
165 166
}

ggaren@apple.com's avatar
ggaren@apple.com committed
167
void JSGlobalObject::setGlobalThis(VM& vm, JSObject* globalThis)
168
{ 
ggaren@apple.com's avatar
ggaren@apple.com committed
169
    m_globalThis.set(vm, this, globalThis);
170 171
}

darin@apple.com's avatar
darin@apple.com committed
172
void JSGlobalObject::init(JSObject* thisValue)
ggaren@apple.com's avatar
ggaren@apple.com committed
173
{
174
    ASSERT(vm().currentThreadIsHoldingAPILock());
darin@apple.com's avatar
darin@apple.com committed
175

ggaren@apple.com's avatar
ggaren@apple.com committed
176
    setGlobalThis(vm(), thisValue);
177
    JSGlobalObject::globalExec()->init(0, 0, this, CallFrame::noCaller(), 0, 0);
ap@webkit.org's avatar
ap@webkit.org committed
178

179
    m_debugger = 0;
180

ggaren@apple.com's avatar
ggaren@apple.com committed
181 182 183
    reset(prototype());
}

184
void JSGlobalObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
185
{
186
    JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
187
    ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
ap@webkit.org's avatar
ap@webkit.org committed
188

189
    if (symbolTablePut(thisObject, exec, propertyName, value, slot.isStrictMode()))
ggaren@apple.com's avatar
ggaren@apple.com committed
190
        return;
191
    Base::put(thisObject, exec, propertyName, value, slot);
darin@apple.com's avatar
darin@apple.com committed
192 193
}

194
bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
195
{
196
    JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
197
    PropertySlot slot(thisObject);
198
    // silently ignore attempts to add accessors aliasing vars.
199
    if (descriptor.isAccessorDescriptor() && symbolTableGet(thisObject, propertyName, slot))
200 201
        return false;
    return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
202 203
}

204 205 206 207 208 209 210 211
int JSGlobalObject::addGlobalVar(const Identifier& ident, ConstantMode constantMode, FunctionMode functionMode)
{
    ConcurrentJITLocker locker(symbolTable()->m_lock);
    int index = symbolTable()->size(locker);
    SymbolTableEntry newEntry(index, (constantMode == IsConstant) ? ReadOnly : 0);
    if (functionMode == IsFunctionToSpecialize)
        newEntry.attemptToWatch();
    SymbolTable::Map::AddResult result = symbolTable()->add(locker, ident.impl(), newEntry);
212 213 214
    if (result.isNewEntry)
        addRegisters(1);
    else {
215 216 217 218 219
        result.iterator->value.notifyWrite();
        index = result.iterator->value.getIndex();
    }
    return index;
}
220

ggaren@apple.com's avatar
ggaren@apple.com committed
221 222 223
static inline JSObject* lastInPrototypeChain(JSObject* object)
{
    JSObject* o = object;
weinig@apple.com's avatar
weinig@apple.com committed
224
    while (o->prototype().isObject())
darin@apple.com's avatar
darin@apple.com committed
225
        o = asObject(o->prototype());
ggaren@apple.com's avatar
ggaren@apple.com committed
226 227 228
    return o;
}

ggaren@apple.com's avatar
ggaren@apple.com committed
229
void JSGlobalObject::reset(JSValue prototype)
ggaren@apple.com's avatar
ggaren@apple.com committed
230
{
darin@apple.com's avatar
darin@apple.com committed
231
    ExecState* exec = JSGlobalObject::globalExec();
232
    VM& vm = exec->vm();
ggaren@apple.com's avatar
ggaren@apple.com committed
233

234
    m_functionPrototype.set(vm, this, FunctionPrototype::create(vm, FunctionPrototype::createStructure(vm, this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
235 236 237 238
    m_functionStructure.set(vm, this, JSFunction::createStructure(vm, this, m_functionPrototype.get()));
    m_boundFunctionStructure.set(vm, this, JSBoundFunction::createStructure(vm, this, m_functionPrototype.get()));
    m_namedFunctionStructure.set(vm, this, Structure::addPropertyTransition(vm, m_functionStructure.get(), vm.propertyNames->name, DontDelete | ReadOnly | DontEnum, 0, m_functionNameOffset));
    m_internalFunctionStructure.set(vm, this, InternalFunction::createStructure(vm, this, m_functionPrototype.get()));
239 240
    JSFunction* callFunction = 0;
    JSFunction* applyFunction = 0;
241
    m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
242 243
    m_callFunction.set(vm, this, callFunction);
    m_applyFunction.set(vm, this, applyFunction);
244
    m_objectPrototype.set(vm, this, ObjectPrototype::create(vm, this, ObjectPrototype::createStructure(vm, this, jsNull())));
245
    GetterSetter* protoAccessor = GetterSetter::create(vm);
246 247 248
    protoAccessor->setGetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoGetter));
    protoAccessor->setSetter(vm, JSFunction::create(vm, this, 0, String(), globalFuncProtoSetter));
    m_objectPrototype->putDirectNonIndexAccessor(vm, vm.propertyNames->underscoreProto, protoAccessor, Accessor | DontEnum);
249
    m_functionPrototype->structure()->setPrototypeWithoutTransition(vm, m_objectPrototype.get());
250
    
251 252 253 254 255 256 257 258 259
    m_typedArrays[toIndex(TypeInt8)].prototype.set(vm, this, JSInt8ArrayPrototype::create(vm, this, JSInt8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeInt16)].prototype.set(vm, this, JSInt16ArrayPrototype::create(vm, this, JSInt16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeInt32)].prototype.set(vm, this, JSInt32ArrayPrototype::create(vm, this, JSInt32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint8)].prototype.set(vm, this, JSUint8ArrayPrototype::create(vm, this, JSUint8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint8Clamped)].prototype.set(vm, this, JSUint8ClampedArrayPrototype::create(vm, this, JSUint8ClampedArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint16)].prototype.set(vm, this, JSUint16ArrayPrototype::create(vm, this, JSUint16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint32)].prototype.set(vm, this, JSUint32ArrayPrototype::create(vm, this, JSUint32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeFloat32)].prototype.set(vm, this, JSFloat32ArrayPrototype::create(vm, this, JSFloat32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeFloat64)].prototype.set(vm, this, JSFloat64ArrayPrototype::create(vm, this, JSFloat64ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
260
    m_typedArrays[toIndex(TypeDataView)].prototype.set(vm, this, JSDataViewPrototype::create(vm, JSDataViewPrototype::createStructure(vm, this, m_objectPrototype.get())));
261
    
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    m_typedArrays[toIndex(TypeInt8)].structure.set(vm, this, JSInt8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt8)].prototype.get()));
    m_typedArrays[toIndex(TypeInt16)].structure.set(vm, this, JSInt16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt16)].prototype.get()));
    m_typedArrays[toIndex(TypeInt32)].structure.set(vm, this, JSInt32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeInt32)].prototype.get()));
    m_typedArrays[toIndex(TypeUint8)].structure.set(vm, this, JSUint8Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8)].prototype.get()));
    m_typedArrays[toIndex(TypeUint8Clamped)].structure.set(vm, this, JSUint8ClampedArray::createStructure(vm, this, m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get()));
    m_typedArrays[toIndex(TypeUint16)].structure.set(vm, this, JSUint16Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint16)].prototype.get()));
    m_typedArrays[toIndex(TypeUint32)].structure.set(vm, this, JSUint32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeUint32)].prototype.get()));
    m_typedArrays[toIndex(TypeFloat32)].structure.set(vm, this, JSFloat32Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat32)].prototype.get()));
    m_typedArrays[toIndex(TypeFloat64)].structure.set(vm, this, JSFloat64Array::createStructure(vm, this, m_typedArrays[toIndex(TypeFloat64)].prototype.get()));
    m_typedArrays[toIndex(TypeDataView)].structure.set(vm, this, JSDataView::createStructure(vm, this, m_typedArrays[toIndex(TypeDataView)].prototype.get()));

    m_nameScopeStructure.set(vm, this, JSNameScope::createStructure(vm, this, jsNull()));
    m_activationStructure.set(vm, this, JSActivation::createStructure(vm, this, jsNull()));
    m_strictEvalActivationStructure.set(vm, this, StrictEvalActivation::createStructure(vm, this, jsNull()));
    m_withScopeStructure.set(vm, this, JSWithScope::createStructure(vm, this, jsNull()));

    m_nullPrototypeObjectStructure.set(vm, this, JSFinalObject::createStructure(vm, this, jsNull(), JSFinalObject::defaultInlineCapacity()));

    m_callbackFunctionStructure.set(vm, this, JSCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
    m_argumentsStructure.set(vm, this, Arguments::createStructure(vm, this, m_objectPrototype.get()));
    m_callbackConstructorStructure.set(vm, this, JSCallbackConstructor::createStructure(vm, this, m_objectPrototype.get()));
    m_callbackObjectStructure.set(vm, this, JSCallbackObject<JSDestructibleObject>::createStructure(vm, this, m_objectPrototype.get()));
284
#if JSC_OBJC_API_ENABLED
285 286
    m_objcCallbackFunctionStructure.set(vm, this, ObjCCallbackFunction::createStructure(vm, this, m_functionPrototype.get()));
    m_objcWrapperObjectStructure.set(vm, this, JSCallbackObject<JSAPIWrapperObject>::createStructure(vm, this, m_objectPrototype.get()));
287
#endif
288

oliver@apple.com's avatar
oliver@apple.com committed
289
    m_arrayPrototype.set(vm, this, ArrayPrototype::create(vm, this, ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
290
    
291 292 293 294 295 296
    m_originalArrayStructureForIndexingShape[UndecidedShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithUndecided));
    m_originalArrayStructureForIndexingShape[Int32Shape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithInt32));
    m_originalArrayStructureForIndexingShape[DoubleShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithDouble));
    m_originalArrayStructureForIndexingShape[ContiguousShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithContiguous));
    m_originalArrayStructureForIndexingShape[ArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithArrayStorage));
    m_originalArrayStructureForIndexingShape[SlowPutArrayStorageShape >> IndexingShapeShift].set(vm, this, JSArray::createStructure(vm, this, m_arrayPrototype.get(), ArrayWithSlowPutArrayStorage));
297 298 299
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
        m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
    
300
    m_regExpMatchesArrayStructure.set(vm, this, RegExpMatchesArray::createStructure(vm, this, m_arrayPrototype.get()));
301

302
    RegExp* emptyRegex = RegExp::create(vm, "", NoFlags);
303
    
304
    m_regExpPrototype.set(vm, this, RegExpPrototype::create(vm, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()), emptyRegex));
305
    m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get()));
306

307
#if ENABLE(PROMISES)
308 309
    m_promisePrototype.set(vm, this, JSPromisePrototype::create(exec, this, JSPromisePrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_promiseStructure.set(vm, this, JSPromise::createStructure(vm, this, m_promisePrototype.get()));
weinig@apple.com's avatar
weinig@apple.com committed
310

311 312 313 314
    m_promiseResolverPrototype.set(vm, this, JSPromiseResolverPrototype::create(exec, this, JSPromiseResolverPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_promiseResolverStructure.set(vm, this, JSPromiseResolver::createStructure(vm, this, m_promiseResolverPrototype.get()));
    m_promiseCallbackStructure.set(vm, this, JSPromiseCallback::createStructure(vm, this, m_functionPrototype.get()));
    m_promiseWrapperCallbackStructure.set(vm, this, JSPromiseWrapperCallback::createStructure(vm, this, m_functionPrototype.get()));
315 316
#endif // ENABLE(PROMISES)

317
#define CREATE_PROTOTYPE_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
318
    m_ ## lowerName ## Prototype.set(vm, this, capitalName##Prototype::create(vm, this, capitalName##Prototype::createStructure(vm, this, m_objectPrototype.get()))); \
319
    m_ ## properName ## Structure.set(vm, this, instanceType::createStructure(vm, this, m_ ## lowerName ## Prototype.get()));
320 321 322 323

    FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_PROTOTYPE_FOR_SIMPLE_TYPE)

#undef CREATE_PROTOTYPE_FOR_SIMPLE_TYPE
oliver@apple.com's avatar
oliver@apple.com committed
324

ggaren@apple.com's avatar
ggaren@apple.com committed
325
    // Constructors
darin@apple.com's avatar
darin@apple.com committed
326

327 328 329
    JSCell* objectConstructor = ObjectConstructor::create(vm, ObjectConstructor::createStructure(vm, this, m_functionPrototype.get()), m_objectPrototype.get());
    JSCell* functionConstructor = FunctionConstructor::create(vm, FunctionConstructor::createStructure(vm, this, m_functionPrototype.get()), m_functionPrototype.get());
    JSCell* arrayConstructor = ArrayConstructor::create(vm, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get());
330 331

#if ENABLE(PROMISES)
332 333
    JSCell* promiseConstructor = JSPromiseConstructor::create(vm, JSPromiseConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promisePrototype.get());
    JSCell* promiseResolverConstructor = JSPromiseResolverConstructor::create(vm, JSPromiseResolverConstructor::createStructure(vm, this, m_functionPrototype.get()), m_promiseResolverPrototype.get());
334
#endif // ENABLE(PROMISES)
ggaren@apple.com's avatar
ggaren@apple.com committed
335

336
    m_regExpConstructor.set(vm, this, RegExpConstructor::create(vm, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get()));
ggaren@apple.com's avatar
ggaren@apple.com committed
337

338
#define CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
339
    capitalName ## Constructor* lowerName ## Constructor = capitalName ## Constructor::create(vm, capitalName ## Constructor::createStructure(vm, this, m_functionPrototype.get()), m_ ## lowerName ## Prototype.get()); \
340
    m_ ## lowerName ## Prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, lowerName ## Constructor, DontEnum); \
341 342 343 344 345

    FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE)

#undef CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE

346 347 348 349
    m_errorConstructor.set(vm, this, errorConstructor);

    Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(vm, this, m_errorPrototype.get());
    Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(vm, this, m_functionPrototype.get());
350 351 352 353 354 355
    m_evalErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("EvalError")));
    m_rangeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("RangeError")));
    m_referenceErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("ReferenceError")));
    m_syntaxErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("SyntaxError")));
    m_typeErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("TypeError")));
    m_URIErrorConstructor.set(vm, this, NativeErrorConstructor::create(vm, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("URIError")));
356 357 358 359 360

    m_objectPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, objectConstructor, DontEnum);
    m_functionPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, functionConstructor, DontEnum);
    m_arrayPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, arrayConstructor, DontEnum);
    m_regExpPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, m_regExpConstructor.get(), DontEnum);
361
#if ENABLE(PROMISES)
362 363
    m_promisePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseConstructor, DontEnum);
    m_promiseResolverPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseResolverConstructor, DontEnum);
364
#endif
ggaren@apple.com's avatar
ggaren@apple.com committed
365

366 367 368 369 370 371 372 373 374 375
    putDirectWithoutTransition(vm, vm.propertyNames->Object, objectConstructor, DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->Function, functionConstructor, DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->Array, arrayConstructor, DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->RegExp, m_regExpConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->EvalError, m_evalErrorConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->RangeError, m_rangeErrorConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->ReferenceError, m_referenceErrorConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->SyntaxError, m_syntaxErrorConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->TypeError, m_typeErrorConstructor.get(), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->URIError, m_URIErrorConstructor.get(), DontEnum);
376
#if ENABLE(PROMISES)
377 378
    putDirectWithoutTransition(vm, vm.propertyNames->Promise, promiseConstructor, DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->PromiseResolver, promiseResolverConstructor, DontEnum);
379
#endif
380 381 382


#define PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE(capitalName, lowerName, properName, instanceType, jsName) \
383
    putDirectWithoutTransition(vm, vm.propertyNames-> jsName, lowerName ## Constructor, DontEnum); \
384 385 386 387

    FOR_EACH_SIMPLE_BUILTIN_TYPE(PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE)

#undef PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE
388 389 390 391 392 393
    PrototypeMap& prototypeMap = vm.prototypeMap;
    Structure* iteratorResultStructure = prototypeMap.emptyObjectStructureForPrototype(m_objectPrototype.get(), JSFinalObject::defaultInlineCapacity());
    PropertyOffset offset;
    iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->done, 0, 0, offset);
    iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->value, 0, 0, offset);
    m_iteratorResultStructure.set(vm, this, iteratorResultStructure);
ggaren@apple.com's avatar
ggaren@apple.com committed
394

395
    m_evalFunction.set(vm, this, JSFunction::create(vm, this, 1, vm.propertyNames->eval.string(), globalFuncEval));
396
    putDirectWithoutTransition(vm, vm.propertyNames->eval, m_evalFunction.get(), DontEnum);
ggaren@apple.com's avatar
ggaren@apple.com committed
397

398
    putDirectWithoutTransition(vm, vm.propertyNames->JSON, JSONObject::create(exec, this, JSONObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
399
    putDirectWithoutTransition(vm, vm.propertyNames->Math, MathObject::create(vm, this, MathObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
400 401
    
    FixedArray<InternalFunction*, NUMBER_OF_TYPED_ARRAY_TYPES> typedArrayConstructors;
402 403 404 405 406 407 408 409 410 411
    typedArrayConstructors[toIndex(TypeInt8)] = JSInt8ArrayConstructor::create(vm, JSInt8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt8)].prototype.get(), "Int8Array");
    typedArrayConstructors[toIndex(TypeInt16)] = JSInt16ArrayConstructor::create(vm, JSInt16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt16)].prototype.get(), "Int16Array");
    typedArrayConstructors[toIndex(TypeInt32)] = JSInt32ArrayConstructor::create(vm, JSInt32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeInt32)].prototype.get(), "Int32Array");
    typedArrayConstructors[toIndex(TypeUint8)] = JSUint8ArrayConstructor::create(vm, JSUint8ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8)].prototype.get(), "Uint8Array");
    typedArrayConstructors[toIndex(TypeUint8Clamped)] = JSUint8ClampedArrayConstructor::create(vm, JSUint8ClampedArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint8Clamped)].prototype.get(), "Uint8ClampedArray");
    typedArrayConstructors[toIndex(TypeUint16)] = JSUint16ArrayConstructor::create(vm, JSUint16ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint16)].prototype.get(), "Uint16Array");
    typedArrayConstructors[toIndex(TypeUint32)] = JSUint32ArrayConstructor::create(vm, JSUint32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeUint32)].prototype.get(), "Uint32Array");
    typedArrayConstructors[toIndex(TypeFloat32)] = JSFloat32ArrayConstructor::create(vm, JSFloat32ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat32)].prototype.get(), "Float32Array");
    typedArrayConstructors[toIndex(TypeFloat64)] = JSFloat64ArrayConstructor::create(vm, JSFloat64ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeFloat64)].prototype.get(), "Float64Array");
    typedArrayConstructors[toIndex(TypeDataView)] = JSDataViewConstructor::create(vm, JSDataViewConstructor::createStructure(vm, this, m_functionPrototype.get()), m_typedArrays[toIndex(TypeDataView)].prototype.get(), "DataView");
412

413
    for (unsigned typedArrayIndex = NUMBER_OF_TYPED_ARRAY_TYPES; typedArrayIndex--;) {
414 415
        m_typedArrays[typedArrayIndex].prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, typedArrayConstructors[typedArrayIndex], DontEnum);
        putDirectWithoutTransition(vm, Identifier(exec, typedArrayConstructors[typedArrayIndex]->name(exec)), typedArrayConstructors[typedArrayIndex], DontEnum);
416
    }
417

418
    GlobalPropertyInfo staticGlobals[] = {
419 420 421
        GlobalPropertyInfo(vm.propertyNames->NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
        GlobalPropertyInfo(vm.propertyNames->Infinity, jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
        GlobalPropertyInfo(vm.propertyNames->undefinedKeyword, jsUndefined(), DontEnum | DontDelete | ReadOnly)
422
    };
423
    addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
424 425 426
    
    m_specialPointers[Special::CallFunction] = m_callFunction.get();
    m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
427 428
    m_specialPointers[Special::ObjectConstructor] = objectConstructor;
    m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
429

430
    if (m_experimentsEnabled) {
431 432
        NamePrototype* privateNamePrototype = NamePrototype::create(exec, NamePrototype::createStructure(vm, this, m_objectPrototype.get()));
        m_privateNameStructure.set(vm, this, NameInstance::createStructure(vm, this, privateNamePrototype));
433

434
        JSCell* privateNameConstructor = NameConstructor::create(vm, NameConstructor::createStructure(vm, this, m_functionPrototype.get()), privateNamePrototype);
435 436
        privateNamePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, privateNameConstructor, DontEnum);
        putDirectWithoutTransition(vm, Identifier(exec, "Name"), privateNameConstructor, DontEnum);
437 438
    }

439
    resetPrototype(vm, prototype);
weinig@apple.com's avatar
weinig@apple.com committed
440
}
ggaren@apple.com's avatar
ggaren@apple.com committed
441

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
// Private namespace for helpers for JSGlobalObject::haveABadTime()
namespace {

class ObjectsWithBrokenIndexingFinder : public MarkedBlock::VoidFunctor {
public:
    ObjectsWithBrokenIndexingFinder(MarkedArgumentBuffer&, JSGlobalObject*);
    void operator()(JSCell*);

private:
    MarkedArgumentBuffer& m_foundObjects;
    JSGlobalObject* m_globalObject;
};

ObjectsWithBrokenIndexingFinder::ObjectsWithBrokenIndexingFinder(
    MarkedArgumentBuffer& foundObjects, JSGlobalObject* globalObject)
    : m_foundObjects(foundObjects)
    , m_globalObject(globalObject)
{
}

inline bool hasBrokenIndexing(JSObject* object)
{
    // This will change if we have more indexing types.
465
    IndexingType type = object->structure()->indexingType();
466 467 468
    // This could be made obviously more efficient, but isn't made so right now, because
    // we expect this to be an unlikely slow path anyway.
    return hasUndecided(type) || hasInt32(type) || hasDouble(type) || hasContiguous(type) || hasFastArrayStorage(type);
469 470 471 472 473 474 475 476
}

void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
{
    if (!cell->isObject())
        return;
    
    JSObject* object = asObject(cell);
477 478 479

    // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
    if (!hasBrokenIndexing(object))
480 481
        return;
    
482 483
    // We only want to have a bad time in the affected global object, not in the entire
    // VM. But we have to be careful, since there may be objects that claim to belong to
484
    // a different global object that have prototypes from our global object.
485 486
    bool foundGlobalObject = false;
    for (JSObject* current = object; ;) {
487
        if (current->globalObject() == m_globalObject) {
488 489 490 491 492 493 494 495 496 497
            foundGlobalObject = true;
            break;
        }
        
        JSValue prototypeValue = current->prototype();
        if (prototypeValue.isNull())
            break;
        current = asObject(prototypeValue);
    }
    if (!foundGlobalObject)
498 499 500 501 502 503 504
        return;
    
    m_foundObjects.append(object);
}

} // end private namespace for helpers for JSGlobalObject::haveABadTime()

ggaren@apple.com's avatar
ggaren@apple.com committed
505
void JSGlobalObject::haveABadTime(VM& vm)
506
{
ggaren@apple.com's avatar
ggaren@apple.com committed
507
    ASSERT(&vm == &this->vm());
508 509 510 511 512 513 514 515 516 517 518 519
    
    if (isHavingABadTime())
        return;
    
    // Make sure that all allocations or indexed storage transitions that are inlining
    // the assumption that it's safe to transition to a non-SlowPut array storage don't
    // do so anymore.
    m_havingABadTimeWatchpoint->notifyWrite();
    ASSERT(isHavingABadTime()); // The watchpoint is what tells us that we're having a bad time.
    
    // Make sure that all JSArray allocations that load the appropriate structure from
    // this object now load a structure that uses SlowPut.
520
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
ggaren@apple.com's avatar
ggaren@apple.com committed
521
        m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
522 523 524 525 526
    
    // Make sure that all objects that have indexed storage switch to the slow kind of
    // indexed storage.
    MarkedArgumentBuffer foundObjects; // Use MarkedArgumentBuffer because switchToSlowPutArrayStorage() may GC.
    ObjectsWithBrokenIndexingFinder finder(foundObjects, this);
527 528 529 530
    {
        HeapIterationScope iterationScope(vm.heap);
        vm.heap.objectSpace().forEachLiveCell(iterationScope, finder);
    }
531 532 533 534
    while (!foundObjects.isEmpty()) {
        JSObject* object = asObject(foundObjects.last());
        foundObjects.removeLast();
        ASSERT(hasBrokenIndexing(object));
ggaren@apple.com's avatar
ggaren@apple.com committed
535
        object->switchToSlowPutArrayStorage(vm);
536 537 538
    }
}

539 540 541 542 543 544
bool JSGlobalObject::objectPrototypeIsSane()
{
    return !hasIndexedProperties(m_objectPrototype->structure()->indexingType())
        && m_objectPrototype->prototype().isNull();
}

545 546 547 548
bool JSGlobalObject::arrayPrototypeChainIsSane()
{
    return !hasIndexedProperties(m_arrayPrototype->structure()->indexingType())
        && m_arrayPrototype->prototype() == m_objectPrototype.get()
549 550 551 552 553 554 555 556
        && objectPrototypeIsSane();
}

bool JSGlobalObject::stringPrototypeChainIsSane()
{
    return !hasIndexedProperties(m_stringPrototype->structure()->indexingType())
        && m_stringPrototype->prototype() == m_objectPrototype.get()
        && objectPrototypeIsSane();
557 558
}

559
void JSGlobalObject::createThrowTypeError(VM& vm)
560
{
561
    JSFunction* thrower = JSFunction::create(vm, this, 0, String(), globalFuncThrowTypeError);
562 563 564 565
    GetterSetter* getterSetter = GetterSetter::create(vm);
    getterSetter->setGetter(vm, thrower);
    getterSetter->setSetter(vm, thrower);
    m_throwTypeErrorGetterSetter.set(vm, this, getterSetter);
566 567
}

weinig@apple.com's avatar
weinig@apple.com committed
568
// Set prototype, and also insert the object prototype at the end of the chain.
ggaren@apple.com's avatar
ggaren@apple.com committed
569
void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
weinig@apple.com's avatar
weinig@apple.com committed
570
{
ggaren@apple.com's avatar
ggaren@apple.com committed
571
    setPrototype(vm, prototype);
572 573

    JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
574
    JSObject* objectPrototype = m_objectPrototype.get();
575
    if (oldLastInPrototypeChain != objectPrototype)
ggaren@apple.com's avatar
ggaren@apple.com committed
576
        oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
ggaren@apple.com's avatar
ggaren@apple.com committed
577 578
}

579 580
void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
{ 
581
    JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
582
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
583
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
584
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
585
    Base::visitChildren(thisObject, visitor);
586

587
    visitor.append(&thisObject->m_globalThis);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

    visitor.append(&thisObject->m_regExpConstructor);
    visitor.append(&thisObject->m_errorConstructor);
    visitor.append(&thisObject->m_evalErrorConstructor);
    visitor.append(&thisObject->m_rangeErrorConstructor);
    visitor.append(&thisObject->m_referenceErrorConstructor);
    visitor.append(&thisObject->m_syntaxErrorConstructor);
    visitor.append(&thisObject->m_typeErrorConstructor);
    visitor.append(&thisObject->m_URIErrorConstructor);

    visitor.append(&thisObject->m_evalFunction);
    visitor.append(&thisObject->m_callFunction);
    visitor.append(&thisObject->m_applyFunction);
    visitor.append(&thisObject->m_throwTypeErrorGetterSetter);

    visitor.append(&thisObject->m_objectPrototype);
    visitor.append(&thisObject->m_functionPrototype);
    visitor.append(&thisObject->m_arrayPrototype);
    visitor.append(&thisObject->m_errorPrototype);
607
#if ENABLE(PROMISES)
weinig@apple.com's avatar
weinig@apple.com committed
608 609
    visitor.append(&thisObject->m_promisePrototype);
    visitor.append(&thisObject->m_promiseResolverPrototype);
610
#endif
611

612 613 614 615
    visitor.append(&thisObject->m_withScopeStructure);
    visitor.append(&thisObject->m_strictEvalActivationStructure);
    visitor.append(&thisObject->m_activationStructure);
    visitor.append(&thisObject->m_nameScopeStructure);
616
    visitor.append(&thisObject->m_argumentsStructure);
617 618 619 620
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
        visitor.append(&thisObject->m_originalArrayStructureForIndexingShape[i]);
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
        visitor.append(&thisObject->m_arrayStructureForIndexingShapeDuringAllocation[i]);
621 622 623 624
    visitor.append(&thisObject->m_booleanObjectStructure);
    visitor.append(&thisObject->m_callbackConstructorStructure);
    visitor.append(&thisObject->m_callbackFunctionStructure);
    visitor.append(&thisObject->m_callbackObjectStructure);
625 626
#if JSC_OBJC_API_ENABLED
    visitor.append(&thisObject->m_objcCallbackFunctionStructure);
627
    visitor.append(&thisObject->m_objcWrapperObjectStructure);
628
#endif
629 630 631 632 633 634 635 636 637
    visitor.append(&thisObject->m_nullPrototypeObjectStructure);
    visitor.append(&thisObject->m_errorStructure);
    visitor.append(&thisObject->m_functionStructure);
    visitor.append(&thisObject->m_boundFunctionStructure);
    visitor.append(&thisObject->m_namedFunctionStructure);
    visitor.append(&thisObject->m_privateNameStructure);
    visitor.append(&thisObject->m_regExpMatchesArrayStructure);
    visitor.append(&thisObject->m_regExpStructure);
    visitor.append(&thisObject->m_internalFunctionStructure);
638 639

#if ENABLE(PROMISES)
weinig@apple.com's avatar
weinig@apple.com committed
640 641 642 643
    visitor.append(&thisObject->m_promiseStructure);
    visitor.append(&thisObject->m_promiseResolverStructure);
    visitor.append(&thisObject->m_promiseCallbackStructure);
    visitor.append(&thisObject->m_promiseWrapperCallbackStructure);
644
#endif // ENABLE(PROMISES)
645 646 647 648 649 650 651 652 653

#define VISIT_SIMPLE_TYPE(CapitalName, lowerName, properName, instanceType, jsName) \
    visitor.append(&thisObject->m_ ## lowerName ## Prototype); \
    visitor.append(&thisObject->m_ ## properName ## Structure); \

    FOR_EACH_SIMPLE_BUILTIN_TYPE(VISIT_SIMPLE_TYPE)

#undef VISIT_SIMPLE_TYPE

654 655 656 657
    for (unsigned i = NUMBER_OF_TYPED_ARRAY_TYPES; i--;) {
        visitor.append(&thisObject->m_typedArrays[i].prototype);
        visitor.append(&thisObject->m_typedArrays[i].structure);
    }
ggaren@apple.com's avatar
ggaren@apple.com committed
658 659
}

660
JSValue JSGlobalObject::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
661
{
662 663 664
    if (ecmaMode == StrictMode)
        return jsUndefined();
    return exec->globalThisValue();
665 666
}

ggaren@apple.com's avatar
ggaren@apple.com committed
667 668
ExecState* JSGlobalObject::globalExec()
{
669
    return CallFrame::create(m_globalCallFrame);
ggaren@apple.com's avatar
ggaren@apple.com committed
670 671
}

672 673
void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
{
674
    addRegisters(count);
675

676
    for (int i = 0; i < count; ++i) {
677 678
        GlobalPropertyInfo& global = globals[i];
        ASSERT(global.attributes & DontDelete);
679
        
680
        int index = symbolTable()->size();
681
        SymbolTableEntry newEntry(index, global.attributes);
682
        symbolTable()->add(global.identifier.impl(), newEntry);
ggaren@apple.com's avatar
ggaren@apple.com committed
683
        registerAt(index).set(vm(), this, global.value);
684 685 686
    }
}

687
bool JSGlobalObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
688
{
689
    JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
690
    if (getStaticFunctionSlot<Base>(exec, ExecState::globalObjectTable(exec), thisObject, propertyName, slot))
691
        return true;
692
    return symbolTableGet(thisObject, propertyName, slot);
693 694
}

695
void JSGlobalObject::clearRareData(JSCell* cell)
696
{
697
    jsCast<JSGlobalObject*>(cell)->m_rareData.clear();
698 699
}

ggaren@apple.com's avatar
ggaren@apple.com committed
700 701
DynamicGlobalObjectScope::DynamicGlobalObjectScope(VM& vm, JSGlobalObject* dynamicGlobalObject)
    : m_dynamicGlobalObjectSlot(vm.dynamicGlobalObject)
702 703 704 705 706
    , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
{
    if (!m_dynamicGlobalObjectSlot) {
#if ENABLE(ASSEMBLER)
        if (ExecutableAllocator::underMemoryPressure())
ggaren@apple.com's avatar
ggaren@apple.com committed
707
            vm.heap.deleteAllCompiledCode();
708 709 710 711 712 713
#endif

        m_dynamicGlobalObjectSlot = dynamicGlobalObject;

        // Reset the date cache between JS invocations to force the VM
        // to observe time zone changes.
ggaren@apple.com's avatar
ggaren@apple.com committed
714
        vm.resetDateCache();
715
    }
716
    // Clear the exception stack between entries
717
    vm.clearExceptionStack();
718 719
}

720 721
void slowValidateCell(JSGlobalObject* globalObject)
{
722
    RELEASE_ASSERT(globalObject->isGlobalObject());
723
    ASSERT_GC_OBJECT_INHERITS(globalObject, JSGlobalObject::info());
724 725
}

726 727 728 729 730 731
UnlinkedProgramCodeBlock* JSGlobalObject::createProgramCodeBlock(CallFrame* callFrame, ProgramExecutable* executable, JSObject** exception)
{
    ParserError error;
    JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
    DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
    ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
ggaren@apple.com's avatar
ggaren@apple.com committed
732
    UnlinkedProgramCodeBlock* unlinkedCode = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
733 734 735 736 737 738 739 740 741 742 743 744

    if (hasDebugger())
        debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);

    if (error.m_type != ParserError::ErrorNone) {
        *exception = error.toErrorObject(this, executable->source());
        return 0;
    }
    
    return unlinkedCode;
}

745
UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CallFrame* callFrame, EvalExecutable* executable)
746 747 748 749 750
{
    ParserError error;
    JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
    DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
    ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
751
    UnlinkedEvalCodeBlock* unlinkedCode = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
752 753 754 755 756

    if (hasDebugger())
        debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);

    if (error.m_type != ParserError::ErrorNone) {
757
        throwVMError(callFrame, error.toErrorObject(this, executable->source()));
758 759 760 761 762 763
        return 0;
    }

    return unlinkedCode;
}

764
} // namespace JSC