Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Dec 27, 2024
1 parent 9bb456b commit e06556d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 0 additions & 2 deletions classfile/src/constant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use common::int_types::{s4, s8, u1, u2, u4};

// https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-4.html#jvms-4.4

// TODO: Need to make a cache so we don't need to keep resolving classes and interning strings

#[derive(PartialEq, Clone)]
#[repr(transparent)]
pub struct ConstantPool {
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/objects/constant_pool/cp_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::objects::method::Method;
use common::int_types::{s4, s8, u2};
use symbols::Symbol;

/// A constant pool entry of any type
pub enum Entry {
Class(<Class as EntryType>::Resolved),
Integer(<Integer as EntryType>::Resolved),
Expand All @@ -23,8 +24,10 @@ pub enum Entry {

/// A trait for types that can be stored in the constant pool.
pub trait EntryType: sealed::Sealed {
/// The final type an entry will resolve to.
type Resolved;

/// Convert the `ResolvedEntry` to the final type.
#[doc(hidden)]
fn resolved_entry(entry: ResolvedEntry) -> Self::Resolved;

Expand Down
18 changes: 18 additions & 0 deletions runtime/src/objects/constant_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ use common::int_types::u2;

// https://docs.oracle.com/javase/specs/jvms/se19/html/jvms-4.html#jvms-4.4

/// The runtime constant pool for a class
///
/// This provides two things:
///
/// * A type-safe interface to the constant pool
/// * A cache of resolved constant pool entries
///
/// The cache will ensure that each entry is only resolved once, and that the same instance is
/// returned each time.
///
/// This is important for types like `Class` which are loaded once and then shared between all
/// instances of the class.
pub struct ConstantPool {
class: &'static Class,
entries: Box<[entry::ConstantPoolEntry]>,
Expand All @@ -28,6 +40,9 @@ impl ConstantPool {
}
}

/// Get a constant pool entry of a specific type
///
/// See [`ConstantPool`] for notes on resolution.
pub fn get<T: cp_types::EntryType>(&self, index: u2) -> T::Resolved {
let entry = &self.entries[index as usize];
if let Some(resolved) = entry.resolved::<T>() {
Expand All @@ -37,6 +52,9 @@ impl ConstantPool {
entry.resolve::<T>(self.class, &self, index)
}

/// Get a constant pool entry of any type
///
/// See [`ConstantPool`] for notes on resolution.
pub fn get_any(&self, index: u2) -> Entry {
let raw = &self.raw[index as usize];
match raw {
Expand Down

0 comments on commit e06556d

Please sign in to comment.