22//! Bytecode object.
33//!
44
5+ use std:: collections:: BTreeMap ;
6+ use std:: collections:: BTreeSet ;
7+
58///
69/// Bytecode object.
710///
@@ -21,11 +24,13 @@ pub struct Object {
2124 pub code_segment : era_compiler_common:: CodeSegment ,
2225 /// Dependencies.
2326 pub dependencies : era_yul:: Dependencies ,
27+ /// The unlinked unlinked libraries.
28+ pub unlinked_libraries : BTreeSet < String > ,
2429 /// Whether the object is already assembled.
2530 pub is_assembled : bool ,
26- /// The binary object format.
27- pub object_format : era_compiler_common:: ObjectFormat ,
28- /// Warnings produced during compilation .
31+ /// Binary object format.
32+ pub format : era_compiler_common:: ObjectFormat ,
33+ /// Compilation warnings .
2934 pub warnings : Vec < era_compiler_llvm_context:: EVMWarning > ,
3035}
3136
@@ -40,6 +45,8 @@ impl Object {
4045 codegen : Option < era_solc:: StandardJsonInputCodegen > ,
4146 code_segment : era_compiler_common:: CodeSegment ,
4247 dependencies : era_yul:: Dependencies ,
48+ unlinked_libraries : BTreeSet < String > ,
49+ format : era_compiler_common:: ObjectFormat ,
4350 warnings : Vec < era_compiler_llvm_context:: EVMWarning > ,
4451 ) -> Self {
4552 Self {
@@ -49,33 +56,63 @@ impl Object {
4956 codegen,
5057 code_segment,
5158 dependencies,
59+ unlinked_libraries,
5260 is_assembled : false ,
53- object_format : era_compiler_common :: ObjectFormat :: ELF ,
61+ format ,
5462 warnings,
5563 }
5664 }
5765
5866 ///
59- /// Whether the object requires assebmling with its dependencies .
67+ /// Links the object with its linker symbols .
6068 ///
61- pub fn requires_assembling ( & self ) -> bool {
62- !self . is_assembled && !self . dependencies . inner . is_empty ( )
69+ pub fn link (
70+ & mut self ,
71+ linker_symbols : & BTreeMap < String , [ u8 ; era_compiler_common:: BYTE_LENGTH_ETH_ADDRESS ] > ,
72+ ) -> anyhow:: Result < ( ) > {
73+ let memory_buffer = inkwell:: memory_buffer:: MemoryBuffer :: create_from_memory_range (
74+ self . bytecode . as_slice ( ) ,
75+ self . identifier . as_str ( ) ,
76+ false ,
77+ ) ;
78+
79+ let ( linked_object, object_format) =
80+ era_compiler_llvm_context:: evm_link ( memory_buffer, linker_symbols) ?;
81+ self . format = object_format;
82+
83+ self . bytecode = linked_object. as_slice ( ) . to_owned ( ) ;
84+ // if let era_compiler_common::CodeSegment::Deploy = self.code_segment {
85+ // let metadata = match contract.metadata_hash {
86+ // Some(era_compiler_common::Hash::IPFS(ref hash)) => {
87+ // let cbor = era_compiler_common::CBOR::new(
88+ // Some((
89+ // era_compiler_common::EVMMetadataHashType::IPFS,
90+ // hash.as_bytes(),
91+ // )),
92+ // crate::r#const::SOLC_PRODUCTION_NAME.to_owned(),
93+ // cbor_data.clone(),
94+ // );
95+ // cbor.to_vec()
96+ // }
97+ // Some(era_compiler_common::Hash::Keccak256(ref hash)) => hash.to_vec(),
98+ // None => {
99+ // let cbor = era_compiler_common::CBOR::<'_, String>::new(
100+ // None,
101+ // crate::r#const::SOLC_PRODUCTION_NAME.to_owned(),
102+ // cbor_data.clone(),
103+ // );
104+ // cbor.to_vec()
105+ // }
106+ // };
107+ // self.bytecode.extend(metadata);
108+ // }
109+ Ok ( ( ) )
63110 }
64111
65112 ///
66- /// Checks whether the object name matches a dot-separated dependency name.
67- ///
68- /// This function is only useful for Yul codegen where object names like `A_25.A_25_deployed` are found.
69- /// For EVM assembly codegen, it performs a simple comparison.
113+ /// Whether the object requires assebmling with its dependencies.
70114 ///
71- pub fn matches_dependency ( & self , dependency : & str ) -> bool {
72- let dependency = match self . codegen {
73- Some ( era_solc:: StandardJsonInputCodegen :: EVMLA ) | None => dependency,
74- Some ( era_solc:: StandardJsonInputCodegen :: Yul ) => {
75- dependency. split ( '.' ) . last ( ) . expect ( "Always exists" )
76- }
77- } ;
78-
79- self . identifier . as_str ( ) == dependency
115+ pub fn requires_assembling ( & self ) -> bool {
116+ !self . is_assembled && !self . dependencies . inner . is_empty ( )
80117 }
81118}
0 commit comments