From a0539f62ef9d43c2869b45bb8eb6c4d4b22f309f Mon Sep 17 00:00:00 2001 From: clearloop Date: Sun, 15 Sep 2024 01:58:49 +0800 Subject: [PATCH] chore(zink): remove language item constructor --- codegen/src/codegen/constructor.rs | 5 ++--- compiler/src/artifact.rs | 14 -------------- compiler/src/compiler.rs | 1 - compiler/src/lib.rs | 2 +- zink/codegen/src/constructor.rs | 22 ---------------------- zink/codegen/src/lib.rs | 8 -------- zink/src/lib.rs | 2 +- zint/src/contract.rs | 13 +++++++++---- 8 files changed, 13 insertions(+), 54 deletions(-) delete mode 100644 zink/codegen/src/constructor.rs diff --git a/codegen/src/codegen/constructor.rs b/codegen/src/codegen/constructor.rs index d17dac627..a2428022c 100644 --- a/codegen/src/codegen/constructor.rs +++ b/codegen/src/codegen/constructor.rs @@ -32,7 +32,6 @@ impl Constructor { /// the runtime bytecode. pub fn finish(&self, runtime_bytecode: Buffer) -> Result { 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(); @@ -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()?; diff --git a/compiler/src/artifact.rs b/compiler/src/artifact.rs index 941746fc4..c6ff44c0b 100644 --- a/compiler/src/artifact.rs +++ b/compiler/src/artifact.rs @@ -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))] @@ -15,16 +13,4 @@ pub struct Artifact { pub config: Config, /// Runtime bytecode of the contract. pub runtime_bytecode: Vec, - /// 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 { - self.constructor - .finish(self.runtime_bytecode.clone().into()) - .map_err(Into::into) - } } diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 3a53f59fa..0a6e24d8e 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -62,7 +62,6 @@ impl Compiler { abi, config, runtime_bytecode: buffer.to_vec(), - ..Default::default() }) } diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index b6c6f16e3..bcd388032 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -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; diff --git a/zink/codegen/src/constructor.rs b/zink/codegen/src/constructor.rs deleted file mode 100644 index 6bee818ad..000000000 --- a/zink/codegen/src/constructor.rs +++ /dev/null @@ -1,22 +0,0 @@ -//! Contract constructor. - -use proc_macro2::TokenStream; -use quote::ToTokens; -use syn::{parse_quote, ItemFn}; - -/// Parse function into contract constructor. -pub fn parse(input: ItemFn) -> TokenStream { - let block = input.block; - let inputs = input.sig.inputs; - let mut item: ItemFn = parse_quote! { - #[no_mangle] - pub extern "C" fn constructor( #inputs ) { - #block - } - }; - - item.attrs.push(parse_quote! { #[no_mangle] }); - item.attrs - .push(parse_quote! { #[allow(improper_ctypes_definitions)] }); - item.into_token_stream() -} diff --git a/zink/codegen/src/lib.rs b/zink/codegen/src/lib.rs index 026cabdf7..668c72108 100644 --- a/zink/codegen/src/lib.rs +++ b/zink/codegen/src/lib.rs @@ -3,7 +3,6 @@ use proc_macro::TokenStream; use syn::{parse_macro_input, DeriveInput, ItemFn, ItemType}; -mod constructor; mod event; mod selector; mod storage; @@ -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() -} diff --git a/zink/src/lib.rs b/zink/src/lib.rs index a1fd75fba..00a751054 100644 --- a/zink/src/lib.rs +++ b/zink/src/lib.rs @@ -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")] diff --git a/zint/src/contract.rs b/zint/src/contract.rs index 222077ade..5e8de2c53 100644 --- a/zint/src/contract.rs +++ b/zint/src/contract.rs @@ -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)] @@ -14,6 +14,8 @@ pub struct Contract { pub artifact: Artifact, /// The source WASM of the contract. pub wasm: Vec, + /// Bytecode constructor + pub constructor: Constructor, } impl From for Contract @@ -34,16 +36,19 @@ where impl Contract { /// Get the bytecode of the contract. pub fn bytecode(&self) -> Result> { - 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) }