Skip to content

Commit 53770aa

Browse files
rafal-chaido-mth
andauthored
feat(hermes): Initialize module via direct function call, not an event (#528)
* Call init instead of emitting an init event * Remove `InitEvent` * Fix `ApplicationName` * Revert `Vfs` visibiliy * Update docs * Report failed module in logs * Clean-up app initialization * Improve error handling * Fix module initialization * Fix component build in the integration tests * Add integration test for failed module initialization * Fix formatting * Satisfy Lints * Add TODO * Update hermes/bin/src/cli/run.rs Co-authored-by: Vladislav Borbut <[email protected]> * Add missing import * Temporarily disable `parallel_execution` test --------- Co-authored-by: Vladislav Borbut <[email protected]>
1 parent a18a2b9 commit 53770aa

File tree

17 files changed

+264
-72
lines changed

17 files changed

+264
-72
lines changed

hermes/Cargo.lock

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hermes/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ resolver = "2"
33
members = [
44
"bin",
55
"bin/tests/integration/components/http_request_rte_01",
6-
"bin/tests/integration/components/sleep_component",
6+
"bin/tests/integration/components/failed_init",
7+
"bin/tests/integration/components/sleep_component"
78
]
89
default-members = [
910
"bin",

hermes/bin/src/app.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ impl Application {
9393
}
9494
}
9595

96+
/// Initialize every module.
97+
pub(crate) fn init(&self) -> anyhow::Result<()> {
98+
for module in self.indexed_modules.values() {
99+
if let Err(e) = module.init(self.name.clone(), Arc::clone(&self.vfs)) {
100+
anyhow::bail!("Failed to initialize module {}: {}", module.id(), e)
101+
}
102+
}
103+
Ok(())
104+
}
105+
96106
/// Dispatch event for the target module by the `module_id`.
97107
pub(crate) fn dispatch_event_for_target_module(
98108
&self,

hermes/bin/src/cli/run.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ impl Run {
6262
Emoji::new("🛠️", ""),
6363
app.name()
6464
);
65+
// TODO[RC]: Prevent the app from receiving any events until it is fully initialized.
66+
// TODO[RC]: Currently, when a module fails to initialize, the whole app fails to run.
6567
reactor::load_app(app)?;
6668

6769
let exit = if let Some(timeout_ms) = self.timeout_ms {

hermes/bin/src/reactor.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use once_cell::sync::OnceCell;
66
use crate::{
77
app::{Application, ApplicationName},
88
event::{self, queue::ExitLock},
9-
runtime_extensions::hermes::init,
109
};
1110

1211
/// Global Hermes reactor state
@@ -45,7 +44,6 @@ pub(crate) fn init() -> anyhow::Result<ExitLock> {
4544
/// # Errors
4645
///
4746
/// - Reactor not initialized.
48-
/// - Cannot send initialization event to the application.
4947
pub(crate) fn load_app(app: Application) -> anyhow::Result<()> {
5048
let reactor = REACTOR_STATE.get().ok_or(anyhow::anyhow!(
5149
"Reactor not been initialized. Call `HermesEventQueue::init` first."
@@ -54,8 +52,13 @@ pub(crate) fn load_app(app: Application) -> anyhow::Result<()> {
5452
let app_name = app.name().clone();
5553
reactor.apps.insert(app_name.clone(), app);
5654

57-
init::emit_init_event(app_name)?;
58-
Ok(())
55+
init_app(&app_name)
56+
}
57+
58+
/// Initialize the Application.
59+
pub(crate) fn init_app(app_name: &ApplicationName) -> anyhow::Result<()> {
60+
let app = get_app(app_name)?;
61+
app.init()
5962
}
6063

6164
/// Get Hermes application from the Hermes Reactor.

hermes/bin/src/runtime_extensions/hermes/init/event.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
11
//! Init runtime extension implementation.
22
3-
use crate::{
4-
app::ApplicationName,
5-
event as hermes_event,
6-
event::{HermesEvent, TargetApp, TargetModule},
7-
};
8-
9-
mod event;
103
mod host;
11-
12-
/// Emit Init event for a provided Hermes app target
13-
pub(crate) fn emit_init_event(target_app: ApplicationName) -> anyhow::Result<()> {
14-
let init_event = HermesEvent::new(
15-
event::InitEvent {},
16-
TargetApp::List(vec![target_app]),
17-
TargetModule::All,
18-
);
19-
hermes_event::queue::send(init_event)?;
20-
Ok(())
21-
}

hermes/bin/src/wasm/module.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
77
use std::{
88
io::Read,
9-
sync::atomic::{AtomicU32, Ordering},
9+
sync::{
10+
atomic::{AtomicU32, Ordering},
11+
Arc,
12+
},
1013
};
1114

1215
use rusty_ulid::Ulid;
@@ -22,7 +25,9 @@ use crate::{
2225
runtime_extensions::{
2326
bindings::{self, LinkOptions},
2427
init::trait_module::{RteInitModule, RteModule},
28+
new_context,
2529
},
30+
vfs::Vfs,
2631
wasm::engine::Engine,
2732
};
2833

@@ -117,6 +122,33 @@ impl Module {
117122
})
118123
}
119124

125+
/// Initializes the WASM module by calling its `init` function.
126+
pub(crate) fn init(
127+
&self,
128+
app_name: ApplicationName,
129+
vfs: Arc<Vfs>,
130+
) -> anyhow::Result<()> {
131+
let runtime_ctx = HermesRuntimeContext::new(
132+
app_name,
133+
self.id.clone(),
134+
"init_function_call".to_string(),
135+
0,
136+
vfs,
137+
);
138+
139+
new_context(&runtime_ctx);
140+
141+
let mut store = WasmStore::new(&self.engine, runtime_ctx);
142+
//let instance = self.pre_instance.instantiate(store).unwrap();
143+
let instance =
144+
bindings::HermesPre::new(self.pre_instance.clone())?.instantiate(&mut store)?;
145+
let init_result = instance.hermes_init_event().call_init(&mut store)?;
146+
if !init_result {
147+
anyhow::bail!("WASM module init function returned false")
148+
}
149+
Ok(())
150+
}
151+
120152
/// Instantiate WASM module reader
121153
///
122154
/// # Errors
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "failed_init"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
wit-bindgen = "0.43.0"
12+
13+
[lints]
14+
workspace = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

0 commit comments

Comments
 (0)