The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / handles.h @ 40c0f755

History | View | Annotate | Download (12.4 KB)

1
// Copyright 2006-2008 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_HANDLES_H_
29
#define V8_HANDLES_H_
30

    
31
#include "apiutils.h"
32

    
33
namespace v8 { namespace internal {
34

    
35
// ----------------------------------------------------------------------------
36
// A Handle provides a reference to an object that survives relocation by
37
// the garbage collector.
38
// Handles are only valid within a HandleScope.
39
// When a handle is created for an object a cell is allocated in the heap.
40

    
41
template<class T>
42
class Handle {
43
 public:
44
  INLINE(Handle(T** location))  { location_ = location; }
45
  INLINE(explicit Handle(T* obj));
46

    
47
  INLINE(Handle()) : location_(NULL) {}
48

    
49
  // Constructor for handling automatic up casting.
50
  // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
51
  template <class S> Handle(Handle<S> handle) {
52
#ifdef DEBUG
53
    T* a = NULL;
54
    S* b = NULL;
55
    a = b;  // Fake assignment to enforce type checks.
56
    USE(a);
57
#endif
58
    location_ = reinterpret_cast<T**>(handle.location());
59
  }
60

    
61
  INLINE(T* operator ->() const)  { return operator*(); }
62

    
63
  // Check if this handle refers to the exact same object as the other handle.
64
  bool is_identical_to(const Handle<T> other) const {
65
    return operator*() == *other;
66
  }
67

    
68
  // Provides the C++ dereference operator.
69
  INLINE(T* operator*() const);
70

    
71
  // Returns the address to where the raw pointer is stored.
72
  T** location() const {
73
    ASSERT(location_ == NULL ||
74
           reinterpret_cast<Address>(*location_) != kZapValue);
75
    return location_;
76
  }
77

    
78
  template <class S> static Handle<T> cast(Handle<S> that) {
79
    T::cast(*that);
80
    return Handle<T>(reinterpret_cast<T**>(that.location()));
81
  }
82

    
83
  static Handle<T> null() { return Handle<T>(); }
84
  bool is_null() {return location_ == NULL; }
85

    
86
  // Closes the given scope, but lets this handle escape. See
87
  // implementation in api.h.
88
  inline Handle<T> EscapeFrom(v8::HandleScope* scope);
89

    
90
 private:
91
  T** location_;
92
};
93

    
94

    
95
// A stack-allocated class that governs a number of local handles.
96
// After a handle scope has been created, all local handles will be
97
// allocated within that handle scope until either the handle scope is
98
// deleted or another handle scope is created.  If there is already a
99
// handle scope and a new one is created, all allocations will take
100
// place in the new handle scope until it is deleted.  After that,
101
// new handles will again be allocated in the original handle scope.
102
//
103
// After the handle scope of a local handle has been deleted the
104
// garbage collector will no longer track the object stored in the
105
// handle and may deallocate it.  The behavior of accessing a handle
106
// for which the handle scope has been deleted is undefined.
107
class HandleScope {
108
 public:
109
  HandleScope() : previous_(current_) {
110
    current_.extensions = 0;
111
  }
112

    
113
  ~HandleScope() {
114
    Leave(&previous_);
115
  }
116

    
117
  // Counts the number of allocated handles.
118
  static int NumberOfHandles();
119

    
120
  // Creates a new handle with the given value.
121
  static inline void** CreateHandle(void* value) {
122
    void** result = current_.next;
123
    if (result == current_.limit) result = Extend();
124
    // Update the current next field, set the value in the created
125
    // handle, and return the result.
126
    ASSERT(result < current_.limit);
127
    current_.next = result + 1;
128
    *result = value;
129
    return result;
130
  }
131

    
132
 private:
133
  // Prevent heap allocation or illegal handle scopes.
134
  HandleScope(const HandleScope&);
135
  void operator=(const HandleScope&);
136
  void* operator new(size_t size);
137
  void operator delete(void* size_t);
138

    
139
  static v8::ImplementationUtilities::HandleScopeData current_;
140
  const v8::ImplementationUtilities::HandleScopeData previous_;
141

    
142
  // Pushes a fresh handle scope to be used when allocating new handles.
143
  static void Enter(
144
      v8::ImplementationUtilities::HandleScopeData* previous) {
145
    *previous = current_;
146
    current_.extensions = 0;
147
  }
148

    
149
  // Re-establishes the previous scope state. Should be called only
150
  // once, and only for the current scope.
151
  static void Leave(
152
      const v8::ImplementationUtilities::HandleScopeData* previous) {
153
    if (current_.extensions > 0) {
154
      DeleteExtensions();
155
    }
156
    current_ = *previous;
157
#ifdef DEBUG
158
    ZapRange(current_.next, current_.limit);
159
#endif
160
  }
161

    
162
  // Extend the handle scope making room for more handles.
163
  static void** Extend();
164

    
165
  // Deallocates any extensions used by the current scope.
166
  static void DeleteExtensions();
167

    
168
  // Zaps the handles in the half-open interval [start, end).
169
  static void ZapRange(void** start, void** end);
170

    
171
  friend class v8::HandleScope;
172
  friend class v8::ImplementationUtilities;
173
};
174

    
175

    
176
// ----------------------------------------------------------------------------
177
// Handle operations.
178
// They might invoke garbage collection. The result is an handle to
179
// an object of expected type, or the handle is an error if running out
180
// of space or encountering an internal error.
181

    
182
void NormalizeProperties(Handle<JSObject> object,
183
                         PropertyNormalizationMode mode);
184
void NormalizeElements(Handle<JSObject> object);
185
void TransformToFastProperties(Handle<JSObject> object,
186
                               int unused_property_fields);
187
void FlattenString(Handle<String> str);
188

    
189
Handle<Object> SetProperty(Handle<JSObject> object,
190
                           Handle<String> key,
191
                           Handle<Object> value,
192
                           PropertyAttributes attributes);
193

    
194
Handle<Object> SetProperty(Handle<Object> object,
195
                           Handle<Object> key,
196
                           Handle<Object> value,
197
                           PropertyAttributes attributes);
198

    
199
Handle<Object> IgnoreAttributesAndSetLocalProperty(Handle<JSObject> object,
200
                                                   Handle<String> key,
201
                                                   Handle<Object> value,
202
    PropertyAttributes attributes);
203

    
204
Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
205
                                          Handle<String> key,
206
                                          Handle<Object> value,
207
                                          PropertyAttributes attributes);
208

    
209
Handle<Object> SetElement(Handle<JSObject> object,
210
                          uint32_t index,
211
                          Handle<Object> value);
212

    
213
Handle<Object> GetProperty(Handle<JSObject> obj,
214
                           const char* name);
215

    
216
Handle<Object> GetProperty(Handle<Object> obj,
217
                           Handle<Object> key);
218

    
219
Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
220
                                          Handle<JSObject> holder,
221
                                          Handle<String> name,
222
                                          PropertyAttributes* attributes);
