@@ -13,6 +13,10 @@ use wasm_bindgen_shared as shared;
13
13
pub trait TryToTokens {
14
14
fn try_to_tokens ( & self , tokens : & mut TokenStream ) -> Result < ( ) , Diagnostic > ;
15
15
16
+ fn try_identifier_to_tokens ( & self , _: & mut TokenStream ) -> Result < ( ) , Diagnostic > {
17
+ Ok ( ( ) )
18
+ }
19
+
16
20
fn try_to_token_stream ( & self ) -> Result < TokenStream , Diagnostic > {
17
21
let mut tokens = TokenStream :: new ( ) ;
18
22
self . try_to_tokens ( & mut tokens) ?;
@@ -101,11 +105,8 @@ impl TryToTokens for ast::Program {
101
105
prefix_bytes. push ( ( prefix_json. len ( ) >> 24 ) as u8 ) ;
102
106
prefix_bytes. extend_from_slice ( prefix_json. as_bytes ( ) ) ;
103
107
104
- let generated_static_length =
105
- prefix_bytes. len ( ) +
106
- custom_section. iter ( ) . map ( encode:: EncodePart :: len) . sum :: < usize > ( )
107
- ;
108
-
108
+ let generated_static_length = prefix_bytes. len ( ) + encoded. size as usize ;
109
+
109
110
// We already consumed the contents of included files when generating
110
111
// the custom section, but we want to make sure that updates to the
111
112
// generated files will cause this macro to rerun incrementally. To do
@@ -128,25 +129,37 @@ impl TryToTokens for ast::Program {
128
129
pub static #generated_static_name: [ u8 ; #generated_static_length] = {
129
130
static _INCLUDED_FILES: & [ & str ] = & [ #( #file_dependencies) , * ] ;
130
131
131
- [ #( #prefix_bytes) , * , #( # custom_section) , * ]
132
+ [ #( #prefix_bytes) , * , #custom_section ]
132
133
} ;
133
134
134
135
} )
135
136
. to_tokens ( tokens) ;
136
137
137
138
Ok ( ( ) )
138
139
}
140
+
141
+ fn try_identifier_to_tokens ( & self , tokens : & mut TokenStream ) -> Result < ( ) , Diagnostic > {
142
+ let mut errors = Vec :: new ( ) ;
143
+
144
+ for export in self . exports . iter ( ) {
145
+ if let Err ( e) = export. try_identifier_to_tokens ( tokens) {
146
+ errors. push ( e) ;
147
+ }
148
+ }
149
+
150
+ Diagnostic :: from_vec ( errors) ?;
151
+ Ok ( ( ) )
152
+ }
139
153
}
140
154
141
155
fn gen_id ( ) -> [ u8 ; 8 ] {
142
- use :: std:: time:: { SystemTime , UNIX_EPOCH } ;
143
-
144
- let duration = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) ;
145
-
146
- let mut bytes = duration. as_secs ( ) . to_be_bytes ( ) ;
147
- bytes[ 0 ..4 ] . copy_from_slice ( & duration. subsec_nanos ( ) . to_be_bytes ( ) ) ;
148
-
149
- bytes
156
+ static CNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
157
+ u64:: from_str_radix (
158
+ & ShortHash ( CNT . fetch_add ( 1 , Ordering :: SeqCst ) ) . to_string ( ) ,
159
+ 16
160
+ )
161
+ . unwrap ( )
162
+ . to_be_bytes ( )
150
163
}
151
164
152
165
impl ToTokens for ast:: Struct {
@@ -277,6 +290,29 @@ impl ToTokens for ast::Struct {
277
290
} )
278
291
. to_tokens ( tokens) ;
279
292
293
+ if let Some ( parent) = & self . parent {
294
+ ( quote ! {
295
+ impl From <#name> for #parent {
296
+ fn from( child: #name) -> #parent {
297
+ unimplemented!( )
298
+ }
299
+ }
300
+
301
+ impl AsRef <#parent> for #name {
302
+ fn as_ref( & self ) -> & #parent {
303
+ unimplemented!( )
304
+ }
305
+ }
306
+
307
+ impl AsMut <#parent> for #name {
308
+ fn as_mut( & mut self ) -> & mut #parent {
309
+ unimplemented!( )
310
+ }
311
+ }
312
+ } )
313
+ . to_tokens ( tokens) ;
314
+ }
315
+
280
316
for field in self . fields . iter ( ) {
281
317
field. to_tokens ( tokens) ;
282
318
}
@@ -481,6 +517,11 @@ impl TryToTokens for ast::Export {
481
517
quote ! { }
482
518
} ;
483
519
520
+ let const_name = encode:: lookup_constant_for_fn ( name) ;
521
+ let id_bytes = gen_id ( ) ;
522
+
523
+ ( quote ! { const #const_name: [ u8 ; 8 ] = [ #( #id_bytes) , * ] ; } ) . to_tokens ( into) ;
524
+
484
525
( quote ! {
485
526
#( #attrs) *
486
527
#[ export_name = #export_name]
@@ -532,6 +573,15 @@ impl TryToTokens for ast::Export {
532
573
533
574
Ok ( ( ) )
534
575
}
576
+
577
+ fn try_identifier_to_tokens ( self : & ast:: Export , into : & mut TokenStream ) -> Result < ( ) , Diagnostic > {
578
+ let const_name = encode:: lookup_constant_for_fn ( & self . rust_name ) ;
579
+ let id_bytes = gen_id ( ) ;
580
+
581
+ ( quote ! { const #const_name: [ u8 ; 8 ] = [ #( #id_bytes) , * ] ; } ) . to_tokens ( into) ;
582
+
583
+ Ok ( ( ) )
584
+ }
535
585
}
536
586
537
587
impl TryToTokens for ast:: ImportKind {
@@ -1014,7 +1064,12 @@ impl TryToTokens for ast::ImportFunction {
1014
1064
& self . rust_name ,
1015
1065
) ;
1016
1066
1067
+ let const_name = encode:: lookup_constant_for_fn ( & self . rust_name ) ;
1068
+ let id_bytes = gen_id ( ) ;
1069
+
1017
1070
let invocation = quote ! {
1071
+ const #const_name: [ u8 ; 8 ] = [ #( #id_bytes) , * ] ;
1072
+
1018
1073
#( #attrs) *
1019
1074
#[ allow( bad_style) ]
1020
1075
#[ doc = #doc_comment]
0 commit comments