feat(solc): add source map access functions (#1253)

This commit is contained in:
Matthias Seitz 2022-05-12 23:47:45 +02:00 committed by GitHub
parent 1e2b02920b
commit 494af08b85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -22,6 +22,7 @@ use crate::{
SourceFile, SourceFile,
}, },
compile::output::{contracts::VersionedContracts, sources::VersionedSourceFiles}, compile::output::{contracts::VersionedContracts, sources::VersionedSourceFiles},
sourcemap::{SourceMap, SyntaxError},
}; };
pub use configurable::*; pub use configurable::*;
@ -398,6 +399,22 @@ pub trait Artifact {
fn get_abi(&self) -> Option<Cow<Abi>> { fn get_abi(&self) -> Option<Cow<Abi>> {
self.get_contract_bytecode().abi self.get_contract_bytecode().abi
} }
/// Returns the `sourceMap` of the contract
///
/// Returns `None` if no `sourceMap` string was included in the compiler output
/// Returns `Some(Err)` if parsing the sourcemap failed
fn get_source_map(&self) -> Option<std::result::Result<SourceMap, SyntaxError>> {
self.get_bytecode()?.source_map()
}
/// Returns the `sourceMap` as str if it was included in the compiler output
fn get_source_map_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

@ -46,6 +46,13 @@ pub struct CompactBytecode {
} }
impl CompactBytecode { impl CompactBytecode {
/// 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.source_map.as_ref().map(|map| sourcemap::parse(map))
}
/// Tries to link the bytecode object with the `file` and `library` name. /// Tries to link the bytecode object with the `file` and `library` name.
/// Replaces all library placeholders with the given address. /// Replaces all library placeholders with the given address.
/// ///