@@ -43,6 +43,11 @@ impl Opts {
43
43
}
44
44
}
45
45
46
+ struct InterfaceFragment {
47
+ src : String ,
48
+ stub : String ,
49
+ }
50
+
46
51
#[ derive( Default ) ]
47
52
pub struct TeaVmJava {
48
53
opts : Opts ,
@@ -52,7 +57,8 @@ pub struct TeaVmJava {
52
57
tuple_counts : HashSet < usize > ,
53
58
needs_cleanup : bool ,
54
59
needs_result : bool ,
55
- classes : HashMap < String , String > ,
60
+ interface_fragments : HashMap < String , Vec < InterfaceFragment > > ,
61
+ world_fragments : Vec < InterfaceFragment > ,
56
62
sizes : SizeAlign ,
57
63
interface_names : HashMap < InterfaceId , String > ,
58
64
}
@@ -95,7 +101,7 @@ impl WorldGenerator for TeaVmJava {
95
101
gen. import ( name, func) ;
96
102
}
97
103
98
- gen. add_class ( ) ;
104
+ gen. add_interface_fragment ( ) ;
99
105
}
100
106
101
107
fn import_funcs (
@@ -105,14 +111,14 @@ impl WorldGenerator for TeaVmJava {
105
111
funcs : & [ ( & str , & Function ) ] ,
106
112
_files : & mut Files ,
107
113
) {
108
- let name = & resolve. worlds [ world] . name ;
114
+ let name = & format ! ( "{}-world" , resolve. worlds[ world] . name) ;
109
115
let mut gen = self . interface ( resolve, name) ;
110
116
111
117
for ( _, func) in funcs {
112
118
gen. import ( name, func) ;
113
119
}
114
120
115
- gen. add_class ( ) ;
121
+ gen. add_world_fragment ( ) ;
116
122
}
117
123
118
124
fn export_interface (
@@ -130,7 +136,7 @@ impl WorldGenerator for TeaVmJava {
130
136
gen. export ( func, Some ( name) ) ;
131
137
}
132
138
133
- gen. add_class ( ) ;
139
+ gen. add_interface_fragment ( ) ;
134
140
}
135
141
136
142
fn export_funcs (
@@ -140,14 +146,14 @@ impl WorldGenerator for TeaVmJava {
140
146
funcs : & [ ( & str , & Function ) ] ,
141
147
_files : & mut Files ,
142
148
) {
143
- let name = & resolve. worlds [ world] . name ;
149
+ let name = & format ! ( "{}-world" , resolve. worlds[ world] . name) ;
144
150
let mut gen = self . interface ( resolve, name) ;
145
151
146
152
for ( _, func) in funcs {
147
153
gen. export ( func, None ) ;
148
154
}
149
155
150
- gen. add_class ( ) ;
156
+ gen. add_world_fragment ( ) ;
151
157
}
152
158
153
159
fn export_types (
@@ -157,14 +163,14 @@ impl WorldGenerator for TeaVmJava {
157
163
types : & [ ( & str , TypeId ) ] ,
158
164
_files : & mut Files ,
159
165
) {
160
- let name = & resolve. worlds [ world] . name ;
166
+ let name = & format ! ( "{}-world" , resolve. worlds[ world] . name) ;
161
167
let mut gen = self . interface ( resolve, name) ;
162
168
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) ;
165
171
}
166
172
167
- gen. add_class ( ) ;
173
+ gen. add_world_fragment ( ) ;
168
174
}
169
175
170
176
fn finish ( & mut self , resolve : & Resolve , id : WorldId , files : & mut Files ) {
@@ -186,6 +192,15 @@ impl WorldGenerator for TeaVmJava {
186
192
"
187
193
) ;
188
194
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
+
189
204
let component_type =
190
205
wit_component:: metadata:: encode ( resolve, id, wit_component:: StringEncoding :: UTF8 )
191
206
. unwrap ( )
@@ -324,8 +339,56 @@ impl WorldGenerator for TeaVmJava {
324
339
325
340
files. push ( & format ! ( "{name}World.java" ) , indent ( & src) . as_bytes ( ) ) ;
326
341
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
+ }
329
392
}
330
393
}
331
394
}
@@ -356,41 +419,22 @@ impl InterfaceGenerator<'_> {
356
419
}
357
420
}
358
421
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
+ }
392
432
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
+ } ) ;
394
438
}
395
439
396
440
fn import ( & mut self , module : & str , func : & Function ) {
0 commit comments