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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use crate::backend::*;
use crate::model::*;

use crate::backend::java::jni::conversion::*;

use std::path::Path;

mod classes;
mod conversion;
mod enums;
mod exceptions;
mod interface;
mod structs;

/// Configuration for JNI (Rust) generation
pub struct JniBindgenConfig<'a> {
    /// Maven group id (e.g. io.stepfunc)
    pub group_id: &'a str,
    /// Name of the FFI target
    pub ffi_name: &'a str,
}

impl<'a> JniBindgenConfig<'a> {
    fn java_signature_path(&self, libname: &str) -> String {
        let mut result = self.group_id.replace('.', "/");
        result.push('/');
        result.push_str(libname);
        result
    }
}

fn module_string(name: &str, f: &mut dyn Printer, content: &str) -> FormattingResult<()> {
    module(name, f, |f| {
        for line in content.lines() {
            f.writeln(line)?;
        }
        Ok(())
    })
}

fn module<F>(name: &str, f: &mut dyn Printer, write: F) -> FormattingResult<()>
where
    F: Fn(&mut dyn Printer) -> FormattingResult<()>,
{
    f.newline()?;
    f.writeln(&format!("pub(crate) mod {name} {{"))?;
    indented(f, |f| write(f))?;
    f.writeln("}")?;
    Ok(())
}

/// Generate all of the JNI (Rust) source code that glues the Java and FFI together
///
/// This function is typically called from a build.rs script in a target that builds
/// the JNI shared library
pub fn generate_jni(path: &Path, lib: &Library, config: &JniBindgenConfig) -> FormattingResult<()> {
    let mut f = FilePrinter::new(path)?;

    generate_cache(&mut f)?;
    write_functions(&mut f, lib, config)?;
    write_collection_conversions(&mut f, lib, config)?;
    write_iterator_conversions(&mut f, lib, config)?;

    module("classes", &mut f, |f| {
        classes::generate_classes_cache(f, lib, config)
    })?;

    module("enums", &mut f, |f| {
        enums::generate_enums_cache(f, lib, config)
    })?;

    module("structs", &mut f, |f| structs::generate(f, lib, config))?;

    module("interfaces", &mut f, |f| {
        interface::generate_interfaces_cache(f, lib, config)
    })?;

    module("exceptions", &mut f, |f| {
        exceptions::generate_exceptions_cache(f, lib, config)
    })?;

    // Copy the modules that never change
    module_string("primitives", &mut f, include_str!("copy/primitives.rs"))?;
    module_string("unsigned", &mut f, include_str!("copy/unsigned.rs"))?;
    module_string("duration", &mut f, include_str!("copy/duration.rs"))?;
    module_string("collection", &mut f, include_str!("copy/collection.rs"))?;
    module_string("pointers", &mut f, include_str!("copy/pointers.rs"))?;
    module_string("util", &mut f, include_str!("copy/util.rs"))?;

    Ok(())
}

