Skip to content

Commit 6fbd5b7

Browse files
committed
factors: Remove module code
Signed-off-by: Lann Martin <[email protected]>
1 parent 264a7b5 commit 6fbd5b7

File tree

9 files changed

+153
-236
lines changed

9 files changed

+153
-236
lines changed

crates/factor-outbound-http/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ impl Factor for OutboundHttpFactor {
2222
mut ctx: spin_factors::InitContext<T, Self>,
2323
) -> anyhow::Result<()> {
2424
ctx.link_bindings(spin_world::v1::http::add_to_linker)?;
25-
if let Some(linker) = ctx.linker() {
26-
wasi::add_to_linker::<T>(linker)?;
27-
}
25+
wasi::add_to_linker::<T>(ctx.linker())?;
2826
Ok(())
2927
}
3028

crates/factor-wasi/src/lib.rs

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
pub mod preview1;
2-
31
use std::{future::Future, net::SocketAddr, path::Path};
42

53
use spin_factors::{
@@ -37,36 +35,35 @@ impl Factor for WasiFactor {
3735
}
3836
let get_data = ctx.get_data_fn();
3937
let closure = type_annotate(move |data| WasiImpl(get_data(data)));
40-
if let Some(linker) = ctx.linker() {
41-
use wasmtime_wasi::bindings;
42-
bindings::clocks::wall_clock::add_to_linker_get_host(linker, closure)?;
43-
bindings::clocks::monotonic_clock::add_to_linker_get_host(linker, closure)?;
44-
bindings::filesystem::types::add_to_linker_get_host(linker, closure)?;
45-
bindings::filesystem::preopens::add_to_linker_get_host(linker, closure)?;
46-
bindings::io::error::add_to_linker_get_host(linker, closure)?;
47-
bindings::io::poll::add_to_linker_get_host(linker, closure)?;
48-
bindings::io::streams::add_to_linker_get_host(linker, closure)?;
49-
bindings::random::random::add_to_linker_get_host(linker, closure)?;
50-
bindings::random::insecure::add_to_linker_get_host(linker, closure)?;
51-
bindings::random::insecure_seed::add_to_linker_get_host(linker, closure)?;
52-
bindings::cli::exit::add_to_linker_get_host(linker, closure)?;
53-
bindings::cli::environment::add_to_linker_get_host(linker, closure)?;
54-
bindings::cli::stdin::add_to_linker_get_host(linker, closure)?;
55-
bindings::cli::stdout::add_to_linker_get_host(linker, closure)?;
56-
bindings::cli::stderr::add_to_linker_get_host(linker, closure)?;
57-
bindings::cli::terminal_input::add_to_linker_get_host(linker, closure)?;
58-
bindings::cli::terminal_output::add_to_linker_get_host(linker, closure)?;
59-
bindings::cli::terminal_stdin::add_to_linker_get_host(linker, closure)?;
60-
bindings::cli::terminal_stdout::add_to_linker_get_host(linker, closure)?;
61-
bindings::cli::terminal_stderr::add_to_linker_get_host(linker, closure)?;
62-
bindings::sockets::tcp::add_to_linker_get_host(linker, closure)?;
63-
bindings::sockets::tcp_create_socket::add_to_linker_get_host(linker, closure)?;
64-
bindings::sockets::udp::add_to_linker_get_host(linker, closure)?;
65-
bindings::sockets::udp_create_socket::add_to_linker_get_host(linker, closure)?;
66-
bindings::sockets::instance_network::add_to_linker_get_host(linker, closure)?;
67-
bindings::sockets::network::add_to_linker_get_host(linker, closure)?;
68-
bindings::sockets::ip_name_lookup::add_to_linker_get_host(linker, closure)?;
69-
}
38+
let linker = ctx.linker();
39+
use wasmtime_wasi::bindings;
40+
bindings::clocks::wall_clock::add_to_linker_get_host(linker, closure)?;
41+
bindings::clocks::monotonic_clock::add_to_linker_get_host(linker, closure)?;
42+
bindings::filesystem::types::add_to_linker_get_host(linker, closure)?;
43+
bindings::filesystem::preopens::add_to_linker_get_host(linker, closure)?;
44+
bindings::io::error::add_to_linker_get_host(linker, closure)?;
45+
bindings::io::poll::add_to_linker_get_host(linker, closure)?;
46+
bindings::io::streams::add_to_linker_get_host(linker, closure)?;
47+
bindings::random::random::add_to_linker_get_host(linker, closure)?;
48+
bindings::random::insecure::add_to_linker_get_host(linker, closure)?;
49+
bindings::random::insecure_seed::add_to_linker_get_host(linker, closure)?;
50+
bindings::cli::exit::add_to_linker_get_host(linker, closure)?;
51+
bindings::cli::environment::add_to_linker_get_host(linker, closure)?;
52+
bindings::cli::stdin::add_to_linker_get_host(linker, closure)?;
53+
bindings::cli::stdout::add_to_linker_get_host(linker, closure)?;
54+
bindings::cli::stderr::add_to_linker_get_host(linker, closure)?;
55+
bindings::cli::terminal_input::add_to_linker_get_host(linker, closure)?;
56+
bindings::cli::terminal_output::add_to_linker_get_host(linker, closure)?;
57+
bindings::cli::terminal_stdin::add_to_linker_get_host(linker, closure)?;
58+
bindings::cli::terminal_stdout::add_to_linker_get_host(linker, closure)?;
59+
bindings::cli::terminal_stderr::add_to_linker_get_host(linker, closure)?;
60+
bindings::sockets::tcp::add_to_linker_get_host(linker, closure)?;
61+
bindings::sockets::tcp_create_socket::add_to_linker_get_host(linker, closure)?;
62+
bindings::sockets::udp::add_to_linker_get_host(linker, closure)?;
63+
bindings::sockets::udp_create_socket::add_to_linker_get_host(linker, closure)?;
64+
bindings::sockets::instance_network::add_to_linker_get_host(linker, closure)?;
65+
bindings::sockets::network::add_to_linker_get_host(linker, closure)?;
66+
bindings::sockets::ip_name_lookup::add_to_linker_get_host(linker, closure)?;
7067
Ok(())
7168
}
7269

