@@ -43,6 +43,11 @@ impl Opts {
4343 }
4444}
4545
46+ struct InterfaceFragment {
47+ src : String ,
48+ stub : String ,
49+ }
50+
4651#[ derive( Default ) ]
4752pub 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 ) {
0 commit comments