From 7e55808212d1ee4af1f30e3c70faa60e531d591c Mon Sep 17 00:00:00 2001 From: dark64 Date: Sat, 21 Dec 2019 01:49:49 +0100 Subject: [PATCH] simplify js to rust closures --- zokrates_cli/src/bin.rs | 8 ++--- zokrates_core/src/compile.rs | 2 +- zokrates_js/Cargo.lock | 65 +++++++++++++++++++----------------- zokrates_js/Cargo.toml | 4 +-- zokrates_js/js/index.d.ts | 10 +++--- zokrates_js/js/index.js | 23 +++++-------- zokrates_js/src/lib.rs | 55 +++++++++++++----------------- zokrates_test/src/lib.rs | 2 +- 8 files changed, 79 insertions(+), 90 deletions(-) diff --git a/zokrates_cli/src/bin.rs b/zokrates_cli/src/bin.rs index 52ddee46..503a3c0e 100644 --- a/zokrates_cli/src/bin.rs +++ b/zokrates_cli/src/bin.rs @@ -276,7 +276,7 @@ fn cli() -> Result<(), String> { .map_err(|why| format!("couldn't open input file {}: {}", path.display(), why))?; let program_flattened: ir::Prog = - compile(source, location, Some(fs_resolve)) + compile(source, location, Some(&fs_resolve)) .map_err(|e| format!("Compilation failed:\n\n {}", e))?; // number of constraints the flattened program will translate to. @@ -629,7 +629,7 @@ mod tests { let mut source = String::new(); reader.read_to_string(&mut source).unwrap(); - let _: ir::Prog = compile(source, location, Some(fs_resolve)).unwrap(); + let _: ir::Prog = compile(source, location, Some(&fs_resolve)).unwrap(); } } @@ -658,7 +658,7 @@ mod tests { reader.read_to_string(&mut source).unwrap(); let program_flattened: ir::Prog = - compile(source, location, Some(fs_resolve)).unwrap(); + compile(source, location, Some(&fs_resolve)).unwrap(); let _ = program_flattened .execute(&vec![FieldPrime::from(0)]) @@ -692,7 +692,7 @@ mod tests { reader.read_to_string(&mut source).unwrap(); let program_flattened: ir::Prog = - compile(source, location, Some(fs_resolve)).unwrap(); + compile(source, location, Some(&fs_resolve)).unwrap(); let _ = program_flattened .execute(&vec![FieldPrime::from(0)]) diff --git a/zokrates_core/src/compile.rs b/zokrates_core/src/compile.rs index a9df576b..f7607a58 100644 --- a/zokrates_core/src/compile.rs +++ b/zokrates_core/src/compile.rs @@ -120,7 +120,7 @@ impl fmt::Display for CompileErrorInner { } } -pub type Resolve = fn(String, String) -> Result<(String, String), E>; +pub type Resolve<'a, E> = &'a dyn Fn(String, String) -> Result<(String, String), E>; pub fn compile>( source: String, diff --git a/zokrates_js/Cargo.lock b/zokrates_js/Cargo.lock index 2420cbde..4d0c227c 100644 --- a/zokrates_js/Cargo.lock +++ b/zokrates_js/Cargo.lock @@ -58,7 +58,7 @@ dependencies = [ "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "pairing_ce 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "web-sys 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "web-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -72,7 +72,7 @@ dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "pairing_ce 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "web-sys 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "web-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -170,7 +170,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -328,10 +328,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "js-sys" -version = "0.3.32" +version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -647,6 +647,9 @@ dependencies = [ name = "serde" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "serde_bytes" @@ -820,18 +823,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "wasm-bindgen" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-macro 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-macro 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bumpalo 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -840,38 +843,38 @@ dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-shared 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-macro-support 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-macro-support 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-backend 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-shared 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-backend 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "wasm-bindgen-webidl" -version = "0.2.55" +version = "0.2.56" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -880,20 +883,20 @@ dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-backend 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-backend 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", "weedle 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "web-sys" -version = "0.3.32" +version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", - "js-sys 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)", "sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen-webidl 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-webidl 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -989,9 +992,9 @@ version = "1.0.18" dependencies = [ "bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "console_error_panic_hook 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "js-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", - "wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)", "zokrates_abi 0.1.0", "zokrates_core 0.4.0", "zokrates_field 0.3.4", @@ -1059,7 +1062,7 @@ dependencies = [ "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" -"checksum js-sys 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "1c840fdb2167497b0bd0db43d6dfe61e91637fa72f9d061f8bd17ddc44ba6414" +"checksum js-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)" = "367647c532db6f1555d7151e619540ec5f713328235b8c062c6b4f63e84adfe3" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum log 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "1b9ad466a945c9c40f6f9a449c55675547e59bc75a2722d4689042ab3ae80c9c" @@ -1121,13 +1124,13 @@ dependencies = [ "checksum utf8-ranges 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" -"checksum wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "29ae32af33bacd663a9a28241abecf01f2be64e6a185c6139b04f18b6385c5f2" -"checksum wasm-bindgen-backend 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "1845584bd3593442dc0de6e6d9f84454a59a057722f36f005e44665d6ab19d85" -"checksum wasm-bindgen-macro 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "87fcc747e6b73c93d22c947a6334644d22cfec5abd8b66238484dc2b0aeb9fe4" -"checksum wasm-bindgen-macro-support 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "3dc4b3f2c4078c8c4a5f363b92fcf62604c5913cbd16c6ff5aaf0f74ec03f570" -"checksum wasm-bindgen-shared 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "ca0b78d6d3be8589b95d1d49cdc0794728ca734adf36d7c9f07e6459508bb53d" -"checksum wasm-bindgen-webidl 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "3126356474ceb717c8fb5549ae387c9fbf4872818454f4d87708bee997214bb5" -"checksum web-sys 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "98405c0a2e722ed3db341b4c5b70eb9fe0021621f7350bab76df93b09b649bbf" +"checksum wasm-bindgen 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "99de4b68939a880d530aed51289a7c7baee154e3ea8ac234b542c49da7134aaf" +"checksum wasm-bindgen-backend 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "b58e66a093a7b7571cb76409763c495b8741ac4319ac20acc2b798f6766d92ee" +"checksum wasm-bindgen-macro 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "a80f89daea7b0a67b11f6e9f911422ed039de9963dce00048a653b63d51194bf" +"checksum wasm-bindgen-macro-support 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "4f9dbc3734ad6cff6b76b75b7df98c06982becd0055f651465a08f769bca5c61" +"checksum wasm-bindgen-shared 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "d907984f8506b3554eab48b8efff723e764ddbf76d4cd4a3fe4196bc00c49a70" +"checksum wasm-bindgen-webidl 0.2.56 (registry+https://github.com/rust-lang/crates.io-index)" = "f85a3825a459cf6a929d03bacb54dca37a614d43032ad1343ef2d4822972947d" +"checksum web-sys 0.3.33 (registry+https://github.com/rust-lang/crates.io-index)" = "2fb60433d0dc12c803b9b017b3902d80c9451bab78d27bc3210bf2a7b96593f1" "checksum weedle 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/zokrates_js/Cargo.toml b/zokrates_js/Cargo.toml index 731f8f4f..70efcf6e 100644 --- a/zokrates_js/Cargo.toml +++ b/zokrates_js/Cargo.toml @@ -8,9 +8,9 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -serde = "^1.0.59" -serde_derive = "^1.0.59" bincode = "1.1.4" +js-sys = "0.3.33" +serde = { version = "^1.0.59", features = ["derive"] } wasm-bindgen = { version = "0.2.46", features = ["serde-serialize"] } zokrates_core = { path = "../zokrates_core", features = ["wasm"] } zokrates_field = { path = "../zokrates_field" } diff --git a/zokrates_js/js/index.d.ts b/zokrates_js/js/index.d.ts index 7512d90a..0693efd8 100644 --- a/zokrates_js/js/index.d.ts +++ b/zokrates_js/js/index.d.ts @@ -9,15 +9,15 @@ declare module 'zokrates-js' { vk: string, pk: Uint8Array, } - + + export type ResolveCallback = (location: string, path: string) => ResolverResult; export interface ZoKratesProvider { - compile(source: string, location: string): Uint8Array; + compile(source: string, location: string, callback: ResolveCallback): Uint8Array; setup(program: Uint8Array): SetupKeypair; computeWitness(program: Uint8Array, args: string[]): string; exportSolidityVerifier(verifyingKey: string, isAbiv2: boolean): string generateProof(program: Uint8Array, witness: string, provingKey: Uint8Array): string; } - - export type ResolveCallback = (location: string, path: string) => ResolverResult; - export function initialize(callback: ResolveCallback): Promise; + + export function initialize(): Promise; } \ No newline at end of file diff --git a/zokrates_js/js/index.js b/zokrates_js/js/index.js index b45896e6..d866e57c 100644 --- a/zokrates_js/js/index.js +++ b/zokrates_js/js/index.js @@ -1,7 +1,7 @@ import { appendExtension, getAbsolutePath } from './utils'; import stdlib from '../stdlib.json'; -const initialize = async (callback) => { +const initialize = async () => { const EXTENSION_ZOK = '.zok'; const RESERVED_PATHS = [ @@ -14,18 +14,11 @@ const initialize = async (callback) => { // load web assembly module const zokrates = await import('../pkg/index.js'); - // register module resolving callback to provided namespace - globalThis.resolve = (location, path) => resolveModule(location, path, callback); - const resolveModule = (location, path, callback) => { - let result; if (isReserved(location) || isReserved(path)) { - result = resolveFromStandardLibrary(location, path); - } else { - result = callback(location, path); - } - if (!result) return null; - return zokrates.ResolverResult.new(result.source, result.location); + return resolveFromStandardLibrary(location, path); + } + return callback(location, path); } const isReserved = (path) => RESERVED_PATHS.some(p => path.startsWith(p)); @@ -37,8 +30,10 @@ const initialize = async (callback) => { } return { - compile: (source, location) => { - return Uint8Array.from(zokrates.compile(source, location)); + compile: (source, location, callback) => { + return Uint8Array.from( + zokrates.compile(source, location, (location, path) => resolveModule(location, path, callback)) + ); }, setup: (program) => { let result = zokrates.setup(Array.from(program)); @@ -60,4 +55,4 @@ const initialize = async (callback) => { } } -export { initialize }; +export { initialize } \ No newline at end of file diff --git a/zokrates_js/src/lib.rs b/zokrates_js/src/lib.rs index 0e96f9c7..9a9fbf83 100644 --- a/zokrates_js/src/lib.rs +++ b/zokrates_js/src/lib.rs @@ -1,59 +1,50 @@ +use serde::{Deserialize, Serialize}; use wasm_bindgen::prelude::*; use zokrates_abi::{parse_strict, Encode, Inputs}; use zokrates_core::compile::{compile as compile_core, CompileErrors}; +use zokrates_core::imports::Error; use zokrates_core::ir; use zokrates_core::proof_system::{self, ProofSystem}; use zokrates_field::field::FieldPrime; -extern crate serde_derive; - -#[wasm_bindgen] +#[derive(Serialize, Deserialize)] pub struct ResolverResult { source: String, location: String, } -#[wasm_bindgen] -impl ResolverResult { - #[wasm_bindgen] - pub fn new(source: String, location: String) -> Self { - ResolverResult { source, location } - } -} - -#[wasm_bindgen] -extern "C" { - fn resolve(l: String, p: String) -> Option; - - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); -} - fn deserialize_program(input: &JsValue) -> ir::Prog { let vec: Vec = input.into_serde().unwrap(); bincode::deserialize(&vec).unwrap() } #[wasm_bindgen] -pub fn compile(source: JsValue, location: JsValue) -> Result { - fn resolve_closure<'a>( - l: String, - p: String, - ) -> Result<(String, String), zokrates_core::imports::Error> { - let result = resolve(l, p.clone()); - match result { - Some(res) => Ok((res.source, res.location)), - None => Err(zokrates_core::imports::Error::new(String::from(format!( - "Unable to resolve {}", - p - )))), +pub fn compile( + source: JsValue, + location: JsValue, + resolve: &js_sys::Function, +) -> Result { + let closure = |l: String, p: String| match resolve.call2( + &JsValue::UNDEFINED, + &l.into(), + &p.clone().into(), + ) { + Ok(value) => { + let result: ResolverResult = value + .into_serde() + .map_err(|_| Error::new(format!("Could not resolve `{}`", p)))?; + Ok((result.source, result.location)) } + Err(_) => Err(Error::new(format!( + "Error thrown in resolve callback; could not resolve `{}`", + p + ))), }; let program_flattened: Result, CompileErrors> = compile_core( source.as_string().unwrap(), location.as_string().unwrap(), - Some(resolve_closure), + Some(&closure), ); match program_flattened { diff --git a/zokrates_test/src/lib.rs b/zokrates_test/src/lib.rs index 2c6fa7ab..1ebfdaab 100644 --- a/zokrates_test/src/lib.rs +++ b/zokrates_test/src/lib.rs @@ -85,7 +85,7 @@ pub fn test_inner(test_path: &str) { .to_str() .unwrap() .to_string(), - Some(resolve), + Some(&resolve), ) .unwrap();