Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jan 1, 2025
1 parent 41b7b13 commit 8fe2509
Show file tree
Hide file tree
Showing 47 changed files with 777 additions and 319 deletions.
33 changes: 23 additions & 10 deletions generators/native_methods/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,33 @@ pub fn generate_definitions_for_class(def_path: &Path, class: &Class) {
writeln!(definitions_file, "}}").unwrap();
}

macro_rules! non_static_signature {
() => {
"\tpub fn _{}(env: std::ptr::NonNull<::jni::env::JniEnv>, locals: \
crate::stack::local_stack::LocalStack) -> crate::native::NativeReturn {{"
};
}

macro_rules! static_signature {
() => {
"\tpub fn _{}(env: std::ptr::NonNull<::jni::env::JniEnv>, class: &'static \
crate::objects::class::Class, locals: crate::stack::local_stack::LocalStack) -> \
crate::native::NativeReturn {{"
};
}
const STATIC_SIGNATURE: &str = "pub fn ";

fn generate_methods_for_class(class: &Class, definitions_file: &mut File) {
for method in class.methods().filter(|method| {
method.name.is_some() && method.modifiers.contains(AccessFlags::ACC_NATIVE)
}) {
writeln!(
definitions_file,
"\tpub fn _{}(env: std::ptr::NonNull<::jni::env::JniEnv>, locals: \
crate::stack::local_stack::LocalStack) -> crate::native::NativeReturn {{",
method.name()
)
.unwrap();
if method.is_static() {
writeln!(definitions_file, static_signature!(), method.name()).unwrap();
} else {
writeln!(definitions_file, non_static_signature!(), method.name()).unwrap();
}

let is_static = method.modifiers.contains(AccessFlags::ACC_STATIC);
let is_static = method.is_static();
if !is_static {
writeln!(
definitions_file,
Expand Down Expand Up @@ -95,13 +109,12 @@ fn generate_methods_for_class(class: &Class, definitions_file: &mut File) {
}
}

// TODO: static methods should have a `class` argument
let mut method_call = String::new();
write!(
method_call,
"super::{}(env,{}",
method.name(),
if is_static { "" } else { "this," }
if is_static { "class," } else { "this," }
)
.unwrap();

Expand Down
4 changes: 4 additions & 0 deletions generators/native_methods/src/parse/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl Method {
}
}

pub fn is_static(&self) -> bool {
self.modifiers.contains(AccessFlags::ACC_STATIC)
}

/// The symbol for the class name + method name
///
/// For example, `java.lang.Object#hashCode` would become `Object_hashCode`.
Expand Down
2 changes: 1 addition & 1 deletion generators/native_methods/src/registernatives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! native_method_table_file_header {
static NATIVES_REGISTERED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
#[allow(trivial_casts, unused_imports)]
pub fn registerNatives(_: std::ptr::NonNull<JniEnv>) {{
pub fn registerNatives(_: std::ptr::NonNull<JniEnv>, _: &'static crate::objects::class::Class) {{
use symbols::sym;
if NATIVES_REGISTERED.compare_exchange(false, true, std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::Acquire) != Ok(false) {{
Expand Down
24 changes: 19 additions & 5 deletions generators/native_methods/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,31 @@ use std::path::{Component, Path};

/// Create a `NativeMethodDef` for a method
pub(crate) fn method_table_entry(module: &str, class: &Class, method: &Method) -> String {
let ptr = if method.is_static() {
format!(
"NativeMethodPtr::new_static(crate::native::{}{}::definitions::_{})",
escape_module_name(module).replace('/', "::"),
class.class_name.replace('$', "::"),
method.name()
)
} else {
format!(
"NativeMethodPtr::new_non_static(crate::native::{}{}::definitions::_{})",
escape_module_name(module).replace('/', "::"),
class.class_name.replace('$', "::"),
method.name()
)
};

format!(
"NativeMethodDef {{ class: sym!({}), name: sym!({}), descriptor: sym!({}) }}, \
crate::native::{}{}::definitions::_{} as NativeMethodPtr",
"NativeMethodDef {{ class: sym!({}), name: sym!({}), descriptor: sym!({}), is_static: {} \
}}, {ptr}",
format!("{}{}", module, class.class_name)
.replace('/', "_")
.replace('$', "_"),
method.name_symbol(),
method.signature_symbol_name(),
escape_module_name(module).replace('/', "::"),
class.class_name.replace('$', "::"),
method.name()
method.is_static(),
)
}

Expand Down
Loading

0 comments on commit 8fe2509

Please sign in to comment.