Fix sync build.

This commit is contained in:
Stephen Chung 2022-01-28 08:00:46 +08:00
parent e476929597
commit 09d03d07ed

View File

@ -6,7 +6,6 @@ use rhai::debugger::DebuggerCommand;
#[cfg(feature = "debugging")] #[cfg(feature = "debugging")]
use std::{ use std::{
cell::RefCell,
env, env,
fs::File, fs::File,
io::{stdin, stdout, Read, Write}, io::{stdin, stdout, Read, Write},
@ -234,25 +233,33 @@ fn main() {
// Hook up debugger // Hook up debugger
let lines: Vec<_> = script.trim().split('\n').map(|s| s.to_string()).collect(); let lines: Vec<_> = script.trim().split('\n').map(|s| s.to_string()).collect();
let current_source = RefCell::new(rhai::Identifier::new_const()); #[cfg(not(feature = "sync"))]
let current_source = std::cell::RefCell::new(rhai::Identifier::new_const());
#[cfg(feature = "sync")]
let current_source = std::sync::RwLock::new(rhai::Identifier::new_const());
engine.on_debugger(move |context, node, source, pos| { engine.on_debugger(move |context, node, source, pos| {
#[cfg(not(feature = "sync"))]
let current_source = &mut *current_source.borrow_mut();
#[cfg(feature = "sync")]
let current_source = &mut *current_source.write().unwrap();
// Check source // Check source
if let Some(src) = source { if let Some(src) = source {
if src != &*current_source.borrow() { if src != current_source {
println!(">>> Source => {}", src); println!(">>> Source => {}", src);
} }
// Print just a line number for imported modules // Print just a line number for imported modules
println!("{} @ {:?}", src, pos); println!("{} @ {:?}", src, pos);
} else { } else {
// The root file has no source // The root file has no source
if !current_source.borrow().is_empty() { if !current_source.is_empty() {
println!(">>> Source => main script."); println!(">>> Source => main script.");
} }
// Print the current source line // Print the current source line
print_source(&lines, pos, 0); print_source(&lines, pos, 0);
} }
*current_source.borrow_mut() = source.unwrap_or("").into(); *current_source = source.unwrap_or("").into();
// Read stdin for commands // Read stdin for commands
let mut input = String::new(); let mut input = String::new();