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

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: Queue up phantom callbacks during GC to dispatch later 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') | 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 V8_INLINE Local<T> Get(Isolate* isolate); 411 V8_INLINE Local<T> Get(Isolate* isolate);
412 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; } 412 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
413 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle); 413 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
414 414
415 private: 415 private:
416 static const int kInitialValue = -1; 416 static const int kInitialValue = -1;
417 int index_; 417 int index_;
418 }; 418 };
419 419
420 420
421 template<class T>
422 class CallbackData {
rmcilroy 2014/12/03 11:56:35 nit - BaseCallbackData or maybe even BaseWeakCallb
Erik Corry 2014/12/15 15:12:41 Done.
423 public:
424 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
425 V8_INLINE T* GetParameter() const { return parameter_; }
426
427 protected:
428 CallbackData(Isolate* isolate, T* parameter)
429 : isolate_(isolate), parameter_(parameter) { }
430
431 private:
432 Isolate* isolate_;
433 T* parameter_;
434 };
435
rmcilroy 2014/12/03 11:56:35 nit - two newlines between classes in V8
Erik Corry 2014/12/15 15:12:41 Done.
421 template<class T, class P> 436 template<class T, class P>
422 class WeakCallbackData { 437 class WeakCallbackData : public CallbackData<P> {
423 public: 438 public:
424 typedef void (*Callback)(const WeakCallbackData<T, P>& data); 439 typedef void (*Callback)(const WeakCallbackData<T, P>& data);
425 440
426 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
427 V8_INLINE Local<T> GetValue() const { return handle_; } 441 V8_INLINE Local<T> GetValue() const { return handle_; }
428 V8_INLINE P* GetParameter() const { return parameter_; }
429 442
430 private: 443 private:
431 friend class internal::GlobalHandles; 444 friend class internal::GlobalHandles;
432 WeakCallbackData(Isolate* isolate, Local<T> handle, P* parameter) 445 WeakCallbackData(Isolate* isolate, Local<T> handle, P* parameter)
433 : isolate_(isolate), handle_(handle), parameter_(parameter) { } 446 : CallbackData<P>(isolate, parameter), handle_(handle) { }
434 Isolate* isolate_;
435 Local<T> handle_; 447 Local<T> handle_;
436 P* parameter_;
437 }; 448 };
438 449
439 450
451 template<typename T>
452 class PhantomCallbackData : public CallbackData<T> {
453 public:
454 typedef void (*Callback)(const PhantomCallbackData<T>& data);
455
456 V8_INLINE void* GetInternalField1() const { return internal_field1_; }
457 V8_INLINE void* GetInternalField2() const { return internal_field2_; }
458
459 private:
460 friend class internal::GlobalHandles;
461 PhantomCallbackData(Isolate* isolate, void* internalField1,
462 void* internalField2)
463 : CallbackData<T>(isolate, NULL),
rmcilroy 2014/12/03 11:56:35 Do we want to expose NULL parameters to PhantomCal
Erik Corry 2014/12/15 15:12:41 Fixed to pass a type that has only valid fields to
464 internal_field1_(internalField1),
465 internal_field2_(internalField2) { }
466 PhantomCallbackData(Isolate* isolate, T* parameter)
467 : CallbackData<T>(isolate, parameter),
468 internal_field1_(NULL),
469 internal_field2_(NULL) { }
470 void* internal_field1_;
471 void* internal_field2_;
472 };
473
474
440 /** 475 /**
441 * An object reference that is independent of any handle scope. Where 476 * 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 477 * 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 478 * allocated, a PersistentBase handle remains valid until it is explicitly
444 * disposed. 479 * disposed.
445 * 480 *
446 * A persistent handle contains a reference to a storage cell within 481 * 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 482 * 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 483 * the garbage collector whenever the object is moved. A new storage
449 * cell can be created using the constructor or PersistentBase::Reset and 484 * cell can be created using the constructor or PersistentBase::Reset and
(...skipping 14 matching lines...) Expand all
464 template <class S> 499 template <class S>
465 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other); 500 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other);
466 501
467 /** 502 /**
468 * If non-empty, destroy the underlying storage cell 503 * If non-empty, destroy the underlying storage cell
469 * and create a new one with the contents of other if other is non empty 504 * and create a new one with the contents of other if other is non empty
470 */ 505 */
471 template <class S> 506 template <class S>
472 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); 507 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
473 508
474 V8_INLINE bool IsEmpty() const { return val_ == 0; } 509 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
510 V8_INLINE void Empty() { val_ = NULL; }
jochen (gone - plz use gerrit) 2014/12/02 10:25:59 what do you need Empty() for?
Erik Corry 2014/12/02 10:40:48 It seems I don't need it any more. Removed.
Erik Corry 2014/12/15 15:12:41 Actually I need this for the corresponding Blink c
475 511
476 template <class S> 512 template <class S>
477 V8_INLINE bool operator==(const PersistentBase<S>& that) const { 513 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
478 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 514 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
479 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 515 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
480 if (a == 0) return b == 0; 516 if (a == NULL) return b == NULL;
481 if (b == 0) return false; 517 if (b == NULL) return false;
482 return *a == *b; 518 return *a == *b;
483 } 519 }
484 520
485 template <class S> V8_INLINE bool operator==(const Handle<S>& that) const { 521 template <class S> V8_INLINE bool operator==(const Handle<S>& that) const {
486 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 522 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
487 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 523 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
488 if (a == 0) return b == 0; 524 if (a == NULL) return b == NULL;
489 if (b == 0) return false; 525 if (b == NULL) return false;
490 return *a == *b; 526 return *a == *b;
491 } 527 }
492 528
493 template <class S> 529 template <class S>
494 V8_INLINE bool operator!=(const PersistentBase<S>& that) const { 530 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
495 return !operator==(that); 531 return !operator==(that);
496 } 532 }
497 533
498 template <class S> V8_INLINE bool operator!=(const Handle<S>& that) const { 534 template <class S> V8_INLINE bool operator!=(const Handle<S>& that) const {
499 return !operator==(that); 535 return !operator==(that);
(...skipping 14 matching lines...) Expand all
514 template<typename S, typename P> 550 template<typename S, typename P>
515 V8_INLINE void SetWeak( 551 V8_INLINE void SetWeak(
516 P* parameter, 552 P* parameter,
517 typename WeakCallbackData<S, P>::Callback callback); 553 typename WeakCallbackData<S, P>::Callback callback);
518 554
519 // Phantom persistents work like weak persistents, except that the pointer to 555 // Phantom persistents work like weak persistents, except that the pointer to
520 // the object being collected is not available in the finalization callback. 556 // the object being collected is not available in the finalization callback.
521 // This enables the garbage collector to collect the object and any objects 557 // This enables the garbage collector to collect the object and any objects
522 // it references transitively in one GC cycle. 558 // it references transitively in one GC cycle.
523 template <typename P> 559 template <typename P>
524 V8_INLINE void SetPhantom(P* parameter, 560 V8_INLINE void SetPhantom(
525 typename WeakCallbackData<T, P>::Callback callback); 561 P* parameter, typename PhantomCallbackData<P>::Callback callback);
526 562
527 template <typename S, typename P> 563 V8_INLINE void SetPhantom(
528 V8_INLINE void SetPhantom(P* parameter, 564 typename PhantomCallbackData<void>::Callback callback,
529 typename WeakCallbackData<S, P>::Callback callback); 565 int internalFieldOffset1, int internalFieldOffset2);
jochen (gone - plz use gerrit) 2014/12/02 10:25:59 internal_field_index1
Erik Corry 2014/12/02 10:40:48 Done.
530 566
531 template<typename P> 567 template<typename P>
532 V8_INLINE P* ClearWeak(); 568 V8_INLINE P* ClearWeak();
533 569
534 // TODO(dcarney): remove this. 570 // TODO(dcarney): remove this.
535 V8_INLINE void ClearWeak() { ClearWeak<void>(); } 571 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
536 572
537 /** 573 /**
538 * Marks the reference to this object independent. Garbage collector is free 574 * Marks the reference to this object independent. Garbage collector is free
539 * to ignore any object groups containing this object. Weak callback for an 575 * to ignore any object groups containing this object. Weak callback for an
(...skipping 1936 matching lines...) Expand 10 before | Expand all | Expand 10 after
2476 Local<String> ObjectProtoToString(); 2512 Local<String> ObjectProtoToString();
2477 2513
2478 /** 2514 /**
2479 * Returns the name of the function invoked as a constructor for this object. 2515 * Returns the name of the function invoked as a constructor for this object.
2480 */ 2516 */
2481 Local<String> GetConstructorName(); 2517 Local<String> GetConstructorName();
2482 2518
2483 /** Gets the number of internal fields for this Object. */ 2519 /** Gets the number of internal fields for this Object. */
2484 int InternalFieldCount(); 2520 int InternalFieldCount();
2485 2521
2522 static const int kNoInternalFieldIndex = -1;
2523
2486 /** Same as above, but works for Persistents */ 2524 /** Same as above, but works for Persistents */
2487 V8_INLINE static int InternalFieldCount( 2525 V8_INLINE static int InternalFieldCount(
2488 const PersistentBase<Object>& object) { 2526 const PersistentBase<Object>& object) {
2489 return object.val_->InternalFieldCount(); 2527 return object.val_->InternalFieldCount();
2490 } 2528 }
2491 2529
2492 /** Gets the value from an internal field. */ 2530 /** Gets the value from an internal field. */
2493 V8_INLINE Local<Value> GetInternalField(int index); 2531 V8_INLINE Local<Value> GetInternalField(int index);
2494 2532
2495 /** Sets the value in an internal field. */ 2533 /** Sets the value in an internal field. */
(...skipping 2984 matching lines...) Expand 10 before | Expand all | Expand 10 after
5480 private: 5518 private:
5481 V8(); 5519 V8();
5482 5520
5483 enum WeakHandleType { PhantomHandle, NonphantomHandle }; 5521 enum WeakHandleType { PhantomHandle, NonphantomHandle };
5484 5522
5485 static internal::Object** GlobalizeReference(internal::Isolate* isolate, 5523 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
5486 internal::Object** handle); 5524 internal::Object** handle);
5487 static internal::Object** CopyPersistent(internal::Object** handle); 5525 static internal::Object** CopyPersistent(internal::Object** handle);
5488 static void DisposeGlobal(internal::Object** global_handle); 5526 static void DisposeGlobal(internal::Object** global_handle);
5489 typedef WeakCallbackData<Value, void>::Callback WeakCallback; 5527 typedef WeakCallbackData<Value, void>::Callback WeakCallback;
5528 typedef PhantomCallbackData<void>::Callback PhantomCallback;
5490 static void MakeWeak(internal::Object** global_handle, void* data, 5529 static void MakeWeak(internal::Object** global_handle, void* data,
5491 WeakCallback weak_callback, WeakHandleType phantom); 5530 WeakCallback weak_callback);
5531 static void MakePhantom(
5532 internal::Object** global_handle, void* data,
5533 PhantomCallback weak_callback,
5534 int internal_field_index1 = Object::kNoInternalFieldIndex,
5535 int internal_field_index2 = Object::kNoInternalFieldIndex);
5492 static void* ClearWeak(internal::Object** global_handle); 5536 static void* ClearWeak(internal::Object** global_handle);
5493 static void Eternalize(Isolate* isolate, 5537 static void Eternalize(Isolate* isolate,
5494 Value* handle, 5538 Value* handle,
5495 int* index); 5539 int* index);
5496 static Local<Value> GetEternal(Isolate* isolate, int index); 5540 static Local<Value> GetEternal(Isolate* isolate, int index);
5497 5541
5498 template <class T> friend class Handle; 5542 template <class T> friend class Handle;
5499 template <class T> friend class Local; 5543 template <class T> friend class Local;
5500 template <class T> friend class Eternal; 5544 template <class T> friend class Eternal;
5501 template <class T> friend class PersistentBase; 5545 template <class T> friend class PersistentBase;
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
6090 static const int kTrueValueRootIndex = 8; 6134 static const int kTrueValueRootIndex = 8;
6091 static const int kFalseValueRootIndex = 9; 6135 static const int kFalseValueRootIndex = 9;
6092 static const int kEmptyStringRootIndex = 154; 6136 static const int kEmptyStringRootIndex = 154;
6093 6137
6094 // The external allocation limit should be below 256 MB on all architectures 6138 // The external allocation limit should be below 256 MB on all architectures
6095 // to avoid that resource-constrained embedders run low on memory. 6139 // to avoid that resource-constrained embedders run low on memory.
6096 static const int kExternalAllocationLimit = 192 * 1024 * 1024; 6140 static const int kExternalAllocationLimit = 192 * 1024 * 1024;
6097 6141
6098 static const int kNodeClassIdOffset = 1 * kApiPointerSize; 6142 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
6099 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; 6143 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
6100 static const int kNodeStateMask = 0xf; 6144 static const int kNodeStateMask = 0x7;
6101 static const int kNodeStateIsWeakValue = 2; 6145 static const int kNodeStateIsWeakValue = 2;
6102 static const int kNodeStateIsPendingValue = 3; 6146 static const int kNodeStateIsPendingValue = 3;
6103 static const int kNodeStateIsNearDeathValue = 4; 6147 static const int kNodeStateIsNearDeathValue = 4;
6104 static const int kNodeIsIndependentShift = 4; 6148 static const int kNodeIsIndependentShift = 3;
6105 static const int kNodeIsPartiallyDependentShift = 5; 6149 static const int kNodeIsPartiallyDependentShift = 4;
6106 6150
6107 static const int kJSObjectType = 0xbd; 6151 static const int kJSObjectType = 0xbd;
6108 static const int kFirstNonstringType = 0x80; 6152 static const int kFirstNonstringType = 0x80;
6109 static const int kOddballType = 0x83; 6153 static const int kOddballType = 0x83;
6110 static const int kForeignType = 0x88; 6154 static const int kForeignType = 0x88;
6111 6155
6112 static const int kUndefinedOddballKind = 5; 6156 static const int kUndefinedOddballKind = 5;
6113 static const int kNullOddballKind = 3; 6157 static const int kNullOddballKind = 3;
6114 6158
6115 static const uint32_t kNumIsolateDataSlots = 4; 6159 static const uint32_t kNumIsolateDataSlots = 4;
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
6353 6397
6354 6398
6355 template <class T> 6399 template <class T>
6356 template <typename S, typename P> 6400 template <typename S, typename P>
6357 void PersistentBase<T>::SetWeak( 6401 void PersistentBase<T>::SetWeak(
6358 P* parameter, 6402 P* parameter,
6359 typename WeakCallbackData<S, P>::Callback callback) { 6403 typename WeakCallbackData<S, P>::Callback callback) {
6360 TYPE_CHECK(S, T); 6404 TYPE_CHECK(S, T);
6361 typedef typename WeakCallbackData<Value, void>::Callback Callback; 6405 typedef typename WeakCallbackData<Value, void>::Callback Callback;
6362 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, 6406 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
6363 reinterpret_cast<Callback>(callback), V8::NonphantomHandle); 6407 reinterpret_cast<Callback>(callback));
6364 } 6408 }
6365 6409
6366 6410
6367 template <class T> 6411 template <class T>
6368 template <typename P> 6412 template <typename P>
6369 void PersistentBase<T>::SetWeak( 6413 void PersistentBase<T>::SetWeak(
6370 P* parameter, 6414 P* parameter,
6371 typename WeakCallbackData<T, P>::Callback callback) { 6415 typename WeakCallbackData<T, P>::Callback callback) {
6372 SetWeak<T, P>(parameter, callback); 6416 SetWeak<T, P>(parameter, callback);
6373 } 6417 }
6374 6418
6375 6419
6376 template <class T> 6420 template <class T>
6377 template <typename S, typename P>
6378 void PersistentBase<T>::SetPhantom(
6379 P* parameter, typename WeakCallbackData<S, P>::Callback callback) {
6380 TYPE_CHECK(S, T);
6381 typedef typename WeakCallbackData<Value, void>::Callback Callback;
6382 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
6383 reinterpret_cast<Callback>(callback), V8::PhantomHandle);
6384 }
6385
6386
6387 template <class T>
6388 template <typename P> 6421 template <typename P>
6389 void PersistentBase<T>::SetPhantom( 6422 void PersistentBase<T>::SetPhantom(
6390 P* parameter, typename WeakCallbackData<T, P>::Callback callback) { 6423 P* parameter, typename PhantomCallbackData<P>::Callback callback) {
6391 SetPhantom<T, P>(parameter, callback); 6424 typedef typename PhantomCallbackData<void>::Callback Callback;
6425 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter,
6426 reinterpret_cast<Callback>(callback),
6427 Object::kNoInternalFieldIndex, Object::kNoInternalFieldIndex);
6392 } 6428 }
6393 6429
6394 6430
6431 template <class T>
6432 void PersistentBase<T>::SetPhantom(
6433 typename PhantomCallbackData<void>::Callback callback,
6434 int internal_field_index1, int internal_field_index2) {
6435 typedef typename PhantomCallbackData<void>::Callback Callback;
6436 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), 0,
6437 reinterpret_cast<Callback>(callback), internal_field_index1,
6438 internal_field_index2);
6439 }
6440
6441
6395 template <class T> 6442 template <class T>
6396 template <typename P> 6443 template <typename P>
6397 P* PersistentBase<T>::ClearWeak() { 6444 P* PersistentBase<T>::ClearWeak() {
6398 return reinterpret_cast<P*>( 6445 return reinterpret_cast<P*>(
6399 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); 6446 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
6400 } 6447 }
6401 6448
6402 6449
6403 template <class T> 6450 template <class T>
6404 void PersistentBase<T>::MarkIndependent() { 6451 void PersistentBase<T>::MarkIndependent() {
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
7378 */ 7425 */
7379 7426
7380 7427
7381 } // namespace v8 7428 } // namespace v8
7382 7429
7383 7430
7384 #undef TYPE_CHECK 7431 #undef TYPE_CHECK
7385 7432
7386 7433
7387 #endif // V8_H_ 7434 #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