Skip to content

Commit 1e9a211

Browse files
authored
fix worlds-with-types.wit test case for Java generator (#480)
Signed-off-by: Joel Dice <[email protected]>
1 parent 5635975 commit 1e9a211

File tree

8 files changed

+97
-55
lines changed

8 files changed

+97
-55
lines changed

crates/gen-guest-teavm-java/src/lib.rs

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ impl Opts {
4343
}
4444
}
4545

46+
struct InterfaceFragment {
47+
src: String,
48+
stub: String,
49+
}
50+
4651
#[derive(Default)]
4752
pub struct TeaVmJava {
4853
opts: Opts,
@@ -52,7 +57,8 @@ pub struct TeaVmJava {
5257
tuple_counts: HashSet<usize>,
5358
needs_cleanup: bool,
5459
needs_result: bool,
55-
classes: HashMap<String, String>,
60+
interface_fragments: HashMap<String, Vec<InterfaceFragment>>,
61+
world_fragments: Vec<InterfaceFragment>,
5662
sizes: SizeAlign,
5763
interface_names: HashMap<InterfaceId, String>,
5864
}
@@ -95,7 +101,7 @@ impl WorldGenerator for TeaVmJava {
95101
gen.import(name, func);
96102
}
97103

98-
gen.add_class();
104+
gen.add_interface_fragment();
99105
}
100106

