Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: include/v8.h

Issue 842153004: Unify phantom and internal fields weak handle callbacks (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Require callback to reset handle. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/api.cc » ('j') | src/global-handles.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** \mainpage V8 API Reference Guide 5 /** \mainpage V8 API Reference Guide
6 * 6 *
7 * V8 is Google's open source JavaScript engine. 7 * V8 is Google's open source JavaScript engine.
8 * 8 *
9 * This set of documents provides reference material generated from the 9 * This set of documents provides reference material generated from the
10 * V8 header file, include/v8.h. 10 * V8 header file, include/v8.h.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 class Heap; 130 class Heap;
131 class HeapObject; 131 class HeapObject;
132 class Isolate; 132 class Isolate;
133 class Object; 133 class Object;
134 struct StreamedSource; 134 struct StreamedSource;
135 template<typename T> class CustomArguments; 135 template<typename T> class CustomArguments;
136 class PropertyCallbackArguments; 136 class PropertyCallbackArguments;
137 class FunctionCallbackArguments; 137 class FunctionCallbackArguments;
138 class GlobalHandles; 138 class GlobalHandles;
139 139
140 template <typename T>
140 class CallbackData { 141 class CallbackData {
dcarney 2015/01/09 14:31:10 one comment disappeared from my previous mail - th
Erik Corry Chromium.org 2015/01/12 11:37:53 The reason is static type checking. If your funct
141 public: 142 public:
142 V8_INLINE v8::Isolate* GetIsolate() const { return isolate_; } 143 V8_INLINE v8::Isolate* GetIsolate() const { return isolate_; }
143 144
144 protected: 145 explicit CallbackData(v8::Isolate* isolate, T* parameter)
145 explicit CallbackData(v8::Isolate* isolate) : isolate_(isolate) {} 146 : isolate_(isolate), parameter_(parameter) {}
147 V8_INLINE T* GetParameter() const { return parameter_; }
146 148
147 private: 149 private:
148 v8::Isolate* isolate_; 150 v8::Isolate* isolate_;
151 T* parameter_;
149 }; 152 };
150 } 153 }
151 154
152 155
153 /** 156 /**
154 * General purpose unique identifier. 157 * General purpose unique identifier.
155 */ 158 */
156 class UniqueId { 159 class UniqueId {
157 public: 160 public:
158 explicit UniqueId(intptr_t data) 161 explicit UniqueId(intptr_t data)
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 V8_INLINE Local<T> Get(Isolate* isolate); 421 V8_INLINE Local<T> Get(Isolate* isolate);
419 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; } 422 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
420 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle); 423 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
421 424
422 private: 425 private:
423 static const int kInitialValue = -1; 426 static const int kInitialValue = -1;
424 int index_; 427 int index_;
425 }; 428 };
426 429
427 430
428 template <typename T> 431 template <typename T, typename U = void, typename V = void>
429 class PhantomCallbackData : public internal::CallbackData { 432 class PhantomCallbackData : public internal::CallbackData<T> {
430 public: 433 public:
431 typedef void (*Callback)(const PhantomCallbackData<T>& data); 434 typedef void (*Callback)(const PhantomCallbackData<T, U, V>& data);
432 435
433 V8_INLINE T* GetParameter() const { return parameter_; } 436 V8_INLINE U* GetInternalField1() const { return internal_field1_; }
437 V8_INLINE V* GetInternalField2() const { return internal_field2_; }
434 438
435 PhantomCallbackData<T>(Isolate* isolate, T* parameter) 439 PhantomCallbackData(Isolate* isolate, T* parameter, U* internal_field1,
436 : internal::CallbackData(isolate), parameter_(parameter) {} 440 V* internal_field2)
441 : internal::CallbackData<T>(isolate, parameter),
442 internal_field1_(internal_field1),
443 internal_field2_(internal_field2) {}
437 444
438 private: 445 private:
439 T* parameter_; 446 U* internal_field1_;
447 V* internal_field2_;
440 }; 448 };
441 449
442 450
443 template <class T, class P> 451 template <class T, class P>
444 class WeakCallbackData : public PhantomCallbackData<P> { 452 class WeakCallbackData : public internal::CallbackData<P> {
445 public: 453 public:
446 typedef void (*Callback)(const WeakCallbackData<T, P>& data); 454 typedef void (*Callback)(const WeakCallbackData<T, P>& data);
447 455
448 V8_INLINE Local<T> GetValue() const { return handle_; } 456 V8_INLINE Local<T> GetValue() const { return handle_; }
449 457
450 private: 458 private:
451 friend class internal::GlobalHandles; 459 friend class internal::GlobalHandles;
452 WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle) 460 WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle)
453 : PhantomCallbackData<P>(isolate, parameter), handle_(handle) {} 461 : internal::CallbackData<P>(isolate, parameter), handle_(handle) {}
454 Local<T> handle_; 462 Local<T> handle_;
455 }; 463 };
456 464
457 465
458 template <typename T, typename U>
459 class InternalFieldsCallbackData : public internal::CallbackData {
460 public:
461 typedef void (*Callback)(const InternalFieldsCallbackData<T, U>& data);
462
463 InternalFieldsCallbackData(Isolate* isolate, T* internalField1,
464 U* internalField2)
465 : internal::CallbackData(isolate),
466 internal_field1_(internalField1),
467 internal_field2_(internalField2) {}
468
469 V8_INLINE T* GetInternalField1() const { return internal_field1_; }
470 V8_INLINE U* GetInternalField2() const { return internal_field2_; }
471
472 private:
473 T* internal_field1_;
474 U* internal_field2_;
475 };
476
477
478 /** 466 /**
479 * An object reference that is independent of any handle scope. Where 467 * An object reference that is independent of any handle scope. Where
480 * a Local handle only lives as long as the HandleScope in which it was 468 * a Local handle only lives as long as the HandleScope in which it was
481 * allocated, a PersistentBase handle remains valid until it is explicitly 469 * allocated, a PersistentBase handle remains valid until it is explicitly
482 * disposed. 470 * disposed.
483 * 471 *
484 * A persistent handle contains a reference to a storage cell within 472 * A persistent handle contains a reference to a storage cell within
485 * the v8 engine which holds an object value and which is updated by 473 * the v8 engine which holds an object value and which is updated by
486 * the garbage collector whenever the object is moved. A new storage 474 * the garbage collector whenever the object is moved. A new storage
487 * cell can be created using the constructor or PersistentBase::Reset and 475 * cell can be created using the constructor or PersistentBase::Reset and
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 } 527 }
540 528
541 /** 529 /**
542 * Install a finalization callback on this object. 530 * Install a finalization callback on this object.
543 * NOTE: There is no guarantee as to *when* or even *if* the callback is 531 * NOTE: There is no guarantee as to *when* or even *if* the callback is
544 * invoked. The invocation is performed solely on a best effort basis. 532 * invoked. The invocation is performed solely on a best effort basis.
545 * As always, GC-based finalization should *not* be relied upon for any 533 * As always, GC-based finalization should *not* be relied upon for any
546 * critical form of resource management! 534 * critical form of resource management!
547 */ 535 */
548 template<typename P> 536 template<typename P>
549 V8_INLINE void SetWeak( 537 V8_INLINE void SetWeak(
dcarney 2015/01/09 14:22:52 another follow up: make two new SetWeak(P*, WeakC
Erik Corry Chromium.org 2015/01/12 11:37:53 Again, I'd rather not move to this more dynamic ve
550 P* parameter, 538 P* parameter,
551 typename WeakCallbackData<T, P>::Callback callback); 539 typename WeakCallbackData<T, P>::Callback callback);
552 540
553 template<typename S, typename P> 541 template<typename S, typename P>
554 V8_INLINE void SetWeak( 542 V8_INLINE void SetWeak(
555 P* parameter, 543 P* parameter,
556 typename WeakCallbackData<S, P>::Callback callback); 544 typename WeakCallbackData<S, P>::Callback callback);
557 545
558 // Phantom persistents work like weak persistents, except that the pointer to 546 // Phantom persistents work like weak persistents, except that the pointer to
559 // the object being collected is not available in the finalization callback. 547 // the object being collected is not available in the finalization callback.
560 // This enables the garbage collector to collect the object and any objects 548 // This enables the garbage collector to collect the object and any objects
561 // it references transitively in one GC cycle. At the moment you can either 549 // it references transitively in one GC cycle. At the moment you can either
562 // specify a parameter for the callback or the location of two internal 550 // specify a parameter for the callback or the location of two internal
563 // fields in the dying object. 551 // fields in the dying object.
564 template <typename P> 552 template <typename P>
565 V8_INLINE void SetPhantom(P* parameter, 553 V8_INLINE void SetPhantom(
566 typename PhantomCallbackData<P>::Callback callback); 554 P* parameter,
567 555 typename PhantomCallbackData<P, void, void>::Callback callback);
568 template <typename P, typename Q> 556 template <typename P, typename Q>
569 V8_INLINE void SetPhantom( 557 V8_INLINE void SetPhantom(
570 void (*callback)(const InternalFieldsCallbackData<P, Q>&), 558 P* parameter, int internal_field_index1,
dcarney 2015/01/09 14:22:52 i'm not sure i see the point in setting these fiel
Erik Corry Chromium.org 2015/01/12 11:37:53 The code is not setting any fields from these para
571 int internal_field_index1, int internal_field_index2); 559 typename PhantomCallbackData<P, Q, void>::Callback callback);
560 template <typename P, typename Q, typename R>
561 V8_INLINE void SetPhantom(
562 P* parameter, int internal_field_index1, int internal_field_index2,
563 typename PhantomCallbackData<P, Q, R>::Callback callback);
572 564
573 template<typename P> 565 template<typename P>
574 V8_INLINE P* ClearWeak(); 566 V8_INLINE P* ClearWeak();
575 567
576 // TODO(dcarney): remove this. 568 // TODO(dcarney): remove this.
577 V8_INLINE void ClearWeak() { ClearWeak<void>(); } 569 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
578 570
579 /** 571 /**
580 * Marks the reference to this object independent. Garbage collector is free 572 * Marks the reference to this object independent. Garbage collector is free
581 * to ignore any object groups containing this object. Weak callback for an 573 * to ignore any object groups containing this object. Weak callback for an
(...skipping 5006 matching lines...) Expand 10 before | Expand all | Expand 10 after
5588 5580
5589 enum WeakHandleType { PhantomHandle, NonphantomHandle }; 5581 enum WeakHandleType { PhantomHandle, NonphantomHandle };
5590 5582
5591 static internal::Object** GlobalizeReference(internal::Isolate* isolate, 5583 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
5592 internal::Object** handle); 5584 internal::Object** handle);
5593 static internal::Object** CopyPersistent(internal::Object** handle); 5585 static internal::Object** CopyPersistent(internal::Object** handle);
5594 static void DisposeGlobal(internal::Object** global_handle); 5586 static void DisposeGlobal(internal::Object** global_handle);
5595 typedef WeakCallbackData<Value, void>::Callback WeakCallback; 5587 typedef WeakCallbackData<Value, void>::Callback WeakCallback;
5596 static void MakeWeak(internal::Object** global_handle, void* data, 5588 static void MakeWeak(internal::Object** global_handle, void* data,
5597 WeakCallback weak_callback); 5589 WeakCallback weak_callback);
5598 static void MakePhantom(internal::Object** global_handle, void* data,
5599 PhantomCallbackData<void>::Callback weak_callback);
5600 static void MakePhantom( 5590 static void MakePhantom(
5601 internal::Object** global_handle, 5591 internal::Object** global_handle, void* data,
5602 InternalFieldsCallbackData<void, void>::Callback weak_callback, 5592 // Must be 0 or kNoInternalFieldIndex.
5603 int internal_field_index1, 5593 int internal_field_index1,
5604 int internal_field_index2 = Object::kNoInternalFieldIndex); 5594 // Must be 1 or kNoInternalFieldIndex.
5595 int internal_field_index2,
5596 PhantomCallbackData<void, void, void>::Callback weak_callback);
5605 static void* ClearWeak(internal::Object** global_handle); 5597 static void* ClearWeak(internal::Object** global_handle);
5606 static void Eternalize(Isolate* isolate, 5598 static void Eternalize(Isolate* isolate,
5607 Value* handle, 5599 Value* handle,
5608 int* index); 5600 int* index);
5609 static Local<Value> GetEternal(Isolate* isolate, int index); 5601 static Local<Value> GetEternal(Isolate* isolate, int index);
5610 5602
5611 template <class T> friend class Handle; 5603 template <class T> friend class Handle;
5612 template <class T> friend class Local; 5604 template <class T> friend class Local;
5613 template <class T> friend class Eternal; 5605 template <class T> friend class Eternal;
5614 template <class T> friend class PersistentBase; 5606 template <class T> friend class PersistentBase;
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after
6482 void PersistentBase<T>::SetWeak( 6474 void PersistentBase<T>::SetWeak(
6483 P* parameter, 6475 P* parameter,
6484 typename WeakCallbackData<T, P>::Callback callback) { 6476 typename WeakCallbackData<T, P>::Callback callback) {
6485 SetWeak<T, P>(parameter, callback); 6477 SetWeak<T, P>(parameter, callback);
6486 } 6478 }
6487 6479
6488 6480
6489 template <class T> 6481 template <class T>
6490 template <typename P> 6482 template <typename P>
6491 void PersistentBase<T>::SetPhantom( 6483 void PersistentBase<T>::SetPhantom(
6492 P* parameter, typename PhantomCallbackData<P>::Callback callback) { 6484 P* parameter,
6493 typedef typename PhantomCallbackData<void>::Callback Callback; 6485 typename PhantomCallbackData<P, void, void>::Callback callback) {
6486 typedef typename PhantomCallbackData<void, void, void>::Callback Callback;
6494 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter, 6487 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter,
6488 Object::kNoInternalFieldIndex, Object::kNoInternalFieldIndex,
6495 reinterpret_cast<Callback>(callback)); 6489 reinterpret_cast<Callback>(callback));
6496 } 6490 }
6497 6491
6498 6492
6499 template <class T> 6493 template <class T>
6500 template <typename U, typename V> 6494 template <typename P, typename Q>
6501 void PersistentBase<T>::SetPhantom( 6495 void PersistentBase<T>::SetPhantom(
6502 void (*callback)(const InternalFieldsCallbackData<U, V>&), 6496 P* parameter, int internal_field_index1,
6503 int internal_field_index1, int internal_field_index2) { 6497 typename PhantomCallbackData<P, Q, void>::Callback callback) {
6504 typedef typename InternalFieldsCallbackData<void, void>::Callback Callback; 6498 typedef typename PhantomCallbackData<void, void, void>::Callback Callback;
6505 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), 6499 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter,
6506 reinterpret_cast<Callback>(callback), internal_field_index1, 6500 internal_field_index1, Object::kNoInternalFieldIndex,
6507 internal_field_index2); 6501 reinterpret_cast<Callback>(callback));
6508 } 6502 }
6509 6503
6510 6504
6505 template <class T>
6506 template <typename P, typename Q, typename R>
6507 void PersistentBase<T>::SetPhantom(
6508 P* parameter, int internal_field_index1, int internal_field_index2,
6509 typename PhantomCallbackData<P, Q, R>::Callback callback) {
6510 typedef typename PhantomCallbackData<void, void, void>::Callback Callback;
6511 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter,
6512 internal_field_index1, internal_field_index2,
6513 reinterpret_cast<Callback>(callback));
6514 }
6515
6516
6511 template <class T> 6517 template <class T>
6512 template <typename P> 6518 template <typename P>
6513 P* PersistentBase<T>::ClearWeak() { 6519 P* PersistentBase<T>::ClearWeak() {
6514 return reinterpret_cast<P*>( 6520 return reinterpret_cast<P*>(
6515 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); 6521 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
6516 } 6522 }
6517 6523
6518 6524
6519 template <class T> 6525 template <class T>
6520 void PersistentBase<T>::MarkIndependent() { 6526 void PersistentBase<T>::MarkIndependent() {
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
7494 */ 7500 */
7495 7501
7496 7502
7497 } // namespace v8 7503 } // namespace v8
7498 7504
7499 7505
7500 #undef TYPE_CHECK 7506 #undef TYPE_CHECK
7501 7507
7502 7508
7503 #endif // V8_H_ 7509 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | src/api.cc » ('j') | src/global-handles.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698