JSObject.h 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /*
  2. * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2001 Peter Kelly (pmk@post.com)
  4. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. #ifndef JSObject_h
  23. #define JSObject_h
  24. #include "ArgList.h"
  25. #include "ArrayConventions.h"
  26. #include "ArrayStorage.h"
  27. #include "Butterfly.h"
  28. #include "ClassInfo.h"
  29. #include "CommonIdentifiers.h"
  30. #include "CallFrame.h"
  31. #include "JSCell.h"
  32. #include "PropertySlot.h"
  33. #include "PropertyStorage.h"
  34. #include "PutDirectIndexMode.h"
  35. #include "PutPropertySlot.h"
  36. #include "Structure.h"
  37. #include "VM.h"
  38. #include "JSString.h"
  39. #include "SlotVisitorInlines.h"
  40. #include "SparseArrayValueMap.h"
  41. #include <wtf/StdLibExtras.h>
  42. namespace JSC {
  43. inline JSCell* getJSFunction(JSValue value)
  44. {
  45. if (value.isCell() && (value.asCell()->structure()->typeInfo().type() == JSFunctionType))
  46. return value.asCell();
  47. return 0;
  48. }
  49. JS_EXPORT_PRIVATE JSCell* getCallableObjectSlow(JSCell*);
  50. inline JSCell* getCallableObject(JSValue value)
  51. {
  52. if (!value.isCell())
  53. return 0;
  54. return getCallableObjectSlow(value.asCell());
  55. }
  56. class GetterSetter;
  57. class HashEntry;
  58. class InternalFunction;
  59. class LLIntOffsetsExtractor;
  60. class MarkedBlock;
  61. class PropertyDescriptor;
  62. class PropertyNameArray;
  63. class Structure;
  64. struct HashTable;
  65. JS_EXPORT_PRIVATE JSObject* throwTypeError(ExecState*, const String&);
  66. extern JS_EXPORTDATA const char* StrictModeReadonlyPropertyWriteError;
  67. // ECMA 262-3 8.6.1
  68. // Property attributes
  69. enum Attribute {
  70. None = 0,
  71. ReadOnly = 1 << 1, // property can be only read, not written
  72. DontEnum = 1 << 2, // property doesn't appear in (for .. in ..)
  73. DontDelete = 1 << 3, // property can't be deleted
  74. Function = 1 << 4, // property is a function - only used by static hashtables
  75. Accessor = 1 << 5, // property is a getter/setter
  76. };
  77. COMPILE_ASSERT(None < FirstInternalAttribute, None_is_below_FirstInternalAttribute);
  78. COMPILE_ASSERT(ReadOnly < FirstInternalAttribute, ReadOnly_is_below_FirstInternalAttribute);
  79. COMPILE_ASSERT(DontEnum < FirstInternalAttribute, DontEnum_is_below_FirstInternalAttribute);
  80. COMPILE_ASSERT(DontDelete < FirstInternalAttribute, DontDelete_is_below_FirstInternalAttribute);
  81. COMPILE_ASSERT(Function < FirstInternalAttribute, Function_is_below_FirstInternalAttribute);
  82. COMPILE_ASSERT(Accessor < FirstInternalAttribute, Accessor_is_below_FirstInternalAttribute);
  83. class JSFinalObject;
  84. class JSObject : public JSCell {
  85. friend class BatchedTransitionOptimizer;
  86. friend class JIT;
  87. friend class JSCell;
  88. friend class JSFinalObject;
  89. friend class MarkedBlock;
  90. JS_EXPORT_PRIVATE friend bool setUpStaticFunctionSlot(ExecState*, const HashEntry*, JSObject*, PropertyName, PropertySlot&);
  91. enum PutMode {
  92. PutModePut,
  93. PutModeDefineOwnProperty,
  94. };
  95. public:
  96. typedef JSCell Base;
  97. static size_t allocationSize(size_t inlineCapacity)
  98. {
  99. return sizeof(JSObject) + inlineCapacity * sizeof(WriteBarrierBase<Unknown>);
  100. }
  101. JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
  102. JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&);
  103. JS_EXPORT_PRIVATE static String className(const JSObject*);
  104. JSValue prototype() const;
  105. void setPrototype(VM&, JSValue prototype);
  106. bool setPrototypeWithCycleCheck(VM&, JSValue prototype);
  107. bool mayInterceptIndexedAccesses()
  108. {
  109. return structure()->mayInterceptIndexedAccesses();
  110. }
  111. JSValue get(ExecState*, PropertyName) const;
  112. JSValue get(ExecState*, unsigned propertyName) const;
  113. bool getPropertySlot(ExecState*, PropertyName, PropertySlot&);
  114. bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
  115. JS_EXPORT_PRIVATE bool getPropertyDescriptor(ExecState*, PropertyName, PropertyDescriptor&);
  116. static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
  117. JS_EXPORT_PRIVATE static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
  118. JS_EXPORT_PRIVATE static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
  119. bool allowsAccessFrom(ExecState*);
  120. unsigned getArrayLength() const
  121. {
  122. if (!hasIndexedProperties(structure()->indexingType()))
  123. return 0;
  124. return m_butterfly->publicLength();
  125. }
  126. unsigned getVectorLength()
  127. {
  128. if (!hasIndexedProperties(structure()->indexingType()))
  129. return 0;
  130. return m_butterfly->vectorLength();
  131. }
  132. JS_EXPORT_PRIVATE static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
  133. JS_EXPORT_PRIVATE static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
  134. void putByIndexInline(ExecState* exec, unsigned propertyName, JSValue value, bool shouldThrow)
  135. {
  136. if (canSetIndexQuickly(propertyName)) {
  137. setIndexQuickly(exec->vm(), propertyName, value);
  138. return;
  139. }
  140. methodTable()->putByIndex(this, exec, propertyName, value, shouldThrow);
  141. }
  142. // This is similar to the putDirect* methods:
  143. // - the prototype chain is not consulted
  144. // - accessors are not called.
  145. // - it will ignore extensibility and read-only properties if PutDirectIndexLikePutDirect is passed as the mode (the default).
  146. // This method creates a property with attributes writable, enumerable and configurable all set to true.
  147. bool putDirectIndex(ExecState* exec, unsigned propertyName, JSValue value, unsigned attributes, PutDirectIndexMode mode)
  148. {
  149. if (!attributes && canSetIndexQuicklyForPutDirect(propertyName)) {
  150. setIndexQuickly(exec->vm(), propertyName, value);
  151. return true;
  152. }
  153. return putDirectIndexBeyondVectorLength(exec, propertyName, value, attributes, mode);
  154. }
  155. bool putDirectIndex(ExecState* exec, unsigned propertyName, JSValue value)
  156. {
  157. return putDirectIndex(exec, propertyName, value, 0, PutDirectIndexLikePutDirect);
  158. }
  159. // A non-throwing version of putDirect and putDirectIndex.
  160. JS_EXPORT_PRIVATE void putDirectMayBeIndex(ExecState*, PropertyName, JSValue);
  161. bool canGetIndexQuickly(unsigned i)
  162. {
  163. switch (structure()->indexingType()) {
  164. case ALL_BLANK_INDEXING_TYPES:
  165. case ALL_UNDECIDED_INDEXING_TYPES:
  166. return false;
  167. case ALL_INT32_INDEXING_TYPES:
  168. case ALL_CONTIGUOUS_INDEXING_TYPES:
  169. return i < m_butterfly->vectorLength() && m_butterfly->contiguous()[i];
  170. case ALL_DOUBLE_INDEXING_TYPES: {
  171. if (i >= m_butterfly->vectorLength())
  172. return false;
  173. double value = m_butterfly->contiguousDouble()[i];
  174. if (value != value)
  175. return false;
  176. return true;
  177. }
  178. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  179. return i < m_butterfly->arrayStorage()->vectorLength() && m_butterfly->arrayStorage()->m_vector[i];
  180. default:
  181. RELEASE_ASSERT_NOT_REACHED();
  182. return false;
  183. }
  184. }
  185. JSValue getIndexQuickly(unsigned i)
  186. {
  187. switch (structure()->indexingType()) {
  188. case ALL_INT32_INDEXING_TYPES:
  189. case ALL_CONTIGUOUS_INDEXING_TYPES:
  190. return m_butterfly->contiguous()[i].get();
  191. case ALL_DOUBLE_INDEXING_TYPES:
  192. return JSValue(JSValue::EncodeAsDouble, m_butterfly->contiguousDouble()[i]);
  193. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  194. return m_butterfly->arrayStorage()->m_vector[i].get();
  195. default:
  196. RELEASE_ASSERT_NOT_REACHED();
  197. return JSValue();
  198. }
  199. }
  200. JSValue tryGetIndexQuickly(unsigned i)
  201. {
  202. switch (structure()->indexingType()) {
  203. case ALL_BLANK_INDEXING_TYPES:
  204. case ALL_UNDECIDED_INDEXING_TYPES:
  205. break;
  206. case ALL_INT32_INDEXING_TYPES:
  207. case ALL_CONTIGUOUS_INDEXING_TYPES:
  208. if (i < m_butterfly->publicLength())
  209. return m_butterfly->contiguous()[i].get();
  210. break;
  211. case ALL_DOUBLE_INDEXING_TYPES: {
  212. if (i >= m_butterfly->publicLength())
  213. break;
  214. double result = m_butterfly->contiguousDouble()[i];
  215. if (result != result)
  216. break;
  217. return JSValue(JSValue::EncodeAsDouble, result);
  218. }
  219. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  220. if (i < m_butterfly->arrayStorage()->vectorLength())
  221. return m_butterfly->arrayStorage()->m_vector[i].get();
  222. break;
  223. default:
  224. RELEASE_ASSERT_NOT_REACHED();
  225. break;
  226. }
  227. return JSValue();
  228. }
  229. JSValue getDirectIndex(ExecState* exec, unsigned i)
  230. {
  231. if (JSValue result = tryGetIndexQuickly(i))
  232. return result;
  233. PropertySlot slot(this);
  234. if (methodTable()->getOwnPropertySlotByIndex(this, exec, i, slot))
  235. return slot.getValue(exec, i);
  236. return JSValue();
  237. }
  238. JSValue getIndex(ExecState* exec, unsigned i)
  239. {
  240. if (JSValue result = tryGetIndexQuickly(i))
  241. return result;
  242. return get(exec, i);
  243. }
  244. bool canSetIndexQuickly(unsigned i)
  245. {
  246. switch (structure()->indexingType()) {
  247. case ALL_BLANK_INDEXING_TYPES:
  248. case ALL_UNDECIDED_INDEXING_TYPES:
  249. return false;
  250. case ALL_INT32_INDEXING_TYPES:
  251. case ALL_DOUBLE_INDEXING_TYPES:
  252. case ALL_CONTIGUOUS_INDEXING_TYPES:
  253. case NonArrayWithArrayStorage:
  254. case ArrayWithArrayStorage:
  255. return i < m_butterfly->vectorLength();
  256. case NonArrayWithSlowPutArrayStorage:
  257. case ArrayWithSlowPutArrayStorage:
  258. return i < m_butterfly->arrayStorage()->vectorLength()
  259. && !!m_butterfly->arrayStorage()->m_vector[i];
  260. default:
  261. RELEASE_ASSERT_NOT_REACHED();
  262. return false;
  263. }
  264. }
  265. bool canSetIndexQuicklyForPutDirect(unsigned i)
  266. {
  267. switch (structure()->indexingType()) {
  268. case ALL_BLANK_INDEXING_TYPES:
  269. case ALL_UNDECIDED_INDEXING_TYPES:
  270. return false;
  271. case ALL_INT32_INDEXING_TYPES:
  272. case ALL_DOUBLE_INDEXING_TYPES:
  273. case ALL_CONTIGUOUS_INDEXING_TYPES:
  274. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  275. return i < m_butterfly->vectorLength();
  276. default:
  277. RELEASE_ASSERT_NOT_REACHED();
  278. return false;
  279. }
  280. }
  281. void setIndexQuickly(VM& vm, unsigned i, JSValue v)
  282. {
  283. switch (structure()->indexingType()) {
  284. case ALL_INT32_INDEXING_TYPES: {
  285. ASSERT(i < m_butterfly->vectorLength());
  286. if (!v.isInt32()) {
  287. convertInt32ToDoubleOrContiguousWhilePerformingSetIndex(vm, i, v);
  288. return;
  289. }
  290. // Fall through to contiguous case.
  291. }
  292. case ALL_CONTIGUOUS_INDEXING_TYPES: {
  293. ASSERT(i < m_butterfly->vectorLength());
  294. m_butterfly->contiguous()[i].set(vm, this, v);
  295. if (i >= m_butterfly->publicLength())
  296. m_butterfly->setPublicLength(i + 1);
  297. break;
  298. }
  299. case ALL_DOUBLE_INDEXING_TYPES: {
  300. ASSERT(i < m_butterfly->vectorLength());
  301. if (!v.isNumber()) {
  302. convertDoubleToContiguousWhilePerformingSetIndex(vm, i, v);
  303. return;
  304. }
  305. double value = v.asNumber();
  306. if (value != value) {
  307. convertDoubleToContiguousWhilePerformingSetIndex(vm, i, v);
  308. return;
  309. }
  310. m_butterfly->contiguousDouble()[i] = value;
  311. if (i >= m_butterfly->publicLength())
  312. m_butterfly->setPublicLength(i + 1);
  313. break;
  314. }
  315. case ALL_ARRAY_STORAGE_INDEXING_TYPES: {
  316. ArrayStorage* storage = m_butterfly->arrayStorage();
  317. WriteBarrier<Unknown>& x = storage->m_vector[i];
  318. JSValue old = x.get();
  319. x.set(vm, this, v);
  320. if (!old) {
  321. ++storage->m_numValuesInVector;
  322. if (i >= storage->length())
  323. storage->setLength(i + 1);
  324. }
  325. break;
  326. }
  327. default:
  328. RELEASE_ASSERT_NOT_REACHED();
  329. }
  330. }
  331. void initializeIndex(VM& vm, unsigned i, JSValue v)
  332. {
  333. switch (structure()->indexingType()) {
  334. case ALL_UNDECIDED_INDEXING_TYPES: {
  335. setIndexQuicklyToUndecided(vm, i, v);
  336. break;
  337. }
  338. case ALL_INT32_INDEXING_TYPES: {
  339. ASSERT(i < m_butterfly->publicLength());
  340. ASSERT(i < m_butterfly->vectorLength());
  341. if (!v.isInt32()) {
  342. convertInt32ToDoubleOrContiguousWhilePerformingSetIndex(vm, i, v);
  343. break;
  344. }
  345. // Fall through.
  346. }
  347. case ALL_CONTIGUOUS_INDEXING_TYPES: {
  348. ASSERT(i < m_butterfly->publicLength());
  349. ASSERT(i < m_butterfly->vectorLength());
  350. m_butterfly->contiguous()[i].set(vm, this, v);
  351. break;
  352. }
  353. case ALL_DOUBLE_INDEXING_TYPES: {
  354. ASSERT(i < m_butterfly->publicLength());
  355. ASSERT(i < m_butterfly->vectorLength());
  356. if (!v.isNumber()) {
  357. convertDoubleToContiguousWhilePerformingSetIndex(vm, i, v);
  358. return;
  359. }
  360. double value = v.asNumber();
  361. if (value != value) {
  362. convertDoubleToContiguousWhilePerformingSetIndex(vm, i, v);
  363. return;
  364. }
  365. m_butterfly->contiguousDouble()[i] = value;
  366. break;
  367. }
  368. case ALL_ARRAY_STORAGE_INDEXING_TYPES: {
  369. ArrayStorage* storage = m_butterfly->arrayStorage();
  370. ASSERT(i < storage->length());
  371. ASSERT(i < storage->m_numValuesInVector);
  372. storage->m_vector[i].set(vm, this, v);
  373. break;
  374. }
  375. default:
  376. RELEASE_ASSERT_NOT_REACHED();
  377. }
  378. }
  379. bool hasSparseMap()
  380. {
  381. switch (structure()->indexingType()) {
  382. case ALL_BLANK_INDEXING_TYPES:
  383. case ALL_UNDECIDED_INDEXING_TYPES:
  384. case ALL_INT32_INDEXING_TYPES:
  385. case ALL_DOUBLE_INDEXING_TYPES:
  386. case ALL_CONTIGUOUS_INDEXING_TYPES:
  387. return false;
  388. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  389. return m_butterfly->arrayStorage()->m_sparseMap;
  390. default:
  391. RELEASE_ASSERT_NOT_REACHED();
  392. return false;
  393. }
  394. }
  395. bool inSparseIndexingMode()
  396. {
  397. switch (structure()->indexingType()) {
  398. case ALL_BLANK_INDEXING_TYPES:
  399. case ALL_UNDECIDED_INDEXING_TYPES:
  400. case ALL_INT32_INDEXING_TYPES:
  401. case ALL_DOUBLE_INDEXING_TYPES:
  402. case ALL_CONTIGUOUS_INDEXING_TYPES:
  403. return false;
  404. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  405. return m_butterfly->arrayStorage()->inSparseMode();
  406. default:
  407. RELEASE_ASSERT_NOT_REACHED();
  408. return false;
  409. }
  410. }
  411. void enterDictionaryIndexingMode(VM&);
  412. // putDirect is effectively an unchecked vesion of 'defineOwnProperty':
  413. // - the prototype chain is not consulted
  414. // - accessors are not called.
  415. // - attributes will be respected (after the call the property will exist with the given attributes)
  416. // - the property name is assumed to not be an index.
  417. JS_EXPORT_PRIVATE static void putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned attributes);
  418. void putDirect(VM&, PropertyName, JSValue, unsigned attributes = 0);
  419. void putDirect(VM&, PropertyName, JSValue, PutPropertySlot&);
  420. void putDirectWithoutTransition(VM&, PropertyName, JSValue, unsigned attributes = 0);
  421. void putDirectAccessor(ExecState*, PropertyName, JSValue, unsigned attributes);
  422. bool propertyIsEnumerable(ExecState*, const Identifier& propertyName) const;
  423. JS_EXPORT_PRIVATE bool hasProperty(ExecState*, PropertyName) const;
  424. JS_EXPORT_PRIVATE bool hasProperty(ExecState*, unsigned propertyName) const;
  425. bool hasOwnProperty(ExecState*, PropertyName) const;
  426. JS_EXPORT_PRIVATE static bool deleteProperty(JSCell*, ExecState*, PropertyName);
  427. JS_EXPORT_PRIVATE static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
  428. JS_EXPORT_PRIVATE static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
  429. bool hasInstance(ExecState*, JSValue);
  430. static bool defaultHasInstance(ExecState*, JSValue, JSValue prototypeProperty);
  431. JS_EXPORT_PRIVATE static void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  432. JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  433. JS_EXPORT_PRIVATE static void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  434. JSValue toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
  435. bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const;
  436. JS_EXPORT_PRIVATE double toNumber(ExecState*) const;
  437. JS_EXPORT_PRIVATE JSString* toString(ExecState*) const;
  438. // NOTE: JSObject and its subclasses must be able to gracefully handle ExecState* = 0,
  439. // because this call may come from inside the compiler.
  440. JS_EXPORT_PRIVATE static JSObject* toThisObject(JSCell*, ExecState*);
  441. bool getPropertySpecificValue(ExecState*, PropertyName, JSCell*& specificFunction) const;
  442. // This get function only looks at the property map.
  443. JSValue getDirect(VM& vm, PropertyName propertyName) const
  444. {
  445. PropertyOffset offset = structure()->get(vm, propertyName);
  446. checkOffset(offset, structure()->inlineCapacity());
  447. return offset != invalidOffset ? getDirect(offset) : JSValue();
  448. }
  449. PropertyOffset getDirectOffset(VM& vm, PropertyName propertyName)
  450. {
  451. PropertyOffset offset = structure()->get(vm, propertyName);
  452. checkOffset(offset, structure()->inlineCapacity());
  453. return offset;
  454. }
  455. bool hasInlineStorage() const { return structure()->hasInlineStorage(); }
  456. ConstPropertyStorage inlineStorageUnsafe() const
  457. {
  458. return bitwise_cast<ConstPropertyStorage>(this + 1);
  459. }
  460. PropertyStorage inlineStorageUnsafe()
  461. {
  462. return bitwise_cast<PropertyStorage>(this + 1);
  463. }
  464. ConstPropertyStorage inlineStorage() const
  465. {
  466. ASSERT(hasInlineStorage());
  467. return inlineStorageUnsafe();
  468. }
  469. PropertyStorage inlineStorage()
  470. {
  471. ASSERT(hasInlineStorage());
  472. return inlineStorageUnsafe();
  473. }
  474. const Butterfly* butterfly() const { return m_butterfly; }
  475. Butterfly* butterfly() { return m_butterfly; }
  476. ConstPropertyStorage outOfLineStorage() const { return m_butterfly->propertyStorage(); }
  477. PropertyStorage outOfLineStorage() { return m_butterfly->propertyStorage(); }
  478. const WriteBarrierBase<Unknown>* locationForOffset(PropertyOffset offset) const
  479. {
  480. if (isInlineOffset(offset))
  481. return &inlineStorage()[offsetInInlineStorage(offset)];
  482. return &outOfLineStorage()[offsetInOutOfLineStorage(offset)];
  483. }
  484. WriteBarrierBase<Unknown>* locationForOffset(PropertyOffset offset)
  485. {
  486. if (isInlineOffset(offset))
  487. return &inlineStorage()[offsetInInlineStorage(offset)];
  488. return &outOfLineStorage()[offsetInOutOfLineStorage(offset)];
  489. }
  490. void transitionTo(VM&, Structure*);
  491. bool removeDirect(VM&, PropertyName); // Return true if anything is removed.
  492. bool hasCustomProperties() { return structure()->didTransition(); }
  493. bool hasGetterSetterProperties() { return structure()->hasGetterSetterProperties(); }
  494. // putOwnDataProperty has 'put' like semantics, however this method:
  495. // - assumes the object contains no own getter/setter properties.
  496. // - provides no special handling for __proto__
  497. // - does not walk the prototype chain (to check for accessors or non-writable properties).
  498. // This is used by JSActivation.
  499. bool putOwnDataProperty(VM&, PropertyName, JSValue, PutPropertySlot&);
  500. // Fast access to known property offsets.
  501. JSValue getDirect(PropertyOffset offset) const { return locationForOffset(offset)->get(); }
  502. void putDirect(VM& vm, PropertyOffset offset, JSValue value) { locationForOffset(offset)->set(vm, this, value); }
  503. void putDirectUndefined(PropertyOffset offset) { locationForOffset(offset)->setUndefined(); }
  504. void putDirectNativeFunction(ExecState*, JSGlobalObject*, const PropertyName&, unsigned functionLength, NativeFunction, Intrinsic, unsigned attributes);
  505. JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
  506. bool isGlobalObject() const;
  507. bool isVariableObject() const;
  508. bool isStaticScopeObject() const;
  509. bool isNameScopeObject() const;
  510. bool isActivationObject() const;
  511. bool isErrorInstance() const;
  512. void seal(VM&);
  513. void freeze(VM&);
  514. JS_EXPORT_PRIVATE void preventExtensions(VM&);
  515. bool isSealed(VM& vm) { return structure()->isSealed(vm); }
  516. bool isFrozen(VM& vm) { return structure()->isFrozen(vm); }
  517. bool isExtensible() { return structure()->isExtensible(); }
  518. bool indexingShouldBeSparse()
  519. {
  520. return !isExtensible()
  521. || structure()->typeInfo().interceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero();
  522. }
  523. bool staticFunctionsReified() { return structure()->staticFunctionsReified(); }
  524. void reifyStaticFunctionsForDelete(ExecState* exec);
  525. JS_EXPORT_PRIVATE Butterfly* growOutOfLineStorage(VM&, size_t oldSize, size_t newSize);
  526. void setButterfly(VM&, Butterfly*, Structure*);
  527. void setButterflyWithoutChangingStructure(Butterfly*); // You probably don't want to call this.
  528. void setStructureAndReallocateStorageIfNecessary(VM&, unsigned oldCapacity, Structure*);
  529. void setStructureAndReallocateStorageIfNecessary(VM&, Structure*);
  530. void flattenDictionaryObject(VM& vm)
  531. {
  532. structure()->flattenDictionaryStructure(vm, this);
  533. }
  534. JSGlobalObject* globalObject() const
  535. {
  536. ASSERT(structure()->globalObject());
  537. ASSERT(!isGlobalObject() || ((JSObject*)structure()->globalObject()) == this);
  538. return structure()->globalObject();
  539. }
  540. void switchToSlowPutArrayStorage(VM&);
  541. // The receiver is the prototype in this case. The following:
  542. //
  543. // asObject(foo->structure()->storedPrototype())->attemptToInterceptPutByIndexOnHoleForPrototype(...)
  544. //
  545. // is equivalent to:
  546. //
  547. // foo->attemptToInterceptPutByIndexOnHole(...);
  548. bool attemptToInterceptPutByIndexOnHoleForPrototype(ExecState*, JSValue thisValue, unsigned propertyName, JSValue, bool shouldThrow);
  549. // Returns 0 if int32 storage cannot be created - either because
  550. // indexing should be sparse, we're having a bad time, or because
  551. // we already have a more general form of storage (double,
  552. // contiguous, array storage).
  553. ContiguousJSValues ensureInt32(VM& vm)
  554. {
  555. if (LIKELY(hasInt32(structure()->indexingType())))
  556. return m_butterfly->contiguousInt32();
  557. return ensureInt32Slow(vm);
  558. }
  559. // Returns 0 if double storage cannot be created - either because
  560. // indexing should be sparse, we're having a bad time, or because
  561. // we already have a more general form of storage (contiguous,
  562. // or array storage).
  563. ContiguousDoubles ensureDouble(VM& vm)
  564. {
  565. if (LIKELY(hasDouble(structure()->indexingType())))
  566. return m_butterfly->contiguousDouble();
  567. return ensureDoubleSlow(vm);
  568. }
  569. // Returns 0 if contiguous storage cannot be created - either because
  570. // indexing should be sparse or because we're having a bad time.
  571. ContiguousJSValues ensureContiguous(VM& vm)
  572. {
  573. if (LIKELY(hasContiguous(structure()->indexingType())))
  574. return m_butterfly->contiguous();
  575. return ensureContiguousSlow(vm);
  576. }
  577. // Same as ensureContiguous(), except that if the indexed storage is in
  578. // double mode, then it does a rage conversion to contiguous: it
  579. // attempts to convert each double to an int32.
  580. ContiguousJSValues rageEnsureContiguous(VM& vm)
  581. {
  582. if (LIKELY(hasContiguous(structure()->indexingType())))
  583. return m_butterfly->contiguous();
  584. return rageEnsureContiguousSlow(vm);
  585. }
  586. // Ensure that the object is in a mode where it has array storage. Use
  587. // this if you're about to perform actions that would have required the
  588. // object to be converted to have array storage, if it didn't have it
  589. // already.
  590. ArrayStorage* ensureArrayStorage(VM& vm)
  591. {
  592. if (LIKELY(hasArrayStorage(structure()->indexingType())))
  593. return m_butterfly->arrayStorage();
  594. return ensureArrayStorageSlow(vm);
  595. }
  596. static size_t offsetOfInlineStorage();
  597. static ptrdiff_t butterflyOffset()
  598. {
  599. return OBJECT_OFFSETOF(JSObject, m_butterfly);
  600. }
  601. void* butterflyAddress()
  602. {
  603. return &m_butterfly;
  604. }
  605. #if ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT
  606. private:
  607. static JS_EXPORTDATA const ClassInfo s_info;
  608. #else
  609. public:
  610. static JS_EXPORTDATA const ClassInfo s_info;
  611. #endif
  612. protected:
  613. void finishCreation(VM& vm)
  614. {
  615. DEFINE_STATIC_sJSObjectClassInfo;
  616. Base::finishCreation(vm);
  617. ASSERT(inherits(sJSObjectClassInfo));
  618. ASSERT(!structure()->outOfLineCapacity());
  619. ASSERT(structure()->isEmpty());
  620. ASSERT(prototype().isNull() || Heap::heap(this) == Heap::heap(prototype()));
  621. ASSERT(structure()->isObject());
  622. ASSERT(classInfo());
  623. }
  624. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  625. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  626. {
  627. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
  628. }
  629. #endif
  630. // To instantiate objects you likely want JSFinalObject, below.
  631. // To create derived types you likely want JSNonFinalObject, below.
  632. JSObject(VM&, Structure*, Butterfly* = 0);
  633. void visitButterfly(SlotVisitor&, Butterfly*, size_t storageSize);
  634. void copyButterfly(CopyVisitor&, Butterfly*, size_t storageSize);
  635. // Call this if you know that the object is in a mode where it has array
  636. // storage. This will assert otherwise.
  637. ArrayStorage* arrayStorage()
  638. {
  639. ASSERT(hasArrayStorage(structure()->indexingType()));
  640. return m_butterfly->arrayStorage();
  641. }
  642. // Call this if you want to predicate some actions on whether or not the
  643. // object is in a mode where it has array storage.
  644. ArrayStorage* arrayStorageOrNull()
  645. {
  646. switch (structure()->indexingType()) {
  647. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  648. return m_butterfly->arrayStorage();
  649. default:
  650. return 0;
  651. }
  652. }
  653. Butterfly* createInitialUndecided(VM&, unsigned length);
  654. ContiguousJSValues createInitialInt32(VM&, unsigned length);
  655. ContiguousDoubles createInitialDouble(VM&, unsigned length);
  656. ContiguousJSValues createInitialContiguous(VM&, unsigned length);
  657. void convertUndecidedForValue(VM&, JSValue);
  658. void convertInt32ForValue(VM&, JSValue);
  659. ArrayStorage* createArrayStorage(VM&, unsigned length, unsigned vectorLength);
  660. ArrayStorage* createInitialArrayStorage(VM&);
  661. ContiguousJSValues convertUndecidedToInt32(VM&);
  662. ContiguousDoubles convertUndecidedToDouble(VM&);
  663. ContiguousJSValues convertUndecidedToContiguous(VM&);
  664. ArrayStorage* convertUndecidedToArrayStorage(VM&, NonPropertyTransition, unsigned neededLength);
  665. ArrayStorage* convertUndecidedToArrayStorage(VM&, NonPropertyTransition);
  666. ArrayStorage* convertUndecidedToArrayStorage(VM&);
  667. ContiguousDoubles convertInt32ToDouble(VM&);
  668. ContiguousJSValues convertInt32ToContiguous(VM&);
  669. ArrayStorage* convertInt32ToArrayStorage(VM&, NonPropertyTransition, unsigned neededLength);
  670. ArrayStorage* convertInt32ToArrayStorage(VM&, NonPropertyTransition);
  671. ArrayStorage* convertInt32ToArrayStorage(VM&);
  672. ContiguousJSValues convertDoubleToContiguous(VM&);
  673. ContiguousJSValues rageConvertDoubleToContiguous(VM&);
  674. ArrayStorage* convertDoubleToArrayStorage(VM&, NonPropertyTransition, unsigned neededLength);
  675. ArrayStorage* convertDoubleToArrayStorage(VM&, NonPropertyTransition);
  676. ArrayStorage* convertDoubleToArrayStorage(VM&);
  677. ArrayStorage* convertContiguousToArrayStorage(VM&, NonPropertyTransition, unsigned neededLength);
  678. ArrayStorage* convertContiguousToArrayStorage(VM&, NonPropertyTransition);
  679. ArrayStorage* convertContiguousToArrayStorage(VM&);
  680. ArrayStorage* ensureArrayStorageExistsAndEnterDictionaryIndexingMode(VM&);
  681. bool defineOwnNonIndexProperty(ExecState*, PropertyName, PropertyDescriptor&, bool throwException);
  682. template<IndexingType indexingShape>
  683. void putByIndexBeyondVectorLengthWithoutAttributes(ExecState*, unsigned propertyName, JSValue);
  684. void putByIndexBeyondVectorLengthWithArrayStorage(ExecState*, unsigned propertyName, JSValue, bool shouldThrow, ArrayStorage*);
  685. bool increaseVectorLength(VM&, unsigned newLength);
  686. void deallocateSparseIndexMap();
  687. bool defineOwnIndexedProperty(ExecState*, unsigned, PropertyDescriptor&, bool throwException);
  688. SparseArrayValueMap* allocateSparseIndexMap(VM&);
  689. void notifyPresenceOfIndexedAccessors(VM&);
  690. bool attemptToInterceptPutByIndexOnHole(ExecState*, unsigned index, JSValue, bool shouldThrow);
  691. // Call this if you want setIndexQuickly to succeed and you're sure that
  692. // the array is contiguous.
  693. void ensureLength(VM& vm, unsigned length)
  694. {
  695. ASSERT(length < MAX_ARRAY_INDEX);
  696. ASSERT(hasContiguous(structure()->indexingType()) || hasInt32(structure()->indexingType()) || hasDouble(structure()->indexingType()) || hasUndecided(structure()->indexingType()));
  697. if (m_butterfly->vectorLength() < length)
  698. ensureLengthSlow(vm, length);
  699. if (m_butterfly->publicLength() < length)
  700. m_butterfly->setPublicLength(length);
  701. }
  702. template<IndexingType indexingShape>
  703. unsigned countElements(Butterfly*);
  704. // This is relevant to undecided, int32, double, and contiguous.
  705. unsigned countElements();
  706. // This strange method returns a pointer to the start of the indexed data
  707. // as if it contained JSValues. But it won't always contain JSValues.
  708. // Make sure you cast this to the appropriate type before using.
  709. template<IndexingType indexingType>
  710. ContiguousJSValues indexingData()
  711. {
  712. switch (indexingType) {
  713. case ALL_INT32_INDEXING_TYPES:
  714. case ALL_DOUBLE_INDEXING_TYPES:
  715. case ALL_CONTIGUOUS_INDEXING_TYPES:
  716. return m_butterfly->contiguous();
  717. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  718. return m_butterfly->arrayStorage()->vector();
  719. default:
  720. CRASH();
  721. return ContiguousJSValues();
  722. }
  723. }
  724. ContiguousJSValues currentIndexingData()
  725. {
  726. switch (structure()->indexingType()) {
  727. case ALL_INT32_INDEXING_TYPES:
  728. case ALL_CONTIGUOUS_INDEXING_TYPES:
  729. return m_butterfly->contiguous();
  730. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  731. return m_butterfly->arrayStorage()->vector();
  732. default:
  733. CRASH();
  734. return ContiguousJSValues();
  735. }
  736. }
  737. JSValue getHolyIndexQuickly(unsigned i)
  738. {
  739. ASSERT(i < m_butterfly->vectorLength());
  740. switch (structure()->indexingType()) {
  741. case ALL_INT32_INDEXING_TYPES:
  742. case ALL_CONTIGUOUS_INDEXING_TYPES:
  743. return m_butterfly->contiguous()[i].get();
  744. case ALL_DOUBLE_INDEXING_TYPES: {
  745. double value = m_butterfly->contiguousDouble()[i];
  746. if (value == value)
  747. return JSValue(JSValue::EncodeAsDouble, value);
  748. return JSValue();
  749. }
  750. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  751. return m_butterfly->arrayStorage()->m_vector[i].get();
  752. default:
  753. CRASH();
  754. return JSValue();
  755. }
  756. }
  757. template<IndexingType indexingType>
  758. unsigned relevantLength()
  759. {
  760. switch (indexingType) {
  761. case ALL_INT32_INDEXING_TYPES:
  762. case ALL_DOUBLE_INDEXING_TYPES:
  763. case ALL_CONTIGUOUS_INDEXING_TYPES:
  764. return m_butterfly->publicLength();
  765. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  766. return std::min(
  767. m_butterfly->arrayStorage()->length(),
  768. m_butterfly->arrayStorage()->vectorLength());
  769. default:
  770. CRASH();
  771. return 0;
  772. }
  773. }
  774. unsigned currentRelevantLength()
  775. {
  776. switch (structure()->indexingType()) {
  777. case ALL_INT32_INDEXING_TYPES:
  778. case ALL_DOUBLE_INDEXING_TYPES:
  779. case ALL_CONTIGUOUS_INDEXING_TYPES:
  780. return m_butterfly->publicLength();
  781. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  782. return std::min(
  783. m_butterfly->arrayStorage()->length(),
  784. m_butterfly->arrayStorage()->vectorLength());
  785. default:
  786. CRASH();
  787. return 0;
  788. }
  789. }
  790. private:
  791. friend class LLIntOffsetsExtractor;
  792. // Nobody should ever ask any of these questions on something already known to be a JSObject.
  793. using JSCell::isAPIValueWrapper;
  794. using JSCell::isGetterSetter;
  795. void getObject();
  796. void getString(ExecState* exec);
  797. void isObject();
  798. void isString();
  799. Butterfly* createInitialIndexedStorage(VM&, unsigned length, size_t elementSize);
  800. ArrayStorage* enterDictionaryIndexingModeWhenArrayStorageAlreadyExists(VM&, ArrayStorage*);
  801. template<PutMode>
  802. bool putDirectInternal(VM&, PropertyName, JSValue, unsigned attr, PutPropertySlot&, JSCell*);
  803. bool inlineGetOwnPropertySlot(ExecState*, PropertyName, PropertySlot&);
  804. JS_EXPORT_PRIVATE void fillGetterPropertySlot(PropertySlot&, PropertyOffset);
  805. const HashEntry* findPropertyHashEntry(ExecState*, PropertyName) const;
  806. void putIndexedDescriptor(ExecState*, SparseArrayEntry*, PropertyDescriptor&, PropertyDescriptor& old);
  807. void putByIndexBeyondVectorLength(ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
  808. bool putDirectIndexBeyondVectorLengthWithArrayStorage(ExecState*, unsigned propertyName, JSValue, unsigned attributes, PutDirectIndexMode, ArrayStorage*);
  809. JS_EXPORT_PRIVATE bool putDirectIndexBeyondVectorLength(ExecState*, unsigned propertyName, JSValue, unsigned attributes, PutDirectIndexMode);
  810. unsigned getNewVectorLength(unsigned currentVectorLength, unsigned currentLength, unsigned desiredLength);
  811. unsigned getNewVectorLength(unsigned desiredLength);
  812. JS_EXPORT_PRIVATE bool getOwnPropertySlotSlow(ExecState*, PropertyName, PropertySlot&);
  813. ArrayStorage* constructConvertedArrayStorageWithoutCopyingElements(VM&, unsigned neededLength);
  814. JS_EXPORT_PRIVATE void setIndexQuicklyToUndecided(VM&, unsigned index, JSValue);
  815. JS_EXPORT_PRIVATE void convertInt32ToDoubleOrContiguousWhilePerformingSetIndex(VM&, unsigned index, JSValue);
  816. JS_EXPORT_PRIVATE void convertDoubleToContiguousWhilePerformingSetIndex(VM&, unsigned index, JSValue);
  817. void ensureLengthSlow(VM&, unsigned length);
  818. ContiguousJSValues ensureInt32Slow(VM&);
  819. ContiguousDoubles ensureDoubleSlow(VM&);
  820. ContiguousJSValues ensureContiguousSlow(VM&);
  821. ContiguousJSValues rageEnsureContiguousSlow(VM&);
  822. ArrayStorage* ensureArrayStorageSlow(VM&);
  823. enum DoubleToContiguousMode { EncodeValueAsDouble, RageConvertDoubleToValue };
  824. template<DoubleToContiguousMode mode>
  825. ContiguousJSValues genericConvertDoubleToContiguous(VM&);
  826. ContiguousJSValues ensureContiguousSlow(VM&, DoubleToContiguousMode);
  827. protected:
  828. Butterfly* m_butterfly;
  829. };
  830. // JSNonFinalObject is a type of JSObject that has some internal storage,
  831. // but also preserves some space in the collector cell for additional
  832. // data members in derived types.
  833. class JSNonFinalObject : public JSObject {
  834. friend class JSObject;
  835. public:
  836. typedef JSObject Base;
  837. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  838. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  839. {
  840. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
  841. }
  842. #endif
  843. protected:
  844. explicit JSNonFinalObject(VM& vm, Structure* structure, Butterfly* butterfly = 0)
  845. : JSObject(vm, structure, butterfly)
  846. {
  847. }
  848. void finishCreation(VM& vm)
  849. {
  850. Base::finishCreation(vm);
  851. ASSERT(!this->structure()->totalStorageCapacity());
  852. ASSERT(classInfo());
  853. }
  854. };
  855. class JSFinalObject;
  856. // JSFinalObject is a type of JSObject that contains sufficent internal
  857. // storage to fully make use of the colloctor cell containing it.
  858. class JSFinalObject : public JSObject {
  859. friend class JSObject;
  860. public:
  861. typedef JSObject Base;
  862. static const unsigned defaultSize = 64;
  863. static inline unsigned defaultInlineCapacity()
  864. {
  865. return (defaultSize - allocationSize(0)) / sizeof(WriteBarrier<Unknown>);
  866. }
  867. static const unsigned maxSize = 512;
  868. static inline unsigned maxInlineCapacity()
  869. {
  870. return (maxSize - allocationSize(0)) / sizeof(WriteBarrier<Unknown>);
  871. }
  872. static JSFinalObject* create(ExecState*, Structure*);
  873. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  874. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, unsigned inlineCapacity)
  875. {
  876. return Structure::create(vm, globalObject, prototype, TypeInfo(FinalObjectType, StructureFlags), &s_info, NonArray, inlineCapacity);
  877. }
  878. #endif
  879. JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
  880. #if ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT
  881. private:
  882. static JS_EXPORTDATA const ClassInfo s_info;
  883. #else
  884. public:
  885. static JS_EXPORTDATA const ClassInfo s_info;
  886. #endif
  887. protected:
  888. void visitChildrenCommon(SlotVisitor&);
  889. void finishCreation(VM& vm)
  890. {
  891. Base::finishCreation(vm);
  892. ASSERT(structure()->totalStorageCapacity() == structure()->inlineCapacity());
  893. ASSERT(classInfo());
  894. }
  895. private:
  896. friend class LLIntOffsetsExtractor;
  897. explicit JSFinalObject(VM& vm, Structure* structure)
  898. : JSObject(vm, structure)
  899. {
  900. }
  901. static const unsigned StructureFlags = JSObject::StructureFlags;
  902. };
  903. inline JSFinalObject* JSFinalObject::create(ExecState* exec, Structure* structure)
  904. {
  905. JSFinalObject* finalObject = new (
  906. NotNull,
  907. allocateCell<JSFinalObject>(
  908. *exec->heap(),
  909. allocationSize(structure->inlineCapacity())
  910. )
  911. ) JSFinalObject(exec->vm(), structure);
  912. finalObject->finishCreation(exec->vm());
  913. return finalObject;
  914. }
  915. inline bool isJSFinalObject(JSCell* cell)
  916. {
  917. DEFINE_STATIC_sJSFinalObjectClassInfo;
  918. return cell->classInfo() == sJSFinalObjectClassInfo;
  919. }
  920. inline bool isJSFinalObject(JSValue value)
  921. {
  922. return value.isCell() && isJSFinalObject(value.asCell());
  923. }
  924. inline size_t JSObject::offsetOfInlineStorage()
  925. {
  926. return sizeof(JSObject);
  927. }
  928. inline bool JSObject::isGlobalObject() const
  929. {
  930. return structure()->typeInfo().type() == GlobalObjectType;
  931. }
  932. inline bool JSObject::isVariableObject() const
  933. {
  934. return structure()->typeInfo().type() >= VariableObjectType;
  935. }
  936. inline bool JSObject::isStaticScopeObject() const
  937. {
  938. JSType type = structure()->typeInfo().type();
  939. return type == NameScopeObjectType || type == ActivationObjectType;
  940. }
  941. inline bool JSObject::isNameScopeObject() const
  942. {
  943. return structure()->typeInfo().type() == NameScopeObjectType;
  944. }
  945. inline bool JSObject::isActivationObject() const
  946. {
  947. return structure()->typeInfo().type() == ActivationObjectType;
  948. }
  949. inline bool JSObject::isErrorInstance() const
  950. {
  951. return structure()->typeInfo().type() == ErrorInstanceType;
  952. }
  953. inline void JSObject::setButterfly(VM& vm, Butterfly* butterfly, Structure* structure)
  954. {
  955. ASSERT(structure);
  956. ASSERT(!butterfly == (!structure->outOfLineCapacity() && !hasIndexingHeader(structure->indexingType())));
  957. setStructure(vm, structure);
  958. m_butterfly = butterfly;
  959. }
  960. inline void JSObject::setButterflyWithoutChangingStructure(Butterfly* butterfly)
  961. {
  962. m_butterfly = butterfly;
  963. }
  964. inline CallType getCallData(JSValue value, CallData& callData)
  965. {
  966. CallType result = value.isCell() ? value.asCell()->methodTable()->getCallData(value.asCell(), callData) : CallTypeNone;
  967. ASSERT(result == CallTypeNone || value.isValidCallee());
  968. return result;
  969. }
  970. inline ConstructType getConstructData(JSValue value, ConstructData& constructData)
  971. {
  972. ConstructType result = value.isCell() ? value.asCell()->methodTable()->getConstructData(value.asCell(), constructData) : ConstructTypeNone;
  973. ASSERT(result == ConstructTypeNone || value.isValidCallee());
  974. return result;
  975. }
  976. inline JSObject* asObject(JSCell* cell)
  977. {
  978. ASSERT(cell->isObject());
  979. return jsCast<JSObject*>(cell);
  980. }
  981. inline JSObject* asObject(JSValue value)
  982. {
  983. return asObject(value.asCell());
  984. }
  985. inline JSObject::JSObject(VM& vm, Structure* structure, Butterfly* butterfly)
  986. : JSCell(vm, structure)
  987. , m_butterfly(butterfly)
  988. {
  989. }
  990. inline JSValue JSObject::prototype() const
  991. {
  992. return structure()->storedPrototype();
  993. }
  994. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  995. ALWAYS_INLINE bool JSObject::inlineGetOwnPropertySlot(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  996. {
  997. PropertyOffset offset = structure()->get(exec->vm(), propertyName);
  998. if (LIKELY(isValidOffset(offset))) {
  999. JSValue value = getDirect(offset);
  1000. if (structure()->hasGetterSetterProperties() && value.isGetterSetter())
  1001. fillGetterPropertySlot(slot, offset);
  1002. else
  1003. slot.setValue(this, value, offset);
  1004. return true;
  1005. }
  1006. return getOwnPropertySlotSlow(exec, propertyName, slot);
  1007. }
  1008. // It may seem crazy to inline a function this large, especially a virtual function,
  1009. // but it makes a big difference to property lookup that derived classes can inline their
  1010. // base class call to this.
  1011. ALWAYS_INLINE bool JSObject::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  1012. {
  1013. return jsCast<JSObject*>(cell)->inlineGetOwnPropertySlot(exec, propertyName, slot);
  1014. }
  1015. // It may seem crazy to inline a function this large but it makes a big difference
  1016. // since this is function very hot in variable lookup
  1017. ALWAYS_INLINE bool JSObject::getPropertySlot(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  1018. {
  1019. JSObject* object = this;
  1020. while (true) {
  1021. if (object->fastGetOwnPropertySlot(exec, propertyName, slot))
  1022. return true;
  1023. JSValue prototype = object->prototype();
  1024. if (!prototype.isObject())
  1025. return false;
  1026. object = asObject(prototype);
  1027. }
  1028. }
  1029. ALWAYS_INLINE bool JSObject::getPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
  1030. {
  1031. JSObject* object = this;
  1032. while (true) {
  1033. if (object->methodTable()->getOwnPropertySlotByIndex(object, exec, propertyName, slot))
  1034. return true;
  1035. JSValue prototype = object->prototype();
  1036. if (!prototype.isObject())
  1037. return false;
  1038. object = asObject(prototype);
  1039. }
  1040. }
  1041. inline JSValue JSObject::get(ExecState* exec, PropertyName propertyName) const
  1042. {
  1043. PropertySlot slot(this);
  1044. if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
  1045. return slot.getValue(exec, propertyName);
  1046. return jsUndefined();
  1047. }
  1048. inline JSValue JSObject::get(ExecState* exec, unsigned propertyName) const
  1049. {
  1050. PropertySlot slot(this);
  1051. if (const_cast<JSObject*>(this)->getPropertySlot(exec, propertyName, slot))
  1052. return slot.getValue(exec, propertyName);
  1053. return jsUndefined();
  1054. }
  1055. template<JSObject::PutMode mode>
  1056. inline bool JSObject::putDirectInternal(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes, PutPropertySlot& slot, JSCell* specificFunction)
  1057. {
  1058. ASSERT(value);
  1059. ASSERT(value.isGetterSetter() == !!(attributes & Accessor));
  1060. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
  1061. ASSERT(propertyName.asIndex() == PropertyName::NotAnIndex);
  1062. if (structure()->isDictionary()) {
  1063. unsigned currentAttributes;
  1064. JSCell* currentSpecificFunction;
  1065. PropertyOffset offset = structure()->get(vm, propertyName, currentAttributes, currentSpecificFunction);
  1066. if (offset != invalidOffset) {
  1067. // If there is currently a specific function, and there now either isn't,
  1068. // or the new value is different, then despecify.
  1069. if (currentSpecificFunction && (specificFunction != currentSpecificFunction))
  1070. structure()->despecifyDictionaryFunction(vm, propertyName);
  1071. if ((mode == PutModePut) && currentAttributes & ReadOnly)
  1072. return false;
  1073. putDirect(vm, offset, value);
  1074. // At this point, the objects structure only has a specific value set if previously there
  1075. // had been one set, and if the new value being specified is the same (otherwise we would
  1076. // have despecified, above). So, if currentSpecificFunction is not set, or if the new
  1077. // value is different (or there is no new value), then the slot now has no value - and
  1078. // as such it is cachable.
  1079. // If there was previously a value, and the new value is the same, then we cannot cache.
  1080. if (!currentSpecificFunction || (specificFunction != currentSpecificFunction))
  1081. slot.setExistingProperty(this, offset);
  1082. return true;
  1083. }
  1084. if ((mode == PutModePut) && !isExtensible())
  1085. return false;
  1086. Butterfly* newButterfly = m_butterfly;
  1087. if (structure()->putWillGrowOutOfLineStorage())
  1088. newButterfly = growOutOfLineStorage(vm, structure()->outOfLineCapacity(), structure()->suggestedNewOutOfLineStorageCapacity());
  1089. offset = structure()->addPropertyWithoutTransition(vm, propertyName, attributes, specificFunction);
  1090. setButterfly(vm, newButterfly, structure());
  1091. validateOffset(offset);
  1092. ASSERT(structure()->isValidOffset(offset));
  1093. putDirect(vm, offset, value);
  1094. // See comment on setNewProperty call below.
  1095. if (!specificFunction)
  1096. slot.setNewProperty(this, offset);
  1097. if (attributes & ReadOnly)
  1098. structure()->setContainsReadOnlyProperties();
  1099. return true;
  1100. }
  1101. PropertyOffset offset;
  1102. size_t currentCapacity = structure()->outOfLineCapacity();
  1103. if (Structure* structure = Structure::addPropertyTransitionToExistingStructure(this->structure(), propertyName, attributes, specificFunction, offset)) {
  1104. Butterfly* newButterfly = m_butterfly;
  1105. if (currentCapacity != structure->outOfLineCapacity())
  1106. newButterfly = growOutOfLineStorage(vm, currentCapacity, structure->outOfLineCapacity());
  1107. validateOffset(offset);
  1108. ASSERT(structure->isValidOffset(offset));
  1109. setButterfly(vm, newButterfly, structure);
  1110. putDirect(vm, offset, value);
  1111. // This is a new property; transitions with specific values are not currently cachable,
  1112. // so leave the slot in an uncachable state.
  1113. if (!specificFunction)
  1114. slot.setNewProperty(this, offset);
  1115. return true;
  1116. }
  1117. unsigned currentAttributes;
  1118. JSCell* currentSpecificFunction;
  1119. offset = structure()->get(vm, propertyName, currentAttributes, currentSpecificFunction);
  1120. if (offset != invalidOffset) {
  1121. if ((mode == PutModePut) && currentAttributes & ReadOnly)
  1122. return false;
  1123. // There are three possibilities here:
  1124. // (1) There is an existing specific value set, and we're overwriting with *the same value*.
  1125. // * Do nothing - no need to despecify, but that means we can't cache (a cached
  1126. // put could write a different value). Leave the slot in an uncachable state.
  1127. // (2) There is a specific value currently set, but we're writing a different value.
  1128. // * First, we have to despecify. Having done so, this is now a regular slot
  1129. // with no specific value, so go ahead & cache like normal.
  1130. // (3) Normal case, there is no specific value set.
  1131. // * Go ahead & cache like normal.
  1132. if (currentSpecificFunction) {
  1133. // case (1) Do the put, then return leaving the slot uncachable.
  1134. if (specificFunction == currentSpecificFunction) {
  1135. putDirect(vm, offset, value);
  1136. return true;
  1137. }
  1138. // case (2) Despecify, fall through to (3).
  1139. setStructure(vm, Structure::despecifyFunctionTransition(vm, structure(), propertyName));
  1140. }
  1141. // case (3) set the slot, do the put, return.
  1142. slot.setExistingProperty(this, offset);
  1143. putDirect(vm, offset, value);
  1144. return true;
  1145. }
  1146. if ((mode == PutModePut) && !isExtensible())
  1147. return false;
  1148. Structure* structure = Structure::addPropertyTransition(vm, this->structure(), propertyName, attributes, specificFunction, offset);
  1149. validateOffset(offset);
  1150. ASSERT(structure->isValidOffset(offset));
  1151. setStructureAndReallocateStorageIfNecessary(vm, structure);
  1152. putDirect(vm, offset, value);
  1153. // This is a new property; transitions with specific values are not currently cachable,
  1154. // so leave the slot in an uncachable state.
  1155. if (!specificFunction)
  1156. slot.setNewProperty(this, offset);
  1157. if (attributes & ReadOnly)
  1158. structure->setContainsReadOnlyProperties();
  1159. return true;
  1160. }
  1161. inline void JSObject::setStructureAndReallocateStorageIfNecessary(VM& vm, unsigned oldCapacity, Structure* newStructure)
  1162. {
  1163. ASSERT(oldCapacity <= newStructure->outOfLineCapacity());
  1164. if (oldCapacity == newStructure->outOfLineCapacity()) {
  1165. setStructure(vm, newStructure);
  1166. return;
  1167. }
  1168. Butterfly* newButterfly = growOutOfLineStorage(
  1169. vm, oldCapacity, newStructure->outOfLineCapacity());
  1170. setButterfly(vm, newButterfly, newStructure);
  1171. }
  1172. inline void JSObject::setStructureAndReallocateStorageIfNecessary(VM& vm, Structure* newStructure)
  1173. {
  1174. setStructureAndReallocateStorageIfNecessary(
  1175. vm, structure()->outOfLineCapacity(), newStructure);
  1176. }
  1177. inline bool JSObject::putOwnDataProperty(VM& vm, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  1178. {
  1179. ASSERT(value);
  1180. ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
  1181. ASSERT(!structure()->hasGetterSetterProperties());
  1182. return putDirectInternal<PutModePut>(vm, propertyName, value, 0, slot, getCallableObject(value));
  1183. }
  1184. inline void JSObject::putDirect(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes)
  1185. {
  1186. ASSERT(!value.isGetterSetter() && !(attributes & Accessor));
  1187. PutPropertySlot slot;
  1188. putDirectInternal<PutModeDefineOwnProperty>(vm, propertyName, value, attributes, slot, getCallableObject(value));
  1189. }
  1190. inline void JSObject::putDirect(VM& vm, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
  1191. {
  1192. ASSERT(!value.isGetterSetter());
  1193. putDirectInternal<PutModeDefineOwnProperty>(vm, propertyName, value, 0, slot, getCallableObject(value));
  1194. }
  1195. inline void JSObject::putDirectWithoutTransition(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes)
  1196. {
  1197. ASSERT(!value.isGetterSetter() && !(attributes & Accessor));
  1198. Butterfly* newButterfly = m_butterfly;
  1199. if (structure()->putWillGrowOutOfLineStorage())
  1200. newButterfly = growOutOfLineStorage(vm, structure()->outOfLineCapacity(), structure()->suggestedNewOutOfLineStorageCapacity());
  1201. PropertyOffset offset = structure()->addPropertyWithoutTransition(vm, propertyName, attributes, getCallableObject(value));
  1202. setButterfly(vm, newButterfly, structure());
  1203. putDirect(vm, offset, value);
  1204. }
  1205. inline JSValue JSObject::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
  1206. {
  1207. return methodTable()->defaultValue(this, exec, preferredType);
  1208. }
  1209. ALWAYS_INLINE JSObject* Register::function() const
  1210. {
  1211. if (!jsValue())
  1212. return 0;
  1213. return asObject(jsValue());
  1214. }
  1215. ALWAYS_INLINE Register Register::withCallee(JSObject* callee)
  1216. {
  1217. Register r;
  1218. r = JSValue(callee);
  1219. return r;
  1220. }
  1221. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  1222. inline size_t offsetInButterfly(PropertyOffset offset)
  1223. {
  1224. return offsetInOutOfLineStorage(offset) + Butterfly::indexOfPropertyStorage();
  1225. }
  1226. // Helpers for patching code where you want to emit a load or store and
  1227. // the base is:
  1228. // For inline offsets: a pointer to the out-of-line storage pointer.
  1229. // For out-of-line offsets: the base of the out-of-line storage.
  1230. inline size_t offsetRelativeToPatchedStorage(PropertyOffset offset)
  1231. {
  1232. if (isOutOfLineOffset(offset))
  1233. return sizeof(EncodedJSValue) * offsetInButterfly(offset);
  1234. return JSObject::offsetOfInlineStorage() - JSObject::butterflyOffset() + sizeof(EncodedJSValue) * offsetInInlineStorage(offset);
  1235. }
  1236. // Returns the maximum offset (away from zero) a load instruction will encode.
  1237. inline size_t maxOffsetRelativeToPatchedStorage(PropertyOffset offset)
  1238. {
  1239. ptrdiff_t addressOffset = static_cast<ptrdiff_t>(offsetRelativeToPatchedStorage(offset));
  1240. #if USE(JSVALUE32_64)
  1241. if (addressOffset >= 0)
  1242. return static_cast<size_t>(addressOffset) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.tag);
  1243. #endif
  1244. return static_cast<size_t>(addressOffset);
  1245. }
  1246. inline int indexRelativeToBase(PropertyOffset offset)
  1247. {
  1248. if (isOutOfLineOffset(offset))
  1249. return offsetInOutOfLineStorage(offset) + Butterfly::indexOfPropertyStorage();
  1250. ASSERT(!(JSObject::offsetOfInlineStorage() % sizeof(EncodedJSValue)));
  1251. return JSObject::offsetOfInlineStorage() / sizeof(EncodedJSValue) + offsetInInlineStorage(offset);
  1252. }
  1253. inline int offsetRelativeToBase(PropertyOffset offset)
  1254. {
  1255. if (isOutOfLineOffset(offset))
  1256. return offsetInOutOfLineStorage(offset) * sizeof(EncodedJSValue) + Butterfly::offsetOfPropertyStorage();
  1257. return JSObject::offsetOfInlineStorage() + offsetInInlineStorage(offset) * sizeof(EncodedJSValue);
  1258. }
  1259. COMPILE_ASSERT(!(sizeof(JSObject) % sizeof(WriteBarrierBase<Unknown>)), JSObject_inline_storage_has_correct_alignment);
  1260. ALWAYS_INLINE Identifier makeIdentifier(ExecState* exec, const char* name)
  1261. {
  1262. return Identifier(exec, name);
  1263. }
  1264. ALWAYS_INLINE Identifier makeIdentifier(ExecState*, const Identifier& name)
  1265. {
  1266. return name;
  1267. }
  1268. // Helper for defining native functions, if you're not using a static hash table.
  1269. // Use this macro from within finishCreation() methods in prototypes. This assumes
  1270. // you've defined variables called exec, globalObject, and vm, and they
  1271. // have the expected meanings.
  1272. #define JSC_NATIVE_INTRINSIC_FUNCTION(jsName, cppName, attributes, length, intrinsic) \
  1273. putDirectNativeFunction(\
  1274. exec, globalObject, makeIdentifier(exec, (jsName)), (length), cppName, \
  1275. (intrinsic), (attributes))
  1276. // As above, but this assumes that the function you're defining doesn't have an
  1277. // intrinsic.
  1278. #define JSC_NATIVE_FUNCTION(jsName, cppName, attributes, length) \
  1279. JSC_NATIVE_INTRINSIC_FUNCTION(jsName, cppName, (attributes), (length), NoIntrinsic)
  1280. } // namespace JSC
  1281. #endif // JSObject_h