diff --git a/prost-build/Cargo.toml b/prost-build/Cargo.toml index c8c9d19c3..08e48a3d7 100644 --- a/prost-build/Cargo.toml +++ b/prost-build/Cargo.toml @@ -19,9 +19,14 @@ petgraph = { version = "0.5", default-features = false } prost = { version = "0.6.1", path = "..", default-features = false } prost-types = { version = "0.6.1", path = "../prost-types", default-features = false } tempfile = "3" +wasm-bindgen = { version = "0.2", optional = true } [build-dependencies] which = { version = "4", default-features = false } [dev-dependencies] env_logger = { version = "0.8", default-features = false } + +[features] +default = [] +use-wasm-bindgen = [] diff --git a/prost-build/src/code_generator.rs b/prost-build/src/code_generator.rs index 500c8cadb..1ade8c27e 100644 --- a/prost-build/src/code_generator.rs +++ b/prost-build/src/code_generator.rs @@ -96,6 +96,12 @@ impl<'a> CodeGenerator<'a> { code_gen.package ); + // this should only be called once and not per file + // if cfg!(feature = "use-wasm-bindgen") { + code_gen.buf + .push_str("use wasm_bindgen::prelude::*;\n\n"); + // } + code_gen.path.push(4); for (idx, message) in file.message_type.into_iter().enumerate() { code_gen.path.push(idx as i32); @@ -188,6 +194,11 @@ impl<'a> CodeGenerator<'a> { self.append_doc(); self.append_type_attributes(&fq_message_name); self.push_indent(); + // if cfg!(feature = "use-wasm-bindgen") { + self.buf + .push_str("#[wasm_bindgen]\n"); + self.push_indent(); + // } self.buf .push_str("#[derive(Clone, PartialEq, ::prost::Message)]\n"); self.push_indent(); @@ -388,7 +399,13 @@ impl<'a> CodeGenerator<'a> { self.buf.push_str("\")]\n"); self.append_field_attributes(msg_name, field.name()); self.push_indent(); - self.buf.push_str("pub "); + // see https://github.com/rustwasm/wasm-bindgen/issues/439 + // pub not working in structs for vectors + if !repeated + // && cfg!(feature = "use-wasm-bindgen") + { + self.buf.push_str("pub "); + } self.buf.push_str(&to_snake(field.name())); self.buf.push_str(": "); if repeated { @@ -508,6 +525,12 @@ impl<'a> CodeGenerator<'a> { let oneof_name = format!("{}.{}", msg_name, oneof.name()); self.append_type_attributes(&oneof_name); + // wasm-bindgen does not support enums holding data + // this might need a lot more changes to work + + // self.push_indent(); + // self.buf + // .push_str("#[wasm_bindgen]\n"); self.push_indent(); self.buf .push_str("#[derive(Clone, PartialEq, ::prost::Oneof)]\n"); @@ -593,6 +616,11 @@ impl<'a> CodeGenerator<'a> { self.append_doc(); self.append_type_attributes(&fq_enum_name); self.push_indent(); + // if cfg!(feature = "use-wasm-bindgen") { + self.buf + .push_str("#[wasm_bindgen]\n"); + self.push_indent(); + // } self.buf.push_str( "#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]\n", ); @@ -726,6 +754,12 @@ impl<'a> CodeGenerator<'a> { self.package.push_str(module); self.depth += 1; + + // if cfg!(feature = "use-wasm-bindgen") { + self.push_indent(); + self.buf + .push_str("use wasm_bindgen::prelude::*;\n\n"); + // } } fn pop_mod(&mut self) {