brokkr

Bytecode virtual machine for Valhalla.
git clone git://git.knutsen.co/brokkr
Log | Files | Refs | README | LICENSE

commit 9a50799821ec4ea2a3b7de85a21233f001694e9a
parent bf94eb9d2678ca1b9148520ea07c66186395cd47
Author: Demonstrandum <moi@knutsen.co>
Date:   Fri, 21 Feb 2020 15:16:25 +0000

Add way to return reference to structure from .

Diffstat:
Msrc/vm/address.rs | 21+++++++++++++++++++--
Msrc/vm/unmarshal.rs | 7++++++-
2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/vm/address.rs b/src/vm/address.rs @@ -1,4 +1,5 @@ use std::fmt; +use num_traits::FromPrimitive; #[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct Address(pub usize); @@ -12,7 +13,7 @@ pub struct Address(pub usize); #[allow(clippy::inline_always)] impl Address { #[inline(always)] - pub fn new<T : Copy>(structure : T) -> Self { + pub fn new<T>(structure : T) -> Self { Self(Box::into_raw(Box::new(structure)) as usize) } @@ -24,7 +25,23 @@ impl Address { *(self.0 as *mut T) } - // For temporary use. + /// # Safety + /// This function dereferences a raw pointer. + #[inline(always)] + #[must_use] + pub unsafe fn reference<T>(self) -> &'static T { + &*(self.0 as *mut T) + } + + /// Get value of self.0, if it does not represent a pointer, + /// and instead is a value such as f64, stored as a usize. + #[inline(always)] + #[must_use] + pub fn value<T>(self) -> T { + unsafe { std::mem::transmute_copy(&self.0) } + } + + /// Null-pointer, for temporary use. #[inline(always)] #[must_use] pub fn null() -> Self { Address(0) } diff --git a/src/vm/unmarshal.rs b/src/vm/unmarshal.rs @@ -59,6 +59,12 @@ mod eat { (i + size, padded) } + #[derive(Debug)] + pub struct Egg { + a : f64, + b : String + } + fn constant(mut i : usize, bytes : &ByteSlice) -> (usize, Address) { let const_type = bytes[i]; i += 1; @@ -79,7 +85,6 @@ mod eat { // Store string on heap, `Address` holds a raw pointer to it. let string = Address::new(std::str::from_utf8(&bytes[i..i + str_len]) .expect("Invalid utf8 bytes in string. Bad bytecode.")); - (i + str_len, string) } _ => panic!(format!(