crates/factor-wasi/src/preview1.rs

-47
This file was deleted.

crates/factors-derive/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
7070
#[allow(clippy::needless_option_as_deref)]
7171
pub fn init(
7272
&mut self,
73-
mut linker: Option<&mut #wasmtime::component::Linker<#state_name>>,
74-
mut module_linker: Option<&mut #wasmtime::Linker<#state_name>>,
73+
linker: &mut #wasmtime::component::Linker<#state_name>,
7574
) -> #Result<()> {
7675
#(
7776
#Factor::init::<Self>(
7877
&mut self.#factor_names,
7978
#factors_path::InitContext::<Self, #factor_types>::new(
80-
linker.as_deref_mut(),
81-
module_linker.as_deref_mut(),
79+
linker,
8280
|state| &mut state.#factor_names,
8381
)
8482
)?;

crates/factors/src/factor.rs

+7-39
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::Context;
44

55
use crate::{
66
prepare::FactorInstanceBuilder, runtime_config::RuntimeConfigTracker, App, FactorRuntimeConfig,
7-
InstanceBuilders, Linker, ModuleLinker, PrepareContext, RuntimeConfigSource, RuntimeFactors,
7+
InstanceBuilders, Linker, PrepareContext, RuntimeConfigSource, RuntimeFactors,
88
};
99

1010
pub trait Factor: Any + Sized {
@@ -65,31 +65,18 @@ pub(crate) type GetDataFn<Facts, F> =
6565
/// An InitContext is passed to [`Factor::init`], giving access to the global
6666
/// common [`wasmtime::component::Linker`].
6767
pub struct InitContext<'a, T: RuntimeFactors, F: Factor> {
68-
pub(crate) linker: Option<&'a mut Linker<T>>,
69-
pub(crate) module_linker: Option<&'a mut ModuleLinker<T>>,
68+
pub(crate) linker: &'a mut Linker<T>,
7069
pub(crate) get_data: GetDataFn<T, F>,
7170
}
7271

