Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jan 30, 2025
1 parent 92622c8 commit d131a41
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 132 deletions.
17 changes: 17 additions & 0 deletions classfile/src/accessflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ impl ClassAccessFlags {
pub const ACC_ENUM : ClassAccessFlags = Self(0x4000);
/// Is a module, not a class or interface.
pub const ACC_MODULE : ClassAccessFlags = Self(0x8000);

/// Returns the `u2` representation of the access flags
pub fn as_u2(self) -> u16 {
self.0
}
}

impl_accessflags_for!(ClassAccessFlags);
Expand All @@ -135,6 +140,7 @@ impl_is_methods! {

/// Method access and property flags (§4.6-A)
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct MethodAccessFlags(u16);

#[rustfmt::skip]
Expand Down Expand Up @@ -167,6 +173,11 @@ impl MethodAccessFlags {
pub const ACC_STRICT : MethodAccessFlags = Self(0x0800);
/// Declared synthetic; not present in the source code.
pub const ACC_SYNTHETIC : MethodAccessFlags = Self(0x1000);

/// Returns the `u2` representation of the access flags
pub fn as_u2(self) -> u16 {
self.0
}
}

impl_accessflags_for!(MethodAccessFlags);
Expand All @@ -190,6 +201,7 @@ impl_is_methods! {

/// Field access and property flags (§4.5-A)
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct FieldAccessFlags(u16);

#[rustfmt::skip]
Expand All @@ -215,6 +227,11 @@ impl FieldAccessFlags {
pub const ACC_SYNTHETIC: FieldAccessFlags = Self(0x1000);
/// Declared as an element of an enum class.
pub const ACC_ENUM : FieldAccessFlags = Self(0x4000);

/// Returns the `u2` representation of the access flags
pub fn as_u2(self) -> u16 {
self.0
}
}

impl_accessflags_for!(FieldAccessFlags);
Expand Down
28 changes: 26 additions & 2 deletions classfile/src/constant_pool/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a> FieldRefEntry<'a> {
}

/// The type of a method handle
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReferenceKind {
/// REF_getField
GetField = 1,
Expand All @@ -85,7 +85,7 @@ pub enum ReferenceKind {
}

impl ReferenceKind {
fn from_u8(value: u8) -> Option<ReferenceKind> {
pub fn from_u8(value: u8) -> Option<ReferenceKind> {
match value {
1 => Some(ReferenceKind::GetField),
2 => Some(ReferenceKind::GetStatic),
Expand All @@ -99,6 +99,30 @@ impl ReferenceKind {
_ => None,
}
}

pub fn is_field(self) -> bool {
matches!(
self,
ReferenceKind::GetField
| ReferenceKind::GetStatic
| ReferenceKind::PutField
| ReferenceKind::PutStatic
)
}

pub fn is_method(self) -> bool {
matches!(
self,
ReferenceKind::InvokeVirtual
| ReferenceKind::InvokeStatic
| ReferenceKind::InvokeSpecial
| ReferenceKind::InvokeInterface
)
}

pub fn is_constructor(self) -> bool {
self == ReferenceKind::NewInvokeSpecial
}
}

pub enum ReferenceEntry<'a> {
Expand Down
6 changes: 5 additions & 1 deletion generators/native_methods/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ fn class_contains_fields(class: &Class) -> bool {
for member in &class.members {
match member {
Member::Field(_) => return true,
Member::Class(c) => return class_contains_fields(c),
Member::Class(c) => {
if class_contains_fields(c) {
return true;
}
},
Member::Method(_) => {},
}
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/globals/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ define_classes!(
java_io_Serializable,
java_lang_Module,
java_lang_invoke_MethodHandleNatives,
java_lang_invoke_MemberName,
java_lang_invoke_ResolvedMethodName,
java_lang_ref_Reference,
java_lang_ref_Finalizer,
java_io_FileDescriptor,
Expand Down
48 changes: 48 additions & 0 deletions runtime/src/globals/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,51 @@ pub mod jdk_internal_misc_UnsafeConstants {
// TODO: class.set_static_field(data_cache_line_flush_size_field_offset(), /* ... */);
}
}

pub mod java_lang_invoke_MemberName {
use classfile::FieldType;
use instructions::Operand;
use jni::sys::jint;

field_module! {
@CLASS java_lang_invoke_MemberName;

@FIELDSTART
/// `java.lang.invoke.MemberName#clazz` field offset
///
/// Expected field type: `Reference` to `java.lang.Class`
@FIELD clazz: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/Class"),
/// `java.lang.invoke.MemberName#name` field offset
///
/// Expected field type: `Reference` to `java.lang.String`
@FIELD name: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/String"),
/// `java.lang.invoke.MemberName#type` field offset
///
/// Expected field type: `Reference` to `java.lang.Object`
@FIELD r#type: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/Object"),
/// `java.lang.invoke.MemberName#flags` field offset
///
/// Expected field type: jint
@FIELD flags: FieldType::Int,
/// `java.lang.invoke.MemberName#method` field offset
///
/// Expected field type: `Reference` to `java.lang.invoke.ResolvedMethodName`
@FIELD method: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/invoke/ResolvedMethodName"),
}
}

pub mod java_lang_invoke_ResolvedMethodName {
use classfile::FieldType;
use instructions::Operand;
use jni::sys::jint;

field_module! {
@CLASS java_lang_invoke_ResolvedMethodName;

@FIELDSTART
/// `java.lang.invoke.ResolvedMethodName#vmholder` field offset
///
/// Expected field type: `Reference` to `java.lang.Class`
@FIELD vmholder: ty @ FieldType::Object(_) if ty.is_class(b"java/lang/Class"),
}
}
16 changes: 16 additions & 0 deletions runtime/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ fn load_global_classes() {
java_io_Serializable,
java_lang_ref_Reference,
java_lang_ref_Finalizer,
);

// MethodHandle stuff
load!(
jdk_internal_reflect_MethodAccessorImpl,
java_lang_invoke_MethodHandleNatives,
java_lang_invoke_MemberName,
java_lang_invoke_ResolvedMethodName,
);

// Primitive types
Expand Down Expand Up @@ -178,6 +184,16 @@ fn init_field_offsets() {
unsafe {
crate::globals::fields::java_lang_Thread::init_offsets();
}

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

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

fn initialize_global_classes(thread: &JavaThread) {
Expand Down
Loading

0 comments on commit d131a41

Please sign in to comment.