Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compile): Noir std lib embedded #973

Merged
merged 4 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions crates/fm/src/file_reader.rs
Original file line number Diff line number Diff line change
@@ -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<String, JsValue>;

}

pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result<String, Error> {
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<String, Error> {
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)

}
}
}
}
9 changes: 2 additions & 7 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"));
Expand Down