[V8] Introduce a QML compilation mode
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / elements.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_ELEMENTS_H_
29 #define V8_ELEMENTS_H_
30
31 #include "objects.h"
32 #include "heap.h"
33 #include "isolate.h"
34
35 namespace v8 {
36 namespace internal {
37
38 // Abstract base class for handles that can operate on objects with differing
39 // ElementsKinds.
40 class ElementsAccessor {
41  public:
42   explicit ElementsAccessor(const char* name) : name_(name) { }
43   virtual ~ElementsAccessor() { }
44
45   virtual ElementsKind kind() const = 0;
46   const char* name() const { return name_; }
47
48   // Returns true if a holder contains an element with the specified key
49   // without iterating up the prototype chain.  The caller can optionally pass
50   // in the backing store to use for the check, which must be compatible with
51   // the ElementsKind of the ElementsAccessor. If backing_store is NULL, the
52   // holder->elements() is used as the backing store.
53   virtual bool HasElement(Object* receiver,
54                           JSObject* holder,
55                           uint32_t key,
56                           FixedArrayBase* backing_store = NULL) = 0;
57
58   // Returns the element with the specified key or undefined if there is no such
59   // element. This method doesn't iterate up the prototype chain.  The caller
60   // can optionally pass in the backing store to use for the check, which must
61   // be compatible with the ElementsKind of the ElementsAccessor. If
62   // backing_store is NULL, the holder->elements() is used as the backing store.
63   MUST_USE_RESULT virtual MaybeObject* Get(
64       Object* receiver,
65       JSObject* holder,
66       uint32_t key,
67       FixedArrayBase* backing_store = NULL) = 0;
68
69   // Modifies the length data property as specified for JSArrays and resizes the
70   // underlying backing store accordingly. The method honors the semantics of
71   // changing array sizes as defined in EcmaScript 5.1 15.4.5.2, i.e. array that
72   // have non-deletable elements can only be shrunk to the size of highest
73   // element that is non-deletable.
74   MUST_USE_RESULT virtual MaybeObject* SetLength(JSArray* holder,
75                                                  Object* new_length) = 0;
76
77   // Modifies both the length and capacity of a JSArray, resizing the underlying
78   // backing store as necessary. This method does NOT honor the semantics of
79   // EcmaScript 5.1 15.4.5.2, arrays can be shrunk beyond non-deletable
80   // elements. This method should only be called for array expansion OR by
81   // runtime JavaScript code that use InternalArrays and don't care about
82   // EcmaScript 5.1 semantics.
83   MUST_USE_RESULT virtual MaybeObject* SetCapacityAndLength(JSArray* array,
84                                                             int capacity,
85                                                             int length) = 0;
86
87   // Deletes an element in an object, returning a new elements backing store.
88   MUST_USE_RESULT virtual MaybeObject* Delete(JSObject* holder,
89                                               uint32_t key,
90                                               JSReceiver::DeleteMode mode) = 0;
91
92   // If kCopyToEnd is specified as the copy_size to CopyElements, it copies all
93   // of elements from source after source_start to the destination array.
94   static const int kCopyToEnd = -1;
95   // If kCopyToEndAndInitializeToHole is specified as the copy_size to
96   // CopyElements, it copies all of elements from source after source_start to
97   // destination array, padding any remaining uninitialized elements in the
98   // destination array with the hole.
99   static const int kCopyToEndAndInitializeToHole = -2;
100
101   // Copy elements from one backing store to another. Typically, callers specify
102   // the source JSObject or JSArray in source_holder. If the holder's backing
103   // store is available, it can be passed in source and source_holder is
104   // ignored.
105   MUST_USE_RESULT virtual MaybeObject* CopyElements(
106       JSObject* source_holder,
107       uint32_t source_start,
108       FixedArrayBase* destination,
109       ElementsKind destination_kind,
110       uint32_t destination_start,
111       int copy_size,
112       FixedArrayBase* source = NULL) = 0;
113
114   MUST_USE_RESULT MaybeObject* CopyElements(JSObject* from_holder,
115                                             FixedArrayBase* to,
116                                             ElementsKind to_kind,
117                                             FixedArrayBase* from = NULL) {
118     return CopyElements(from_holder, 0, to, to_kind, 0,
119                         kCopyToEndAndInitializeToHole, from);
120   }
121
122   MUST_USE_RESULT virtual MaybeObject* AddElementsToFixedArray(
123       Object* receiver,
124       JSObject* holder,
125       FixedArray* to,
126       FixedArrayBase* from = NULL) = 0;
127
128   // Returns a shared ElementsAccessor for the specified ElementsKind.
129   static ElementsAccessor* ForKind(ElementsKind elements_kind) {
130     ASSERT(elements_kind < kElementsKindCount);
131     return elements_accessors_[elements_kind];
132   }
133
134   static ElementsAccessor* ForArray(FixedArrayBase* array);
135
136   static void InitializeOncePerProcess();
137   static void TearDown();
138
139  protected:
140   friend class NonStrictArgumentsElementsAccessor;
141
142   virtual uint32_t GetCapacity(FixedArrayBase* backing_store) = 0;
143
144   // Element handlers distinguish between indexes and keys when they manipulate
145   // elements.  Indexes refer to elements in terms of their location in the
146   // underlying storage's backing store representation, and are between 0 and
147   // GetCapacity.  Keys refer to elements in terms of the value that would be
148   // specified in JavaScript to access the element. In most implementations,
149   // keys are equivalent to indexes, and GetKeyForIndex returns the same value
150   // it is passed. In the NumberDictionary ElementsAccessor, GetKeyForIndex maps
151   // the index to a key using the KeyAt method on the NumberDictionary.
152   virtual uint32_t GetKeyForIndex(FixedArrayBase* backing_store,
153                                   uint32_t index) = 0;
154
155  private:
156   static ElementsAccessor** elements_accessors_;
157   const char* name_;
158
159   DISALLOW_COPY_AND_ASSIGN(ElementsAccessor);
160 };
161
162
163 void CopyObjectToObjectElements(FixedArray* from_obj,
164                                 ElementsKind from_kind,
165                                 uint32_t from_start,
166                                 FixedArray* to_obj,
167                                 ElementsKind to_kind,
168                                 uint32_t to_start,
169                                 int copy_size);
170
171
172 } }  // namespace v8::internal
173
174 #endif  // V8_ELEMENTS_H_