SparseArrayValueMap.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef SparseArrayValueMap_h
  26. #define SparseArrayValueMap_h
  27. #include "JSCell.h"
  28. #include "JSTypeInfo.h"
  29. #include "PropertyDescriptor.h"
  30. #include "PutDirectIndexMode.h"
  31. #include "WriteBarrier.h"
  32. #include <wtf/HashMap.h>
  33. #include <wtf/Platform.h>
  34. namespace JSC {
  35. class SparseArrayValueMap;
  36. struct SparseArrayEntry : public WriteBarrier<Unknown> {
  37. typedef WriteBarrier<Unknown> Base;
  38. SparseArrayEntry() : attributes(0) { }
  39. JSValue get(ExecState*, JSObject*) const;
  40. void get(PropertySlot&) const;
  41. void get(PropertyDescriptor&) const;
  42. void put(ExecState*, JSValue thisValue, SparseArrayValueMap*, JSValue, bool shouldThrow);
  43. JSValue getNonSparseMode() const;
  44. unsigned attributes;
  45. };
  46. class SparseArrayValueMap : public JSCell {
  47. public:
  48. typedef JSCell Base;
  49. private:
  50. typedef HashMap<uint64_t, SparseArrayEntry, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t> > Map;
  51. enum Flags {
  52. Normal = 0,
  53. SparseMode = 1,
  54. LengthIsReadOnly = 2,
  55. };
  56. SparseArrayValueMap(VM&);
  57. ~SparseArrayValueMap();
  58. void finishCreation(VM&);
  59. static const unsigned StructureFlags = OverridesVisitChildren | JSCell::StructureFlags;
  60. public:
  61. static JS_EXPORTDATA const ClassInfo s_info;
  62. typedef Map::iterator iterator;
  63. typedef Map::const_iterator const_iterator;
  64. typedef Map::AddResult AddResult;
  65. static SparseArrayValueMap* create(VM&);
  66. static const bool needsDestruction = true;
  67. static const bool hasImmortalStructure = true;
  68. static void destroy(JSCell*);
  69. static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
  70. static void visitChildren(JSCell*, SlotVisitor&);
  71. bool sparseMode()
  72. {
  73. return m_flags & SparseMode;
  74. }
  75. void setSparseMode()
  76. {
  77. m_flags = static_cast<Flags>(m_flags | SparseMode);
  78. }
  79. bool lengthIsReadOnly()
  80. {
  81. return m_flags & LengthIsReadOnly;
  82. }
  83. void setLengthIsReadOnly()
  84. {
  85. m_flags = static_cast<Flags>(m_flags | LengthIsReadOnly);
  86. }
  87. // These methods may mutate the contents of the map
  88. void putEntry(ExecState*, JSObject*, unsigned, JSValue, bool shouldThrow);
  89. bool putDirect(ExecState*, JSObject*, unsigned, JSValue, unsigned attributes, PutDirectIndexMode);
  90. AddResult add(JSObject*, unsigned);
  91. iterator find(unsigned i) { return m_map.find(i); }
  92. // This should ASSERT the remove is valid (check the result of the find).
  93. void remove(iterator it) { m_map.remove(it); }
  94. void remove(unsigned i) { m_map.remove(i); }
  95. // These methods do not mutate the contents of the map.
  96. iterator notFound() { return m_map.end(); }
  97. bool isEmpty() const { return m_map.isEmpty(); }
  98. bool contains(unsigned i) const { return m_map.contains(i); }
  99. size_t size() const { return m_map.size(); }
  100. // Only allow const begin/end iteration.
  101. const_iterator begin() const { return m_map.begin(); }
  102. const_iterator end() const { return m_map.end(); }
  103. private:
  104. Map m_map;
  105. Flags m_flags;
  106. size_t m_reportedCapacity;
  107. };
  108. } // namespace JSC
  109. #endif // SparseArrayValueMap_h