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
//! Java's consumer side of the FFI presence channel.
//!
//! A Rust `Option<i64>` return crosses the C ABI as a bare `i64`, so `None` and a legitimate
//! `Some(0)` arrive at the Panama downcall as the same bits. Java then wrapped that value with an
//! unconditional `Optional.of(...)` / `OptionalLong.of(...)`, which is *never* empty — so before
//! this gate every `None` reached the caller as `Optional[0]`, and the facade's `.orElse(null)`
//! turned it into a `0L` rather than a `null`. The FFI backend exports an additive
//! `{fn}_has_result` companion that answers presence out of band; this module supplies both the
//! `MethodHandle` that binds it and the conditional return expression that consults it.
//!
//! Whether the companion exists at all is asked of
//! [`crate::backends::ffi::type_map::result_presence_companion_exists`] — the same predicate the
//! FFI backend uses to decide whether to export the symbol. Re-deriving "is this an ambiguous
//! `Option` leaf" here would let the two sides drift, and a `MethodHandle` for a symbol the FFI
//! crate never exported fails at class-initialization time with
//! `ExceptionInInitializerError: Native symbol not found` — the whole binding refuses to load,
//! not just the one call. See `two-generators-disagree` in the repo's skill set. ~keep
use crateresult_presence_companion_exists;
use cratec_consumerresult_presence_symbol;
use crate;
/// The companion's "result is present" return value. `0` means absent and `-1` means the
/// companion itself failed (bad handle, param conversion, caught panic), so the emitted test is
/// `== 1` rather than `!= 0` — treating a failed companion as "present" would hand the caller the
/// primary downcall's sentinel as if it were real data. ~keep
const PRESENT: &str = "1";
/// The Java local the presence downcall is captured into. Distinct from `result` and
/// `primitiveResult`, the two names the primary invocations already bind in the same scope.
const PRESENCE_LOCAL: &str = "presenceResult";
/// The companion always returns `i32` regardless of the primary's return shape, so its descriptor
/// is a fixed layout rather than anything derived from the return type. ~keep
const PRESENCE_RETURN_LAYOUT: &str = "ValueLayout.JAVA_INT";
/// Suffix distinguishing a companion's `MethodHandle` constant from the primary's.
///
/// One definition, shared by the declaration in `native_lib` and by the call sites in the two
/// result emitters, so the handle can never be declared under one name and invoked under another.
/// ~keep
const HANDLE_SUFFIX: &str = "_HAS_RESULT";
/// The `MethodHandle` constant name for a primary handle's presence companion.
pub
/// The `_HAS_RESULT` downcall-handle declaration for one export, or `None` when the FFI crate
/// exports no companion for this return type and receiver.
///
/// `primary_handle_name` is the primary export's `MethodHandle` constant (`{PREFIX}_{FUNC}`),
/// `primary_ffi_name` its C symbol; the companion's spellings are derived from those through
/// [`presence_handle_name`] and [`result_presence_symbol`] so each has exactly one definition.
/// `param_layouts` must be the primary's own layout vector — the companion's C signature *is* the
/// primary's parameter list, so reusing the caller's already-computed vector keeps the two
/// descriptors from disagreeing about arity.
///
/// `receiver` is passed straight through to the eligibility authority: the companion re-invokes
/// the underlying method to observe presence, which an owned receiver cannot survive because its
/// first call already removed the handle from the registry.
pub
/// The statement capturing the companion's answer, or `None` when no companion exists.
///
/// Emitted *before* the primary invocation, never after: the companion clears the FFI crate's
/// last-error slot on entry, so running it second would wipe an error the primary had just
/// recorded and `checkLastError()` would see a clean slate. ~keep
///
/// `primary_handle` is the fully qualified primary handle (`NativeLib.{PREFIX}_{FUNC}`) and
/// `call_args` the same argument text the primary invocation passes.
pub
/// The same capture statement for a caller that already holds the resolved companion handle.
///
/// The opaque-method path resolves eligibility once when it builds its symbol table and keeps the
/// companion handle rather than the `MethodDef`, so it cannot go back through
/// [`presence_capture`]'s predicate — it would have to rebuild the return type and receiver the
/// predicate judges. Both entry points render the one template. ~keep
pub
/// Wrap an already-built `Optional`/`OptionalLong` construction in the presence test.
///
/// `present_expr` is what the caller would have returned unconditionally before the presence
/// channel existed; `empty_expr` is the matching empty constructor for the same Optional flavour.
/// Callers pass both so this function never has to know which flavour is in play.
pub