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

106 107 108 109 110 111 112 113 114 115
#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)

116 117
#include "JSGlobalObject.lut.h"

118
namespace JSC {
ggaren@apple.com's avatar
ggaren@apple.com committed
119

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

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

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* 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
139 140
JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
    : 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 146 147 148 149
    , m_evalEnabled(true)
    , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
{
}

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

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

159 160
void JSGlobalObject::destroy(JSCell* cell)
{
161
    static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
162 163
}

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

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

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

176
    m_debugger = 0;
177

ggaren@apple.com's avatar
ggaren@apple.com committed
178 179 180
    reset(prototype());
}

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

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

191
bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
192
{
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))
197 198
        return false;
    return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
199 200
}

201 202 203 204 205 206 207 208
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);
209 210 211
    if (result.isNewEntry)
        addRegisters(1);
    else {
212 213 214 215 216
        result.iterator->value.notifyWrite();
        index = result.iterator->value.getIndex();
    }
    return index;
}
217

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

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

231
    m_functionPrototype.set(vm, this, FunctionPrototype::create(vm, FunctionPrototype::createStructure(vm, this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
232 233 234 235
    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()));
236 237
    JSFunction* callFunction = 0;
    JSFunction* applyFunction = 0;
238
    m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
239 240 241 242
    m_callFunction.set(vm, this, callFunction);
    m_applyFunction.set(vm, this, applyFunction);
    m_objectPrototype.set(vm, this, ObjectPrototype::create(exec, this, ObjectPrototype::createStructure(vm, this, jsNull())));
    GetterSetter* protoAccessor = GetterSetter::create(vm);
243 244 245
    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);
246
    m_functionPrototype->structure()->setPrototypeWithoutTransition(vm, m_objectPrototype.get());
247
    
248 249 250 251 252 253 254 255 256 257
    m_typedArrays[toIndex(TypeInt8)].prototype.set(vm, this, JSInt8ArrayPrototype::create(exec, this, JSInt8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeInt16)].prototype.set(vm, this, JSInt16ArrayPrototype::create(exec, this, JSInt16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeInt32)].prototype.set(vm, this, JSInt32ArrayPrototype::create(exec, this, JSInt32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint8)].prototype.set(vm, this, JSUint8ArrayPrototype::create(exec, this, JSUint8ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint8Clamped)].prototype.set(vm, this, JSUint8ClampedArrayPrototype::create(exec, this, JSUint8ClampedArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint16)].prototype.set(vm, this, JSUint16ArrayPrototype::create(exec, this, JSUint16ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeUint32)].prototype.set(vm, this, JSUint32ArrayPrototype::create(exec, this, JSUint32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeFloat32)].prototype.set(vm, this, JSFloat32ArrayPrototype::create(exec, this, JSFloat32ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeFloat64)].prototype.set(vm, this, JSFloat64ArrayPrototype::create(exec, this, JSFloat64ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
    m_typedArrays[toIndex(TypeDataView)].prototype.set(vm, this, JSDataViewPrototype::create(vm, JSDataViewPrototype::createStructure(vm, this, m_objectPrototype.get())));
258
    
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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()));
281
#if JSC_OBJC_API_ENABLED
282 283
    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()));
284
#endif
285

286
    m_arrayPrototype.set(vm, this, ArrayPrototype::create(vm, ArrayPrototype::createStructure(vm, this, m_objectPrototype.get())));
287
    
288 289 290 291 292 293
    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));
294 295 296
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
        m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
    
297
    m_regExpMatchesArrayStructure.set(vm, this, RegExpMatchesArray::createStructure(vm, this, m_arrayPrototype.get()));
298

299
    RegExp* emptyRegex = RegExp::create(vm, "", NoFlags);
300
    
301 302
    m_regExpPrototype.set(vm, this, RegExpPrototype::create(exec, this, RegExpPrototype::createStructure(vm, this, m_objectPrototype.get()), emptyRegex));
    m_regExpStructure.set(vm, this, RegExpObject::createStructure(vm, this, m_regExpPrototype.get()));
303

304
#if ENABLE(PROMISES)
305 306
    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
307

308 309 310 311
    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()));
312 313
#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()));
317 318 319 320

    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
321

