Skip to content

Commit

Permalink
chore(zink): remove language item constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Sep 14, 2024
1 parent ad77ee8 commit a0539f6
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 54 deletions.
5 changes: 2 additions & 3 deletions codegen/src/codegen/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ impl Constructor {
/// the runtime bytecode.
pub fn finish(&self, runtime_bytecode: Buffer) -> Result<Buffer> {
let init_code = self.masm.buffer();
tracing::trace!("init code: {}", hex::encode(init_code));
let init_code_len = init_code.len();
let runtime_bytecode_len = runtime_bytecode.len();
let runtime_bytecode_size = runtime_bytecode_len.to_ls_bytes();
Expand All @@ -41,13 +40,13 @@ impl Constructor {

let mut masm = self.masm.clone();

// 2. copy runtime bytecode to memory
// 1. copy runtime bytecode to memory
masm.push(&runtime_bytecode_size)?; // code size
masm.push(&runtime_bytecode_offset.to_ls_bytes())?; // code offset
masm._push0()?; // dest offset in memory
masm._codecopy()?;

// 3. return runtime bytecode
// 2. return runtime bytecode
masm.push(&runtime_bytecode_size)?; // code size
masm._push0()?; // memory offset
masm.asm._return()?;
Expand Down
14 changes: 0 additions & 14 deletions compiler/src/artifact.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Zink compiler artifact
use crate::Config;
use anyhow::Result;
use zabi::Abi;
use zingen::{Buffer, Constructor};

/// Zink compiler artifact
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -15,16 +13,4 @@ pub struct Artifact {
pub config: Config,
/// Runtime bytecode of the contract.
pub runtime_bytecode: Vec<u8>,
/// Creation bytecode constructor
#[cfg_attr(feature = "serde", serde(skip))]
pub constructor: Constructor,
}

impl Artifact {
/// Generate the creation bytecode just in time
pub fn bytecode(&self) -> Result<Buffer> {
self.constructor
.finish(self.runtime_bytecode.clone().into())
.map_err(Into::into)
}
}
1 change: 0 additions & 1 deletion compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl Compiler {
abi,
config,
runtime_bytecode: buffer.to_vec(),
..Default::default()
})
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use crate::{
config::Config,
result::{Error, Result},
};
pub use zingen::InitStorage;
pub use zingen::{Constructor, InitStorage};

mod artifact;
pub mod cli;
Expand Down
22 changes: 0 additions & 22 deletions zink/codegen/src/constructor.rs

This file was deleted.

8 changes: 0 additions & 8 deletions zink/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput, ItemFn, ItemType};

mod constructor;
mod event;
mod selector;
mod storage;
Expand Down Expand Up @@ -78,10 +77,3 @@ pub fn external(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemFn);
selector::external(input)
}

/// Mark the function as constructor
#[proc_macro_attribute]
pub fn constructor(_args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemFn);
constructor::parse(input).into()
}
2 changes: 1 addition & 1 deletion zink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod ffi;
mod storage;

pub use self::{asm::Asm, event::Event, storage::Storage};
pub use zink_codegen::{constructor, external, storage, Event};
pub use zink_codegen::{external, storage, Event};

// Panic hook implementation
#[cfg(target_arch = "wasm32")]
Expand Down
13 changes: 9 additions & 4 deletions zint/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{lookup, Bytes32, Info, EVM};
use anyhow::{anyhow, Result};
use std::fs;
use zinkc::{Artifact, Compiler, Config, InitStorage};
use zinkc::{Artifact, Compiler, Config, Constructor, InitStorage};

/// Contract instance for testing.
#[derive(Default)]
Expand All @@ -14,6 +14,8 @@ pub struct Contract {
pub artifact: Artifact,
/// The source WASM of the contract.
pub wasm: Vec<u8>,
/// Bytecode constructor
pub constructor: Constructor,
}

impl<T> From<T> for Contract
Expand All @@ -34,16 +36,19 @@ where
impl Contract {
/// Get the bytecode of the contract.
pub fn bytecode(&self) -> Result<Vec<u8>> {
let bytecode = self.artifact.bytecode().map(|v| v.to_vec())?;
tracing::debug!("bytecode: {}", hex::encode(&bytecode));
let bytecode = self
.constructor
.finish(self.artifact.runtime_bytecode.clone().into())
.map(|v| v.to_vec())?;

tracing::debug!("bytecode: {}", hex::encode(&bytecode));
Ok(bytecode)
}

/// Preset the storage of the contract, similar with the concept `constructor`
/// in solidity, but just in time.
pub fn construct(&mut self, storage: InitStorage) -> Result<&mut Self> {
self.artifact.constructor.storage(storage)?;
self.constructor.storage(storage)?;
Ok(self)
}

Expand Down

0 comments on commit a0539f6

Please sign in to comment.