Skip to content

Commit 3841ba9

Browse files
committed
Combined updates giving effect to latest proposals
1 parent 8b9ac19 commit 3841ba9

File tree

20 files changed

+695
-197
lines changed

20 files changed

+695
-197
lines changed

crates/backend/src/ast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,17 @@ pub enum ImportFunctionKind {
139139
Normal,
140140
}
141141

142+
#[cfg_attr(feature = "extra-traits", derive(Debug))]
143+
#[derive(Clone)]
144+
pub enum SuperArgs {
145+
Constant(syn::ExprArray),
146+
Dynamic(syn::Path),
147+
}
148+
142149
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
143150
#[derive(Clone)]
144151
pub enum MethodKind {
145-
Constructor,
152+
Constructor(Option<SuperArgs>),
146153
Operation(Operation),
147154
}
148155

crates/backend/src/codegen.rs

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use wasm_bindgen_shared as shared;
1313
pub trait TryToTokens {
1414
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic>;
1515

16+
fn try_identifier_to_tokens(&self, _: &mut TokenStream) -> Result<(), Diagnostic> {
17+
Ok(())
18+
}
19+
1620
fn try_to_token_stream(&self) -> Result<TokenStream, Diagnostic> {
1721
let mut tokens = TokenStream::new();
1822
self.try_to_tokens(&mut tokens)?;
@@ -101,11 +105,8 @@ impl TryToTokens for ast::Program {
101105
prefix_bytes.push((prefix_json.len() >> 24) as u8);
102106
prefix_bytes.extend_from_slice(prefix_json.as_bytes());
103107

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+
109110
// We already consumed the contents of included files when generating
110111
// the custom section, but we want to make sure that updates to the
111112
// generated files will cause this macro to rerun incrementally. To do
@@ -128,25 +129,37 @@ impl TryToTokens for ast::Program {
128129
pub static #generated_static_name: [u8; #generated_static_length] = {
129130
static _INCLUDED_FILES: &[&str] = &[#(#file_dependencies),*];
130131

131-
[ #(#prefix_bytes),* , #(#custom_section),* ]
132+
[ #(#prefix_bytes),* , #custom_section ]
132133
};
133134

134135
})
135136
.to_tokens(tokens);
136137

137138
Ok(())
138139
}
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+
}
139153
}
140154

141155
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()
150163
}
151164

152165
impl ToTokens for ast::Struct {
@@ -277,6 +290,29 @@ impl ToTokens for ast::Struct {
277290
})
278291
.to_tokens(tokens);
279292

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+
280316
for field in self.fields.iter() {
281317
field.to_tokens(tokens);
282318
}
@@ -481,6 +517,11 @@ impl TryToTokens for ast::Export {
481517
quote! {}
482518
};
483519

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+
484525
(quote! {
485526
#(#attrs)*
486527
#[export_name = #export_name]
@@ -532,6 +573,15 @@ impl TryToTokens for ast::Export {
532573

533574
Ok(())
534575
}
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+
}
535585
}
536586

537587
impl TryToTokens for ast::ImportKind {
@@ -1014,7 +1064,12 @@ impl TryToTokens for ast::ImportFunction {
10141064
&self.rust_name,
10151065
);
10161066

1067+
let const_name = encode::lookup_constant_for_fn(&self.rust_name);
1068+
let id_bytes = gen_id();
1069+
10171070
let invocation = quote! {
1071+
const #const_name: [u8; 8] = [#(#id_bytes),*];
1072+
10181073
#(#attrs)*
10191074
#[allow(bad_style)]
10201075
#[doc = #doc_comment]

0 commit comments

Comments
 (0)