Skip to content

Commit

Permalink
Refactor Module::from_buf to Module::from_vec to avoid cloning (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD authored Oct 21, 2024
1 parent 274596c commit 563e244
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "");
let module = Module::from_vec(&runtime, binary, "");
assert!(module.is_ok());
let module = module.unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "add");
let module = Module::from_vec(&runtime, binary, "add");
assert!(module.is_ok());

let module = &module.unwrap();
Expand Down Expand Up @@ -165,7 +165,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "");
let module = Module::from_vec(&runtime, binary, "");
assert!(module.is_ok());

let module = &module.unwrap();
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "add");
let module = Module::from_vec(&runtime, binary, "add");
assert!(module.is_ok());

let module = &module.unwrap();
Expand Down
11 changes: 5 additions & 6 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Module {
let mut binary: Vec<u8> = Vec::new();
wasm_file.read_to_end(&mut binary)?;

Self::from_buf(runtime, &binary, name)
Self::from_vec(runtime, binary, name)
}

/// compile a module int the given buffer,
Expand All @@ -52,8 +52,7 @@ impl Module {
///
/// If the file does not exist or the file cannot be read, an `RuntimeError::WasmFileFSError` will be returned.
/// If the wasm file is not a valid wasm file, an `RuntimeError::CompilationError` will be returned.
pub fn from_buf(_runtime: &Runtime, buf: &[u8], name: &str) -> Result<Self, RuntimeError> {
let mut content = buf.to_vec();
pub fn from_vec(_runtime: &Runtime, mut content: Vec<u8>, name: &str) -> Result<Self, RuntimeError> {
let mut error_buf: [c_char; DEFAULT_ERROR_BUF_SIZE] = [0; DEFAULT_ERROR_BUF_SIZE];
let module = unsafe {
wasm_runtime_load(
Expand Down Expand Up @@ -222,7 +221,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "");
let module = Module::from_vec(&runtime, binary, "");
assert!(module.is_ok());
}

Expand Down Expand Up @@ -255,7 +254,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "add");
let module = Module::from_vec(&runtime, binary, "add");
assert!(module.is_ok());
let mut module = module.unwrap();

Expand Down Expand Up @@ -287,7 +286,7 @@ mod tests {
];
let binary = binary.into_iter().map(|c| c as u8).collect::<Vec<u8>>();

let module = Module::from_buf(&runtime, &binary, "add")?;
let module = Module::from_vec(&runtime, binary, "add")?;

assert_eq!(module.get_name(), "add");

Expand Down

0 comments on commit 563e244

Please sign in to comment.