From 60d5951eebde8b6a5f9b002328ea0f99adf994bd Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Mon, 25 Nov 2024 21:16:04 +0100 Subject: [PATCH] add memory --- src/runtime/memory.rs | 22 +++++++++++++++++++--- src/runtime/mod.rs | 10 ++++++---- src/utils/crypto.rs | 1 + tests/runtime.rs | 4 +++- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/runtime/memory.rs b/src/runtime/memory.rs index 153df06..fe40fdd 100644 --- a/src/runtime/memory.rs +++ b/src/runtime/memory.rs @@ -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 { @@ -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) -> 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); } } \ No newline at end of file diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 835e0d3..1d323ed 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -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}; @@ -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> } impl Runtime<'_> { @@ -25,7 +27,7 @@ impl Runtime<'_> { version: VERSION.to_string(), crypto, ctx, - memory: Memory::new() + memory: Rc::new(RefCell::new(Memory::new())) } } @@ -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())) } } diff --git a/src/utils/crypto.rs b/src/utils/crypto.rs index e715693..4500b6e 100644 --- a/src/utils/crypto.rs +++ b/src/utils/crypto.rs @@ -1,3 +1,4 @@ + pub trait Crypto { fn encrypt_aes(&self, buffer:&[u8]) -> Vec; } \ No newline at end of file diff --git a/tests/runtime.rs b/tests/runtime.rs index 3f20b95..ec929bc 100644 --- a/tests/runtime.rs +++ b/tests/runtime.rs @@ -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}}; @@ -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")); }