1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

make zokrates_core platform independent by removing the canonicalize call

This commit is contained in:
schaeff 2020-02-13 16:31:43 +01:00
parent 43075af3d6
commit 602d5c27ea
8 changed files with 57 additions and 66 deletions

View file

@ -1,2 +0,0 @@
def foo() -> (field):
return 1

View file

@ -1,4 +0,0 @@
from "./dep/foo.zok" import foo as bar
def foo() -> (field):
return 2 + bar()

View file

@ -1,4 +0,0 @@
from "./dep/foo.zok" import foo
def main() -> (field):
return foo()

View file

@ -13,7 +13,7 @@ use std::io::{stdin, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::string::String; use std::string::String;
use zokrates_abi::Encode; use zokrates_abi::Encode;
use zokrates_core::compile::{compile, CompilationArtifacts}; use zokrates_core::compile::{compile, CompilationArtifacts, CompileError};
use zokrates_core::ir; use zokrates_core::ir;
use zokrates_core::proof_system::*; use zokrates_core::proof_system::*;
use zokrates_core::typed_absy::abi::Abi; use zokrates_core::typed_absy::abi::Abi;
@ -287,9 +287,29 @@ fn cli() -> Result<(), String> {
.read_to_string(&mut source) .read_to_string(&mut source)
.map_err(|why| format!("couldn't open input file {}: {}", path.display(), why))?; .map_err(|why| format!("couldn't open input file {}: {}", path.display(), why))?;
let fmt_error = |e: &CompileError| {
format!(
"{}:{}",
e.file()
.canonicalize()
.unwrap()
.strip_prefix(std::env::current_dir().unwrap())
.unwrap()
.display(),
e.value()
)
};
let artifacts: CompilationArtifacts<FieldPrime> = let artifacts: CompilationArtifacts<FieldPrime> =
compile(source, path, Some(&fs_resolve)) compile(source, path, Some(&fs_resolve)).map_err(|e| {
.map_err(|e| format!("Compilation failed:\n\n {}", e))?; format!(
"Compilation failed:\n\n{}",
e.0.iter()
.map(|e| fmt_error(e))
.collect::<Vec<_>>()
.join("\n\n")
)
})?;
let program_flattened = artifacts.prog(); let program_flattened = artifacts.prog();

View file

@ -44,20 +44,6 @@ impl From<CompileError> for CompileErrors {
} }
} }
impl fmt::Display for CompileErrors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
self.0
.iter()
.map(|e| format!("{}", e))
.collect::<Vec<_>>()
.join("\n\n")
)
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum CompileErrorInner { pub enum CompileErrorInner {
ParserError(pest::Error), ParserError(pest::Error),
@ -81,6 +67,16 @@ pub struct CompileError {
value: CompileErrorInner, value: CompileErrorInner,
} }
impl CompileError {
pub fn file(&self) -> &PathBuf {
&self.file
}
pub fn value(&self) -> &CompileErrorInner {
&self.value
}
}
impl CompileErrors { impl CompileErrors {
pub fn with_context(self, file: PathBuf) -> Self { pub fn with_context(self, file: PathBuf) -> Self {
CompileErrors( CompileErrors(
@ -95,22 +91,6 @@ impl CompileErrors {
} }
} }
impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}:{}",
self.file
.canonicalize()
.unwrap()
.strip_prefix(std::env::current_dir().unwrap())
.unwrap()
.display(),
self.value
)
}
}
impl From<pest::Error> for CompileErrorInner { impl From<pest::Error> for CompileErrorInner {
fn from(error: pest::Error) -> Self { fn from(error: pest::Error) -> Self {
CompileErrorInner::ParserError(error) CompileErrorInner::ParserError(error)
@ -140,13 +120,12 @@ impl From<semantics::Error> for CompileError {
impl fmt::Display for CompileErrorInner { impl fmt::Display for CompileErrorInner {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = match *self { match *self {
CompileErrorInner::ParserError(ref e) => format!("{}", e), CompileErrorInner::ParserError(ref e) => write!(f, "{}", e),
CompileErrorInner::SemanticError(ref e) => format!("{}", e), CompileErrorInner::SemanticError(ref e) => write!(f, "{}", e),
CompileErrorInner::ReadError(ref e) => format!("{}", e), CompileErrorInner::ReadError(ref e) => write!(f, "{}", e),
CompileErrorInner::ImportError(ref e) => format!("{}", e), CompileErrorInner::ImportError(ref e) => write!(f, "{}", e),
}; }
write!(f, "{}", res)
} }
} }

View file

@ -180,7 +180,6 @@ impl Importer {
} }
} else { } else {
// to resolve imports, we need a resolver // to resolve imports, we need a resolver
let folder = location.clone().parent().unwrap();
match resolve_option { match resolve_option {
Some(resolve) => match resolve(location.clone(), import.source.to_path_buf()) { Some(resolve) => match resolve(location.clone(), import.source.to_path_buf()) {
Ok((source, new_location)) => { Ok((source, new_location)) => {

View file

@ -14,12 +14,6 @@ pub fn resolve<'a>(
current_location: CurrentLocation, current_location: CurrentLocation,
import_location: ImportLocation<'a>, import_location: ImportLocation<'a>,
) -> Result<(SourceCode, CurrentLocation), io::Error> { ) -> Result<(SourceCode, CurrentLocation), io::Error> {
println!(
"get file {} {}",
current_location.display(),
import_location.display()
);
let source = Path::new(&import_location); let source = Path::new(&import_location);
// paths starting with `./` or `../` are interpreted relative to the current file // paths starting with `./` or `../` are interpreted relative to the current file

View file

@ -1,9 +1,10 @@
use bincode::{deserialize, serialize}; use bincode::{deserialize, serialize};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::to_string_pretty; use serde_json::to_string_pretty;
use std::path::PathBuf;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use zokrates_abi::{parse_strict, Encode, Decode, Inputs}; use zokrates_abi::{parse_strict, Encode, Decode, Inputs};
use zokrates_core::compile::{compile as core_compile, CompilationArtifacts}; use zokrates_core::compile::{compile as core_compile, CompilationArtifacts, CompileError};
use zokrates_core::imports::Error; use zokrates_core::imports::Error;
use zokrates_core::ir; use zokrates_core::ir;
use zokrates_core::proof_system::{self, ProofSystem}; use zokrates_core::proof_system::{self, ProofSystem};
@ -30,8 +31,8 @@ pub struct ComputationResult {
} }
impl ResolverResult { impl ResolverResult {
fn into_tuple(self) -> (String, String) { fn into_tuple(self) -> (String, PathBuf) {
(self.source, self.location) (self.source, PathBuf::from(self.location))
} }
} }
@ -53,30 +54,38 @@ pub fn compile(
location: JsValue, location: JsValue,
resolve: &js_sys::Function, resolve: &js_sys::Function,
) -> Result<JsValue, JsValue> { ) -> Result<JsValue, JsValue> {
let closure = |l: String, p: String| { let closure = |l: PathBuf, p: PathBuf| {
let value = resolve let value = resolve
.call2(&JsValue::UNDEFINED, &l.into(), &p.clone().into()) .call2(&JsValue::UNDEFINED, &l.display().to_string().into(), &p.clone().display().to_string().into())
.map_err(|_| { .map_err(|_| {
Error::new(format!( Error::new(format!(
"Error thrown in callback: Could not resolve `{}`", "Error thrown in callback: Could not resolve `{}`",
p p.display()
)) ))
})?; })?;
if value.is_null() || value.is_undefined() { if value.is_null() || value.is_undefined() {
Err(Error::new(format!("Could not resolve `{}`", p))) Err(Error::new(format!("Could not resolve `{}`", p.display())))
} else { } else {
let result: ResolverResult = value.into_serde().unwrap(); let result: ResolverResult = value.into_serde().unwrap();
Ok(result.into_tuple()) Ok(result.into_tuple())
} }
}; };
let fmt_error = |e: &CompileError| {
format!(
"{}:{}",
e.file().display(),
e.value()
)
};
let artifacts: CompilationArtifacts<FieldPrime> = core_compile( let artifacts: CompilationArtifacts<FieldPrime> = core_compile(
source.as_string().unwrap(), source.as_string().unwrap(),
location.as_string().unwrap(), PathBuf::from(location.as_string().unwrap()),
Some(&closure), Some(&closure),
) )
.map_err(|ce| JsValue::from_str(&format!("{}", ce)))?; .map_err(|ce| JsValue::from_str(&format!("{}", ce.0.iter().map(|e| fmt_error(e)).collect::<Vec<_>>().join("\n"))))?;
let result = CompilationResult { let result = CompilationResult {
program: serialize_program(artifacts.prog())?, program: serialize_program(artifacts.prog())?,