101107
fn import_funcs(
@@ -105,14 +111,14 @@ impl WorldGenerator for TeaVmJava {
105111
funcs: &[(&str, &Function)],
106112
_files: &mut Files,
107113
) {
108-
let name = &resolve.worlds[world].name;
114+
let name = &format!("{}-world", resolve.worlds[world].name);
109115
let mut gen = self.interface(resolve, name);
110116

111117
for (_, func) in funcs {
112118
gen.import(name, func);
113119
}
114120

115-
gen.add_class();
121+
gen.add_world_fragment();
116122
}
117123

118124
fn export_interface(
@@ -130,7 +136,7 @@ impl WorldGenerator for TeaVmJava {
130136
gen.export(func, Some(name));
131137
}
132138

133-
gen.add_class();
139+
gen.add_interface_fragment();
134140
}
135141

136142
fn export_funcs(
@@ -140,14 +146,14 @@ impl WorldGenerator for TeaVmJava {
140146
funcs: &[(&str, &Function)],
141147
_files: &mut Files,
142148
) {
143-
let name = &resolve.worlds[world].name;
149+
let name = &format!("{}-world", resolve.worlds[world].name);
144150
let mut gen = self.interface(resolve, name);
145151

146152
for (_, func) in funcs {
147153
gen.export(func, None);
148154
}
149155

150-
gen.add_class();
156+
gen.add_world_fragment();
151157
}
152158

153159
fn export_types(
@@ -157,14 +163,14 @@ impl WorldGenerator for TeaVmJava {
157163
types: &[(&str, TypeId)],
158164
_files: &mut Files,
159165
) {
160-
let name = &resolve.worlds[world].name;
166+
let name = &format!("{}-world", resolve.worlds[world].name);
161167
let mut gen = self.interface(resolve, name);
162168

163-
for (name, ty) in types {
164-
gen.define_type(name, *ty);
169+
for (ty_name, ty) in types {
170+
gen.define_type(ty_name, *ty);
165171
}
166172

167-
gen.add_class();
173+
gen.add_world_fragment();
168174
}
169175

170176
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) {
@@ -186,6 +192,15 @@ impl WorldGenerator for TeaVmJava {
186192
"
187193
);
188194

195+
src.push_str(
196+
&self
197+
.world_fragments
198+
.iter()
199+
.map(|f| f.src.deref())
200+
.collect::<Vec<_>>()
201+
.join("\n"),
202+
);
203+
189204
let component_type =
190205
wit_component::metadata::encode(resolve, id, wit_component::StringEncoding::UTF8)
191206
.unwrap()
@@ -324,8 +339,56 @@ impl WorldGenerator for TeaVmJava {
324339

325340
files.push(&format!("{name}World.java"), indent(&src).as_bytes());
326341

327-
for (name, body) in &self.classes {
328-
files.push(&format!("{name}.java"), indent(body).as_bytes());
342+
let generate_stub = |name, fragments: &[InterfaceFragment], files: &mut Files| {
343+
let body = fragments
344+
.iter()
345+
.map(|f| f.stub.deref())
346+
.collect::<Vec<_>>()
347+
.join("\n");
348+
349+
let body = format!(
350+
"package {package};
351+
352+
{IMPORTS}
353+
354+
public class {name} {{
355+
{body}
356+
}}
357+
"
358+
);
359+
360+
files.push(&format!("{name}.java"), indent(&body).as_bytes());
361+
};
362+
363+
if self.opts.generate_stub {
364+
generate_stub(format!("{name}WorldImpl"), &self.world_fragments, files);
365+
}
366+
367+
for (name, fragments) in &self.interface_fragments {
368+
let body = fragments
369+
.iter()
370+
.map(|f| f.src.deref())
371+
.collect::<Vec<_>>()
372+
.join("\n");
373+
374+
let body = format!(
375+
"package {package};
376+
377+
{IMPORTS}
378+
379+
public final class {name} {{
380+
private {name}() {{}}
381+
382+
{body}
383+
}}
384+
"
385+
);
386+
387+
files.push(&format!("{name}.java"), indent(&body).as_bytes());
388+
389+
if self.opts.generate_stub {
390+
generate_stub(format!("{name}Impl"), fragments, files);
391+
}
329392
}
330393
}
331394
}
@@ -356,41 +419,22 @@ impl InterfaceGenerator<'_> {
356419
}
357420
}
358421

359-
fn add_class(self) {
360-
let package = format!("wit_{}", self.gen.name.to_snake_case());
361-
let name = self.name.to_upper_camel_case();
362-
let body = self.src;
363-
let body = format!(
364-
"package {package};
365-
366-
{IMPORTS}
367-
368-
public final class {name} {{
369-
private {name}() {{}}
370-
371-
{body}
372-
}}
373-
"
374-
);
375-
376-
if self.gen.opts.generate_stub {
377-
let name = format!("{name}Impl");
378-
let body = self.stub;
379-
let body = format!(
380-
"package {package};
381-
382-
{IMPORTS}
383-
384-
public class {name} {{
385-
{body}
386-
}}
387-
"
388-
);
389-
390-
self.gen.classes.insert(name, body);
391-
}
422+
fn add_interface_fragment(self) {
423+
self.gen
424+
.interface_fragments
425+
.entry(self.name.to_upper_camel_case())
426+
.or_default()
427+
.push(InterfaceFragment {
428+
src: self.src,
429+
stub: self.stub,
430+
});
431+
}
392432

393-
self.gen.classes.insert(name, body);
433+
fn add_world_fragment(self) {
434+
self.gen.world_fragments.push(InterfaceFragment {
435+
src: self.src,
436+
stub: self.stub,
437+
});
394438
}
395439

396440
fn import(&mut self, module: &str, func: &Function) {

crates/gen-guest-teavm-java/tests/codegen.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::path::Path;
44
use std::process::Command;
55

66
macro_rules! codegen_test {
7-
(worlds_with_types $name:tt $test:tt) => {};
8-
97
($id:ident $name:tt $test:tt) => {
108
#[test]
119
fn $id() {

tests/runtime/lists/ExportsImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package wit_lists;
22

3-
import static wit_lists.ListsImpl.expect;
3+
import static wit_lists.ListsWorldImpl.expect;
44

55
import java.util.ArrayList;
66

tests/runtime/lists/ListsImpl.java renamed to tests/runtime/lists/ListsWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import wit_lists.ListsWorld.Tuple2;
88

9-
public class ListsImpl {
9+
public class ListsWorldImpl {
1010
public static int allocatedBytes() {
1111
return 0;
1212
}

tests/runtime/numbers/NumbersImpl.java renamed to tests/runtime/numbers/NumbersWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package wit_numbers;
22

3-
public class NumbersImpl {
3+
public class NumbersWorldImpl {
44
private static void expect(boolean v) {
55
if (!v) {
66
throw new AssertionError();

tests/runtime/records/RecordsImpl.java renamed to tests/runtime/records/RecordsWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import wit_records.RecordsWorld.Tuple2;
66
import wit_records.RecordsWorld.Tuple4;
77

8-
public class RecordsImpl {
8+
public class RecordsWorldImpl {
99
public static void testImports() {
1010
{
1111
Tuple2<Byte, Short> results = Imports.multipleResults();

tests/runtime/smoke/SmokeImpl.java renamed to tests/runtime/smoke/SmokeWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package wit_smoke;
22

3-
public class SmokeImpl {
3+
public class SmokeWorldImpl {
44
public static void thunk() {
55
Imports.thunk();
66
}

tests/runtime/variants/VariantsImpl.java renamed to tests/runtime/variants/VariantsWorldImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import wit_variants.VariantsWorld.Tuple4;
77
import wit_variants.VariantsWorld.Tuple6;
88

9-
public class VariantsImpl {
9+
public class VariantsWorldImpl {
1010
public static void testImports() {
1111
expect(Imports.roundtripOption(1.0F) == (byte) 1);
1212
expect(Imports.roundtripOption(null) == null);

0 commit comments

Comments
 (0)