1 2 3 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/*
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
* Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
*
* 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.
*
*/
#ifndef JSGlobalObject_h
#define JSGlobalObject_h
#include "JSArray.h"
#include "JSGlobalData.h"
#include "JSVariableObject.h"
#include "NativeFunctionWrapper.h"
#include "NumberPrototype.h"
#include "StringPrototype.h"
#include <wtf/HashSet.h>
#include <wtf/OwnPtr.h>
namespace JSC {
class ArrayPrototype;
class BooleanPrototype;
class DatePrototype;
class Debugger;
class ErrorConstructor;
class FunctionPrototype;
class GlobalCodeBlock;
class GlobalEvalFunction;
class NativeErrorConstructor;
class ProgramCodeBlock;
class PrototypeFunction;
class RegExpConstructor;
class RegExpPrototype;
class RegisterFile;
struct ActivationStackNode;
struct HashTable;
typedef Vector<ExecState*, 16> ExecStateStack;
class JSGlobalObject : public JSVariableObject {
protected:
using JSVariableObject::JSVariableObjectData;
struct JSGlobalObjectData : public JSVariableObjectData {
// We use an explicit destructor function pointer instead of a
// virtual destructor because we want to avoid adding a vtable
// pointer to this struct. Adding a vtable pointer would force the
// compiler to emit costly pointer fixup code when casting from
// JSVariableObjectData* to JSGlobalObjectData*.
typedef void (*Destructor)(void*);
JSGlobalObjectData(Destructor destructor)
: JSVariableObjectData(&symbolTable, 0)
, destructor(destructor)
, registerArraySize(0)
, globalScopeChain(NoScopeChain())
, regExpConstructor(0)
, errorConstructor(0)
, evalErrorConstructor(0)
, rangeErrorConstructor(0)
, referenceErrorConstructor(0)
, syntaxErrorConstructor(0)
, typeErrorConstructor(0)
, URIErrorConstructor(0)
, evalFunction(0)
, callFunction(0)
, applyFunction(0)
, objectPrototype(0)
, functionPrototype(0)
, arrayPrototype(0)
, booleanPrototype(0)
, stringPrototype(0)
, numberPrototype(0)
, datePrototype(0)
, regExpPrototype(0)
, methodCallDummy(0)
{
}
Destructor destructor;
size_t registerArraySize;
JSGlobalObject* next;
JSGlobalObject* prev;
Debugger* debugger;
ScopeChain globalScopeChain;
Register globalCallFrame[RegisterFile::CallFrameHeaderSize];
int recursion;
RegExpConstructor* regExpConstructor;
ErrorConstructor* errorConstructor;
NativeErrorConstructor* evalErrorConstructor;
NativeErrorConstructor* rangeErrorConstructor;
NativeErrorConstructor* referenceErrorConstructor;
NativeErrorConstructor* syntaxErrorConstructor;
NativeErrorConstructor* typeErrorConstructor;
NativeErrorConstructor* URIErrorConstructor;
GlobalEvalFunction* evalFunction;
NativeFunctionWrapper* callFunction;
NativeFunctionWrapper* applyFunction;
ObjectPrototype* objectPrototype;
FunctionPrototype* functionPrototype;
ArrayPrototype* arrayPrototype;
BooleanPrototype* booleanPrototype;
StringPrototype* stringPrototype;
NumberPrototype* numberPrototype;
DatePrototype* datePrototype;
RegExpPrototype* regExpPrototype;
JSObject* methodCallDummy;
RefPtr<Structure> argumentsStructure;
RefPtr<Structure> arrayStructure;
RefPtr<Structure> booleanObjectStructure;
RefPtr<Structure> callbackConstructorStructure;
RefPtr<Structure> callbackFunctionStructure;
RefPtr<Structure> callbackObjectStructure;
RefPtr<Structure> dateStructure;
RefPtr<Structure> emptyObjectStructure;
RefPtr<Structure> errorStructure;
RefPtr<Structure> functionStructure;
RefPtr<Structure> numberObjectStructure;
RefPtr<Structure> prototypeFunctionStructure;
RefPtr<Structure> regExpMatchesArrayStructure;
RefPtr<Structure> regExpStructure;
RefPtr<Structure> stringObjectStructure;
SymbolTable symbolTable;
unsigned profileGroup;
RefPtr<JSGlobalData> globalData;
HashSet<GlobalCodeBlock*> codeBlocks;
};
public:
void* operator new(size_t, JSGlobalData*);
explicit JSGlobalObject()
: JSVariableObject(JSGlobalObject::createStructure(jsNull()), new JSGlobalObjectData(destroyJSGlobalObjectData))
{
init(this);
}
protected:
JSGlobalObject(NonNullPassRefPtr<Structure> structure, JSGlobalObjectData* data, JSObject* thisValue)
: JSVariableObject(structure, data)
{
init(thisValue);
}
public:
virtual ~JSGlobalObject();
virtual void markChildren(MarkStack&);
virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
virtual bool hasOwnPropertyForWrite(ExecState*, const Identifier&);
virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue value, unsigned attributes);
virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes);
virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes);
// Linked list of all global objects that use the same JSGlobalData.
JSGlobalObject*& head() { return d()->globalData->head; }
JSGlobalObject* next() { return d()->next; }
// The following accessors return pristine values, even if a script
// replaces the global object's associated property.
RegExpConstructor* regExpConstructor() const { return d()->regExpConstructor; }
ErrorConstructor* errorConstructor() const { return d()->errorConstructor; }
NativeErrorConstructor* evalErrorConstructor() const { return d()->evalErrorConstructor; }
NativeErrorConstructor* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
NativeErrorConstructor* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
NativeErrorConstructor* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
NativeErrorConstructor* typeErrorConstructor() const { return d()->typeErrorConstructor; }
NativeErrorConstructor* URIErrorConstructor() const { return d()->URIErrorConstructor; }
GlobalEvalFunction* evalFunction() const { return d()->evalFunction; }
ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
StringPrototype* stringPrototype() const { return d()->stringPrototype; }
NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
DatePrototype* datePrototype() const { return d()->datePrototype; }
RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
JSObject* methodCallDummy() const { return d()->methodCallDummy; }
Structure* argumentsStructure() const { return d()->argumentsStructure.get(); }
Structure* arrayStructure() const { return d()->arrayStructure.get(); }
Structure* booleanObjectStructure() const { return d()->booleanObjectStructure.get(); }
Structure* callbackConstructorStructure() const { return d()->callbackConstructorStructure.get(); }
Structure* callbackFunctionStructure() const { return d()->callbackFunctionStructure.get(); }
Structure* callbackObjectStructure() const { return d()->callbackObjectStructure.get(); }
Structure* dateStructure() const { return d()->dateStructure.get(); }
Structure* emptyObjectStructure() const { return d()->emptyObjectStructure.get(); }
Structure* errorStructure() const { return d()->errorStructure.get(); }
Structure* functionStructure() const { return d()->functionStructure.get(); }
Structure* numberObjectStructure() const { return d()->numberObjectStructure.get(); }
Structure* prototypeFunctionStructure() const { return d()->prototypeFunctionStructure.get(); }
Structure* regExpMatchesArrayStructure() const { return d()->regExpMatchesArrayStructure.get(); }
Structure* regExpStructure() const { return d()->regExpStructure.get(); }
Structure* stringObjectStructure() const { return d()->stringObjectStructure.get(); }
void setProfileGroup(unsigned value) { d()->profileGroup = value; }
unsigned profileGroup() const { return d()->profileGroup; }
Debugger* debugger() const { return d()->debugger; }
void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
virtual bool supportsProfiling() const { return false; }
int recursion() { return d()->recursion; }
void incRecursion() { ++d()->recursion; }
void decRecursion() { --d()->recursion; }
ScopeChain& globalScopeChain() { return d()->globalScopeChain; }
virtual bool isGlobalObject() const { return true; }
virtual ExecState* globalExec();
virtual bool shouldInterruptScript() const { return true; }
virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
virtual bool isDynamicScope() const;
HashSet<GlobalCodeBlock*>& codeBlocks() { return d()->codeBlocks; }
void copyGlobalsFrom(RegisterFile&);
void copyGlobalsTo(RegisterFile&);
void resetPrototype(JSValue prototype);
JSGlobalData* globalData() { return d()->globalData.get(); }
JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
static PassRefPtr<Structure> createStructure(JSValue prototype)
{
return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount);
}
protected:
static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesMarkChildren | OverridesGetPropertyNames | JSVariableObject::StructureFlags;
struct GlobalPropertyInfo {
GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
: identifier(i)
, value(v)
, attributes(a)
{
}
const Identifier identifier;
JSValue value;
unsigned attributes;
};
void addStaticGlobals(GlobalPropertyInfo*, int count);
private:
static void destroyJSGlobalObjectData(void*);
// FIXME: Fold reset into init.
void init(JSObject* thisValue);
void reset(JSValue prototype);
void setRegisters(Register* registers, Register* registerArray, size_t count);
void* operator new(size_t); // can only be allocated with JSGlobalData
};
JSGlobalObject* asGlobalObject(JSValue);
inline JSGlobalObject* asGlobalObject(JSValue value)
{
ASSERT(asObject(value)->isGlobalObject());
return static_cast<JSGlobalObject*>(asObject(value));
}
inline void JSGlobalObject::setRegisters(Register* registers, Register* registerArray, size_t count)
{
JSVariableObject::setRegisters(registers, registerArray);
d()->registerArraySize = count;
}
inline void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
{
size_t oldSize = d()->registerArraySize;
size_t newSize = oldSize + count;
Register* registerArray = new Register[newSize];
if (d()->registerArray)
memcpy(registerArray + count, d()->registerArray.get(), oldSize * sizeof(Register));
setRegisters(registerArray + newSize, registerArray, newSize);
for (int i = 0, index = -static_cast<int>(oldSize) - 1; i < count; ++i, --index) {
GlobalPropertyInfo& global = globals[i];
ASSERT(global.attributes & DontDelete);
SymbolTableEntry newEntry(index, global.attributes);
symbolTable().add(global.identifier.ustring().rep(), newEntry);
registerAt(index) = global.value;
}
}
inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
return true;
return symbolTableGet(propertyName, slot);
}
inline bool JSGlobalObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
{
if (symbolTableGet(propertyName, descriptor))
return true;
return JSVariableObject::getOwnPropertyDescriptor(exec, propertyName, descriptor);
}
inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, const Identifier& propertyName)
{
PropertySlot slot;
if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
return true;
bool slotIsWriteable;
return symbolTableGet(propertyName, slot, slotIsWriteable);
}
inline JSValue Structure::prototypeForLookup(ExecState* exec) const
{
if (typeInfo().type() == ObjectType)
return m_prototype;
#if USE(JSVALUE32)
if (typeInfo().type() == StringType)
return exec->lexicalGlobalObject()->stringPrototype();
ASSERT(typeInfo().type() == NumberType);
return exec->lexicalGlobalObject()->numberPrototype();
#else
ASSERT(typeInfo().type() == StringType);
return exec->lexicalGlobalObject()->stringPrototype();
#endif
}
inline StructureChain* Structure::prototypeChain(ExecState* exec) const
{
// We cache our prototype chain so our clients can share it.
if (!isValid(exec, m_cachedPrototypeChain.get())) {
JSValue prototype = prototypeForLookup(exec);
m_cachedPrototypeChain = StructureChain::create(prototype.isNull() ? 0 : asObject(prototype)->structure());
}
return m_cachedPrototypeChain.get();
}
inline bool Structure::isValid(ExecState* exec, StructureChain* cachedPrototypeChain) const
{
if (!cachedPrototypeChain)
return false;
JSValue prototype = prototypeForLookup(exec);
RefPtr<Structure>* cachedStructure = cachedPrototypeChain->head();
while(*cachedStructure && !prototype.isNull()) {
if (asObject(prototype)->structure() != *cachedStructure)
return false;
++cachedStructure;
prototype = asObject(prototype)->prototype();
}
return prototype.isNull() && !*cachedStructure;
}
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;
}
inline JSObject* constructEmptyObject(ExecState* exec)
{
return new (exec) JSObject(exec->lexicalGlobalObject()->emptyObjectStructure());
}
inline JSObject* constructEmptyObject(ExecState* exec, JSGlobalObject* globalObject)
{
return new (exec) JSObject(globalObject->emptyObjectStructure());
}
inline JSArray* constructEmptyArray(ExecState* exec)
{
return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure());
}
inline JSArray* constructEmptyArray(ExecState* exec, JSGlobalObject* globalObject)
{
return new (exec) JSArray(globalObject->arrayStructure());
}
inline JSArray* constructEmptyArray(ExecState* exec, unsigned initialLength)
{
return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), initialLength);
}
inline JSArray* constructArray(ExecState* exec, JSValue singleItemValue)
{
MarkedArgumentBuffer values;
values.append(singleItemValue);
return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), values);
}
inline JSArray* constructArray(ExecState* exec, const ArgList& values)
{
return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), values);
}
class DynamicGlobalObjectScope : public Noncopyable {
public:
DynamicGlobalObjectScope(CallFrame* callFrame, JSGlobalObject* dynamicGlobalObject)
: m_dynamicGlobalObjectSlot(callFrame->globalData().dynamicGlobalObject)
, m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
{
if (!m_dynamicGlobalObjectSlot) {
m_dynamicGlobalObjectSlot = dynamicGlobalObject;
// Reset the date cache between JS invocations to force the VM
// to observe time zone changes.
callFrame->globalData().resetDateCache();
}
}
~DynamicGlobalObjectScope()
{
m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
}
private:
JSGlobalObject*& m_dynamicGlobalObjectSlot;
JSGlobalObject* m_savedDynamicGlobalObject;
};
} // namespace JSC
#endif // JSGlobalObject_h
|