diff --git a/Cargo.lock b/Cargo.lock index 7b04bbb9f07..c318f2c29db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,7 +72,7 @@ dependencies = [ "k256", "num-bigint", "num-traits", - "sha2", + "sha2 0.9.9", "thiserror", ] @@ -89,7 +89,7 @@ dependencies = [ "k256", "num-bigint", "num-traits", - "sha2", + "sha2 0.9.9", "thiserror", ] @@ -602,6 +602,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "build-data" version = "0.1.3" @@ -1185,6 +1194,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ + "block-buffer 0.10.4", "crypto-common", ] @@ -1407,6 +1417,7 @@ version = "0.2.0" dependencies = [ "cfg-if 1.0.0", "codespan-reporting 0.9.5", + "rust-embed", "tempfile", "wasm-bindgen", ] @@ -1935,7 +1946,7 @@ dependencies = [ "cfg-if 1.0.0", "ecdsa", "elliptic-curve", - "sha2", + "sha2 0.9.9", ] [[package]] @@ -2774,6 +2785,40 @@ dependencies = [ "serde", ] +[[package]] +name = "rust-embed" +version = "6.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb133b9a38b5543fad3807fb2028ea47c5f2b566f4f5e28a11902f1a358348b6" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "6.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4e0f0ced47ded9a68374ac145edd65a6c1fa13a96447b873660b2a568a0fd7" +dependencies = [ + "proc-macro2", + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "7.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512b0ab6853f7e14e3c8754acb43d6f748bb9ced66aa5915a6553ac8213f7731" +dependencies = [ + "sha2 0.10.6", + "walkdir", +] + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -2904,6 +2949,15 @@ dependencies = [ "safe-regex-compiler", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -3029,13 +3083,24 @@ version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" dependencies = [ - "block-buffer", + "block-buffer 0.9.0", "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", ] +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.6", +] + [[package]] name = "sharded-slab" version = "0.1.4" @@ -3508,6 +3573,17 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + [[package]] name = "want" version = "0.3.0" diff --git a/crates/fm/Cargo.toml b/crates/fm/Cargo.toml index 1a4c4dd7f1c..8614671a861 100644 --- a/crates/fm/Cargo.toml +++ b/crates/fm/Cargo.toml @@ -9,6 +9,7 @@ edition.workspace = true [dependencies] codespan-reporting.workspace = true cfg-if.workspace = true +rust-embed = "6.6.0" [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen.workspace = true diff --git a/crates/fm/src/file_reader.rs b/crates/fm/src/file_reader.rs index 28c1c148fdc..2902e27e9f3 100644 --- a/crates/fm/src/file_reader.rs +++ b/crates/fm/src/file_reader.rs @@ -1,29 +1,57 @@ +use rust_embed::RustEmbed; use std::io::Error; use std::path::Path; + // Based on the environment, we either read files using the rust standard library or we // read files using the javascript host function +#[derive(RustEmbed)] +#[folder = "../../noir_stdlib/src"] +#[prefix = "std/"] +struct StdLibAssets; + cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] { use wasm_bindgen::{prelude::*, JsValue}; #[wasm_bindgen(module = "@noir-lang/noir-source-resolver")] extern "C" { + #[wasm_bindgen(catch)] fn read_file(path: &str) -> Result; + } pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result { use std::io::ErrorKind; + let path_str = path_to_file.as_os_str().to_str().unwrap(); - match read_file(path_str) { - Ok(buffer) => Ok(buffer), - Err(_) => Err(Error::new(ErrorKind::Other, "could not read file using wasm")), + + match StdLibAssets::get(path_str) { + + Some(std_lib_asset) => { + Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string()) + }, + + None => match read_file(path_str) { + Ok(buffer) => Ok(buffer), + Err(_) => Err(Error::new(ErrorKind::Other, "could not read file using wasm")), + } + } } } else { pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result { - std::fs::read_to_string(path_to_file) + + match StdLibAssets::get(path_to_file.to_str().unwrap()) { + + Some(std_lib_asset) => { + Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string()) + }, + + None => std::fs::read_to_string(path_to_file) + + } } } } diff --git a/crates/nargo/src/cli/mod.rs b/crates/nargo/src/cli/mod.rs index 618830527b8..2120a58768a 100644 --- a/crates/nargo/src/cli/mod.rs +++ b/crates/nargo/src/cli/mod.rs @@ -99,16 +99,11 @@ pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> boo } fn add_std_lib(driver: &mut Driver) { - let path_to_std_lib_file = path_to_stdlib().join("lib.nr"); - let std_crate = driver.create_non_local_crate(path_to_std_lib_file, CrateType::Library); let std_crate_name = "std"; + let path_to_std_lib_file = PathBuf::from(std_crate_name).join("lib.nr"); + let std_crate = driver.create_non_local_crate(path_to_std_lib_file, CrateType::Library); driver.propagate_dep(std_crate, &CrateName::new(std_crate_name).unwrap()); } - -fn path_to_stdlib() -> PathBuf { - dirs::config_dir().unwrap().join("noir-lang").join("std/src") -} - // FIXME: I not sure that this is the right place for this tests. #[cfg(test)] mod tests { diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index f34103cc3de..7088080aac3 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -36,11 +36,11 @@ const BUILD_INFO: BuildInfo = BuildInfo { dirty: env!("GIT_DIRTY"), }; -pub fn add_noir_lib(driver: &mut Driver, crate_name: String) { +pub fn add_noir_lib(driver: &mut Driver, crate_name: &str) { let path_to_lib = PathBuf::from(&crate_name).join("lib.nr"); let library_crate = driver.create_non_local_crate(path_to_lib, CrateType::Library); - driver.propagate_dep(library_crate, &CrateName::new(crate_name.as_str()).unwrap()); + driver.propagate_dep(library_crate, &CrateName::new(crate_name).unwrap()); } #[wasm_bindgen] @@ -54,8 +54,11 @@ pub fn compile(args: JsValue) -> JsValue { driver.create_local_crate(path, CrateType::Binary); + // We are always adding std lib implicitly. It comes bundled with binary. + add_noir_lib(&mut driver, "std"); + for dependency in options.optional_dependencies_set { - add_noir_lib(&mut driver, dependency); + add_noir_lib(&mut driver, dependency.as_str()); } driver.check_crate(&options.compile_options).unwrap_or_else(|_| panic!("Crate check failed"));