mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-24 18:43:33 +03:00
Generate enum js code
This commit is contained in:
parent
45543c545e
commit
f11121b095
@ -978,6 +978,9 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
for f in self.program.imports.iter() {
|
||||
self.generate_import(f);
|
||||
}
|
||||
for e in self.program.enums.iter() {
|
||||
self.generate_enum(e);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_export(&mut self, export: &shared::Export) {
|
||||
@ -1424,6 +1427,19 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
self.cx.globals.push_str(&dst);
|
||||
self.cx.globals.push_str("\n");
|
||||
}
|
||||
|
||||
pub fn generate_enum(&mut self, enum_: &shared::Enum) {
|
||||
let mut variants = String::new();
|
||||
|
||||
let mut value = 0;
|
||||
for variant in enum_.variants.iter() {
|
||||
variants.push_str(&format!("{}:{},", variant.name, value));
|
||||
value = value + 1;
|
||||
}
|
||||
self.cx.globals.push_str(&format!("export const {} = {{", enum_.name));
|
||||
self.cx.globals.push_str(&variants);
|
||||
self.cx.globals.push_str("}\n");
|
||||
}
|
||||
}
|
||||
|
||||
struct VectorType {
|
||||
|
@ -327,17 +327,24 @@ impl Program {
|
||||
a.fields(&[
|
||||
("exports", &|a| a.list(&self.exports, Export::wbg_literal)),
|
||||
("imports", &|a| a.list(&self.imports, Import::wbg_literal)),
|
||||
("enums", &|a| a.list(&self.enums, Enum::wbg_literal)),
|
||||
("custom_type_names", &|a| {
|
||||
let names = self.exports.iter()
|
||||
let struct_descriptors = self.exports.iter()
|
||||
.filter_map(|e| e.class)
|
||||
.chain(self.structs.iter().map(|s| s.name))
|
||||
.chain(self.enums.iter().map(|s| s.name))
|
||||
.collect::<BTreeSet<_>>();
|
||||
a.list(&names, |s, a| {
|
||||
let val = shared::name_to_descriptor(s.as_ref());
|
||||
.map(|n| {
|
||||
let val = shared::name_to_descriptor(n.as_ref());
|
||||
(val, n.to_string())
|
||||
});
|
||||
let enum_descriptors = self.enums.iter().map(|e| {
|
||||
(shared::TYPE_ENUM, e.name.to_string())
|
||||
});
|
||||
let descriptors = struct_descriptors.chain(enum_descriptors).collect::<BTreeSet<_>>();
|
||||
|
||||
a.list(&descriptors, |s, a| {
|
||||
a.fields(&[
|
||||
("descriptor", &|a| a.char(val)),
|
||||
("name", &|a| a.str(s.as_ref()))
|
||||
("descriptor", &|a| a.char(s.0)),
|
||||
("name", &|a| a.str(&s.1))
|
||||
]);
|
||||
})
|
||||
}),
|
||||
@ -665,6 +672,19 @@ impl Import {
|
||||
}
|
||||
}
|
||||
|
||||
impl Enum {
|
||||
fn wbg_literal(&self, a: &mut LiteralBuilder) {
|
||||
a.fields(&[
|
||||
("name", &|a| a.str(self.name.as_ref())),
|
||||
("variants", &|a| a.list(&self.variants, |v, a| {
|
||||
a.fields(&[
|
||||
("name", &|a| a.str(v.as_ref()))
|
||||
])
|
||||
})),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
impl Struct {
|
||||
fn from(s: syn::ItemStruct, _opts: BindgenAttrs) -> Struct {
|
||||
Struct { name: s.ident }
|
||||
|
@ -8,6 +8,7 @@ use std::hash::{Hash, Hasher};
|
||||
#[derive(Deserialize)]
|
||||
pub struct Program {
|
||||
pub exports: Vec<Export>,
|
||||
pub enums: Vec<Enum>,
|
||||
pub imports: Vec<Import>,
|
||||
pub custom_type_names: Vec<CustomTypeName>,
|
||||
}
|
||||
@ -32,6 +33,17 @@ pub struct Export {
|
||||
pub function: Function,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Enum {
|
||||
pub name: String,
|
||||
pub variants: Vec<EnumVariant>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct EnumVariant {
|
||||
pub name: String
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Function {
|
||||
pub name: String,
|
||||
|
Loading…
Reference in New Issue
Block a user