Skip to content

Commit

Permalink
feat: 2 constructors for ModuleCache -- one that takes a ModuleBuilde…
Browse files Browse the repository at this point in the history
…r and one that constructs the default ModuleBuilder (#145)
  • Loading branch information
mattyg authored Jan 16, 2025
1 parent 81dce1e commit af27d24
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

- Modify the `ModuleCache::new` constructor to no longer take a `ModuleBuilder` parameter.
- Add a `ModuleCache::new_with_builder` constructor which does take a `ModuleBuilder` parameter.

## [0.0.98] - 2025-01-15

### Changed
Expand Down
8 changes: 7 additions & 1 deletion crates/host/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ pub struct ModuleCache {
}

impl ModuleCache {
pub fn new(builder: ModuleBuilder, filesystem_path: Option<PathBuf>) -> Self {
/// Construct a ModuleCache with the default ModuleBuilder
pub fn new(filesystem_path: Option<PathBuf>) -> Self {
Self::new_with_builder(ModuleBuilder::new(make_engine), filesystem_path)
}

/// Construct a ModuleCache with a custom ModuleBuilder
pub fn new_with_builder(builder: ModuleBuilder, filesystem_path: Option<PathBuf>) -> Self {
let cache = Arc::new(RwLock::new(InMemoryModuleCache::default()));
ModuleCache {
cache,
Expand Down
17 changes: 5 additions & 12 deletions crates/host/src/module/wasmer_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn get_ios_module_from_file(path: &Path) -> Result<Module, DeserializeError>
#[cfg(test)]
mod tests {
use super::make_engine;
use crate::module::{builder::ModuleBuilder, CacheKey, ModuleCache, PlruCache};
use crate::module::{CacheKey, ModuleCache, PlruCache};
use std::io::Write;
use tempfile::TempDir;
use wasmer::Module;
Expand All @@ -96,9 +96,7 @@ mod tests {
0x70, 0x30,
];
let tmp_fs_cache_dir = TempDir::new().unwrap();
let module_builder = ModuleBuilder::new(make_engine);
let module_cache =
ModuleCache::new(module_builder, Some(tmp_fs_cache_dir.path().to_owned()));
let module_cache = ModuleCache::new(Some(tmp_fs_cache_dir.path().to_owned()));
assert!(module_cache
.filesystem_path
.clone()
Expand Down Expand Up @@ -138,8 +136,7 @@ mod tests {
0x61, 0x64, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x02, 0x07, 0x01, 0x00, 0x01, 0x00, 0x02,
0x70, 0x30,
];
let module_builder = ModuleBuilder::new(make_engine);
let module_cache = ModuleCache::new(module_builder, None);
let module_cache = ModuleCache::new(None);
assert!(module_cache.cache.read().cache.is_empty());

let key: CacheKey = [0u8; 32];
Expand All @@ -165,9 +162,7 @@ mod tests {
0x70, 0x30,
];
let tmp_fs_cache_dir = TempDir::new().unwrap();
let module_builder = ModuleBuilder::new(make_engine);
let module_cache =
ModuleCache::new(module_builder, Some(tmp_fs_cache_dir.path().to_owned()));
let module_cache = ModuleCache::new(Some(tmp_fs_cache_dir.path().to_owned()));
let key: CacheKey = [0u8; 32];

// Build module, serialize, save directly to filesystem
Expand Down Expand Up @@ -217,9 +212,7 @@ mod tests {
let bad_serialized_wasm = vec![0x00];

let tmp_fs_cache_dir = TempDir::new().unwrap();
let module_builder = ModuleBuilder::new(make_engine);
let module_cache =
ModuleCache::new(module_builder, Some(tmp_fs_cache_dir.path().to_owned()));
let module_cache = ModuleCache::new(Some(tmp_fs_cache_dir.path().to_owned()));
let key: CacheKey = [0u8; 32];

// Build module, serialize, save directly to filesystem
Expand Down
20 changes: 10 additions & 10 deletions test-crates/tests/src/wasms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ impl TestWasm {
// This will error if the cache is already initialized
// which could happen if two tests are running in parallel.
// It doesn't matter which one wins, so we just ignore the error.
let _did_init_ok =
self.module_cache(metered)
.set(parking_lot::RwLock::new(ModuleCache::new(
ModuleBuilder::new(if metered {
cranelift_fn
} else {
compiler_fn_unmetered
}),
None,
)));
let _did_init_ok = self.module_cache(metered).set(parking_lot::RwLock::new(
ModuleCache::new_with_builder(
ModuleBuilder::new(if metered {
cranelift_fn
} else {
compiler_fn_unmetered
}),
None,
),
));

// Just recurse now that the cache is initialized.
self.module(metered)
Expand Down

0 comments on commit af27d24

Please sign in to comment.