Skip to content

Commit

Permalink
move CPointer from program to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed May 1, 2024
1 parent dc02edc commit 0174a25
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion x/programs/rust/wasmlanche-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod memory;
mod program;

pub use self::{
memory::from_host_ptr,
memory::{from_host_ptr, CPointer},
params::{serialize_param, Params},
program::Program,
};
Expand Down
5 changes: 4 additions & 1 deletion x/programs/rust/wasmlanche-sdk/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
//! the program. These methods are unsafe as should be used
//! with caution.
use crate::{program::CPointer, state::Error as StateError};
use crate::{state::Error as StateError};
use borsh::{from_slice, BorshDeserialize};
use std::{alloc::Layout, cell::RefCell, collections::HashMap};

#[repr(C)]
pub struct CPointer(pub *const u8, pub usize);

thread_local! {
/// Map of pointer to the length of its content on the heap
static GLOBAL_STORE: RefCell<HashMap<*const u8, usize>> = RefCell::new(HashMap::new());
Expand Down
2 changes: 1 addition & 1 deletion x/programs/rust/wasmlanche-sdk/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{memory::to_ffi_ptr, program::CPointer, state::Error as StateError, Error};
use crate::{memory::{to_ffi_ptr, CPointer}, state::Error as StateError, Error};
use borsh::BorshSerialize;

#[macro_export]
Expand Down
7 changes: 1 addition & 6 deletions x/programs/rust/wasmlanche-sdk/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::hash::Hash;

use borsh::{BorshDeserialize, BorshSerialize};

use crate::{
memory::to_ffi_ptr,
memory::{to_ffi_ptr, CPointer},
state::{Error as StateError, Key, State},
Params,
};
Expand Down Expand Up @@ -59,9 +57,6 @@ impl Program {
}
}

#[repr(C)]
pub struct CPointer(pub *const u8, pub usize);

#[link(wasm_import_module = "program")]
extern "C" {
#[link_name = "call_program"]
Expand Down
8 changes: 4 additions & 4 deletions x/programs/rust/wasmlanche-sdk/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ macro_rules! call_host_fn {

mod host {
use super::{Key, Program};
use crate::{memory::to_ffi_ptr, program::CPointer, state::Error};
use crate::{memory::{to_ffi_ptr, CPointer}, state::Error};

/// Persists the bytes at key on the host storage.
pub(super) unsafe fn put_bytes(caller: &Program, key: &Key, bytes: &[u8]) -> Result<(), Error>
Expand All @@ -226,15 +226,15 @@ mod host {
pub(super) unsafe fn get_bytes(caller: &Program, key: &Key) -> Result<i32, Error> {
Ok(call_host_fn! {
wasm_import_module = "state"
link_name = "get"
args = (caller, key)
link_name = "get"
args = (caller, key)
})
}

/// Deletes the bytes at key ptr from the host storage
pub(super) unsafe fn delete_bytes(caller: &Program, key: &Key) -> Result<(), Error> {
match call_host_fn! {
wasm_import_module = "state"
wasm_import_module = "state"
link_name = "delete"
args = (caller, key)
} {
Expand Down

0 comments on commit 0174a25

Please sign in to comment.