Skip to content

Commit

Permalink
Remove wasm-bindgen deps from rust (#343)
Browse files Browse the repository at this point in the history
Rust CML crates must use wasm-bindgen for enums as these are used from
the WASM CML crates.

Using noop_proc_macro we can replace the wasm_bindgen proc macro with a
no-op that does nothing when consumed directly from rust.

This is now locked behind a feature (used_from_wasm) as we can't just
use cfg target checks as compiling the wasm crate (not using web-pack)
would use the rust target and break.

Crates using cml-chain and cml-multi-era from a wasm crate must specify
this feature to activate the wasm_bindgen dependancy instead of the
no-op macro.
  • Loading branch information
rooooooooob authored Jul 16, 2024
1 parent cf10721 commit 6a28e88
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 51 deletions.
21 changes: 5 additions & 16 deletions chain/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ keywords = ["cardano"]
[lib]
crate-type = ["cdylib", "rlib"]

[features]
used_from_wasm = ["wasm-bindgen"]

[dependencies]
cml-core = { "path" = "../../core/rust", version = "5.3.1" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.3.1" }
Expand All @@ -32,29 +35,15 @@ fraction = "0.10.0"
base64 = "0.21.5"
num-bigint = "0.4.0"
num-integer = "0.1.45"
#rand_os = "0.1"
thiserror = "1.0.37"
num = "0.4"
unicode-segmentation = "1.10.1"
# These can be removed if we make wasm bindings for ALL functionality here.
# This was not done right now as there is a lot of existing legacy code e.g.
# for Byron that might need to be used from WASM and might not.
# We can remove this dependency when that is decided.
#
# The other use-case here is enums. Without this two enums would need to be defined
# despite wasm_bindgen supporting C-style enums (with non-negative values) 100%
# This could possibly be resolved with macros but maybe not.

# non-wasm
#[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]
#rand_os = "0.1"
#noop_proc_macro = "0.3.0"
noop_proc_macro = { version = "0.3.0", optional = false }

# wasm
#[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "0.2.87" }
#rand_os = { version = "0.1", features = ["wasm-bindgen"] }
#js-sys = "=0.3.59"
wasm-bindgen = { version = "0.2.87", optional = true }


[dev-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion chain/rust/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use schemars::JsonSchema;
use std::convert::{TryFrom, TryInto};
use std::io::{BufRead, Write};

// for enums
#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use cml_crypto::{Ed25519KeyHash, ScriptHash};
Expand Down
4 changes: 3 additions & 1 deletion chain/rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ use std::convert::TryInto;
use std::io::{BufRead, Seek, Write};
use std::ops::DerefMut;

// for enums:
#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

/**
Expand Down
8 changes: 5 additions & 3 deletions chain/rust/src/byron/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
//use noop_proc_macro::wasm_bindgen;
#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use std::io::{BufRead, Write};

Expand Down Expand Up @@ -76,7 +78,7 @@ impl Default for AddrAttributes {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum ByronAddrType {
PublicKey = 0,
Script = 1,
Expand Down
7 changes: 6 additions & 1 deletion chain/rust/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub mod cbor_encodings;
pub mod serialization;
pub mod utils;

#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use crate::address::RewardAccount;
use crate::assets::Coin;
use crate::block::ProtocolVersion;
Expand Down Expand Up @@ -321,7 +326,7 @@ impl UpdateCommittee {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum Vote {
No,
Yes,
Expand Down
3 changes: 3 additions & 0 deletions chain/rust/src/json/metadatums.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use crate::{
Expand Down
3 changes: 3 additions & 0 deletions chain/rust/src/json/plutus_datums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::{
use std::collections::BTreeMap;
use std::str::FromStr;

#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

/// JSON <-> PlutusData conversion schemas.
Expand Down
9 changes: 7 additions & 2 deletions chain/rust/src/plutus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub mod cbor_encodings;
pub mod serialization;
pub mod utils;

#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use self::cbor_encodings::{
LegacyRedeemerEncoding, PlutusV3ScriptEncoding, RedeemerKeyEncoding, RedeemerValEncoding,
};
Expand Down Expand Up @@ -135,7 +140,7 @@ impl ExUnits {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum Language {
PlutusV1,
PlutusV2,
Expand Down Expand Up @@ -477,7 +482,7 @@ impl RedeemerKey {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum RedeemerTag {
Spend,
Mint,
Expand Down
2 changes: 1 addition & 1 deletion chain/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["cardano"]
crate-type = ["cdylib", "rlib"]

[dependencies]
cml-chain = { path = "../rust", version = "5.3.1" }
cml-chain = { path = "../rust", version = "5.3.1", features = ["used_from_wasm"] }
cml-core = { path = "../../core/rust", version = "5.3.1" }
cml-core-wasm = { path = "../../core/wasm", version = "5.3.1" }
# TODO: remove this dependency if possible to reduce confusion? maybe pub export necessary things in crypto-wasm?
Expand Down
2 changes: 1 addition & 1 deletion cip25/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
cbor_event = "2.2.0"
cml-chain = { path = "../../chain/rust", version = "5.3.1" }
cml-chain = { path = "../../chain/rust", version = "5.3.1", features = ["used_from_wasm"] }
cml-chain-wasm = { path = "../../chain/wasm", version = "5.3.1" }
cml-core = { path = "../../core/rust", version = "5.3.1" }
cml-core-wasm = { path = "../../core/wasm", version = "5.3.1" }
Expand Down
20 changes: 0 additions & 20 deletions core/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,6 @@ num-integer = "0.1.45"
thiserror = "1.0.37"
cfg-if = "1"

# These can be removed if we make wasm bindings for ALL functionality here.
# This was not done right now as there is a lot of existing legacy code e.g.
# for Byron that might need to be used from WASM and might not.
# We can remove this dependency when that is decided.
#
# The other use-case here is enums. Without this two enums would need to be defined
# despite wasm_bindgen supporting C-style enums (with non-negative values) 100%
# This could possibly be resolved with macros but maybe not.

# non-wasm
[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]
#rand_os = "0.1"
noop_proc_macro = "0.3.0"

# wasm
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "0.2.87" }
#rand_os = { version = "0.1", features = ["wasm-bindgen"] }
#js-sys = "=0.3.59"


[dev-dependencies]
quickcheck = "0.9.2"
Expand Down
10 changes: 9 additions & 1 deletion multi-era/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ keywords = ["cardano"]
[lib]
crate-type = ["cdylib", "rlib"]

[features]
used_from_wasm = ["wasm-bindgen"]

[dependencies]
cml-core = { path = "../../core/rust", version = "5.3.1" }
cml-crypto = { path = "../../crypto/rust", version = "5.3.1" }
Expand All @@ -23,8 +26,13 @@ derivative = "2.2.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.57"
schemars = "0.8.8"
wasm-bindgen = { version = "0.2.87" }

# only for declaring hash types
bech32 = "0.7.2"
hex = "0.4.0"

# non-wasm
noop_proc_macro = { version = "0.3.0" }

# wasm
wasm-bindgen = { version = "0.2.87", optional = true }
7 changes: 6 additions & 1 deletion multi-era/rust/src/allegra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub mod cbor_encodings;
pub mod serialization;
pub mod utils;

#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use crate::shelley::{
GenesisKeyDelegation, ShelleyHeader, ShelleyPoolParams, ShelleyPoolRegistration,
ShelleyTransactionOutput, ShelleyUpdate,
Expand Down Expand Up @@ -259,7 +264,7 @@ impl MIRAction {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum MIRPot {
Reserve,
Treasury,
Expand Down
7 changes: 6 additions & 1 deletion multi-era/rust/src/alonzo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub mod cbor_encodings;
pub mod serialization;
pub mod utils;

#[cfg(not(feature = "used_from_wasm"))]
use noop_proc_macro::wasm_bindgen;
#[cfg(feature = "used_from_wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

use crate::allegra::AllegraCertificate;
use crate::shelley::{ProtocolVersionStruct, ShelleyHeader};
use cbor_encodings::{
Expand Down Expand Up @@ -210,7 +215,7 @@ impl AlonzoRedeemer {
serde::Serialize,
schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen]
pub enum AlonzoRedeemerTag {
Spend,
Mint,
Expand Down
4 changes: 2 additions & 2 deletions multi-era/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ keywords = ["cardano"]
crate-type = ["cdylib", "rlib"]

[dependencies]
cml-chain = { path = "../../chain/rust", version = "5.3.1" }
cml-chain = { path = "../../chain/rust", version = "5.3.1", features = ["used_from_wasm"] }
cml-chain-wasm = { path = "../../chain/wasm", version = "5.3.1" }
cml-crypto = { path = "../../crypto/rust", version = "5.3.1" }
cml-crypto-wasm = { path = "../../crypto/wasm", version = "5.3.1" }
cml-core = { path = "../../core/rust", version = "5.3.1" }
cml-core-wasm = { path = "../../core/wasm", version = "5.3.1" }
cml-multi-era = { path = "../rust", version = "5.3.1" }
cml-multi-era = { path = "../rust", version = "5.3.1", features = ["used_from_wasm"] }
cbor_event = "2.4.0"
hex = "0.4.0"
linked-hash-map = "0.5.3"
Expand Down

0 comments on commit 6a28e88

Please sign in to comment.