Skip to content

Commit

Permalink
Merge branch 'master' of github.com:noir-lang/noir into jb-constructo…
Browse files Browse the repository at this point in the history
…r-formatter
  • Loading branch information
jonybur committed Oct 12, 2023
2 parents 8ae96b8 + 526a68e commit 9f88f2d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ impl AcirContext {
// Optimistically try executing the brillig now, if we can complete execution they just return the results.
// This is a temporary measure pending SSA optimizations being applied to Brillig which would remove constant-input opcodes (See #2066)
if let Some(brillig_outputs) =
self.execute_brillig(generated_brillig.byte_code.clone(), &b_inputs, &outputs)
self.execute_brillig(&generated_brillig.byte_code, &b_inputs, &outputs)
{
return Ok(brillig_outputs);
}
Expand Down Expand Up @@ -965,7 +965,7 @@ impl AcirContext {

fn execute_brillig(
&mut self,
code: Vec<BrilligOpcode>,
code: &[BrilligOpcode],
inputs: &[BrilligInputs],
outputs_types: &[AcirType],
) -> Option<Vec<AcirValue>> {
Expand Down Expand Up @@ -1238,7 +1238,7 @@ pub(crate) struct AcirVar(usize);
///
/// Returns `None` if complete execution of the Brillig bytecode is not possible.
fn execute_brillig(
code: Vec<BrilligOpcode>,
code: &[BrilligOpcode],
inputs: &[BrilligInputs],
) -> Option<(Registers, Vec<Value>)> {
struct NullBbSolver;
Expand Down Expand Up @@ -1294,7 +1294,7 @@ fn execute_brillig(

// Instantiate a Brillig VM given the solved input registers and memory, along with the Brillig bytecode.
let input_registers = Registers::load(input_register_values);
let mut vm = VM::new(input_registers, input_memory, &code, Vec::new(), &NullBbSolver);
let mut vm = VM::new(input_registers, input_memory, code, Vec::new(), &NullBbSolver);

// Run the Brillig VM on these inputs, bytecode, etc!
let vm_status = vm.process_opcodes();
Expand Down
1 change: 1 addition & 0 deletions compiler/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fm.workspace = true
nargo.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_errors.workspace = true
wasm-bindgen.workspace = true
serde.workspace = true
js-sys.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub fn compile(

if contracts.unwrap_or_default() {
let compiled_contract = compile_contract(&mut context, crate_id, &compile_options)
.map_err(|_| JsCompileError::new("Failed to compile contract".to_string()))?
.map_err(|errs| {
JsCompileError::new("Failed to compile contract", errs, &context.file_manager)
})?
.0;

let optimized_contract =
Expand All @@ -68,7 +70,9 @@ pub fn compile(
Ok(<JsValue as JsValueSerdeExt>::from_serde(&preprocessed_contract).unwrap())
} else {
let compiled_program = compile_main(&mut context, crate_id, &compile_options, None, true)
.map_err(|_| JsCompileError::new("Failed to compile program".to_string()))?
.map_err(|errs| {
JsCompileError::new("Failed to compile program", errs, &context.file_manager)
})?
.0;

let optimized_program =
Expand Down
91 changes: 72 additions & 19 deletions compiler/wasm/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,82 @@
use js_sys::JsString;

use gloo_utils::format::JsValueSerdeExt;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

use fm::FileManager;
use noirc_errors::FileDiagnostic;

#[wasm_bindgen(typescript_custom_section)]
const COMPILE_ERROR: &'static str = r#"
export type CompileError = Error;
const DIAGNOSTICS: &'static str = r#"
export type Diagnostic = {
message: string;
file_path: string;
secondaries: ReadonlyArray<{
message: string;
start: number;
end: number;
}>;
}
interface CompileError {
diagnostics: ReadonlyArray<Diagnostic>;
}
"#;

/// `CompileError` is a raw js error.
/// It'd be ideal that `CompileError` was a subclass of `Error`, but for that we'd need to use JS snippets or a js module.
/// Currently JS snippets don't work with a nodejs target. And a module would be too much for just a custom error type.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = js_sys::Error, js_name = "CompileError", typescript_type = "CompileError")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JsCompileError;

#[wasm_bindgen(constructor, js_class = "Error")]
fn constructor(message: JsString) -> JsCompileError;
#[derive(Serialize, Deserialize)]
struct JsDiagnosticLabel {
message: String,
start: u32,
end: u32,
}

#[derive(Serialize, Deserialize)]
struct JsDiagnostic {
message: String,
file_path: String,
secondaries: Vec<JsDiagnosticLabel>,
}

impl JsDiagnostic {
fn new(file_diagnostic: &FileDiagnostic, file_path: String) -> JsDiagnostic {
let diagnostic = &file_diagnostic.diagnostic;
let message = diagnostic.message.clone();

let secondaries = diagnostic
.secondaries
.iter()
.map(|label| JsDiagnosticLabel {
message: label.message.clone(),
start: label.span.start(),
end: label.span.end(),
})
.collect();

JsDiagnostic { message, file_path, secondaries }
}
}

#[wasm_bindgen(getter_with_clone, js_name = "CompileError")]
pub struct JsCompileError {
pub message: js_sys::JsString,
pub diagnostics: JsValue,
}

impl JsCompileError {
/// Creates a new execution error with the given call stack.
/// Call stacks won't be optional in the future, after removing ErrorLocation in ACVM.
pub fn new(message: String) -> Self {
JsCompileError::constructor(JsString::from(message))
pub fn new(
message: &str,
file_diagnostics: Vec<FileDiagnostic>,
file_manager: &FileManager,
) -> JsCompileError {
let diagnostics: Vec<_> = file_diagnostics
.iter()
.map(|err| {
JsDiagnostic::new(err, file_manager.path(err.file_id).to_str().unwrap().to_string())
})
.collect();

JsCompileError {
message: js_sys::JsString::from(message.to_string()),
diagnostics: <JsValue as JsValueSerdeExt>::from_serde(&diagnostics).unwrap(),
}
}
}
10 changes: 10 additions & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ impl FmtVisitor<'_> {
.join(", ");
format!("{} {{ {} }}", type_str, formatted_fields)
}
ExpressionKind::Call(call_expr) => {
let formatted_func = self.format_expr(*call_expr.func);
let formatted_args = call_expr
.arguments
.into_iter()
.map(|arg| self.format_expr(arg))
.collect::<Vec<_>>()
.join(", ");
format!("{}({})", formatted_func, formatted_args)
}
ExpressionKind::MethodCall(method_call_expr) => {
let formatted_object = self.format_expr(method_call_expr.object).trim().to_string();
let formatted_args = method_call_expr
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/tests/expected/call.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
main(4, 3);
fn foo() {
my_function(10, some_value, another_func(20, 30));
}
6 changes: 3 additions & 3 deletions tooling/nargo_fmt/tests/input/call.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
main(4, 3);
}
fn foo() {
my_function( 10,some_value,another_func( 20 , 30) );
}

0 comments on commit 9f88f2d

Please sign in to comment.