fn generate_cache(f: &mut dyn Printer) -> FormattingResult<()> {
    // Create cache
    f.writeln("pub(crate) struct JCache")?;
    blocked(f, |f| {
        f.writeln("vm: jni::JavaVM,")?;
        f.writeln("primitives: primitives::Primitives,")?;
        f.writeln("unsigned: unsigned::Unsigned,")?;
        f.writeln("duration: duration::Duration,")?;
        f.writeln("collection: collection::Collection,")?;
        f.writeln("classes: classes::Classes,")?;
        f.writeln("enums: enums::Enums,")?;
        f.writeln("structs: structs::Structs,")?;
        f.writeln("interfaces: interfaces::Interfaces,")?;
        f.writeln("exceptions: exceptions::Exceptions,")?;
        Ok(())
    })?;

    f.newline()?;

    f.writeln("impl JCache")?;
    blocked(f, |f| {
        f.writeln("fn init(vm: jni::JavaVM) -> Self")?;
        blocked(f, |f| {
            f.writeln("let env = vm.get_env().unwrap();")?;
            f.writeln("let primitives = primitives::Primitives::init(&env);")?;
            f.writeln("let unsigned = unsigned::Unsigned::init(&env);")?;
            f.writeln("let duration = duration::Duration::init(&env);")?;
            f.writeln("let collection = collection::Collection::init(&env);")?;
            f.writeln("let classes = classes::Classes::init(&env);")?;
            f.writeln("let enums = enums::Enums::init(&env);")?;
            f.writeln("let structs = structs::Structs::init(&env);")?;
            f.writeln("let interfaces = interfaces::Interfaces::init(&env);")?;
            f.writeln("let exceptions = exceptions::Exceptions::init(&env);")?;
            f.writeln("Self")?;
            blocked(f, |f| {
                f.writeln("vm,")?;
                f.writeln("primitives,")?;
                f.writeln("unsigned,")?;
                f.writeln("duration,")?;
                f.writeln("collection,")?;
                f.writeln("classes,")?;
                f.writeln("enums,")?;
                f.writeln("structs,")?;
                f.writeln("interfaces,")?;
                f.writeln("exceptions,")?;
                Ok(())
            })
        })
    })?;

    f.newline()?;

    f.writeln("static mut JCACHE: Option<JCache> = None;")?;

    f.newline()?;

    f.writeln("pub(crate) fn get_cache<'a>() -> &'a JCache {")?;
    indented(f, |f| f.writeln("unsafe { JCACHE.as_ref().unwrap() }"))?;
    f.writeln("}")?;

    f.newline()?;

    // OnLoad function
    f.writeln("#[no_mangle]")?;
    f.writeln("pub extern \"C\" fn JNI_OnLoad(vm: *mut jni::sys::JavaVM, _: *mut std::ffi::c_void) -> jni::sys::jint")?;
    blocked(f, |f| {
        f.writeln("let vm = unsafe { jni::JavaVM::from_raw(vm).unwrap() };")?;
        f.writeln("let jcache = JCache::init(vm);")?;
        f.writeln("unsafe { JCACHE.replace(jcache) };")?;
        f.writeln("jni::JNIVersion::V8.into()")
    })?;

    f.newline()?;

    // OnUnload function
    f.writeln("#[no_mangle]")?;
    f.writeln("pub extern \"C\" fn JNI_OnUnload(_vm: *mut jni::sys::JavaVM, _: *mut std::ffi::c_void) -> jni::sys::jint")?;
    blocked(f, |f| {
        f.writeln("unsafe { JCACHE.take().unwrap(); }")?;
        f.writeln("return 0;")
    })
}

fn write_collection_conversions(
    f: &mut dyn Printer,
    lib: &Library,
    config: &JniBindgenConfig,
) -> FormattingResult<()> {
    f.newline()?;
    f.writeln("/// convert Java lists into native API collections")?;
    f.writeln("pub(crate) mod collections {")?;
    indented(f, |f| {
        for col in lib.collections() {
            f.newline()?;
            write_collection_guard(f, config, col)?;
        }
        Ok(())
    })?;
    f.writeln("}")
}

fn write_iterator_conversions(
    f: &mut dyn Printer,
    lib: &Library,
    config: &JniBindgenConfig,
) -> FormattingResult<()> {
    f.newline()?;
    f.writeln("/// functions that convert native API iterators into Java lists")?;
    f.writeln("pub(crate) mod iterators {")?;
    indented(f, |f| {
        for iter in lib.iterators() {
            f.newline()?;
            write_iterator_conversion(f, config, iter)?;
        }
        Ok(())
    })?;
    f.writeln("}")
}

fn write_iterator_conversion(
    f: &mut dyn Printer,
    config: &JniBindgenConfig,
    iter: &Handle<AbstractIterator<Validated>>,
) -> FormattingResult<()> {
    f.writeln(&format!("pub(crate) fn {}(_env: &jni::JNIEnv, _cache: &crate::JCache, iter: {}) -> jni::sys::jobject {{", iter.name(), iter.iter_class.get_rust_type(config.ffi_name)))?;
    indented(f, |f| {
        f.writeln("let list = _cache.collection.new_array_list(&_env);")?;
        f.writeln(&format!(
            "while let Some(next) = unsafe {{ {}::ffi::{}_{}(iter).as_ref() }} {{",
            config.ffi_name, iter.iter_class.settings.c_ffi_prefix, iter.next_function.name
        ))?;
        indented(f, |f| {
            match &iter.item_type {
                IteratorItemType::Primitive(x) => {
                    let converted = x
                        .maybe_convert("*next")
                        .unwrap_or_else(|| "*next".to_string());
                    f.writeln(&format!("let next = _env.auto_local({converted});"))?;
                }
                IteratorItemType::Struct(x) => {
                    f.writeln(&format!(
                        "let next = _env.auto_local({});",
                        x.convert("next")
                    ))?;
                }
            }
            f.writeln("_cache.collection.add_to_array_list(&_env, list, next.as_obj().into());")
        })?;
        f.writeln("}")?;
        f.writeln("list.into_inner()")
    })?;
    f.writeln("}")
}

