JSGlobalObject.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #include "JSGlobalObject.h"
  31. #include "Arguments.h"
  32. #include "ArrayConstructor.h"
  33. #include "ArrayPrototype.h"
  34. #include "BooleanConstructor.h"
  35. #include "BooleanPrototype.h"
  36. #include "CodeBlock.h"
  37. #include "CodeCache.h"
  38. #include "DateConstructor.h"
  39. #include "DatePrototype.h"
  40. #include "Debugger.h"
  41. #include "Error.h"
  42. #include "ErrorConstructor.h"
  43. #include "ErrorPrototype.h"
  44. #include "FunctionConstructor.h"
  45. #include "FunctionPrototype.h"
  46. #include "GetterSetter.h"
  47. #include "Interpreter.h"
  48. #include "JSAPIWrapperObject.h"
  49. #include "JSActivation.h"
  50. #include "JSBoundFunction.h"
  51. #include "JSCallbackConstructor.h"
  52. #include "JSCallbackFunction.h"
  53. #include "JSCallbackObject.h"
  54. #include "JSFunction.h"
  55. #include "JSGlobalObjectFunctions.h"
  56. #include "JSLock.h"
  57. #include "JSNameScope.h"
  58. #include "JSONObject.h"
  59. #include "JSWithScope.h"
  60. #include "LegacyProfiler.h"
  61. #include "Lookup.h"
  62. #include "MathObject.h"
  63. #include "NameConstructor.h"
  64. #include "NameInstance.h"
  65. #include "NamePrototype.h"
  66. #include "NativeErrorConstructor.h"
  67. #include "NativeErrorPrototype.h"
  68. #include "NumberConstructor.h"
  69. #include "NumberPrototype.h"
  70. #include "ObjCCallbackFunction.h"
  71. #include "ObjectConstructor.h"
  72. #include "ObjectPrototype.h"
  73. #include "Operations.h"
  74. #include "ParserError.h"
  75. #include "RegExpConstructor.h"
  76. #include "RegExpMatchesArray.h"
  77. #include "RegExpObject.h"
  78. #include "RegExpPrototype.h"
  79. #include "StrictEvalActivation.h"
  80. #include "StringConstructor.h"
  81. #include "StringPrototype.h"
  82. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  83. #include "JSGlobalObject.lut.h"
  84. #endif
  85. namespace JSC {
  86. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  87. const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &Base::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
  88. const GlobalObjectMethodTable JSGlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled };
  89. /* Source for JSGlobalObject.lut.h
  90. @begin globalObjectTable
  91. parseInt globalFuncParseInt DontEnum|Function 2
  92. parseFloat globalFuncParseFloat DontEnum|Function 1
  93. isNaN globalFuncIsNaN DontEnum|Function 1
  94. isFinite globalFuncIsFinite DontEnum|Function 1
  95. escape globalFuncEscape DontEnum|Function 1
  96. unescape globalFuncUnescape DontEnum|Function 1
  97. decodeURI globalFuncDecodeURI DontEnum|Function 1
  98. decodeURIComponent globalFuncDecodeURIComponent DontEnum|Function 1
  99. encodeURI globalFuncEncodeURI DontEnum|Function 1
  100. encodeURIComponent globalFuncEncodeURIComponent DontEnum|Function 1
  101. @end
  102. */
  103. JSGlobalObject::JSGlobalObject(VM& vm, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable)
  104. : Base(vm, structure, 0)
  105. , m_masqueradesAsUndefinedWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
  106. , m_havingABadTimeWatchpoint(adoptRef(new WatchpointSet(InitializedWatching)))
  107. , m_weakRandom(Options::forceWeakRandomSeed() ? Options::forcedWeakRandomSeed() : static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
  108. , m_evalEnabled(true)
  109. , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
  110. {
  111. }
  112. JSGlobalObject::~JSGlobalObject()
  113. {
  114. if (m_debugger)
  115. m_debugger->detach(this);
  116. if (LegacyProfiler* profiler = vm().enabledProfiler())
  117. profiler->stopProfiling(this);
  118. }
  119. void JSGlobalObject::destroy(JSCell* cell)
  120. {
  121. static_cast<JSGlobalObject*>(cell)->JSGlobalObject::~JSGlobalObject();
  122. }
  123. void JSGlobalObject::setGlobalThis(VM& vm, JSObject* globalThis)
  124. {
  125. m_globalThis.set(vm, this, globalThis);
  126. }
  127. void JSGlobalObject::init(JSObject* thisValue)
  128. {
  129. ASSERT(vm().apiLock().currentThreadIsHoldingLock());
  130. setGlobalThis(vm(), thisValue);
  131. JSGlobalObject::globalExec()->init(0, 0, this, CallFrame::noCaller(), 0, 0);
  132. m_debugger = 0;
  133. reset(prototype());
  134. }
  135. void JSGlobalObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  136. {
  137. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
  138. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
  139. if (symbolTablePut(thisObject, exec, propertyName, value, slot.isStrictMode()))
  140. return;
  141. Base::put(thisObject, exec, propertyName, value, slot);
  142. }
  143. void JSGlobalObject::putDirectVirtual(JSObject* object, ExecState* exec, PropertyName propertyName, JSValue value, unsigned attributes)
  144. {
  145. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
  146. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
  147. if (symbolTablePutWithAttributes(thisObject, exec->vm(), propertyName, value, attributes))
  148. return;
  149. JSValue valueBefore = thisObject->getDirect(exec->vm(), propertyName);
  150. PutPropertySlot slot;
  151. Base::put(thisObject, exec, propertyName, value, slot);
  152. if (!valueBefore) {
  153. JSValue valueAfter = thisObject->getDirect(exec->vm(), propertyName);
  154. if (valueAfter)
  155. JSObject::putDirectVirtual(thisObject, exec, propertyName, valueAfter, attributes);
  156. }
  157. }
  158. bool JSGlobalObject::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
  159. {
  160. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
  161. PropertySlot slot;
  162. // silently ignore attempts to add accessors aliasing vars.
  163. if (descriptor.isAccessorDescriptor() && symbolTableGet(thisObject, propertyName, slot))
  164. return false;
  165. return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
  166. }
  167. static inline JSObject* lastInPrototypeChain(JSObject* object)
  168. {
  169. JSObject* o = object;
  170. while (o->prototype().isObject())
  171. o = asObject(o->prototype());
  172. return o;
  173. }
  174. void JSGlobalObject::reset(JSValue prototype)
  175. {
  176. ExecState* exec = JSGlobalObject::globalExec();
  177. m_functionPrototype.set(exec->vm(), this, FunctionPrototype::create(exec, this, FunctionPrototype::createStructure(exec->vm(), this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
  178. m_functionStructure.set(exec->vm(), this, JSFunction::createStructure(exec->vm(), this, m_functionPrototype.get()));
  179. m_boundFunctionStructure.set(exec->vm(), this, JSBoundFunction::createStructure(exec->vm(), this, m_functionPrototype.get()));
  180. m_namedFunctionStructure.set(exec->vm(), this, Structure::addPropertyTransition(exec->vm(), m_functionStructure.get(), exec->vm().propertyNames->name, DontDelete | ReadOnly | DontEnum, 0, m_functionNameOffset));
  181. m_internalFunctionStructure.set(exec->vm(), this, InternalFunction::createStructure(exec->vm(), this, m_functionPrototype.get()));
  182. JSFunction* callFunction = 0;
  183. JSFunction* applyFunction = 0;
  184. m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
  185. m_callFunction.set(exec->vm(), this, callFunction);
  186. m_applyFunction.set(exec->vm(), this, applyFunction);
  187. m_objectPrototype.set(exec->vm(), this, ObjectPrototype::create(exec, this, ObjectPrototype::createStructure(exec->vm(), this, jsNull())));
  188. GetterSetter* protoAccessor = GetterSetter::create(exec);
  189. protoAccessor->setGetter(exec->vm(), JSFunction::create(exec, this, 0, String(), globalFuncProtoGetter));
  190. protoAccessor->setSetter(exec->vm(), JSFunction::create(exec, this, 0, String(), globalFuncProtoSetter));
  191. m_objectPrototype->putDirectAccessor(exec, exec->propertyNames().underscoreProto, protoAccessor, Accessor | DontEnum);
  192. m_functionPrototype->structure()->setPrototypeWithoutTransition(exec->vm(), m_objectPrototype.get());
  193. m_nameScopeStructure.set(exec->vm(), this, JSNameScope::createStructure(exec->vm(), this, jsNull()));
  194. m_activationStructure.set(exec->vm(), this, JSActivation::createStructure(exec->vm(), this, jsNull()));
  195. m_strictEvalActivationStructure.set(exec->vm(), this, StrictEvalActivation::createStructure(exec->vm(), this, jsNull()));
  196. m_withScopeStructure.set(exec->vm(), this, JSWithScope::createStructure(exec->vm(), this, jsNull()));
  197. m_nullPrototypeObjectStructure.set(exec->vm(), this, JSFinalObject::createStructure(vm(), this, jsNull(), JSFinalObject::defaultInlineCapacity()));
  198. m_callbackFunctionStructure.set(exec->vm(), this, JSCallbackFunction::createStructure(exec->vm(), this, m_functionPrototype.get()));
  199. m_argumentsStructure.set(exec->vm(), this, Arguments::createStructure(exec->vm(), this, m_objectPrototype.get()));
  200. m_callbackConstructorStructure.set(exec->vm(), this, JSCallbackConstructor::createStructure(exec->vm(), this, m_objectPrototype.get()));
  201. m_callbackObjectStructure.set(exec->vm(), this, JSCallbackObject<JSDestructibleObject>::createStructure(exec->vm(), this, m_objectPrototype.get()));
  202. #if JSC_OBJC_API_ENABLED
  203. m_objcCallbackFunctionStructure.set(exec->vm(), this, ObjCCallbackFunction::createStructure(exec->vm(), this, m_functionPrototype.get()));
  204. m_objcWrapperObjectStructure.set(exec->vm(), this, JSCallbackObject<JSAPIWrapperObject>::createStructure(exec->vm(), this, m_objectPrototype.get()));
  205. #endif
  206. m_arrayPrototype.set(exec->vm(), this, ArrayPrototype::create(exec, this, ArrayPrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  207. m_originalArrayStructureForIndexingShape[UndecidedShape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithUndecided));
  208. m_originalArrayStructureForIndexingShape[Int32Shape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithInt32));
  209. m_originalArrayStructureForIndexingShape[DoubleShape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithDouble));
  210. m_originalArrayStructureForIndexingShape[ContiguousShape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithContiguous));
  211. m_originalArrayStructureForIndexingShape[ArrayStorageShape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithArrayStorage));
  212. m_originalArrayStructureForIndexingShape[SlowPutArrayStorageShape >> IndexingShapeShift].set(exec->vm(), this, JSArray::createStructure(exec->vm(), this, m_arrayPrototype.get(), ArrayWithSlowPutArrayStorage));
  213. for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
  214. m_arrayStructureForIndexingShapeDuringAllocation[i] = m_originalArrayStructureForIndexingShape[i];
  215. m_regExpMatchesArrayStructure.set(exec->vm(), this, RegExpMatchesArray::createStructure(exec->vm(), this, m_arrayPrototype.get()));
  216. m_stringPrototype.set(exec->vm(), this, StringPrototype::create(exec, this, StringPrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  217. m_stringObjectStructure.set(exec->vm(), this, StringObject::createStructure(exec->vm(), this, m_stringPrototype.get()));
  218. m_booleanPrototype.set(exec->vm(), this, BooleanPrototype::create(exec, this, BooleanPrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  219. m_booleanObjectStructure.set(exec->vm(), this, BooleanObject::createStructure(exec->vm(), this, m_booleanPrototype.get()));
  220. m_numberPrototype.set(exec->vm(), this, NumberPrototype::create(exec, this, NumberPrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  221. m_numberObjectStructure.set(exec->vm(), this, NumberObject::createStructure(exec->vm(), this, m_numberPrototype.get()));
  222. m_datePrototype.set(exec->vm(), this, DatePrototype::create(exec, this, DatePrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  223. m_dateStructure.set(exec->vm(), this, DateInstance::createStructure(exec->vm(), this, m_datePrototype.get()));
  224. RegExp* emptyRegex = RegExp::create(exec->vm(), "", NoFlags);
  225. m_regExpPrototype.set(exec->vm(), this, RegExpPrototype::create(exec, this, RegExpPrototype::createStructure(exec->vm(), this, m_objectPrototype.get()), emptyRegex));
  226. m_regExpStructure.set(exec->vm(), this, RegExpObject::createStructure(exec->vm(), this, m_regExpPrototype.get()));
  227. m_errorPrototype.set(exec->vm(), this, ErrorPrototype::create(exec, this, ErrorPrototype::createStructure(exec->vm(), this, m_objectPrototype.get())));
  228. m_errorStructure.set(exec->vm(), this, ErrorInstance::createStructure(exec->vm(), this, m_errorPrototype.get()));
  229. // Constructors
  230. JSCell* objectConstructor = ObjectConstructor::create(exec, this, ObjectConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_objectPrototype.get());
  231. JSCell* functionConstructor = FunctionConstructor::create(exec, this, FunctionConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_functionPrototype.get());
  232. JSCell* arrayConstructor = ArrayConstructor::create(exec, this, ArrayConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_arrayPrototype.get());
  233. JSCell* stringConstructor = StringConstructor::create(exec, this, StringConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_stringPrototype.get());
  234. JSCell* booleanConstructor = BooleanConstructor::create(exec, this, BooleanConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_booleanPrototype.get());
  235. JSCell* numberConstructor = NumberConstructor::create(exec, this, NumberConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_numberPrototype.get());
  236. JSCell* dateConstructor = DateConstructor::create(exec, this, DateConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_datePrototype.get());
  237. m_regExpConstructor.set(exec->vm(), this, RegExpConstructor::create(exec, this, RegExpConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_regExpPrototype.get()));
  238. m_errorConstructor.set(exec->vm(), this, ErrorConstructor::create(exec, this, ErrorConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), m_errorPrototype.get()));
  239. Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(exec->vm(), this, m_errorPrototype.get());
  240. Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(exec->vm(), this, m_functionPrototype.get());
  241. m_evalErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("EvalError")));
  242. m_rangeErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("RangeError")));
  243. m_referenceErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("ReferenceError")));
  244. m_syntaxErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("SyntaxError")));
  245. m_typeErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("TypeError")));
  246. m_URIErrorConstructor.set(exec->vm(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, ASCIILiteral("URIError")));
  247. m_objectPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, objectConstructor, DontEnum);
  248. m_functionPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, functionConstructor, DontEnum);
  249. m_arrayPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, arrayConstructor, DontEnum);
  250. m_booleanPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, booleanConstructor, DontEnum);
  251. m_stringPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, stringConstructor, DontEnum);
  252. m_numberPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, numberConstructor, DontEnum);
  253. m_datePrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, dateConstructor, DontEnum);
  254. m_regExpPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, m_regExpConstructor.get(), DontEnum);
  255. m_errorPrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, m_errorConstructor.get(), DontEnum);
  256. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Object, objectConstructor, DontEnum);
  257. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Function, functionConstructor, DontEnum);
  258. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Array, arrayConstructor, DontEnum);
  259. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Boolean, booleanConstructor, DontEnum);
  260. putDirectWithoutTransition(exec->vm(), exec->propertyNames().String, stringConstructor, DontEnum);
  261. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Number, numberConstructor, DontEnum);
  262. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Date, dateConstructor, DontEnum);
  263. putDirectWithoutTransition(exec->vm(), exec->propertyNames().RegExp, m_regExpConstructor.get(), DontEnum);
  264. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Error, m_errorConstructor.get(), DontEnum);
  265. putDirectWithoutTransition(exec->vm(), exec->propertyNames().EvalError, m_evalErrorConstructor.get(), DontEnum);
  266. putDirectWithoutTransition(exec->vm(), exec->propertyNames().RangeError, m_rangeErrorConstructor.get(), DontEnum);
  267. putDirectWithoutTransition(exec->vm(), exec->propertyNames().ReferenceError, m_referenceErrorConstructor.get(), DontEnum);
  268. putDirectWithoutTransition(exec->vm(), exec->propertyNames().SyntaxError, m_syntaxErrorConstructor.get(), DontEnum);
  269. putDirectWithoutTransition(exec->vm(), exec->propertyNames().TypeError, m_typeErrorConstructor.get(), DontEnum);
  270. putDirectWithoutTransition(exec->vm(), exec->propertyNames().URIError, m_URIErrorConstructor.get(), DontEnum);
  271. m_evalFunction.set(exec->vm(), this, JSFunction::create(exec, this, 1, exec->propertyNames().eval.string(), globalFuncEval));
  272. putDirectWithoutTransition(exec->vm(), exec->propertyNames().eval, m_evalFunction.get(), DontEnum);
  273. putDirectWithoutTransition(exec->vm(), exec->propertyNames().JSON, JSONObject::create(exec, this, JSONObject::createStructure(exec->vm(), this, m_objectPrototype.get())), DontEnum);
  274. putDirectWithoutTransition(exec->vm(), exec->propertyNames().Math, MathObject::create(exec, this, MathObject::createStructure(exec->vm(), this, m_objectPrototype.get())), DontEnum);
  275. GlobalPropertyInfo staticGlobals[] = {
  276. GlobalPropertyInfo(exec->propertyNames().NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
  277. GlobalPropertyInfo(exec->propertyNames().Infinity, jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
  278. GlobalPropertyInfo(exec->propertyNames().undefinedKeyword, jsUndefined(), DontEnum | DontDelete | ReadOnly)
  279. };
  280. addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
  281. m_specialPointers[Special::CallFunction] = m_callFunction.get();
  282. m_specialPointers[Special::ApplyFunction] = m_applyFunction.get();
  283. m_specialPointers[Special::ObjectConstructor] = objectConstructor;
  284. m_specialPointers[Special::ArrayConstructor] = arrayConstructor;
  285. if (m_experimentsEnabled) {
  286. NamePrototype* privateNamePrototype = NamePrototype::create(exec, NamePrototype::createStructure(exec->vm(), this, m_objectPrototype.get()));
  287. m_privateNameStructure.set(exec->vm(), this, NameInstance::createStructure(exec->vm(), this, privateNamePrototype));
  288. JSCell* privateNameConstructor = NameConstructor::create(exec, this, NameConstructor::createStructure(exec->vm(), this, m_functionPrototype.get()), privateNamePrototype);
  289. privateNamePrototype->putDirectWithoutTransition(exec->vm(), exec->propertyNames().constructor, privateNameConstructor, DontEnum);
  290. putDirectWithoutTransition(exec->vm(), Identifier(exec, "Name"), privateNameConstructor, DontEnum);
  291. }
  292. resetPrototype(exec->vm(), prototype);
  293. }
  294. // Private namespace for helpers for JSGlobalObject::haveABadTime()
  295. namespace {
  296. class ObjectsWithBrokenIndexingFinder : public MarkedBlock::VoidFunctor {
  297. public:
  298. ObjectsWithBrokenIndexingFinder(MarkedArgumentBuffer&, JSGlobalObject*);
  299. void operator()(JSCell*);
  300. private:
  301. MarkedArgumentBuffer& m_foundObjects;
  302. JSGlobalObject* m_globalObject;
  303. };
  304. ObjectsWithBrokenIndexingFinder::ObjectsWithBrokenIndexingFinder(
  305. MarkedArgumentBuffer& foundObjects, JSGlobalObject* globalObject)
  306. : m_foundObjects(foundObjects)
  307. , m_globalObject(globalObject)
  308. {
  309. }
  310. inline bool hasBrokenIndexing(JSObject* object)
  311. {
  312. // This will change if we have more indexing types.
  313. IndexingType type = object->structure()->indexingType();
  314. // This could be made obviously more efficient, but isn't made so right now, because
  315. // we expect this to be an unlikely slow path anyway.
  316. return hasUndecided(type) || hasInt32(type) || hasDouble(type) || hasContiguous(type) || hasFastArrayStorage(type);
  317. }
  318. void ObjectsWithBrokenIndexingFinder::operator()(JSCell* cell)
  319. {
  320. if (!cell->isObject())
  321. return;
  322. JSObject* object = asObject(cell);
  323. // Run this filter first, since it's cheap, and ought to filter out a lot of objects.
  324. if (!hasBrokenIndexing(object))
  325. return;
  326. // We only want to have a bad time in the affected global object, not in the entire
  327. // VM. But we have to be careful, since there may be objects that claim to belong to
  328. // a different global object that have prototypes from our global object.
  329. bool foundGlobalObject = false;
  330. for (JSObject* current = object; ;) {
  331. if (current->globalObject() == m_globalObject) {
  332. foundGlobalObject = true;
  333. break;
  334. }
  335. JSValue prototypeValue = current->prototype();
  336. if (prototypeValue.isNull())
  337. break;
  338. current = asObject(prototypeValue);
  339. }
  340. if (!foundGlobalObject)
  341. return;
  342. m_foundObjects.append(object);
  343. }
  344. } // end private namespace for helpers for JSGlobalObject::haveABadTime()
  345. void JSGlobalObject::haveABadTime(VM& vm)
  346. {
  347. ASSERT(&vm == &this->vm());
  348. if (isHavingABadTime())
  349. return;
  350. // Make sure that all allocations or indexed storage transitions that are inlining
  351. // the assumption that it's safe to transition to a non-SlowPut array storage don't
  352. // do so anymore.
  353. m_havingABadTimeWatchpoint->notifyWrite();
  354. ASSERT(isHavingABadTime()); // The watchpoint is what tells us that we're having a bad time.
  355. // Make sure that all JSArray allocations that load the appropriate structure from
  356. // this object now load a structure that uses SlowPut.
  357. for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
  358. m_arrayStructureForIndexingShapeDuringAllocation[i].set(vm, this, originalArrayStructureForIndexingType(ArrayWithSlowPutArrayStorage));
  359. // Make sure that all objects that have indexed storage switch to the slow kind of
  360. // indexed storage.
  361. MarkedArgumentBuffer foundObjects; // Use MarkedArgumentBuffer because switchToSlowPutArrayStorage() may GC.
  362. ObjectsWithBrokenIndexingFinder finder(foundObjects, this);
  363. vm.heap.objectSpace().forEachLiveCell(finder);
  364. while (!foundObjects.isEmpty()) {
  365. JSObject* object = asObject(foundObjects.last());
  366. foundObjects.removeLast();
  367. ASSERT(hasBrokenIndexing(object));
  368. object->switchToSlowPutArrayStorage(vm);
  369. }
  370. }
  371. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  372. bool JSGlobalObject::arrayPrototypeChainIsSane()
  373. {
  374. return !hasIndexedProperties(m_arrayPrototype->structure()->indexingType())
  375. && m_arrayPrototype->prototype() == m_objectPrototype.get()
  376. && !hasIndexedProperties(m_objectPrototype->structure()->indexingType())
  377. && m_objectPrototype->prototype().isNull();
  378. }
  379. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  380. void JSGlobalObject::createThrowTypeError(ExecState* exec)
  381. {
  382. JSFunction* thrower = JSFunction::create(exec, this, 0, String(), globalFuncThrowTypeError);
  383. GetterSetter* getterSetter = GetterSetter::create(exec);
  384. getterSetter->setGetter(exec->vm(), thrower);
  385. getterSetter->setSetter(exec->vm(), thrower);
  386. m_throwTypeErrorGetterSetter.set(exec->vm(), this, getterSetter);
  387. }
  388. // Set prototype, and also insert the object prototype at the end of the chain.
  389. void JSGlobalObject::resetPrototype(VM& vm, JSValue prototype)
  390. {
  391. setPrototype(vm, prototype);
  392. JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
  393. JSObject* objectPrototype = m_objectPrototype.get();
  394. if (oldLastInPrototypeChain != objectPrototype)
  395. oldLastInPrototypeChain->setPrototype(vm, objectPrototype);
  396. }
  397. void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
  398. {
  399. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
  400. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  401. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  402. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  403. Base::visitChildren(thisObject, visitor);
  404. visitor.append(&thisObject->m_globalThis);
  405. visitor.append(&thisObject->m_regExpConstructor);
  406. visitor.append(&thisObject->m_errorConstructor);
  407. visitor.append(&thisObject->m_evalErrorConstructor);
  408. visitor.append(&thisObject->m_rangeErrorConstructor);
  409. visitor.append(&thisObject->m_referenceErrorConstructor);
  410. visitor.append(&thisObject->m_syntaxErrorConstructor);
  411. visitor.append(&thisObject->m_typeErrorConstructor);
  412. visitor.append(&thisObject->m_URIErrorConstructor);
  413. visitor.append(&thisObject->m_evalFunction);
  414. visitor.append(&thisObject->m_callFunction);
  415. visitor.append(&thisObject->m_applyFunction);
  416. visitor.append(&thisObject->m_throwTypeErrorGetterSetter);
  417. visitor.append(&thisObject->m_objectPrototype);
  418. visitor.append(&thisObject->m_functionPrototype);
  419. visitor.append(&thisObject->m_arrayPrototype);
  420. visitor.append(&thisObject->m_booleanPrototype);
  421. visitor.append(&thisObject->m_stringPrototype);
  422. visitor.append(&thisObject->m_numberPrototype);
  423. visitor.append(&thisObject->m_datePrototype);
  424. visitor.append(&thisObject->m_regExpPrototype);
  425. visitor.append(&thisObject->m_errorPrototype);
  426. visitor.append(&thisObject->m_withScopeStructure);
  427. visitor.append(&thisObject->m_strictEvalActivationStructure);
  428. visitor.append(&thisObject->m_activationStructure);
  429. visitor.append(&thisObject->m_nameScopeStructure);
  430. visitor.append(&thisObject->m_argumentsStructure);
  431. for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
  432. visitor.append(&thisObject->m_originalArrayStructureForIndexingShape[i]);
  433. for (unsigned i = 0; i < NumberOfIndexingShapes; ++i)
  434. visitor.append(&thisObject->m_arrayStructureForIndexingShapeDuringAllocation[i]);
  435. visitor.append(&thisObject->m_booleanObjectStructure);
  436. visitor.append(&thisObject->m_callbackConstructorStructure);
  437. visitor.append(&thisObject->m_callbackFunctionStructure);
  438. visitor.append(&thisObject->m_callbackObjectStructure);
  439. #if JSC_OBJC_API_ENABLED
  440. visitor.append(&thisObject->m_objcCallbackFunctionStructure);
  441. visitor.append(&thisObject->m_objcWrapperObjectStructure);
  442. #endif
  443. visitor.append(&thisObject->m_dateStructure);
  444. visitor.append(&thisObject->m_nullPrototypeObjectStructure);
  445. visitor.append(&thisObject->m_errorStructure);
  446. visitor.append(&thisObject->m_functionStructure);
  447. visitor.append(&thisObject->m_boundFunctionStructure);
  448. visitor.append(&thisObject->m_namedFunctionStructure);
  449. visitor.append(&thisObject->m_numberObjectStructure);
  450. visitor.append(&thisObject->m_privateNameStructure);
  451. visitor.append(&thisObject->m_regExpMatchesArrayStructure);
  452. visitor.append(&thisObject->m_regExpStructure);
  453. visitor.append(&thisObject->m_stringObjectStructure);
  454. visitor.append(&thisObject->m_internalFunctionStructure);
  455. }
  456. JSObject* JSGlobalObject::toThisObject(JSCell* cell, ExecState*)
  457. {
  458. return jsCast<JSGlobalObject*>(cell)->globalThis();
  459. }
  460. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  461. ExecState* JSGlobalObject::globalExec()
  462. {
  463. return CallFrame::create(m_globalCallFrame + JSStack::CallFrameHeaderSize);
  464. }
  465. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  466. void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
  467. {
  468. addRegisters(count);
  469. for (int i = 0; i < count; ++i) {
  470. GlobalPropertyInfo& global = globals[i];
  471. ASSERT(global.attributes & DontDelete);
  472. int index = symbolTable()->size();
  473. SymbolTableEntry newEntry(index, global.attributes);
  474. symbolTable()->add(global.identifier.impl(), newEntry);
  475. registerAt(index).set(vm(), this, global.value);
  476. }
  477. }
  478. bool JSGlobalObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  479. {
  480. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(cell);
  481. if (getStaticFunctionSlot<Base>(exec, ExecState::globalObjectTable(exec), thisObject, propertyName, slot))
  482. return true;
  483. return symbolTableGet(thisObject, propertyName, slot);
  484. }
  485. bool JSGlobalObject::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  486. {
  487. JSGlobalObject* thisObject = jsCast<JSGlobalObject*>(object);
  488. if (getStaticFunctionDescriptor<Base>(exec, ExecState::globalObjectTable(exec), thisObject, propertyName, descriptor))
  489. return true;
  490. return symbolTableGet(thisObject, propertyName, descriptor);
  491. }
  492. void JSGlobalObject::clearRareData(JSCell* cell)
  493. {
  494. jsCast<JSGlobalObject*>(cell)->m_rareData.clear();
  495. }
  496. DynamicGlobalObjectScope::DynamicGlobalObjectScope(VM& vm, JSGlobalObject* dynamicGlobalObject)
  497. : m_dynamicGlobalObjectSlot(vm.dynamicGlobalObject)
  498. , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
  499. {
  500. if (!m_dynamicGlobalObjectSlot) {
  501. #if ENABLE(ASSEMBLER)
  502. if (ExecutableAllocator::underMemoryPressure())
  503. vm.heap.deleteAllCompiledCode();
  504. #endif
  505. m_dynamicGlobalObjectSlot = dynamicGlobalObject;
  506. // Reset the date cache between JS invocations to force the VM
  507. // to observe time zone changes.
  508. vm.resetDateCache();
  509. }
  510. // Clear the exception stack between entries
  511. vm.clearExceptionStack();
  512. }
  513. void slowValidateCell(JSGlobalObject* globalObject)
  514. {
  515. RELEASE_ASSERT(globalObject->isGlobalObject());
  516. ASSERT_GC_OBJECT_INHERITS(globalObject, &JSGlobalObject::s_info);
  517. }
  518. UnlinkedProgramCodeBlock* JSGlobalObject::createProgramCodeBlock(CallFrame* callFrame, ProgramExecutable* executable, JSObject** exception)
  519. {
  520. ParserError error;
  521. JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
  522. DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
  523. ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
  524. UnlinkedProgramCodeBlock* unlinkedCode = vm().codeCache()->getProgramCodeBlock(vm(), executable, executable->source(), strictness, debuggerMode, profilerMode, error);
  525. if (hasDebugger())
  526. debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
  527. if (error.m_type != ParserError::ErrorNone) {
  528. *exception = error.toErrorObject(this, executable->source());
  529. return 0;
  530. }
  531. return unlinkedCode;
  532. }
  533. UnlinkedEvalCodeBlock* JSGlobalObject::createEvalCodeBlock(CodeCache* cache, CallFrame* callFrame, JSScope* scope, EvalExecutable* executable, JSObject** exception)
  534. {
  535. ParserError error;
  536. JSParserStrictness strictness = executable->isStrictMode() ? JSParseStrict : JSParseNormal;
  537. DebuggerMode debuggerMode = hasDebugger() ? DebuggerOn : DebuggerOff;
  538. ProfilerMode profilerMode = hasProfiler() ? ProfilerOn : ProfilerOff;
  539. UnlinkedEvalCodeBlock* unlinkedCode = cache->getEvalCodeBlock(vm(), scope, executable, executable->source(), strictness, debuggerMode, profilerMode, error);
  540. if (hasDebugger())
  541. debugger()->sourceParsed(callFrame, executable->source().provider(), error.m_line, error.m_message);
  542. if (error.m_type != ParserError::ErrorNone) {
  543. *exception = error.toErrorObject(this, executable->source());
  544. return 0;
  545. }
  546. return unlinkedCode;
  547. }
  548. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  549. } // namespace JSC