Skip to content

fix: free parameters to host functions when using the host_fn macro #77

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

Merged
merged 2 commits into from
Mar 21, 2025
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: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "extism-pdk"
version = "1.3.0"
version = "1.4.0"
edition = "2021"
authors = ["The Extism Authors", "[email protected]"]
license = "BSD-3-Clause"
Expand All @@ -12,9 +12,9 @@ description = "Extism Plug-in Development Kit (PDK) for Rust"
anyhow = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
extism-pdk-derive = { path = "./derive", version = "1.3.0" }
extism-manifest = { version = "1.2.0", optional = true }
extism-convert = { version = "1.2.0", features = ["extism-pdk-path"] }
extism-pdk-derive = { path = "./derive", version = "1.4.0" }
extism-manifest = { version = "1.10.0", optional = true }
extism-convert = { version = "1.10.0", features = ["extism-pdk-path"] }
base64 = "0.22.1"

[features]
Expand Down
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "extism-pdk-derive"
version = "1.3.0"
version = "1.4.0"
edition = "2021"
authors = ["The Extism Authors", "[email protected]"]
license = "BSD-3-Clause"
Expand Down
4 changes: 3 additions & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ pub fn host_fn(
match &*t.pat {
syn::Pat::Ident(i) => {
into_inputs
.push(quote!(extism_pdk::ToMemory::to_memory(&&#i)?.offset()));
.push(quote!(
extism_pdk::ManagedMemory::from(extism_pdk::ToMemory::to_memory(&&#i)?).offset()
));
}
_ => panic!("invalid host function argument"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use anyhow::Error;
pub use extism_convert::*;
pub use extism_convert::{FromBytes, FromBytesOwned, ToBytes};
pub use extism_pdk_derive::{host_fn, plugin_fn, shared_fn};
pub use memory::{Memory, MemoryPointer};
pub use memory::{ManagedMemory, Memory, MemoryPointer};
pub use to_memory::ToMemory;

#[cfg(feature = "http")]
Expand Down
40 changes: 40 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::*;

pub struct Memory(pub MemoryHandle);

pub struct ManagedMemory(pub Memory);

pub mod internal {
use super::*;

Expand Down Expand Up @@ -195,3 +197,41 @@ impl<T: FromBytesOwned> MemoryPointer<T> {
}
}
}

impl Drop for ManagedMemory {
fn drop(&mut self) {
internal::memory_free((self.0).0)
}
}

impl ManagedMemory {
pub fn new(mem: Memory) -> Self {
ManagedMemory(mem)
}

pub fn offset(&self) -> u64 {
self.0.offset()
}

pub fn len(&self) -> usize {
self.0.len()
}
}

impl From<Memory> for ManagedMemory {
fn from(value: Memory) -> Self {
ManagedMemory(value)
}
}

impl AsRef<Memory> for ManagedMemory {
fn as_ref(&self) -> &Memory {
&self.0
}
}

impl AsMut<Memory> for ManagedMemory {
fn as_mut(&mut self) -> &mut Memory {
&mut self.0
}
}
Loading