JSGlobalObject.h 24.4 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"
27
#include "JSArrayBufferPrototype.h"
weinig@apple.com's avatar
weinig@apple.com committed
28
#include "JSClassRef.h"
29
#include "JSSegmentedVariableObject.h"
30
#include "JSWeakObjectMapRefInternal.h"
31
#include "NumberPrototype.h"
32
#include "SpecialPointer.h"
33
#include "StringPrototype.h"
34
#include "StructureChain.h"
35
#include "StructureRareDataInlines.h"
36
#include "VM.h"
37
#include "Watchpoint.h"
38
#include <JavaScriptCore/JSBase.h>
39 40
#include <wtf/HashSet.h>
#include <wtf/OwnPtr.h>
weinig@apple.com's avatar
weinig@apple.com committed
41
#include <wtf/PassRefPtr.h>
42
#include <wtf/RandomNumber.h>
43

44 45 46
struct OpaqueJSClass;
struct OpaqueJSClassContextData;

47
namespace JSC {
ggaren@apple.com's avatar
ggaren@apple.com committed
48

49 50 51 52 53 54 55 56 57 58 59 60 61
class ArrayPrototype;
class BooleanPrototype;
class DatePrototype;
class Debugger;
class ErrorConstructor;
class ErrorPrototype;
class EvalCodeBlock;
class EvalExecutable;
class FunctionCodeBlock;
class FunctionExecutable;
class FunctionPrototype;
class GetterSetter;
class GlobalCodeBlock;
weinig@apple.com's avatar
weinig@apple.com committed
62 63
class JSPromisePrototype;
class JSPromiseResolverPrototype;
64 65 66 67 68 69 70 71 72 73 74 75
class JSStack;
class LLIntOffsetsExtractor;
class NativeErrorConstructor;
class ProgramCodeBlock;
class ProgramExecutable;
class RegExpConstructor;
class RegExpPrototype;
class SourceCode;
struct ActivationStackNode;
struct HashTable;

typedef Vector<ExecState*, 16> ExecStateStack;
weinig@apple.com's avatar
weinig@apple.com committed
76 77 78 79 80 81 82 83

class TaskContext : public RefCounted<TaskContext> {
public:
    virtual ~TaskContext()
    {
    }
};

84 85 86
struct GlobalObjectMethodTable {
    typedef bool (*AllowsAccessFromFunctionPtr)(const JSGlobalObject*, ExecState*);
    AllowsAccessFromFunctionPtr allowsAccessFrom;
87

88 89
    typedef bool (*SupportsProfilingFunctionPtr)(const JSGlobalObject*); 
    SupportsProfilingFunctionPtr supportsProfiling;
90

91 92
    typedef bool (*SupportsRichSourceInfoFunctionPtr)(const JSGlobalObject*);
    SupportsRichSourceInfoFunctionPtr supportsRichSourceInfo;
93

94 95
    typedef bool (*ShouldInterruptScriptFunctionPtr)(const JSGlobalObject*);
    ShouldInterruptScriptFunctionPtr shouldInterruptScript;
96

97 98
    typedef bool (*JavaScriptExperimentsEnabledFunctionPtr)(const JSGlobalObject*);
    JavaScriptExperimentsEnabledFunctionPtr javaScriptExperimentsEnabled;
weinig@apple.com's avatar
weinig@apple.com committed
99 100 101 102

    typedef void (*QueueTaskToEventLoopCallbackFunctionPtr)(ExecState*, TaskContext*);
    typedef void (*QueueTaskToEventLoopFunctionPtr)(const JSGlobalObject*, QueueTaskToEventLoopCallbackFunctionPtr, PassRefPtr<TaskContext>);
    QueueTaskToEventLoopFunctionPtr queueTaskToEventLoop;
103 104 105 106 107
};

class JSGlobalObject : public JSSegmentedVariableObject {
private:
    typedef HashSet<RefPtr<OpaqueJSWeakObjectMap> > WeakMapSet;
108
    typedef HashMap<OpaqueJSClass*, OwnPtr<OpaqueJSClassContextData> > OpaqueJSClassDataMap;
109 110 111 112 113 114 115 116 117

    struct JSGlobalObjectRareData {
        JSGlobalObjectRareData()
            : profileGroup(0)
        {
        }

        WeakMapSet weakMaps;
        unsigned profileGroup;
118 119
        
        OpaqueJSClassDataMap opaqueJSClassData;
120 121
    };

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
protected:

    Register m_globalCallFrame[JSStack::CallFrameHeaderSize];

    WriteBarrier<JSObject> m_globalThis;

    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;
    WriteBarrier<GetterSetter> m_throwTypeErrorGetterSetter;

    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;
    WriteBarrier<ErrorPrototype> m_errorPrototype;
weinig@apple.com's avatar
weinig@apple.com committed
151 152
    WriteBarrier<JSPromisePrototype> m_promisePrototype;
    WriteBarrier<JSPromiseResolverPrototype> m_promiseResolverPrototype;
153 154 155 156 157 158

    WriteBarrier<Structure> m_withScopeStructure;
    WriteBarrier<Structure> m_strictEvalActivationStructure;
    WriteBarrier<Structure> m_activationStructure;
    WriteBarrier<Structure> m_nameScopeStructure;
    WriteBarrier<Structure> m_argumentsStructure;
159
        
160 161 162 163
    // 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];
164
        
165 166 167 168
    WriteBarrier<Structure> m_booleanObjectStructure;
    WriteBarrier<Structure> m_callbackConstructorStructure;
    WriteBarrier<Structure> m_callbackFunctionStructure;
    WriteBarrier<Structure> m_callbackObjectStructure;
169 170
#if JSC_OBJC_API_ENABLED
    WriteBarrier<Structure> m_objcCallbackFunctionStructure;
171
    WriteBarrier<Structure> m_objcWrapperObjectStructure;
172
#endif
173 174 175 176 177 178 179 180 181 182 183 184 185
    WriteBarrier<Structure> m_dateStructure;
    WriteBarrier<Structure> m_nullPrototypeObjectStructure;
    WriteBarrier<Structure> m_errorStructure;
    WriteBarrier<Structure> m_functionStructure;
    WriteBarrier<Structure> m_boundFunctionStructure;
    WriteBarrier<Structure> m_namedFunctionStructure;
    PropertyOffset m_functionNameOffset;
    WriteBarrier<Structure> m_numberObjectStructure;
    WriteBarrier<Structure> m_privateNameStructure;
    WriteBarrier<Structure> m_regExpMatchesArrayStructure;
    WriteBarrier<Structure> m_regExpStructure;
    WriteBarrier<Structure> m_stringObjectStructure;
    WriteBarrier<Structure> m_internalFunctionStructure;
weinig@apple.com's avatar
weinig@apple.com committed
186 187 188 189 190
    WriteBarrier<Structure> m_promiseStructure;
    WriteBarrier<Structure> m_promiseResolverStructure;
    WriteBarrier<Structure> m_promiseCallbackStructure;
    WriteBarrier<Structure> m_promiseWrapperCallbackStructure;

191 192 193 194 195 196 197 198 199
    WriteBarrier<JSArrayBufferPrototype> m_arrayBufferPrototype;
    WriteBarrier<Structure> m_arrayBufferStructure;
    
    struct TypedArrayData {
        WriteBarrier<JSObject> prototype;
        WriteBarrier<Structure> structure;
    };
    
