Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add bindings to length_unsafe #41

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading