Skip to content

Commit f1dc5ed

Browse files
committed
factors: Merge InitContext and ModuleInitContext
Signed-off-by: Lann Martin <[email protected]>
1 parent a10f1c8 commit f1dc5ed

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

crates/factor-wasi/src/preview1.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use spin_factors::{
2-
Factor, FactorInstancePreparer, ModuleInitContext, PrepareContext, Result, SpinFactors,
2+
Factor, FactorInstancePreparer, InitContext, PrepareContext, Result, SpinFactors,
33
};
44
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
55

@@ -9,11 +9,8 @@ impl Factor for WasiPreview1Factor {
99
type InstancePreparer = InstancePreparer;
1010
type InstanceState = WasiP1Ctx;
1111

12-
fn module_init<Factors: SpinFactors>(
13-
&mut self,
14-
mut ctx: ModuleInitContext<Factors, Self>,
15-
) -> Result<()> {
16-
ctx.link_bindings(wasmtime_wasi::preview1::add_to_linker_async)
12+
fn init<Factors: SpinFactors>(&mut self, mut ctx: InitContext<Factors, Self>) -> Result<()> {
13+
ctx.link_module_bindings(wasmtime_wasi::preview1::add_to_linker_async)
1714
}
1815
}
1916

crates/factors-derive/src/lib.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,15 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
6363
impl #name {
6464
pub fn init(
6565
&mut self,
66-
linker: &mut #wasmtime::component::Linker<#state_name>
66+
mut linker: Option<&mut #wasmtime::component::Linker<#state_name>>,
67+
mut module_linker: Option<&mut #wasmtime::Linker<#state_name>>,
6768
) -> #Result<()> {
6869
#(
6970
#Factor::init::<Self>(
7071
&mut self.#factor_names,
7172
#factors_path::InitContext::<Self, #factor_types>::new(
72-
linker,
73-
|state| &mut state.#factor_names,
74-
)
75-
)?;
76-
)*
77-
Ok(())
78-
}
79-
80-
#[allow(dead_code)]
81-
pub fn module_init(
82-
&mut self,
83-
linker: &mut #wasmtime::Linker<#state_name>
84-
) -> #Result<()> {
85-
#(
86-
#Factor::module_init::<Self>(
87-
&mut self.#factor_names,
88-
#factors_path::ModuleInitContext::<Self, #factor_types>::new(
89-
linker,
73+
linker.as_deref_mut(),
74+
module_linker.as_deref_mut(),
9075
|state| &mut state.#factor_names,
9176
)
9277
)?;

crates/factors/src/lib.rs

+41-22
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ pub trait Factor: Any + Sized {
2121
Ok(())
2222
}
2323

