Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Feb 3, 2025
1 parent cb99486 commit 4229b51
Show file tree
Hide file tree
Showing 20 changed files with 283 additions and 182 deletions.
19 changes: 15 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[workspace]
resolver = "2"
members = [
"generators/*",
"tools/*",
"classfile",
"instructions",
"runtime",
Expand All @@ -13,7 +15,6 @@ members = [
"jni",
"jni/sys",
]
exclude = ["tools", "generators"]

[workspace.dependencies]
classfile = { path = "classfile" }
Expand All @@ -34,6 +35,16 @@ tracing = "0.1.41"
paste = "1.0.15"
zip = { version = "2.2.1", default-features = false }

# Nested workspace members
tools = { path = "tools" }
generators = { path = "generators" }
# Generators
combine = "4.6.7"
bitflags = "2.6.0"
indexmap = "2.7.0"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", default-features = false }
walkdir = "2.5.0"

# Tools
anyhow = "1.0.93"
clap = "4.0.29"
tracing-subscriber = "0.3.18"
13 changes: 12 additions & 1 deletion classfile/src/attribute/resolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::attribute::BootstrapMethod;
use crate::constant_pool::types::{self, LoadableConstantPoolValue, MethodHandleEntry};
use crate::constant_pool::ConstantPool;

use std::fmt::{Debug, Formatter};

use common::int_types::{s4, s8, u2};

pub struct ResolvedAnnotation {
Expand Down Expand Up @@ -140,13 +142,22 @@ pub enum ResolvedElementValueType {
},
}

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct ResolvedBootstrapMethod {
pub method_handle_index: u2,
pub method_handle_info: MethodHandleEntry<'static>,
pub arguments: Vec<LoadableConstantPoolValue<'static>>,
}

impl Debug for ResolvedBootstrapMethod {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ResolvedBoostrapMethod")
.field("method_handle_info", &self.method_handle_info)
.field("arguments", &self.arguments)
.finish()
}
}

impl ResolvedBootstrapMethod {
pub(crate) fn resolve_from(raw: &BootstrapMethod, constant_pool: &ConstantPool) -> Self {
let method_handle_info =
Expand Down
30 changes: 24 additions & 6 deletions classfile/src/constant_pool/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{ConstantPool, ConstantPoolValueInfo};

use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter};

use common::int_types::{s4, s8, u1, u2, u4};

Expand Down Expand Up @@ -76,12 +77,12 @@ pub enum ReferenceKind {
PutStatic = 4,
/// REF_invokeVirtual
InvokeVirtual = 5,
/// REF_newInvokeSpecial
NewInvokeSpecial = 6,
/// REF_invokeStatic
InvokeStatic = 7,
InvokeStatic = 6,
/// REF_invokeSpecial
InvokeSpecial = 8,
InvokeSpecial = 7,
/// REF_newInvokeSpecial
NewInvokeSpecial = 8,
/// REF_invokeInterface
InvokeInterface = 9,
}
Expand Down Expand Up @@ -142,12 +143,20 @@ impl<'a> ReferenceEntry<'a> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct ClassNameEntry<'a> {
pub name_index: u2,
pub name: <raw::RawConstantUtf8 as CpEntry<'a>>::Entry,
}

impl Debug for ClassNameEntry<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ClassNameEntry")
.field(&String::from_utf8_lossy(&self.name))
.finish()
}
}

impl<'a> ClassNameEntry<'a> {
pub fn into_owned(self) -> ClassNameEntry<'static> {
ClassNameEntry {
Expand Down Expand Up @@ -195,14 +204,23 @@ impl<'a> MethodRefEntry<'a> {
}
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct NameAndTypeEntry<'a> {
pub name_index: u2,
pub name: <raw::RawConstantUtf8 as CpEntry<'a>>::Entry,
pub descriptor_index: u2,
pub descriptor: <raw::RawConstantUtf8 as CpEntry<'a>>::Entry,
}

impl Debug for NameAndTypeEntry<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("NameAndTypeEntry")
.field(&String::from_utf8_lossy(&self.name))
.field(&String::from_utf8_lossy(&self.descriptor))
.finish()
}
}

