mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-01 18:01:06 +00: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() {
|
for f in self.program.imports.iter() {
|
||||||
self.generate_import(f);
|
self.generate_import(f);
|
||||||
}
|
}
|
||||||
|
for e in self.program.enums.iter() {
|
||||||
|
self.generate_enum(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_export(&mut self, export: &shared::Export) {
|
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(&dst);
|
||||||
self.cx.globals.push_str("\n");
|
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 {
|
struct VectorType {
|
||||||
|
@ -327,17 +327,24 @@ impl Program {
|
|||||||
a.fields(&[
|
a.fields(&[
|
||||||
("exports", &|a| a.list(&self.exports, Export::wbg_literal)),
|
("exports", &|a| a.list(&self.exports, Export::wbg_literal)),
|
||||||
("imports", &|a| a.list(&self.imports, Import::wbg_literal)),
|
("imports", &|a| a.list(&self.imports, Import::wbg_literal)),
|
||||||
|
("enums", &|a| a.list(&self.enums, Enum::wbg_literal)),
|
||||||
("custom_type_names", &|a| {
|
("custom_type_names", &|a| {
|
||||||
let names = self.exports.iter()
|
let struct_descriptors = self.exports.iter()
|
||||||
.filter_map(|e| e.class)
|
.filter_map(|e| e.class)
|
||||||
.chain(self.structs.iter().map(|s| s.name))
|
.chain(self.structs.iter().map(|s| s.name))
|
||||||
.chain(self.enums.iter().map(|s| s.name))
|
.map(|n| {
|
||||||
.collect::<BTreeSet<_>>();
|
let val = shared::name_to_descriptor(n.as_ref());
|
||||||
a.list(&names, |s, a| {
|
(val, n.to_string())
|
||||||
let val = shared::name_to_descriptor(s.as_ref());
|
});
|
||||||
|
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(&[
|
a.fields(&[
|
||||||
("descriptor", &|a| a.char(val)),
|
("descriptor", &|a| a.char(s.0)),
|
||||||
("name", &|a| a.str(s.as_ref()))
|
("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 {
|
impl Struct {
|
||||||
fn from(s: syn::ItemStruct, _opts: BindgenAttrs) -> Struct {
|
fn from(s: syn::ItemStruct, _opts: BindgenAttrs) -> Struct {
|
||||||
Struct { name: s.ident }
|
Struct { name: s.ident }
|
||||||
|
@ -8,6 +8,7 @@ use std::hash::{Hash, Hasher};
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub exports: Vec<Export>,
|
pub exports: Vec<Export>,
|
||||||
|
pub enums: Vec<Enum>,
|
||||||
pub imports: Vec<Import>,
|
pub imports: Vec<Import>,
|
||||||
pub custom_type_names: Vec<CustomTypeName>,
|
pub custom_type_names: Vec<CustomTypeName>,
|
||||||
}
|
}
|
||||||
@ -32,6 +33,17 @@ pub struct Export {
|
|||||||
pub function: Function,
|
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)]
|
#[derive(Deserialize)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user