-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for basic system native calls (#35)
Add support for basic system native calls
- Loading branch information
1 parent
e91c1ab
commit 46b9204
Showing
17 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package samples.nativecall.system; | ||
|
||
public class NativeCallSystem { | ||
public static void main(String[] args) { | ||
long currentTimeMillis = System.currentTimeMillis(); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// javac --patch-module java.base=. System.java | ||
package java.lang; | ||
|
||
public final class System { | ||
private System() { | ||
} | ||
|
||
public static native long currentTimeMillis(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub(crate) mod engine; | ||
pub(crate) mod opcode; | ||
mod system_native_table; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::error::Error; | ||
use crate::system_native::system::current_time_millis_wrp; | ||
use once_cell::sync::Lazy; | ||
use std::collections::HashMap; | ||
|
||
static SYSTEM_NATIVE_TABLE: Lazy<HashMap<&'static str, fn(&[i32]) -> Vec<i32>>> = | ||
Lazy::new(|| { | ||
let mut table = HashMap::new(); | ||
table.insert( | ||
"java/lang/System:currentTimeMillis:()J", | ||
current_time_millis_wrp as fn(&[i32]) -> Vec<i32>, | ||
); | ||
|
||
table | ||
}); | ||
|
||
pub(crate) fn invoke_native_method( | ||
method_signature: &str, | ||
args: &[i32], | ||
) -> crate::error::Result<Vec<i32>> { | ||
let native_method = SYSTEM_NATIVE_TABLE.get(method_signature).ok_or_else(|| { | ||
Error::new_native(&format!("Native method {method_signature} not found")) | ||
})?; | ||
|
||
let result = native_method(args); | ||
|
||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod vm; | |
mod execution_engine; | ||
mod heap; | ||
mod method_area; | ||
mod system_native; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod system; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// use crate::stack::stack_frame::i32toi64; | ||
// | ||
// pub(crate) fn subtract_exact_wrp(args: &[i32]) -> Vec<i32> { | ||
// let i = i32toi64(args[1], args[0]); | ||
// let j = i32toi64(args[3], args[2]); | ||
// | ||
// let millis = subtract_exact(i, j); | ||
// | ||
// let low = millis as i32; | ||
// let high = (millis >> 32) as i32; | ||
// | ||
// vec![high, low] | ||
// } | ||
// fn subtract_exact(first: i64, second: i64) -> i64 { | ||
// first.checked_sub(second).expect("Subtraction overflow") | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
pub(crate) fn current_time_millis_wrp(_args: &[i32]) -> Vec<i32> { | ||
let millis = current_time_millis(); | ||
|
||
let low = millis as i32; | ||
let high = (millis >> 32) as i32; | ||
|
||
vec![high, low] | ||
} | ||
fn current_time_millis() -> i64 { | ||
let now = std::time::SystemTime::now(); | ||
let since_the_epoch = now | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.expect("Time went backwards"); | ||
since_the_epoch.as_millis() as i64 | ||
} |