Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Feb 2, 2025
1 parent d131a41 commit 0500a65
Show file tree
Hide file tree
Showing 25 changed files with 613 additions and 99 deletions.
59 changes: 58 additions & 1 deletion classfile/src/attribute/resolved.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{Annotation, ElementValue, ElementValuePair, ElementValueTag, ElementValueType};
use super::{
Annotation, ElementValue, ElementValuePair, ElementValueTag, ElementValueType, EnclosingMethod,
InnerClass, InnerClasses,
};
use crate::attribute::BootstrapMethod;
use crate::constant_pool::types::{self, LoadableConstantPoolValue, MethodHandleEntry};
use crate::constant_pool::ConstantPool;
Expand Down Expand Up @@ -163,3 +166,57 @@ impl ResolvedBootstrapMethod {
}
}
}

pub struct ResolvedEnclosingMethod<'a> {
pub class: <types::raw::RawClassName as types::CpEntry<'a>>::Entry,
pub method: Option<<types::raw::RawNameAndType as types::CpEntry<'a>>::Entry>,
}

impl<'a> ResolvedEnclosingMethod<'a> {
pub(crate) fn resolve_from(raw: EnclosingMethod, constant_pool: &'a ConstantPool) -> Self {
let class = constant_pool.get::<types::raw::RawClassName>(raw.class_index);
let method = if raw.method_index == 0 {
None
} else {
Some(constant_pool.get::<types::raw::RawNameAndType>(raw.method_index))
};

Self { class, method }
}
}

pub struct ResolvedInnerClass<'a> {
pub inner_class: <types::raw::RawClassName as types::CpEntry<'a>>::Entry,
pub outer_class: Option<<types::raw::RawClassName as types::CpEntry<'a>>::Entry>,
pub inner_name: Option<<types::raw::RawConstantUtf8 as types::CpEntry<'a>>::Entry>,
pub access_flags: u2,
}

impl<'a> ResolvedInnerClass<'a> {
pub(crate) fn resolve_from(raw: &InnerClass, constant_pool: &'a ConstantPool) -> Self {
let inner_class = constant_pool.get::<types::raw::RawClassName>(raw.inner_class_info_index);

let outer_class;
if raw.outer_class_info_index == 0 {
outer_class = None;
} else {
outer_class =
Some(constant_pool.get::<types::raw::RawClassName>(raw.outer_class_info_index));
}

let inner_name;
if raw.inner_name_index == 0 {
inner_name = None;
} else {
inner_name =
Some(constant_pool.get::<types::raw::RawConstantUtf8>(raw.inner_name_index));
}

Self {
inner_class,
outer_class,
inner_name,
access_flags: raw.inner_class_access_flags,
}
}
}
37 changes: 36 additions & 1 deletion classfile/src/classfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::accessflags::ClassAccessFlags;
use crate::attribute::resolved::ResolvedBootstrapMethod;
use crate::attribute::resolved::{
ResolvedBootstrapMethod, ResolvedEnclosingMethod, ResolvedInnerClass,
};
use crate::attribute::{Attribute, AttributeType, SourceFile};
use crate::constant_pool::{self, ConstantPool};
use crate::fieldinfo::FieldInfo;
Expand Down Expand Up @@ -69,6 +71,39 @@ impl ClassFile {
None
}

pub fn enclosing_method(&self) -> Option<ResolvedEnclosingMethod<'_>> {
for attr in &self.attributes {
let Some(enclosing_method) = attr.enclosing_method() else {
continue;
};

return Some(ResolvedEnclosingMethod::resolve_from(
enclosing_method,
&self.constant_pool,
));
}

None
}

pub fn inner_classes(
&self,
) -> Option<impl ExactSizeIterator<Item = ResolvedInnerClass<'_>> + use<'_>> {
for attr in &self.attributes {
let Some(inner_classes) = attr.inner_classes() else {
continue;
};

let iter = inner_classes.classes.iter().map(move |inner_class| {
ResolvedInnerClass::resolve_from(inner_class, &self.constant_pool)
});

return Some(iter);
}

None
}

pub fn bootstrap_methods(
&self,
) -> Option<impl Iterator<Item = ResolvedBootstrapMethod> + use<'_>> {
Expand Down
25 changes: 24 additions & 1 deletion classfile/src/fieldinfo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::accessflags::FieldAccessFlags;
use crate::attribute::{Attribute, ConstantValue};
use crate::error::Result;

use std::borrow::Cow;
use std::fmt::Display;
use std::io::Read;

use common::int_types::{u1, u2};
Expand Down Expand Up @@ -140,4 +141,26 @@ impl FieldType {
pub fn is_array(&self) -> bool {
matches!(self, Self::Array(_))
}

pub fn as_signature(&self) -> Cow<'static, str> {
match self {
Self::Byte => "B".into(),
Self::Char => "C".into(),
Self::Double => "D".into(),
Self::Float => "F".into(),
Self::Int => "I".into(),
Self::Long => "J".into(),
Self::Short => "S".into(),
Self::Boolean => "Z".into(),
Self::Void => "V".into(),
Self::Object(name) => format!("L{};", String::from_utf8_lossy(name)).into(),
Self::Array(component) => format!("[{}", component.as_signature()).into(),
}
}
}