ggaren@apple.com's avatar
ggaren@apple.com committed
322
    // Constructors
darin@apple.com's avatar
darin@apple.com committed
323

324 325 326
    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());
327 328

#if ENABLE(PROMISES)
329 330
    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());
331
#endif // ENABLE(PROMISES)
ggaren@apple.com's avatar
ggaren@apple.com committed
332

333
    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
334

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

    FOR_EACH_SIMPLE_BUILTIN_TYPE(CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE)

#undef CREATE_CONSTRUCTOR_FOR_SIMPLE_TYPE

343 344 345 346
    m_errorConstructor.set(vm, this, errorConstructor);

    Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(vm, this, m_errorPrototype.get());
    Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(vm, this, m_functionPrototype.get());
347 348 349 350 351 352
    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")));
353 354 355 356 357

    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);
358
#if ENABLE(PROMISES)
359 360
    m_promisePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseConstructor, DontEnum);
    m_promiseResolverPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, promiseResolverConstructor, DontEnum);
361
#endif
ggaren@apple.com's avatar
ggaren@apple.com committed
362

363 364 365 366 367 368 369 370 371 372
    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);
373
#if ENABLE(PROMISES)
374 375
    putDirectWithoutTransition(vm, vm.propertyNames->Promise, promiseConstructor, DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->PromiseResolver, promiseResolverConstructor, DontEnum);
376
#endif
377 378 379


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

    FOR_EACH_SIMPLE_BUILTIN_TYPE(PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE)

#undef PUT_CONSTRUCTOR_FOR_SIMPLE_TYPE

ggaren@apple.com's avatar
ggaren@apple.com committed
386

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);
ggaren@apple.com's avatar
ggaren@apple.com committed
389

390 391
    putDirectWithoutTransition(vm, vm.propertyNames->JSON, JSONObject::create(exec, this, JSONObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
    putDirectWithoutTransition(vm, vm.propertyNames->Math, MathObject::create(exec, this, MathObject::createStructure(vm, this, m_objectPrototype.get())), DontEnum);
392 393
    
    FixedArray<InternalFunction*, NUMBER_OF_TYPED_ARRAY_TYPES> typedArrayConstructors;
394 395 396 397 398 399 400 401 402 403
    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");
404

405
    for (unsigned typedArrayIndex = NUMBER_OF_TYPED_ARRAY_TYPES; typedArrayIndex--;) {
406 407
        m_typedArrays[typedArrayIndex].prototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, typedArrayConstructors[typedArrayIndex], DontEnum);
        putDirectWithoutTransition(vm, Identifier(exec, typedArrayConstructors[typedArrayIndex]->name(exec)), typedArrayConstructors[typedArrayIndex], DontEnum);
408
    }
409

410
    GlobalPropertyInfo staticGlobals[] = {
411 412 413
        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)
414
    };
415
    addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
416 417 418
    
    m_specialPointers[Special::CallFunction] = m_callFunction.get();
    m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
419 420
    m_specialPointers[Special::ObjectConstructor] = objectConstructor;
    m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
421

422
    if (m_experimentsEnabled) {
423 424
        NamePrototype* privateNamePrototype = NamePrototype::create(exec, NamePrototype::createStructure(vm, this, m_objectPrototype.get()));
        m_privateNameStructure.set(vm, this, NameInstance::createStructure(vm, this, privateNamePrototype));
425

426
        JSCell* privateNameConstructor = NameConstructor::create(vm, NameConstructor::createStructure(vm, this, m_functionPrototype.get()), privateNamePrototype);
427 428
        privateNamePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, privateNameConstructor, DontEnum);
        putDirectWithoutTransition(vm, Identifier(exec, "Name"), privateNameConstructor, DontEnum);
429 430
    }

431
    resetPrototype(vm, prototype);
weinig@apple.com's avatar
weinig@apple.com committed
432
}
ggaren@apple.com's avatar
ggaren@apple.com committed
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
// 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.
457
    IndexingType type = object->structure()->indexingType();
458 459 460
    // 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);
461 462 463 464 465 466 467 468
}