fn write_collection_guard(
    f: &mut dyn Printer,
    config: &JniBindgenConfig,
    col: &Handle<Collection<Validated>>,
) -> FormattingResult<()> {
    let collection_name = col.collection_class.name.camel_case();
    let c_ffi_prefix = col.collection_class.settings.c_ffi_prefix.clone();

    f.writeln("/// Guard that builds the C collection type from a Java list")?;
    f.writeln(&format!("pub(crate) struct {collection_name} {{"))?;
    indented(f, |f| {
        f.writeln(&format!(
            "inner: *mut {}::{}",
            config.ffi_name, collection_name
        ))
    })?;
    f.writeln("}")?;

    f.newline()?;

    f.writeln(&format!("impl std::ops::Deref for {collection_name} {{"))?;
    indented(f, |f| {
        f.writeln(&format!(
            "type Target = *mut {}::{};",
            config.ffi_name, collection_name
        ))?;
        f.newline()?;
        f.writeln("fn deref(&self) -> &Self::Target {")?;
        indented(f, |f| f.writeln("&self.inner"))?;
        f.writeln("}")
    })?;
    f.writeln("}")?;

    f.newline()?;

    f.writeln(&format!("impl {collection_name} {{"))?;
    indented(f, |f| {
        f.writeln("pub(crate) fn new(_env: jni::JNIEnv, list: jni::sys::jobject) -> Result<Self, jni::errors::Error> {")?;
        indented(f, |f| {
            f.writeln("let _cache = crate::get_cache();")?;
            let size = if col.has_reserve {
                f.writeln("let size = _cache.collection.get_size(&_env, list.into());")?;
                "size"
            } else {
                ""
            };
            f.writeln(&format!(
                "let col = Self {{ inner: unsafe {{ {}::ffi::{}_{}({}) }} }};",
                config.ffi_name, c_ffi_prefix, col.create_func.name, size
            ))?;
            f.writeln(
                "let it = _env.auto_local(_cache.collection.get_iterator(&_env, list.into()));",
            )?;
            f.writeln("while _cache.collection.has_next(&_env, it.as_obj()) {")?;
            indented(f, |f| {
                f.writeln(
                    "let next = _env.auto_local(_cache.collection.next(&_env, it.as_obj()));",
                )?;
                if let Some(converted) = col
                    .item_type
                    .to_rust_from_object("next.as_obj().into_inner()")
                {
                    // perform  primary conversion that shadows the variable
                    f.writeln(&format!("let next = {converted};"))?;
                }
                let arg = col
                    .item_type
                    .call_site("next")
                    .unwrap_or_else(|| "next".to_string());
                f.writeln(&format!(
                    "unsafe {{ {}::ffi::{}_{}(col.inner, {}) }};",
                    config.ffi_name, c_ffi_prefix, col.add_func.name, arg
                ))?;
                Ok(())
            })?;
            f.writeln("}")?;
            f.writeln("Ok(col)")?;
            Ok(())
        })?;
        f.writeln("}")
    })?;
    f.writeln("}")?;

    f.newline()?;

    f.writeln("/// Destroy the C collection on drop")?;
    f.writeln(&format!("impl Drop for {collection_name} {{"))?;
    indented(f, |f| {
        f.writeln("fn drop(&mut self) {")?;
        indented(f, |f| {
            f.writeln(&format!(
                "unsafe {{ {}::ffi::{}_{}(self.inner) }}",
                config.ffi_name, c_ffi_prefix, col.delete_func.name
            ))
        })?;
        f.writeln("}")
    })?;
    f.writeln("}")
}

fn write_functions(
    f: &mut dyn Printer,
    lib: &Library,
    config: &JniBindgenConfig,
) -> FormattingResult<()> {
    fn skip(c: FunctionCategory) -> bool {
        match c {
            FunctionCategory::Native => false,
            // these all get used internally to the JNI and
            // don't need external wrappers accessed from Java
            FunctionCategory::CollectionCreate => true,
            FunctionCategory::CollectionDestroy => true,
            FunctionCategory::CollectionAdd => true,
            FunctionCategory::IteratorNext => true,
        }
    }

    for handle in lib.functions().filter(|f| !skip(f.category)) {
        f.newline()?;
        write_function(f, lib, config, handle)?;
    }
    Ok(())
}

