Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use {
serde::Deserialize,
std::{
collections::HashMap,
env, fs,
io::Cursor,
iter,
fs, iter,
ops::Deref,
path::{Path, PathBuf},
str,
Expand All @@ -34,6 +32,7 @@ mod abi;
mod bindgen;
mod bindings;
pub mod command;
mod link;
mod prelink;
#[cfg(feature = "pyo3")]
mod python;
Expand Down Expand Up @@ -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)?
Expand Down
30 changes: 30 additions & 0 deletions src/link.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::io::Cursor;

use anyhow::Result;

use crate::Library;

pub fn link_libraries(libraries: &[Library]) -> Result<Vec<u8>> {
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"
))))?,
)?;

linker.encode().map_err(|e| anyhow::anyhow!(e))
}
14 changes: 6 additions & 8 deletions src/prelink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
collections::{HashMap, HashSet},
fs::{self},
io::{self, Cursor},
io::Cursor,
ops::Deref,
path::{Path, PathBuf},
};
Expand All @@ -21,7 +21,7 @@ static NATIVE_EXTENSION_SUFFIX: &str = ".cpython-312-wasm32-wasi.so";
type ConfigsMatchedWorlds<'a> =
IndexMap<String, (ConfigContext<ComponentizePyConfig>, Option<&'a str>)>;

pub fn embedded_python_standard_library() -> Result<TempDir, io::Error> {
pub fn embedded_python_standard_library() -> Result<TempDir> {
// Untar the embedded copy of the Python standard library into a temporary directory
let stdlib = tempfile::tempdir()?;

Expand All @@ -35,7 +35,7 @@ pub fn embedded_python_standard_library() -> Result<TempDir, io::Error> {
Ok(stdlib)
}

pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {
pub fn embedded_helper_utils() -> Result<TempDir> {
// Untar the embedded copy of helper utilities into a temporary directory
let bundled = tempfile::tempdir()?;

Expand All @@ -49,9 +49,7 @@ pub fn embedded_helper_utils() -> Result<TempDir, io::Error> {
Ok(bundled)
}

pub fn bundle_libraries(
library_path: Vec<(&str, Vec<PathBuf>)>,
) -> Result<Vec<Library>, anyhow::Error> {
pub fn bundle_libraries(library_path: Vec<(&str, Vec<PathBuf>)>) -> Result<Vec<Library>> {
let mut libraries = vec![
Library {
name: "libcomponentize_py_runtime.so".into(),
Expand Down Expand Up @@ -152,7 +150,7 @@ 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<Library>), anyhow::Error> {
) -> Result<(ConfigsMatchedWorlds<'a>, Vec<Library>)> {
let mut raw_configs: Vec<ConfigContext<RawComponentizePyConfig>> = Vec::new();
let mut library_path: Vec<(&str, Vec<PathBuf>)> = Vec::with_capacity(python_path.len());
for path in python_path {
Expand Down Expand Up @@ -219,7 +217,7 @@ fn search_directory(
libraries: &mut Vec<PathBuf>,
configs: &mut Vec<ConfigContext<RawComponentizePyConfig>>,
modules_seen: &mut HashSet<String>,
) -> Result<(), anyhow::Error> {
) -> Result<()> {
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)?;
Expand Down
33 changes: 18 additions & 15 deletions src/stubwasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use wasmparser::{FuncType, Parser, Payload, TypeRef};

use crate::Library;

pub fn link_stub_modules(
libraries: Vec<Library>,
) -> Result<Option<(Vec<u8>, impl Fn(u32) -> u32)>, Error> {
type LinkedStubModules = Option<(Vec<u8>, Box<dyn Fn(u32) -> u32>)>;

pub fn link_stub_modules(libraries: Vec<Library>) -> Result<LinkedStubModules, Error> {
let mut wasi_imports = HashMap::new();
let mut linker = wit_component::Linker::default()
.validate(true)
Expand Down Expand Up @@ -47,18 +47,21 @@ pub fn link_stub_modules(
let new_adapter_count = u32::try_from(wasi_imports.len())?;
assert!(new_adapter_count >= old_adapter_count);

Ok(Some((component, move |index: u32| {
if index == 0 {
// `main` module
0
} else if index <= new_adapter_count {
// adapter module
old_adapter_count
} else {
// one of the other kinds of module
index + old_adapter_count - new_adapter_count
}
})))
Ok(Some((
component,
Box::new(move |index: u32| {
if index == 0 {
// `main` module
0
} else if index <= new_adapter_count {
// adapter module
old_adapter_count
} else {
// one of the other kinds of module
index + old_adapter_count - new_adapter_count
}
}),
)))
}

fn add_wasi_imports<'a>(
Expand Down
2 changes: 1 addition & 1 deletion src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct MyFunction<'a> {
pub wit_kind: wit_parser::FunctionKind,
}

impl<'a> MyFunction<'a> {
impl MyFunction<'_> {
fn key(&self) -> WorldKey {
if let Some(interface) = self.interface.as_ref() {
WorldKey::Interface(interface.id)
Expand Down
2 changes: 2 additions & 0 deletions src/test/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_local_definitions)]

use {
super::{Ctx, Tester, SEED},
anyhow::{anyhow, Error, Result},
Expand Down
Loading