-
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 basic support for instance fields
- Loading branch information
1 parent
3666479
commit eb19e0a
Showing
10 changed files
with
245 additions
and
31 deletions.
There are no files selected for viewing
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
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,43 @@ | ||
|
||
public class InstanceFields { | ||
|
||
private int resultSub; | ||
private int resultAdd; | ||
private int resultMul; | ||
public InstanceFields() { | ||
} | ||
|
||
public static void main(String[] args) { | ||
int first = 11; | ||
int second = 1000; | ||
InstanceFields instance = new InstanceFields(); | ||
instance.sub(first, second); | ||
instance.add(first, second); | ||
instance.mul(first, second); | ||
int result = instance.resultSub + instance.resultAdd + instance.resultMul; | ||
} | ||
|
||
public void sub(int first, int second) { | ||
resultSub = first - second; | ||
} | ||
|
||
public void add(int first, int second) { | ||
resultAdd = first + second; | ||
} | ||
|
||
public void mul(int first, int second) { | ||
resultMul = first * second; | ||
} | ||
|
||
public int getResultSub() { | ||
return resultSub; | ||
} | ||
|
||
public int getResultAdd() { | ||
return resultAdd; | ||
} | ||
|
||
public int getResultMul() { | ||
return resultMul; | ||
} | ||
} |
Binary file not shown.
This file was deleted.
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
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,21 +1,49 @@ | ||
use crate::error::Error; | ||
use crate::method_area::java_class::JavaClass; | ||
use crate::util::get_fields; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct JavaInstance<'a> { | ||
#[allow(dead_code)] | ||
class_ref: &'a JavaClass, | ||
fields: HashMap<String, Field>, | ||
} | ||
|
||
impl<'a> JavaInstance<'a> { | ||
pub fn new(class_ref: &'a JavaClass) -> Self { | ||
let mut fields = HashMap::new(); | ||
pub fn new(class_ref: &'a JavaClass) -> crate::error::Result<Self> { | ||
Ok(Self { | ||
class_ref, | ||
fields: get_fields(&class_ref.class_file)?, | ||
}) | ||
} | ||
|
||
pub fn set_field_value(&mut self, fieldname: &str, value: i32) -> crate::error::Result<()> { | ||
self.fields | ||
.get_mut(fieldname) | ||
.and_then(|v| Some(v.set_value(value))) | ||
.ok_or(Error::new_execution("error setting instance field value")) | ||
} | ||
|
||
Self { class_ref, fields } | ||
pub fn get_field_value(&self, fieldname: &str) -> crate::error::Result<i32> { | ||
self.fields | ||
.get(fieldname) | ||
.and_then(|v| Some(v.value)) | ||
.ok_or(Error::new_execution("error getting instance field value")) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Field { | ||
value: i32, // todo: support other types | ||
} | ||
|
||
impl Field { | ||
pub fn new() -> Self { | ||
Self { value: 0 } | ||
} | ||
|
||
pub fn set_value(&mut self, value: i32) { | ||
self.value = value; | ||
} | ||
} |
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