JSGlobalObject.h 24.5 KB
Newer Older
1 2
/*
 *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3
 *  Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

weinig@apple.com's avatar
weinig@apple.com committed
22 23
#ifndef JSGlobalObject_h
#define JSGlobalObject_h
24

25
#include "ArrayAllocationProfile.h"
26
#include "JSArray.h"
ap@webkit.org's avatar
ap@webkit.org committed
27
#include "JSGlobalData.h"
28
#include "JSSegmentedVariableObject.h"
29
#include "JSWeakObjectMapRefInternal.h"
30
#include "NumberPrototype.h"
31
#include "SpecialPointer.h"
32
#include "StringPrototype.h"
33
#include "StructureChain.h"
34
#include "StructureRareDataInlines.h"
35
#include "Watchpoint.h"
36 37
#include <wtf/HashSet.h>
#include <wtf/OwnPtr.h>
38
#include <wtf/RandomNumber.h>
39

40
namespace JSC {
ggaren@apple.com's avatar
ggaren@apple.com committed
41

ggaren@apple.com's avatar
ggaren@apple.com committed
42 43 44 45
    class ArrayPrototype;
    class BooleanPrototype;
    class DatePrototype;
    class Debugger;
darin@apple.com's avatar
darin@apple.com committed
46
    class ErrorConstructor;
arv@chromium.org's avatar
arv@chromium.org committed
47
    class ErrorPrototype;
48 49 50 51
    class EvalCodeBlock;
    class EvalExecutable;
    class FunctionCodeBlock;
    class FunctionExecutable;
ggaren@apple.com's avatar
ggaren@apple.com committed
52
    class FunctionPrototype;
53
    class GetterSetter;
54
    class GlobalCodeBlock;
55
    class JSStack;
56
    class LLIntOffsetsExtractor;
darin@apple.com's avatar
darin@apple.com committed
57
    class NativeErrorConstructor;
58
    class ProgramCodeBlock;
59
    class ProgramExecutable;
darin@apple.com's avatar
darin@apple.com committed
60
    class RegExpConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
61
    class RegExpPrototype;
62
    class SourceCode;
63
    struct ActivationStackNode;
darin@apple.com's avatar
darin@apple.com committed
64
    struct HashTable;
ggaren@apple.com's avatar
ggaren@apple.com committed
65

weinig@apple.com's avatar
weinig@apple.com committed
66
    typedef Vector<ExecState*, 16> ExecStateStack;
67
    
68
    struct GlobalObjectMethodTable {
69 70 71
        typedef bool (*AllowsAccessFromFunctionPtr)(const JSGlobalObject*, ExecState*);
        AllowsAccessFromFunctionPtr allowsAccessFrom;

72 73 74 75 76 77 78 79
        typedef bool (*SupportsProfilingFunctionPtr)(const JSGlobalObject*); 
        SupportsProfilingFunctionPtr supportsProfiling;

        typedef bool (*SupportsRichSourceInfoFunctionPtr)(const JSGlobalObject*);
        SupportsRichSourceInfoFunctionPtr supportsRichSourceInfo;

        typedef bool (*ShouldInterruptScriptFunctionPtr)(const JSGlobalObject*);
        ShouldInterruptScriptFunctionPtr shouldInterruptScript;
80 81 82

        typedef bool (*JavaScriptExperimentsEnabledFunctionPtr)(const JSGlobalObject*);
        JavaScriptExperimentsEnabledFunctionPtr javaScriptExperimentsEnabled;
83 84
    };

85
    class JSGlobalObject : public JSSegmentedVariableObject {
86
    private:
87
        typedef HashSet<RefPtr<OpaqueJSWeakObjectMap> > WeakMapSet;
ggaren@apple.com's avatar
ggaren@apple.com committed
88

89 90 91 92 93 94 95 96 97 98 99 100
        struct JSGlobalObjectRareData {
            JSGlobalObjectRareData()
                : profileGroup(0)
            {
            }

            WeakMapSet weakMaps;
            unsigned profileGroup;
        };

    protected:

101
        Register m_globalCallFrame[JSStack::CallFrameHeaderSize];
102

103
        WriteBarrier<JSObject> m_globalThis;
104 105 106 107 108 109 110 111 112 113 114 115 116

        WriteBarrier<RegExpConstructor> m_regExpConstructor;
        WriteBarrier<ErrorConstructor> m_errorConstructor;
        WriteBarrier<NativeErrorConstructor> m_evalErrorConstructor;
        WriteBarrier<NativeErrorConstructor> m_rangeErrorConstructor;
        WriteBarrier<NativeErrorConstructor> m_referenceErrorConstructor;
        WriteBarrier<NativeErrorConstructor> m_syntaxErrorConstructor;
        WriteBarrier<NativeErrorConstructor> m_typeErrorConstructor;
        WriteBarrier<NativeErrorConstructor> m_URIErrorConstructor;

        WriteBarrier<JSFunction> m_evalFunction;
        WriteBarrier<JSFunction> m_callFunction;
        WriteBarrier<JSFunction> m_applyFunction;
117
        WriteBarrier<GetterSetter> m_throwTypeErrorGetterSetter;
118 119 120 121 122 123 124 125 126

        WriteBarrier<ObjectPrototype> m_objectPrototype;
        WriteBarrier<FunctionPrototype> m_functionPrototype;
        WriteBarrier<ArrayPrototype> m_arrayPrototype;
        WriteBarrier<BooleanPrototype> m_booleanPrototype;
        WriteBarrier<StringPrototype> m_stringPrototype;
        WriteBarrier<NumberPrototype> m_numberPrototype;
        WriteBarrier<DatePrototype> m_datePrototype;
        WriteBarrier<RegExpPrototype> m_regExpPrototype;
arv@chromium.org's avatar
arv@chromium.org committed
127
        WriteBarrier<ErrorPrototype> m_errorPrototype;
128

129 130 131 132
        WriteBarrier<Structure> m_withScopeStructure;
        WriteBarrier<Structure> m_strictEvalActivationStructure;
        WriteBarrier<Structure> m_activationStructure;
        WriteBarrier<Structure> m_nameScopeStructure;
133
        WriteBarrier<Structure> m_argumentsStructure;
134 135 136 137 138 139
        
        // Lists the actual structures used for having these particular indexing shapes.
        WriteBarrier<Structure> m_originalArrayStructureForIndexingShape[NumberOfIndexingShapes];
        // Lists the structures we should use during allocation for these particular indexing shapes.
        WriteBarrier<Structure> m_arrayStructureForIndexingShapeDuringAllocation[NumberOfIndexingShapes];
        
140 141 142 143 144
        WriteBarrier<Structure> m_booleanObjectStructure;
        WriteBarrier<Structure> m_callbackConstructorStructure;
        WriteBarrier<Structure> m_callbackFunctionStructure;
        WriteBarrier<Structure> m_callbackObjectStructure;
        WriteBarrier<Structure> m_dateStructure;
145
        WriteBarrier<Structure> m_nullPrototypeObjectStructure;
146 147
        WriteBarrier<Structure> m_errorStructure;
        WriteBarrier<Structure> m_functionStructure;
148
        WriteBarrier<Structure> m_boundFunctionStructure;
149
        WriteBarrier<Structure> m_namedFunctionStructure;
150
        PropertyOffset m_functionNameOffset;
151
        WriteBarrier<Structure> m_numberObjectStructure;
152
        WriteBarrier<Structure> m_privateNameStructure;
153 154 155 156
        WriteBarrier<Structure> m_regExpMatchesArrayStructure;
        WriteBarrier<Structure> m_regExpStructure;
        WriteBarrier<Structure> m_stringObjectStructure;
        WriteBarrier<Structure> m_internalFunctionStructure;
157 158
        
        void* m_specialPointers[Special::TableSize]; // Special pointers used by the LLInt and JIT.
159

160
        Debugger* m_debugger;
161

162
        RefPtr<WatchpointSet> m_masqueradesAsUndefinedWatchpoint;
163
        RefPtr<WatchpointSet> m_havingABadTimeWatchpoint;
164

165
        OwnPtr<JSGlobalObjectRareData> m_rareData;
166

167
        WeakRandom m_weakRandom;
ggaren@apple.com's avatar
ggaren@apple.com committed
168

169
        bool m_evalEnabled;
170
        String m_evalDisabledErrorMessage;
171
        bool m_experimentsEnabled;
172

173 174 175
        static JS_EXPORTDATA const GlobalObjectMethodTable s_globalObjectMethodTable;
        const GlobalObjectMethodTable* m_globalObjectMethodTable;

176 177
        void createRareDataIfNeeded()
        {
178 179 180
            if (m_rareData)
                return;
            m_rareData = adoptPtr(new JSGlobalObjectRareData);
181 182
        }
        
ggaren@apple.com's avatar
ggaren@apple.com committed
183
    public:
184 185
        typedef JSSegmentedVariableObject Base;

186 187
        static JSGlobalObject* create(JSGlobalData& globalData, Structure* structure)
        {
188
            JSGlobalObject* globalObject = new (NotNull, allocateCell<JSGlobalObject>(globalData.heap)) JSGlobalObject(globalData, structure);
189
            globalObject->finishCreation(globalData);
190
            globalData.heap.addFinalizer(globalObject, destroy);
191
            return globalObject;
192 193 194 195
        }

        static JS_EXPORTDATA const ClassInfo s_info;

196 197 198
        bool hasDebugger() const { return m_debugger; }
        bool hasProfiler() const { return globalObjectMethodTable()->supportsProfiling(this); }

199 200
        void* m_apiData;

201
    protected:
202
        JS_EXPORT_PRIVATE explicit JSGlobalObject(JSGlobalData&, Structure*, const GlobalObjectMethodTable* = 0);
203

204 205 206 207
        void finishCreation(JSGlobalData& globalData)
        {
            Base::finishCreation(globalData);
            structure()->setGlobalObject(globalData, this);
208
            m_experimentsEnabled = m_globalObjectMethodTable->javaScriptExperimentsEnabled(this);
209 210 211
            init(this);
        }

212
        void finishCreation(JSGlobalData& globalData, JSObject* thisValue)
ggaren@apple.com's avatar
ggaren@apple.com committed
213
        {
214
            Base::finishCreation(globalData);
215
            structure()->setGlobalObject(globalData, this);
216
            m_experimentsEnabled = m_globalObjectMethodTable->javaScriptExperimentsEnabled(this);
darin@apple.com's avatar
darin@apple.com committed
217
            init(thisValue);
ggaren@apple.com's avatar
ggaren@apple.com committed
218 219
        }

220
    public:
221 222
        JS_EXPORT_PRIVATE ~JSGlobalObject();
        JS_EXPORT_PRIVATE static void destroy(JSCell*);
223 224
        // We don't need a destructor because we use a finalizer instead.
        static const bool needsDestruction = false;
ggaren@apple.com's avatar
ggaren@apple.com committed
225

226
        JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
227

228 229 230 231
        JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
        JS_EXPORT_PRIVATE static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
        bool hasOwnPropertyForWrite(ExecState*, PropertyName);
        JS_EXPORT_PRIVATE static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
232

233
        JS_EXPORT_PRIVATE static void putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned attributes);
ggaren@apple.com's avatar
ggaren@apple.com committed
234

235 236 237
        JS_EXPORT_PRIVATE static void defineGetter(JSObject*, ExecState*, PropertyName, JSObject* getterFunc, unsigned attributes);
        JS_EXPORT_PRIVATE static void defineSetter(JSObject*, ExecState*, PropertyName, JSObject* setterFunc, unsigned attributes);
        JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
238

239 240
        // We use this in the code generator as we perform symbol table
        // lookups prior to initializing the properties
241
        bool symbolTableHasProperty(PropertyName);
242

ggaren@apple.com's avatar
ggaren@apple.com committed
243 244 245
        // The following accessors return pristine values, even if a script 
        // replaces the global object's associated property.

246 247 248 249 250 251 252 253 254 255 256 257 258
        RegExpConstructor* regExpConstructor() const { return m_regExpConstructor.get(); }

        ErrorConstructor* errorConstructor() const { return m_errorConstructor.get(); }
        NativeErrorConstructor* evalErrorConstructor() const { return m_evalErrorConstructor.get(); }
        NativeErrorConstructor* rangeErrorConstructor() const { return m_rangeErrorConstructor.get(); }
        NativeErrorConstructor* referenceErrorConstructor() const { return m_referenceErrorConstructor.get(); }
        NativeErrorConstructor* syntaxErrorConstructor() const { return m_syntaxErrorConstructor.get(); }
        NativeErrorConstructor* typeErrorConstructor() const { return m_typeErrorConstructor.get(); }
        NativeErrorConstructor* URIErrorConstructor() const { return m_URIErrorConstructor.get(); }

        JSFunction* evalFunction() const { return m_evalFunction.get(); }
        JSFunction* callFunction() const { return m_callFunction.get(); }
        JSFunction* applyFunction() const { return m_applyFunction.get(); }
259 260 261 262 263 264
        GetterSetter* throwTypeErrorGetterSetter(ExecState* exec)
        {
            if (!m_throwTypeErrorGetterSetter)
                createThrowTypeError(exec);
            return m_throwTypeErrorGetterSetter.get();
        }
265 266 267 268 269 270 271 272 273

        ObjectPrototype* objectPrototype() const { return m_objectPrototype.get(); }
        FunctionPrototype* functionPrototype() const { return m_functionPrototype.get(); }
        ArrayPrototype* arrayPrototype() const { return m_arrayPrototype.get(); }
        BooleanPrototype* booleanPrototype() const { return m_booleanPrototype.get(); }
        StringPrototype* stringPrototype() const { return m_stringPrototype.get(); }
        NumberPrototype* numberPrototype() const { return m_numberPrototype.get(); }
        DatePrototype* datePrototype() const { return m_datePrototype.get(); }
        RegExpPrototype* regExpPrototype() const { return m_regExpPrototype.get(); }
arv@chromium.org's avatar
arv@chromium.org committed
274
        ErrorPrototype* errorPrototype() const { return m_errorPrototype.get(); }
275

276 277 278 279
        Structure* withScopeStructure() const { return m_withScopeStructure.get(); }
        Structure* strictEvalActivationStructure() const { return m_strictEvalActivationStructure.get(); }
        Structure* activationStructure() const { return m_activationStructure.get(); }
        Structure* nameScopeStructure() const { return m_nameScopeStructure.get(); }
280
        Structure* argumentsStructure() const { return m_argumentsStructure.get(); }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        Structure* originalArrayStructureForIndexingType(IndexingType indexingType) const
        {
            ASSERT(indexingType & IsArray);
            return m_originalArrayStructureForIndexingShape[(indexingType & IndexingShapeMask) >> IndexingShapeShift].get();
        }
        Structure* arrayStructureForIndexingTypeDuringAllocation(IndexingType indexingType) const
        {
            ASSERT(indexingType & IsArray);
            return m_arrayStructureForIndexingShapeDuringAllocation[(indexingType & IndexingShapeMask) >> IndexingShapeShift].get();
        }
        Structure* arrayStructureForProfileDuringAllocation(ArrayAllocationProfile* profile) const
        {
            return arrayStructureForIndexingTypeDuringAllocation(ArrayAllocationProfile::selectIndexingTypeFor(profile));
        }
        
296 297
        bool isOriginalArrayStructure(Structure* structure)
        {
298
            return originalArrayStructureForIndexingType(structure->indexingType() | IsArray) == structure;
299
        }
300
        
301 302 303 304 305
        Structure* booleanObjectStructure() const { return m_booleanObjectStructure.get(); }
        Structure* callbackConstructorStructure() const { return m_callbackConstructorStructure.get(); }
        Structure* callbackFunctionStructure() const { return m_callbackFunctionStructure.get(); }
        Structure* callbackObjectStructure() const { return m_callbackObjectStructure.get(); }
        Structure* dateStructure() const { return m_dateStructure.get(); }
306
        Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
307 308
        Structure* errorStructure() const { return m_errorStructure.get(); }
        Structure* functionStructure() const { return m_functionStructure.get(); }
309
        Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(); }
310
        Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
311
        PropertyOffset functionNameOffset() const { return m_functionNameOffset; }
312
        Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
313
        Structure* privateNameStructure() const { return m_privateNameStructure.get(); }
314 315 316 317 318
        Structure* internalFunctionStructure() const { return m_internalFunctionStructure.get(); }
        Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
        Structure* regExpStructure() const { return m_regExpStructure.get(); }
        Structure* stringObjectStructure() const { return m_stringObjectStructure.get(); }

319 320 321 322 323 324
        void* actualPointerFor(Special::Pointer pointer)
        {
            ASSERT(pointer < Special::TableSize);
            return m_specialPointers[pointer];
        }

325
        WatchpointSet* masqueradesAsUndefinedWatchpoint() { return m_masqueradesAsUndefinedWatchpoint.get(); }
326 327 328 329 330 331 332 333
        WatchpointSet* havingABadTimeWatchpoint() { return m_havingABadTimeWatchpoint.get(); }
        
        bool isHavingABadTime() const
        {
            return m_havingABadTimeWatchpoint->hasBeenInvalidated();
        }
        
        void haveABadTime(JSGlobalData&);
334 335
        
        bool arrayPrototypeChainIsSane();
336

337 338 339 340 341 342 343
        void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
        unsigned profileGroup() const
        { 
            if (!m_rareData)
                return 0;
            return m_rareData->profileGroup;
        }
344 345 346

        Debugger* debugger() const { return m_debugger; }
        void setDebugger(Debugger* debugger) { m_debugger = debugger; }
347

348 349
        const GlobalObjectMethodTable* globalObjectMethodTable() const { return m_globalObjectMethodTable; }

350
        static bool allowsAccessFrom(const JSGlobalObject*, ExecState*) { return true; }
351 352
        static bool supportsProfiling(const JSGlobalObject*) { return false; }
        static bool supportsRichSourceInfo(const JSGlobalObject*) { return true; }
353

354
        JS_EXPORT_PRIVATE ExecState* globalExec();
ggaren@apple.com's avatar
ggaren@apple.com committed
355

356
        static bool shouldInterruptScript(const JSGlobalObject*) { return true; }
357
        static bool javaScriptExperimentsEnabled(const JSGlobalObject*) { return false; }
ggaren@apple.com's avatar
ggaren@apple.com committed
358

359
        bool isDynamicScope(bool& requiresDynamicChecks) const;
360

361 362 363 364 365 366 367
        bool evalEnabled() const { return m_evalEnabled; }
        const String& evalDisabledErrorMessage() const { return m_evalDisabledErrorMessage; }
        void setEvalEnabled(bool enabled, const String& errorMessage = String())
        {
            m_evalEnabled = enabled;
            m_evalDisabledErrorMessage = errorMessage;
        }
368

369
        void resetPrototype(JSGlobalData&, JSValue prototype);
370

371
        JSGlobalData& globalData() const { return *Heap::heap(this)->globalData(); }
372
        JSObject* globalThis() const;
ggaren@apple.com's avatar
ggaren@apple.com committed
373

374
        static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
darin@apple.com's avatar
darin@apple.com committed
375
        {
376
            return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
darin@apple.com's avatar
darin@apple.com committed
377 378
        }

379 380
        void registerWeakMap(OpaqueJSWeakObjectMap* map)
        {
381 382
            createRareDataIfNeeded();
            m_rareData->weakMaps.add(map);
383 384
        }

385
        void unregisterWeakMap(OpaqueJSWeakObjectMap* map)
386
        {
387 388
            if (m_rareData)
                m_rareData->weakMaps.remove(map);
389 390
        }

391
        double weakRandomNumber() { return m_weakRandom.get(); }
392
        unsigned weakRandomInteger() { return m_weakRandom.getUint32(); }
393 394 395 396

        UnlinkedProgramCodeBlock* createProgramCodeBlock(CallFrame*, ProgramExecutable*, JSObject** exception);
        UnlinkedEvalCodeBlock* createEvalCodeBlock(CallFrame*, EvalExecutable*, JSObject** exception);

mjs@apple.com's avatar
mjs@apple.com committed
397
    protected:
398

399
        static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
400

401
        struct GlobalPropertyInfo {
ggaren@apple.com's avatar
ggaren@apple.com committed
402
            GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
403 404 405 406 407 408
                : identifier(i)
                , value(v)
                , attributes(a)
            {
            }

409
            const Identifier identifier;
ggaren@apple.com's avatar
ggaren@apple.com committed
410
            JSValue value;
411 412
            unsigned attributes;
        };
413
        JS_EXPORT_PRIVATE void addStaticGlobals(GlobalPropertyInfo*, int count);
414

415 416
        JS_EXPORT_PRIVATE static JSC::JSObject* toThisObject(JSC::JSCell*, JSC::ExecState*);

417 418
        JS_EXPORT_PRIVATE void setGlobalThis(JSGlobalData&, JSObject* globalThis);

ap@webkit.org's avatar
ap@webkit.org committed
419
    private:
420 421
        friend class LLIntOffsetsExtractor;
        
darin@apple.com's avatar
darin@apple.com committed
422
        // FIXME: Fold reset into init.
423
        JS_EXPORT_PRIVATE void init(JSObject* thisValue);
ggaren@apple.com's avatar
ggaren@apple.com committed
424
        void reset(JSValue prototype);
weinig@apple.com's avatar
weinig@apple.com committed
425

426 427
        void createThrowTypeError(ExecState*);

428
        JS_EXPORT_PRIVATE static void clearRareData(JSCell*);
429
    };
ggaren@apple.com's avatar
ggaren@apple.com committed
430

ggaren@apple.com's avatar
ggaren@apple.com committed
431
    JSGlobalObject* asGlobalObject(JSValue);
darin@apple.com's avatar
darin@apple.com committed
432

ggaren@apple.com's avatar
ggaren@apple.com committed
433
    inline JSGlobalObject* asGlobalObject(JSValue value)
darin@apple.com's avatar
darin@apple.com committed
434 435
    {
        ASSERT(asObject(value)->isGlobalObject());
436
        return jsCast<JSGlobalObject*>(asObject(value));
darin@apple.com's avatar
darin@apple.com committed
437 438
    }

439
    inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, PropertyName propertyName)
440
    {
barraclough@apple.com's avatar
barraclough@apple.com committed
441
        PropertySlot slot;
442
        if (Base::getOwnPropertySlot(this, exec, propertyName, slot))
443
            return true;
barraclough@apple.com's avatar
barraclough@apple.com committed
444
        bool slotIsWriteable;
445
        return symbolTableGet(this, propertyName, slot, slotIsWriteable);
446 447
    }

448
    inline bool JSGlobalObject::symbolTableHasProperty(PropertyName propertyName)
449
    {
450
        SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
451 452 453
        return !entry.isNull();
    }

454
    inline JSValue Structure::prototypeForLookup(JSGlobalObject* globalObject) const
455
    {
456
        if (isObject())
457
            return m_prototype.get();
458

459
        ASSERT(typeInfo().type() == StringType);
460 461 462 463 464 465
        return globalObject->stringPrototype();
    }

    inline JSValue Structure::prototypeForLookup(ExecState* exec) const
    {
        return prototypeForLookup(exec->lexicalGlobalObject());
466 467
    }

468
    inline StructureChain* Structure::prototypeChain(JSGlobalData& globalData, JSGlobalObject* globalObject) const
ggaren@apple.com's avatar
ggaren@apple.com committed
469 470
    {
        // We cache our prototype chain so our clients can share it.
471 472 473
        if (!isValid(globalObject, m_cachedPrototypeChain.get())) {
            JSValue prototype = prototypeForLookup(globalObject);
            m_cachedPrototypeChain.set(globalData, this, StructureChain::create(globalData, prototype.isNull() ? 0 : asObject(prototype)->structure()));
ggaren@apple.com's avatar
ggaren@apple.com committed
474 475 476 477
        }
        return m_cachedPrototypeChain.get();
    }

478 479 480 481 482 483
    inline StructureChain* Structure::prototypeChain(ExecState* exec) const
    {
        return prototypeChain(exec->globalData(), exec->lexicalGlobalObject());
    }

    inline bool Structure::isValid(JSGlobalObject* globalObject, StructureChain* cachedPrototypeChain) const
ggaren@apple.com's avatar
ggaren@apple.com committed
484 485 486 487
    {
        if (!cachedPrototypeChain)
            return false;

488
        JSValue prototype = prototypeForLookup(globalObject);
489
        WriteBarrier<Structure>* cachedStructure = cachedPrototypeChain->head();
ggaren@apple.com's avatar
ggaren@apple.com committed
490
        while(*cachedStructure && !prototype.isNull()) {
491
            if (asObject(prototype)->structure() != cachedStructure->get())
ggaren@apple.com's avatar
ggaren@apple.com committed
492 493 494 495 496 497 498
                return false;
            ++cachedStructure;
            prototype = asObject(prototype)->prototype();
        }
        return prototype.isNull() && !*cachedStructure;
    }

499 500 501 502 503
    inline bool Structure::isValid(ExecState* exec, StructureChain* cachedPrototypeChain) const
    {
        return isValid(exec->lexicalGlobalObject(), cachedPrototypeChain);
    }

504 505 506 507 508 509 510 511 512 513
    inline JSGlobalObject* ExecState::dynamicGlobalObject()
    {
        if (this == lexicalGlobalObject()->globalExec())
            return lexicalGlobalObject();

        // For any ExecState that's not a globalExec, the 
        // dynamic global object must be set since code is running
        ASSERT(globalData().dynamicGlobalObject);
        return globalData().dynamicGlobalObject;
    }
514

515
    inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, unsigned initialLength = 0)
516
    {
517
        return ArrayAllocationProfile::updateLastAllocationFor(profile, JSArray::create(exec->globalData(), initialLength >= MIN_SPARSE_ARRAY_INDEX ? globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithArrayStorage) : globalObject->arrayStructureForProfileDuringAllocation(profile), initialLength));
518
    }
519

520
    inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, unsigned initialLength = 0)
521
    {
522
        return constructEmptyArray(exec, profile, exec->lexicalGlobalObject(), initialLength);
523
    }
524
 
525
    inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const ArgList& values)
526
    {
527
        return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, globalObject->arrayStructureForProfileDuringAllocation(profile), values));
528 529
    }

530
    inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const ArgList& values)
531
    {
532
        return constructArray(exec, profile, exec->lexicalGlobalObject(), values);
533 534
    }

535
    inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const JSValue* values, unsigned length)
536
    {
537
        return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, globalObject->arrayStructureForProfileDuringAllocation(profile), values, length));
538 539
    }

540
    inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, const JSValue* values, unsigned length)
541
    {
542
        return constructArray(exec, profile, exec->lexicalGlobalObject(), values, length);
543 544
    }

545 546
    class DynamicGlobalObjectScope {
        WTF_MAKE_NONCOPYABLE(DynamicGlobalObjectScope);
547
    public:
548
        JS_EXPORT_PRIVATE DynamicGlobalObjectScope(JSGlobalData&, JSGlobalObject*);
549 550 551 552 553 554 555 556 557 558 559

        ~DynamicGlobalObjectScope()
        {
            m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
        }

    private:
        JSGlobalObject*& m_dynamicGlobalObjectSlot;
        JSGlobalObject* m_savedDynamicGlobalObject;
    };

560 561 562 563 564
    inline bool JSGlobalObject::isDynamicScope(bool&) const
    {
        return true;
    }

565 566 567 568 569 570 571 572 573 574
    inline JSObject* JSScope::globalThis()
    { 
        return globalObject()->globalThis();
    }

    inline JSObject* JSGlobalObject::globalThis() const
    { 
        return m_globalThis.get();
    }

575
} // namespace JSC
576

weinig@apple.com's avatar
weinig@apple.com committed
577
#endif // JSGlobalObject_h