    FixedArray<TypedArrayData, NUMBER_OF_TYPED_ARRAY_TYPES> m_typedArrays;
200
        
201
    void* m_specialPointers[Special::TableSize]; // Special pointers used by the LLInt and JIT.
202

203
    Debugger* m_debugger;
204

205 206
    RefPtr<WatchpointSet> m_masqueradesAsUndefinedWatchpoint;
    RefPtr<WatchpointSet> m_havingABadTimeWatchpoint;
207
    RefPtr<WatchpointSet> m_varInjectionWatchpoint;
208

209
    OwnPtr<JSGlobalObjectRareData> m_rareData;
210

211
    WeakRandom m_weakRandom;
ggaren@apple.com's avatar
ggaren@apple.com committed
212

213 214 215
    bool m_evalEnabled;
    String m_evalDisabledErrorMessage;
    bool m_experimentsEnabled;
216

217 218
    static JS_EXPORTDATA const GlobalObjectMethodTable s_globalObjectMethodTable;
    const GlobalObjectMethodTable* m_globalObjectMethodTable;
219

220 221 222 223 224 225
    void createRareDataIfNeeded()
    {
        if (m_rareData)
            return;
        m_rareData = adoptPtr(new JSGlobalObjectRareData);
    }
226
        
227 228
public:
    typedef JSSegmentedVariableObject Base;
229

ggaren@apple.com's avatar
ggaren@apple.com committed
230
    static JSGlobalObject* create(VM& vm, Structure* structure)
231
    {
ggaren@apple.com's avatar
ggaren@apple.com committed
232 233 234
        JSGlobalObject* globalObject = new (NotNull, allocateCell<JSGlobalObject>(vm.heap)) JSGlobalObject(vm, structure);
        globalObject->finishCreation(vm);
        vm.heap.addFinalizer(globalObject, destroy);
235 236
        return globalObject;
    }
237

238
    DECLARE_EXPORT_INFO;
239

240 241
    bool hasDebugger() const { return m_debugger; }
    bool hasProfiler() const { return globalObjectMethodTable()->supportsProfiling(this); }
242

243
protected:
ggaren@apple.com's avatar
ggaren@apple.com committed
244
    JS_EXPORT_PRIVATE explicit JSGlobalObject(VM&, Structure*, const GlobalObjectMethodTable* = 0);
245

ggaren@apple.com's avatar
ggaren@apple.com committed
246
    void finishCreation(VM& vm)
247
    {
ggaren@apple.com's avatar
ggaren@apple.com committed
248 249
        Base::finishCreation(vm);
        structure()->setGlobalObject(vm, this);
250 251 252
        m_experimentsEnabled = m_globalObjectMethodTable->javaScriptExperimentsEnabled(this);
        init(this);
    }
253

ggaren@apple.com's avatar
ggaren@apple.com committed
254
    void finishCreation(VM& vm, JSObject* thisValue)
255
    {
ggaren@apple.com's avatar
ggaren@apple.com committed
256 257
        Base::finishCreation(vm);
        structure()->setGlobalObject(vm, this);
258 259 260
        m_experimentsEnabled = m_globalObjectMethodTable->javaScriptExperimentsEnabled(this);
        init(thisValue);
    }
ggaren@apple.com's avatar
ggaren@apple.com committed
261

262 263 264 265
    enum ConstantMode { IsConstant, IsVariable };
    enum FunctionMode { IsFunctionToSpecialize, NotFunctionOrNotSpecializable };
    int addGlobalVar(const Identifier&, ConstantMode, FunctionMode);

266 267 268 269 270
public:
    JS_EXPORT_PRIVATE ~JSGlobalObject();
    JS_EXPORT_PRIVATE static void destroy(JSCell*);
    // 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
271

272
    JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
273

274
    JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
275 276
    bool hasOwnPropertyForWrite(ExecState*, PropertyName);
    JS_EXPORT_PRIVATE static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
277

278 279
    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);
280
    JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
281

282 283 284
    // We use this in the code generator as we perform symbol table
    // lookups prior to initializing the properties
    bool symbolTableHasProperty(PropertyName);
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    void addVar(ExecState* exec, const Identifier& propertyName)
    {
        if (!hasProperty(exec, propertyName))
            addGlobalVar(propertyName, IsVariable, NotFunctionOrNotSpecializable);
    }
    void addConst(ExecState* exec, const Identifier& propertyName)
    {
        if (!hasProperty(exec, propertyName))
            addGlobalVar(propertyName, IsConstant, NotFunctionOrNotSpecializable);
    }
    void addFunction(ExecState* exec, const Identifier& propertyName, JSValue value)
    {
        bool propertyDidExist = removeDirect(exec->vm(), propertyName); // Newly declared functions overwrite existing properties.
        int index = addGlobalVar(propertyName, IsVariable, !propertyDidExist ? IsFunctionToSpecialize : NotFunctionOrNotSpecializable);
        registerAt(index).set(exec->vm(), this, value);
    }

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

306
    RegExpConstructor* regExpConstructor() const { return m_regExpConstructor.get(); }
307

308 309 310 311 312 313 314
    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(); }
315

316 317 318 319 320 321 322 323 324
    JSFunction* evalFunction() const { return m_evalFunction.get(); }
    JSFunction* callFunction() const { return m_callFunction.get(); }
    JSFunction* applyFunction() const { return m_applyFunction.get(); }
    GetterSetter* throwTypeErrorGetterSetter(ExecState* exec)
    {
        if (!m_throwTypeErrorGetterSetter)
            createThrowTypeError(exec);
        return m_throwTypeErrorGetterSetter.get();
    }
325

326 327 328 329 330 331 332 333 334
    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(); }
    ErrorPrototype* errorPrototype() const { return m_errorPrototype.get(); }
weinig@apple.com's avatar
weinig@apple.com committed
335 336
    JSPromisePrototype* promisePrototype() const { return m_promisePrototype.get(); }
    JSPromiseResolverPrototype* promiseResolverPrototype() const { return m_promiseResolverPrototype.get(); }
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

    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(); }
    Structure* argumentsStructure() const { return m_argumentsStructure.get(); }
    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));
    }
