This commit is contained in:
Sebastian Miasojed
2024-11-07 15:15:52 +01:00
parent 4f6debcbe3
commit d260472330
4 changed files with 39 additions and 24 deletions
+17 -7
View File
@@ -17,15 +17,18 @@ pub extern "C" fn solidity_license() -> *const c_char {
#[no_mangle] #[no_mangle]
pub extern "C" fn solidity_version() -> *const c_char { pub extern "C" fn solidity_version() -> *const c_char {
let mut solc = SoljsonCompiler { version: None }; let mut solc = SoljsonCompiler { version: None };
let version = solc.version().map(|v| v.long).unwrap_or("unknown".to_owned()); let version = solc
// Store the string in a static variable .version()
unsafe { .map(|v| v.long)
.unwrap_or("unknown".to_owned());
// Store the string in a static variable
unsafe {
if VERSION.is_none() { if VERSION.is_none() {
VERSION = Some(Box::new(CString::new(version).unwrap())); VERSION = Some(Box::new(CString::new(version).unwrap()));
} }
VERSION.as_ref().map_or_else(std::ptr::null, |s| s.as_ptr()) VERSION.as_ref().map_or_else(std::ptr::null, |s| s.as_ptr())
} }
} }
#[no_mangle] #[no_mangle]
@@ -46,7 +49,15 @@ pub extern "C" fn solidity_free(data: *mut c_char) {
#[no_mangle] #[no_mangle]
pub extern "C" fn solidity_compile( pub extern "C" fn solidity_compile(
input: *const c_char, 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, _readContext: *mut libc::c_void,
) -> *mut c_char { ) -> *mut c_char {
let input = unsafe { CStr::from_ptr(input).to_str().unwrap_or("") }; let input = unsafe { CStr::from_ptr(input).to_str().unwrap_or("") };
@@ -58,5 +69,4 @@ pub extern "C" fn solidity_compile(
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn solidity_reset() { pub extern "C" fn solidity_reset() {}
}
+14 -12
View File
@@ -22,7 +22,7 @@ impl Process for NativeProcess {
let mut stdin = std::io::stdin(); let mut stdin = std::io::stdin();
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();
let mut stderr = std::io::stderr(); let mut stderr = std::io::stderr();
let mut buffer = Vec::with_capacity(16384); let mut buffer = Vec::with_capacity(16384);
match input_file { match input_file {
Some(ins) => { Some(ins) => {
@@ -39,7 +39,7 @@ impl Process for NativeProcess {
} }
} }
} }
let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?; let input: Input = revive_common::deserialize_from_slice(buffer.as_slice())?;
let result = input.contract.compile( let result = input.contract.compile(
input.project, input.project,
@@ -47,7 +47,7 @@ impl Process for NativeProcess {
input.include_metadata_hash, input.include_metadata_hash,
input.debug_config, input.debug_config,
); );
match result { match result {
Ok(build) => { Ok(build) => {
let output = Output::new(build); let output = Output::new(build);
@@ -66,16 +66,16 @@ impl Process for NativeProcess {
} }
} }
} }
/// Runs this process recursively to compile a single contract. /// Runs this process recursively to compile a single contract.
fn call(input: Input) -> anyhow::Result<Output> { fn call(input: Input) -> anyhow::Result<Output> {
let input_json = serde_json::to_vec(&input).expect("Always valid"); let input_json = serde_json::to_vec(&input).expect("Always valid");
let executable = match EXECUTABLE.get() { let executable = match EXECUTABLE.get() {
Some(executable) => executable.to_owned(), Some(executable) => executable.to_owned(),
None => std::env::current_exe()?, None => std::env::current_exe()?,
}; };
let mut command = Command::new(executable.as_path()); let mut command = Command::new(executable.as_path());
command.stdin(std::process::Stdio::piped()); command.stdin(std::process::Stdio::piped());
command.stdout(std::process::Stdio::piped()); command.stdout(std::process::Stdio::piped());
@@ -84,7 +84,7 @@ impl Process for NativeProcess {
let process = command.spawn().map_err(|error| { let process = command.spawn().map_err(|error| {
anyhow::anyhow!("{:?} subprocess spawning error: {:?}", executable, error) anyhow::anyhow!("{:?} subprocess spawning error: {:?}", executable, error)
})?; })?;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
if let Some(dbg_config) = &input.debug_config { if let Some(dbg_config) = &input.debug_config {
dbg_config dbg_config
@@ -97,13 +97,15 @@ impl Process for NativeProcess {
) )
})?; })?;
} }
process process
.stdin .stdin
.as_ref() .as_ref()
.ok_or_else(|| anyhow::anyhow!("{:?} stdin getting error", executable))? .ok_or_else(|| anyhow::anyhow!("{:?} stdin getting error", executable))?
.write_all(input_json.as_slice()) .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| { let output = process.wait_with_output().map_err(|error| {
anyhow::anyhow!("{:?} subprocess output error: {:?}", executable, error) anyhow::anyhow!("{:?} subprocess output error: {:?}", executable, error)
})?; })?;
@@ -113,9 +115,9 @@ impl Process for NativeProcess {
String::from_utf8_lossy(output.stderr.as_slice()).to_string(), String::from_utf8_lossy(output.stderr.as_slice()).to_string(),
); );
} }
let output: Output = let output: Output = revive_common::deserialize_from_slice(output.stdout.as_slice())
revive_common::deserialize_from_slice(output.stdout.as_slice()).map_err(|error| { .map_err(|error| {
anyhow::anyhow!( anyhow::anyhow!(
"{:?} subprocess output parsing error: {}", "{:?} subprocess output parsing error: {}",
executable, executable,
+2 -2
View File
@@ -42,8 +42,8 @@ impl SolcCompiler {
} }
impl Compiler for SolcCompiler { impl Compiler for SolcCompiler {
/// Compiles the Solidity `--standard-json` input into Yul IR. /// Compiles the Solidity `--standard-json` input into Yul IR.
fn standard_json( fn standard_json(
&mut self, &mut self,
mut input: StandardJsonInput, mut input: StandardJsonInput,
pipeline: Pipeline, pipeline: Pipeline,
+6 -3
View File
@@ -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::optimizer::Optimizer as SolcStandardJsonInputSettingsOptimizer;
use crate::compiler::standard_json::input::settings::selection::Selection as SolcStandardJsonInputSettingsSelection; 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::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::contract::evm::bytecode::DeployedBytecode;
use crate::compiler::standard_json::output::Output as SolcStandardJsonOutput; use crate::compiler::standard_json::output::Output as SolcStandardJsonOutput;
use crate::compiler::Compiler; use crate::compiler::Compiler;
use crate::project::Project; use crate::project::Project;
use crate::compiler::standard_json::output::contract::evm::bytecode::Bytecode;
use crate::warning::Warning; use crate::warning::Warning;
static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default); static PVM_BLOB_CACHE: Lazy<Mutex<HashMap<CachedBlob, Vec<u8>>>> = Lazy::new(Default::default);
@@ -214,8 +214,11 @@ pub fn build_yul(source_code: &str) -> anyhow::Result<()> {
revive_llvm_context::initialize_target(revive_llvm_context::Target::PVM); revive_llvm_context::initialize_target(revive_llvm_context::Target::PVM);
let optimizer_settings = revive_llvm_context::OptimizerSettings::none(); let optimizer_settings = revive_llvm_context::OptimizerSettings::none();
let project = let project = Project::try_from_yul_string::<SolcCompiler>(
Project::try_from_yul_string::<SolcCompiler>(PathBuf::from("test.yul").as_path(), source_code, None)?; PathBuf::from("test.yul").as_path(),
source_code,
None,
)?;
let _build = project.compile(optimizer_settings, false, None)?; let _build = project.compile(optimizer_settings, false, None)?;
Ok(()) Ok(())