Skip to content

Commit

Permalink
Fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed committed Nov 7, 2024
1 parent 4f6debc commit d260472
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
24 changes: 17 additions & 7 deletions crates/solidity/src/libsolc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ pub extern "C" fn solidity_license() -> *const c_char {
#[no_mangle]
pub extern "C" fn solidity_version() -> *const c_char {
let mut solc = SoljsonCompiler { version: None };
let version = solc.version().map(|v| v.long).unwrap_or("unknown".to_owned());
// Store the string in a static variable
unsafe {
let version = solc
.version()
.map(|v| v.long)
.unwrap_or("unknown".to_owned());
// Store the string in a static variable
unsafe {
if VERSION.is_none() {
VERSION = Some(Box::new(CString::new(version).unwrap()));
}

VERSION.as_ref().map_or_else(std::ptr::null, |s| s.as_ptr())
}
}
}

#[no_mangle]
Expand All @@ -46,7 +49,15 @@ pub extern "C" fn solidity_free(data: *mut c_char) {
#[no_mangle]
pub extern "C" fn solidity_compile(
input: *const c_char,
_readCallback: Option<extern "C" fn(*mut libc::c_void, *const c_char, *const c_char, *mut *mut c_char, *mut *mut c_char)>,
_readCallback: Option<
extern "C" fn(
*mut libc::c_void,
*const c_char,
*const c_char,
*mut *mut c_char,
*mut *mut c_char,
),
>,
_readContext: *mut libc::c_void,
) -> *mut c_char {
let input = unsafe { CStr::from_ptr(input).to_str().unwrap_or("") };
Expand All @@ -58,5 +69,4 @@ pub extern "C" fn solidity_compile(
}

#[no_mangle]
pub extern "C" fn solidity_reset() {
}
pub extern "C" fn solidity_reset() {}
26 changes: 14 additions & 12 deletions crates/solidity/src/process/native_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Process for NativeProcess {
let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let mut stderr = std::io::stderr();

let mut buffer = Vec::with_capacity(16384);
match input_file {
Some(ins) => {
Expand All @@ -39,15 +39,15 @@ impl Process for NativeProcess {
}
}
}

let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
let result = input.contract.compile(
input.project,
input.optimizer_settings,
input.include_metadata_hash,
input.debug_config,
);

match result {
Ok(build) => {
let output = Output::new(build);
Expand All @@ -66,16 +66,16 @@ impl Process for NativeProcess {
}
}
}

/// Runs this process recursively to compile a single contract.
fn call(input: Input) -> anyhow::Result<Output> {
let input_json = serde_json::to_vec(&input).expect("Always valid");

let executable = match EXECUTABLE.get() {
Some(executable) => executable.to_owned(),
None => std::env::current_exe()?,
};

let mut command = Command::new(executable.as_path());
command.stdin(std::process::Stdio::piped());
command.stdout(std::process::Stdio::piped());
Expand All @@ -84,7 +84,7 @@ impl Process for NativeProcess {
let process = command.spawn().map_err(|error| {
anyhow::anyhow!("{:?} subprocess spawning error: {:?}", executable, error)
})?;

#[cfg(debug_assertions)]
if let Some(dbg_config) = &input.debug_config {
dbg_config
Expand All @@ -97,13 +97,15 @@ impl Process for NativeProcess {
)
})?;
}

process
.stdin
.as_ref()
.ok_or_else(|| anyhow::anyhow!("{:?} stdin getting error", executable))?
.write_all(input_json.as_slice())
.map_err(|error| anyhow::anyhow!("{:?} stdin writing error: {:?}", executable, error))?;
.map_err(|error| {
anyhow::anyhow!("{:?} stdin writing error: {:?}", executable, error)
})?;
let output = process.wait_with_output().map_err(|error| {
anyhow::anyhow!("{:?} subprocess output error: {:?}", executable, error)
})?;
Expand All @@ -113,9 +115,9 @@ impl Process for NativeProcess {
String::from_utf8_lossy(output.stderr.as_slice()).to_string(),
);
}
let output: Output =
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| {

let output: Output = revive_common::deserialize_from_slice(output.stdout.as_slice())
.map_err(|error| {
anyhow::anyhow!(
"{:?} subprocess output parsing error: {}",
executable,
Expand Down
4 changes: 2 additions & 2 deletions crates/solidity/src/solc/solc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl SolcCompiler {
}

impl Compiler for SolcCompiler {
/// Compiles the Solidity `--standard-json` input into Yul IR.
fn standard_json(
/// Compiles the Solidity `--standard-json` input into Yul IR.
fn standard_json(
&mut self,
mut input: StandardJsonInput,
pipeline: Pipeline,
Expand Down
9 changes: 6 additions & 3 deletions crates/solidity/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::compiler::solc::SolcCompiler;
use crate::compiler::standard_json::input::settings::optimizer::Optimizer as SolcStandardJsonInputSettingsOptimizer;
use crate::compiler::standard_json::input::settings::selection::Selection as SolcStandardJsonInputSettingsSelection;
use crate::compiler::standard_json::input::Input as SolcStandardJsonInput;
use crate::compiler::standard_json::output::contract::evm::bytecode::Bytecode;
use crate::compiler::standard_json::output::contract::evm::bytecode::DeployedBytecode;
use crate::compiler::standard_json::output::Output as SolcStandardJsonOutput;
use crate::compiler::Compiler;
use crate::project::Project;
use crate::compiler::standard_json::output::contract::evm::bytecode::Bytecode;
use crate::warning::Warning;

static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
Expand Down Expand Up @@ -214,8 +214,11 @@ pub fn build_yul(source_code: &str) -> anyhow::Result<()> {
revive_llvm_context::initialize_target(revive_llvm_context::Target::PVM);
let optimizer_settings = revive_llvm_context::OptimizerSettings::none();

let project =
Project::try_from_yul_string::<SolcCompiler>(PathBuf::from("test.yul").as_path(), source_code, None)?;
let project = Project::try_from_yul_string::<SolcCompiler>(
PathBuf::from("test.yul").as_path(),
source_code,
None,
)?;
let _build = project.compile(optimizer_settings, false, None)?;

Ok(())
Expand Down

0 comments on commit d260472

Please sign in to comment.