-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjava.rs
More file actions
341 lines (311 loc) · 11.8 KB
/
java.rs
File metadata and controls
341 lines (311 loc) · 11.8 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::debug;
use crate::node::class_cache::ClassCache;
use crate::node::config::ClassConfiguration;
use crate::node::helpers::napi_error::{MapToNapiError, StrIntoNapiError};
use crate::node::init::check_init_result;
use crate::node::interface_proxy::interface_proxy_options::InterfaceProxyOptions;
use crate::node::interface_proxy::java_interface_proxy::JavaInterfaceProxy;
use crate::node::java_class_instance::{JavaClassInstance, CLASS_PROXY_PROPERTY, OBJECT_PROPERTY};
use crate::node::java_class_proxy::JavaClassProxy;
use crate::node::java_options::JavaOptions;
use crate::node::stdout_redirect::StdoutRedirect;
use crate::node::util::helpers::{
call_async_method_with_resolver, list_files, parse_array_or_string, parse_classpath_args,
};
use app_state::{stateful, AppStateTrait, MutAppState, MutAppStateLock};
use java_rs::java_env::JavaEnv;
use java_rs::java_vm::JavaVM;
use java_rs::objects::args::AsJavaArg;
use java_rs::objects::class::JavaClass;
use java_rs::objects::java_object::JavaObject;
use java_rs::objects::object::GlobalJavaObject;
use java_rs::objects::string::JavaString;
use napi::{Env, JsFunction, JsObject, JsUnknown, ValueType};
use std::collections::HashMap;
use std::sync::Arc;
/// The main java class.
/// This should only be created once per process.
/// Any other attempts to create a new jvm instance will fail.
#[napi]
pub struct Java {
root_vm: JavaVM,
wanted_version: String,
loaded_jars: Vec<String>,
}
#[napi]
impl Java {
/// Create a new JVM instance.
/// @param libPath The path to jvm.(dll|so|dylib)
/// @param version The JVM version to use
/// @param opts The JVM options to use
/// @param javaOptions additional options to pass to the jvm
/// @param javaLibPath the path to the java library (JavaBridge.jar)
/// @param nativeLibPath the path to the native library (java.*.\[dll|so|dylib])
#[napi(constructor)]
pub fn new(
lib_path: Option<String>,
version: Option<String>,
opts: Option<Vec<String>>,
java_options: Option<JavaOptions>,
java_lib_path: String,
native_lib_path: String,
env: Env,
) -> napi::Result<Self> {
check_init_result()?;
let ver = version.unwrap_or("1.8".to_string());
let mut args = opts.unwrap_or_default();
let mut loaded_jars = vec![];
debug!("Loading JVM version {}", ver);
if let Some(cp) = java_options.as_ref().and_then(|o| o.classpath.as_ref()) {
let cp = list_files(
cp.clone(),
java_options
.as_ref()
.and_then(|o| o.ignore_unreadable_class_path_entries)
.unwrap_or(false),
)?;
loaded_jars.extend(cp.clone());
let parsed = parse_classpath_args(&cp, &mut args);
args.push(parsed);
}
debug!("Parsed startup args: {:?}", args);
let root_vm = JavaVM::new(&ver, lib_path, &args).map_napi_err(Some(env))?;
let java_env = root_vm.attach_thread().map_napi_err(Some(env))?;
java_env
.append_class_path(vec![java_lib_path])
.map_napi_err(Some(env))?;
let native_library_class = JavaClass::by_java_name(
"io.github.markusjx.bridge.NativeLibrary".to_string(),
&java_env,
)
.map_napi_err(Some(env))?;
let load_library = native_library_class
.get_static_void_method("loadLibrary", "(Ljava/lang/String;)V")
.map_napi_err(Some(env))?;
load_library
.call(&[JavaString::from_string(native_lib_path, &java_env)
.map_napi_err(Some(env))?
.as_arg()])
.map_napi_err(Some(env))?;
Ok(Self {
root_vm,
wanted_version: ver,
loaded_jars,
})
}
/// Import a java class
/// Will import the class and parse all of its methods and fields.
/// The imported class will be cached for future use.
#[napi(ts_return_type = "object")]
pub fn import_class(
&self,
env: Env,
class_name: String,
config: Option<ClassConfiguration>,
) -> napi::Result<JsFunction> {
let proxy_result = MutAppState::<ClassCache>::get_or_insert_default()
.get_mut()
.get_class_proxy(&self.root_vm, class_name, config);
// Map the result here in order to release the lock
// on the class cache before mapping the error.
JavaClassInstance::create_class_instance(&env, proxy_result.map_napi_err(Some(env))?)
}
/// Import a java class (async)
/// Will return a promise that resolves to the class instance.
///
/// If the underlying Java throwable should be contained in the error object,
/// set `asyncJavaExceptionObjects` to `true`. This will cause the JavaScript
/// stack trace to be lost. Setting this option in the global config will
/// **not** affect this method, this option has to be set each time this
/// method is called.
///
/// @see importClass
#[napi(ts_return_type = "Promise<object>")]
pub fn import_class_async(
&self,
env: Env,
class_name: String,
config: Option<ClassConfiguration>,
) -> napi::Result<JsObject> {
let root_vm = self.root_vm.clone();
call_async_method_with_resolver(
env,
config
.as_ref()
.and_then(|c| c.async_java_exception_objects)
.unwrap_or_default(),
move || {
MutAppState::<ClassCache>::get_or_insert_default()
.get_mut()
.get_class_proxy(&root_vm, class_name, config)
},
|env, res| JavaClassInstance::create_class_instance(env, res),
)
}
/// Get the wanted JVM version.
/// This may not match the actual JVM version.
#[napi(getter)]
pub fn wanted_version(&self) -> String {
self.wanted_version.to_string()
}
/// Get the actual JVM version.
/// This may not match the wanted JVM version.
#[napi(getter)]
pub fn version(&self, env: Env) -> napi::Result<String> {
self.root_vm.get_version().map_napi_err(Some(env))
}
/// Get the loaded jars.
#[napi(getter)]
pub fn loaded_jars(&self) -> &Vec<String> {
&self.loaded_jars
}
/// Append a single or multiple jars to the classpath.
#[napi(ts_args_type = "classpath: string | string[]")]
pub fn append_classpath(
&mut self,
classpath: JsUnknown,
ignore_unreadable: Option<bool>,
env: Env,
) -> napi::Result<()> {
let mut paths = list_files(
parse_array_or_string(classpath)?,
ignore_unreadable.unwrap_or(false),
)?;
let java_env = self.root_vm.attach_thread().map_napi_err(Some(env))?;
java_env
.append_class_path(paths.clone())
.map_napi_err(Some(env))?;
self.loaded_jars.append(&mut paths);
Ok(())
}
/// Set the stdout/stderr callbacks
#[napi]
pub fn set_stdout_callbacks(
&self,
env: Env,
#[napi(ts_arg_type = "((err: Error | null, data?: string) => void) | undefined | null")]
stdout_callback: Option<JsFunction>,
#[napi(ts_arg_type = "((err: Error | null, data?: string) => void) | undefined | null")]
stderr_callback: Option<JsFunction>,
) -> napi::Result<StdoutRedirect> {
let j_env = self.root_vm.attach_thread().map_napi_err(Some(env))?;
StdoutRedirect::new(
env,
&j_env,
self.root_vm.clone(),
stdout_callback,
stderr_callback,
)
.map_napi_err(Some(env))
}
#[napi]
pub fn create_interface_proxy(
&self,
env: Env,
classname: String,
#[napi(
ts_arg_type = "Record<string, (err: null | Error, callback: (err: Error | null, data?: any | null) => void, ...args: any[]) => void>"
)]
methods: HashMap<String, JsFunction>,
options: Option<InterfaceProxyOptions>,
) -> napi::Result<JavaInterfaceProxy> {
JavaInterfaceProxy::new(
self.root_vm.clone(),
env,
classname,
methods,
options.unwrap_or_default(),
)
.map_napi_err(Some(env))
}
/// Check if `this` is instance of `other`
#[napi]
pub fn is_instance_of(
&self,
node_env: Env,
this_obj: JsObject,
#[napi(ts_arg_type = "string | object")] other: JsUnknown,
) -> napi::Result<bool> {
let env = self.root_vm.attach_thread().map_napi_err(Some(node_env))?;
Self::_is_instance_of(env, &node_env, this_obj, other)
}
#[napi(getter, ts_return_type = "object")]
pub fn get_class_loader(&self, env: Env) -> napi::Result<JsUnknown> {
let proxy = MutAppState::<ClassCache>::get_or_insert_default()
.get_mut()
.get_class_proxy(&self.root_vm, "java.net.URLClassLoader".into(), None)
.map_napi_err(Some(env))?;
let j_env = self.root_vm.attach_thread().map_napi_err(Some(env))?;
JavaClassInstance::from_existing(
proxy,
&env,
j_env.get_class_loader().map_napi_err(Some(env))?,
)
}
#[napi(setter)]
pub fn set_class_loader(
&self,
env: Env,
#[napi(ts_arg_type = "object")] class_loader: JsUnknown,
) -> napi::Result<()> {
let j_env = self.root_vm.attach_thread().map_napi_err(Some(env))?;
let obj = class_loader.coerce_to_object()?;
let instance =
env.unwrap::<GlobalJavaObject>(&obj.get_named_property::<JsObject>(OBJECT_PROPERTY)?)?;
j_env
.replace_class_loader(instance.clone())
.map_napi_err(Some(env))
}
#[napi]
pub fn delete(&self, env: Env, mut obj: JsObject) -> napi::Result<()> {
let instance_obj: JsObject = obj.get_named_property(OBJECT_PROPERTY)?;
obj.set_named_property(OBJECT_PROPERTY, env.get_null()?)?;
env.drop_wrapped::<GlobalJavaObject>(&instance_obj)
}
pub fn vm(&self) -> JavaVM {
self.root_vm.clone()
}
pub fn _is_instance_of(
env: JavaEnv,
node_env: &Env,
this: JsObject,
other: JsUnknown,
) -> napi::Result<bool> {
let other = if other.get_type()? == ValueType::String {
env.find_global_class_by_java_name(other.coerce_to_string()?.into_utf16()?.as_str()?)
.map_napi_err(Some(*node_env))?
} else if other.get_type()? == ValueType::Function || other.get_type()? == ValueType::Object
{
let err_fn = |_| "'other' is not a java object".into_napi_err();
let obj: JsObject = other
.coerce_to_object()?
.get_named_property(CLASS_PROXY_PROPERTY)
.map_err(err_fn)?;
node_env
.unwrap::<Arc<JavaClassProxy>>(&obj)
.map_err(err_fn)?
.class
.clone()
} else {
return Err("'other' must be either a string or a java object".into_napi_err());
};
let err_fn = |_| "'this' is not a java object".into_napi_err();
let this_obj: JsObject = this.get_named_property(OBJECT_PROPERTY).map_err(err_fn)?;
let this = node_env
.unwrap::<GlobalJavaObject>(&this_obj)
.map_err(err_fn)?;
env.instance_of(JavaObject::from(this.clone()), other)
.map_napi_err(Some(*node_env))
}
}
/// Clear the class proxy cache.
/// Use this method in order to reset the config for all class proxies.
/// The new config will be applied once the class is imported again.
///
/// @since 2.4.0
#[stateful(init(cache))]
#[napi]
#[allow(unused)]
pub fn clear_class_proxies(mut cache: MutAppStateLock<ClassCache>) {
cache.clear();
}