feat: source map getters for deployed bytecode (#1348)

* feat: source map getters for deployed bytecode

* fix: borrow checker
This commit is contained in:
Bjerg 2022-06-06 15:42:45 +02:00 committed by GitHub
parent a532eb4d29
commit f56146025a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -401,7 +401,7 @@ pub trait Artifact {
self.get_contract_bytecode().abi self.get_contract_bytecode().abi
} }
/// Returns the `sourceMap` of the contract /// Returns the `sourceMap` of the creation bytecode
/// ///
/// Returns `None` if no `sourceMap` string was included in the compiler output /// Returns `None` if no `sourceMap` string was included in the compiler output
/// Returns `Some(Err)` if parsing the sourcemap failed /// Returns `Some(Err)` if parsing the sourcemap failed
@ -409,13 +409,29 @@ pub trait Artifact {
self.get_bytecode()?.source_map() self.get_bytecode()?.source_map()
} }
/// Returns the `sourceMap` as str if it was included in the compiler output /// Returns the creation bytecode `sourceMap` as str if it was included in the compiler output
fn get_source_map_str(&self) -> Option<Cow<str>> { fn get_source_map_str(&self) -> Option<Cow<str>> {
match self.get_bytecode()? { match self.get_bytecode()? {
Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed), Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed),
Cow::Owned(code) => code.source_map.map(Cow::Owned), Cow::Owned(code) => code.source_map.map(Cow::Owned),
} }
} }
/// Returns the `sourceMap` of the runtime bytecode
///
/// Returns `None` if no `sourceMap` string was included in the compiler output
/// Returns `Some(Err)` if parsing the sourcemap failed
fn get_source_map_deployed(&self) -> Option<std::result::Result<SourceMap, SyntaxError>> {
self.get_deployed_bytecode()?.source_map()
}
/// Returns the runtime bytecode `sourceMap` as str if it was included in the compiler output
fn get_source_map_deployed_str(&self) -> Option<Cow<str>> {
match self.get_bytecode()? {
Cow::Borrowed(code) => code.source_map.as_deref().map(Cow::Borrowed),
Cow::Owned(code) => code.source_map.map(Cow::Owned),
}
}
} }
impl<T> Artifact for T impl<T> Artifact for T

View File

@ -411,6 +411,13 @@ impl CompactDeployedBytecode {
pub fn empty() -> Self { pub fn empty() -> Self {
Self { bytecode: Some(CompactBytecode::empty()), immutable_references: Default::default() } Self { bytecode: Some(CompactBytecode::empty()), immutable_references: Default::default() }
} }
/// Returns the parsed source map
///
/// See also <https://docs.soliditylang.org/en/v0.8.10/internals/source_mappings.html>
pub fn source_map(&self) -> Option<Result<SourceMap, SyntaxError>> {
self.bytecode.as_ref().and_then(|bytecode| bytecode.source_map())
}
} }
impl From<DeployedBytecode> for CompactDeployedBytecode { impl From<DeployedBytecode> for CompactDeployedBytecode {