impl Display for FieldType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.as_signature())
}
}
8 changes: 4 additions & 4 deletions runtime/src/classpath/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl ClassLoader {
let name_str = StringInterner::rust_string_from_java_string(name_obj.extract_class());

if !name_str.is_empty() {
name = Some(Symbol::intern_owned(name_str));
name = Some(Symbol::intern(name_str));
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ impl ClassLoader {
Self {
obj,
name,
name_and_id: Symbol::intern_owned(name_and_id),
name_and_id: Symbol::intern(name_and_id),

unnamed_module: SyncUnsafeCell::new(Some(Box::leak(Box::new(unnamed_module)))),
classes: Mutex::new(HashMap::new()),
Expand Down Expand Up @@ -329,7 +329,7 @@ impl ClassLoader {
// Only Object has no direct superclass.
let mut super_class = None;
if let Some(super_class_name) = classfile.get_super_class() {
super_class = Some(self.resolve_super_class(Symbol::intern_bytes(&*super_class_name))?);
super_class = Some(self.resolve_super_class(Symbol::intern(&*super_class_name))?);
}

// TODO:
Expand Down Expand Up @@ -406,7 +406,7 @@ impl ClassLoader {

loop {
if let FieldType::Object(obj) = &*component {
self.load(Symbol::intern_bytes(&obj));
self.load(Symbol::intern(&obj));
break;
}

Expand Down
1 change: 1 addition & 0 deletions runtime/src/globals/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ define_classes!(
java_lang_invoke_MethodHandleNatives,
java_lang_invoke_MemberName,
java_lang_invoke_ResolvedMethodName,
java_lang_invoke_MethodType,
java_lang_ref_Reference,
java_lang_ref_Finalizer,
java_io_FileDescriptor,
Expand Down
18 changes: 18 additions & 0 deletions runtime/src/globals/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,24 @@ pub mod java_lang_invoke_MemberName {
}
}

pub mod java_lang_invoke_MethodType {
use classfile::FieldType;

field_module! {
@CLASS java_lang_invoke_MethodType;

@FIELDSTART
/// `java.lang.invoke.MethodType#ptypes` field offset
///
/// Expected field type: `Reference` to `java.lang.Class[]`
@FIELD ptypes: FieldType::Array(ref val) if val.is_class(b"java/lang/Class"),
/// `java.lang.invoke.MethodType#rtype` field offset
///
/// Expected field type: `Reference` to `java.lang.Class`
@FIELD rtype: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/Class"),
}
}

pub mod java_lang_invoke_ResolvedMethodName {
use classfile::FieldType;
use instructions::Operand;
Expand Down
10 changes: 7 additions & 3 deletions runtime/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ fn load_global_classes() {

load!(
java_lang_Object,
java_lang_Class,
java_lang_String,
java_lang_ClassLoader,
java_lang_Class,
);

// Fixup mirrors, as we have classes that were loaded before java.lang.Class
Expand All @@ -120,6 +120,7 @@ fn load_global_classes() {
java_lang_invoke_MethodHandleNatives,
java_lang_invoke_MemberName,
java_lang_invoke_ResolvedMethodName,
java_lang_invoke_MethodType,
);

// Primitive types
Expand Down Expand Up @@ -194,6 +195,11 @@ fn init_field_offsets() {
unsafe {
crate::globals::fields::java_lang_invoke_ResolvedMethodName::init_offsets();
}

// java.lang.invoke.MethodType
unsafe {
crate::globals::fields::java_lang_invoke_MethodType::init_offsets();
}
}

fn initialize_global_classes(thread: &JavaThread) {
Expand All @@ -209,8 +215,6 @@ fn initialize_global_classes(thread: &JavaThread) {

crate::globals::classes::jdk_internal_misc_UnsafeConstants().initialize(thread);
crate::globals::classes::java_lang_Module().initialize(thread);

crate::globals::classes::java_lang_invoke_MethodHandleNatives().initialize(thread);
}

fn create_thread_object(thread: &JavaThread) {
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/modules/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Module {
break;
}

package_symbols.push(Symbol::intern_owned(package));
package_symbols.push(Symbol::intern(package));
}

let loader = ClassLoaderSet::find_or_add(loader);
Expand All @@ -230,7 +230,7 @@ impl Module {
);
}

let module_name_sym = Symbol::intern_owned(module_name);
let module_name_sym = Symbol::intern(module_name);

let mut module_already_defined = false;
let mut duplicate_package: Option<&Package> = None;
Expand Down Expand Up @@ -349,7 +349,7 @@ fn init_java_base(
return;
}

let package = Package::new(Symbol::intern_owned(package), java_base);
let package = Package::new(Symbol::intern(package), java_base);
ClassLoader::bootstrap().insert_package_if_absent(guard, package);
}

Expand Down Expand Up @@ -443,7 +443,7 @@ impl Module {
super::with_module_lock(|guard| {
let Some(package) = self
.classloader()
.lookup_package(guard, Symbol::intern_owned(package_name))
.lookup_package(guard, Symbol::intern(package_name))
else {
return;
};
Expand Down
Loading

0 comments on commit 0500a65

Please sign in to comment.