24-
fn module_init<Factors: SpinFactors>(
25-
&mut self,
26-
mut ctx: ModuleInitContext<Factors, Self>,
27-
) -> Result<()> {
28-
_ = &mut ctx;
29-
Ok(())
30-
}
31-
3224
fn validate_app(&self, app: &App) -> Result<()> {
3325
_ = app;
3426
Ok(())
@@ -38,24 +30,32 @@ pub trait Factor: Any + Sized {
3830
type GetDataFn<Factors, Fact> =
3931
fn(&mut <Factors as SpinFactors>::InstanceState) -> &mut <Fact as Factor>::InstanceState;
4032

41-
pub struct FactorInitContext<'a, Factors: SpinFactors, Fact: Factor, Linker> {
42-
linker: &'a mut Linker,
33+
pub struct InitContext<'a, Factors: SpinFactors, Fact: Factor> {
34+
linker: Option<&'a mut Linker<Factors>>,
35+
module_linker: Option<&'a mut ModuleLinker<Factors>>,
4336
get_data: GetDataFn<Factors, Fact>,
4437
}
4538

46-
pub type InitContext<'a, Factors, Fact> = FactorInitContext<'a, Factors, Fact, Linker<Factors>>;
47-
48-
pub type ModuleInitContext<'a, Factors, Fact> =
49-
FactorInitContext<'a, Factors, Fact, ModuleLinker<Factors>>;
50-
51-
impl<'a, Factors: SpinFactors, Fact: Factor, Linker> FactorInitContext<'a, Factors, Fact, Linker> {
39+
impl<'a, Factors: SpinFactors, Fact: Factor> InitContext<'a, Factors, Fact> {
5240
#[doc(hidden)]
53-
pub fn new(linker: &'a mut Linker, get_data: GetDataFn<Factors, Fact>) -> Self {
54-
Self { linker, get_data }
41+
pub fn new(
42+
linker: Option<&'a mut Linker<Factors>>,
43+
module_linker: Option<&'a mut ModuleLinker<Factors>>,
44+
get_data: GetDataFn<Factors, Fact>,
45+
) -> Self {
46+
Self {
47+
linker,
48+
module_linker,
49+
get_data,
50+
}
51+
}
52+
53+
pub fn linker(&mut self) -> Option<&mut Linker<Factors>> {
54+
self.linker.as_deref_mut()
5555
}
5656

57-
pub fn linker(&mut self) -> &mut Linker {
58-
self.linker
57+
pub fn module_linker(&mut self) -> Option<&mut ModuleLinker<Factors>> {
58+
self.module_linker.as_deref_mut()
5959
}
6060

6161
pub fn get_data_fn(&self) -> GetDataFn<Factors, Fact> {
@@ -65,12 +65,31 @@ impl<'a, Factors: SpinFactors, Fact: Factor, Linker> FactorInitContext<'a, Facto
6565
pub fn link_bindings(
6666
&mut self,
6767
add_to_linker: impl Fn(
68-
&mut Linker,
68+
&mut Linker<Factors>,
6969
fn(&mut Factors::InstanceState) -> &mut Fact::InstanceState,
7070
) -> Result<()>,
7171
) -> Result<()>
7272
where {
73-
add_to_linker(self.linker, self.get_data)
73+
if let Some(linker) = self.linker.as_deref_mut() {
74+
add_to_linker(linker, self.get_data)
75+
} else {
76+
Ok(())
77+
}
78+
}
79+
80+
pub fn link_module_bindings(
81+
&mut self,
82+
add_to_linker: impl Fn(
83+
&mut ModuleLinker<Factors>,
84+
fn(&mut Factors::InstanceState) -> &mut Fact::InstanceState,
85+
) -> Result<()>,
86+
) -> Result<()>
87+
where {
88+
if let Some(linker) = self.module_linker.as_deref_mut() {
89+
add_to_linker(linker, self.get_data)
90+
} else {
91+
Ok(())
92+
}
7493
}
7594
}
7695

crates/factors/tests/smoke.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
use spin_factor_wasi::WasiFactor;
1+
use spin_factor_wasi::{preview1::WasiPreview1Factor, WasiFactor};
22
use spin_factors::SpinFactors;
33

44
#[derive(SpinFactors)]
55
struct Factors {
66
wasi: WasiFactor,
7+
wasip1: WasiPreview1Factor,
78
}
89

910
fn main() -> anyhow::Result<()> {
1011
let engine = wasmtime::Engine::default();
1112
let mut linker = wasmtime::component::Linker::new(&engine);
13+
let mut module_linker = wasmtime::Linker::new(&engine);
1214

1315
let mut factors = Factors {
1416
wasi: WasiFactor,
17+
wasip1: WasiPreview1Factor,
1518
// outbound_networking_factor: OutboundNetworkingFactor,
1619
// outbound_http_factor: OutboundHttpFactor,
1720
};
18-
factors.init(&mut linker).unwrap();
21+
factors
22+
.init(Some(&mut linker), Some(&mut module_linker))
23+
.unwrap();
1924
let data = factors.build_store_data().unwrap();
2025

2126
let mut store = wasmtime::Store::new(&engine, data);
27+
2228
let component = wasmtime::component::Component::new(&engine, b"(component)").unwrap();
2329
let _instance = linker.instantiate(&mut store, &component).unwrap();
30+
31+
let module = wasmtime::Module::new(&engine, b"(module)").unwrap();
32+
let _module_instance = module_linker.instantiate(&mut store, &module).unwrap();
33+
2434
Ok(())
2535
}

0 commit comments

Comments
 (0)