357
        
358 359 360 361
    bool isOriginalArrayStructure(Structure* structure)
    {
        return originalArrayStructureForIndexingType(structure->indexingType() | IsArray) == structure;
    }
362
        
363 364 365 366
    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(); }
367 368
#if JSC_OBJC_API_ENABLED
    Structure* objcCallbackFunctionStructure() const { return m_objcCallbackFunctionStructure.get(); }
369
    Structure* objcWrapperObjectStructure() const { return m_objcWrapperObjectStructure.get(); }
370
#endif
371 372 373 374 375 376 377 378 379 380 381 382 383
    Structure* dateStructure() const { return m_dateStructure.get(); }
    Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
    Structure* errorStructure() const { return m_errorStructure.get(); }
    Structure* functionStructure() const { return m_functionStructure.get(); }
    Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(); }
    Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
    PropertyOffset functionNameOffset() const { return m_functionNameOffset; }
    Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
    Structure* privateNameStructure() const { return m_privateNameStructure.get(); }
    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(); }
weinig@apple.com's avatar
weinig@apple.com committed
384 385 386 387 388
    Structure* promiseStructure() const { return m_promiseStructure.get(); }
    Structure* promiseResolverStructure() const { return m_promiseResolverStructure.get(); }
    Structure* promiseCallbackStructure() const { return m_promiseCallbackStructure.get(); }
    Structure* promiseWrapperCallbackStructure() const { return m_promiseWrapperCallbackStructure.get(); }

389 390 391 392 393 394 395
    JSArrayBufferPrototype* arrayBufferPrototype() const { return m_arrayBufferPrototype.get(); }
    Structure* arrayBufferStructure() const { return m_arrayBufferStructure.get(); }
    
    Structure* typedArrayStructure(TypedArrayType type) const
    {
        return m_typedArrays[toIndex(type)].structure.get();
    }
396 397 398 399 400 401

    void* actualPointerFor(Special::Pointer pointer)
    {
        ASSERT(pointer < Special::TableSize);
        return m_specialPointers[pointer];
    }
402

403 404
    WatchpointSet* masqueradesAsUndefinedWatchpoint() { return m_masqueradesAsUndefinedWatchpoint.get(); }
    WatchpointSet* havingABadTimeWatchpoint() { return m_havingABadTimeWatchpoint.get(); }
405
    WatchpointSet* varInjectionWatchpoint() { return m_varInjectionWatchpoint.get(); }
406
        
407 408 409 410
    bool isHavingABadTime() const
    {
        return m_havingABadTimeWatchpoint->hasBeenInvalidated();
    }
411
        
ggaren@apple.com's avatar
ggaren@apple.com committed
412
    void haveABadTime(VM&);
413
        
414
    bool objectPrototypeIsSane();
415
    bool arrayPrototypeChainIsSane();
416
    bool stringPrototypeChainIsSane();
ggaren@apple.com's avatar
ggaren@apple.com committed
417

418 419 420 421 422 423 424
    void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
    unsigned profileGroup() const
    { 
        if (!m_rareData)
            return 0;
        return m_rareData->profileGroup;
    }
425

426 427
    Debugger* debugger() const { return m_debugger; }
    void setDebugger(Debugger* debugger) { m_debugger = debugger; }
428

429
    const GlobalObjectMethodTable* globalObjectMethodTable() const { return m_globalObjectMethodTable; }
430

431 432 433
    static bool allowsAccessFrom(const JSGlobalObject*, ExecState*) { return true; }
    static bool supportsProfiling(const JSGlobalObject*) { return false; }
    static bool supportsRichSourceInfo(const JSGlobalObject*) { return true; }
weinig@apple.com's avatar
weinig@apple.com committed
434

435
    JS_EXPORT_PRIVATE ExecState* globalExec();
436

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

440 441 442
    bool evalEnabled() const { return m_evalEnabled; }
    const String& evalDisabledErrorMessage() const { return m_evalDisabledErrorMessage; }
    void setEvalEnabled(bool enabled, const String& errorMessage = String())
darin@apple.com's avatar
darin@apple.com committed
443
    {
444 445
        m_evalEnabled = enabled;
        m_evalDisabledErrorMessage = errorMessage;
darin@apple.com's avatar
darin@apple.com committed
446 447
    }

ggaren@apple.com's avatar
ggaren@apple.com committed
448
    void resetPrototype(VM&, JSValue prototype);
