hermes.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef HERMES_HERMES_H
  8. #define HERMES_HERMES_H
  9. #include <exception>
  10. #include <list>
  11. #include <memory>
  12. #include <string>
  13. #include <hermes/Public/RuntimeConfig.h>
  14. #include <jsi/jsi.h>
  15. struct HermesTestHelper;
  16. namespace llvm {
  17. class raw_ostream;
  18. }
  19. namespace hermes {
  20. namespace vm {
  21. struct MockedEnvironment;
  22. } // namespace vm
  23. } // namespace hermes
  24. namespace facebook {
  25. namespace jsi {
  26. class ThreadSafeRuntime;
  27. }
  28. namespace hermes {
  29. #ifdef HERMES_ENABLE_DEBUGGER
  30. namespace debugger {
  31. class Debugger;
  32. }
  33. #endif
  34. class HermesRuntimeImpl;
  35. /// Represents a Hermes JS runtime.
  36. class HermesRuntime : public jsi::Runtime {
  37. public:
  38. static bool isHermesBytecode(const uint8_t *data, size_t len);
  39. // (EXPERIMENTAL) Issues madvise calls for portions of the given
  40. // bytecode file that will likely be used when loading the bytecode
  41. // file and running its global function.
  42. static void prefetchHermesBytecode(const uint8_t *data, size_t len);
  43. // Returns whether the data is valid HBC with more extensive checks than
  44. // isHermesBytecode and returns why it isn't in errorMessage (if nonnull)
  45. // if not.
  46. static bool hermesBytecodeSanityCheck(
  47. const uint8_t *data,
  48. size_t len,
  49. std::string *errorMessage = nullptr);
  50. static void setFatalHandler(void (*handler)(const std::string &));
  51. // Assuming that \p data is valid HBC bytecode data, returns a pointer to then
  52. // first element of the epilogue, data append to the end of the bytecode
  53. // stream. Return pair contain ptr to data and header.
  54. static std::pair<const uint8_t *, size_t> getBytecodeEpilogue(
  55. const uint8_t *data,
  56. size_t len);
  57. /// Enable sampling profiler.
  58. static void enableSamplingProfiler();
  59. /// Disable the sampling profiler
  60. static void disableSamplingProfiler();
  61. /// Dump sampled stack trace to the given file name.
  62. static void dumpSampledTraceToFile(const std::string &fileName);
  63. /// Return the executed JavaScript function info.
  64. /// Each function info is a 64bit integer with the module id encoded in
  65. /// upper 32bit and function virtual offset in lower 32bit.
  66. static std::vector<int64_t> getExecutedFunctions();
  67. /// Enable code coverage profiler.
  68. static void enableCodeCoverageProfiler();
  69. /// Disable code coverage profiler.
  70. static void disableCodeCoverageProfiler();
  71. // The base class declares most of the interesting methods. This
  72. // just declares new methods which are specific to HermesRuntime.
  73. // The actual implementations of the pure virtual methods are
  74. // provided by a class internal to the .cpp file, which is created
  75. // by the factory.
  76. /// Load a new segment into the Runtime.
  77. /// The \param context must be a valid RequireContext retrieved from JS
  78. /// using `require.context`.
  79. void loadSegment(
  80. std::unique_ptr<const jsi::Buffer> buffer,
  81. const jsi::Value &context);
  82. /// Gets a guaranteed unique id for an object, which is assigned at
  83. /// allocation time and is static throughout that object's lifetime.
  84. uint64_t getUniqueID(const jsi::Object &o) const;
  85. /// Get a structure representing the enviroment-dependent behavior, so
  86. /// it can be written into the trace for later replay.
  87. const ::hermes::vm::MockedEnvironment &getMockedEnvironment() const;
  88. /// Make the runtime read from \p env to replay its environment-dependent
  89. /// behavior.
  90. void setMockedEnvironment(const ::hermes::vm::MockedEnvironment &env);
  91. /// Get IO tracking (aka HBC page access) info as a JSON string.
  92. /// See hermes::vm::Runtime::getIOTrackingInfoJSON() for conditions
  93. /// needed for there to be useful output.
  94. std::string getIOTrackingInfoJSON();
  95. #ifdef HERMESVM_PROFILER_BB
  96. /// Write the trace to the given stream.
  97. void dumpBasicBlockProfileTrace(llvm::raw_ostream &os) const;
  98. #endif
  99. #ifdef HERMESVM_PROFILER_OPCODE
  100. /// Write the opcode stats to the given stream.
  101. void dumpOpcodeStats(llvm::raw_ostream &os) const;
  102. #endif
  103. #ifdef HERMESVM_PROFILER_EXTERN
  104. /// Dump map of profiler symbols to given file name.
  105. void dumpProfilerSymbolsToFile(const std::string &fileName) const;
  106. #endif
  107. #ifdef HERMES_ENABLE_DEBUGGER
  108. /// \return a reference to the Debugger for this Runtime.
  109. debugger::Debugger &getDebugger();
  110. struct DebugFlags {
  111. bool lazy{false};
  112. };
  113. /// Evaluate the given code in an unoptimized form,
  114. /// used for debugging.
  115. void debugJavaScript(
  116. const std::string &src,
  117. const std::string &sourceURL,
  118. const DebugFlags &debugFlags);
  119. #endif
  120. /// Register this runtime for sampling profiler.
  121. void registerForProfiling();
  122. /// Unregister this runtime for sampling profiler.
  123. void unregisterForProfiling();
  124. /// Register this runtime for execution time limit monitoring, with a time
  125. /// limit of \p timeoutInMs milliseconds.
  126. /// All JS compiled to bytecode via prepareJS, or evaluateJS, will support the
  127. /// time limit monitoring. If JS prepared in other ways is executed, care
  128. /// must be taken to ensure that it is compiled in a mode that supports the
  129. /// monitoring (i.e., the emitted code contains async break checks).
  130. void watchTimeLimit(uint32_t timeoutInMs);
  131. /// Unregister this runtime for execution time limit monitoring.
  132. void unwatchTimeLimit();
  133. private:
  134. // Only HermesRuntimeImpl can subclass this.
  135. HermesRuntime() = default;
  136. friend class HermesRuntimeImpl;
  137. friend struct ::HermesTestHelper;
  138. size_t rootsListLength() const;
  139. // Do not add any members here. This ensures that there are no
  140. // object size inconsistencies. All data should be in the impl
  141. // class in the .cpp file.
  142. };
  143. std::unique_ptr<HermesRuntime> makeHermesRuntime(
  144. const ::hermes::vm::RuntimeConfig &runtimeConfig =
  145. ::hermes::vm::RuntimeConfig());
  146. std::unique_ptr<jsi::ThreadSafeRuntime> makeThreadSafeHermesRuntime(
  147. const ::hermes::vm::RuntimeConfig &runtimeConfig =
  148. ::hermes::vm::RuntimeConfig());
  149. } // namespace hermes
  150. } // namespace facebook
  151. #endif