Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] wasm-bindgen support #167

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions prost-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
36 changes: 35 additions & 1 deletion prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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",
);
Expand Down Expand Up @@ -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) {
Expand Down