pinnable.h 883 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (c) 2020 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef SHELL_COMMON_GIN_HELPER_PINNABLE_H_
  5. #define SHELL_COMMON_GIN_HELPER_PINNABLE_H_
  6. #include "v8/include/v8.h"
  7. namespace gin_helper {
  8. template <typename T>
  9. class Pinnable {
  10. protected:
  11. // Prevent the object from being garbage collected until Unpin() is called.
  12. void Pin(v8::Isolate* isolate) {
  13. v8::Locker locker(isolate);
  14. v8::HandleScope scope(isolate);
  15. v8::Local<v8::Value> wrapper;
  16. if (static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper)) {
  17. pinned_.Reset(isolate, wrapper);
  18. }
  19. }
  20. // Allow the object to be garbage collected.
  21. void Unpin() { pinned_.Reset(); }
  22. private:
  23. v8::Global<v8::Value> pinned_;
  24. };
  25. } // namespace gin_helper
  26. #endif // SHELL_COMMON_GIN_HELPER_PINNABLE_H_