JSGlobalObject.h 24.6 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 "Watchpoint.h"
35 36
#include <wtf/HashSet.h>
#include <wtf/OwnPtr.h>
37
#include <wtf/RandomNumber.h>
38

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

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

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

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

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

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

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

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

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

            WeakMapSet weakMaps;
            unsigned profileGroup;
        };

    protected:

100
        Register m_globalCallFrame[JSStack::CallFrameHeaderSize];
101

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

        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;
116
        WriteBarrier<GetterSetter> m_throwTypeErrorGetterSetter;
117 118 119 120 121 122 123 124 125

        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
126
        WriteBarrier<ErrorPrototype> m_errorPrototype;
127

128 129 130 131
        WriteBarrier<Structure> m_withScopeStructure;
        WriteBarrier<Structure> m_strictEvalActivationStructure;
        WriteBarrier<Structure> m_activationStructure;
        WriteBarrier<Structure> m_nameScopeStructure;
132
        WriteBarrier<Structure> m_argumentsStructure;
133 134 135 136 137 138
        
        // 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];
        
139 140 141 142 143
        WriteBarrier<Structure> m_booleanObjectStructure;
        WriteBarrier<Structure> m_callbackConstructorStructure;
        WriteBarrier<Structure> m_callbackFunctionStructure;
        WriteBarrier<Structure> m_callbackObjectStructure;
        WriteBarrier<Structure> m_dateStructure;
144
        WriteBarrier<Structure> m_nullPrototypeObjectStructure;
145 146
        WriteBarrier<Structure> m_errorStructure;
        WriteBarrier<Structure> m_functionStructure;
147
        WriteBarrier<Structure> m_boundFunctionStructure;
148
        WriteBarrier<Structure> m_namedFunctionStructure;
149
        PropertyOffset m_functionNameOffset;
150
        WriteBarrier<Structure> m_numberObjectStructure;
151
        WriteBarrier<Structure> m_privateNameStructure;
152 153 154 155
        WriteBarrier<Structure> m_regExpMatchesArrayStructure;
        WriteBarrier<Structure> m_regExpStructure;
        WriteBarrier<Structure> m_stringObjectStructure;
        WriteBarrier<Structure> m_internalFunctionStructure;
156 157
        
        void* m_specialPointers[Special::TableSize]; // Special pointers used by the LLInt and JIT.
158

159
        Debugger* m_debugger;
160

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

164
        OwnPtr<JSGlobalObjectRareData> m_rareData;
165

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

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

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

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

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

        static JS_EXPORTDATA const ClassInfo s_info;

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

198 199
        void* m_apiData;

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

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

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

219
    public:
220 221
        JS_EXPORT_PRIVATE ~JSGlobalObject();
        JS_EXPORT_PRIVATE static void destroy(JSCell*);
222 223
        // 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
224

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

227 228 229 230
        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&);
231

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

234 235 236
        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);
237

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

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

245 246 247 248 249 250 251 252 253 254 255 256 257
        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(); }
258 259 260 261 262 263
        GetterSetter* throwTypeErrorGetterSetter(ExecState* exec)
        {
            if (!m_throwTypeErrorGetterSetter)
                createThrowTypeError(exec);
            return m_throwTypeErrorGetterSetter.get();
        }
264 265 266 267 268 269 270 271 272

        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
273
        ErrorPrototype* errorPrototype() const { return m_errorPrototype.get(); }
274

275 276 277 278
        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(); }
279
        Structure* argumentsStructure() const { return m_argumentsStructure.get(); }
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        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));
        }
        
295 296
        bool isOriginalArrayStructure(Structure* structure)
        {
297
            return originalArrayStructureForIndexingType(structure->indexingType() | IsArray) == structure;
298
        }
299
        
300 301 302 303 304
        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(); }
305
        Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
306 307
        Structure* errorStructure() const { return m_errorStructure.get(); }
        Structure* functionStructure() const { return m_functionStructure.get(); }
308
        Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(); }
309
        Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
310
        PropertyOffset functionNameOffset() const { return m_functionNameOffset; }
311
        Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
312
        Structure* privateNameStructure() const { return m_privateNameStructure.get(); }
313 314 315 316 317
        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(); }

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

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

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

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

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

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

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

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

358
        bool isDynamicScope(bool& requiresDynamicChecks) const;
359

360 361 362 363 364 365 366
        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;
        }
367

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

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

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

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

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

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

        UnlinkedProgramCodeBlock* createProgramCodeBlock(CallFrame*, ProgramExecutable*, JSObject** exception);
        UnlinkedEvalCodeBlock* createEvalCodeBlock(CallFrame*, EvalExecutable*, JSObject** exception);
        UnlinkedFunctionExecutable* createFunctionExecutableFromGlobalCode(CallFrame*, const Identifier&, const SourceCode&, 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