fn write_function_signature(
    f: &mut dyn Printer,
    lib: &Library,
    config: &JniBindgenConfig,
    handle: &Handle<Function<Validated>>,
) -> FormattingResult<()> {
    let args = handle
        .arguments
        .iter()
        .map(|param| format!("{}: {}", param.name, param.arg_type.jni_signature_type()))
        .collect::<Vec<String>>()
        .join(", ");

    let returns = match handle.return_type.get_value() {
        None => "".to_string(),
        Some(x) => {
            format!(" -> {}", x.jni_signature_type())
        }
    };

    f.writeln("#[no_mangle]")?;
    f.writeln(
        &format!(
            "pub extern \"C\" fn Java_{}_{}_NativeFunctions_{}(_env: jni::JNIEnv, _: jni::sys::jobject, {}){}",
            config.group_id.replace('.', "_"),
            lib.settings.name,
            handle.name.replace('_', "_1"),
            args,
            returns
        )
    )
}

fn write_function(
    f: &mut dyn Printer,
    lib: &Library,
    config: &JniBindgenConfig,
    handle: &Handle<Function<Validated>>,
) -> FormattingResult<()> {
    write_function_signature(f, lib, config, handle)?;
    blocked(f, |f| {
        // Get the JCache
        f.writeln("let _cache = get_cache();")?;

        f.newline()?;

        // Perform the primary conversion of the parameters if required
        for param in &handle.arguments {
            if let Some(converted) = param.arg_type.to_rust(&param.name) {
                let conversion = format!("let {} = {};", param.name, converted);
                f.writeln(&conversion)?;
            }
        }

        f.newline()?;

        let extra_param = match handle.get_signature_type() {
            SignatureType::NoErrorNoReturn => None,
            SignatureType::NoErrorWithReturn(_, _) => None,
            SignatureType::ErrorNoReturn(_) => None,
            SignatureType::ErrorWithReturn(_, _, _) => Some("_out.as_mut_ptr()".to_string()),
        };

        // list of arguments in the invocation
        let args = handle
            .arguments
            .iter()
            .map(|param| {
                param
                    .arg_type
                    .call_site(&param.name)
                    .unwrap_or_else(|| param.name.to_string())
            })
            .chain(extra_param)
            .collect::<Vec<String>>()
            .join(", ");

        // the invocation of the native function
        let invocation = format!(
            "unsafe {{ {}::ffi::{}_{}({}) }}",
            config.ffi_name, lib.settings.c_ffi_prefix, handle.name, args
        );

        // Call the C FFI
        match handle.get_signature_type() {
            SignatureType::NoErrorNoReturn => {
                f.writeln(&format!("{invocation};"))?;
            }
            SignatureType::NoErrorWithReturn(_, _) | SignatureType::ErrorNoReturn(_) => {
                f.writeln(&format!("let _result = {invocation};"))?;
            }
            SignatureType::ErrorWithReturn(_, _, _) => {
                f.writeln("let mut _out = std::mem::MaybeUninit::uninit();")?;
                f.writeln(&format!("let _result = {invocation};"))?;
            }
        };

        // Convert return value
        match handle.get_signature_type() {
            SignatureType::NoErrorNoReturn => (),
            SignatureType::NoErrorWithReturn(return_type, _) => {
                if let Some(conversion) = return_type.maybe_convert("_result") {
                    f.writeln(&format!("let _result = {conversion};"))?;
                }
            }
            SignatureType::ErrorNoReturn(error_type) => {
                f.writeln("if _result != 0")?;
                blocked(f, |f| {
                    error_type.inner.convert("_result");
                    f.writeln(&format!(
                        "let _error = {};",
                        error_type.inner.convert("_result")
                    ))?;
                    f.writeln(&format!(
                        "let error = _cache.exceptions.{}.throw(&_env, _error);",
                        error_type.exception_name
                    ))
                })?;
            }
            SignatureType::ErrorWithReturn(error_type, return_type, _) => {
                f.writeln("let _result = if _result == 0")?;
                blocked(f, |f| {
                    f.writeln("let _result = unsafe { _out.assume_init() };")?;
                    if let Some(conversion) = return_type.maybe_convert("_result") {
                        f.writeln(&conversion)?;
                    }
                    Ok(())
                })?;
                f.writeln("else")?;
                blocked(f, |f| {
                    f.writeln(&format!(
                        "let _error = {};",
                        error_type.inner.convert("_result")
                    ))?;
                    f.writeln(&format!(
                        "let error = _cache.exceptions.{}.throw(&_env, _error);",
                        error_type.exception_name
                    ))?;
                    f.writeln(return_type.get_default_value())
                })?;
                f.write(";")?;
            }
        }

        // Return value
        if !handle.return_type.is_none() {
            f.writeln("return _result.into();")?;
        }

        Ok(())
    })
}