7372
impl<'a, T: RuntimeFactors, F: Factor> InitContext<'a, T, F> {
7473
#[doc(hidden)]
75-
pub fn new(
76-
linker: Option<&'a mut Linker<T>>,
77-
module_linker: Option<&'a mut ModuleLinker<T>>,
78-
get_data: GetDataFn<T, F>,
79-
) -> Self {
80-
Self {
81-
linker,
82-
module_linker,
83-
get_data,
84-
}
74+
pub fn new(linker: &'a mut Linker<T>, get_data: GetDataFn<T, F>) -> Self {
75+
Self { linker, get_data }
8576
}
8677

87-
pub fn linker(&mut self) -> Option<&mut Linker<T>> {
88-
self.linker.as_deref_mut()
89-
}
90-
91-
pub fn module_linker(&mut self) -> Option<&mut ModuleLinker<T>> {
92-
self.module_linker.as_deref_mut()
78+
pub fn linker(&mut self) -> &mut Linker<T> {
79+
self.linker
9380
}
9481

9582
pub fn get_data_fn(&self) -> GetDataFn<T, F> {
@@ -104,26 +91,7 @@ impl<'a, T: RuntimeFactors, F: Factor> InitContext<'a, T, F> {
10491
) -> anyhow::Result<()>,
10592
) -> anyhow::Result<()>
10693
where {
107-
if let Some(linker) = self.linker.as_deref_mut() {
108-
add_to_linker(linker, self.get_data)
109-
} else {
110-
Ok(())
111-
}
112-
}
113-
114-
pub fn link_module_bindings(
115-
&mut self,
116-
add_to_linker: impl Fn(
117-
&mut ModuleLinker<T>,
118-
fn(&mut T::InstanceState) -> &mut FactorInstanceState<F>,
119-
) -> anyhow::Result<()>,
120-
) -> anyhow::Result<()>
121-
where {
122-
if let Some(linker) = self.module_linker.as_deref_mut() {
123-
add_to_linker(linker, self.get_data)
124-
} else {
125-
Ok(())
126-
}
94+
add_to_linker(self.linker, self.get_data)
12795
}
12896
}
12997

crates/factors/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub use crate::{
1616
};
1717

1818
pub type Linker<T> = wasmtime::component::Linker<<T as RuntimeFactors>::InstanceState>;
19-
pub type ModuleLinker<T> = wasmtime::Linker<<T as RuntimeFactors>::InstanceState>;
2019

2120
// Temporary wrappers while refactoring
2221
pub type App = spin_app::App<'static, spin_app::InertLoader>;

crates/factors/tests/smoke.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use spin_factor_key_value::{KeyValueFactor, MakeKeyValueStore};
88
use spin_factor_outbound_http::OutboundHttpFactor;
99
use spin_factor_outbound_networking::OutboundNetworkingFactor;
1010
use spin_factor_variables::{StaticVariables, VariablesFactor};
11-
use spin_factor_wasi::{preview1::WasiPreview1Factor, DummyFilesMounter, WasiFactor};
11+
use spin_factor_wasi::{DummyFilesMounter, WasiFactor};
1212
use spin_factors::{FactorRuntimeConfig, RuntimeConfigSource, RuntimeFactors};
1313
use spin_key_value_sqlite::{DatabaseLocation, KeyValueSqlite};
1414
use wasmtime_wasi_http::WasiHttpView;
1515

1616
#[derive(RuntimeFactors)]
1717
struct Factors {
1818
wasi: WasiFactor,
19-
wasi_p1: WasiPreview1Factor,
2019
variables: VariablesFactor,
2120
outbound_networking: OutboundNetworkingFactor,
2221
outbound_http: OutboundHttpFactor,
@@ -27,7 +26,6 @@ struct Factors {
2726
async fn main() -> anyhow::Result<()> {
2827
let mut factors = Factors {
2928
wasi: WasiFactor::new(DummyFilesMounter),
30-
wasi_p1: WasiPreview1Factor,
3129
variables: VariablesFactor::default(),
3230
outbound_networking: OutboundNetworkingFactor,
3331
outbound_http: OutboundHttpFactor,
@@ -48,11 +46,8 @@ async fn main() -> anyhow::Result<()> {
4846

4947
let engine = wasmtime::Engine::new(wasmtime::Config::new().async_support(true))?;
5048
let mut linker = wasmtime::component::Linker::new(&engine);
51-
let mut module_linker = wasmtime::Linker::new(&engine);
5249

53-
factors
54-
.init(Some(&mut linker), Some(&mut module_linker))
55-
.unwrap();
50+
factors.init(&mut linker).unwrap();
5651

5752
let configured_app = factors.configure_app(app, TestSource)?;
5853
let data = factors.build_store_data(&configured_app, "smoke-app")?;
@@ -82,9 +77,6 @@ async fn main() -> anyhow::Result<()> {
8277
let component = wasmtime::component::Component::new(&engine, component_bytes)?;
8378
let instance = linker.instantiate_async(&mut store, &component).await?;
8479

85-
let module = wasmtime::Module::new(&engine, b"(module)")?;
86-
let _module_instance = module_linker.instantiate_async(&mut store, &module).await?;
87-
8880
// Invoke handler
8981
let req = http::Request::get("/").body(Default::default()).unwrap();
9082
let mut wasi_http_view =

0 commit comments

Comments
 (0)