Skip to content

Commit 406f50c

Browse files
committed
Fix boolean issue in Java bindings.
This is due to the fact that jni::sys::jboolean is just a typedef to u8. When I was setting the the boolean value, I was using the JValue conversion. It was converting it erroneously to a u8, not a boolean. The rest is just the JVM having a very bad time with it.
1 parent 074dcc7 commit 406f50c

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

generators/java-oo-bindgen/src/rust/conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl TypeConverter for BooleanConverter {
431431
}
432432

433433
fn convert_from_rust(&self, f: &mut dyn Printer, from: &str, to: &str) -> FormattingResult<()> {
434-
f.writeln(&format!("{}if {} {{ 1 }} else {{ 0 }}", to, from))
434+
f.writeln(&format!("{}{}", to, from))
435435
}
436436
}
437437

generators/java-oo-bindgen/src/rust/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn generate_functions(
329329

330330
// Return value
331331
if !handle.return_type.is_void() {
332-
f.writeln("return _result;")?;
332+
f.writeln("return _result.into();")?;
333333
}
334334

335335
Ok(())

tests/bindings/java/foo-tests/src/test/java/io/stepfunc/foo_test/StructureTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ private static void checkStructure(Structure structure) {
6666
assertThat(structure.stringValue).isEqualTo("Hello");
6767

6868
assertThat(structure.structureValue.test).isEqualTo(ushort(41));
69+
assertThat(structure.structureValue.firstEnumValue).isEqualTo(StructureEnum.VAR2);
70+
assertThat(structure.structureValue.secondEnumValue).isEqualTo(StructureEnum.VAR2);
6971
assertThat(structure.enumValue).isEqualTo(StructureEnum.VAR2);
72+
assertThat(structure.enumValue2).isEqualTo(StructureEnum.VAR2);
7073

7174
assertThat(structure.durationMillis).isEqualTo(Duration.ofMillis(4200));
7275
assertThat(structure.durationSeconds).isEqualTo(Duration.ofSeconds(76));

tests/foo-schema/src/structure.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ use oo_bindgen::native_struct::StructElementType;
55
use oo_bindgen::*;
66

77
pub fn define(lib: &mut LibraryBuilder) -> Result<(), BindingError> {
8-
let other_structure = lib.declare_native_struct("OtherStructure")?;
9-
let other_structure = lib
10-
.define_native_struct(&other_structure)?
11-
.add("test", StructElementType::Uint16(Some(41)), "test")?
12-
.doc("Structure within a structure")?
13-
.build()?;
14-
158
let structure_enum = lib
169
.define_native_enum("StructureEnum")?
1710
.push("Var1", "Var1")?
@@ -20,6 +13,26 @@ pub fn define(lib: &mut LibraryBuilder) -> Result<(), BindingError> {
2013
.doc("Enum")?
2114
.build()?;
2215

16+
let other_structure = lib.declare_native_struct("OtherStructure")?;
17+
let other_structure = lib
18+
.define_native_struct(&other_structure)?
19+
.add("test", StructElementType::Uint16(Some(41)), "test")?
20+
// The following pattern used to crash in Java because of the way we handled boolean
21+
.add(
22+
"first_enum_value",
23+
StructElementType::Enum(structure_enum.clone(), Some("Var2".to_string())),
24+
"first_enum_value",
25+
)?
26+
.add("bool1", StructElementType::Sint16(Some(1)), "bool1")?
27+
.add("bool2", StructElementType::Bool(Some(false)), "bool2")?
28+
.add(
29+
"second_enum_value",
30+
StructElementType::Enum(structure_enum.clone(), Some("Var2".to_string())),
31+
"second_enum_value",
32+
)?
33+
.doc("Structure within a structure")?
34+
.build()?;
35+
2336
let structure = lib.declare_native_struct("Structure")?;
2437

2538
let structure_interface = lib
@@ -33,11 +46,26 @@ pub fn define(lib: &mut LibraryBuilder) -> Result<(), BindingError> {
3346

3447
let structure = lib
3548
.define_native_struct(&structure)?
49+
.add(
50+
"enum_value",
51+
StructElementType::Enum(structure_enum.clone(), Some("Var2".to_string())),
52+
"enum_value",
53+
)?
3654
.add(
3755
"boolean_value",
3856
StructElementType::Bool(Some(true)),
3957
"boolean_value",
4058
)?
59+
.add(
60+
"boolean_value2",
61+
StructElementType::Bool(Some(true)),
62+
"boolean_value2",
63+
)?
64+
.add(
65+
"enum_value2",
66+
StructElementType::Enum(structure_enum, Some("Var2".to_string())),
67+
"enum_value2",
68+
)?
4169
.add(
4270
"uint8_value",
4371
StructElementType::Uint8(Some(1)),
@@ -98,11 +126,6 @@ pub fn define(lib: &mut LibraryBuilder) -> Result<(), BindingError> {
98126
Type::Struct(other_structure),
99127
"structure_value",
100128
)?
101-
.add(
102-
"enum_value",
103-
StructElementType::Enum(structure_enum, Some("Var2".to_string())),
104-
"enum_value",
105-
)?
106129
.add(
107130
"interface_value",
108131
Type::Interface(structure_interface),

0 commit comments

Comments
 (0)