Skip to content

Commit

Permalink
chore: add bindings to length_unsafe (#41)
Browse files Browse the repository at this point in the history
* chore: add bindings to length_unsafe

* cleanup: use length_unsafe for result of http request
  • Loading branch information
zshipko authored Jan 3, 2024
1 parent 44370c5 commit 5bef721
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/extism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern "C" {
pub fn input_load_u8(offs: u64) -> u8;
pub fn input_load_u64(offs: u64) -> u64;
pub fn length(offs: u64) -> u64;
pub fn length_unsafe(offs: u64) -> u64;
pub fn alloc(length: u64) -> u64;
pub fn free(offs: u64);
pub fn output_set(offs: u64, length: u64);
Expand Down Expand Up @@ -37,7 +38,7 @@ pub unsafe fn load(offs: u64, data: &mut [u8]) {

let mut_ptr = data.as_mut_ptr() as *mut u64;
for chunk_idx in 0..chunk_count {
let x = load_u64(offs + (chunk_idx<<3) as u64);
let x = load_u64(offs + (chunk_idx << 3) as u64);
mut_ptr.add(chunk_idx).write(x);
}

Expand Down Expand Up @@ -89,10 +90,7 @@ pub unsafe fn store(offs: u64, data: &[u8]) {

let ptr = data.as_ptr() as *const u64;
for chunk_idx in 0..chunk_count {
store_u64(
offs + (chunk_idx << 3) as u64,
ptr.add(chunk_idx).read(),
);
store_u64(offs + (chunk_idx << 3) as u64, ptr.add(chunk_idx).read());
}

let remainder = len & 7;
Expand Down
2 changes: 1 addition & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn request<T: ToMemory>(
let data = body.as_ref().map(|x| x.offset()).unwrap_or(0);
let offs = unsafe { extism::http_request(req.offset(), data) };
let status = unsafe { extism::http_status_code() };
let len = unsafe { extism::length(offs) };
let len = unsafe { extism::length_unsafe(offs) };
Ok(HttpResponse {
memory: Memory(MemoryHandle {
offset: offs,
Expand Down
7 changes: 7 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ pub mod internal {
data
}

/// Get the length of the memory handle stored at the given offset, this will return 0 if called on a non-handle pointer
pub fn memory_length(offs: u64) -> u64 {
unsafe { extism::length(offs) }
}

/// Get the length of the memory handle stored at the given offset, this may return garbage if called on a non-handle pointer
pub fn memory_length_unsafe(offs: u64) -> u64 {
unsafe { extism::length_unsafe(offs) }
}

/// Load data from memory into a `u8` slice
pub fn load(handle: MemoryHandle, mut buf: impl AsMut<[u8]>) {
let buf = buf.as_mut();
Expand Down

0 comments on commit 5bef721

Please sign in to comment.