1mod bash;
2mod c;
3mod cpp;
4mod crystal;
5mod csharp;
6mod dart;
7mod elixir;
8mod go;
9mod groovy;
10mod haskell;
11mod java;
12mod javascript;
13mod julia;
14mod kotlin;
15mod lua;
16mod nim;
17mod perl;
18mod php;
19mod python;
20mod r;
21mod ruby;
22mod rust;
23mod swift;
24mod typescript;
25mod zig;
26
27use std::borrow::Cow;
28use std::collections::HashMap;
29use std::path::{Path, PathBuf};
30use std::process::{Child, Command, Output, Stdio};
31use std::sync::atomic::{AtomicBool, Ordering};
32use std::sync::{LazyLock, Mutex, OnceLock};
33use std::time::{Duration, Instant};
34
35use anyhow::{Context, Result, bail};
36
37use crate::cli::InputSource;
38use crate::language::{LanguageSpec, canonical_language_id};
39
40static SCCACHE_INIT: OnceLock<()> = OnceLock::new();
45static SCCACHE_READY: AtomicBool = AtomicBool::new(false);
46static PERF_COUNTERS: LazyLock<Mutex<HashMap<String, u64>>> =
47 LazyLock::new(|| Mutex::new(HashMap::new()));
48
49pub fn hash_source(source: &str) -> u64 {
51 let digest = blake3::hash(source.as_bytes());
52 let mut bytes = [0u8; 8];
53 bytes.copy_from_slice(&digest.as_bytes()[..8]);
54 u64::from_le_bytes(bytes)
55}
56
57fn perf_file_path() -> PathBuf {
58 std::env::temp_dir().join("run-perf-counters.csv")
59}
60
61fn read_perf_file() -> HashMap<String, u64> {
62 let path = perf_file_path();
63 let Ok(text) = std::fs::read_to_string(path) else {
64 return HashMap::new();
65 };
66 let mut map = HashMap::new();
67 for line in text.lines() {
68 if let Some((key, value)) = line.split_once(',')
69 && let Ok(parsed) = value.parse::<u64>()
70 {
71 map.insert(key.to_string(), parsed);
72 }
73 }
74 map
75}
76
77fn write_perf_file(map: &HashMap<String, u64>) {
78 let mut rows = map.iter().collect::<Vec<_>>();
79 rows.sort_by(|a, b| a.0.cmp(b.0));
80 let mut buf = String::new();
81 for (key, value) in rows {
82 buf.push_str(key);
83 buf.push(',');
84 buf.push_str(&value.to_string());
85 buf.push('\n');
86 }
87 let _ = std::fs::write(perf_file_path(), buf);
88}
89
90pub fn cache_lookup(namespace: &str, source_hash: u64) -> Option<PathBuf> {
93 crate::cache::lookup(namespace, source_hash)
94}
95
96pub fn cache_store(namespace: &str, source_hash: u64, binary: &Path) -> Option<PathBuf> {
98 crate::cache::store(namespace, source_hash, binary)
99}
100
101pub fn try_cached_execution(namespace: &str, source_hash: u64) -> Option<std::process::Output> {
103 let cached = cache_lookup(namespace, source_hash)?;
104 let mut cmd = std::process::Command::new(&cached);
105 cmd.stdout(std::process::Stdio::piped())
106 .stderr(std::process::Stdio::piped())
107 .stdin(std::process::Stdio::inherit());
108 cmd.output().ok()
109}
110
111pub fn compiler_command(compiler: &Path) -> Command {
119 let mode = std::env::var("RUN_COMPILER_DAEMON")
120 .unwrap_or_else(|_| "adaptive".to_string())
121 .to_ascii_lowercase();
122 if mode == "off" {
123 perf_record("global", "compiler.raw");
124 return Command::new(compiler);
125 }
126
127 let want_sccache = mode == "sccache" || mode == "auto" || mode == "adaptive";
128 if want_sccache && let Ok(sccache) = which::which("sccache") {
129 if mode == "adaptive" && !SCCACHE_READY.load(Ordering::Relaxed) {
130 let _ = SCCACHE_INIT.get_or_init(|| {
131 let sccache_clone = sccache.clone();
132 std::thread::spawn(move || {
133 let ready = std::process::Command::new(&sccache_clone)
134 .arg("--start-server")
135 .stdout(Stdio::null())
136 .stderr(Stdio::null())
137 .status()
138 .is_ok_and(|s| s.success());
139 if ready {
140 SCCACHE_READY.store(true, Ordering::Relaxed);
141 }
142 });
143 });
144 perf_record("global", "compiler.raw.adaptive_warmup");
145 return Command::new(compiler);
146 }
147 let _ = SCCACHE_INIT.get_or_init(|| {
148 let ready = std::process::Command::new(&sccache)
149 .arg("--start-server")
150 .stdout(Stdio::null())
151 .stderr(Stdio::null())
152 .status()
153 .is_ok_and(|s| s.success());
154 if ready {
155 SCCACHE_READY.store(true, Ordering::Relaxed);
156 }
157 });
158 let mut cmd = Command::new(sccache);
159 cmd.arg(compiler);
160 perf_record("global", "compiler.sccache");
161 return cmd;
162 }
163
164 let want_ccache = mode == "ccache" || mode == "auto" || mode == "adaptive";
165 if want_ccache && let Ok(ccache) = which::which("ccache") {
166 let mut cmd = Command::new(ccache);
167 cmd.arg(compiler);
168 perf_record("global", "compiler.ccache");
169 return cmd;
170 }
171
172 perf_record("global", "compiler.raw.fallback");
173 Command::new(compiler)
174}
175
176pub fn perf_record(language: &str, event: &str) {
177 let key = format!("{language}.{event}");
178 if let Ok(mut counters) = PERF_COUNTERS.lock() {
179 let entry = counters.entry(key).or_insert(0);
180 *entry += 1;
181 let mut disk = read_perf_file();
182 let disk_entry = disk.entry(language.to_string() + "." + event).or_insert(0);
183 *disk_entry += 1;
184 write_perf_file(&disk);
185 }
186}
187
188pub fn perf_snapshot() -> Vec<(String, u64)> {
189 let disk = read_perf_file();
190 let mut rows = if !disk.is_empty() {
191 disk.into_iter().collect::<Vec<_>>()
192 } else if let Ok(counters) = PERF_COUNTERS.lock() {
193 counters
194 .iter()
195 .map(|(k, v)| (k.clone(), *v))
196 .collect::<Vec<_>>()
197 } else {
198 Vec::new()
199 };
200 rows.sort_by(|a, b| a.0.cmp(&b.0));
201 rows
202}
203
204pub fn perf_reset() {
205 if let Ok(mut counters) = PERF_COUNTERS.lock() {
206 counters.clear();
207 }
208 let _ = std::fs::remove_file(perf_file_path());
209}
210
211pub fn execution_timeout() -> Duration {
215 let secs = crate::runtime::timeout_secs();
216 Duration::from_secs(secs)
217}
218
219pub fn wait_with_timeout(mut child: Child, timeout: Duration) -> Result<std::process::Output> {
222 let start = Instant::now();
223 let poll_interval = Duration::from_millis(50);
224
225 loop {
226 match child.try_wait() {
227 Ok(Some(_status)) => {
228 return child.wait_with_output().map_err(Into::into);
229 }
230 Ok(None) => {
231 if timeout.as_secs() > 0 && start.elapsed() > timeout {
232 let _ = child.kill();
233 let _ = child.wait(); bail!("Execution timed out after {}s", timeout.as_secs());
235 }
236 std::thread::sleep(poll_interval);
237 }
238 Err(e) => {
239 return Err(e.into());
240 }
241 }
242 }
243}
244
245pub use bash::BashEngine;
246pub use c::CEngine;
247pub use cpp::CppEngine;
248pub use crystal::CrystalEngine;
249pub use csharp::CSharpEngine;
250pub use dart::DartEngine;
251pub use elixir::ElixirEngine;
252pub use go::GoEngine;
253pub use groovy::GroovyEngine;
254pub use haskell::HaskellEngine;
255pub use java::JavaEngine;
256pub use javascript::JavascriptEngine;
257pub use julia::JuliaEngine;
258pub use kotlin::KotlinEngine;
259pub use lua::LuaEngine;
260pub use nim::NimEngine;
261pub use perl::PerlEngine;
262pub use php::PhpEngine;
263pub use python::PythonEngine;
264pub use r::REngine;
265pub use ruby::RubyEngine;
266pub use rust::RustEngine;
267pub use swift::SwiftEngine;
268pub use typescript::TypeScriptEngine;
269pub use zig::ZigEngine;
270
271pub trait LanguageSession {
272 fn language_id(&self) -> &str;
273 fn eval(&mut self, code: &str) -> Result<ExecutionOutcome>;
274 fn shutdown(&mut self) -> Result<()>;
275}
276
277pub trait LanguageEngine {
278 fn id(&self) -> &'static str;
279 fn display_name(&self) -> &'static str {
280 self.id()
281 }
282 fn aliases(&self) -> &[&'static str] {
283 &[]
284 }
285 fn supports_sessions(&self) -> bool {
286 false
287 }
288 fn validate(&self) -> Result<()> {
289 Ok(())
290 }
291 fn toolchain_version(&self) -> Result<Option<String>> {
292 Ok(None)
293 }
294 fn execute(&self, payload: &ExecutionPayload) -> Result<ExecutionOutcome>;
295 fn start_session(&self) -> Result<Box<dyn LanguageSession>> {
296 bail!("{} does not support interactive sessions yet", self.id())
297 }
298}
299
300pub(crate) fn version_line_from_output(output: &Output) -> Option<String> {
301 let stdout = String::from_utf8_lossy(&output.stdout);
302 for line in stdout.lines() {
303 let trimmed = line.trim();
304 if !trimmed.is_empty() {
305 return Some(trimmed.to_string());
306 }
307 }
308 let stderr = String::from_utf8_lossy(&output.stderr);
309 for line in stderr.lines() {
310 let trimmed = line.trim();
311 if !trimmed.is_empty() {
312 return Some(trimmed.to_string());
313 }
314 }
315 None
316}
317
318pub(crate) fn run_version_command(mut cmd: Command, context: &str) -> Result<Option<String>> {
319 cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
320 let output = cmd
321 .output()
322 .with_context(|| format!("failed to invoke {context}"))?;
323 let version = version_line_from_output(&output);
324 if output.status.success() || version.is_some() {
325 Ok(version)
326 } else {
327 bail!("{context} exited with status {}", output.status);
328 }
329}
330
331#[derive(Debug, Clone, PartialEq, Eq)]
332pub enum ExecutionPayload {
333 Inline {
334 code: String,
335 args: Vec<String>,
336 },
337 File {
338 path: std::path::PathBuf,
339 args: Vec<String>,
340 },
341 Stdin {
342 code: String,
343 args: Vec<String>,
344 },
345}
346
347impl ExecutionPayload {
348 pub fn from_input_source(source: &InputSource, args: &[String]) -> Result<Self> {
349 let args = args.to_vec();
350 match source {
351 InputSource::Inline(code) => Ok(Self::Inline {
352 code: normalize_inline_code(code).into_owned(),
353 args,
354 }),
355 InputSource::File(path) => Ok(Self::File {
356 path: path.clone(),
357 args,
358 }),
359 InputSource::Stdin => {
360 use std::io::Read;
361 let mut buffer = String::new();
362 std::io::stdin().read_to_string(&mut buffer)?;
363 Ok(Self::Stdin { code: buffer, args })
364 }
365 }
366 }
367
368 pub fn as_inline(&self) -> Option<&str> {
369 match self {
370 ExecutionPayload::Inline { code, .. } => Some(code.as_str()),
371 ExecutionPayload::Stdin { code, .. } => Some(code.as_str()),
372 ExecutionPayload::File { .. } => None,
373 }
374 }
375
376 pub fn as_file_path(&self) -> Option<&Path> {
377 match self {
378 ExecutionPayload::File { path, .. } => Some(path.as_path()),
379 _ => None,
380 }
381 }
382
383 pub fn args(&self) -> &[String] {
384 match self {
385 ExecutionPayload::Inline { args, .. } => args.as_slice(),
386 ExecutionPayload::File { args, .. } => args.as_slice(),
387 ExecutionPayload::Stdin { args, .. } => args.as_slice(),
388 }
389 }
390}
391
392fn normalize_inline_code(code: &str) -> Cow<'_, str> {
393 if !code.contains('\\') {
394 return Cow::Borrowed(code);
395 }
396
397 let mut result = String::with_capacity(code.len());
398 let mut chars = code.chars().peekable();
399 let mut in_single = false;
400 let mut in_double = false;
401 let mut escape_in_quote = false;
402
403 while let Some(ch) = chars.next() {
404 if in_single {
405 result.push(ch);
406 if escape_in_quote {
407 escape_in_quote = false;
408 } else if ch == '\\' {
409 escape_in_quote = true;
410 } else if ch == '\'' {
411 in_single = false;
412 }
413 continue;
414 }
415
416 if in_double {
417 result.push(ch);
418 if escape_in_quote {
419 escape_in_quote = false;
420 } else if ch == '\\' {
421 escape_in_quote = true;
422 } else if ch == '"' {
423 in_double = false;
424 }
425 continue;
426 }
427
428 match ch {
429 '\'' => {
430 in_single = true;
431 result.push(ch);
432 }
433 '"' => {
434 in_double = true;
435 result.push(ch);
436 }
437 '\\' => match chars.next() {
438 Some('n') => result.push('\n'),
439 Some('r') => result.push('\r'),
440 Some('t') => result.push('\t'),
441 Some('\\') => result.push('\\'),
442 Some(other) => {
443 result.push('\\');
444 result.push(other);
445 }
446 None => result.push('\\'),
447 },
448 _ => result.push(ch),
449 }
450 }
451
452 Cow::Owned(result)
453}
454
455#[derive(Debug, Clone, PartialEq, Eq)]
456pub struct ExecutionOutcome {
457 pub language: String,
458 pub exit_code: Option<i32>,
459 pub stdout: String,
460 pub stderr: String,
461 pub duration: Duration,
462}
463
464impl ExecutionOutcome {
465 pub fn success(&self) -> bool {
466 match self.exit_code {
467 Some(code) => code == 0,
468 None => self.stderr.trim().is_empty(),
469 }
470 }
471}
472
473pub struct LanguageRegistry {
474 engines: HashMap<String, Box<dyn LanguageEngine + Send + Sync>>, alias_lookup: HashMap<String, String>,
476}
477
478impl LanguageRegistry {
479 pub fn bootstrap() -> Self {
480 let mut registry = Self {
481 engines: HashMap::new(),
482 alias_lookup: HashMap::new(),
483 };
484
485 registry.register_language(PythonEngine::new());
486 registry.register_language(BashEngine::new());
487 registry.register_language(JavascriptEngine::new());
488 registry.register_language(RubyEngine::new());
489 registry.register_language(RustEngine::new());
490 registry.register_language(GoEngine::new());
491 registry.register_language(CSharpEngine::new());
492 registry.register_language(TypeScriptEngine::new());
493 registry.register_language(LuaEngine::new());
494 registry.register_language(JavaEngine::new());
495 registry.register_language(GroovyEngine::new());
496 registry.register_language(PhpEngine::new());
497 registry.register_language(KotlinEngine::new());
498 registry.register_language(CEngine::new());
499 registry.register_language(CppEngine::new());
500 registry.register_language(REngine::new());
501 registry.register_language(DartEngine::new());
502 registry.register_language(SwiftEngine::new());
503 registry.register_language(PerlEngine::new());
504 registry.register_language(JuliaEngine::new());
505 registry.register_language(HaskellEngine::new());
506 registry.register_language(ElixirEngine::new());
507 registry.register_language(CrystalEngine::new());
508 registry.register_language(ZigEngine::new());
509 registry.register_language(NimEngine::new());
510
511 registry
512 }
513
514 pub fn register_language<E>(&mut self, engine: E)
515 where
516 E: LanguageEngine + Send + Sync + 'static,
517 {
518 let id = engine.id().to_string();
519 for alias in engine.aliases() {
520 self.alias_lookup
521 .insert(canonical_language_id(alias), id.clone());
522 }
523 self.alias_lookup
524 .insert(canonical_language_id(&id), id.clone());
525 self.engines.insert(id, Box::new(engine));
526 }
527
528 pub fn resolve(&self, spec: &LanguageSpec) -> Option<&(dyn LanguageEngine + Send + Sync)> {
529 let canonical = canonical_language_id(spec.canonical_id());
530 let target_id = self
531 .alias_lookup
532 .get(&canonical)
533 .cloned()
534 .unwrap_or(canonical);
535 self.engines
536 .get(&target_id)
537 .map(|engine| engine.as_ref() as _)
538 }
539
540 pub fn resolve_by_id(&self, id: &str) -> Option<&(dyn LanguageEngine + Send + Sync)> {
541 let canonical = canonical_language_id(id);
542 let target_id = self
543 .alias_lookup
544 .get(&canonical)
545 .cloned()
546 .unwrap_or(canonical);
547 self.engines
548 .get(&target_id)
549 .map(|engine| engine.as_ref() as _)
550 }
551
552 pub fn engines(&self) -> impl Iterator<Item = &(dyn LanguageEngine + Send + Sync)> {
553 self.engines.values().map(|engine| engine.as_ref() as _)
554 }
555
556 pub fn known_languages(&self) -> Vec<String> {
557 let mut ids: Vec<_> = self.engines.keys().cloned().collect();
558 ids.sort();
559 ids
560 }
561}
562
563pub fn package_install_command(
566 language_id: &str,
567) -> Option<(&'static str, &'static [&'static str])> {
568 match language_id {
569 "python" => Some(("pip", &["install"])),
570 "javascript" | "typescript" => Some(("npm", &["install"])),
571 "rust" => Some(("cargo", &["add"])),
572 "go" => Some(("go", &["get"])),
573 "ruby" => Some(("gem", &["install"])),
574 "php" => Some(("composer", &["require"])),
575 "lua" => Some(("luarocks", &["install"])),
576 "dart" => Some(("dart", &["pub", "add"])),
577 "perl" => Some(("cpanm", &[])),
578 "julia" => Some(("julia", &["-e"])), "haskell" => Some(("cabal", &["install"])),
580 "nim" => Some(("nimble", &["install"])),
581 "r" => Some(("Rscript", &["-e"])), "kotlin" => None, "java" => None, "c" | "cpp" => None, "bash" => None,
586 "swift" => None,
587 "crystal" => Some(("shards", &["install"])),
588 "elixir" => None, "groovy" => None,
590 "csharp" => Some(("dotnet", &["add", "package"])),
591 "zig" => None,
592 _ => None,
593 }
594}
595
596fn install_override_command(language_id: &str, package: &str) -> Option<std::process::Command> {
597 let key = format!("RUN_INSTALL_COMMAND_{}", language_id.to_ascii_uppercase());
598 let template = std::env::var(&key).ok()?;
599 let expanded = if template.contains("{package}") {
600 template.replace("{package}", package)
601 } else {
602 format!("{template} {package}")
603 };
604 let parts = shell_words::split(&expanded).ok()?;
605 if parts.is_empty() {
606 return None;
607 }
608 let mut cmd = std::process::Command::new(&parts[0]);
609 for arg in &parts[1..] {
610 cmd.arg(arg);
611 }
612 Some(cmd)
613}
614
615pub fn build_install_command(language_id: &str, package: &str) -> Option<std::process::Command> {
618 if let Some(cmd) = install_override_command(language_id, package) {
619 return Some(cmd);
620 }
621
622 if language_id == "python" {
623 let python = python::resolve_python_binary();
624 let mut cmd = std::process::Command::new(python);
625 cmd.arg("-m").arg("pip").arg("install").arg(package);
626 return Some(cmd);
627 }
628
629 let (binary, base_args) = package_install_command(language_id)?;
630
631 let mut cmd = std::process::Command::new(binary);
632
633 match language_id {
634 "julia" => {
635 cmd.arg("-e")
637 .arg(format!("using Pkg; Pkg.add(\"{package}\")"));
638 }
639 "r" => {
640 cmd.arg("-e").arg(format!(
642 "install.packages(\"{package}\", repos=\"https://cran.r-project.org\")"
643 ));
644 }
645 _ => {
646 for arg in base_args {
647 cmd.arg(arg);
648 }
649 cmd.arg(package);
650 }
651 }
652
653 Some(cmd)
654}
655
656pub fn default_language() -> &'static str {
657 "python"
658}
659
660pub fn ensure_known_language(spec: &LanguageSpec, registry: &LanguageRegistry) -> Result<()> {
661 if registry.resolve(spec).is_some() {
662 return Ok(());
663 }
664
665 let available = registry.known_languages();
666 bail!(
667 "Unknown language '{}'. Available languages: {}",
668 spec.canonical_id(),
669 available.join(", ")
670 )
671}
672
673pub fn detect_language_for_source(
674 source: &ExecutionPayload,
675 registry: &LanguageRegistry,
676) -> Option<LanguageSpec> {
677 if let Some(path) = source.as_file_path()
678 && let Some(ext) = path.extension().and_then(|e| e.to_str())
679 {
680 let ext_lower = ext.to_ascii_lowercase();
681 if let Some(lang) = extension_to_language(&ext_lower) {
682 let spec = LanguageSpec::new(lang);
683 if registry.resolve(&spec).is_some() {
684 return Some(spec);
685 }
686 }
687 }
688
689 if let Some(code) = source.as_inline()
690 && let Some(lang) = crate::detect::detect_language_from_snippet(code)
691 {
692 let spec = LanguageSpec::new(lang);
693 if registry.resolve(&spec).is_some() {
694 return Some(spec);
695 }
696 }
697
698 None
699}
700
701fn extension_to_language(ext: &str) -> Option<&'static str> {
702 match ext {
703 "py" | "pyw" => Some("python"),
704 "rs" => Some("rust"),
705 "go" => Some("go"),
706 "cs" => Some("csharp"),
707 "ts" | "tsx" => Some("typescript"),
708 "js" | "mjs" | "cjs" | "jsx" => Some("javascript"),
709 "rb" => Some("ruby"),
710 "lua" => Some("lua"),
711 "java" => Some("java"),
712 "groovy" => Some("groovy"),
713 "php" => Some("php"),
714 "kt" | "kts" => Some("kotlin"),
715 "c" => Some("c"),
716 "cpp" | "cc" | "cxx" | "hpp" | "hxx" => Some("cpp"),
717 "sh" | "bash" | "zsh" => Some("bash"),
718 "r" => Some("r"),
719 "dart" => Some("dart"),
720 "swift" => Some("swift"),
721 "perl" | "pl" | "pm" => Some("perl"),
722 "julia" | "jl" => Some("julia"),
723 "hs" => Some("haskell"),
724 "ex" | "exs" => Some("elixir"),
725 "cr" => Some("crystal"),
726 "zig" => Some("zig"),
727 "nim" => Some("nim"),
728 _ => None,
729 }
730}