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
use std::{ffi::CString, os::raw::c_void};
use thiserror::Error;
use crate::{
sys::{JavaVMInitArgs, JavaVMOption},
JNIVersion,
};
#[derive(Debug, Error)]
pub enum JvmError {
#[error("internal null in option: {0}")]
NullOptString(String),
}
#[derive(Debug)]
pub struct InitArgsBuilder {
opts: Vec<String>,
ignore_unrecognized: bool,
version: JNIVersion,
}
impl Default for InitArgsBuilder {
fn default() -> Self {
InitArgsBuilder {
opts: vec![],
ignore_unrecognized: false,
version: JNIVersion::V8,
}
}
}
impl InitArgsBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn option(self, opt_string: &str) -> Self {
let mut s = self;
match opt_string {
"vfprintf" | "abort" | "exit" => return s,
_ => {}
}
s.opts.push(opt_string.into());
s
}
pub fn version(self, version: JNIVersion) -> Self {
let mut s = self;
s.version = version;
s
}
pub fn ignore_unrecognized(self, ignore: bool) -> Self {
let mut s = self;
s.ignore_unrecognized = ignore;
s
}
pub fn build(self) -> Result<InitArgs, JvmError> {
let mut opts = Vec::with_capacity(self.opts.len());
for opt in self.opts {
let option_string =
CString::new(opt.as_str()).map_err(|_| JvmError::NullOptString(opt))?;
let jvm_opt = JavaVMOption {
optionString: option_string.into_raw(),
extraInfo: ::std::ptr::null_mut(),
};
opts.push(jvm_opt);
}
Ok(InitArgs {
inner: JavaVMInitArgs {
version: self.version.into(),
ignoreUnrecognized: self.ignore_unrecognized as _,
options: opts.as_ptr() as _,
nOptions: opts.len() as _,
},
opts,
})
}
pub fn options(&self) -> Vec<String> {
self.opts.clone()
}
}
pub struct InitArgs {
inner: JavaVMInitArgs,
opts: Vec<JavaVMOption>,
}
impl InitArgs {
pub(crate) fn inner_ptr(&self) -> *mut c_void {
&self.inner as *const _ as _
}
}
impl Drop for InitArgs {
fn drop(&mut self) {
for opt in self.opts.iter() {
unsafe { CString::from_raw(opt.optionString) };
}
}
}