RuntimeAdapter.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <memory>
  9. #include <hermes/hermes.h>
  10. namespace facebook {
  11. namespace hermes {
  12. namespace inspector {
  13. /**
  14. * RuntimeAdapter encapsulates a HermesRuntime object. The underlying Hermes
  15. * runtime object should stay alive for at least as long as the RuntimeAdapter
  16. * is alive.
  17. */
  18. class RuntimeAdapter {
  19. public:
  20. virtual ~RuntimeAdapter() = 0;
  21. /// getRuntime should return the runtime encapsulated by this adapter.
  22. virtual jsi::Runtime &getRuntime() = 0;
  23. virtual debugger::Debugger &getDebugger() = 0;
  24. /// tickleJs is a method that subclasses can choose to override to make the
  25. /// inspector more responsive. If overridden, it should call the "__tickleJs"
  26. /// function. The call should occur with appropriate locking (e.g. via a
  27. /// thread-safe runtime instance, or by enqueuing the call on to a dedicated
  28. /// JS thread).
  29. ///
  30. /// This makes the inspector more responsive because it gives the inspector
  31. /// the ability to force the process to enter the Hermes interpreter loop
  32. /// soon. This is important because the inspector can only do a number of
  33. /// important operations (like manipulating breakpoints) within the context of
  34. /// a Hermes interperter loop.
  35. ///
  36. /// The default implementation does nothing.
  37. virtual void tickleJs();
  38. };
  39. /**
  40. * SharedRuntimeAdapter is a simple implementation of RuntimeAdapter that
  41. * uses shared_ptr to hold on to the runtime. It's generally only used in tests,
  42. * since it does not implement tickleJs.
  43. */
  44. class SharedRuntimeAdapter : public RuntimeAdapter {
  45. public:
  46. SharedRuntimeAdapter(
  47. std::shared_ptr<jsi::Runtime> runtime,
  48. debugger::Debugger &debugger);
  49. virtual ~SharedRuntimeAdapter();
  50. jsi::Runtime &getRuntime() override;
  51. debugger::Debugger &getDebugger() override;
  52. private:
  53. std::shared_ptr<jsi::Runtime> runtime_;
  54. debugger::Debugger &debugger_;
  55. };
  56. } // namespace inspector
  57. } // namespace hermes
  58. } // namespace facebook