Skip to content

Commit c5afbd9

Browse files
authored
[mbf-wasm] Adds wasm flag to mbf (#11257)
## Description - adds a WASM feature for `move-binary-format` crate - adds a `@mysten/move-binary-format` package (private) - adds a `move-binary-format` package to `sdk/` - adds tests and pnpm setup - adds a readme - builds for node and for web ## Test Plan - Features tests ### Type of Change (Check all that apply) - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes Adds a prototype of the `move-binary-format` adapted for wasm. Currently private - not published to npm.
1 parent 9d3c0e9 commit c5afbd9

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Cargo.lock

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

move-binary-format/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ arbitrary = { version = "1.1.7", optional = true, features = ["derive"] }
2222
enum-compat-util = { path = "../testing-infra/enum-compat-util"}
2323
move-proc-macros = { path = "../testing-infra/move-proc-macros"}
2424

25+
# wasm support (requires js feature of getrandom)
26+
getrandom = { version = "0.2.9", features = ["js"], optional = true }
27+
2528
[dev-dependencies]
2629
proptest = "1.0.0"
2730
proptest-derive = "0.3.0"
@@ -31,3 +34,4 @@ serde_json = "1.0.64"
3134
[features]
3235
default = []
3336
fuzzing = ["proptest", "proptest-derive", "arbitrary", "move-core-types/fuzzing"]
37+
wasm = ["getrandom"]

move-binary-format/src/file_format.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ macro_rules! define_index {
6161
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
6262
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
6363
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
64+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
6465
#[doc=$comment]
6566
pub struct $name(pub TableIndex);
6667

@@ -218,6 +219,7 @@ pub const NO_TYPE_ARGUMENTS: SignatureIndex = SignatureIndex(0);
218219
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
219220
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
220221
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
222+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
221223
pub struct ModuleHandle {
222224
/// Index into the `AddressIdentifierIndex`. Identifies module-holding account's address.
223225
pub address: AddressIdentifierIndex,
@@ -242,6 +244,7 @@ pub struct ModuleHandle {
242244
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
243245
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
244246
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
247+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
245248
pub struct StructHandle {
246249
/// The module that defines the type.
247250
pub module: ModuleHandleIndex,
@@ -284,6 +287,7 @@ pub struct StructTypeParameter {
284287
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
285288
#[cfg_attr(any(test, feature = "fuzzing"), proptest(params = "usize"))]
286289
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
290+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
287291
pub struct FunctionHandle {
288292
/// The module that defines the function.
289293
pub module: ModuleHandleIndex,
@@ -302,6 +306,7 @@ pub struct FunctionHandle {
302306
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
303307
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
304308
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
309+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
305310
pub struct FieldHandle {
306311
pub owner: StructDefinitionIndex,
307312
pub field: MemberCount,
@@ -315,6 +320,7 @@ pub struct FieldHandle {
315320
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
316321
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
317322
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
323+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
318324
pub enum StructFieldInformation {
319325
Native,
320326
Declared(Vec<FieldDefinition>),
@@ -333,6 +339,7 @@ pub enum StructFieldInformation {
333339
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
334340
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
335341
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
342+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
336343
pub struct StructDefInstantiation {
337344
pub def: StructDefinitionIndex,
338345
pub type_parameters: SignatureIndex,
@@ -343,6 +350,7 @@ pub struct StructDefInstantiation {
343350
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
344351
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
345352
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
353+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
346354
pub struct FunctionInstantiation {
347355
pub handle: FunctionHandleIndex,
348356
pub type_parameters: SignatureIndex,
@@ -358,6 +366,7 @@ pub struct FunctionInstantiation {
358366
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
359367
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
360368
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
369+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
361370
pub struct FieldInstantiation {
362371
pub handle: FieldHandleIndex,
363372
pub type_parameters: SignatureIndex,
@@ -369,6 +378,7 @@ pub struct FieldInstantiation {
369378
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
370379
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
371380
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
381+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
372382
pub struct StructDefinition {
373383
/// The `StructHandle` for this `StructDefinition`. This has the name and the abilities
374384
/// for the type.
@@ -402,6 +412,7 @@ impl StructDefinition {
402412
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
403413
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
404414
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
415+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
405416
pub struct FieldDefinition {
406417
/// The name of the field.
407418
pub name: IdentifierIndex,
@@ -453,6 +464,7 @@ impl std::convert::TryFrom<u8> for Visibility {
453464
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
454465
#[cfg_attr(any(test, feature = "fuzzing"), proptest(params = "usize"))]
455466
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
467+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
456468
pub struct FunctionDefinition {
457469
/// The prototype of the function (module, name, signature).
458470
pub function: FunctionHandleIndex,
@@ -506,6 +518,7 @@ impl FunctionDefinition {
506518
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
507519
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
508520
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
521+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
509522
pub struct TypeSignature(pub SignatureToken);
510523

511524
// TODO: remove at some point or move it in the front end (language/move-ir-compiler)
@@ -515,6 +528,7 @@ pub struct TypeSignature(pub SignatureToken);
515528
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
516529
#[cfg_attr(any(test, feature = "fuzzing"), proptest(params = "usize"))]
517530
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
531+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
518532
pub struct FunctionSignature {
519533
/// The list of return types.
520534
#[cfg_attr(
@@ -540,6 +554,7 @@ pub struct FunctionSignature {
540554
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
541555
#[cfg_attr(any(test, feature = "fuzzing"), proptest(params = "usize"))]
542556
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
557+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
543558
pub struct Signature(
544559
#[cfg_attr(
545560
any(test, feature = "fuzzing"),
@@ -571,6 +586,7 @@ pub type TypeParameterIndex = u16;
571586
#[derive(Debug, Clone, Eq, Copy, Hash, Ord, PartialEq, PartialOrd)]
572587
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
573588
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
589+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
574590
pub enum Ability {
575591
/// Allows values of types with this ability to be copied, via CopyLoc or ReadRef
576592
Copy = 0x1,
@@ -834,6 +850,7 @@ impl Arbitrary for AbilitySet {
834850
/// enforced by the verifier.
835851
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
836852
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
853+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
837854
pub enum SignatureToken {
838855
/// Boolean, `true` or `false`.
839856
Bool,
@@ -1112,6 +1129,7 @@ impl SignatureToken {
11121129
/// loader/evauluator
11131130
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
11141131
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
1132+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
11151133
pub struct Constant {
11161134
pub type_: SignatureToken,
11171135
pub data: Vec<u8>,
@@ -1122,6 +1140,7 @@ pub struct Constant {
11221140
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
11231141
#[cfg_attr(any(test, feature = "fuzzing"), proptest(params = "usize"))]
11241142
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
1143+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
11251144
pub struct CodeUnit {
11261145
/// List of locals type. All locals are typed.
11271146
pub locals: SignatureIndex,
@@ -1142,6 +1161,7 @@ pub struct CodeUnit {
11421161
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
11431162
#[cfg_attr(any(test, feature = "fuzzing"), proptest(no_params))]
11441163
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
1164+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
11451165
pub enum Bytecode {
11461166
/// Pop and discard the value at the top of the stack.
11471167
/// The value on the stack must be an copyable type.
@@ -1785,6 +1805,7 @@ impl Bytecode {
17851805
/// A CompiledScript defines the constant pools (string, address, signatures, etc.), the handle
17861806
/// tables (external code references) and it has a `main` definition.
17871807
#[derive(Clone, Default, Eq, PartialEq, Debug)]
1808+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
17881809
pub struct CompiledScript {
17891810
/// Version number found during deserialization
17901811
pub version: u32,
@@ -1828,6 +1849,7 @@ impl CompiledScript {
18281849
/// A module is published as a single entry and it is retrieved as a single blob.
18291850
#[derive(Clone, Debug, Default, Eq, PartialEq)]
18301851
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
1852+
#[cfg_attr(feature = "wasm", derive(Serialize, Deserialize))]
18311853
pub struct CompiledModule {
18321854
/// Version number found during deserialization
18331855
pub version: u32,

move-core/types/src/metadata.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) The Move Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use serde::{Deserialize, Serialize};
5+
46
/// Representation of metadata,
5-
#[derive(Clone, PartialEq, Eq, Debug)]
7+
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
68
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
79
pub struct Metadata {
810
/// The key identifying the type of metadata.

0 commit comments

Comments
 (0)