Skip to content

Commit

Permalink
expose a way to create a JSI HernesRuntime instance from an existing …
Browse files Browse the repository at this point in the history
…hermes vm runtime instance
  • Loading branch information
rFlex committed Jun 5, 2024
1 parent a8084ab commit 5c967c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions API/hermes/DebuggerAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ class HERMES_EXPORT Debugger {
private:
friend std::unique_ptr<HermesRuntime> hermes::makeHermesRuntime(
const ::hermes::vm::RuntimeConfig &);
friend std::unique_ptr<HermesRuntime> hermes::adoptHermesRuntime(
const std::shared_ptr<::hermes::vm::Runtime> &,
const ::hermes::vm::RuntimeConfig &);
friend std::unique_ptr<jsi::ThreadSafeRuntime>
hermes::makeThreadSafeHermesRuntime(const ::hermes::vm::RuntimeConfig &);
friend ProgramState;
Expand Down
27 changes: 26 additions & 1 deletion API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,16 @@ class HermesRuntimeImpl final : public HermesRuntime,
static constexpr uint32_t kSentinelNativeValue = 0x6ef71fe1;

HermesRuntimeImpl(const vm::RuntimeConfig &runtimeConfig)
: HermesRuntimeImpl(
runtimeConfig,
::hermes::vm::Runtime::create(runtimeConfig)) {}

HermesRuntimeImpl(
const vm::RuntimeConfig &runtimeConfig,
const std::shared_ptr<::hermes::vm::Runtime> &rt)
: hermesValues_(runtimeConfig.getGCConfig().getOccupancyTarget()),
weakHermesValues_(runtimeConfig.getGCConfig().getOccupancyTarget()),
rt_(::hermes::vm::Runtime::create(runtimeConfig)),
rt_(rt),
runtime_(*rt_),
vmExperimentFlags_(runtimeConfig.getVMExperimentFlags()) {
#ifdef HERMES_ENABLE_DEBUGGER
Expand Down Expand Up @@ -2465,6 +2472,24 @@ std::unique_ptr<HermesRuntime> makeHermesRuntime(
return ret;
}

std::unique_ptr<HermesRuntime> adoptHermesRuntime(
const std::shared_ptr<::hermes::vm::Runtime> &runtime,
const vm::RuntimeConfig &runtimeConfig) {
auto ret = std::make_unique<HermesRuntimeImpl>(runtimeConfig, runtime);

#ifdef HERMES_ENABLE_DEBUGGER
// Only HermesRuntime can create a debugger instance. This requires
// the setter and not using make_unique, so the call to new is here
// in this function, which is a friend of debugger::Debugger.
ret->setDebugger(std::unique_ptr<debugger::Debugger>(
new debugger::Debugger(ret.get(), ret->runtime_)));
#else
ret->setDebugger(std::make_unique<debugger::Debugger>());
#endif

return ret;
}

std::unique_ptr<jsi::ThreadSafeRuntime> makeThreadSafeHermesRuntime(
const vm::RuntimeConfig &runtimeConfig) {
#if defined(HERMESVM_PLATFORM_LOGGING)
Expand Down
4 changes: 4 additions & 0 deletions API/hermes/hermes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct HermesTestHelper;
namespace hermes {
namespace vm {
class GCExecTrace;
class Runtime;
} // namespace vm
} // namespace hermes

Expand Down Expand Up @@ -238,6 +239,9 @@ HERMES_EXPORT ::hermes::vm::RuntimeConfig hardenedHermesRuntimeConfig();
HERMES_EXPORT std::unique_ptr<HermesRuntime> makeHermesRuntime(
const ::hermes::vm::RuntimeConfig &runtimeConfig =
::hermes::vm::RuntimeConfig());
HERMES_EXPORT std::unique_ptr<HermesRuntime> adoptHermesRuntime(
const std::shared_ptr<::hermes::vm::Runtime> &runtime,
const ::hermes::vm::RuntimeConfig &runtimeConfig);
HERMES_EXPORT std::unique_ptr<jsi::ThreadSafeRuntime>
makeThreadSafeHermesRuntime(
const ::hermes::vm::RuntimeConfig &runtimeConfig =
Expand Down

0 comments on commit 5c967c3

Please sign in to comment.