Skip to content
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
610 changes: 210 additions & 400 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions laythe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "laythe"
version = "0.1.0"
authors = ["John Chabot <johnchabot2013@gmail.com>"]
edition = "2021"
edition = "2024"

[features]
jemalloc = ["jemallocator"]
Expand All @@ -16,4 +16,4 @@ laythe_vm = { path = "../laythe_vm", features=[] }
# laythe_vm features
# debug

jemallocator = { version = "0.5.0", optional = true }
jemallocator = { version = "0.5.4", optional = true }
4 changes: 2 additions & 2 deletions laythe_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "laythe_core"
version = "0.1.0"
authors = ["John Chabot <johnchabot2013@gmail.com>"]
edition = "2021"
edition = "2024"

[features]
nan_boxing = []
Expand All @@ -19,4 +19,4 @@ path = "src/lib.rs"
[dependencies]
laythe_env = { path = "../laythe_env" }
fnv = "1.0.7"
hashbrown = "0.14.5"
hashbrown = "0.15.4"
4 changes: 2 additions & 2 deletions laythe_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ impl Chunk {
/// This method assumes the index comes from a trusted
/// source that is inbounds.
#[inline]
pub unsafe fn get_constant_unchecked(&self, offset: usize) -> Value {
pub unsafe fn get_constant_unchecked(&self, offset: usize) -> Value { unsafe {
*self.constants.get_unchecked(offset)
}
}}

/// Get the line number at a token offset
///
Expand Down
4 changes: 2 additions & 2 deletions laythe_core/src/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ impl<T, H> Array<T, H> {
/// whichever object hold this slice also hold a reference to
/// the Array itself in order to keep it alive. This is primarily
/// to use rust method requiring a lifetime, typically for iterators
pub unsafe fn deref_static(&self) -> &'static [T] {
pub unsafe fn deref_static(&self) -> &'static [T] { unsafe {
slice::from_raw_parts(self.as_ptr(), self.len())
}
}}

