Skip to content

Commit

Permalink
fix(compiler): compile race condition in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Nov 2, 2023
1 parent 343287c commit f968262
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub mod world;

/// Diff and parse the source code.
mod parser;
mod utils;

/// Convenient services over [`world::CompilerWorld`].
pub mod service;
Expand Down
22 changes: 22 additions & 0 deletions compiler/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[allow(unused_macros)]
macro_rules! console_log {
($($arg:tt)*) => {
web_sys::console::info_1(&format!(
$($arg)*
).into());
}
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[allow(unused_macros)]
macro_rules! console_log {
($($arg:tt)*) => {
println!(
$($arg)*
);
}
}

#[allow(unused_imports)]
pub(crate) use console_log;
24 changes: 22 additions & 2 deletions compiler/src/vfs/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::time::SystemTime;

use super::AccessModel;

#[derive(Debug)]
#[derive(Debug, Clone)]
struct OverlayFileMeta {
mt: SystemTime,
content: Bytes,
Expand Down Expand Up @@ -52,7 +52,27 @@ impl<M: AccessModel> OverlayAccessModel<M> {

let mt = SystemTime::now();
let meta = OverlayFileMeta { mt, content };
self.files.write().insert(path, meta);
self.files
.write()
.entry(path)
.and_modify(|e| {
// unlikely to happen but still possible in concurrent
// environment
// The case is found in browser test
if e.mt == meta.mt && e.content != meta.content {
e.mt = meta
.mt
// instant::SystemTime has a minimum resolution of 1ms
// we negate the time by 1ms so that the time is always
// invalidated
.checked_sub(std::time::Duration::from_millis(1))
.unwrap();
e.content = meta.content.clone();
} else {
*e = meta.clone();
}
})
.or_insert(meta);
}

pub fn remove_file(&self, path: &Path) {
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/vfs/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<M: AccessModel + Sized, C: Clone> TraceAccessModel<CachedAccessModel<M, C>>
elapsed.as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
println!("read_all_diff: {:?} {:?}", src, elapsed);
crate::utils::console_log!("read_all_diff: {:?} {:?}", src, elapsed);
res
}
}
Expand All @@ -60,7 +60,7 @@ impl<M: AccessModel + Sized> AccessModel for TraceAccessModel<M> {
elapsed.as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
println!("mtime: {:?} {:?}", src, elapsed);
crate::utils::console_log!("mtime: {:?} {:?} => {:?}", src, elapsed, res);
res
}

Expand All @@ -72,7 +72,7 @@ impl<M: AccessModel + Sized> AccessModel for TraceAccessModel<M> {
elapsed.as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
println!("is_file: {:?} {:?}", src, elapsed);
crate::utils::console_log!("is_file: {:?} {:?}", src, elapsed);
res
}

Expand All @@ -84,7 +84,7 @@ impl<M: AccessModel + Sized> AccessModel for TraceAccessModel<M> {
elapsed.as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
println!("real_path: {:?} {:?}", src, elapsed);
crate::utils::console_log!("real_path: {:?} {:?}", src, elapsed);
res
}

Expand All @@ -96,7 +96,7 @@ impl<M: AccessModel + Sized> AccessModel for TraceAccessModel<M> {
elapsed.as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed,
);
println!("read_all: {:?} {:?}", src, elapsed);
crate::utils::console_log!("read_all: {:?} {:?}", src, elapsed);
res
}

Expand Down

0 comments on commit f968262

Please sign in to comment.