void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
{
    if (!cell->isObject())
        return;
    
    JSObject* object = asObject(cell);
469 470 471

    // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
    if (!hasBrokenIndexing(object))
472 473
        return;
    
474 475
    // 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
476
    // a different global object that have prototypes from our global object.
477 478
    bool foundGlobalObject = false;
    for (JSObject* current = object; ;) {
479
        if (current->globalObject() == m_globalObject) {
480 481 482 483 484 485 486 487 488 489
            foundGlobalObject = true;
            break;
        }
        
        JSValue prototypeValue = current->prototype();
        if (prototypeValue.isNull())
            break;
        current = asObject(prototypeValue);
    }
    if (!foundGlobalObject)
490 491 492 493 494 495 496
        return;
    
    m_foundObjects.append(object);
}

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

ggaren@apple.com's avatar
ggaren@apple.com committed
497
void JSGlobalObject::haveABadTime(VM& vm)
498
{
ggaren@apple.com's avatar
ggaren@apple.com committed
499
    ASSERT(&vm == &this->vm());
500 501 502 503 504 505 506 507 508 509 510 511
    
    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.
512
    for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
ggaren@apple.com's avatar
ggaren@apple.com committed
513
        m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
514 515 516 517 518
    
    // 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);
519 520 521 522
    {
        HeapIterationScope iterationScope(vm.heap);
        vm.heap.objectSpace().forEachLiveCell(iterationScope, finder);
    }
523 524 525 526
    while (!foundObjects.isEmpty()) {
        JSObject* object = asObject(foundObjects.last());
        foundObjects.removeLast();
        ASSERT(hasBrokenIndexing(object));
ggaren@apple.com's avatar
ggaren@apple.com committed
527
        object->switchToSlowPutArrayStorage(vm);
528 529 530
    }
}

531 532 533 534 535 536
bool JSGlobalObject::objectPrototypeIsSane()
{
    return !hasIndexedProperties(m_objectPrototype->structure()->indexingType())
        && m_objectPrototype->prototype().isNull();
}

537 538 539 540
bool JSGlobalObject::arrayPrototypeChainIsSane()
{
    return !hasIndexedProperties(m_arrayPrototype->structure()->indexingType())
        && m_arrayPrototype->prototype() == m_objectPrototype.get()
541 542 543 544 545 546 547 548
        && objectPrototypeIsSane();
}

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

551
void JSGlobalObject::createThrowTypeError(VM& vm)
552
{
553
    JSFunction* thrower = JSFunction::create(vm, this, 0, String(), globalFuncThrowTypeError);
554 555 556 557
    GetterSetter* getterSetter = GetterSetter::create(vm);
    getterSetter->setGetter(vm, thrower);
    getterSetter->setSetter(vm, thrower);
    m_throwTypeErrorGetterSetter.set(vm, this, getterSetter);
558 559
}

weinig@apple.com's avatar
weinig@apple.com committed
560
// Set prototype, and also insert the object prototype at the end of the chain.
ggaren@apple.com's avatar
ggaren@apple.com committed
561
void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
weinig@apple.com's avatar
weinig@apple.com committed
562
{
ggaren@apple.com's avatar
ggaren@apple.com committed
563
    setPrototype(vm, prototype);
564 565

    JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
566
    JSObject* objectPrototype = m_objectPrototype.get();
567
    if (oldLastInPrototypeChain != objectPrototype)
ggaren@apple.com's avatar
ggaren@apple.com committed
568
        oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
ggaren@apple.com's avatar
ggaren@apple.com committed
569 570
}

571 572
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);
578

579
    visitor.append(&thisObject->m_globalThis);
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

    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);
599
#if ENABLE(PROMISES)
weinig@apple.com's avatar
weinig@apple.com committed
600 601
    visitor.append(&thisObject->m_promisePrototype);
    visitor.append(&thisObject->m_promiseResolverPrototype);
602
#endif
603

604 605 606 607
    visitor.append(&thisObject->m_withScopeStructure);
    visitor.append(&thisObject->m_strictEvalActivationStructure);
    visitor.append(&thisObject->m_activationStructure);
    visitor.append(&thisObject->m_nameScopeStructure);
608
    visitor.append(&thisObject->m_argumentsStructure);
609 610 611 612
    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]);
613 614 615 616
    visitor.append(&thisObject->m_booleanObjectStructure);
    visitor.append(&thisObject->m_callbackConstructorStructure);
    visitor.append(&thisObject->m_callbackFunctionStructure);
    visitor.append(&thisObject->m_callbackObjectStructure);
