v8-weak-callback-info.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 the V8 project authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef INCLUDE_V8_WEAK_CALLBACK_INFO_H_
  5. #define INCLUDE_V8_WEAK_CALLBACK_INFO_H_
  6. #include "v8config.h" // NOLINT(build/include_directory)
  7. namespace v8 {
  8. class Isolate;
  9. namespace api_internal {
  10. V8_EXPORT void InternalFieldOutOfBounds(int index);
  11. } // namespace api_internal
  12. static const int kInternalFieldsInWeakCallback = 2;
  13. static const int kEmbedderFieldsInWeakCallback = 2;
  14. template <typename T>
  15. class WeakCallbackInfo {
  16. public:
  17. using Callback = void (*)(const WeakCallbackInfo<T>& data);
  18. WeakCallbackInfo(Isolate* isolate, T* parameter,
  19. void* embedder_fields[kEmbedderFieldsInWeakCallback],
  20. Callback* callback)
  21. : isolate_(isolate), parameter_(parameter), callback_(callback) {
  22. for (int i = 0; i < kEmbedderFieldsInWeakCallback; ++i) {
  23. embedder_fields_[i] = embedder_fields[i];
  24. }
  25. }
  26. V8_INLINE Isolate* GetIsolate() const { return isolate_; }
  27. V8_INLINE T* GetParameter() const { return parameter_; }
  28. V8_INLINE void* GetInternalField(int index) const;
  29. // When first called, the embedder MUST Reset() the Global which triggered the
  30. // callback. The Global itself is unusable for anything else. No v8 other api
  31. // calls may be called in the first callback. Should additional work be
  32. // required, the embedder must set a second pass callback, which will be
  33. // called after all the initial callbacks are processed.
  34. // Calling SetSecondPassCallback on the second pass will immediately crash.
  35. void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
  36. private:
  37. Isolate* isolate_;
  38. T* parameter_;
  39. Callback* callback_;
  40. void* embedder_fields_[kEmbedderFieldsInWeakCallback];
  41. };
  42. /**
  43. * Weakness type for weak handles.
  44. */
  45. enum class WeakCallbackType {
  46. /**
  47. * Passes a user-defined void* parameter back to the callback.
  48. */
  49. kParameter,
  50. /**
  51. * Passes the first two internal fields of the object back to the callback.
  52. */
  53. kInternalFields,
  54. /**
  55. * Passes a user-defined void* parameter back to the callback. Will do so
  56. * before the object is actually reclaimed, allowing it to be resurrected. In
  57. * this case it is not possible to set a second-pass callback.
  58. */
  59. kFinalizer V8_ENUM_DEPRECATED("Resurrecting finalizers are deprecated "
  60. "and will not be supported going forward.")
  61. };
  62. template <class T>
  63. void* WeakCallbackInfo<T>::GetInternalField(int index) const {
  64. #ifdef V8_ENABLE_CHECKS
  65. if (index < 0 || index >= kEmbedderFieldsInWeakCallback) {
  66. api_internal::InternalFieldOutOfBounds(index);
  67. }
  68. #endif
  69. return embedder_fields_[index];
  70. }
  71. } // namespace v8
  72. #endif // INCLUDE_V8_WEAK_CALLBACK_INFO_H_