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

add memory #3

Merged
merged 2 commits into from
Nov 25, 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
22 changes: 19 additions & 3 deletions src/runtime/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::datex_values::Pointer;

pub struct Memory {
pointers: HashMap<[i8; 26], Pointer>, // all pointers
pointers: HashMap<[u8; 26], Pointer>, // all pointers
}

impl Memory {
Expand All @@ -13,11 +13,27 @@ impl Memory {
}
}

pub fn get_pointer_by_id(&mut self, address: [i8; 26]) -> Option<&mut Pointer> {
pub fn get_pointer_by_id(&mut self, address: [u8; 26]) -> Option<&mut Pointer> {
self.pointers.get_mut(&address)
}

pub fn store_pointer(&mut self, address: [i8; 26], pointer: Pointer) {
pub fn get_pointer_by_id_vec(&mut self, address: Vec<u8>) -> Option<&mut Pointer> {
let mut address_array: [u8; 26] = [0; 26];
for i in 0..26 {
address_array[i] = address[i];
}
self.get_pointer_by_id(address_array)
}

pub fn get_pointer_ids(&self) -> Vec<[u8; 26]> {
let mut ids: Vec<[u8; 26]> = Vec::new();
for id in self.pointers.keys() {
ids.push(*id);
}
ids
}

pub fn store_pointer(&mut self, address: [u8; 26], pointer: Pointer) {
self.pointers.insert(address, pointer);
}
}
10 changes: 6 additions & 4 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{cell::RefCell, rc::Rc};

use crate::{utils::{logger::{LoggerContext, Logger}, crypto::Crypto, rust_crypto::RustCrypto}, datex_values::ValueResult};

mod stack;
mod execution;
mod memory;
pub mod memory;

use self::{execution::execute, memory::Memory};

Expand All @@ -13,7 +15,7 @@ pub struct Runtime<'a> {
pub version: String,
pub ctx: &'a LoggerContext,
pub crypto: &'a dyn Crypto,
pub memory: Memory
pub memory: Rc<RefCell<Memory>>
}

impl Runtime<'_> {
Expand All @@ -25,7 +27,7 @@ impl Runtime<'_> {
version: VERSION.to_string(),
crypto,
ctx,
memory: Memory::new()
memory: Rc::new(RefCell::new(Memory::new()))
}
}

Expand All @@ -34,7 +36,7 @@ impl Runtime<'_> {
version: VERSION.to_string(),
crypto: &RustCrypto{},
ctx: &LoggerContext { log_redirect: None},
memory: Memory::new()
memory: Rc::new(RefCell::new(Memory::new()))
}
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

pub trait Crypto {
fn encrypt_aes(&self, buffer:&[u8]) -> Vec<u8>;
}
4 changes: 3 additions & 1 deletion tests/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use datex_core::{runtime::Runtime, utils::{logger::LoggerContext, buffers::{buffer_to_hex, hex_to_buffer, hex_to_buffer_advanced}, crypto::Crypto}};


Expand All @@ -7,7 +9,7 @@ use datex_core::{runtime::Runtime, utils::{logger::LoggerContext, buffers::{buff
#[test]
pub fn init_runtime() {
let runtime = Runtime::new();
assert_eq!(runtime.version, 1);
assert_eq!(runtime.version, env!("CARGO_PKG_VERSION"));
}


Expand Down