223

    
224
Handle<Object> GetPrototype(Handle<Object> obj);
225

    
226
Handle<Object> GetHiddenProperties(Handle<JSObject> obj, bool create_if_needed);
227

    
228
Handle<Object> DeleteElement(Handle<JSObject> obj, uint32_t index);
229
Handle<Object> DeleteProperty(Handle<JSObject> obj, Handle<String> prop);
230

    
231
Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
232

    
233
Handle<JSObject> Copy(Handle<JSObject> obj);
234

    
235
Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
236
                                      Handle<JSArray> array);
237

    
238
// Get the JS object corresponding to the given script; create it
239
// if none exists.
240
Handle<JSValue> GetScriptWrapper(Handle<Script> script);
241

    
242
// Script line number computations.
243
void InitScriptLineEnds(Handle<Script> script);
244
int GetScriptLineNumber(Handle<Script> script, int code_position);
245

    
246
// Computes the enumerable keys from interceptors. Used for debug mirrors and
247
// by GetKeysInFixedArrayFor below.
248
v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
249
                                                 Handle<JSObject> object);
250
v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
251
                                                   Handle<JSObject> object);
252
// Computes the enumerable keys for a JSObject. Used for implementing
253
// "for (n in object) { }".
254
Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object);
255
Handle<JSArray> GetKeysFor(Handle<JSObject> object);
256
Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object);
257

    
258
// Computes the union of keys and return the result.
259
// Used for implementing "for (n in object) { }"
260
Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
261
                               Handle<FixedArray> second);
262

    
263
Handle<String> SubString(Handle<String> str, int start, int end);
264

    
265

    
266
// Sets the expected number of properties for the function's instances.
267
void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
268

    
269
// Sets the prototype property for a function instance.
270
void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
271

    
272
// Sets the expected number of properties based on estimate from compiler.
273
void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
274
                                          int estimate);
275
void SetExpectedNofPropertiesFromEstimate(Handle<JSFunction> func,
276
                                          int estimate);
277

    
278

    
279
Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
280
    Handle<JSFunction> constructor,
281
    Handle<JSGlobalProxy> global);
282

    
283
Handle<Object> SetPrototype(Handle<JSFunction> function,
284
                            Handle<Object> prototype);
285

    
286

    
287
// Do lazy compilation of the given function. Returns true on success
288
// and false if the compilation resulted in a stack overflow.
289
enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION };
290

    
291
bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
292
                       ClearExceptionFlag flag,
293
                       int loop_nesting);
294

    
295
bool CompileLazy(Handle<JSFunction> function, ClearExceptionFlag flag);
296
bool CompileLazyInLoop(Handle<JSFunction> function, ClearExceptionFlag flag);
297

    
298
// These deal with lazily loaded properties.
299
void SetupLazy(Handle<JSFunction> fun,
300
               int index,
301
               Handle<Context> compile_context,
302
               Handle<Context> function_context);
303
void LoadLazy(Handle<JSFunction> fun, bool* pending_exception);
304

    
305
class NoHandleAllocation BASE_EMBEDDED {
306
 public:
307
#ifndef DEBUG
308
  NoHandleAllocation() {}
309
  ~NoHandleAllocation() {}
310
#else
311
  inline NoHandleAllocation();
312
  inline ~NoHandleAllocation();
313
 private:
314
  int extensions_;
315
#endif
316
};
317

    
318

    
319
// ----------------------------------------------------------------------------
320

    
321

    
322
// Stack allocated wrapper call for optimizing adding multiple
323
// properties to an object.
324
class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED {
325
 public:
326
  OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object,
327
                                             bool condition = true);
328
  ~OptimizedObjectForAddingMultipleProperties();
329
 private:
330
  bool has_been_transformed_;  // Tells whether the object has been transformed.
331
  int unused_property_fields_;  // Captures the unused number of field.
332
  Handle<JSObject> object_;    // The object being optimized.
333
};
334

    
335

    
336
} }  // namespace v8::internal
337

    
338
#endif  // V8_HANDLES_H_