impl<'a> NameAndTypeEntry<'a> {
pub fn into_owned(self) -> NameAndTypeEntry<'static> {
NameAndTypeEntry {
Expand Down
17 changes: 0 additions & 17 deletions generators/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion generators/vm_symbols/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"
proc-macro = true

[dependencies]
syn = { workspace = true, features = ["full"] }
syn = { workspace = true, features = ["full", "parsing"] }
proc-macro2.workspace = true
quote.workspace = true
11 changes: 6 additions & 5 deletions runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
fn main() {
println!("cargo:rerun-if-changed=../generators/native_methods");
println!("cargo:rerun-if-changed=src/native/mod.rs");
println!("cargo:rerun-if-changed=src/native/java/*.rs");
println!("cargo:rerun-if-changed=src/native/jdk/*.rs");
println!("cargo::rerun-if-changed=../generators/native_methods");
println!("cargo::rerun-if-changed=src/native/mod.rs");
println!("cargo::rerun-if-changed=src/native/java/*.rs");
println!("cargo::rerun-if-changed=src/native/jdk/*.rs");

if let Err(e) = native_methods::generate() {
panic!("Failed to generate native methods: {}", e);
println!("cargo::error=Failed to generate native methods: {e}");
std::process::exit(1);
}
}
59 changes: 59 additions & 0 deletions runtime/src/globals/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,66 @@ pub mod jdk_internal_misc_UnsafeConstants {
}

pub mod java_lang_invoke_MemberName {
use crate::objects::class_instance::ClassInstance;
use crate::objects::instance::Instance;
use crate::objects::reference::{MirrorInstanceRef, Reference};
use classfile::FieldType;
use instructions::Operand;
use jni::sys::jint;

/// `java.lang.invoke.MemberName#clazz` field
pub fn clazz(instance: &ClassInstance) -> MirrorInstanceRef {
instance
.get_field_value0(clazz_field_offset())
.expect_reference()
.extract_mirror()
}

pub fn set_clazz(instance: &mut ClassInstance, value: Reference) {
instance.put_field_value0(clazz_field_offset(), Operand::Reference(value))
}

/// `java.lang.invoke.MemberName#name` field
pub fn name(instance: &ClassInstance) -> Reference {
instance
.get_field_value0(name_field_offset())
.expect_reference()
}

pub fn set_name(instance: &mut ClassInstance, value: Reference) {
instance.put_field_value0(name_field_offset(), Operand::Reference(value))
}

/// `java.lang.invoke.MemberName#type` field
pub fn type_(instance: &ClassInstance) -> Reference {
instance
.get_field_value0(type_field_offset())
.expect_reference()
}

pub fn set_type(instance: &mut ClassInstance, value: Reference) {
instance.put_field_value0(type_field_offset(), Operand::Reference(value));
}

/// `java.lang.invoke.MemberName#flags` field
pub fn flags(instance: &ClassInstance) -> jint {
instance.get_field_value0(flags_field_offset()).expect_int()
}

pub fn set_flags(instance: &mut ClassInstance, value: jint) {
instance.put_field_value0(flags_field_offset(), Operand::Int(value));
}

/// `java.lang.invoke.MemberName#method` field
pub fn method(instance: &ClassInstance) -> Reference {
instance
.get_field_value0(flags_field_offset())
.expect_reference()
}

pub fn set_method(instance: &mut ClassInstance, value: Reference) {
instance.put_field_value0(method_field_offset(), Operand::Reference(value));
}

field_module! {
@CLASS java_lang_invoke_MemberName;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl Interpreter {

return;
}

match opcode {
OpCode::instanceof => stack.push_int(0),
OpCode::checkcast => panic!("ClassCastException"), // TODO
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/native/java/lang/Class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::objects::class::Class;
use crate::objects::instance::Instance;
use crate::objects::reference::{ArrayInstanceRef, MirrorInstanceRef, Reference};
use crate::string_interner::StringInterner;
use crate::thread::exceptions::{handle_exception, throw, throw_with_ret, Throws};
use crate::thread::exceptions::{
handle_exception, throw, throw_and_return_null, throw_with_ret, Throws,
};
use crate::thread::JavaThread;

use std::sync::Arc;
Expand Down Expand Up @@ -222,7 +224,7 @@ pub fn getDeclaringClass0(
handle_exception!(Reference::null(), thread, target_class.loader().load(outer));

if outer.is_array() {
throw_with_ret!(Reference::null(), thread, IncompatibleClassChangeError);
throw_and_return_null!(thread, IncompatibleClassChangeError);
}

declaring_class = Reference::mirror(outer.mirror());
Expand Down
10 changes: 4 additions & 6 deletions runtime/src/native/java/lang/Object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::objects::instance::CloneableInstance;
use crate::objects::reference::Reference;
use crate::thread::JavaThread;

use crate::thread::exceptions::throw_and_return_null;
use ::jni::env::JniEnv;
use ::jni::sys::{jint, jlong};
use common::traits::PtrType;
Expand Down Expand Up @@ -31,20 +32,17 @@ pub fn clone(_: JniEnv, this: Reference /* java.lang.Object */) -> Reference /*
if this.is_array() {
let array = this.extract_array();
let instance = array.get();
let cloned = unsafe { instance.clone() };
let cloned = unsafe { CloneableInstance::clone(instance) };
return Reference::array(cloned);
}

let instance_ref = this.extract_class();
let instance = instance_ref.get();
if !instance.class().is_cloneable() {
// TODO
panic!("CloneNotSupportedException");
throw_and_return_null!(JavaThread::current(), CloneNotSupportedException);
}

tracing::debug!(target: "method", "Cloning instance of {}", instance.class().name);

let cloned = unsafe { instance.clone() };
let cloned = unsafe { CloneableInstance::clone(instance) };
Reference::class(cloned)
}

Expand Down
Loading

0 comments on commit 4229b51

Please sign in to comment.