Skip to content

Commit

Permalink
Enable storage initializers and dump out a JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadfawaz committed Jun 21, 2022
1 parent 7e0f858 commit caffa42
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ semver = { version = "1.0", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_ignored = "0.1"
sway-core = { version = "0.16.1", path = "../sway-core" }
sway-types = { version = "0.16.1", path = "../sway-types" }
sway-utils = { version = "0.16.1", path = "../sway-utils" }
toml = "0.5"
tracing = "0.1"
Expand Down
8 changes: 8 additions & 0 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sway_core::{
semantic_analysis::namespace, source_map::SourceMap, types::*, BytecodeCompilationResult,
CompileAstResult, CompileError, TreeType,
};
use sway_types::JsonStorageInitializers;
use sway_utils::constants;
use tracing::info;
use url::Url;
Expand All @@ -45,6 +46,7 @@ pub struct PinnedId(u64);
/// The result of successfully compiling a package.
pub struct Compiled {
pub json_abi: JsonABI,
pub json_storage_initializers: JsonStorageInitializers,
pub bytecode: Vec<u8>,
pub tree_type: TreeType,
}
Expand Down Expand Up @@ -1358,6 +1360,7 @@ pub fn compile(
warnings,
} => {
let json_abi = typed_program.kind.generate_json_abi();
let json_storage_initializers = typed_program.kind.generate_json_storage_initializers();
let tree_type = typed_program.kind.tree_type();
match tree_type {
// If we're compiling a library, we don't need to compile any further.
Expand All @@ -1368,6 +1371,7 @@ pub fn compile(
let lib_namespace = typed_program.root.namespace.clone();
let compiled = Compiled {
json_abi,
json_storage_initializers,
bytecode,
tree_type,
};
Expand All @@ -1384,6 +1388,7 @@ pub fn compile(
let bytecode = bytes;
let compiled = Compiled {
json_abi,
json_storage_initializers,
bytecode,
tree_type,
};
Expand Down Expand Up @@ -1416,6 +1421,7 @@ pub fn build(
let mut namespace_map = Default::default();
let mut source_map = SourceMap::new();
let mut json_abi = vec![];
let mut json_storage_initializers = vec![];
let mut bytecode = vec![];
let mut tree_type = None;
for &node in &plan.compilation_order {
Expand All @@ -1430,6 +1436,7 @@ pub fn build(
namespace_map.insert(node, namespace.into());
}
json_abi.extend(compiled.json_abi);
json_storage_initializers.extend(compiled.json_storage_initializers);
bytecode = compiled.bytecode;
tree_type = Some(compiled.tree_type);
source_map.insert_dependency(path.clone());
Expand All @@ -1439,6 +1446,7 @@ pub fn build(
let compiled = Compiled {
bytecode,
json_abi,
json_storage_initializers,
tree_type,
};
Ok((compiled, source_map))
Expand Down
4 changes: 4 additions & 0 deletions forc/src/cli/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub struct Command {
/// output will be "minified", i.e. all on one line without whitespace.
#[clap(long)]
pub minify_json_abi: bool,
/// By default the JSON for storage initializers is formatted for human readability. By using
/// this option JSON output will be "minified", i.e. all on one line without whitespace.
#[clap(long)]
pub minify_json_storage_initializers: bool,
/// Requires that the Forc.lock file is up-to-date. If the lock file is missing, or it
/// needs to be updated, Forc will exit with an error
#[clap(long)]
Expand Down
4 changes: 4 additions & 0 deletions forc/src/cli/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub struct Command {
/// output will be "minified", i.e. all on one line without whitespace.
#[clap(long)]
pub minify_json_abi: bool,
/// By default the JSON for storage initializers is formatted for human readability. By using
/// this option JSON output will be "minified", i.e. all on one line without whitespace.
#[clap(long)]
pub minify_json_storage_initializers: bool,
/// Requires that the Forc.lock file is up-to-date. If the lock file is missing, or it
/// needs to be updated, Forc will exit with an error
#[clap(long)]
Expand Down
5 changes: 5 additions & 0 deletions forc/src/cli/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub struct Command {
#[clap(long)]
pub minify_json_abi: bool,

/// By default the JSON for storage initializers is formatted for human readability. By using
/// this option JSON output will be "minified", i.e. all on one line without whitespace.
#[clap(long)]
pub minify_json_storage_initializers: bool,

/// Set the transaction byte price. Defaults to 0.
#[clap(long)]
pub byte_price: Option<u64>,
Expand Down
16 changes: 16 additions & 0 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
silent_mode,
output_directory,
minify_json_abi,
minify_json_storage_initializers,
locked,
build_profile,
release,
Expand Down Expand Up @@ -163,6 +164,21 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
res?;
}

if !compiled.json_storage_initializers.is_empty() {
let json_storage_initializers_stem =
format!("{}-storage_initializers", manifest.project.name);
let json_storage_initializers_path = output_dir
.join(&json_storage_initializers_stem)
.with_extension("json");
let file = File::create(json_storage_initializers_path)?;
let res = if minify_json_storage_initializers {
serde_json::to_writer(&file, &compiled.json_storage_initializers)
} else {
serde_json::to_writer_pretty(&file, &compiled.json_storage_initializers)
};
res?;
}

info!(" Bytecode size is {} bytes.", compiled.bytecode.len());

match compiled.tree_type {
Expand Down
2 changes: 2 additions & 0 deletions forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
silent_mode,
output_directory,
minify_json_abi,
minify_json_storage_initializers,
locked,
url,
build_profile,
Expand All @@ -50,6 +51,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
silent_mode,
output_directory,
minify_json_abi,
minify_json_storage_initializers,
locked,
build_profile,
release,
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
silent_mode: command.silent_mode,
output_directory: command.output_directory,
minify_json_abi: command.minify_json_abi,
minify_json_storage_initializers: command.minify_json_storage_initializers,
locked: command.locked,
build_profile: None,
release: false,
Expand Down
5 changes: 4 additions & 1 deletion sway-core/src/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,10 @@ fn storage_field_to_storage_field(
let storage_field = StorageField {
name: storage_field.name,
type_info: ty_to_type_info(ec, storage_field.ty)?,
//initializer: expr_to_expression(storage_field.expr),
initializer: match storage_field.initializer {
Some(initializer) => Some(expr_to_expression(ec, initializer.1)?),
None => None,
},
};
Ok(storage_field)
}
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod compile;
mod const_eval;
pub mod const_eval;
mod convert;
mod function;
mod lexical_map;
Expand Down
Loading

0 comments on commit caffa42

Please sign in to comment.