• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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_API_H_
29 #define V8_API_H_
30 
31 #include "apiutils.h"
32 #include "factory.h"
33 
34 namespace v8 {
35 
36 // Constants used in the implementation of the API.  The most natural thing
37 // would usually be to place these with the classes that use them, but
38 // we want to keep them out of v8.h because it is an externally
39 // visible file.
40 class Consts {
41  public:
42   enum TemplateType {
43     FUNCTION_TEMPLATE = 0,
44     OBJECT_TEMPLATE = 1
45   };
46 };
47 
48 
49 // Utilities for working with neander-objects, primitive
50 // env-independent JSObjects used by the api.
51 class NeanderObject {
52  public:
53   explicit NeanderObject(int size);
54   inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
55   inline NeanderObject(v8::internal::Object* obj);
56   inline v8::internal::Object* get(int index);
57   inline void set(int index, v8::internal::Object* value);
value()58   inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
59   int size();
60  private:
61   v8::internal::Handle<v8::internal::JSObject> value_;
62 };
63 
64 
65 // Utilities for working with neander-arrays, a simple extensible
66 // array abstraction built on neander-objects.
67 class NeanderArray {
68  public:
69   NeanderArray();
70   inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
value()71   inline v8::internal::Handle<v8::internal::JSObject> value() {
72     return obj_.value();
73   }
74 
75   void add(v8::internal::Handle<v8::internal::Object> value);
76 
77   int length();
78 
79   v8::internal::Object* get(int index);
80   // Change the value at an index to undefined value. If the index is
81   // out of bounds, the request is ignored. Returns the old value.
82   void set(int index, v8::internal::Object* value);
83  private:
84   NeanderObject obj_;
85 };
86 
87 
NeanderObject(v8::internal::Handle<v8::internal::Object> obj)88 NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
89     : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
90 
91 
NeanderObject(v8::internal::Object * obj)92 NeanderObject::NeanderObject(v8::internal::Object* obj)
93     : value_(v8::internal::Handle<v8::internal::JSObject>(
94         v8::internal::JSObject::cast(obj))) { }
95 
96 
NeanderArray(v8::internal::Handle<v8::internal::Object> obj)97 NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
98     : obj_(obj) { }
99 
100 
get(int offset)101 v8::internal::Object* NeanderObject::get(int offset) {
102   ASSERT(value()->HasFastElements());
103   return v8::internal::FixedArray::cast(value()->elements())->get(offset);
104 }
105 
106 
set(int offset,v8::internal::Object * value)107 void NeanderObject::set(int offset, v8::internal::Object* value) {
108   ASSERT(value_->HasFastElements());
109   v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
110 }
111 
112 
ToCData(v8::internal::Object * obj)113 template <typename T> static inline T ToCData(v8::internal::Object* obj) {
114   STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
115   return reinterpret_cast<T>(
116       reinterpret_cast<intptr_t>(v8::internal::Proxy::cast(obj)->proxy()));
117 }
118 
119 
120 template <typename T>
FromCData(T obj)121 static inline v8::internal::Handle<v8::internal::Object> FromCData(T obj) {
122   STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
123   return v8::internal::Factory::NewProxy(
124       reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
125 }
126 
127 
Arguments(v8::Local<v8::Value> data,v8::Local<v8::Object> holder,v8::Local<v8::Function> callee,bool is_construct_call,void ** values,int length)128 v8::Arguments::Arguments(v8::Local<v8::Value> data,
129                          v8::Local<v8::Object> holder,
130                          v8::Local<v8::Function> callee,
131                          bool is_construct_call,
132                          void** values, int length)
133     : data_(data), holder_(holder), callee_(callee),
134       is_construct_call_(is_construct_call),
135       values_(values), length_(length) { }
136 
137 
138 enum ExtensionTraversalState {
139   UNVISITED, VISITED, INSTALLED
140 };
141 
142 
143 class RegisteredExtension {
144  public:
145   explicit RegisteredExtension(Extension* extension);
146   static void Register(RegisteredExtension* that);
extension()147   Extension* extension() { return extension_; }
next()148   RegisteredExtension* next() { return next_; }
next_auto()149   RegisteredExtension* next_auto() { return next_auto_; }
state()150   ExtensionTraversalState state() { return state_; }
set_state(ExtensionTraversalState value)151   void set_state(ExtensionTraversalState value) { state_ = value; }
first_extension()152   static RegisteredExtension* first_extension() { return first_extension_; }
153  private:
154   Extension* extension_;
155   RegisteredExtension* next_;
156   RegisteredExtension* next_auto_;
157   ExtensionTraversalState state_;
158   static RegisteredExtension* first_extension_;
159   static RegisteredExtension* first_auto_extension_;
160 };
161 
162 
163 class Utils {
164  public:
165   static bool ReportApiFailure(const char* location, const char* message);
166 
167   static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
168   static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
169 
170   static inline Local<Context> ToLocal(
171       v8::internal::Handle<v8::internal::Context> obj);
172   static inline Local<Value> ToLocal(
173       v8::internal::Handle<v8::internal::Object> obj);
174   static inline Local<Function> ToLocal(
175       v8::internal::Handle<v8::internal::JSFunction> obj);
176   static inline Local<String> ToLocal(
177       v8::internal::Handle<v8::internal::String> obj);
178   static inline Local<Object> ToLocal(
179       v8::internal::Handle<v8::internal::JSObject> obj);
180   static inline Local<Array> ToLocal(
181       v8::internal::Handle<v8::internal::JSArray> obj);
182   static inline Local<External> ToLocal(
183       v8::internal::Handle<v8::internal::Proxy> obj);
184   static inline Local<Message> MessageToLocal(
185       v8::internal::Handle<v8::internal::Object> obj);
186   static inline Local<Number> NumberToLocal(
187       v8::internal::Handle<v8::internal::Object> obj);
188   static inline Local<Integer> IntegerToLocal(
189       v8::internal::Handle<v8::internal::Object> obj);
190   static inline Local<Uint32> Uint32ToLocal(
191       v8::internal::Handle<v8::internal::Object> obj);
192   static inline Local<FunctionTemplate> ToLocal(
193       v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
194   static inline Local<ObjectTemplate> ToLocal(
195       v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
196   static inline Local<Signature> ToLocal(
197       v8::internal::Handle<v8::internal::SignatureInfo> obj);
198   static inline Local<TypeSwitch> ToLocal(
199       v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
200 
201   static inline v8::internal::Handle<v8::internal::TemplateInfo>
202       OpenHandle(const Template* that);
203   static inline v8::internal::Handle<v8::internal::FunctionTemplateInfo>
204       OpenHandle(const FunctionTemplate* that);
205   static inline v8::internal::Handle<v8::internal::ObjectTemplateInfo>
206       OpenHandle(const ObjectTemplate* that);
207   static inline v8::internal::Handle<v8::internal::Object>
208       OpenHandle(const Data* data);
209   static inline v8::internal::Handle<v8::internal::JSObject>
210       OpenHandle(const v8::Object* data);
211   static inline v8::internal::Handle<v8::internal::JSArray>
212       OpenHandle(const v8::Array* data);
213   static inline v8::internal::Handle<v8::internal::String>
214       OpenHandle(const String* data);
215   static inline v8::internal::Handle<v8::internal::JSFunction>
216       OpenHandle(const Script* data);
217   static inline v8::internal::Handle<v8::internal::JSFunction>
218       OpenHandle(const Function* data);
219   static inline v8::internal::Handle<v8::internal::JSObject>
220       OpenHandle(const Message* message);
221   static inline v8::internal::Handle<v8::internal::Context>
222       OpenHandle(const v8::Context* context);
223   static inline v8::internal::Handle<v8::internal::SignatureInfo>
224       OpenHandle(const v8::Signature* sig);
225   static inline v8::internal::Handle<v8::internal::TypeSwitchInfo>
226       OpenHandle(const v8::TypeSwitch* that);
227   static inline v8::internal::Handle<v8::internal::Proxy>
228       OpenHandle(const v8::External* that);
229 };
230 
231 
232 template <class T>
ToApi(v8::internal::Handle<v8::internal::Object> obj)233 static inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
234   return reinterpret_cast<T*>(obj.location());
235 }
236 
237 
238 template <class T>
EscapeFrom(v8::HandleScope * scope)239 v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
240     v8::HandleScope* scope) {
241   return Utils::OpenHandle(*scope->Close(Utils::ToLocal(*this)));
242 }
243 
244 
245 // Implementations of ToLocal
246 
247 #define MAKE_TO_LOCAL(Name, From, To)                                       \
248   Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
249     ASSERT(!obj->IsTheHole());                                              \
250     return Local<To>(reinterpret_cast<To*>(obj.location()));                \
251   }
252 
MAKE_TO_LOCAL(ToLocal,Context,Context)253 MAKE_TO_LOCAL(ToLocal, Context, Context)
254 MAKE_TO_LOCAL(ToLocal, Object, Value)
255 MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
256 MAKE_TO_LOCAL(ToLocal, String, String)
257 MAKE_TO_LOCAL(ToLocal, JSObject, Object)
258 MAKE_TO_LOCAL(ToLocal, JSArray, Array)
259 MAKE_TO_LOCAL(ToLocal, Proxy, External)
260 MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
261 MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
262 MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature)
263 MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
264 MAKE_TO_LOCAL(MessageToLocal, Object, Message)
265 MAKE_TO_LOCAL(NumberToLocal, Object, Number)
266 MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
267 MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
268 
269 #undef MAKE_TO_LOCAL
270 
271 
272 // Implementations of OpenHandle
273 
274 #define MAKE_OPEN_HANDLE(From, To) \
275   v8::internal::Handle<v8::internal::To> Utils::OpenHandle(\
276     const v8::From* that) { \
277     return v8::internal::Handle<v8::internal::To>( \
278         reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
279   }
280 
281 MAKE_OPEN_HANDLE(Template, TemplateInfo)
282 MAKE_OPEN_HANDLE(FunctionTemplate, FunctionTemplateInfo)
283 MAKE_OPEN_HANDLE(ObjectTemplate, ObjectTemplateInfo)
284 MAKE_OPEN_HANDLE(Signature, SignatureInfo)
285 MAKE_OPEN_HANDLE(TypeSwitch, TypeSwitchInfo)
286 MAKE_OPEN_HANDLE(Data, Object)
287 MAKE_OPEN_HANDLE(Object, JSObject)
288 MAKE_OPEN_HANDLE(Array, JSArray)
289 MAKE_OPEN_HANDLE(String, String)
290 MAKE_OPEN_HANDLE(Script, JSFunction)
291 MAKE_OPEN_HANDLE(Function, JSFunction)
292 MAKE_OPEN_HANDLE(Message, JSObject)
293 MAKE_OPEN_HANDLE(Context, Context)
294 MAKE_OPEN_HANDLE(External, Proxy)
295 
296 #undef MAKE_OPEN_HANDLE
297 
298 
299 namespace internal {
300 
301 // This class is here in order to be able to declare it a friend of
302 // HandleScope.  Moving these methods to be members of HandleScope would be
303 // neat in some ways, but it would expose external implementation details in
304 // our public header file, which is undesirable.
305 //
306 // There is a singleton instance of this class to hold the per-thread data.
307 // For multithreaded V8 programs this data is copied in and out of storage
308 // so that the currently executing thread always has its own copy of this
309 // data.
310 class HandleScopeImplementer {
311  public:
312 
313   HandleScopeImplementer()
314       : blocks(0),
315         entered_contexts_(0),
316         saved_contexts_(0) {
317     Initialize();
318   }
319 
320   void Initialize() {
321     blocks.Initialize(0);
322     entered_contexts_.Initialize(0);
323     saved_contexts_.Initialize(0);
324     spare = NULL;
325     ignore_out_of_memory = false;
326     call_depth = 0;
327   }
328 
329   static HandleScopeImplementer* instance();
330 
331   // Threading support for handle data.
332   static int ArchiveSpacePerThread();
333   static char* RestoreThread(char* from);
334   static char* ArchiveThread(char* to);
335 
336   // Garbage collection support.
337   static void Iterate(v8::internal::ObjectVisitor* v);
338   static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
339 
340 
341   inline internal::Object** GetSpareOrNewBlock();
342   inline void DeleteExtensions(int extensions);
343 
344   inline void IncrementCallDepth() {call_depth++;}
345   inline void DecrementCallDepth() {call_depth--;}
346   inline bool CallDepthIsZero() { return call_depth == 0; }
347 
348   inline void EnterContext(Handle<Object> context);
349   inline bool LeaveLastContext();
350 
351   // Returns the last entered context or an empty handle if no
352   // contexts have been entered.
353   inline Handle<Object> LastEnteredContext();
354 
355   inline void SaveContext(Context* context);
356   inline Context* RestoreContext();
357   inline bool HasSavedContexts();
358 
359   inline List<internal::Object**>* Blocks() { return &blocks; }
360 
361   inline bool IgnoreOutOfMemory() { return ignore_out_of_memory; }
362   inline void SetIgnoreOutOfMemory(bool value) { ignore_out_of_memory = value; }
363 
364  private:
365   List<internal::Object**> blocks;
366   Object** spare;
367   int call_depth;
368   // Used as a stack to keep track of entered contexts.
369   List<Handle<Object> > entered_contexts_;
370   // Used as a stack to keep track of saved contexts.
371   List<Context*> saved_contexts_;
372   bool ignore_out_of_memory;
373   // This is only used for threading support.
374   v8::ImplementationUtilities::HandleScopeData handle_scope_data_;
375 
376   void IterateThis(ObjectVisitor* v);
377   char* RestoreThreadHelper(char* from);
378   char* ArchiveThreadHelper(char* to);
379 
380   DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
381 };
382 
383 
384 static const int kHandleBlockSize = v8::internal::KB - 2;  // fit in one page
385 
386 
387 void HandleScopeImplementer::SaveContext(Context* context) {
388   saved_contexts_.Add(context);
389 }
390 
391 
392 Context* HandleScopeImplementer::RestoreContext() {
393   return saved_contexts_.RemoveLast();
394 }
395 
396 
397 bool HandleScopeImplementer::HasSavedContexts() {
398   return !saved_contexts_.is_empty();
399 }
400 
401 
402 void HandleScopeImplementer::EnterContext(Handle<Object> context) {
403   entered_contexts_.Add(context);
404 }
405 
406 
407 bool HandleScopeImplementer::LeaveLastContext() {
408   if (entered_contexts_.is_empty()) return false;
409   entered_contexts_.RemoveLast();
410   return true;
411 }
412 
413 
414 Handle<Object> HandleScopeImplementer::LastEnteredContext() {
415   if (entered_contexts_.is_empty()) return Handle<Object>::null();
416   return entered_contexts_.last();
417 }
418 
419 
420 // If there's a spare block, use it for growing the current scope.
421 internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
422   internal::Object** block = (spare != NULL) ?
423       spare :
424       NewArray<internal::Object*>(kHandleBlockSize);
425   spare = NULL;
426   return block;
427 }
428 
429 
430 void HandleScopeImplementer::DeleteExtensions(int extensions) {
431   if (spare != NULL) {
432     DeleteArray(spare);
433     spare = NULL;
434   }
435   for (int i = extensions; i > 1; --i) {
436     internal::Object** block = blocks.RemoveLast();
437 #ifdef DEBUG
438     v8::ImplementationUtilities::ZapHandleRange(block,
439                                                 &block[kHandleBlockSize]);
440 #endif
441     DeleteArray(block);
442   }
443   spare = blocks.RemoveLast();
444 #ifdef DEBUG
445   v8::ImplementationUtilities::ZapHandleRange(
446       spare,
447       &spare[kHandleBlockSize]);
448 #endif
449 }
450 
451 } }  // namespace v8::internal
452 
453 #endif  // V8_API_H_
454