diff --git a/src/lib.rs b/src/lib.rs index 9f945a7..144b27a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,7 @@ use { serde::Deserialize, std::{ collections::HashMap, - env, fs, - io::Cursor, - iter, + fs, iter, ops::Deref, path::{Path, PathBuf}, str, @@ -34,6 +32,7 @@ mod abi; mod bindgen; mod bindings; pub mod command; +mod link; mod prelink; #[cfg(feature = "pyo3")] mod python; @@ -326,29 +325,7 @@ pub async fn componentize( dl_openable: false, }); - // Link all the libraries (including any native extensions) into a single component. - let mut linker = wit_component::Linker::default() - .validate(true) - .use_built_in_libdl(true); - - for Library { - name, - module, - dl_openable, - } in &libraries - { - linker = linker.library(name, module, *dl_openable)?; - } - - linker = linker.adapter( - "wasi_snapshot_preview1", - &zstd::decode_all(Cursor::new(include_bytes!(concat!( - env!("OUT_DIR"), - "/wasi_snapshot_preview1.reactor.wasm.zst" - ))))?, - )?; - - let component = linker.encode()?; + let component = link::link_libraries(&libraries)?; let stubbed_component = if stub_wasi { stubwasi::link_stub_modules(libraries)? diff --git a/src/link.rs b/src/link.rs new file mode 100644 index 0000000..308322a --- /dev/null +++ b/src/link.rs @@ -0,0 +1,30 @@ +use std::io::Cursor; + +use anyhow::Result; + +use crate::Library; + +pub fn link_libraries(libraries: &[Library]) -> Result> { + let mut linker = wit_component::Linker::default() + .validate(true) + .use_built_in_libdl(true); + + for Library { + name, + module, + dl_openable, + } in libraries + { + linker = linker.library(name, module, *dl_openable)?; + } + + linker = linker.adapter( + "wasi_snapshot_preview1", + &zstd::decode_all(Cursor::new(include_bytes!(concat!( + env!("OUT_DIR"), + "/wasi_snapshot_preview1.reactor.wasm.zst" + ))))?, + )?; + + return linker.encode().map_err(|e| anyhow::anyhow!(e)); +} diff --git a/src/prelink.rs b/src/prelink.rs index ce40684..1f9df4a 100644 --- a/src/prelink.rs +++ b/src/prelink.rs @@ -8,7 +8,7 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Error, Result}; use indexmap::IndexMap; use tar::Archive; use tempfile::TempDir; @@ -21,7 +21,7 @@ static NATIVE_EXTENSION_SUFFIX: &str = ".cpython-312-wasm32-wasi.so"; type ConfigsMatchedWorlds<'a> = IndexMap, Option<&'a str>)>; -pub fn embedded_python_standard_library() -> Result { +pub fn embedded_python_standard_library() -> Result { // Untar the embedded copy of the Python standard library into a temporary directory let stdlib = tempfile::tempdir()?; @@ -50,8 +50,8 @@ pub fn embedded_helper_utils() -> Result { } pub fn bundle_libraries( - library_path: Vec<(&str, Vec)>, -) -> Result, anyhow::Error> { + library_path: Vec<(&str, Vec)>, +) -> Result, Error> { let mut libraries = vec![ Library { name: "libcomponentize_py_runtime.so".into(), @@ -152,9 +152,10 @@ pub fn search_for_libraries_and_configs<'a>( python_path: &'a Vec<&'a str>, module_worlds: &'a [(&'a str, &'a str)], world: Option<&'a str>, -) -> Result<(ConfigsMatchedWorlds<'a>, Vec), anyhow::Error> { - let mut raw_configs: Vec> = Vec::new(); - let mut library_path: Vec<(&str, Vec)> = Vec::with_capacity(python_path.len()); +) -> Result<(ConfigsMatchedWorlds<'a>, Vec), Error> { + let mut raw_configs: Vec> = Vec::new(); + let mut library_path: Vec<(&str, Vec)> = + Vec::with_capacity(python_path.len()); for path in python_path { let mut libraries = Vec::new(); search_directory( @@ -219,7 +220,7 @@ fn search_directory( libraries: &mut Vec, configs: &mut Vec>, modules_seen: &mut HashSet, -) -> Result<(), anyhow::Error> { +) -> Result<(), Error> { if path.is_dir() { for entry in fs::read_dir(path).with_context(|| path.display().to_string())? { search_directory(root, &entry?.path(), libraries, configs, modules_seen)?; diff --git a/src/stubwasi.rs b/src/stubwasi.rs index 5305bad..1dae27a 100644 --- a/src/stubwasi.rs +++ b/src/stubwasi.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use anyhow::{bail, Error}; +use anyhow::{bail, Result}; use wasm_convert::IntoValType; use wasm_encoder::{ CodeSection, ExportKind, ExportSection, Function, FunctionSection, Instruction as Ins, Module, @@ -12,7 +12,7 @@ use crate::Library; pub fn link_stub_modules( libraries: Vec, -) -> Result, impl Fn(u32) -> u32)>, Error> { +) -> Result, impl Fn(u32) -> u32)>> { let mut wasi_imports = HashMap::new(); let mut linker = wit_component::Linker::default() .validate(true) @@ -64,7 +64,7 @@ pub fn link_stub_modules( fn add_wasi_imports<'a>( module: &'a [u8], imports: &mut HashMap<&'a str, HashMap<&'a str, FuncType>>, -) -> Result<(), Error> { +) -> Result<()> { let mut types = Vec::new(); for payload in Parser::new(0).parse_all(module) { match payload? {