Skip to content

Commit 34c36e8

Browse files
committed
feat(clarity-wasm): add a linked function for skipping a extra list value in a serialized tuple
1 parent 7160418 commit 34c36e8

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,7 @@ fn link_host_functions(linker: &mut Linker<ClarityWasmContext>) -> Result<(), Er
20232023
link_principal_of_fn(linker)?;
20242024
link_save_constant_fn(linker)?;
20252025
link_load_constant_fn(linker)?;
2026+
link_skip_list(linker)?;
20262027

20272028
link_log(linker)
20282029
}
@@ -6068,6 +6069,44 @@ fn link_load_constant_fn(linker: &mut Linker<ClarityWasmContext>) -> Result<(),
60686069
})
60696070
}
60706071

6072+
fn link_skip_list<T>(linker: &mut Linker<T>) -> Result<(), Error> {
6073+
linker
6074+
.func_wrap(
6075+
"clarity",
6076+
"skip_list",
6077+
|mut caller: Caller<'_, T>, offset_beg: i32, offset_end: i32| {
6078+
let memory = caller
6079+
.get_export("memory")
6080+
.and_then(|export| export.into_memory())
6081+
.ok_or(Error::Wasm(WasmError::MemoryNotFound))?;
6082+
6083+
// we will read the remaining serialized buffer here, and start it with the list type prefix
6084+
let mut serialized_buffer = vec![0u8; (offset_end - offset_beg) as usize + 1];
6085+
serialized_buffer[0] = super::types::serialization::TypePrefix::List as u8;
6086+
memory
6087+
.read(
6088+
&mut caller,
6089+
offset_beg as usize,
6090+
&mut serialized_buffer[1..],
6091+
)
6092+
.map_err(|e| Error::Wasm(WasmError::Runtime(e.into())))?;
6093+
6094+
match Value::deserialize_read_count(&mut serialized_buffer.as_slice(), None, false)
6095+
{
6096+
Ok((_, bytes_read)) => Ok(offset_beg + bytes_read as i32 - 1),
6097+
Err(_) => Ok(0),
6098+
}
6099+
},
6100+
)
6101+
.map(|_| ())
6102+
.map_err(|e| {
6103+
Error::Wasm(WasmError::UnableToLinkHostFunction(
6104+
"skip_list".to_string(),
6105+
e,
6106+
))
6107+
})
6108+
}
6109+
60716110
/// Link host-interface function, `log`, into the Wasm module.
60726111
/// This function is used for debugging the Wasm, and should not be called in
60736112
/// production.

0 commit comments

Comments
 (0)