Skip to content

Commit

Permalink
fully migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Dec 22, 2023
1 parent d9a3a1a commit f5e4871
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/wasix/src/bin_factory/binary_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl BinaryPackageCommand {
}

pub fn hash(&self) -> &ModuleHash {
self.hash.get_or_init(|| ModuleHash::sha256(self.atom()))
self.hash.get_or_init(|| ModuleHash::hash(self.atom()))
}
}

Expand Down Expand Up @@ -144,9 +144,9 @@ impl BinaryPackage {
pub fn hash(&self) -> ModuleHash {
*self.hash.get_or_init(|| {
if let Some(entry) = self.entrypoint_bytes() {
ModuleHash::sha256(entry)
ModuleHash::hash(entry)
} else {
ModuleHash::sha256(self.package_name.as_bytes())
ModuleHash::hash(self.package_name.as_bytes())
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn load_module(
module_cache: &(dyn ModuleCache + Send + Sync),
wasm: &[u8],
) -> Result<Module, anyhow::Error> {
let hash = ModuleHash::xxhash(wasm);
let hash = ModuleHash::hash(wasm);
let result = module_cache.load(hash, engine).await;

match result {
Expand Down
18 changes: 7 additions & 11 deletions lib/wasix/src/runtime/module_cache/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,33 @@ impl CacheError {
}
}

/// The SHA-256 hash of a WebAssembly module.
/// The XXHash hash of a WebAssembly module.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleHash([u8; 8]);

impl ModuleHash {
/// Create a new [`ModuleHash`] from the raw SHA-256 hash.
/// Create a new [`ModuleHash`] from the raw XXHash hash.
pub fn from_bytes(key: [u8; 8]) -> Self {
ModuleHash(key)
}

/// Parse a sha256 hash from a hex-encoded string.
/// Parse a XXHash hash from a hex-encoded string.
pub fn parse_hex(hex_str: &str) -> Result<Self, hex::FromHexError> {
let mut hash = [0_u8; 8];
hex::decode_to_slice(hex_str, &mut hash)?;
Ok(Self(hash))
}

/// Generate a new [`ModuleCache`] based on the SHA-256 hash of some bytes.
pub fn sha256(wasm: impl AsRef<[u8]>) -> Self {
Self::xxhash(wasm)
}

pub fn xxhash(wasm: impl AsRef<[u8]>) -> Self {
/// Generate a new [`ModuleCache`] based on the XXHash hash of some bytes.
pub fn hash(wasm: impl AsRef<[u8]>) -> Self {
let wasm = wasm.as_ref();

let hash = xxhash_rust::xxh64::xxh64(wasm, 0);

Self(hash.to_ne_bytes())
}

/// Get the raw SHA-256 hash.
/// Get the raw XXHash hash.
pub fn as_bytes(self) -> [u8; 8] {
self.0
}
Expand Down Expand Up @@ -190,7 +186,7 @@ mod tests {
let wasm = b"\0asm...";
let raw = [0x0c, 0xc7, 0x88, 0x60, 0xd4, 0x14, 0x71, 0x4c];

let hash = ModuleHash::sha256(wasm);
let hash = ModuleHash::hash(wasm);

assert_eq!(hash.as_bytes(), raw);
}
Expand Down

0 comments on commit f5e4871

Please sign in to comment.