OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 class StackTrace; | 113 class StackTrace; |
114 class String; | 114 class String; |
115 class StringObject; | 115 class StringObject; |
116 class Symbol; | 116 class Symbol; |
117 class SymbolObject; | 117 class SymbolObject; |
118 class Uint32; | 118 class Uint32; |
119 class Utils; | 119 class Utils; |
120 class Value; | 120 class Value; |
121 template <class T> class Handle; | 121 template <class T> class Handle; |
122 template <class T> class Local; | 122 template <class T> class Local; |
| 123 template <class T> class Eternal; |
123 template <class T> class Persistent; | 124 template <class T> class Persistent; |
124 class FunctionTemplate; | 125 class FunctionTemplate; |
125 class ObjectTemplate; | 126 class ObjectTemplate; |
126 class Data; | 127 class Data; |
127 class AccessorInfo; | 128 class AccessorInfo; |
128 template<typename T> class PropertyCallbackInfo; | 129 template<typename T> class PropertyCallbackInfo; |
129 class StackTrace; | 130 class StackTrace; |
130 class StackFrame; | 131 class StackFrame; |
131 class Isolate; | 132 class Isolate; |
132 class DeclaredAccessorDescriptor; | 133 class DeclaredAccessorDescriptor; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 friend class HandleScope; | 364 friend class HandleScope; |
364 | 365 |
365 #ifndef V8_USE_UNSAFE_HANDLES | 366 #ifndef V8_USE_UNSAFE_HANDLES |
366 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that)); | 367 V8_INLINE(static Handle<T> New(Isolate* isolate, T* that)); |
367 #endif | 368 #endif |
368 | 369 |
369 T* val_; | 370 T* val_; |
370 }; | 371 }; |
371 | 372 |
372 | 373 |
373 // A value which will never be returned by Local::Eternalize | |
374 // Useful for static initialization | |
375 const int kUninitializedEternalIndex = -1; | |
376 | |
377 | |
378 /** | 374 /** |
379 * A light-weight stack-allocated object handle. All operations | 375 * A light-weight stack-allocated object handle. All operations |
380 * that return objects from within v8 return them in local handles. They | 376 * that return objects from within v8 return them in local handles. They |
381 * are created within HandleScopes, and all local handles allocated within a | 377 * are created within HandleScopes, and all local handles allocated within a |
382 * handle scope are destroyed when the handle scope is destroyed. Hence it | 378 * handle scope are destroyed when the handle scope is destroyed. Hence it |
383 * is not necessary to explicitly deallocate local handles. | 379 * is not necessary to explicitly deallocate local handles. |
384 */ | 380 */ |
385 // TODO(dcarney): deprecate entire class | 381 // TODO(dcarney): deprecate entire class |
386 template <class T> class Local : public Handle<T> { | 382 template <class T> class Local : public Handle<T> { |
387 public: | 383 public: |
(...skipping 25 matching lines...) Expand all Loading... |
413 template <class S> V8_INLINE(Local(Handle<S> that)) | 409 template <class S> V8_INLINE(Local(Handle<S> that)) |
414 : Handle<T>(reinterpret_cast<T*>(*that)) { | 410 : Handle<T>(reinterpret_cast<T*>(*that)) { |
415 TYPE_CHECK(T, S); | 411 TYPE_CHECK(T, S); |
416 } | 412 } |
417 #endif | 413 #endif |
418 | 414 |
419 template <class S> V8_INLINE(Local<S> As()) { | 415 template <class S> V8_INLINE(Local<S> As()) { |
420 return Local<S>::Cast(*this); | 416 return Local<S>::Cast(*this); |
421 } | 417 } |
422 | 418 |
423 // Keep this Local alive for the lifetime of the Isolate. | |
424 // It remains retrievable via the returned index, | |
425 V8_INLINE(int Eternalize(Isolate* isolate)); | |
426 V8_INLINE(static Local<T> GetEternal(Isolate* isolate, int index)); | |
427 | |
428 /** | 419 /** |
429 * Create a local handle for the content of another handle. | 420 * Create a local handle for the content of another handle. |
430 * The referee is kept alive by the local handle even when | 421 * The referee is kept alive by the local handle even when |
431 * the original handle is destroyed/disposed. | 422 * the original handle is destroyed/disposed. |
432 */ | 423 */ |
433 V8_INLINE(static Local<T> New(Handle<T> that)); | 424 V8_INLINE(static Local<T> New(Handle<T> that)); |
434 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that)); | 425 V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that)); |
435 #ifndef V8_USE_UNSAFE_HANDLES | 426 #ifndef V8_USE_UNSAFE_HANDLES |
436 // TODO(dcarney): remove before cutover | 427 // TODO(dcarney): remove before cutover |
437 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that)); | 428 V8_INLINE(static Local<T> New(Isolate* isolate, const Persistent<T>& that)); |
438 | 429 |
439 #ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR | 430 #ifndef V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR |
440 | 431 |
441 private: | 432 private: |
442 #endif | 433 #endif |
443 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { } | 434 template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { } |
444 #endif | 435 #endif |
445 | 436 |
446 private: | 437 private: |
447 friend class Utils; | 438 friend class Utils; |
| 439 template<class F> friend class Eternal; |
448 template<class F> friend class Persistent; | 440 template<class F> friend class Persistent; |
449 template<class F> friend class Handle; | 441 template<class F> friend class Handle; |
450 friend class Arguments; | 442 friend class Arguments; |
451 template<class F> friend class FunctionCallbackInfo; | 443 template<class F> friend class FunctionCallbackInfo; |
452 template<class F> friend class PropertyCallbackInfo; | 444 template<class F> friend class PropertyCallbackInfo; |
453 friend class String; | 445 friend class String; |
454 friend class Object; | 446 friend class Object; |
455 friend class AccessorInfo; | 447 friend class AccessorInfo; |
456 friend class Context; | 448 friend class Context; |
457 template<class F> friend class internal::CustomArguments; | 449 template<class F> friend class internal::CustomArguments; |
458 friend class HandleScope; | 450 friend class HandleScope; |
459 | 451 |
460 V8_INLINE(static Local<T> New(Isolate* isolate, T* that)); | 452 V8_INLINE(static Local<T> New(Isolate* isolate, T* that)); |
461 }; | 453 }; |
462 | 454 |
| 455 |
| 456 // Eternal handles are set-once handles that live for the life of the isolate. |
| 457 template <class T> class Eternal { |
| 458 public: |
| 459 V8_INLINE(Eternal()) : index_(kInitialValue) { } |
| 460 template<class S> |
| 461 V8_INLINE(Eternal(Isolate* isolate, Local<S> handle)) |
| 462 : index_(kInitialValue) { |
| 463 Set(isolate, handle); |
| 464 } |
| 465 // Can only be safely called if already set. |
| 466 V8_INLINE(Local<T> Get(Isolate* isolate)); |
| 467 V8_INLINE(bool IsEmpty()) { return index_ != kInitialValue; } |
| 468 template<class S> |
| 469 V8_INLINE(void Set(Isolate* isolate, Local<S> handle)); |
| 470 |
| 471 private: |
| 472 static const int kInitialValue = -1; |
| 473 int index_; |
| 474 }; |
| 475 |
| 476 |
463 /** | 477 /** |
464 * An object reference that is independent of any handle scope. Where | 478 * An object reference that is independent of any handle scope. Where |
465 * a Local handle only lives as long as the HandleScope in which it was | 479 * a Local handle only lives as long as the HandleScope in which it was |
466 * allocated, a Persistent handle remains valid until it is explicitly | 480 * allocated, a Persistent handle remains valid until it is explicitly |
467 * disposed. | 481 * disposed. |
468 * | 482 * |
469 * A persistent handle contains a reference to a storage cell within | 483 * A persistent handle contains a reference to a storage cell within |
470 * the v8 engine which holds an object value and which is updated by | 484 * the v8 engine which holds an object value and which is updated by |
471 * the garbage collector whenever the object is moved. A new storage | 485 * the garbage collector whenever the object is moved. A new storage |
472 * cell can be created using Persistent::New and existing handles can | 486 * cell can be created using Persistent::New and existing handles can |
(...skipping 4308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4781 V8(); | 4795 V8(); |
4782 | 4796 |
4783 static internal::Object** GlobalizeReference(internal::Isolate* isolate, | 4797 static internal::Object** GlobalizeReference(internal::Isolate* isolate, |
4784 internal::Object** handle); | 4798 internal::Object** handle); |
4785 static void DisposeGlobal(internal::Object** global_handle); | 4799 static void DisposeGlobal(internal::Object** global_handle); |
4786 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback; | 4800 typedef WeakReferenceCallbacks<Value, void>::Revivable RevivableCallback; |
4787 static void MakeWeak(internal::Object** global_handle, | 4801 static void MakeWeak(internal::Object** global_handle, |
4788 void* data, | 4802 void* data, |
4789 RevivableCallback weak_reference_callback); | 4803 RevivableCallback weak_reference_callback); |
4790 static void ClearWeak(internal::Object** global_handle); | 4804 static void ClearWeak(internal::Object** global_handle); |
4791 static int Eternalize(internal::Isolate* isolate, | 4805 static void Eternalize(Isolate* isolate, |
4792 internal::Object** handle); | 4806 Value* handle, |
4793 static internal::Object** GetEternal(internal::Isolate* isolate, int index); | 4807 int* index); |
| 4808 static Local<Value> GetEternal(Isolate* isolate, int index); |
4794 | 4809 |
4795 template <class T> friend class Handle; | 4810 template <class T> friend class Handle; |
4796 template <class T> friend class Local; | 4811 template <class T> friend class Local; |
| 4812 template <class T> friend class Eternal; |
4797 template <class T> friend class Persistent; | 4813 template <class T> friend class Persistent; |
4798 friend class Context; | 4814 friend class Context; |
4799 }; | 4815 }; |
4800 | 4816 |
4801 | 4817 |
4802 /** | 4818 /** |
4803 * An external exception handler. | 4819 * An external exception handler. |
4804 */ | 4820 */ |
4805 class V8_EXPORT TryCatch { | 4821 class V8_EXPORT TryCatch { |
4806 public: | 4822 public: |
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5648 Local<T> Local<T>::New(Isolate* isolate, T* that) { | 5664 Local<T> Local<T>::New(Isolate* isolate, T* that) { |
5649 if (that == NULL) return Local<T>(); | 5665 if (that == NULL) return Local<T>(); |
5650 T* that_ptr = that; | 5666 T* that_ptr = that; |
5651 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); | 5667 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); |
5652 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( | 5668 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( |
5653 reinterpret_cast<internal::Isolate*>(isolate), *p))); | 5669 reinterpret_cast<internal::Isolate*>(isolate), *p))); |
5654 } | 5670 } |
5655 | 5671 |
5656 | 5672 |
5657 template<class T> | 5673 template<class T> |
5658 int Local<T>::Eternalize(Isolate* isolate) { | 5674 template<class S> |
5659 return V8::Eternalize(reinterpret_cast<internal::Isolate*>(isolate), | 5675 void Eternal<T>::Set(Isolate* isolate, Local<S> handle) { |
5660 reinterpret_cast<internal::Object**>(this->val_)); | 5676 TYPE_CHECK(T, S); |
| 5677 V8::Eternalize(isolate, Value::Cast(*handle), &this->index_); |
5661 } | 5678 } |
5662 | 5679 |
5663 | 5680 |
5664 template<class T> | 5681 template<class T> |
5665 Local<T> Local<T>::GetEternal(Isolate* isolate, int index) { | 5682 Local<T> Eternal<T>::Get(Isolate* isolate) { |
5666 internal::Object** handle = | 5683 return Local<T>::Cast(V8::GetEternal(isolate, index_)); |
5667 V8::GetEternal(reinterpret_cast<internal::Isolate*>(isolate), index); | |
5668 return Local<T>(T::Cast(reinterpret_cast<Value*>(handle))); | |
5669 } | 5684 } |
5670 | 5685 |
5671 | 5686 |
5672 #ifdef V8_USE_UNSAFE_HANDLES | 5687 #ifdef V8_USE_UNSAFE_HANDLES |
5673 template <class T> | 5688 template <class T> |
5674 Persistent<T> Persistent<T>::New(Handle<T> that) { | 5689 Persistent<T> Persistent<T>::New(Handle<T> that) { |
5675 return New(Isolate::GetCurrent(), that.val_); | 5690 return New(Isolate::GetCurrent(), that.val_); |
5676 } | 5691 } |
5677 | 5692 |
5678 | 5693 |
(...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6577 */ | 6592 */ |
6578 | 6593 |
6579 | 6594 |
6580 } // namespace v8 | 6595 } // namespace v8 |
6581 | 6596 |
6582 | 6597 |
6583 #undef TYPE_CHECK | 6598 #undef TYPE_CHECK |
6584 | 6599 |
6585 | 6600 |
6586 #endif // V8_H_ | 6601 #endif // V8_H_ |
OLD | NEW |