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

Side by Side Diff: include/v8.h

Issue 753553002: Phantom references support internal fields (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Delete allocated objects in test to make memory sanitizer happy Created 6 years 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') | no next file with comments »
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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 class Arguments; 133 class Arguments;
134 class Heap; 134 class Heap;
135 class HeapObject; 135 class HeapObject;
136 class Isolate; 136 class Isolate;
137 class Object; 137 class Object;
138 struct StreamedSource; 138 struct StreamedSource;
139 template<typename T> class CustomArguments; 139 template<typename T> class CustomArguments;
140 class PropertyCallbackArguments; 140 class PropertyCallbackArguments;
141 class FunctionCallbackArguments; 141 class FunctionCallbackArguments;
142 class GlobalHandles; 142 class GlobalHandles;
143
144 class CallbackData {
145 public:
146 V8_INLINE v8::Isolate* GetIsolate() const { return isolate_; }
147
148 protected:
149 explicit CallbackData(v8::Isolate* isolate) : isolate_(isolate) {}
150
151 private:
152 v8::Isolate* isolate_;
153 };
143 } 154 }
144 155
145 156
146 /** 157 /**
147 * General purpose unique identifier. 158 * General purpose unique identifier.
148 */ 159 */
149 class UniqueId { 160 class UniqueId {
150 public: 161 public:
151 explicit UniqueId(intptr_t data) 162 explicit UniqueId(intptr_t data)
152 : data_(data) {} 163 : data_(data) {}
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 V8_INLINE Local<T> Get(Isolate* isolate); 422 V8_INLINE Local<T> Get(Isolate* isolate);
412 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; } 423 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
413 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle); 424 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
414 425
415 private: 426 private:
416 static const int kInitialValue = -1; 427 static const int kInitialValue = -1;
417 int index_; 428 int index_;
418 }; 429 };
419 430
420 431
421 template<class T, class P> 432 template <typename T>
422 class WeakCallbackData { 433 class PhantomCallbackData : public internal::CallbackData {
434 public:
435 typedef void (*Callback)(const PhantomCallbackData<T>& data);
436
437 V8_INLINE T* GetParameter() const { return parameter_; }
438
439 PhantomCallbackData<T>(Isolate* isolate, T* parameter)
440 : internal::CallbackData(isolate), parameter_(parameter) {}
441
442 private:
443 T* parameter_;
444 };
445
446
447 template <class T, class P>
448 class WeakCallbackData : public PhantomCallbackData<P> {
423 public: 449 public:
424 typedef void (*Callback)(const WeakCallbackData<T, P>& data); 450 typedef void (*Callback)(const WeakCallbackData<T, P>& data);
425 451
426 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
427 V8_INLINE Local<T> GetValue() const { return handle_; } 452 V8_INLINE Local<T> GetValue() const { return handle_; }
428 V8_INLINE P* GetParameter() const { return parameter_; }
429 453
430 private: 454 private:
431 friend class internal::GlobalHandles; 455 friend class internal::GlobalHandles;
432 WeakCallbackData(Isolate* isolate, Local<T> handle, P* parameter) 456 WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle)
433 : isolate_(isolate), handle_(handle), parameter_(parameter) { } 457 : PhantomCallbackData<P>(isolate, parameter), handle_(handle) {}
434 Isolate* isolate_;
435 Local<T> handle_; 458 Local<T> handle_;
436 P* parameter_;
437 }; 459 };
438 460
439 461
462 template <typename T, typename U>
463 class InternalFieldsCallbackData : public internal::CallbackData {
464 public:
465 typedef void (*Callback)(const InternalFieldsCallbackData<T, U>& data);
466
467 InternalFieldsCallbackData(Isolate* isolate, T* internalField1,
468 U* internalField2)
469 : internal::CallbackData(isolate),
470 internal_field1_(internalField1),
471 internal_field2_(internalField2) {}
472
473 V8_INLINE T* GetInternalField1() const { return internal_field1_; }
474 V8_INLINE U* GetInternalField2() const { return internal_field2_; }
475
476 private:
477 T* internal_field1_;
dcarney 2015/01/07 13:18:51 I'm a little late weighing in here, but I have a f
478 U* internal_field2_;
479 };
480
481
440 /** 482 /**
441 * An object reference that is independent of any handle scope. Where 483 * An object reference that is independent of any handle scope. Where
442 * a Local handle only lives as long as the HandleScope in which it was 484 * a Local handle only lives as long as the HandleScope in which it was
443 * allocated, a PersistentBase handle remains valid until it is explicitly 485 * allocated, a PersistentBase handle remains valid until it is explicitly
444 * disposed. 486 * disposed.
445 * 487 *
446 * A persistent handle contains a reference to a storage cell within 488 * A persistent handle contains a reference to a storage cell within
447 * the v8 engine which holds an object value and which is updated by 489 * the v8 engine which holds an object value and which is updated by
448 * the garbage collector whenever the object is moved. A new storage 490 * the garbage collector whenever the object is moved. A new storage
449 * cell can be created using the constructor or PersistentBase::Reset and 491 * cell can be created using the constructor or PersistentBase::Reset and
(...skipping 14 matching lines...) Expand all
464 template <class S> 506 template <class S>
465 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other); 507 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other);
466 508
467 /** 509 /**
468 * If non-empty, destroy the underlying storage cell 510 * If non-empty, destroy the underlying storage cell
469 * and create a new one with the contents of other if other is non empty 511 * and create a new one with the contents of other if other is non empty
470 */ 512 */
471 template <class S> 513 template <class S>
472 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); 514 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
473 515
474 V8_INLINE bool IsEmpty() const { return val_ == 0; } 516 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
517 V8_INLINE void Empty() { val_ = 0; }
475 518
476 template <class S> 519 template <class S>
477 V8_INLINE bool operator==(const PersistentBase<S>& that) const { 520 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
478 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 521 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
479 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 522 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
480 if (a == 0) return b == 0; 523 if (a == NULL) return b == NULL;
481 if (b == 0) return false; 524 if (b == NULL) return false;
482 return *a == *b; 525 return *a == *b;
483 } 526 }
484 527
485 template <class S> V8_INLINE bool operator==(const Handle<S>& that) const { 528 template <class S> V8_INLINE bool operator==(const Handle<S>& that) const {
486 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 529 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
487 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 530 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
488 if (a == 0) return b == 0; 531 if (a == NULL) return b == NULL;
489 if (b == 0) return false; 532 if (b == NULL) return false;
490 return *a == *b; 533 return *a == *b;
491 } 534 }
492 535
493 template <class S> 536 template <class S>
494 V8_INLINE bool operator!=(const PersistentBase<S>& that) const { 537 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
495 return !operator==(that); 538 return !operator==(that);
496 } 539 }
497 540
498 template <class S> V8_INLINE bool operator!=(const Handle<S>& that) const { 541 template <class S> V8_INLINE bool operator!=(const Handle<S>& that) const {
499 return !operator==(that); 542 return !operator==(that);
(...skipping 12 matching lines...) Expand all
512 typename WeakCallbackData<T, P>::Callback callback); 555 typename WeakCallbackData<T, P>::Callback callback);
513 556
514 template<typename S, typename P> 557 template<typename S, typename P>
515 V8_INLINE void SetWeak( 558 V8_INLINE void SetWeak(
516 P* parameter, 559 P* parameter,
517 typename WeakCallbackData<S, P>::Callback callback); 560 typename WeakCallbackData<S, P>::Callback callback);
518 561
519 // Phantom persistents work like weak persistents, except that the pointer to 562 // Phantom persistents work like weak persistents, except that the pointer to
520 // the object being collected is not available in the finalization callback. 563 // the object being collected is not available in the finalization callback.
521 // This enables the garbage collector to collect the object and any objects 564 // This enables the garbage collector to collect the object and any objects
522 // it references transitively in one GC cycle. 565 // it references transitively in one GC cycle. At the moment you can either
566 // specify a parameter for the callback or the location of two internal
567 // fields in the dying object.
523 template <typename P> 568 template <typename P>
524 V8_INLINE void SetPhantom(P* parameter, 569 V8_INLINE void SetPhantom(P* parameter,
525 typename WeakCallbackData<T, P>::Callback callback); 570 typename PhantomCallbackData<P>::Callback callback);
526 571
527 template <typename S, typename P> 572 template <typename P, typename Q>
528 V8_INLINE void SetPhantom(P* parameter, 573 V8_INLINE void SetPhantom(
529 typename WeakCallbackData<S, P>::Callback callback); 574 void (*callback)(const InternalFieldsCallbackData<P, Q>&),
575 int internal_field_index1, int internal_field_index2);
530 576
531 template<typename P> 577 template<typename P>
532 V8_INLINE P* ClearWeak(); 578 V8_INLINE P* ClearWeak();
533 579
534 // TODO(dcarney): remove this. 580 // TODO(dcarney): remove this.
535 V8_INLINE void ClearWeak() { ClearWeak<void>(); } 581 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
536 582
537 /** 583 /**
538 * Marks the reference to this object independent. Garbage collector is free 584 * Marks the reference to this object independent. Garbage collector is free
539 * to ignore any object groups containing this object. Weak callback for an 585 * to ignore any object groups containing this object. Weak callback for an
(...skipping 1945 matching lines...) Expand 10 before | Expand all | Expand 10 after
2485 Local<String> ObjectProtoToString(); 2531 Local<String> ObjectProtoToString();
2486 2532
2487 /** 2533 /**
2488 * Returns the name of the function invoked as a constructor for this object. 2534 * Returns the name of the function invoked as a constructor for this object.
2489 */ 2535 */
2490 Local<String> GetConstructorName(); 2536 Local<String> GetConstructorName();
2491 2537
2492 /** Gets the number of internal fields for this Object. */ 2538 /** Gets the number of internal fields for this Object. */
2493 int InternalFieldCount(); 2539 int InternalFieldCount();
2494 2540
2541 static const int kNoInternalFieldIndex = -1;
2542
2495 /** Same as above, but works for Persistents */ 2543 /** Same as above, but works for Persistents */
2496 V8_INLINE static int InternalFieldCount( 2544 V8_INLINE static int InternalFieldCount(
2497 const PersistentBase<Object>& object) { 2545 const PersistentBase<Object>& object) {
2498 return object.val_->InternalFieldCount(); 2546 return object.val_->InternalFieldCount();
2499 } 2547 }
2500 2548
2501 /** Gets the value from an internal field. */ 2549 /** Gets the value from an internal field. */
2502 V8_INLINE Local<Value> GetInternalField(int index); 2550 V8_INLINE Local<Value> GetInternalField(int index);
2503 2551
2504 /** Sets the value in an internal field. */ 2552 /** Sets the value in an internal field. */
(...skipping 3103 matching lines...) Expand 10 before | Expand all | Expand 10 after
5608 V8(); 5656 V8();
5609 5657
5610 enum WeakHandleType { PhantomHandle, NonphantomHandle }; 5658 enum WeakHandleType { PhantomHandle, NonphantomHandle };
5611 5659
5612 static internal::Object** GlobalizeReference(internal::Isolate* isolate, 5660 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
5613 internal::Object** handle); 5661 internal::Object** handle);
5614 static internal::Object** CopyPersistent(internal::Object** handle); 5662 static internal::Object** CopyPersistent(internal::Object** handle);
5615 static void DisposeGlobal(internal::Object** global_handle); 5663 static void DisposeGlobal(internal::Object** global_handle);
5616 typedef WeakCallbackData<Value, void>::Callback WeakCallback; 5664 typedef WeakCallbackData<Value, void>::Callback WeakCallback;
5617 static void MakeWeak(internal::Object** global_handle, void* data, 5665 static void MakeWeak(internal::Object** global_handle, void* data,
5618 WeakCallback weak_callback, WeakHandleType phantom); 5666 WeakCallback weak_callback);
5667 static void MakePhantom(internal::Object** global_handle, void* data,
5668 PhantomCallbackData<void>::Callback weak_callback);
5669 static void MakePhantom(
5670 internal::Object** global_handle,
5671 InternalFieldsCallbackData<void, void>::Callback weak_callback,
5672 int internal_field_index1,
5673 int internal_field_index2 = Object::kNoInternalFieldIndex);
5619 static void* ClearWeak(internal::Object** global_handle); 5674 static void* ClearWeak(internal::Object** global_handle);
5620 static void Eternalize(Isolate* isolate, 5675 static void Eternalize(Isolate* isolate,
5621 Value* handle, 5676 Value* handle,
5622 int* index); 5677 int* index);
5623 static Local<Value> GetEternal(Isolate* isolate, int index); 5678 static Local<Value> GetEternal(Isolate* isolate, int index);
5624 5679
5625 template <class T> friend class Handle; 5680 template <class T> friend class Handle;
5626 template <class T> friend class Local; 5681 template <class T> friend class Local;
5627 template <class T> friend class Eternal; 5682 template <class T> friend class Eternal;
5628 template <class T> friend class PersistentBase; 5683 template <class T> friend class PersistentBase;
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
6217 static const int kTrueValueRootIndex = 8; 6272 static const int kTrueValueRootIndex = 8;
6218 static const int kFalseValueRootIndex = 9; 6273 static const int kFalseValueRootIndex = 9;
6219 static const int kEmptyStringRootIndex = 154; 6274 static const int kEmptyStringRootIndex = 154;
6220 6275
6221 // The external allocation limit should be below 256 MB on all architectures 6276 // The external allocation limit should be below 256 MB on all architectures
6222 // to avoid that resource-constrained embedders run low on memory. 6277 // to avoid that resource-constrained embedders run low on memory.
6223 static const int kExternalAllocationLimit = 192 * 1024 * 1024; 6278 static const int kExternalAllocationLimit = 192 * 1024 * 1024;
6224 6279
6225 static const int kNodeClassIdOffset = 1 * kApiPointerSize; 6280 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
6226 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; 6281 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
6227 static const int kNodeStateMask = 0xf; 6282 static const int kNodeStateMask = 0x7;
6228 static const int kNodeStateIsWeakValue = 2; 6283 static const int kNodeStateIsWeakValue = 2;
6229 static const int kNodeStateIsPendingValue = 3; 6284 static const int kNodeStateIsPendingValue = 3;
6230 static const int kNodeStateIsNearDeathValue = 4; 6285 static const int kNodeStateIsNearDeathValue = 4;
6231 static const int kNodeIsIndependentShift = 4; 6286 static const int kNodeIsIndependentShift = 3;
6232 static const int kNodeIsPartiallyDependentShift = 5; 6287 static const int kNodeIsPartiallyDependentShift = 4;
6233 6288
6234 static const int kJSObjectType = 0xbd; 6289 static const int kJSObjectType = 0xbd;
6235 static const int kFirstNonstringType = 0x80; 6290 static const int kFirstNonstringType = 0x80;
6236 static const int kOddballType = 0x83; 6291 static const int kOddballType = 0x83;
6237 static const int kForeignType = 0x88; 6292 static const int kForeignType = 0x88;
6238 6293
6239 static const int kUndefinedOddballKind = 5; 6294 static const int kUndefinedOddballKind = 5;
6240 static const int kNullOddballKind = 3; 6295 static const int kNullOddballKind = 3;
6241 6296
6242 static const uint32_t kNumIsolateDataSlots = 4; 6297 static const uint32_t kNumIsolateDataSlots = 4;
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
6480 6535
6481 6536
6482 template <class T> 6537 template <class T>
6483 template <typename S, typename P> 6538 template <typename S, typename P>
6484 void PersistentBase<T>::SetWeak( 6539 void PersistentBase<T>::SetWeak(
6485 P* parameter, 6540 P* parameter,
6486 typename WeakCallbackData<S, P>::Callback callback) { 6541 typename WeakCallbackData<S, P>::Callback callback) {
6487 TYPE_CHECK(S, T); 6542 TYPE_CHECK(S, T);
6488 typedef typename WeakCallbackData<Value, void>::Callback Callback; 6543 typedef typename WeakCallbackData<Value, void>::Callback Callback;
6489 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, 6544 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
6490 reinterpret_cast<Callback>(callback), V8::NonphantomHandle); 6545 reinterpret_cast<Callback>(callback));
6491 } 6546 }
6492 6547
6493 6548
6494 template <class T> 6549 template <class T>
6495 template <typename P> 6550 template <typename P>
6496 void PersistentBase<T>::SetWeak( 6551 void PersistentBase<T>::SetWeak(
6497 P* parameter, 6552 P* parameter,
6498 typename WeakCallbackData<T, P>::Callback callback) { 6553 typename WeakCallbackData<T, P>::Callback callback) {
6499 SetWeak<T, P>(parameter, callback); 6554 SetWeak<T, P>(parameter, callback);
6500 } 6555 }
6501 6556
6502 6557
6503 template <class T> 6558 template <class T>
6504 template <typename S, typename P>
6505 void PersistentBase<T>::SetPhantom(
6506 P* parameter, typename WeakCallbackData<S, P>::Callback callback) {
6507 TYPE_CHECK(S, T);
6508 typedef typename WeakCallbackData<Value, void>::Callback Callback;
6509 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
6510 reinterpret_cast<Callback>(callback), V8::PhantomHandle);
6511 }
6512
6513
6514 template <class T>
6515 template <typename P> 6559 template <typename P>
6516 void PersistentBase<T>::SetPhantom( 6560 void PersistentBase<T>::SetPhantom(
6517 P* parameter, typename WeakCallbackData<T, P>::Callback callback) { 6561 P* parameter, typename PhantomCallbackData<P>::Callback callback) {
6518 SetPhantom<T, P>(parameter, callback); 6562 typedef typename PhantomCallbackData<void>::Callback Callback;
6563 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter,
6564 reinterpret_cast<Callback>(callback));
6519 } 6565 }
6520 6566
6521 6567
6568 template <class T>
6569 template <typename U, typename V>
6570 void PersistentBase<T>::SetPhantom(
6571 void (*callback)(const InternalFieldsCallbackData<U, V>&),
6572 int internal_field_index1, int internal_field_index2) {
6573 typedef typename InternalFieldsCallbackData<void, void>::Callback Callback;
6574 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_),
6575 reinterpret_cast<Callback>(callback), internal_field_index1,
6576 internal_field_index2);
6577 }
6578
6579
6522 template <class T> 6580 template <class T>
6523 template <typename P> 6581 template <typename P>
6524 P* PersistentBase<T>::ClearWeak() { 6582 P* PersistentBase<T>::ClearWeak() {
6525 return reinterpret_cast<P*>( 6583 return reinterpret_cast<P*>(
6526 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); 6584 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
6527 } 6585 }
6528 6586
6529 6587
6530 template <class T> 6588 template <class T>
6531 void PersistentBase<T>::MarkIndependent() { 6589 void PersistentBase<T>::MarkIndependent() {
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
7505 */ 7563 */
7506 7564
7507 7565
7508 } // namespace v8 7566 } // namespace v8
7509 7567
7510 7568
7511 #undef TYPE_CHECK 7569 #undef TYPE_CHECK
7512 7570
7513 7571
7514 #endif // V8_H_ 7572 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | src/api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698