@@ -35,13 +35,15 @@ pub struct Context<'a> {
35
35
36
36
#[ derive( Default ) ]
37
37
pub struct ExportedClass {
38
+ comments : String ,
38
39
contents : String ,
39
40
typescript : String ,
40
41
constructor : Option < String > ,
41
42
fields : Vec < ClassField > ,
42
43
}
43
44
44
45
struct ClassField {
46
+ comments : String ,
45
47
name : String ,
46
48
readonly : bool ,
47
49
}
@@ -52,9 +54,12 @@ pub struct SubContext<'a, 'b: 'a> {
52
54
}
53
55
54
56
impl < ' a > Context < ' a > {
55
- fn export ( & mut self , name : & str , contents : & str ) {
57
+ fn export ( & mut self , name : & str , contents : & str , comments : Option < String > ) {
56
58
let contents = deindent ( contents) ;
57
59
let contents = contents. trim ( ) ;
60
+ if let Some ( ref c) = comments {
61
+ self . globals . push_str ( c) ;
62
+ }
58
63
let global = if self . config . nodejs {
59
64
if contents. starts_with ( "class" ) {
60
65
format ! ( "{1}\n module.exports.{0} = {0};\n " , name, contents)
@@ -396,7 +401,7 @@ impl<'a> Context<'a> {
396
401
. with_context ( |_| {
397
402
format ! ( "failed to generate internal JS function `{}`" , name)
398
403
} ) ?;
399
- self . export ( name, & contents) ;
404
+ self . export ( name, & contents, None ) ;
400
405
Ok ( ( ) )
401
406
}
402
407
@@ -461,7 +466,7 @@ impl<'a> Context<'a> {
461
466
function(ptr) {{
462
467
return addHeapObject({}.__construct(ptr));
463
468
}}
464
- " , name) ) ;
469
+ " , name) , None ) ;
465
470
}
466
471
467
472
for field in class. fields . iter ( ) {
@@ -484,7 +489,7 @@ impl<'a> Context<'a> {
484
489
. method ( true )
485
490
. ret ( & Some ( descriptor) ) ?
486
491
. finish ( "" , & format ! ( "wasm.{}" , wasm_getter) ) ;
487
-
492
+ dst . push_str ( & field . comments ) ;
488
493
dst. push_str ( "get " ) ;
489
494
dst. push_str ( & field. name ) ;
490
495
dst. push_str ( & get) ;
@@ -510,7 +515,7 @@ impl<'a> Context<'a> {
510
515
dst. push_str ( "}\n " ) ;
511
516
ts_dst. push_str ( "}\n " ) ;
512
517
513
- self . export ( & name, & dst) ;
518
+ self . export ( & name, & dst, Some ( class . comments . clone ( ) ) ) ;
514
519
self . typescript . push_str ( & ts_dst) ;
515
520
516
521
Ok ( ( ) )
@@ -534,7 +539,7 @@ impl<'a> Context<'a> {
534
539
535
540
fn rewrite_imports ( & mut self , module_name : & str ) {
536
541
for ( name, contents) in self . _rewrite_imports ( module_name) {
537
- self . export ( & name, & contents) ;
542
+ self . export ( & name, & contents, None ) ;
538
543
}
539
544
}
540
545
@@ -691,7 +696,7 @@ impl<'a> Context<'a> {
691
696
return;
692
697
throw new Error('stack is not currently empty');
693
698
}
694
- " ) ;
699
+ " , None ) ;
695
700
}
696
701
}
697
702
@@ -715,7 +720,7 @@ impl<'a> Context<'a> {
715
720
throw new Error('slab is not currently empty');
716
721
}}
717
722
}}
718
- " , initial_values. len( ) ) ) ;
723
+ " , initial_values. len( ) ) , None ) ;
719
724
}
720
725
}
721
726
@@ -1406,7 +1411,7 @@ impl<'a> Context<'a> {
1406
1411
1407
1412
// Ensure a blank line between adjacent items, and ensure everything is
1408
1413
// terminated with a newline.
1409
- while !self . globals . ends_with ( "\n \n \n " ) {
1414
+ while !self . globals . ends_with ( "\n \n \n " ) && ! self . globals . ends_with ( "*/ \n " ) {
1410
1415
self . globals . push_str ( "\n " ) ;
1411
1416
}
1412
1417
self . globals . push_str ( s) ;
@@ -1452,14 +1457,16 @@ impl<'a, 'b> SubContext<'a, 'b> {
1452
1457
self . generate_enum ( e) ;
1453
1458
}
1454
1459
for s in self . program . structs . iter ( ) {
1455
- self . cx . exported_classes
1460
+ let mut class = self . cx . exported_classes
1456
1461
. entry ( s. name . clone ( ) )
1457
- . or_insert_with ( Default :: default)
1458
- . fields
1459
- . extend ( s. fields . iter ( ) . map ( |s| {
1462
+ . or_insert_with ( Default :: default) ;
1463
+ class. comments = format_doc_comments ( & s. comments ) ;
1464
+ class. fields
1465
+ . extend ( s. fields . iter ( ) . map ( |f| {
1460
1466
ClassField {
1461
- name : s. name . clone ( ) ,
1462
- readonly : s. readonly ,
1467
+ name : f. name . clone ( ) ,
1468
+ readonly : f. readonly ,
1469
+ comments : format_doc_comments ( & f. comments ) ,
1463
1470
}
1464
1471
} ) ) ;
1465
1472
}
@@ -1477,7 +1484,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
1477
1484
let ( js, ts) = Js2Rust :: new ( & export. function . name , self . cx )
1478
1485
. process ( descriptor. unwrap_function ( ) ) ?
1479
1486
. finish ( "function" , & format ! ( "wasm.{}" , export. function. name) ) ;
1480
- self . cx . export ( & export. function . name , & js) ;
1487
+ self . cx . export ( & export. function . name , & js, Some ( format_doc_comments ( & export . comments ) ) ) ;
1481
1488
self . cx . globals . push_str ( "\n " ) ;
1482
1489
self . cx . typescript . push_str ( "export " ) ;
1483
1490
self . cx . typescript . push_str ( & ts) ;
@@ -1593,7 +1600,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
1593
1600
function() {{
1594
1601
return addHeapObject({});
1595
1602
}}
1596
- " , obj) ) ;
1603
+ " , obj) , None ) ;
1597
1604
Ok ( ( ) )
1598
1605
}
1599
1606
@@ -1688,7 +1695,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
1688
1695
. catch ( import. catch )
1689
1696
. process ( descriptor. unwrap_function ( ) ) ?
1690
1697
. finish ( & target) ;
1691
- self . cx . export ( & import. shim , & js) ;
1698
+ self . cx . export ( & import. shim , & js, None ) ;
1692
1699
Ok ( ( ) )
1693
1700
}
1694
1701
@@ -1698,7 +1705,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
1698
1705
for variant in enum_. variants . iter ( ) {
1699
1706
variants. push_str ( & format ! ( "{}:{}," , variant. name, variant. value) ) ;
1700
1707
}
1701
- self . cx . export ( & enum_. name , & format ! ( "Object.freeze({{ {} }})" , variants) ) ;
1708
+ self . cx . export ( & enum_. name , & format ! ( "Object.freeze({{ {} }})" , variants) , Some ( format_doc_comments ( & enum_ . comments ) ) ) ;
1702
1709
self . cx . typescript . push_str ( & format ! ( "export enum {} {{" , enum_. name) ) ;
1703
1710
1704
1711
variants. clear ( ) ;
@@ -1764,3 +1771,9 @@ fn deindent(s: &str) -> String {
1764
1771
}
1765
1772
ret
1766
1773
}
1774
+
1775
+
1776
+ fn format_doc_comments ( comments : & Vec < String > ) -> String {
1777
+ let body: String = comments. iter ( ) . map ( |c| format ! ( "*{}\n " , c. trim_matches( '"' ) ) ) . collect ( ) ;
1778
+ format ! ( "/**\n {}*/\n " , body)
1779
+ }
0 commit comments