handles.h 11.2 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_HANDLES_H_
#define V8_HANDLES_H_

31
#include "allocation.h"
32 33
#include "apiutils.h"

34 35
namespace v8 {
namespace internal {
36 37 38 39

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

43
template<typename T>
44 45
class Handle {
 public:
46
  INLINE(explicit Handle(T** location)) { location_ = location; }
47
  INLINE(explicit Handle(T* obj));
48
  INLINE(Handle(T* obj, Isolate* isolate));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

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

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

64
  INLINE(T* operator ->() const) { return operator*(); }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

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

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

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

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

  static Handle<T> null() { return Handle<T>(); }
87
  bool is_null() const { return location_ == NULL; }
88 89 90

  // Closes the given scope, but lets this handle escape. See
  // implementation in api.h.
91
  inline Handle<T> EscapeFrom(v8::HandleScope* scope);
92 93 94 95 96 97

 private:
  T** location_;
};


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

115
  inline ~HandleScope();
116 117 118 119 120

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

  // Creates a new handle with the given value.
121
  template <typename T>
122
  static inline T** CreateHandle(T* value, Isolate* isolate);
123

124
  // Deallocates any extensions used by the current scope.
125
  static void DeleteExtensions(Isolate* isolate);
126 127 128

  static Address current_next_address();
  static Address current_limit_address();
129
  static Address current_level_address();
130

131 132 133 134 135
  // Closes the HandleScope (invalidating all handles
  // created in the scope of the HandleScope) and returns
  // a Handle backed by the parent scope holding the
  // value of the argument handle.
  template <typename T>
136 137 138
  Handle<T> CloseAndEscape(Handle<T> handle_value);

  Isolate* isolate() { return isolate_; }
139

140 141 142 143 144 145 146
 private:
  // Prevent heap allocation or illegal handle scopes.
  HandleScope(const HandleScope&);
  void operator=(const HandleScope&);
  void* operator new(size_t size);
  void operator delete(void* size_t);

147
  inline void CloseScope();
148

149
  Isolate* isolate_;
150 151
  Object** prev_next_;
  Object** prev_limit_;
152

153
  // Extend the handle scope making room for more handles.
154
  static internal::Object** Extend();
155

156
  // Zaps the handles in the half-open interval [start, end).
157
  static void ZapRange(internal::Object** start, internal::Object** end);
158 159 160 161 162 163

  friend class v8::HandleScope;
  friend class v8::ImplementationUtilities;
};


164 165
// ----------------------------------------------------------------------------
// Handle operations.
166 167 168
// They might invoke garbage collection. The result is an handle to
// an object of expected type, or the handle is an error if running out
// of space or encountering an internal error.
169

170
// Flattens a string.
171 172
void FlattenString(Handle<String> str);

173
// Flattens a string and returns the underlying external or sequential
174
// string.
175 176
Handle<String> FlattenGetString(Handle<String> str);

177 178 179
Handle<Object> SetProperty(Handle<Object> object,
                           Handle<Object> key,
                           Handle<Object> value,
180
                           PropertyAttributes attributes,
181
                           StrictModeFlag strict_mode);
182

183 184 185 186 187
Handle<Object> ForceSetProperty(Handle<JSObject> object,
                                Handle<Object> key,
                                Handle<Object> value,
                                PropertyAttributes attributes);

188 189 190
Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
                                   Handle<Object> key);

191
Handle<Object> GetProperty(Handle<JSReceiver> obj,
192 193 194 195 196 197 198 199 200 201
                           const char* name);

Handle<Object> GetProperty(Handle<Object> obj,
                           Handle<Object> key);

Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
                                          Handle<JSObject> holder,
                                          Handle<String> name,
                                          PropertyAttributes* attributes);

202 203
Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value);

204 205
Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);

206
Handle<JSObject> Copy(Handle<JSObject> obj);
207

208 209
Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info);

210 211 212
Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
                                      Handle<JSArray> array);

213 214 215 216
// Get the JS object corresponding to the given script; create it
// if none exists.
Handle<JSValue> GetScriptWrapper(Handle<Script> script);

217 218
// Script line number computations.
void InitScriptLineEnds(Handle<Script> script);
219 220 221 222 223
// For string calculates an array of line end positions. If the string
// does not end with a new line character, this character may optionally be
// imagined.
Handle<FixedArray> CalculateLineEnds(Handle<String> string,
                                     bool with_imaginary_last_new_line);
224
int GetScriptLineNumber(Handle<Script> script, int code_position);
225 226
// The safe version does not make heap allocations but may work much slower.
int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
227
int GetScriptColumnNumber(Handle<Script> script, int code_position);
228

229 230
// Computes the enumerable keys from interceptors. Used for debug mirrors and
// by GetKeysInFixedArrayFor below.
231
v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
232
                                                 Handle<JSObject> object);
233
v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
234
                                                   Handle<JSObject> object);
235 236 237

enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };

238 239
// Computes the enumerable keys for a JSObject. Used for implementing
// "for (n in object) { }".
240 241 242 243
Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
                                          KeyCollectionType type,
                                          bool* threw);
Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
244 245
Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
                                       bool cache_result);
246 247 248 249 250 251

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

252 253 254 255
Handle<String> SubString(Handle<String> str,
                         int start,
                         int end,
                         PretenureFlag pretenure = NOT_TENURED);
256 257 258 259 260 261 262 263 264 265 266 267

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

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

// Sets the expected number of properties based on estimate from compiler.
void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
                                          int estimate);


268
Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
269
    Handle<JSFunction> constructor,
270
    Handle<JSGlobalProxy> global);
271 272 273 274

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

275 276 277 278 279 280
Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
                                       Handle<Object> key);

Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
                                          Handle<Object> key);

281
Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
282
                                               Handle<Object> key,
283 284
                                               Handle<Object> value);

285 286 287 288 289 290 291 292 293
class NoHandleAllocation BASE_EMBEDDED {
 public:
#ifndef DEBUG
  NoHandleAllocation() {}
  ~NoHandleAllocation() {}
#else
  inline NoHandleAllocation();
  inline ~NoHandleAllocation();
 private:
294
  int level_;
295 296 297 298 299 300
#endif
};

} }  // namespace v8::internal

#endif  // V8_HANDLES_H_