-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjni.v
347 lines (322 loc) · 8.42 KB
/
jni.v
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
342
343
344
345
346
347
// Copyright(C) 2021 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license file distributed with this software package
module jni
import jni.c
// TODO
pub const used_import = c.used_import
pub const void_arg = []JavaValue{}
pub struct CallResult {
pub:
call string
// method_type MethodType
result Type // TODO = Void ??
}
//
pub fn throw_exception(env &Env, msg string) {
exception_clear(env)
cls := find_class(env, 'java/lang/Exception')
throw_new(env, cls, msg)
}
pub fn panic_on_exception(env &Env) {
if exception_check(env) {
exception_describe(env)
panic(@MOD + '.' + @FN + ' an exception occured in jni.Env (${ptr_str(env)})')
}
}
// sig builds a V `jni` style signature from the supplied arguments.
pub fn sig(pkg string, f_name string, rt Type, args ...Type) string {
mut vtypargs := ''
for arg in args {
vtypargs += arg.type_name() + ', '
}
mut return_type := ' ' + rt.type_name()
is_void := match rt {
string {
rt == 'void'
}
else {
false
}
}
if rt.type_name() == 'string' && is_void {
return_type = ''
}
vtypargs = vtypargs.trim_right(', ')
jni_sig := pkg + '.' + v2j_fn_name(f_name) + '(' + vtypargs + ')' + return_type
// println(jni_sig)
return jni_sig
}
fn parse_signature(fqn_sig string) (string, string) {
sig := fqn_sig.trim_space()
fqn := sig.all_before('(')
mut return_type := sig.all_after_last(')').trim_space()
if return_type == '' {
return_type = 'void'
}
$if debug_signatures ? {
args_str := sig.all_after('(').all_before(')')
args := '(' + args_str + ')'
println(@MOD + '.' + @FN + ' ' + '"${sig}" -> "${fqn}${args} ${return_type}"')
}
return fqn, return_type
}
//
pub fn call_static_method(env &Env, signature string, args ...Type) CallResult {
fqn, return_type := parse_signature(signature)
mut jv_args := []JavaValue{}
mut jargs := ''
for vt in args {
jargs += v2j_signature_type(env, vt)
jv_args << v2j_value(env, vt)
}
jdef := fqn + '(' + jargs + ')' + v2j_string_signature_type(return_type)
$if debug_signatures ? {
println(@MOD + '.' + @FN + ' Java call style definition: "${fqn} -> ${jdef}"')
}
class, mid := get_class_static_method_id(env, jdef)
mut call_result := CallResult{}
//
if return_type.contains('/') || return_type.contains('.') {
call_result = CallResult{
call: signature
result: call_static_boolean_method_a(env, class, mid, jv_args.data)
}
} else {
call_result = match return_type {
'bool' {
CallResult{
call: signature
result: call_static_boolean_method_a(env, class, mid, jv_args.data)
}
}
'u8' {
CallResult{
call: signature
result: call_static_byte_method_a(env, class, mid, jv_args.data)
}
}
'rune' {
CallResult{
call: signature
result: call_static_char_method_a(env, class, mid, jv_args.data)
}
}
'i16' {
CallResult{
call: signature
result: call_static_short_method_a(env, class, mid, jv_args.data)
}
}
'int' {
CallResult{
call: signature
result: call_static_int_method_a(env, class, mid, jv_args.data)
}
}
'i64' {
CallResult{
call: signature
result: call_static_long_method_a(env, class, mid, jv_args.data)
}
}
'f32' {
CallResult{
call: signature
result: call_static_float_method_a(env, class, mid, jv_args.data)
}
}
'f64' {
CallResult{
call: signature
result: call_static_double_method_a(env, class, mid, jv_args.data)
}
}
'string' {
CallResult{
call: signature
result: call_static_string_method_a(env, class, mid, jv_args.data)
}
}
'object' {
CallResult{
call: signature
result: call_static_object_method_a(env, class, mid, jv_args.data)
}
}
'void' {
call_static_void_method_a(env, class, mid, jv_args.data)
CallResult{
call: signature
// result: Void{}
}
}
else {
CallResult{}
}
}
}
// Check for any exceptions
$if debug {
if exception_check(env) {
exception_describe(env)
excp := 'An exception occured while executing "${signature}" in JNIEnv (${ptr_str(env)})'
panic(excp)
}
}
return call_result
}
pub fn call_object_method(env &Env, obj JavaObject, signature string, args ...Type) CallResult {
fqn, return_type := parse_signature(signature)
mut jv_args := []JavaValue{}
mut jargs := ''
for vt in args {
jargs += v2j_signature_type(env, vt)
jv_args << v2j_value(env, vt)
}
jdef := fqn + '(' + jargs + ')' + v2j_string_signature_type(return_type)
$if debug_signatures ? {
println(@MOD + '.' + @FN + ' Java call style definition: "${fqn} -> ${jdef}"')
}
_, mid := get_object_class_and_method_id(env, obj, jdef)
mut call_result := CallResult{}
//
if return_type.contains('/') || return_type.contains('.') {
call_result = CallResult{
call: signature
result: call_object_method_a(env, obj, mid, jv_args.data)
}
} else {
call_result = match return_type {
'bool' {
CallResult{
call: signature
result: call_boolean_method_a(env, obj, mid, jv_args.data)
}
}
'u8' {
CallResult{
call: signature
result: call_byte_method_a(env, obj, mid, jv_args.data)
}
}
'rune' {
CallResult{
call: signature
result: call_char_method_a(env, obj, mid, jv_args.data)
}
}
'i16' {
CallResult{
call: signature
result: call_short_method_a(env, obj, mid, jv_args.data)
}
}
'int' {
CallResult{
call: signature
result: call_int_method_a(env, obj, mid, jv_args.data)
}
}
'i64' {
CallResult{
call: signature
result: call_long_method_a(env, obj, mid, jv_args.data)
}
}
'f32' {
CallResult{
call: signature
result: call_float_method_a(env, obj, mid, jv_args.data)
}
}
'f64' {
CallResult{
call: signature
result: call_double_method_a(env, obj, mid, jv_args.data)
}
}
'string' {
CallResult{
call: signature
result: call_string_method_a(env, obj, mid, jv_args.data)
}
}
'object' {
CallResult{
call: signature
result: call_object_method_a(env, obj, mid, jv_args.data)
}
}
'void' {
call_void_method_a(env, obj, mid, jv_args.data)
CallResult{
call: signature
}
}
else {
CallResult{}
}
}
}
// Check for any exceptions
$if debug {
if exception_check(env) {
exception_describe(env)
excp := @MOD + '.' + @FN +
' an exception occured while executing "${signature}" in JNIEnv (${ptr_str(env)})'
// throw_exception(env, excp)
panic(excp)
}
}
return call_result
}
fn get_class_static_method_id(env &Env, fqn_sig string) (JavaClass, JavaMethodID) {
clazz, fn_name, fn_sig := v2j_signature(fqn_sig)
mut jclazz := JavaClass(unsafe { nil })
// Find the Java class
$if android {
jclazz = find_class(default_env(), clazz)
} $else {
jclazz = find_class(env, clazz)
}
mid := get_static_method_id(env, jclazz, fn_name, fn_sig)
return jclazz, mid
}
fn get_object_class_and_method_id(env &Env, obj JavaObject, fqn_sig string) (JavaClass, JavaMethodID) {
_, f_name, f_sig := v2j_signature(fqn_sig)
// Find the class of the object
jclazz := get_object_class(env, obj)
// Find the method on the class
mid := get_method_id(env, jclazz, f_name, f_sig)
return jclazz, mid
}
@[inline]
pub fn (jo JavaObject) class_name(env &Env) string {
obj := jo
mut cls := get_object_class(env, obj)
// First get the class object
mut mid := get_method_id(env, cls, 'getClass', '()Ljava/lang/Class;')
cls_obj := call_object_method_a(env, obj, mid, void_arg.data) // NOTE vfmt will cause a compile error here if you only use 'void_arg.data'
// Get the class object's class descriptor
cls = get_object_class(env, cls_obj)
// Find the getName() method on the class object
mid = get_method_id(env, cls, 'getName', '()Ljava/lang/String;')
// Call the getName() to get a string struct back
return call_string_method_a(env, cls_obj, mid, void_arg.data) // NOTE vfmt will cause a compile error here if you only use 'void_arg.data'
}
@[inline]
pub fn (jo JavaObject) call(env &Env, typ MethodType, signature string, args ...Type) CallResult {
pkg := jo.class_name(env)
return match typ {
.@static { call_static_method(env, pkg + '.' + signature, ...args) }
.object { call_object_method(env, jo, pkg + '.' + signature, ...args) }
}
}
@[inline]
pub fn (jc JavaClass) get_name(env &Env) string {
unsafe {
// o := &JavaObject(voidptr(&jc))
o := JavaObject(jc)
return o.class_name(env)
}
}