make zokrates_core platform independent by removing the canonicalize call
This commit is contained in:
parent
43075af3d6
commit
602d5c27ea
8 changed files with 57 additions and 66 deletions
|
@ -1,2 +0,0 @@
|
|||
def foo() -> (field):
|
||||
return 1
|
|
@ -1,4 +0,0 @@
|
|||
from "./dep/foo.zok" import foo as bar
|
||||
|
||||
def foo() -> (field):
|
||||
return 2 + bar()
|
|
@ -1,4 +0,0 @@
|
|||
from "./dep/foo.zok" import foo
|
||||
|
||||
def main() -> (field):
|
||||
return foo()
|
|
@ -13,7 +13,7 @@ use std::io::{stdin, BufReader, BufWriter, Read, Write};
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::string::String;
|
||||
use zokrates_abi::Encode;
|
||||
use zokrates_core::compile::{compile, CompilationArtifacts};
|
||||
use zokrates_core::compile::{compile, CompilationArtifacts, CompileError};
|
||||
use zokrates_core::ir;
|
||||
use zokrates_core::proof_system::*;
|
||||
use zokrates_core::typed_absy::abi::Abi;
|
||||
|
@ -287,9 +287,29 @@ fn cli() -> Result<(), String> {
|
|||
.read_to_string(&mut source)
|
||||
.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> =
|
||||
compile(source, path, Some(&fs_resolve))
|
||||
.map_err(|e| format!("Compilation failed:\n\n {}", e))?;
|
||||
compile(source, path, Some(&fs_resolve)).map_err(|e| {
|
||||
format!(
|
||||
"Compilation failed:\n\n{}",
|
||||
e.0.iter()
|
||||
.map(|e| fmt_error(e))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n")
|
||||
)
|
||||
})?;
|
||||
|
||||
let program_flattened = artifacts.prog();
|
||||
|
||||
|
|
|
@ -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)]
|
||||
pub enum CompileErrorInner {
|
||||
ParserError(pest::Error),
|
||||
|
@ -81,6 +67,16 @@ pub struct CompileError {
|
|||
value: CompileErrorInner,
|
||||
}
|
||||
|
||||
impl CompileError {
|
||||
pub fn file(&self) -> &PathBuf {
|
||||
&self.file
|
||||
}
|
||||
|
||||
pub fn value(&self) -> &CompileErrorInner {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl CompileErrors {
|
||||
pub fn with_context(self, file: PathBuf) -> Self {
|
||||
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 {
|
||||
fn from(error: pest::Error) -> Self {
|
||||
CompileErrorInner::ParserError(error)
|
||||
|
@ -140,13 +120,12 @@ impl From<semantics::Error> for CompileError {
|
|||
|
||||
impl fmt::Display for CompileErrorInner {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let res = match *self {
|
||||
CompileErrorInner::ParserError(ref e) => format!("{}", e),
|
||||
CompileErrorInner::SemanticError(ref e) => format!("{}", e),
|
||||
CompileErrorInner::ReadError(ref e) => format!("{}", e),
|
||||
CompileErrorInner::ImportError(ref e) => format!("{}", e),
|
||||
};
|
||||
write!(f, "{}", res)
|
||||
match *self {
|
||||
CompileErrorInner::ParserError(ref e) => write!(f, "{}", e),
|
||||
CompileErrorInner::SemanticError(ref e) => write!(f, "{}", e),
|
||||
CompileErrorInner::ReadError(ref e) => write!(f, "{}", e),
|
||||
CompileErrorInner::ImportError(ref e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -180,7 +180,6 @@ impl Importer {
|
|||
}
|
||||
} else {
|
||||
// to resolve imports, we need a resolver
|
||||
let folder = location.clone().parent().unwrap();
|
||||
match resolve_option {
|
||||
Some(resolve) => match resolve(location.clone(), import.source.to_path_buf()) {
|
||||
Ok((source, new_location)) => {
|
||||
|
|
|
@ -14,12 +14,6 @@ pub fn resolve<'a>(
|
|||
current_location: CurrentLocation,
|
||||
import_location: ImportLocation<'a>,
|
||||
) -> Result<(SourceCode, CurrentLocation), io::Error> {
|
||||
println!(
|
||||
"get file {} {}",
|
||||
current_location.display(),
|
||||
import_location.display()
|
||||
);
|
||||
|
||||
let source = Path::new(&import_location);
|
||||
|
||||
// paths starting with `./` or `../` are interpreted relative to the current file
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use bincode::{deserialize, serialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::to_string_pretty;
|
||||
use std::path::PathBuf;
|
||||
use wasm_bindgen::prelude::*;
|
||||
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::ir;
|
||||
use zokrates_core::proof_system::{self, ProofSystem};
|
||||
|
@ -30,8 +31,8 @@ pub struct ComputationResult {
|
|||
}
|
||||
|
||||
impl ResolverResult {
|
||||
fn into_tuple(self) -> (String, String) {
|
||||
(self.source, self.location)
|
||||
fn into_tuple(self) -> (String, PathBuf) {
|
||||
(self.source, PathBuf::from(self.location))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,30 +54,38 @@ pub fn compile(
|
|||
location: JsValue,
|
||||
resolve: &js_sys::Function,
|
||||
) -> Result<JsValue, JsValue> {
|
||||
let closure = |l: String, p: String| {
|
||||
let closure = |l: PathBuf, p: PathBuf| {
|
||||
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(|_| {
|
||||
Error::new(format!(
|
||||
"Error thrown in callback: Could not resolve `{}`",
|
||||
p
|
||||
p.display()
|
||||
))
|
||||
})?;
|
||||
|
||||
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 {
|
||||
let result: ResolverResult = value.into_serde().unwrap();
|
||||
Ok(result.into_tuple())
|
||||
}
|
||||
};
|
||||
|
||||
let fmt_error = |e: &CompileError| {
|
||||
format!(
|
||||
"{}:{}",
|
||||
e.file().display(),
|
||||
e.value()
|
||||
)
|
||||
};
|
||||
|
||||
let artifacts: CompilationArtifacts<FieldPrime> = core_compile(
|
||||
source.as_string().unwrap(),
|
||||
location.as_string().unwrap(),
|
||||
PathBuf::from(location.as_string().unwrap()),
|
||||
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 {
|
||||
program: serialize_program(artifacts.prog())?,
|
||||
|
|
Loading…
Reference in a new issue