/// Retrieve a pointer data array
#[inline]
Expand Down
30 changes: 19 additions & 11 deletions laythe_core/src/collections/shared_vector/raw_shared_vector.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
VecBuilder,
align_utils::{
get_vector_cap_offset, get_vector_len_offset, get_vector_offset, make_vector_layout,
},
managed::{AllocResult, Allocate, DebugHeap, DebugWrap, Manage, Mark, Marked, Trace, Unmark},
utils::{msb_set, set_msb, strip_msb},
VecBuilder,
};
use ptr::NonNull;
use std::{
Expand Down Expand Up @@ -137,14 +137,16 @@ impl<T, H> RawSharedVector<T, H> {
pub unsafe fn read_len<L: Copy>(&self) -> L {
#[allow(clippy::cast_ptr_alignment)]
let count = get_vector_len_offset::<H>();
*(self.ptr.as_ptr().add(count) as *mut L)
unsafe { *(self.ptr.as_ptr().add(count) as *mut L) }
}

/// Write a length to to the length slot. This assumes the
pub unsafe fn write_len<L>(&mut self, len: L) {
#[allow(clippy::cast_ptr_alignment)]
let count = get_vector_len_offset::<H>();
ptr::write(self.ptr.as_ptr().add(count) as *mut L, len);
unsafe {
ptr::write(self.ptr.as_ptr().add(count) as *mut L, len);
}
}

/// Read the capacity field on this vector. This may indicate a forwarded pointer
Expand Down Expand Up @@ -177,15 +179,17 @@ impl<T, H> RawSharedVector<T, H> {
/// expected that the caller has already checked the bounds
/// of len
pub unsafe fn write_value(&mut self, value: T, index: usize) {
ptr::write(self.item_mut(index), value);
unsafe {
ptr::write(self.item_mut(index), value);
}
}

/// Read a value at the provided index
/// Note this function does not do bounds checks and is
/// expected that the caller has already checked the bounds
/// of len
pub unsafe fn read_value(&mut self, index: usize) -> T {
ptr::read(self.item_mut(index))
unsafe { ptr::read(self.item_mut(index)) }
}

/// Get a mutable pointer to a index into the collection
Expand All @@ -194,9 +198,11 @@ impl<T, H> RawSharedVector<T, H> {
/// This method does no bounds checks so the caller will need to ensure
/// that this is only called within bounds
pub unsafe fn item_mut(&self, index: usize) -> *mut T {
match self.state() {
RawVecLocation::Here(_) => self.ptr.as_ptr().add(self.offset_item(index)) as *mut T,
RawVecLocation::Forwarded(vector) => vector.item_mut(index),
unsafe {
match self.state() {
RawVecLocation::Here(_) => self.ptr.as_ptr().add(self.offset_item(index)) as *mut T,
RawVecLocation::Forwarded(vector) => vector.item_mut(index),
}
}
}

Expand All @@ -206,9 +212,11 @@ impl<T, H> RawSharedVector<T, H> {
/// This method does no bounds checks so the caller will need to ensure
/// that this is only called within bounds
pub unsafe fn item_ptr(&self, index: usize) -> *const T {
match self.state() {
RawVecLocation::Here(_) => self.ptr.as_ptr().add(self.offset_item(index)) as *const T,
RawVecLocation::Forwarded(vector) => vector.item_ptr(index),
unsafe {
match self.state() {
RawVecLocation::Here(_) => self.ptr.as_ptr().add(self.offset_item(index)) as *const T,
RawVecLocation::Forwarded(vector) => vector.item_ptr(index),
}
}
}

Expand Down
24 changes: 15 additions & 9 deletions laythe_core/src/collections/unique_vector/raw_unique_vector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
VecBuilder,
align_utils::{
get_vector_cap_offset, get_vector_len_offset, get_vector_offset, make_vector_layout,
},
managed::{AllocResult, Allocate, DebugHeap, DebugWrap, Manage, Mark, Marked, Trace, Unmark},
VecBuilder,
};
use ptr::NonNull;
use std::{
Expand Down Expand Up @@ -73,23 +73,27 @@ impl<T, H> RawUniqueVector<T, H> {
pub unsafe fn write_len(&mut self, len: usize) {
#[allow(clippy::cast_ptr_alignment)]
let count = get_vector_len_offset::<H>();
ptr::write(self.ptr.as_ptr().add(count) as *mut usize, len);
unsafe {
ptr::write(self.ptr.as_ptr().add(count) as *mut usize, len);
}
}

/// Write a value at the provided index
/// Note this function does not do bounds checks and is
/// expected that the caller has already checked the bounds
/// of len
pub unsafe fn write_value(&mut self, value: T, index: usize) {
ptr::write(self.item_mut(index), value);
unsafe {
ptr::write(self.item_mut(index), value);
}
}

/// Read a value at the provided index
/// Note this function does not do bounds checks and is
/// expected that the caller has already checked the bounds
/// of len
pub unsafe fn read_value(&mut self, index: usize) -> T {
ptr::read(self.item_mut(index))
unsafe { ptr::read(self.item_mut(index)) }
}

/// Get a mutable pointer to a index into the collection
Expand All @@ -98,7 +102,7 @@ impl<T, H> RawUniqueVector<T, H> {
/// This method does no bounds checks so the caller will need to ensure
/// that this is only called within bounds
pub unsafe fn item_mut(&self, index: usize) -> *mut T {
self.ptr.as_ptr().add(self.offset_item(index)) as *mut T
unsafe { self.ptr.as_ptr().add(self.offset_item(index)) as *mut T }
}

/// Get a const pointer to a index into the collection
Expand All @@ -107,7 +111,7 @@ impl<T, H> RawUniqueVector<T, H> {
/// This method does no bounds checks so the caller will need to ensure
/// that this is only called within bounds
pub unsafe fn item_ptr(&self, index: usize) -> *const T {
self.ptr.as_ptr().add(self.offset_item(index)) as *const T
unsafe { self.ptr.as_ptr().add(self.offset_item(index)) as *const T }
}

/// Determine the byte offset of an item in the collection
Expand Down Expand Up @@ -425,7 +429,7 @@ mod test {

let vector: RawUniqueVector<u16, Header> = hooks.manage(VecBuilder::new(&[1, 2, 3], 4));

assert_eq!(vector.header().marked(), false);
assert!(!vector.header().marked());
}

#[test]
Expand All @@ -443,7 +447,8 @@ mod test {
let context = NoContext::default();
let hooks = GcHooks::new(&context);

let vector1: RawUniqueVector<u16, Header> = hooks.manage(VecBuilder::new(&[1, 2, 3, 4, 5], 5));
let vector1: RawUniqueVector<u16, Header> =
hooks.manage(VecBuilder::new(&[1, 2, 3, 4, 5], 5));
let vector2: RawUniqueVector<u16, Header> = hooks.manage(VecBuilder::new(&[], 5));

assert!(!vector1.is_empty());
Expand All @@ -455,7 +460,8 @@ mod test {
let context = NoContext::default();
let hooks = GcHooks::new(&context);

let vector: RawUniqueVector<u16, Header> = hooks.manage(VecBuilder::new(&[1, 2, 3, 4, 5], 10));
let vector: RawUniqueVector<u16, Header> =
hooks.manage(VecBuilder::new(&[1, 2, 3, 4, 5], 10));

assert_eq!(vector.cap(), 10);
}
Expand Down
2 changes: 1 addition & 1 deletion laythe_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T: DebugHeap> DebugHeap for Option<T> {
}
}

impl<'a, T: DebugHeap> DebugHeap for &'a [T] {
impl<T: DebugHeap> DebugHeap for &[T] {
fn fmt_heap(&self, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
f.debug_list()
.entries(self.iter().map(|x| DebugWrap(x, depth)))
Expand Down
4 changes: 4 additions & 0 deletions laythe_core/src/object/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Class {
&self.meta_class
}

pub fn meta_class_mut(&mut self) -> &mut Option<ObjRef<Class>> {
&mut self.meta_class
}

pub fn super_class(&self) -> &Option<ObjRef<Class>> {
&self.super_class
}
Expand Down
6 changes: 3 additions & 3 deletions laythe_core/src/object/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::LyStr;
use crate::ObjectRef;

const MAX_FIELD_COUNT: usize = u16::MAX as usize;
const NIL_ARRAY: [Value; MAX_FIELD_COUNT] = [VALUE_NIL; MAX_FIELD_COUNT];
static NIL_ARRAY: [Value; MAX_FIELD_COUNT] = [VALUE_NIL; MAX_FIELD_COUNT];

pub struct Instance(Array<Value, Header>);

Expand Down Expand Up @@ -67,9 +67,9 @@ impl Instance {
///
/// ## Safety
/// This should only be constructed from a box value
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self {
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self { unsafe {
Instance(Array::from_alloc_ptr(ptr))
}
}}
}

impl Deref for Instance {
Expand Down
4 changes: 2 additions & 2 deletions laythe_core/src/object/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ impl List {
///
/// ## Safety
/// This should only be constructed from a box value
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self {
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self { unsafe {
List(RawSharedVector::from_alloc_ptr(ptr))
}
}}

/// Has this list moved
pub fn has_moved(&self) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions laythe_core/src/object/ly_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ impl LyStr {
/// itself created by the garbage collector. The reference
/// should truly be of 'a for the lifetime of the allocator.
/// This will need to be refactored later
pub unsafe fn deref_static(&self) -> &'static str {
pub unsafe fn deref_static(&self) -> &'static str { unsafe {
str::from_utf8_unchecked(self.0.deref_static())
}
}}

/// Create a LyStr from a `NonNull<u8>`.
///
/// ## Safety
/// This functions assumes that the pointer was originally
/// from a different instance of LyStr. Other pointer
/// will likely crash immediately
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self {
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self { unsafe {
LyStr(Array::from_alloc_ptr(ptr))
}
}}
}

impl Mark for LyStr {
Expand Down
4 changes: 2 additions & 2 deletions laythe_core/src/object/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl Tuple {
///
/// ## Safety
/// This should only be constructed from a box value
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self {
pub unsafe fn from_alloc_ptr(ptr: NonNull<u8>) -> Self { unsafe {
Tuple(Array::from_alloc_ptr(ptr))
}
}}
}

impl Deref for Tuple {
Expand Down
4 changes: 2 additions & 2 deletions laythe_core/src/reference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl<T: 'static> Ref<T> {
/// This object must be keep alive otherwise this can
/// lead to dangling pointer error. This effectively
/// completely circumvents rust type system completely
pub unsafe fn deref_static(&self) -> &'static T {
pub unsafe fn deref_static(&self) -> &'static T { unsafe {
&(*self.ptr.as_ptr()).data
}
}}

/// Return the underlying pointer as a usize. This is
/// used by the nan boxing functionality
Expand Down
18 changes: 9 additions & 9 deletions laythe_core/src/reference/obj_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub struct ObjRef<T: 'static + Object> {
impl<T: 'static + Object> ObjRef<T> {
/// A const pointer to the header of this object
#[inline]
unsafe fn header_ptr(&self) -> *mut u8 {
unsafe fn header_ptr(&self) -> *mut u8 { unsafe {
let offset = get_offset::<ObjHeader, T>();
(self.ptr.as_ptr() as *mut u8).sub(offset)
}
}}

/// Retrieve the header from this array
#[inline]
Expand Down Expand Up @@ -69,9 +69,9 @@ impl<T: 'static + Object> ObjRef<T> {
/// object once all references include this reference have
/// ended. If the allocator collects before this point we'll
/// segfault or read unintended memory. In fewer word very bad
pub unsafe fn data_static(&self) -> &'static T {
pub unsafe fn data_static(&self) -> &'static T { unsafe {
&*(self.ptr.as_ptr() as *const T)
}
}}

/// Degrade this `ObjRef<T>` into a `ObjRefect`
#[inline]
Expand Down Expand Up @@ -266,10 +266,10 @@ impl ObjectRef {
}

#[inline]
unsafe fn data_ptr<T>(&self) -> NonNull<T> {
unsafe fn data_ptr<T>(&self) -> NonNull<T> { unsafe {
let offset = get_offset::<ObjHeader, T>();
NonNull::new_unchecked(self.ptr.as_ptr().add(offset) as *mut T)
}
}}

/// Retrieve the header from this array
#[inline]
Expand All @@ -287,7 +287,7 @@ impl ObjectRef {

#[inline]
pub fn is_kind(&self, kind: ObjectKind) -> bool {
return self.header().kind() == kind;
self.header().kind() == kind
}

#[inline]
Expand Down Expand Up @@ -796,10 +796,10 @@ impl<T: 'static + Object> ObjectHandlerBuilder<T> {
}

impl<T> ObjectHandlerBuilder<T> {
unsafe fn data_ptr<U>(&self) -> NonNull<U> {
unsafe fn data_ptr<U>(&self) -> NonNull<U> { unsafe {
let offset = get_offset::<ObjHeader, U>();
NonNull::new_unchecked(self.ptr.as_ptr().add(offset) as *mut U)
}
}}

#[inline]
pub fn degrade(self) -> ObjectHandle {
Expand Down
Loading
Loading