OLD | NEW |
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 Loading... |
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 { |
| 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 |
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, void* internalFiel
d2) |
| 462 : CallbackData<T>(isolate, NULL), |
| 463 internal_field1_(internalField1), |
| 464 internal_field2_(internalField2) { } |
| 465 PhantomCallbackData(Isolate* isolate, T* parameter) |
| 466 : CallbackData<T>(isolate, parameter), |
| 467 internal_field1_(NULL), |
| 468 internal_field2_(NULL) { } |
| 469 void* internal_field1_; |
| 470 void* internal_field2_; |
| 471 }; |
| 472 |
| 473 |
440 /** | 474 /** |
441 * An object reference that is independent of any handle scope. Where | 475 * 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 | 476 * 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 | 477 * allocated, a PersistentBase handle remains valid until it is explicitly |
444 * disposed. | 478 * disposed. |
445 * | 479 * |
446 * A persistent handle contains a reference to a storage cell within | 480 * 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 | 481 * 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 | 482 * the garbage collector whenever the object is moved. A new storage |
449 * cell can be created using the constructor or PersistentBase::Reset and | 483 * cell can be created using the constructor or PersistentBase::Reset and |
(...skipping 14 matching lines...) Expand all Loading... |
464 template <class S> | 498 template <class S> |
465 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other); | 499 V8_INLINE void Reset(Isolate* isolate, const Handle<S>& other); |
466 | 500 |
467 /** | 501 /** |
468 * If non-empty, destroy the underlying storage cell | 502 * If non-empty, destroy the underlying storage cell |
469 * and create a new one with the contents of other if other is non empty | 503 * and create a new one with the contents of other if other is non empty |
470 */ | 504 */ |
471 template <class S> | 505 template <class S> |
472 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); | 506 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); |
473 | 507 |
474 V8_INLINE bool IsEmpty() const { return val_ == 0; } | 508 V8_INLINE bool IsEmpty() const { return val_ == NULL; } |
| 509 V8_INLINE void Empty() { val_ = NULL; } |
475 | 510 |
476 template <class S> | 511 template <class S> |
477 V8_INLINE bool operator==(const PersistentBase<S>& that) const { | 512 V8_INLINE bool operator==(const PersistentBase<S>& that) const { |
478 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); | 513 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
479 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); | 514 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
480 if (a == 0) return b == 0; | 515 if (a == 0) return b == 0; |
481 if (b == 0) return false; | 516 if (b == 0) return false; |
482 return *a == *b; | 517 return *a == *b; |
483 } | 518 } |
484 | 519 |
(...skipping 30 matching lines...) Expand all Loading... |
515 V8_INLINE void SetWeak( | 550 V8_INLINE void SetWeak( |
516 P* parameter, | 551 P* parameter, |
517 typename WeakCallbackData<S, P>::Callback callback); | 552 typename WeakCallbackData<S, P>::Callback callback); |
518 | 553 |
519 // Phantom persistents work like weak persistents, except that the pointer to | 554 // Phantom persistents work like weak persistents, except that the pointer to |
520 // the object being collected is not available in the finalization callback. | 555 // the object being collected is not available in the finalization callback. |
521 // This enables the garbage collector to collect the object and any objects | 556 // This enables the garbage collector to collect the object and any objects |
522 // it references transitively in one GC cycle. | 557 // it references transitively in one GC cycle. |
523 template <typename P> | 558 template <typename P> |
524 V8_INLINE void SetPhantom(P* parameter, | 559 V8_INLINE void SetPhantom(P* parameter, |
525 typename WeakCallbackData<T, P>::Callback callback); | 560 typename PhantomCallbackData<P>::Callback callback); |
526 | 561 |
527 template <typename S, typename P> | 562 V8_INLINE void SetPhantom(typename PhantomCallbackData<void>::Callback callbac
k, |
528 V8_INLINE void SetPhantom(P* parameter, | 563 int internalFieldOffset1, int internalFieldOffset2); |
529 typename WeakCallbackData<S, P>::Callback callback); | |
530 | 564 |
531 template<typename P> | 565 template<typename P> |
532 V8_INLINE P* ClearWeak(); | 566 V8_INLINE P* ClearWeak(); |
533 | 567 |
534 // TODO(dcarney): remove this. | 568 // TODO(dcarney): remove this. |
535 V8_INLINE void ClearWeak() { ClearWeak<void>(); } | 569 V8_INLINE void ClearWeak() { ClearWeak<void>(); } |
536 | 570 |
537 /** | 571 /** |
538 * Marks the reference to this object independent. Garbage collector is free | 572 * Marks the reference to this object independent. Garbage collector is free |
539 * 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 1936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2476 Local<String> ObjectProtoToString(); | 2510 Local<String> ObjectProtoToString(); |
2477 | 2511 |
2478 /** | 2512 /** |
2479 * Returns the name of the function invoked as a constructor for this object. | 2513 * Returns the name of the function invoked as a constructor for this object. |
2480 */ | 2514 */ |
2481 Local<String> GetConstructorName(); | 2515 Local<String> GetConstructorName(); |
2482 | 2516 |
2483 /** Gets the number of internal fields for this Object. */ | 2517 /** Gets the number of internal fields for this Object. */ |
2484 int InternalFieldCount(); | 2518 int InternalFieldCount(); |
2485 | 2519 |
| 2520 static const int kNoInternalFieldIndex = -1; |
| 2521 |
2486 /** Same as above, but works for Persistents */ | 2522 /** Same as above, but works for Persistents */ |
2487 V8_INLINE static int InternalFieldCount( | 2523 V8_INLINE static int InternalFieldCount( |
2488 const PersistentBase<Object>& object) { | 2524 const PersistentBase<Object>& object) { |
2489 return object.val_->InternalFieldCount(); | 2525 return object.val_->InternalFieldCount(); |
2490 } | 2526 } |
2491 | 2527 |
2492 /** Gets the value from an internal field. */ | 2528 /** Gets the value from an internal field. */ |
2493 V8_INLINE Local<Value> GetInternalField(int index); | 2529 V8_INLINE Local<Value> GetInternalField(int index); |
2494 | 2530 |
2495 /** Sets the value in an internal field. */ | 2531 /** Sets the value in an internal field. */ |
(...skipping 2984 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5480 private: | 5516 private: |
5481 V8(); | 5517 V8(); |
5482 | 5518 |
5483 enum WeakHandleType { PhantomHandle, NonphantomHandle }; | 5519 enum WeakHandleType { PhantomHandle, NonphantomHandle }; |
5484 | 5520 |
5485 static internal::Object** GlobalizeReference(internal::Isolate* isolate, | 5521 static internal::Object** GlobalizeReference(internal::Isolate* isolate, |
5486 internal::Object** handle); | 5522 internal::Object** handle); |
5487 static internal::Object** CopyPersistent(internal::Object** handle); | 5523 static internal::Object** CopyPersistent(internal::Object** handle); |
5488 static void DisposeGlobal(internal::Object** global_handle); | 5524 static void DisposeGlobal(internal::Object** global_handle); |
5489 typedef WeakCallbackData<Value, void>::Callback WeakCallback; | 5525 typedef WeakCallbackData<Value, void>::Callback WeakCallback; |
| 5526 typedef PhantomCallbackData<void>::Callback PhantomCallback; |
5490 static void MakeWeak(internal::Object** global_handle, void* data, | 5527 static void MakeWeak(internal::Object** global_handle, void* data, |
5491 WeakCallback weak_callback, WeakHandleType phantom); | 5528 WeakCallback weak_callback); |
| 5529 static void MakePhantom( |
| 5530 internal::Object** global_handle, void* data, |
| 5531 PhantomCallback weak_callback, |
| 5532 int internal_field_index1 = Object::kNoInternalFieldIndex, |
| 5533 int internal_field_index2 = Object::kNoInternalFieldIndex); |
5492 static void* ClearWeak(internal::Object** global_handle); | 5534 static void* ClearWeak(internal::Object** global_handle); |
5493 static void Eternalize(Isolate* isolate, | 5535 static void Eternalize(Isolate* isolate, |
5494 Value* handle, | 5536 Value* handle, |
5495 int* index); | 5537 int* index); |
5496 static Local<Value> GetEternal(Isolate* isolate, int index); | 5538 static Local<Value> GetEternal(Isolate* isolate, int index); |
5497 | 5539 |
5498 template <class T> friend class Handle; | 5540 template <class T> friend class Handle; |
5499 template <class T> friend class Local; | 5541 template <class T> friend class Local; |
5500 template <class T> friend class Eternal; | 5542 template <class T> friend class Eternal; |
5501 template <class T> friend class PersistentBase; | 5543 template <class T> friend class PersistentBase; |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6090 static const int kTrueValueRootIndex = 8; | 6132 static const int kTrueValueRootIndex = 8; |
6091 static const int kFalseValueRootIndex = 9; | 6133 static const int kFalseValueRootIndex = 9; |
6092 static const int kEmptyStringRootIndex = 154; | 6134 static const int kEmptyStringRootIndex = 154; |
6093 | 6135 |
6094 // The external allocation limit should be below 256 MB on all architectures | 6136 // The external allocation limit should be below 256 MB on all architectures |
6095 // to avoid that resource-constrained embedders run low on memory. | 6137 // to avoid that resource-constrained embedders run low on memory. |
6096 static const int kExternalAllocationLimit = 192 * 1024 * 1024; | 6138 static const int kExternalAllocationLimit = 192 * 1024 * 1024; |
6097 | 6139 |
6098 static const int kNodeClassIdOffset = 1 * kApiPointerSize; | 6140 static const int kNodeClassIdOffset = 1 * kApiPointerSize; |
6099 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; | 6141 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; |
6100 static const int kNodeStateMask = 0xf; | 6142 static const int kNodeStateMask = 0x7; |
6101 static const int kNodeStateIsWeakValue = 2; | 6143 static const int kNodeStateIsWeakValue = 2; |
6102 static const int kNodeStateIsPendingValue = 3; | 6144 static const int kNodeStateIsPendingValue = 3; |
6103 static const int kNodeStateIsNearDeathValue = 4; | 6145 static const int kNodeStateIsNearDeathValue = 4; |
6104 static const int kNodeIsIndependentShift = 4; | 6146 static const int kNodeIsIndependentShift = 3; |
6105 static const int kNodeIsPartiallyDependentShift = 5; | 6147 static const int kNodeIsPartiallyDependentShift = 4; |
6106 | 6148 |
6107 static const int kJSObjectType = 0xbd; | 6149 static const int kJSObjectType = 0xbd; |
6108 static const int kFirstNonstringType = 0x80; | 6150 static const int kFirstNonstringType = 0x80; |
6109 static const int kOddballType = 0x83; | 6151 static const int kOddballType = 0x83; |
6110 static const int kForeignType = 0x88; | 6152 static const int kForeignType = 0x88; |
6111 | 6153 |
6112 static const int kUndefinedOddballKind = 5; | 6154 static const int kUndefinedOddballKind = 5; |
6113 static const int kNullOddballKind = 3; | 6155 static const int kNullOddballKind = 3; |
6114 | 6156 |
6115 static const uint32_t kNumIsolateDataSlots = 4; | 6157 static const uint32_t kNumIsolateDataSlots = 4; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6353 | 6395 |
6354 | 6396 |
6355 template <class T> | 6397 template <class T> |
6356 template <typename S, typename P> | 6398 template <typename S, typename P> |
6357 void PersistentBase<T>::SetWeak( | 6399 void PersistentBase<T>::SetWeak( |
6358 P* parameter, | 6400 P* parameter, |
6359 typename WeakCallbackData<S, P>::Callback callback) { | 6401 typename WeakCallbackData<S, P>::Callback callback) { |
6360 TYPE_CHECK(S, T); | 6402 TYPE_CHECK(S, T); |
6361 typedef typename WeakCallbackData<Value, void>::Callback Callback; | 6403 typedef typename WeakCallbackData<Value, void>::Callback Callback; |
6362 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, | 6404 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, |
6363 reinterpret_cast<Callback>(callback), V8::NonphantomHandle); | 6405 reinterpret_cast<Callback>(callback)); |
6364 } | 6406 } |
6365 | 6407 |
6366 | 6408 |
6367 template <class T> | 6409 template <class T> |
6368 template <typename P> | 6410 template <typename P> |
6369 void PersistentBase<T>::SetWeak( | 6411 void PersistentBase<T>::SetWeak( |
6370 P* parameter, | 6412 P* parameter, |
6371 typename WeakCallbackData<T, P>::Callback callback) { | 6413 typename WeakCallbackData<T, P>::Callback callback) { |
6372 SetWeak<T, P>(parameter, callback); | 6414 SetWeak<T, P>(parameter, callback); |
6373 } | 6415 } |
6374 | 6416 |
6375 | 6417 |
6376 template <class T> | 6418 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> | 6419 template <typename P> |
6389 void PersistentBase<T>::SetPhantom( | 6420 void PersistentBase<T>::SetPhantom( |
6390 P* parameter, typename WeakCallbackData<T, P>::Callback callback) { | 6421 P* parameter, typename PhantomCallbackData<P>::Callback callback) { |
6391 SetPhantom<T, P>(parameter, callback); | 6422 typedef typename PhantomCallbackData<void>::Callback Callback; |
| 6423 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), parameter, |
| 6424 reinterpret_cast<Callback>(callback), |
| 6425 Object::kNoInternalFieldIndex, Object::kNoInternalFieldIndex); |
6392 } | 6426 } |
6393 | 6427 |
6394 | 6428 |
| 6429 template <class T> |
| 6430 void PersistentBase<T>::SetPhantom( |
| 6431 typename PhantomCallbackData<void>::Callback callback, |
| 6432 int internal_field_index1, int internal_field_index2) { |
| 6433 typedef typename PhantomCallbackData<void>::Callback Callback; |
| 6434 V8::MakePhantom(reinterpret_cast<internal::Object**>(this->val_), 0, |
| 6435 reinterpret_cast<Callback>(callback), internal_field_index1, |
| 6436 internal_field_index2); |
| 6437 } |
| 6438 |
| 6439 |
6395 template <class T> | 6440 template <class T> |
6396 template <typename P> | 6441 template <typename P> |
6397 P* PersistentBase<T>::ClearWeak() { | 6442 P* PersistentBase<T>::ClearWeak() { |
6398 return reinterpret_cast<P*>( | 6443 return reinterpret_cast<P*>( |
6399 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); | 6444 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); |
6400 } | 6445 } |
6401 | 6446 |
6402 | 6447 |
6403 template <class T> | 6448 template <class T> |
6404 void PersistentBase<T>::MarkIndependent() { | 6449 void PersistentBase<T>::MarkIndependent() { |
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7378 */ | 7423 */ |
7379 | 7424 |
7380 | 7425 |
7381 } // namespace v8 | 7426 } // namespace v8 |
7382 | 7427 |
7383 | 7428 |
7384 #undef TYPE_CHECK | 7429 #undef TYPE_CHECK |
7385 | 7430 |
7386 | 7431 |
7387 #endif // V8_H_ | 7432 #endif // V8_H_ |
OLD | NEW |