Skip to content

Commit

Permalink
wasm-bindgen support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Mar 13, 2019
1 parent 26adada commit 1ee3ef1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions prost-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
36 changes: 35 additions & 1 deletion prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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",
);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1ee3ef1

Please sign in to comment.