-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobject.rs
More file actions
180 lines (133 loc) · 6.77 KB
/
object.rs
File metadata and controls
180 lines (133 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use core::{hash::BuildHasher, time::Duration};
use alloc::{boxed::Box, format, vec};
use dyn_clone::clone_box;
use hashbrown::DefaultHashBuilder;
use java_class_proto::JavaMethodProto;
use java_constants::MethodAccessFlags;
use jvm::{ClassInstance, ClassInstanceRef, Jvm, Result, runtime::JavaLangString};
use crate::{Runtime, RuntimeClassProto, RuntimeContext, SpawnCallback, classes::java::lang::String};
// class java.lang.Object
pub struct Object;
impl Object {
pub fn as_proto() -> RuntimeClassProto {
RuntimeClassProto {
name: "java/lang/Object",
parent_class: None,
interfaces: vec![],
methods: vec![
JavaMethodProto::new("<init>", "()V", Self::init, Default::default()),
JavaMethodProto::new("getClass", "()Ljava/lang/Class;", Self::get_class, Default::default()),
JavaMethodProto::new("hashCode", "()I", Self::hash_code, Default::default()),
JavaMethodProto::new("equals", "(Ljava/lang/Object;)Z", Self::equals, Default::default()),
JavaMethodProto::new("clone", "()Ljava/lang/Object;", Self::clone, MethodAccessFlags::NATIVE),
JavaMethodProto::new("toString", "()Ljava/lang/String;", Self::to_string, Default::default()),
JavaMethodProto::new("notify", "()V", Self::notify, Default::default()),
JavaMethodProto::new("notifyAll", "()V", Self::notify_all, Default::default()),
JavaMethodProto::new("wait", "(J)V", Self::wait_long, Default::default()),
JavaMethodProto::new("wait", "(JI)V", Self::wait_long_int, Default::default()),
JavaMethodProto::new("wait", "()V", Self::wait, Default::default()),
JavaMethodProto::new("finalize", "()V", Self::finalize, Default::default()),
],
fields: vec![],
access_flags: Default::default(),
}
}
async fn init(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
tracing::debug!("java.lang.Object::<init>({:?})", &this);
Ok(())
}
async fn get_class(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Self>> {
tracing::debug!("java.lang.Object::getClass({:?})", &this);
// TODO can we get class directly?
let this: Box<dyn ClassInstance> = this.into();
let class_name = this.class_definition().name();
let class = jvm.resolve_class(&class_name).await?.java_class();
Ok(class.into())
}
async fn hash_code(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
tracing::debug!("java.lang.Object::hashCode({:?})", &this);
let rust_this: Box<dyn ClassInstance> = this.into();
let hash = DefaultHashBuilder::default().hash_one(&rust_this);
Ok(hash as _)
}
async fn equals(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, other: ClassInstanceRef<Self>) -> Result<bool> {
tracing::debug!("java.lang.Object::equals({:?}, {:?})", &this, &other);
if other.is_null() {
return Ok(false);
}
let rust_this: Box<dyn ClassInstance> = this.into();
let rust_other: Box<dyn ClassInstance> = other.into();
rust_this.equals(&*rust_other)
}
async fn clone(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<Self>> {
tracing::warn!("stub java.lang.Object::clone({:?})", &this);
if !jvm.is_instance(&**this, "java/lang/Cloneable") {
return Err(jvm.exception("java/lang/CloneNotSupportedException", "Cannot clone this object").await);
}
Ok(None.into())
}
async fn to_string(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
tracing::debug!("java.lang.Object::toString({:?})", &this);
let class = jvm.invoke_virtual(&this, "getClass", "()Ljava/lang/Class;", ()).await?;
let class_name = jvm.invoke_virtual(&class, "getName", "()Ljava/lang/String;", ()).await?;
let class_name_rust = JavaLangString::to_rust_string(jvm, &class_name).await?;
let hash_code: i32 = jvm.invoke_virtual(&this, "hashCode", "()I", ()).await?;
let result = format!("{class_name_rust}@{hash_code:x}");
Ok(JavaLangString::from_rust_string(jvm, &result).await?.into())
}
async fn notify(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
tracing::debug!("java.lang.Object::notify({:?})", &this);
jvm.object_notify(&this, 1);
Ok(())
}
async fn notify_all(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
tracing::debug!("java.lang.Object::notifyAll({:?})", &this);
jvm.object_notify(&this, usize::MAX);
Ok(())
}
async fn wait_long(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, millis: i64) -> Result<()> {
tracing::debug!("java.lang.Object::wait({:?}, {:?})", &this, millis);
let _: () = jvm.invoke_virtual(&this, "wait", "(JI)V", (millis, 0)).await?;
Ok(())
}
async fn wait_long_int(jvm: &Jvm, context: &mut RuntimeContext, this: ClassInstanceRef<Self>, millis: i64, nanos: i32) -> Result<()> {
tracing::debug!("java.lang.Object::wait({:?}, {:?}, {:?})", &this, millis, nanos);
struct TimeoutNotifier {
timeout: i64,
jvm: Jvm,
this: Box<dyn ClassInstance>,
context: Box<dyn Runtime>,
}
#[async_trait::async_trait]
impl SpawnCallback for TimeoutNotifier {
async fn call(&self) -> Result<()> {
self.context.sleep(Duration::from_millis(self.timeout as _)).await;
self.jvm.object_notify(&self.this, 1); // TODO this may wake an unrelated waiter
Ok(())
}
}
let timeout = millis; // TODO nanos
if timeout != 0 {
context.spawn(
jvm,
Box::new(TimeoutNotifier {
timeout,
jvm: jvm.clone(),
this: this.clone().into(),
context: clone_box(context),
}),
);
}
jvm.object_wait(&this).await?;
Ok(())
}
async fn wait(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
tracing::debug!("java.lang.Object::wait({:?})", &this);
let _: () = jvm.invoke_virtual(&this, "wait", "(JI)V", (0i64, 0)).await?;
Ok(())
}
async fn finalize(_: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<()> {
tracing::warn!("stub java.lang.Object::finalize({:?})", &this);
Ok(())
}
}