Skip to content

Commit

Permalink
luau: in debug mode, don't precompile scripts to bytecode so we can…
Browse files Browse the repository at this point in the history
… see line numbers when there's an error

also, don't bother to precompile the BEGIN and END blocks as they're only executed once anyway.
  • Loading branch information
jqnatividad committed Nov 24, 2023
1 parent c20798b commit 13584fa
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/cmd/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ fn sequential_mode(
let mut remap_headers = csv::StringRecord::new();
let mut new_column_count = 0_u8;
let mut headers_count = headers.len();
let debug_enabled = log_enabled!(log::Level::Debug);

if !rconfig.no_headers {
if !args.cmd_filter {
Expand Down Expand Up @@ -659,8 +660,7 @@ fn sequential_mode(
info!("Compiling and executing BEGIN script. _IDX: 0 _ROWCOUNT: 0");
LUAU_STAGE.store(Stage::Begin as i8, Ordering::Relaxed);

let begin_bytecode = luau_compiler.compile(begin_script);
if let Err(e) = luau.load(&begin_bytecode).exec() {
if let Err(e) = luau.load(begin_script).exec() {
return fail_clierror!("BEGIN error: Failed to execute \"{begin_script}\".\n{e}");
}
info!("BEGIN executed.");
Expand Down Expand Up @@ -698,7 +698,13 @@ fn sequential_mode(
// check if _IDX was used in the MAIN script
let idx_used = main_script.contains(QSV_V_IDX);

let main_bytecode = luau_compiler.compile(main_script);
// only precompile main script to bytecode if debug is disabled
let main_bytecode = if debug_enabled {
Vec::new()
} else {
luau_compiler.compile(main_script)
};

let mut record = csv::StringRecord::new();
let mut idx = 0_u64;
let mut error_count = 0_usize;
Expand All @@ -716,6 +722,7 @@ fn sequential_mode(
let no_headers = rconfig.no_headers;
let cmd_map = args.cmd_map;
let mut err_msg: String;
let mut computed_result;

// main loop
// without an index, we stream the CSV in sequential order
Expand Down Expand Up @@ -752,7 +759,15 @@ fn sequential_mode(
}
}

computed_value = match luau.load(&main_bytecode).eval() {
// if debug is enabled, we eval the script as string instead of precompiled bytecode
// so we can get more detailed error messages with line numbers
computed_result = if debug_enabled {
luau.load(main_script).eval()
} else {
luau.load(&main_bytecode).eval()
};

computed_value = match computed_result {
Ok(computed) => computed,
Err(e) => {
error_count += 1;
Expand Down Expand Up @@ -852,8 +867,7 @@ fn sequential_mode(
}

info!("Compiling and executing END script. _ROWCOUNT: {idx}");
let end_bytecode = luau_compiler.compile(end_script);
let end_value: Value = match luau.load(&end_bytecode).eval() {
let end_value: Value = match luau.load(end_script).eval() {
Ok(computed) => computed,
Err(e) => {
let err_msg = format!("<ERROR> END error: Cannot evaluate \"{end_script}\".\n{e}");
Expand Down Expand Up @@ -942,6 +956,7 @@ fn random_access_mode(
let mut remap_headers = csv::StringRecord::new();
let mut new_column_count = 0_u8;
let mut headers_count = headers.len();
let debug_enabled = log_enabled!(log::Level::Debug);

if !rconfig.no_headers {
if !args.cmd_filter {
Expand Down Expand Up @@ -980,8 +995,7 @@ fn random_access_mode(
);
LUAU_STAGE.store(Stage::Begin as i8, Ordering::Relaxed);

let begin_bytecode = luau_compiler.compile(begin_script);
if let Err(e) = luau.load(&begin_bytecode).exec() {
if let Err(e) = luau.load(begin_script).exec() {
return fail_clierror!("BEGIN error: Failed to execute \"{begin_script}\".\n{e}");
}
info!("BEGIN executed.");
Expand Down Expand Up @@ -1012,7 +1026,11 @@ fn random_access_mode(
debug!("BEGIN current record: {curr_record}");
idx_file.seek(curr_record)?;

let main_bytecode = luau_compiler.compile(main_script);
let main_bytecode = if debug_enabled {
Vec::new()
} else {
luau_compiler.compile(main_script)
};
let mut record = csv::StringRecord::new();
let mut error_count = 0_usize;
let mut processed_count = 0_usize;
Expand Down Expand Up @@ -1058,6 +1076,7 @@ fn random_access_mode(
let no_headers = rconfig.no_headers;
let cmd_map = args.cmd_map;
let mut err_msg: String;
let mut computed_result;

// main loop - here we use an indexed file reader to implement random access mode,
// seeking to the next record to read by looking at _INDEX special var
Expand Down Expand Up @@ -1092,7 +1111,13 @@ fn random_access_mode(
}
}

computed_value = match luau.load(&main_bytecode).eval() {
computed_result = if debug_enabled {
luau.load(main_script).eval()
} else {
luau.load(&main_bytecode).eval()
};

computed_value = match computed_result {
Ok(computed) => computed,
Err(e) => {
error_count += 1;
Expand Down Expand Up @@ -1191,8 +1216,7 @@ fn random_access_mode(
info!("Compiling and executing END script. _ROWCOUNT: {row_count}");
LUAU_STAGE.store(Stage::End as i8, Ordering::Relaxed);

let end_bytecode = luau_compiler.compile(end_script);
let end_value: Value = match luau.load(&end_bytecode).eval() {
let end_value: Value = match luau.load(end_script).eval() {
Ok(computed) => computed,
Err(e) => {
let err_msg = format!("<ERROR> END error: Cannot evaluate \"{end_script}\".\n{e}");
Expand Down

0 comments on commit 13584fa

Please sign in to comment.