@@ -43,6 +43,8 @@ impl TryToTokens for ast::Program {
43
43
for i in self . imports . iter ( ) {
44
44
DescribeImport ( & i. kind ) . to_tokens ( tokens) ;
45
45
46
+ // If there is a js namespace, check that name isn't a type. If it is,
47
+ // this import might be a method on that type.
46
48
if let Some ( ns) = & i. js_namespace {
47
49
if types. contains ( ns) && i. kind . fits_on_impl ( ) {
48
50
let kind = match i. kind . try_to_token_stream ( ) {
@@ -61,6 +63,11 @@ impl TryToTokens for ast::Program {
61
63
errors. push ( e) ;
62
64
}
63
65
}
66
+ for m in self . modules . iter ( ) {
67
+ if let Err ( e) = ModuleInIter :: from ( m) . try_to_tokens ( tokens) {
68
+ errors. push ( e) ;
69
+ }
70
+ }
64
71
for e in self . enums . iter ( ) {
65
72
e. to_tokens ( tokens) ;
66
73
}
@@ -87,6 +94,7 @@ impl TryToTokens for ast::Program {
87
94
// Each JSON blob is prepended with the length of the JSON blob so when
88
95
// all these sections are concatenated in the final wasm file we know
89
96
// how to extract all the JSON pieces, so insert the byte length here.
97
+ // The value is little-endian.
90
98
let generated_static_length = description. len ( ) + 4 ;
91
99
let mut bytes = vec ! [
92
100
( description. len( ) >> 0 ) as u8 ,
@@ -1103,6 +1111,43 @@ impl ToTokens for ast::Const {
1103
1111
}
1104
1112
}
1105
1113
1114
+ /// Struct to help implementing TryToTokens over the key/value pairs from the hashmap.
1115
+ struct ModuleInIter < ' a > {
1116
+ name : & ' a Ident ,
1117
+ module : & ' a ast:: Module
1118
+ }
1119
+
1120
+ impl < ' a > From < ( & ' a Ident , & ' a ast:: Module ) > for ModuleInIter < ' a > {
1121
+ fn from ( ( name, module) : ( & ' a Ident , & ' a ast:: Module ) ) -> ModuleInIter < ' a > {
1122
+ ModuleInIter { name, module }
1123
+ }
1124
+ }
1125
+
1126
+ impl < ' a > TryToTokens for ModuleInIter < ' a > {
1127
+ fn try_to_tokens ( & self , tokens : & mut TokenStream ) -> Result < ( ) , Diagnostic > {
1128
+ let name = & self . name ;
1129
+ let imports = & self . module . imports ;
1130
+ let mut errors = Vec :: new ( ) ;
1131
+ for i in imports. iter ( ) {
1132
+ DescribeImport ( & i. kind ) . to_tokens ( tokens) ;
1133
+ }
1134
+ let vis = & self . module . vis ;
1135
+ let mut body = TokenStream :: new ( ) ;
1136
+ for i in imports. iter ( ) {
1137
+ if let Err ( e) = i. kind . try_to_tokens ( & mut body) {
1138
+ errors. push ( e) ;
1139
+ }
1140
+ }
1141
+ Diagnostic :: from_vec ( errors) ?;
1142
+ ( quote ! {
1143
+ #vis mod #name {
1144
+ #body
1145
+ }
1146
+ } ) . to_tokens ( tokens) ;
1147
+ Ok ( ( ) )
1148
+ }
1149
+ }
1150
+
1106
1151
/// Emits the necessary glue tokens for "descriptor", generating an appropriate
1107
1152
/// symbol name as well as attributes around the descriptor function itself.
1108
1153
struct Descriptor < ' a , T > ( & ' a Ident , T ) ;
0 commit comments