449

ggaren@apple.com's avatar
ggaren@apple.com committed
450
    VM& vm() const { return *Heap::heap(this)->vm(); }
451
    JSObject* globalThis() const;
452
    JS_EXPORT_PRIVATE void setGlobalThis(VM&, JSObject* globalThis);
453

ggaren@apple.com's avatar
ggaren@apple.com committed
454
    static Structure* createStructure(VM& vm, JSValue prototype)
455
    {
456
        return Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
457
    }
458

459
    void registerWeakMap(OpaqueJSWeakObjectMap* map)
460
    {
461 462
        createRareDataIfNeeded();
        m_rareData->weakMaps.add(map);
463
    }
464

465
    void unregisterWeakMap(OpaqueJSWeakObjectMap* map)
466
    {
467 468
        if (m_rareData)
            m_rareData->weakMaps.remove(map);
469 470
    }

471 472 473 474 475 476
    OpaqueJSClassDataMap& opaqueJSClassData()
    {
        createRareDataIfNeeded();
        return m_rareData->opaqueJSClassData;
    }

477 478
    double weakRandomNumber() { return m_weakRandom.get(); }
    unsigned weakRandomInteger() { return m_weakRandom.getUint32(); }
479

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

483
protected:
484

485
    static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
486

487 488 489 490 491
    struct GlobalPropertyInfo {
        GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
            : identifier(i)
            , value(v)
            , attributes(a)
492 493 494
        {
        }

495 496 497
        const Identifier identifier;
        JSValue value;
        unsigned attributes;
498
    };
499
    JS_EXPORT_PRIVATE void addStaticGlobals(GlobalPropertyInfo*, int count);
500

501
    JS_EXPORT_PRIVATE static JSC::JSValue toThis(JSC::JSCell*, JSC::ExecState*, ECMAMode);
502

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
private:
    friend class LLIntOffsetsExtractor;
        
    // FIXME: Fold reset into init.
    JS_EXPORT_PRIVATE void init(JSObject* thisValue);
    void reset(JSValue prototype);

    void createThrowTypeError(ExecState*);

    JS_EXPORT_PRIVATE static void clearRareData(JSCell*);
};

JSGlobalObject* asGlobalObject(JSValue);

inline JSGlobalObject* asGlobalObject(JSValue value)
{
    ASSERT(asObject(value)->isGlobalObject());
    return jsCast<JSGlobalObject*>(asObject(value));
}

inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, PropertyName propertyName)
{
525
    PropertySlot slot(this);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    if (Base::getOwnPropertySlot(this, exec, propertyName, slot))
        return true;
    bool slotIsWriteable;
    return symbolTableGet(this, propertyName, slot, slotIsWriteable);
}

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

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
ggaren@apple.com's avatar
ggaren@apple.com committed
545 546
    ASSERT(vm().dynamicGlobalObject);
    return vm().dynamicGlobalObject;
547 548 549 550
}

inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, unsigned initialLength = 0)
{
ggaren@apple.com's avatar
ggaren@apple.com committed
551
    return ArrayAllocationProfile::updateLastAllocationFor(profile, JSArray::create(exec->vm(), initialLength >= MIN_SPARSE_ARRAY_INDEX ? globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithArrayStorage) : globalObject->arrayStructureForProfileDuringAllocation(profile), initialLength));
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

inline JSArray* constructEmptyArray(ExecState* exec, ArrayAllocationProfile* profile, unsigned initialLength = 0)
{
    return constructEmptyArray(exec, profile, exec->lexicalGlobalObject(), initialLength);
}
 
inline JSArray* constructArray(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, const ArgList& values)
{
    return ArrayAllocationProfile::updateLastAllocationFor(profile, constructArray(exec, globalObject->arrayStructureForProfileDuringAllocation(profile), values));
}

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

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

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

class DynamicGlobalObjectScope {
    WTF_MAKE_NONCOPYABLE(DynamicGlobalObjectScope);
public:
ggaren@apple.com's avatar
ggaren@apple.com committed
582
    JS_EXPORT_PRIVATE DynamicGlobalObjectScope(VM&, JSGlobalObject*);
583 584 585 586

    ~DynamicGlobalObjectScope()
    {
        m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
587 588
    }

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
private:
    JSGlobalObject*& m_dynamicGlobalObjectSlot;
    JSGlobalObject* m_savedDynamicGlobalObject;
};

inline JSObject* JSScope::globalThis()
{ 
    return globalObject()->globalThis();
}

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

604
} // namespace JSC
605

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