From f11121b0954f9b2eb66d968ea3f0282318e73b12 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Thu, 22 Feb 2018 12:01:38 +0100 Subject: [PATCH] Generate enum js code --- crates/wasm-bindgen-cli-support/src/js.rs | 16 +++++++++++ crates/wasm-bindgen-macro/src/ast.rs | 34 ++++++++++++++++++----- crates/wasm-bindgen-shared/src/lib.rs | 12 ++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs index 37ec69fc..56a82442 100644 --- a/crates/wasm-bindgen-cli-support/src/js.rs +++ b/crates/wasm-bindgen-cli-support/src/js.rs @@ -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 { diff --git a/crates/wasm-bindgen-macro/src/ast.rs b/crates/wasm-bindgen-macro/src/ast.rs index 2904ed0c..5b75e647 100644 --- a/crates/wasm-bindgen-macro/src/ast.rs +++ b/crates/wasm-bindgen-macro/src/ast.rs @@ -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::>(); - 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::>(); + + 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 } diff --git a/crates/wasm-bindgen-shared/src/lib.rs b/crates/wasm-bindgen-shared/src/lib.rs index 2047ce99..7d85eeeb 100644 --- a/crates/wasm-bindgen-shared/src/lib.rs +++ b/crates/wasm-bindgen-shared/src/lib.rs @@ -8,6 +8,7 @@ use std::hash::{Hash, Hasher}; #[derive(Deserialize)] pub struct Program { pub exports: Vec, + pub enums: Vec, pub imports: Vec, pub custom_type_names: Vec, } @@ -32,6 +33,17 @@ pub struct Export { pub function: Function, } +#[derive(Deserialize)] +pub struct Enum { + pub name: String, + pub variants: Vec, +} + +#[derive(Deserialize)] +pub struct EnumVariant { + pub name: String +} + #[derive(Deserialize)] pub struct Function { pub name: String,