617 618
#if JSC_OBJC_API_ENABLED
    visitor.append(&thisObject->m_objcCallbackFunctionStructure);
619
    visitor.append(&thisObject->m_objcWrapperObjectStructure);
620
#endif
621 622 623 624 625 626 627 628 629
    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);
630 631

#if ENABLE(PROMISES)
weinig@apple.com's avatar
weinig@apple.com committed
632 633 634 635
    visitor.append(&thisObject->m_promiseStructure);
    visitor.append(&thisObject->m_promiseResolverStructure);
    visitor.append(&thisObject->m_promiseCallbackStructure);
    visitor.append(&thisObject->m_promiseWrapperCallbackStructure);
636
#endif // ENABLE(PROMISES)
637 638 639 640 641 642 643 644 645

#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

646 647 648 649
    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
650 651
}

652
JSValue JSGlobalObject::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
653
{
654 655 656
    if (ecmaMode == StrictMode)
        return jsUndefined();
    return exec->globalThisValue();
657 658
}

ggaren@apple.com's avatar
ggaren@apple.com committed
659 660
ExecState* JSGlobalObject::globalExec()
{
661
    return CallFrame::create(m_globalCallFrame);
ggaren@apple.com's avatar
ggaren@apple.com committed
662 663
}

664 665
void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
{
666
    addRegisters(count);
667

668
    for (int i = 0; i < count; ++i) {
669 670
        GlobalPropertyInfo& global = globals[i];
        ASSERT(global.attributes & DontDelete);
671
        
672
        int index = symbolTable()->size();
673
        SymbolTableEntry newEntry(index, global.attributes);
674
        symbolTable()->add(global.identifier.impl(), newEntry);
ggaren@apple.com's avatar
ggaren@apple.com committed
675
        registerAt(index).set(vm(), this, global.value);
676 677 678
    }
}

679
bool JSGlobalObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
680
{
681
    JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
682
    if (getStaticFunctionSlot<Base>(exec, ExecState::globalObjectTable(exec), thisObject, propertyName, slot))
683
        return true;
684
    return symbolTableGet(thisObject, propertyName, slot);
685 686
}

687
void JSGlobalObject::clearRareData(JSCell* cell)
688
{
689
    jsCast<JSGlobalObject*>(cell)->m_rareData.clear();
690 691
}

ggaren@apple.com's avatar
ggaren@apple.com committed
692 693
DynamicGlobalObjectScope::DynamicGlobalObjectScope(VM& vm, JSGlobalObject* dynamicGlobalObject)
    : m_dynamicGlobalObjectSlot(vm.dynamicGlobalObject)
694 695 696 697 698
    , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
{
    if (!m_dynamicGlobalObjectSlot) {
#if ENABLE(ASSEMBLER)
        if (ExecutableAllocator::underMemoryPressure())
ggaren@apple.com's avatar
ggaren@apple.com committed
699
            vm.heap.deleteAllCompiledCode();
700 701 702 703 704 705
#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
706
        vm.resetDateCache();
707
    }
708
    // Clear the exception stack between entries
709
    vm.clearExceptionStack();
710 711
}

712 713
void slowValidateCell(JSGlobalObject* globalObject)
{
714
    RELEASE_ASSERT(globalObject->isGlobalObject());
715
    ASSERT_GC_OBJECT_INHERITS(globalObject, JSGlobalObject::info());
716 717
}

718 719 720 721 722 723
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
724
    UnlinkedProgramCodeBlock* unlinkedCode = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
725 726 727 728 729 730 731 732 733 734 735 736

    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;
}

737
UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CallFrame* callFrame, EvalExecutable* executable)
738 739 740 741 742
{
    ParserError error;
    JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
    DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
    ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
743
    UnlinkedEvalCodeBlock* unlinkedCode = vm().codeCache()->getEvalCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
744 745 746 747 748

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

    if (error.m_type != ParserError::ErrorNone) {
749
        throwVMError(callFrame, error.toErrorObject(this, executable->source()));
750 751 752 753 754 755
        return 0;
    }

    return unlinkedCode;
}

756
} // namespace JSC