From 1ee3ef1e5bf219c820824a03099558148aac5a5a Mon Sep 17 00:00:00 2001 From: Mario Reder Date: Wed, 13 Mar 2019 07:37:21 +0100 Subject: [PATCH] wasm-bindgen support --- prost-build/Cargo.toml | 5 +++++ prost-build/src/code_generator.rs | 36 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/prost-build/Cargo.toml b/prost-build/Cargo.toml index 008110768..ac2bc2fc8 100644 --- a/prost-build/Cargo.toml +++ b/prost-build/Cargo.toml @@ -20,9 +20,14 @@ petgraph = { version = "0.4", default-features = false } prost = { version = "0.5.0", path = ".." } prost-types = { version = "0.5.0", path = "../prost-types" } tempfile = "3" +wasm-bindgen = { version = "0.2", optional = true } [build-dependencies] which = "2" [dev-dependencies] env_logger = { version = "0.6", 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 adafa41f6..860b634f4 100644 --- a/prost-build/src/code_generator.rs +++ b/prost-build/src/code_generator.rs @@ -80,6 +80,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); @@ -176,6 +182,11 @@ impl<'a> CodeGenerator<'a> { self.append_doc(); 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.append_type_attributes(&fq_message_name); @@ -367,7 +378,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 { @@ -483,6 +500,12 @@ impl<'a> CodeGenerator<'a> { self.path.pop(); self.path.pop(); + // 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"); @@ -570,6 +593,11 @@ impl<'a> CodeGenerator<'a> { self.append_doc(); 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", ); @@ -701,6 +729,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) {