Skip to content

Commit bbbcf5a

Browse files
committed
constructor-based encoders
1 parent 5f548c8 commit bbbcf5a

2 files changed

Lines changed: 16 additions & 69 deletions

File tree

prebindgen-ext/src/jni/struct_strategy.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use proc_macro2::TokenStream;
66
use quote::{format_ident, quote, ToTokens};
7+
use proc_macro2::Span;
78

89
use prebindgen::SourceLocation;
910

@@ -96,7 +97,8 @@ impl StructStrategy for JniDecoderStruct {
9697
let mut field_preludes: Vec<TokenStream> = Vec::new();
9798
let mut field_init: Vec<TokenStream> = Vec::new();
9899
let mut encoder_field_preludes: Vec<TokenStream> = Vec::new();
99-
let mut field_assignments: Vec<TokenStream> = Vec::new();
100+
let mut ctor_sig = String::from("(");
101+
let mut ctor_args: Vec<TokenStream> = Vec::new();
100102
let mut all_fields_have_encoders = true;
101103

102104
for field in &named.named {
@@ -165,27 +167,19 @@ impl StructStrategy for JniDecoderStruct {
165167

166168
match jni_field_access(binding.wire_type()) {
167169
Some((jni_sig, _jvalue_method, false)) => {
168-
field_assignments.push(quote! {
169-
env.set_field(
170-
&obj,
171-
#camel_fname,
172-
#jni_sig,
173-
jni::objects::JValue::from(#encoded_ident),
174-
)
175-
.map_err(|err| #zerror!(#err_prefix, err))?;
170+
ctor_sig.push_str(jni_sig);
171+
ctor_args.push(quote! {
172+
jni::objects::JValue::from(#encoded_ident)
176173
});
177174
}
178175
Some((jni_sig, _jvalue_method, true)) => {
179176
let encoded_obj_ident = format_ident!("__{}_encoded_obj", fname_ident);
180-
field_assignments.push(quote! {
177+
encoder_field_preludes.push(quote! {
181178
let #encoded_obj_ident: jni::objects::JObject = #encoded_ident.into();
182-
env.set_field(
183-
&obj,
184-
#camel_fname,
185-
#jni_sig,
186-
jni::objects::JValue::Object(&#encoded_obj_ident),
187-
)
188-
.map_err(|err| #zerror!(#err_prefix, err))?;
179+
});
180+
ctor_sig.push_str(jni_sig);
181+
ctor_args.push(quote! {
182+
jni::objects::JValue::Object(&#encoded_obj_ident)
189183
});
190184
}
191185
None => {
@@ -224,20 +218,21 @@ impl StructStrategy for JniDecoderStruct {
224218

225219
// Only generate encoder if all fields have encoders available
226220
if all_fields_have_encoders {
221+
ctor_sig.push_str(")V");
222+
let ctor_sig_lit = syn::LitStr::new(&ctor_sig, Span::call_site());
227223
let encoder_tokens = quote! {
228224
#[allow(non_snake_case, unused_mut, unused_variables)]
229225
pub(crate) fn #encoder_ident(
230226
mut env: &mut jni::JNIEnv,
231227
value: #struct_module::#struct_ident,
232228
) -> #zresult<jni::sys::jobject> {
229+
#(#encoder_field_preludes)*
233230
let obj = env.new_object(
234231
#java_class_name,
235-
"()V",
236-
&[],
232+
#ctor_sig_lit,
233+
&[#(#ctor_args),*],
237234
)
238235
.map_err(|err| #zerror!(err))?;
239-
#(#encoder_field_preludes)*
240-
#(#field_assignments)*
241236
Ok(obj.as_raw())
242237
}
243238
};

zenoh-jni/build.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use itertools::Itertools;
22
use proc_macro2::TokenStream;
33
use quote::quote;
4-
use std::fs;
54

65
use zenoh_flat::core::{
76
primitive_builtins, FunctionsConverter, InputFn, NameMangler, OutputFn, TypeRegistry,
@@ -309,16 +308,6 @@ fn shared_kotlin_types() -> KotlinTypeMap {
309308
.add("ZResult<bool>", "Boolean")
310309
}
311310

312-
fn patch_generated_kotlin_data_classes(path: &str, replacements: &[(&str, &str)]) {
313-
let mut content = fs::read_to_string(path)
314-
.unwrap_or_else(|err| panic!("failed to read generated Kotlin file {path}: {err}"));
315-
for (from, to) in replacements {
316-
content = content.replace(from, to);
317-
}
318-
fs::write(path, content)
319-
.unwrap_or_else(|err| panic!("failed to patch generated Kotlin file {path}: {err}"));
320-
}
321-
322311
fn main() {
323312
let source = prebindgen::Source::new(zenoh_flat::PREBINDGEN_OUT_DIR);
324313

@@ -482,36 +471,6 @@ fn main() {
482471
session_kotlin
483472
.write()
484473
.expect("failed to write generated JNISessionNative.kt");
485-
patch_generated_kotlin_data_classes(
486-
"../zenoh-jni/generated-kotlin/io/zenoh/jni/JNISessionNative.kt",
487-
&[
488-
("val isHeartbeat: Boolean,", "var isHeartbeat: Boolean = false,"),
489-
("val periodMs: Long,", "var periodMs: Long = 0L,"),
490-
("val maxSamples: Long,", "var maxSamples: Long = 0L,"),
491-
("val repliesPriority: Int,", "var repliesPriority: Int = 0,"),
492-
(
493-
"val repliesCongestionControl: Int,",
494-
"var repliesCongestionControl: Int = 0,",
495-
),
496-
(
497-
"val repliesIsExpress: Boolean,",
498-
"var repliesIsExpress: Boolean = false,",
499-
),
500-
(
501-
"val enableHeartbeat: Boolean,",
502-
"var enableHeartbeat: Boolean = false,",
503-
),
504-
("val isSporadic: Boolean,", "var isSporadic: Boolean = false,"),
505-
(
506-
"val detectLatePublishers: Boolean,",
507-
"var detectLatePublishers: Boolean = false,",
508-
),
509-
(
510-
"val maxAgeSeconds: Double,",
511-
"var maxAgeSeconds: Double = 0.0,",
512-
),
513-
],
514-
);
515474

516475
let mut keyexpr_kotlin = KotlinInterfaceGenerator::builder()
517476
.output_path("../zenoh-jni/generated-kotlin/io/zenoh/jni/JNIKeyExprNative.kt")
@@ -533,11 +492,4 @@ fn main() {
533492
keyexpr_kotlin
534493
.write()
535494
.expect("failed to write generated JNIKeyExprNative.kt");
536-
patch_generated_kotlin_data_classes(
537-
"../zenoh-jni/generated-kotlin/io/zenoh/jni/JNIKeyExprNative.kt",
538-
&[
539-
("val ptr: Long,", "var ptr: Long = 0L,"),
540-
("val string: String,", "var string: String = \"\","),
541-
],
542-
);
543495
}

0 commit comments

Comments
 (0)