|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <jsi/jsi.h> |
| 11 | + |
| 12 | +struct SHUnit; |
| 13 | +struct SHRuntime; |
| 14 | +namespace hermes::vm { |
| 15 | +class GCExecTrace; |
| 16 | +} |
| 17 | + |
| 18 | +namespace facebook::hermes { |
| 19 | + |
| 20 | +namespace debugger { |
| 21 | +class Debugger; |
| 22 | +} |
| 23 | + |
| 24 | +/// Interface for Hermes-specific runtime methods.The actual implementations of |
| 25 | +/// the pure virtual methods are provided by Hermes API. |
| 26 | +class JSI_EXPORT IHermes : public jsi::ICast { |
| 27 | + public: |
| 28 | + static constexpr jsi::UUID uuid{ |
| 29 | + 0xe85cfa22, |
| 30 | + 0xdfae, |
| 31 | + 0x11ef, |
| 32 | + 0xa6f7, |
| 33 | + 0x325096b39f47}; |
| 34 | + |
| 35 | + /// Return a ICast pointer to an object that be cast into the interface |
| 36 | + /// IHermesRootAPI. This root API object has static lifetime. |
| 37 | + virtual ICast* getHermesRootAPI() = 0; |
| 38 | + |
| 39 | + /// Serialize the sampled stack to the format expected by DevTools' |
| 40 | + /// Profiler.stop return type. |
| 41 | + virtual void sampledTraceToStreamInDevToolsFormat(std::ostream& stream) = 0; |
| 42 | + |
| 43 | + /// Resets the timezone offset cache used by Hermes for performance |
| 44 | + /// optimization. Hermes maintains a cached timezone offset to accelerate date |
| 45 | + /// and time calculations. However, this cache does not automatically detect |
| 46 | + /// changes to the system timezone. When the system timezone changes, the |
| 47 | + /// integration layer (e.g., React Native) must call this method to invalidate |
| 48 | + /// the cache and ensure correct time calculations. |
| 49 | + /// |
| 50 | + /// \note Call this method immediately after detecting any timezone change in |
| 51 | + /// the integrator. |
| 52 | + virtual void resetTimezoneCache() = 0; |
| 53 | + |
| 54 | + /// Load a new segment into the Runtime. |
| 55 | + /// The \param context must be a valid RequireContext retrieved from JS |
| 56 | + /// using `require.context`. |
| 57 | + virtual void loadSegment( |
| 58 | + std::unique_ptr<const jsi::Buffer> buffer, |
| 59 | + const jsi::Value& context) = 0; |
| 60 | + |
| 61 | + /// Gets a guaranteed unique id for an Object (or, respectively, String |
| 62 | + /// or PropNameId), which is assigned at allocation time and is |
| 63 | + /// static throughout that object's (or string's, or PropNameID's) |
| 64 | + /// lifetime. |
| 65 | + virtual uint64_t getUniqueID(const jsi::Object& o) const = 0; |
| 66 | + virtual uint64_t getUniqueID(const jsi::BigInt& s) const = 0; |
| 67 | + virtual uint64_t getUniqueID(const jsi::String& s) const = 0; |
| 68 | + virtual uint64_t getUniqueID(const jsi::PropNameID& pni) const = 0; |
| 69 | + virtual uint64_t getUniqueID(const jsi::Symbol& sym) const = 0; |
| 70 | + |
| 71 | + /// Same as the other \c getUniqueID, except it can return 0 for some values. |
| 72 | + /// 0 means there is no ID associated with the value. |
| 73 | + virtual uint64_t getUniqueID(const jsi::Value& val) const = 0; |
| 74 | + |
| 75 | + /// From an ID retrieved from \p getUniqueID, go back to the object. |
| 76 | + /// NOTE: This is much slower in general than the reverse operation, and takes |
| 77 | + /// up more memory. Don't use this unless it's absolutely necessary. |
| 78 | + /// \return a jsi::Object if a matching object is found, else returns null. |
| 79 | + virtual jsi::Value getObjectForID(uint64_t id) = 0; |
| 80 | + |
| 81 | + /// Get a structure representing the execution history (currently just of |
| 82 | + /// GC, but will be generalized as necessary), to aid in debugging |
| 83 | + /// non-deterministic execution. |
| 84 | + virtual const ::hermes::vm::GCExecTrace& getGCExecTrace() const = 0; |
| 85 | + |
| 86 | + /// Get IO tracking (aka HBC page access) info as a JSON string. |
| 87 | + /// See hermes::vm::Runtime::getIOTrackingInfoJSON() for conditions |
| 88 | + /// needed for there to be useful output. |
| 89 | + virtual std::string getIOTrackingInfoJSON() = 0; |
| 90 | + |
| 91 | + /// \return a reference to the Debugger for this Runtime. |
| 92 | + virtual debugger::Debugger& getDebugger() = 0; |
| 93 | + |
| 94 | + /// Register this runtime and thread for sampling profiler. Before using the |
| 95 | + /// runtime on another thread, invoke this function again from the new thread |
| 96 | + /// to make the sampling profiler target the new thread (and forget the old |
| 97 | + /// thread). |
| 98 | + virtual void registerForProfiling() = 0; |
| 99 | + /// Unregister this runtime for sampling profiler. |
| 100 | + virtual void unregisterForProfiling() = 0; |
| 101 | + |
| 102 | + /// Define methods to interrupt JS execution and set time limits. |
| 103 | + /// All JS compiled to bytecode via prepareJS, or evaluateJS, will support |
| 104 | + /// interruption and time limit monitoring if the runtime is configured with |
| 105 | + /// AsyncBreakCheckInEval. If JS prepared in other ways is executed, care must |
| 106 | + /// be taken to ensure that it is compiled in a mode that supports it (i.e., |
| 107 | + /// the emitted code contains async break checks). |
| 108 | + |
| 109 | + /// Asynchronously terminates the current execution. This can be called on |
| 110 | + /// any thread. |
| 111 | + virtual void asyncTriggerTimeout() = 0; |
| 112 | + |
| 113 | + /// Register this runtime for execution time limit monitoring, with a time |
| 114 | + /// limit of \p timeoutInMs milliseconds. |
| 115 | + /// See compilation notes above. |
| 116 | + virtual void watchTimeLimit(uint32_t timeoutInMs) = 0; |
| 117 | + /// Unregister this runtime for execution time limit monitoring. |
| 118 | + virtual void unwatchTimeLimit() = 0; |
| 119 | + |
| 120 | + /// Same as \c evaluate JavaScript but with a source map, which will be |
| 121 | + /// applied to exception traces and debug information. |
| 122 | + /// |
| 123 | + /// This is an experimental Hermes-specific API. In the future it may be |
| 124 | + /// renamed, moved or combined with another API, but the provided |
| 125 | + /// functionality will continue to be available in some form. |
| 126 | + virtual jsi::Value evaluateJavaScriptWithSourceMap( |
| 127 | + const std::shared_ptr<const jsi::Buffer>& buffer, |
| 128 | + const std::shared_ptr<const jsi::Buffer>& sourceMapBuf, |
| 129 | + const std::string& sourceURL) = 0; |
| 130 | + |
| 131 | + /// Associate the SHUnit returned by \p shUnitCreator with this runtime and |
| 132 | + /// run its initialization code. The unit will be freed when the runtime is |
| 133 | + /// destroyed. |
| 134 | + virtual jsi::Value evaluateSHUnit(SHUnit* (*shUnitCreator)()) = 0; |
| 135 | + |
| 136 | + /// Retrieve the underlying SHRuntime. |
| 137 | + virtual SHRuntime* getSHRuntime() noexcept = 0; |
| 138 | + |
| 139 | + /// Returns the underlying low level Hermes VM runtime instance. |
| 140 | + /// This function is considered unsafe and unstable. |
| 141 | + /// Direct use of a vm::Runtime should be avoided as the lower level APIs are |
| 142 | + /// unsafe and they can change without notice. |
| 143 | + virtual void* getVMRuntimeUnsafe() const = 0; |
| 144 | + |
| 145 | + protected: |
| 146 | + ~IHermes() = default; |
| 147 | +}; |
| 148 | +} // namespace facebook::hermes |
0 commit comments