Skip to content

Commit 5fa320e

Browse files
committed
wip: wasm-bindgen support
1 parent 278e8ab commit 5fa320e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

prost-build/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ petgraph = { version = "0.5", default-features = false }
1919
prost = { version = "0.6.1", path = "..", default-features = false }
2020
prost-types = { version = "0.6.1", path = "../prost-types", default-features = false }
2121
tempfile = "3"
22+
wasm-bindgen = { version = "0.2", optional = true }
2223

2324
[build-dependencies]
2425
which = { version = "4", default-features = false }
2526

2627
[dev-dependencies]
2728
env_logger = { version = "0.8", default-features = false }
29+
30+
[features]
31+
default = []
32+
use-wasm-bindgen = []

prost-build/src/code_generator.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ impl<'a> CodeGenerator<'a> {
9696
code_gen.package
9797
);
9898

99+
// this should only be called once and not per file
100+
// if cfg!(feature = "use-wasm-bindgen") {
101+
code_gen.buf
102+
.push_str("use wasm_bindgen::prelude::*;\n\n");
103+
// }
104+
99105
code_gen.path.push(4);
100106
for (idx, message) in file.message_type.into_iter().enumerate() {
101107
code_gen.path.push(idx as i32);
@@ -188,6 +194,11 @@ impl<'a> CodeGenerator<'a> {
188194
self.append_doc();
189195
self.append_type_attributes(&fq_message_name);
190196
self.push_indent();
197+
// if cfg!(feature = "use-wasm-bindgen") {
198+
self.buf
199+
.push_str("#[wasm_bindgen]\n");
200+
self.push_indent();
201+
// }
191202
self.buf
192203
.push_str("#[derive(Clone, PartialEq, ::prost::Message)]\n");
193204
self.push_indent();
@@ -388,7 +399,13 @@ impl<'a> CodeGenerator<'a> {
388399
self.buf.push_str("\")]\n");
389400
self.append_field_attributes(msg_name, field.name());
390401
self.push_indent();
391-
self.buf.push_str("pub ");
402+
// see https://github.com/rustwasm/wasm-bindgen/issues/439
403+
// pub not working in structs for vectors
404+
if !repeated
405+
// && cfg!(feature = "use-wasm-bindgen")
406+
{
407+
self.buf.push_str("pub ");
408+
}
392409
self.buf.push_str(&to_snake(field.name()));
393410
self.buf.push_str(": ");
394411
if repeated {
@@ -508,6 +525,12 @@ impl<'a> CodeGenerator<'a> {
508525

509526
let oneof_name = format!("{}.{}", msg_name, oneof.name());
510527
self.append_type_attributes(&oneof_name);
528+
// wasm-bindgen does not support enums holding data
529+
// this might need a lot more changes to work
530+
531+
// self.push_indent();
532+
// self.buf
533+
// .push_str("#[wasm_bindgen]\n");
511534
self.push_indent();
512535
self.buf
513536
.push_str("#[derive(Clone, PartialEq, ::prost::Oneof)]\n");
@@ -593,6 +616,11 @@ impl<'a> CodeGenerator<'a> {
593616
self.append_doc();
594617
self.append_type_attributes(&fq_enum_name);
595618
self.push_indent();
619+
// if cfg!(feature = "use-wasm-bindgen") {
620+
self.buf
621+
.push_str("#[wasm_bindgen]\n");
622+
self.push_indent();
623+
// }
596624
self.buf.push_str(
597625
"#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]\n",
598626
);
@@ -726,6 +754,12 @@ impl<'a> CodeGenerator<'a> {
726754
self.package.push_str(module);
727755

728756
self.depth += 1;
757+
758+
// if cfg!(feature = "use-wasm-bindgen") {
759+
self.push_indent();
760+
self.buf
761+
.push_str("use wasm_bindgen::prelude::*;\n\n");
762+
// }
729763
}
730764

731765
fn pop_mod(&mut self) {

0 commit comments

Comments
 (0)