Skip to content

Commit dfb3a90

Browse files
authored
Merge pull request #763 from lann/spin-core-refactor
Spin core refactor
2 parents e4245de + c12caa5 commit dfb3a90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3701
-2240
lines changed

Cargo.lock

+147-211
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ serde_json = "1.0.82"
3232
sha2 = "0.10.2"
3333
spin-build = { path = "crates/build" }
3434
spin-config = { path = "crates/config" }
35-
spin-engine = { path = "crates/engine" }
3635
spin-http = { path = "crates/http" }
3736
spin-loader = { path = "crates/loader" }
3837
spin-manifest = { path = "crates/manifest" }
@@ -71,9 +70,10 @@ e2e-tests = []
7170
[workspace]
7271
members = [
7372
"crates/abi-conformance",
73+
"crates/app",
7474
"crates/build",
7575
"crates/config",
76-
"crates/engine",
76+
"crates/core",
7777
"crates/http",
7878
"crates/loader",
7979
"crates/manifest",

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ check-rust-examples:
2222

2323
.PHONY: test-unit
2424
test-unit:
25-
RUST_LOG=$(LOG_LEVEL) cargo test --all --no-fail-fast -- --skip integration_tests --nocapture --include-ignored
25+
RUST_LOG=$(LOG_LEVEL) cargo test --all --no-fail-fast -- --skip integration_tests --nocapture
2626

2727
.PHONY: test-integration
2828
test-integration:
29-
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --no-fail-fast -- --nocapture --include-ignored
29+
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --no-fail-fast -- --nocapture
3030

3131
.PHONY: test-e2e
3232
test-e2e:
3333
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- integration_tests::test_dependencies --nocapture
34-
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- --skip integration_tests::test_dependencies --nocapture --include-ignored
34+
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- --skip integration_tests::test_dependencies --nocapture
3535

3636
.PHONY: test-sdk-go
3737
test-sdk-go:

build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ error: the `wasm32-wasi` target is not installed
3939

4040
std::fs::create_dir_all("target/test-programs").unwrap();
4141

42+
build_wasm_test_program("core-wasi-test.wasm", "crates/core/tests/core-wasi-test");
4243
build_wasm_test_program("rust-http-test.wasm", "crates/http/tests/rust-http-test");
4344
build_wasm_test_program("redis-rust.wasm", "crates/redis/tests/rust");
4445
build_wasm_test_program("wagi-test.wasm", "crates/http/tests/wagi-test");

crates/app/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "spin-app"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0"
8+
async-trait = "0.1"
9+
ouroboros = "0.15"
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0"
12+
spin-core = { path = "../core" }
13+
thiserror = "1.0"

crates/app/src/host_component.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::sync::Arc;
2+
3+
use spin_core::{EngineBuilder, HostComponent, HostComponentsData};
4+
5+
use crate::AppComponent;
6+
7+
/// A trait for "dynamic" Spin host components.
8+
///
9+
/// This extends [`HostComponent`] to support per-[`AppComponent`] dynamic
10+
/// runtime configuration.
11+
pub trait DynamicHostComponent: HostComponent {
12+
/// Called on [`AppComponent`] instance initialization.
13+
///
14+
/// The `data` returned by [`HostComponent::build_data`] is passed, along
15+
/// with a reference to the `component` being instantiated.
16+
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()>;
17+
}
18+
19+
impl<DHC: DynamicHostComponent> DynamicHostComponent for Arc<DHC> {
20+
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()> {
21+
(**self).update_data(data, component)
22+
}
23+
}
24+
25+
type DataUpdater =
26+
Box<dyn Fn(&mut HostComponentsData, &AppComponent) -> anyhow::Result<()> + Send + Sync>;
27+
28+
#[derive(Default)]
29+
pub struct DynamicHostComponents {
30+
data_updaters: Vec<DataUpdater>,
31+
}
32+
33+
impl DynamicHostComponents {
34+
pub fn add_dynamic_host_component<T: Send + Sync, DHC: DynamicHostComponent>(
35+
&mut self,
36+
engine_builder: &mut EngineBuilder<T>,
37+
host_component: DHC,
38+
) -> anyhow::Result<()> {
39+
let host_component = Arc::new(host_component);
40+
let handle = engine_builder.add_host_component(host_component.clone())?;
41+
self.data_updaters
42+
.push(Box::new(move |host_components_data, component| {
43+
let data = host_components_data.get_or_insert(handle);
44+
host_component.update_data(data, component)
45+
}));
46+
Ok(())
47+
}
48+
49+
pub fn update_data(
50+
&self,
51+
host_components_data: &mut HostComponentsData,
52+
component: &AppComponent,
53+
) -> anyhow::Result<()> {
54+
for data_updater in &self.data_updaters {
55+
data_updater(host_components_data, component)?;
56+
}
57+
Ok(())
58+
}
59+
}

0 commit comments

Comments
 (0)