Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

Kotlin/JVM: Gradle apps with sources in src/main/kotlin (#63)

chessMan08be2441c4b8
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b609902..9fb711a 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -161,6 +161,20 @@ jobs:
$fa bench --runs 2
$fa disable
git diff --exit-code src
+ - name: end to end, Kotlin with Gradle
+ working-directory: examples/fuzzy-dedupe-kt
+ run: |
+ fa=../../target/release/fast-anything
+ $fa scan -- gradle -q run
+ $fa port
+ cp reference/port.rs .fast/ports/$(ls .fast/ports)/src/port.rs
+ $fa verify
+ $fa enable
+ gradle -q --no-daemon run
+ $fa bench --runs 2
+ $fa disable
+ git diff --exit-code src
+ test -z "$(git status --porcelain .)"
- name: end to end, C#
working-directory: examples/fuzzy-dedupe-cs
run: |
diff --git a/README.md b/README.md
index 02339b2..9bd7089 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@
<a href="https://www.npmjs.com/package/@el4cteo/fast-anything"><img src="https://img.shields.io/npm/v/@el4cteo/fast-anything?color=f74c00&label=npm" alt="npm"></a>
<a href="https://crates.io/crates/fast-anything"><img src="https://img.shields.io/crates/v/fast-anything?color=f74c00" alt="crates.io"></a>
<a href="https://github.com/EL4CTEO/fast-anything/actions/workflows/ci.yml"><img src="https://github.com/EL4CTEO/fast-anything/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
- <img src="https://img.shields.io/badge/Python%20%C2%B7%20Node%20%C2%B7%20TypeScript%20%C2%B7%20Ruby%20%C2%B7%20Java%20%C2%B7%20Go%20%C2%B7%20PHP%20%C2%B7%20C%20%C2%B7%20C%2B%2B%20%C2%B7%20C%23-2563eb" alt="Python, Node, TypeScript, Ruby, Java, Go, PHP, C, C++, C#">
+ <img src="https://img.shields.io/badge/Python%20%C2%B7%20Node%20%C2%B7%20TypeScript%20%C2%B7%20Ruby%20%C2%B7%20Java%20%C2%B7%20Kotlin%20%C2%B7%20Go%20%C2%B7%20PHP%20%C2%B7%20C%20%C2%B7%20C%2B%2B%20%C2%B7%20C%23-2563eb" alt="Python, Node, TypeScript, Ruby, Java, Kotlin, Go, PHP, C, C++, C#">
<img src="https://img.shields.io/badge/Claude%20Code-plugin-d97757" alt="Claude Code plugin">
<img src="https://img.shields.io/badge/MCP-server-555" alt="MCP server">
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-555" alt="MIT"></a>
@@ -96,6 +96,7 @@ fast-anything ports the outermost loop it can, not the tiny function called insi
| JavaScript / TypeScript (Node 22+, Bun, Deno 2+: `bun app.ts`, `deno run -A app.ts`) | napi-rs | ✅ |
| Ruby 3.3+ | C ABI + JSON, through Ruby's built-in Fiddle | ✅ |
| Java 22+ (`java App.java`, Maven, Gradle) | the same C bridge, through the FFM API | ✅ |
+| Kotlin/JVM (Gradle, `src/main/kotlin`, JDK 22+) | the Java bridge; top-level functions, `object` and companion members, extension functions | ✅ |
| Go 1.22+ (`go run .`) | the same C bridge, through the Windows loader or dlopen (cgo) | ✅ |
| PHP 8.1+ (`php app.php`) | the same C bridge, through PHP's FFI extension (`extension=ffi`) | ✅ |
| C and C++ (gcc, clang or Intel oneAPI icx/icpx: `clang -O2 main.c lib.c`) | the same C bridge, through LoadLibrary or dlopen | ✅ |
diff --git a/examples/fuzzy-dedupe-kt/.gitignore b/examples/fuzzy-dedupe-kt/.gitignore
new file mode 100644
index 0000000..ac0e82c
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/.gitignore
@@ -0,0 +1,3 @@
+.gradle/
+.kotlin/
+build/
diff --git a/examples/fuzzy-dedupe-kt/build.gradle.kts b/examples/fuzzy-dedupe-kt/build.gradle.kts
new file mode 100644
index 0000000..db31648
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/build.gradle.kts
@@ -0,0 +1,16 @@
+plugins {
+ kotlin("jvm") version "2.4.21"
+ application
+}
+
+repositories {
+ mavenCentral()
+}
+
+kotlin {
+ jvmToolchain(25)
+}
+
+application {
+ mainClass = "demo.AppKt"
+}
diff --git a/examples/fuzzy-dedupe-kt/reference/port.rs b/examples/fuzzy-dedupe-kt/reference/port.rs
new file mode 100644
index 0000000..01169cb
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/reference/port.rs
@@ -0,0 +1,75 @@
+//! Reference port of findDuplicates (with levenshtein and normalize inside it): what a good
+//! agent run produces. CI uses it to test the pipeline without an agent.
+#![allow(non_snake_case)]
+
+/// Kotlin's name.lowercase().split(' ', '\t', '\n', '\r').filter { it.isNotEmpty() }.joinToString(" "),
+/// as UTF-16 units (Kotlin strings index by them).
+fn normalize(name: &str) -> Vec<u16> {
+ let lower = name.to_lowercase();
+ let mut out = Vec::with_capacity(lower.len());
+ for word in lower.split([' ', '\t', '\n', '\r']).filter(|w| !w.is_empty()) {
+ if !out.is_empty() {
+ out.push(u16::from(b' '));
+ }
+ out.extend(word.encode_utf16());
+ }
+ out
+}
+
+pub fn findDuplicates(names: Vec<String>, maxDistance: i32) -> Result<Vec<Vec<i32>>, String> {
+ if maxDistance < 0 {
+ return Ok(Vec::new());
+ }
+ let clean: Vec<Vec<u16>> = names.iter().map(|n| normalize(n)).collect();
+ Ok(pairs(&clean, maxDistance as usize).into_iter().map(|(i, j)| vec![i as i32, j as i32]).collect())
+}
+
+/// Edit distance, one row on the shorter side, common prefix and suffix skipped.
+fn distance<T: PartialEq>(a: &[T], b: &[T], row: &mut Vec<usize>) -> usize {
+ let prefix = a.iter().zip(b).take_while(|(x, y)| x == y).count();
+ let (a, b) = (&a[prefix..], &b[prefix..]);
+ let suffix = a.iter().rev().zip(b.iter().rev()).take_while(|(x, y)| x == y).count();
+ let (a, b) = (&a[..a.len() - suffix], &b[..b.len() - suffix]);
+ let (long, short) = if a.len() >= b.len() { (a, b) } else { (b, a) };
+ if short.is_empty() {
+ return long.len();
+ }
+ row.clear();
+ row.extend(0..=short.len());
+ for (i, ca) in long.iter().enumerate() {
+ let mut diagonal = row[0];
+ row[0] = i + 1;
+ for (j, cb) in short.iter().enumerate() {
+ let above = row[j + 1];
+ row[j + 1] = (above + 1).min(row[j] + 1).min(diagonal + usize::from(ca != cb));
+ diagonal = above;
+ }
+ }
+ row[short.len()]
+}
+
+/// All (i, j) with i < j and distance <= max, in the original loop order, spread over threads.
+fn pairs<T: PartialEq + Sync>(clean: &[Vec<T>], max: usize) -> Vec<(usize, usize)> {
+ let threads = std::thread::available_parallelism().map_or(1, |n| n.get()).min(clean.len().max(1));
+ let mut found: Vec<(usize, usize)> = std::thread::scope(|s| {
+ let workers: Vec<_> = (0..threads)
+ .map(|t| {
+ s.spawn(move || {
+ let mut row = Vec::new();
+ let mut out = Vec::new();
+ for i in (t..clean.len()).step_by(threads) {
+ for j in i + 1..clean.len() {
+ if clean[i].len().abs_diff(clean[j].len()) <= max && distance(&clean[i], &clean[j], &mut row) <= max {
+ out.push((i, j));
+ }
+ }
+ }
+ out
+ })
+ })
+ .collect();
+ workers.into_iter().flat_map(|w| w.join().expect("worker panicked")).collect()
+ });
+ found.sort_unstable();
+ found
+}
diff --git a/examples/fuzzy-dedupe-kt/settings.gradle.kts b/examples/fuzzy-dedupe-kt/settings.gradle.kts
new file mode 100644
index 0000000..e409d55
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/settings.gradle.kts
@@ -0,0 +1 @@
+rootProject.name = "fuzzy-dedupe-kt"
diff --git a/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/App.kt b/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/App.kt
new file mode 100644
index 0000000..3b28fbc
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/App.kt
@@ -0,0 +1,28 @@
+package demo
+
+// Find near-duplicate customer names, the slow way.
+// gradle -q run
+
+val FIRST = listOf("Maria", "Giulia", "Sofia", "Luca", "Marco", "Matteo", "José", "Zoë", "Łukasz", "Aiko",
+ "Chen", "Olga", "Ahmed", "Fatima", "Liam", "Emma", "Noah", "Ava", "Mateo", "Ingrid")
+val LAST = listOf("Rossi", "Russo", "Ferrari", "Esposito", "Bianchi", "García", "Müller", "Nowak", "Tanaka", "Wang",
+ "Ivanova", "Haddad", "Smith", "Johnson", "Brown", "Silva", "Kowalski", "Novák", "Dubois", "Larsen")
+
+fun makeNames(count: Int, seed: Long = 7): List<String> {
+ val rng = java.util.Random(seed)
+ return List(count) {
+ var name = "${FIRST[rng.nextInt(FIRST.size)]} ${LAST[rng.nextInt(LAST.size)]}"
+ if (rng.nextDouble() < 0.35) {
+ val i = rng.nextInt(name.length)
+ name = name.substring(0, i) + "aeiourstln"[rng.nextInt(10)] + name.substring(i + 1)
+ }
+ name
+ }
+}
+
+fun main() {
+ val names = makeNames(5000)
+ val pairs = findDuplicates(names)
+ println("${names.size} names, ${pairs.size} likely duplicate pairs")
+ pairs.filter { (i, j) -> names[i] != names[j] }.take(5).forEach { (i, j) -> println(" ${names[i]} ~ ${names[j]}") }
+}
diff --git a/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/Fuzzy.kt b/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/Fuzzy.kt
new file mode 100644
index 0000000..717a7ee
--- /dev/null
+++ b/examples/fuzzy-dedupe-kt/src/main/kotlin/demo/Fuzzy.kt
@@ -0,0 +1,32 @@
+package demo
+
+// Fuzzy string matching helpers.
+
+/** Edit distance: how many inserts, deletes or substitutions turn a into b. */
+fun levenshtein(a: String, b: String): Int {
+ val (long, short) = if (a.length >= b.length) a to b else b to a
+ var previous = IntArray(short.length + 1) { it }
+ for (i in long.indices) {
+ val current = IntArray(short.length + 1)
+ current[0] = i + 1
+ for (j in short.indices) {
+ current[j + 1] = minOf(previous[j + 1] + 1, current[j] + 1, previous[j] + if (long[i] == short[j]) 0 else 1)
+ }
+ previous = current
+ }
+ return previous[short.length]
+}
+
+fun normalize(name: String): String = name.lowercase().split(' ', '\t', '\n', '\r').filter { it.isNotEmpty() }.joinToString(" ")
+
+/** Index pairs of names within maxDistance edits of each other. */
+fun findDuplicates(names: List<String>, maxDistance: Int = 2): List<List<Int>> {
+ val clean = names.map { normalize(it) }
+ val pairs = mutableListOf<List<Int>>()
+ for (i in clean.indices) {
+ for (j in i + 1 until clean.size) {
+ if (levenshtein(clean[i], clean[j]) <= maxDistance) pairs.add(listOf(i, j))
+ }
+ }
+ return pairs
+}
diff --git a/src/backend.rs b/src/backend.rs
index 74002d4..eedac2a 100644
--- a/src/backend.rs
+++ b/src/backend.rs
@@ -4,7 +4,7 @@
use crate::hotspot::Hotspot;
use crate::state::{Ctx, Port, Verify};
use crate::util::{output, tail};
-use crate::{cfamily, csharp, golang, java, node, php, python, ruby};
+use crate::{cfamily, csharp, golang, java, kotlin, node, php, python, ruby};
use anyhow::{Context, Result, bail, ensure};
use serde::{Deserialize, Serialize};
use std::fs;
@@ -25,6 +25,7 @@ pub enum Lang {
Cpp,
#[serde(rename = "csharp")]
CSharp,
+ Kotlin,
}
#[derive(Deserialize)]
@@ -87,7 +88,10 @@ impl Lang {
/// Ports called through the C entry point, with the port in src/port.rs.
pub fn c_bridge(self) -> bool {
- matches!(self, Lang::Ruby | Lang::Java | Lang::Go | Lang::Php | Lang::C | Lang::Cpp | Lang::CSharp)
+ matches!(
+ self,
+ Lang::Ruby | Lang::Java | Lang::Go | Lang::Php | Lang::C | Lang::Cpp | Lang::CSharp | Lang::Kotlin
+ )
}
pub fn label(self) -> &'static str {
@@ -101,6 +105,7 @@ impl Lang {
Lang::C => "C",
Lang::Cpp => "C++",
Lang::CSharp => "C#",
+ Lang::Kotlin => "Kotlin",
}
}
@@ -114,6 +119,7 @@ impl Lang {
Lang::Php => php::resolve(program, root),
Lang::C | Lang::Cpp => cfamily::resolve(program, root),
Lang::CSharp => csharp::resolve(program, root),
+ Lang::Kotlin => java::resolve(program, root),
}
}
@@ -127,6 +133,7 @@ impl Lang {
Lang::Php => php::profile(ctx, exe, args),
Lang::C | Lang::Cpp => cfamily::profile(ctx, exe, args, self),
Lang::CSharp => csharp::profile(ctx, exe, args),
+ Lang::Kotlin => kotlin::profile(ctx, exe, args),
}
}
@@ -148,6 +155,7 @@ impl Lang {
Lang::Php => php::record(ctx, exe, args, hs, out, limit),
Lang::C | Lang::Cpp => cfamily::record(ctx, exe, args, hs, out, self),
Lang::CSharp => csharp::record(ctx, exe, args, hs, out, limit),
+ Lang::Kotlin => kotlin::record(ctx, exe, args, hs, out, limit),
}
}
@@ -161,6 +169,7 @@ impl Lang {
Lang::Php => php::scaffold(ctx, hs, helpers, port),
Lang::C | Lang::Cpp => cfamily::scaffold(ctx, hs, helpers, port),
Lang::CSharp => csharp::scaffold(ctx, hs, helpers, port),
+ Lang::Kotlin => kotlin::scaffold(ctx, hs, helpers, port),
}
}
@@ -174,6 +183,7 @@ impl Lang {
Lang::Php => php::verify(ctx, port),
Lang::C | Lang::Cpp => cfamily::verify(ctx, port),
Lang::CSharp => csharp::verify(ctx, port),
+ Lang::Kotlin => kotlin::verify(ctx, port),
}
}
@@ -187,6 +197,7 @@ impl Lang {
Lang::Php => php::enable(ctx, port),
Lang::C | Lang::Cpp => cfamily::enable(ctx, port),
Lang::CSharp => csharp::enable(ctx, port),
+ Lang::Kotlin => kotlin::enable(ctx, port),
}
}
@@ -200,6 +211,7 @@ impl Lang {
Lang::Php => php::disable(ctx, port),
Lang::C | Lang::Cpp => cfamily::disable(ctx, port),
Lang::CSharp => csharp::disable(ctx, port),
+ Lang::Kotlin => kotlin::disable(ctx, port),
}
}
@@ -208,7 +220,7 @@ impl Lang {
let exe = ctx.runtime()?;
match self {
Lang::Go => golang::bench_command(ctx, exe),
- Lang::Java => java::bench_command(ctx, exe),
+ Lang::Java | Lang::Kotlin => java::bench_command(ctx, exe),
Lang::C | Lang::Cpp => cfamily::bench_command(ctx, exe),
Lang::CSharp => csharp::bench_command(ctx, exe),
_ => Ok((exe.to_string(), ctx.state.command[1..].to_vec())),
@@ -229,7 +241,9 @@ impl Lang {
Lang::Python => format!("{ext}.abi3.so"),
Lang::Node => format!("{ext}.node"),
// loaded through a C FFI (Fiddle, FFM, ...): the platform's own library name
- Lang::Ruby | Lang::Java | Lang::Go | Lang::Php | Lang::C | Lang::Cpp | Lang::CSharp => built.clone(),
+ Lang::Ruby | Lang::Java | Lang::Go | Lang::Php | Lang::C | Lang::Cpp | Lang::CSharp | Lang::Kotlin => {
+ built.clone()
+ }
};
(built, dest)
}
diff --git a/src/java.rs b/src/java.rs
index 1cc4243..f8ce941 100644
--- a/src/java.rs
+++ b/src/java.rs
@@ -18,22 +18,22 @@ use std::process::{Command, Stdio};
use std::time::Instant;
const DESCRIBE: &str = include_str!("java/Describe.java");
-const JSON: &str = include_str!("java/Json.java");
-const REC: &str = include_str!("java/Rec.java");
-const BRIDGE: &str = include_str!("java/Bridge.java");
+pub(crate) const JSON: &str = include_str!("java/Json.java");
+pub(crate) const REC: &str = include_str!("java/Rec.java");
+pub(crate) const BRIDGE: &str = include_str!("java/Bridge.java");
/// java options that take the next argument as their value.
const VALUE_OPTS: &[&str] =
&["-cp", "-classpath", "--class-path", "-p", "--module-path", "--add-modules", "--source", "--limit-modules"];
/// How the app is started: a source file, or a Maven or Gradle build.
#[derive(Debug, Clone, Copy, PartialEq)]
-enum Runner {
+pub(crate) enum Runner {
Source,
Maven,
Gradle,
}
-fn runner(program: &str) -> Option<Runner> {
+pub(crate) fn runner(program: &str) -> Option<Runner> {
match util::program_stem(program).trim_end_matches(".cmd").trim_end_matches(".bat") {
"java" => Some(Runner::Source),
"mvn" | "mvnw" | "mvnd" => Some(Runner::Maven),
@@ -65,7 +65,7 @@ fn build_tool(program: &str, root: &Path) -> Result<String> {
}
/// The JDK that runs the build: JAVA_HOME, else java on the PATH.
-fn build_java(root: &Path) -> Result<String> {
+pub(crate) fn build_java(root: &Path) -> Result<String> {
match std::env::var_os("JAVA_HOME").filter(|h| !h.is_empty()) {
Some(home) => {
let java = Path::new(&home).join("bin").join(if cfg!(windows) { "java.exe" } else { "java" });
@@ -170,20 +170,24 @@ fn source_files(start: &Path) -> Vec<PathBuf> {
out
}
+/// A method the profile can name: its JVM class (as the flight recording has it) and its source.
#[derive(Deserialize, Clone)]
-struct Method {
- class: String,
- owner: String,
- name: String,
- path: PathBuf,
- line: u32,
- end_line: u32,
- params: Vec<Param>,
- returns: String,
- blockers: Vec<String>,
- warnings: Vec<String>,
- calls: Vec<String>,
- source: String,
+pub(crate) struct Method {
+ pub class: String,
+ pub owner: String,
+ pub name: String,
+ pub path: PathBuf,
+ pub line: u32,
+ pub end_line: u32,
+ pub params: Vec<Param>,
+ pub returns: String,
+ pub blockers: Vec<String>,
+ pub warnings: Vec<String>,
+ pub calls: Vec<String>,
+ pub source: String,
+ /// The id's module when it isn't the JVM class (Kotlin: demo, not demo.FuzzyKt).
+ #[serde(default)]
+ pub module: Option<String>,
}
fn describe(ctx: &Ctx, java: &str, files: &[PathBuf]) -> Result<Vec<Method>> {
@@ -211,7 +215,7 @@ fn hotspot(ctx: &Ctx, m: &Method, total: f64, wall: f64, own: u64, incl: u64) ->
end_line: m.end_line,
name: m.name.clone(),
qualname: m.name.clone(),
- module: Some(m.class.replace('$', ".")),
+ module: Some(m.module.clone().unwrap_or_else(|| m.class.replace('$', "."))),
kind: "function".into(),
tottime: own as f64 / total * wall,
cumtime: incl as f64 / total * wall,
@@ -238,7 +242,7 @@ pub fn bench_command(ctx: &Ctx, program: &str) -> Result<(String, Vec<String>)>
}
/// The app's command, ready to run: for Gradle without a lingering daemon.
-fn app_command(program: &str, args: &[String]) -> Command {
+pub(crate) fn app_command(program: &str, args: &[String]) -> Command {
let mut cmd = Command::new(program);
if runner(program) == Some(Runner::Gradle) {
cmd.arg("--no-daemon");
@@ -248,6 +252,17 @@ fn app_command(program: &str, args: &[String]) -> Command {
}
pub fn profile(ctx: &Ctx, program: &str, args: &[String]) -> Result<Profile> {
+ profile_with(ctx, program, args, |java| describe(ctx, java, &sources(&ctx.root, program, args)?))
+}
+
+/// Profile the app with Flight Recorder and rank the methods `describe` finds in its sources
+/// (given the JDK that runs the build).
+pub(crate) fn profile_with(
+ ctx: &Ctx,
+ program: &str,
+ args: &[String],
+ describe: impl FnOnce(&str) -> Result<Vec<Method>>,
+) -> Result<Profile> {
let build = runner(program) != Some(Runner::Source);
let java = if build { build_java(&ctx.root)? } else { program.to_string() };
let jfr_dir = ctx.fast.join("jfr");
@@ -270,21 +285,29 @@ pub fn profile(ctx: &Ctx, program: &str, args: &[String]) -> Result<Profile> {
let wall = started.elapsed().as_secs_f64();
ensure!(run.status.success(), "the app failed ({}):\n{}", run.status, tail(&combined(&run), 30));
let mut events = Vec::new();
+ let mut unreadable = None;
for f in fs::read_dir(&jfr_dir)?.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "jfr")) {
let printed = output(
Command::new(tool(&java, "jfr")).args(["print", "--events", "jdk.ExecutionSample", "--json"]).arg(&f),
"jfr",
)?;
- ensure!(printed.status.success(), "reading the flight recording failed:\n{}", tail(&combined(&printed), 10));
+ if !printed.status.success() {
+ // a helper JVM the build stopped before it wrote its recording (Kotlin's compiler daemon)
+ unreadable.get_or_insert_with(|| tail(&combined(&printed), 10));
+ continue;
+ }
let recording: Value = serde_json::from_slice(&printed.stdout).context("parsing the flight recording")?;
if let Some(list) = recording["recording"]["events"].as_array() {
events.extend(list.iter().cloned());
}
}
+ if let (true, Some(log)) = (events.is_empty(), unreadable) {
+ bail!("reading the flight recording failed:\n{log}");
+ }
ensure!(!events.is_empty(), "no flight recording was written: did a JVM run the app?");
let recording = serde_json::json!({ "recording": { "events": events } });
- let methods = describe(ctx, &java, &sources(&ctx.root, program, args)?)?;
+ let methods = describe(&java)?;
let mut by_name: HashMap<(String, String), Vec<usize>> = HashMap::new();
for (i, m) in methods.iter().enumerate() {
by_name.entry((m.class.clone(), m.name.clone())).or_default().push(i);
@@ -480,7 +503,7 @@ fn hotspot_of<'a>(ctx: &'a Ctx, port: &Port) -> Result<&'a Hotspot> {
}
/// The Java release a Maven or Gradle build compiles for, when it says.
-fn declared_release(root: &Path) -> Option<u32> {
+pub(crate) fn declared_release(root: &Path) -> Option<u32> {
let num = |s: &str| -> Option<u32> {
let digits: String =
s.trim_start_matches(|c: char| !c.is_ascii_digit()).chars().take_while(char::is_ascii_digit).collect();
@@ -498,6 +521,7 @@ fn declared_release(root: &Path) -> Option<u32> {
"VERSION_",
"release =",
"release.set(",
+ "jvmToolchain(",
] {
for (i, _) in text.match_indices(key) {
if let Some(n) = num(&text[i + key.len()..(i + key.len() + 12).min(text.len())]) {
diff --git a/src/kotlin.rs b/src/kotlin.rs
new file mode 100644
index 0000000..7517ba5
--- /dev/null
+++ b/src/kotlin.rs
@@ -0,0 +1,945 @@
+//! Kotlin backend (Kotlin/JVM): Gradle projects with sources in src/main/kotlin, run as
+//! gradle run. The JVM side is the Java backend's: Flight Recorder profiles the
+//! run and the same generated Java classes record calls and call the port through the FFM API;
+//! they go in src/main/java, where the build compiles them. Kotlin's own part is reading the
+//! sources (top-level functions, object and companion members, extension functions) and the
+//! Kotlin prologues that hand a call to those classes.
+
+use crate::backend::{Lang, Profile, Recording};
+use crate::hotspot::{Hotspot, Param};
+use crate::java::{self, Method, Runner};
+use crate::native::{self, Flavor, Patches};
+use crate::python::DISABLE_ENV;
+use crate::state::{Ctx, Port, Verify};
+use crate::util::{self, combined, output, tail};
+use anyhow::{Context, Result, bail, ensure};
+use std::fs;
+use std::path::{Path, PathBuf};
+use std::process::Stdio;
+
+/// A Gradle build with Kotlin sources is a Kotlin app.
+pub fn refine(lang: Lang, program: &str, root: &Path) -> Lang {
+ let build = java::runner(program) == Some(Runner::Gradle);
+ if lang == Lang::Java && build && !kotlin_files(root).is_empty() { Lang::Kotlin } else { lang }
+}
+
+fn kotlin_files(root: &Path) -> Vec<PathBuf> {
+ let mut out = Vec::new();
+ let mut stack = vec![root.to_path_buf()];
+ while let Some(dir) = stack.pop() {
+ let Ok(entries) = fs::read_dir(&dir) else { continue };
+ for e in entries.flatten() {
+ let p = e.path();
+ let name = e.file_name().to_string_lossy().into_owned();
+ if p.is_dir() {
+ if !name.starts_with('.') && !["build", "target", "out", "node_modules"].contains(&name.as_str()) {
+ stack.push(p);
+ }
+ } else if name.ends_with(".kt") && in_kotlin_sources(&p) {
+ out.push(p);
+ }
+ }
+ }
+ out.sort();
+ out
+}
+
+/// .../src/main/kotlin/...
+fn in_kotlin_sources(p: &Path) -> bool {
+ let parts: Vec<String> = p.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect();
+ parts.windows(3).any(|w| w == ["src", "main", "kotlin"])
+}
+
+// ------------------------------------------------------------------ reading Kotlin
+
+/// The source with comments, strings and char literals blanked out (same length, newlines kept),
+/// so braces, parentheses and keywords can be found by scanning.
+fn code_only(src: &str) -> String {
+ let b = src.as_bytes();
+ let mut out = b.to_vec();
+ let mut i = 0;
+ while i < b.len() {
+ let end = if b[i..].starts_with(b"//") {
+ b[i..].iter().position(|&c| c == b'\n').map_or(b.len(), |p| i + p)
+ } else if b[i..].starts_with(b"/*") {
+ let (mut depth, mut j) = (0, i);
+ while j < b.len() {
+ if b[j..].starts_with(b"/*") {
+ depth += 1;
+ j += 2;
+ } else if b[j..].starts_with(b"*/") {
+ depth -= 1;
+ j += 2;
+ if depth == 0 {
+ break;
+ }
+ } else {
+ j += 1;
+ }
+ }
+ j
+ } else if b[i] == b'"' || b[i] == b'\'' {
+ string_end(b, i)
+ } else {
+ i += 1;
+ continue;
+ };
+ for c in &mut out[i..end] {
+ if *c != b'\n' {
+ *c = b' ';
+ }
+ }
+ i = end;
+ }
+ String::from_utf8(out).unwrap_or_default()
+}
+
+/// Where the string (raw """ too) or char literal starting at i ends, ${...} templates included.
+fn string_end(b: &[u8], i: usize) -> usize {
+ if b[i] == b'\'' {
+ let mut j = i + 1;
+ while j < b.len() && b[j] != b'\'' {
+ j += if b[j] == b'\\' { 2 } else { 1 };
+ }
+ return (j + 1).min(b.len());
+ }
+ let raw = b[i..].starts_with(b"\"\"\"");
+ let mut j = i + if raw { 3 } else { 1 };
+ while j < b.len() {
+ if raw && b[j..].starts_with(b"\"\"\"") {
+ // a raw string can end with extra quotes: """a"""" is the string a"
+ let mut k = j + 3;
+ while k < b.len() && b[k] == b'"' {
+ k += 1;
+ }
+ return k;
+ }
+ if !raw && b[j] == b'"' {
+ return j + 1;
+ }
+ if !raw && b[j] == b'\\' {
+ j += 2;
+ continue;
+ }
+ if b[j..].starts_with(b"${") {
+ let mut depth = 0;
+ while j < b.len() {
+ match b[j] {
+ b'{' => depth += 1,
+ b'}' => {
+ depth -= 1;
+ if depth == 0 {
+ break;
+ }
+ }
+ b'"' | b'\'' => {
+ j = string_end(b, j);
+ continue;
+ }
+ _ => {}
+ }
+ j += 1;
+ }
+ }
+ j += 1;
+ }
+ b.len()
+}
+
+/// The index of the bracket matching the one at `open` (in code_only text).
+fn matching(code: &[u8], open: usize) -> Option<usize> {
+ let (o, c) = (
+ code[open],
+ match code[open] {
+ b'(' => b')',
+ b'{' => b'}',
+ b'<' => b'>',
+ _ => b']',
+ },
+ );
+ let mut depth = 0;
+ for (k, &ch) in code.iter().enumerate().skip(open) {
+ if ch == o {
+ depth += 1;
+ } else if ch == c {
+ depth -= 1;
+ if depth == 0 {
+ return Some(k);
+ }
+ }
+ }
+ None
+}
+
+fn is_ident(c: u8) -> bool {
+ c.is_ascii_alphanumeric() || c == b'_'
+}
+
+/// The whole word at i (no identifier characters right before or after).
+fn word_at(code: &[u8], i: usize, word: &str) -> bool {
+ code[i..].starts_with(word.as_bytes())
+ && (i == 0 || !is_ident(code[i - 1]))
+ && code.get(i + word.len()).is_none_or(|&c| !is_ident(c))
+}
+
+fn words(text: &str) -> Vec<&str> {
+ text.split(|c: char| !(c.is_alphanumeric() || c == '_' || c == '@')).filter(|w| !w.is_empty()).collect()
+}
+
+/// Split on commas outside brackets.
+fn split_top(s: &str) -> Vec<String> {
+ let (mut depth, mut cur, mut out) = (0i32, String::new(), vec![]);
+ for c in s.chars() {
+ match c {
+ '<' | '(' | '[' | '{' => depth += 1,
+ '>' | ')' | ']' | '}' => depth -= 1,
+ ',' if depth == 0 => {
+ out.push(std::mem::take(&mut cur));
+ continue;
+ }
+ _ => {}
+ }
+ cur.push(c);
+ }
+ if !cur.trim().is_empty() {
+ out.push(cur);
+ }
+ out
+}
+
+#[derive(Clone)]
+enum Scope {
+ Top,
+ /// a class, object or companion: its JVM class and how Kotlin code names it
+ Type {
+ jvm: String,
+ name: String,
+ object: bool,
+ },
+ Other,
+}
+
+const IO: &[&str] = &[
+ "println(",
+ "print(",
+ "readLine(",
+ "readln(",
+ "File(",
+ "java.io.",
+ "kotlin.io.",
+ "Files.",
+ "System.out",
+ "System.err",
+ "Paths.",
+ "URL(",
+ "Socket(",
+ "ProcessBuilder(",
+ "Runtime.getRuntime",
+];
+const NONDET: &[&str] = &[
+ "Random",
+ "random(",
+ "shuffled(",
+ "shuffle(",
+ "currentTimeMillis",
+ "nanoTime",
+ "UUID",
+ "Clock.",
+ "Instant.now",
+ "LocalDate.now",
+ "LocalDateTime.now",
+];
+const COROUTINES: &[&str] = &["launch", "async", "runBlocking", "withContext", "delay("];
+
+/// Every function fast-anything can name in one Kotlin file.
+fn describe_file(path: &Path) -> Vec<Method> {
+ let Ok(src) = fs::read_to_string(path) else { return vec![] };
+ let code = code_only(&src);
+ let c = code.as_bytes();
+ let line_of = |pos: usize| src[..pos].matches('\n').count() as u32 + 1;
+
+ let package = code
+ .lines()
+ .find_map(|l| l.trim().strip_prefix("package ").map(|p| p.trim().trim_end_matches(';').to_string()))
+ .unwrap_or_default();
+ let qualify = |name: &str| if package.is_empty() { name.to_string() } else { format!("{package}.{name}") };
+ // top-level functions live in <File>Kt, or the name @file:JvmName gives
+ let stem = path.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default();
+ let file_class = src
+ .find("@file:JvmName(\"")
+ .and_then(|i| src[i + 15..].split('"').next().map(String::from))
+ .unwrap_or_else(|| {
+ let mut chars = stem.chars();
+ chars.next().map(|f| f.to_uppercase().collect::<String>() + chars.as_str()).unwrap_or_default() + "Kt"
+ });
+
+ struct Found {
+ jvm: String,
+ module: String,
+ owner: String,
+ start: usize,
+ decl: usize,
+ name: String,
+ receiver: Option<String>,
+ params: String,
+ returns: Option<String>,
+ body: Option<(usize, usize)>,
+ expression: bool,
+ prefix: String,
+ generic: bool,
+ }
+ let mut found: Vec<Found> = Vec::new();
+ let mut stack = vec![Scope::Top];
+ let mut boundary = 0; // where the current statement's header starts
+ let mut i = 0;
+ while i < c.len() {
+ match c[i] {
+ b'{' => {
+ let header = code[boundary..i].trim();
+ let w = words(header);
+ let parent = stack.last().cloned().unwrap_or(Scope::Other);
+ let scope = if matches!(parent, Scope::Other) || w.contains(&"fun") {
+ Scope::Other
+ } else if let Some(k) = w.iter().position(|x| *x == "object") {
+ let anonymous = header.contains('=')
+ || header[header.find("object").unwrap_or(0) + 6..].trim_start().starts_with(':');
+ let companion = k > 0 && w[k - 1] == "companion";
+ let named = w
+ .get(k + 1)
+ .filter(|n| n.chars().next().is_some_and(char::is_alphabetic))
+ .map(|n| n.to_string());
+ match (&parent, anonymous, companion) {
+ (_, true, _) => Scope::Other,
+ (Scope::Type { jvm, name, .. }, _, true) => {
+ let own = named.unwrap_or_else(|| "Companion".into());
+ Scope::Type { jvm: format!("{jvm}${own}"), name: name.clone(), object: true }
+ }
+ (Scope::Top, _, false) => {
+ let own = named.unwrap_or_default();
+ Scope::Type { jvm: qualify(&own), name: own, object: true }
+ }
+ (Scope::Type { jvm, name, .. }, _, false) => {
+ let own = named.unwrap_or_default();
+ Scope::Type { jvm: format!("{jvm}${own}"), name: format!("{name}.{own}"), object: true }
+ }
+ _ => Scope::Other,
+ }
+ } else if let Some(k) = w.iter().position(|x| *x == "class" || *x == "interface") {
+ let own = w.get(k + 1).map(|s| s.to_string()).unwrap_or_default();
+ match &parent {
+ Scope::Top => Scope::Type { jvm: qualify(&own), name: own, object: false },
+ Scope::Type { jvm, name, .. } => {
+ Scope::Type { jvm: format!("{jvm}${own}"), name: format!("{name}.{own}"), object: false }
+ }
+ Scope::Other => Scope::Other,
+ }
+ } else {
+ Scope::Other
+ };
+ stack.push(scope);
+ boundary = i + 1;
+ }
+ b'}' => {
+ stack.pop();
+ boundary = i + 1;
+ }
+ b';' => boundary = i + 1,
+ b'f' if word_at(c, i, "fun") => {
+ let collect = match stack.last() {
+ Some(Scope::Top) => Some((qualify(&file_class), package.clone(), file_class.clone())),
+ Some(Scope::Type { jvm, name, object: true }) => {
+ Some((jvm.clone(), qualify(name), name.rsplit('.').next().unwrap_or(name).to_string()))
+ }
+ _ => None,
+ };
+ if let Some((jvm, module, owner)) = collect {
+ // the declaration: its line (modifiers) and the annotation lines right above it
+ let line_start = |pos: usize| code[..pos].rfind('\n').map_or(0, |p| p + 1);
+ let mut prefix_start = line_start(i).max(boundary);
+ while prefix_start > boundary {
+ let above = line_start(prefix_start - 1).max(boundary);
+ if !code[above..prefix_start].trim().starts_with('@') {
+ break;
+ }
+ prefix_start = above;
+ }
+ while prefix_start < i && c[prefix_start].is_ascii_whitespace() {
+ prefix_start += 1;
+ }
+ let mut j = i + 3;
+ while j < c.len() && c[j].is_ascii_whitespace() {
+ j += 1;
+ }
+ let generic = c.get(j) == Some(&b'<');
+ if generic {
+ j = matching(c, j).map_or(c.len(), |k| k + 1);
+ }
+ let Some(open) = c[j..].iter().position(|&ch| ch == b'(').map(|p| j + p) else {
+ i += 3;
+ continue;
+ };
+ let target = code[j..open].trim();
+ let (receiver, name) = match target.rfind('.') {
+ Some(dot)
+ if !target[..dot].contains('<')
+ || target[..dot].matches('<').count() == target[..dot].matches('>').count() =>
+ {
+ (Some(target[..dot].to_string()), target[dot + 1..].to_string())
+ }
+ _ => (None, target.to_string()),
+ };
+ let Some(close) = matching(c, open) else { break };
+ let mut k = close + 1;
+ while k < c.len() && c[k].is_ascii_whitespace() && c[k] != b'\n' {
+ k += 1;
+ }
+ let mut returns = None;
+ if c.get(k) == Some(&b':') {
+ let mut depth = 0;
+ let start = k + 1;
+ while k < c.len() {
+ match c[k] {
+ b'<' | b'(' => depth += 1,
+ b'>' | b')' => depth -= 1,
+ b'{' | b'=' | b'\n' if depth == 0 => break,
+ _ => {}
+ }
+ k += 1;
+ }
+ returns = Some(src[start..k].trim().to_string());
+ }
+ while k < c.len() && c[k].is_ascii_whitespace() {
+ k += 1;
+ }
+ let (body, expression) = match c.get(k) {
+ Some(b'{') => (matching(c, k).map(|e| (k, e)), false),
+ Some(b'=') => (None, true),
+ _ => (None, false),
+ };
+ found.push(Found {
+ jvm,
+ module,
+ owner,
+ start: prefix_start,
+ decl: i,
+ name: name.trim().to_string(),
+ receiver,
+ params: src[open + 1..close].to_string(),
+ returns,
+ body,
+ expression,
+ prefix: code[prefix_start..i].to_string(),
+ generic,
+ });
+ // skip the signature; the body's braces are scanned as usual
+ i = close + 1;
+ continue;
+ }
+ i += 3;
+ continue;
+ }
+ _ => {}
+ }
+ i += 1;
+ }
+
+ let names: Vec<String> = found.iter().map(|f| f.name.clone()).collect();
+ found
+ .into_iter()
+ .map(|f| {
+ let mut blockers = Vec::new();
+ let mut warnings = Vec::new();
+ let modifiers = words(&f.prefix);
+ if modifiers.contains(&"suspend") {
+ blockers.push("suspend function".to_string());
+ }
+ if modifiers.contains(&"inline") {
+ blockers.push(
+ "inline function (the recording and the switch to Rust call it from inside itself)".to_string(),
+ );
+ }
+ if f.generic {
+ blockers.push("generic".to_string());
+ }
+ if f.expression {
+ blockers.push("expression body: write it as { return ... } to port it".to_string());
+ } else if f.body.is_none() {
+ blockers.push("no body".to_string());
+ }
+ match f.returns.as_deref() {
+ None | Some("Unit") => blockers.push("returns nothing (Unit)".to_string()),
+ _ => {}
+ }
+ let mut params = Vec::new();
+ if let Some(r) = &f.receiver {
+ params.push(Param {
+ name: "receiver".into(),
+ annotation: Some(r.clone()),
+ default: None,
+ kwonly: false,
+ });
+ }
+ for p in split_top(&f.params) {
+ let p = p.trim();
+ if words(p).contains(&"vararg") {
+ blockers.push("takes vararg".to_string());
+ }
+ let p = p.trim_start_matches(|ch: char| ch != ':' && !ch.is_alphanumeric() && ch != '_');
+ let (decl, default) = match p.split_once('=') {
+ Some((d, v)) => (d.trim(), Some(v.trim().to_string())),
+ None => (p, None),
+ };
+ let (name, ty) = decl.split_once(':').unwrap_or((decl, ""));
+ let name = words(name).last().copied().unwrap_or_default().to_string();
+ params.push(Param { name, annotation: Some(ty.trim().to_string()), default, kwonly: false });
+ }
+ let body = f.body.map(|(b, e)| &code[b..=e]).unwrap_or_default();
+ let io: Vec<&str> = IO.iter().filter(|w| body.contains(*w)).map(|w| w.trim_end_matches('(')).collect();
+ if !io.is_empty() {
+ blockers.push(format!("does I/O: {}", io[..io.len().min(3)].join(", ")));
+ }
+ let nondet: Vec<&str> =
+ NONDET.iter().filter(|w| body.contains(*w)).map(|w| w.trim_end_matches(['(', '.'])).collect();
+ if !nondet.is_empty() {
+ blockers.push(format!("not deterministic: {}", nondet[..nondet.len().min(3)].join(", ")));
+ }
+ let b = body.as_bytes();
+ if COROUTINES.iter().any(|w| (0..b.len()).any(|k| word_at(b, k, w.trim_end_matches('(')))) {
+ blockers.push("uses coroutines".to_string());
+ }
+ let calls: Vec<String> = names
+ .iter()
+ .filter(|n| {
+ **n != f.name
+ && (0..b.len())
+ .any(|k| word_at(b, k, n) && b[k + n.len()..].trim_ascii_start().starts_with(b"("))
+ })
+ .cloned()
+ .collect();
+ if !calls.is_empty() {
+ warnings.push(format!(
+ "calls {}, they get ported together with it",
+ calls[..calls.len().min(3)].join(", ")
+ ));
+ }
+ let end = f.body.map_or_else(|| src[f.decl..].find('\n').map_or(src.len(), |p| f.decl + p), |(_, e)| e + 1);
+ Method {
+ class: f.jvm,
+ owner: f.owner,
+ name: f.name,
+ path: path.to_path_buf(),
+ line: line_of(f.decl),
+ end_line: line_of(end.saturating_sub(1)),
+ params,
+ returns: f.returns.unwrap_or_else(|| "Unit".into()),
+ blockers,
+ warnings,
+ calls,
+ source: src[f.start..end].to_string(),
+ module: Some(f.module),
+ }
+ })
+ .filter(|m| !m.name.is_empty())
+ .collect()
+}
+
+fn describe(ctx: &Ctx) -> Result<Vec<Method>> {
+ let files = kotlin_files(&ctx.root);
+ ensure!(!files.is_empty(), "no Kotlin sources (src/main/kotlin) in this project");
+ Ok(files.iter().flat_map(|f| describe_file(f)).collect())
+}
+
+pub fn profile(ctx: &Ctx, program: &str, args: &[String]) -> Result<Profile> {
+ java::profile_with(ctx, program, args, |_| describe(ctx))
+}
+
+// ------------------------------------------------------------------ editing the source
+
+/// The line where the function's body opens.
+fn find_body(name: &str, hint: u32) -> impl Fn(&str) -> Option<usize> {
+ let (spaced, dotted, gap) = (format!(" {name}("), format!(".{name}("), format!(" {name} ("));
+ move |text: &str| {
+ native::body_line(text, hint, |l| {
+ let code = l.split("//").next().unwrap_or_default();
+ code.contains("fun ") && (code.contains(&spaced) || code.contains(&dotted) || code.contains(&gap))
+ })
+ }
+}
+
+fn package_of(text: &str) -> String {
+ text.lines()
+ .find_map(|l| l.trim().strip_prefix("package ").map(|p| p.trim().trim_end_matches(';').to_string()))
+ .unwrap_or_default()
+}
+
+/// src/main/java/<package> of the module the Kotlin file is in: where the build compiles the Java
+/// classes fast-anything adds (Java files among the Kotlin sources would be seen by kotlinc but not
+/// compiled).
+fn java_dir(file: &Path, package: &str) -> Result<PathBuf> {
+ let mut module = file.parent();
+ while let Some(dir) = module {
+ if dir.ends_with("src/main/kotlin") {
+ let mut out = dir.parent().context("no src/main")?.join("java");
+ for part in package.split('.').filter(|p| !p.is_empty()) {
+ out.push(part);
+ }
+ return Ok(out);
+ }
+ module = dir.parent();
+ }
+ bail!("{} isn't under src/main/kotlin", file.display())
+}
+
+/// Create a folder; returns the ones that didn't exist, outermost first.
+fn create_dirs(dir: &Path) -> Result<Vec<PathBuf>> {
+ let mut missing = Vec::new();
+ let mut d = Some(dir);
+ while let Some(p) = d.filter(|p| !p.exists()) {
+ missing.push(p.to_path_buf());
+ d = p.parent();
+ }
+ fs::create_dir_all(dir)?;
+ missing.reverse();
+ Ok(missing)
+}
+
+/// Remove empty folders from `dir` up to src/main/java itself.
+fn prune(dir: &Path) {
+ let mut d = Some(dir);
+ while let Some(p) = d {
+ if fs::remove_dir(p).is_err() || p.ends_with("src/main/java") {
+ break;
+ }
+ d = p.parent();
+ }
+}
+
+fn args_array(hs: &Hotspot) -> String {
+ let names: Vec<&str> =
+ hs.params.iter().map(|p| if p.name == "receiver" { "this" } else { p.name.as_str() }).collect();
+ format!("arrayOf<Any?>({})", names.join(", "))
+}
+
+/// The function called again with the arguments from an Object[]: name(a[0] as T, ...), or for an
+/// extension (a[0] as R).name(...).
+fn call_with(hs: &Hotspot) -> String {
+ let cast = |i: usize, p: &Param| format!("__faA[{i}] as {}", p.annotation.as_deref().unwrap_or("Any?"));
+ match hs.params.first().filter(|p| p.name == "receiver") {
+ Some(r) => {
+ let rest: Vec<String> = hs.params.iter().enumerate().skip(1).map(|(i, p)| cast(i, p)).collect();
+ format!("({}).{}({})", cast(0, r), hs.name, rest.join(", "))
+ }
+ None => {
+ let all: Vec<String> = hs.params.iter().enumerate().map(|(i, p)| cast(i, p)).collect();
+ format!("{}({})", hs.name, all.join(", "))
+ }
+ }
+}
+
+fn ret(hs: &Hotspot) -> String {
+ hs.returns.clone().unwrap_or_else(|| "Any?".into())
+}
+
+fn record_prologue(hs: &Hotspot) -> String {
+ format!(
+ "if (!FastAnythingRec.inside) {{\n @Suppress(\"UNCHECKED_CAST\")\n val __faR = FastAnythingRec.record({args}) {{ __faA -> {call} }} as {ret}\n return __faR\n}}",
+ args = args_array(hs),
+ call = call_with(hs),
+ ret = ret(hs),
+ )
+}
+
+pub fn record(ctx: &Ctx, program: &str, args: &[String], hs: &Hotspot, out: &Path, limit: usize) -> Result<Recording> {
+ let file = ctx.root.join(&hs.file);
+ let text = fs::read_to_string(&file).with_context(|| format!("reading {}", hs.file))?;
+ let package = package_of(&text);
+ let dir = java_dir(&file, &package)?;
+ let package_line = if package.is_empty() { String::new() } else { format!("package {package};") };
+ let rec = java::REC.replace("/*PACKAGE*/", &package_line).replace("/*ID*/", &hs.id).replace("/*JSON*/", java::JSON);
+ let patched = native::insert_in_body(
+ &text,
+ "//",
+ &format!("recording {}", hs.id),
+ &record_prologue(hs),
+ find_body(&hs.name, hs.line),
+ )?;
+ let meta = out.with_extension("meta.json");
+ let created = create_dirs(&dir)?;
+ let mut patches = Patches::new(ctx)?;
+ patches.write(&dir.join("FastAnythingRec.java"), &rec)?;
+ patches.write(&file, &patched)?;
+ let run = output(
+ java::app_command(program, args)
+ .current_dir(&ctx.root)
+ .env("FAST_ANYTHING_RECORD_OUT", out)
+ .env("FAST_ANYTHING_RECORD_META", &meta)
+ .env("FAST_ANYTHING_RECORD_LIMIT", limit.to_string())
+ .env(DISABLE_ENV, "1")
+ .stdin(Stdio::null()),
+ program,
+ );
+ patches.restore()?;
+ for d in created.iter().rev() {
+ let _ = fs::remove_dir(d);
+ }
+ let run = run?;
+ ensure!(run.status.success(), "the app failed while recording ({}):\n{}", run.status, tail(&combined(&run), 30));
+ serde_json::from_str(&fs::read_to_string(&meta).context("the recorder wrote nothing")?)
+ .context("reading the recording summary")
+}
+
+// ------------------------------------------------------------------ the crate
+
+/// Rust type for a Kotlin type.
+pub fn rust_type(t: &str) -> Option<String> {
+ let t: String = t.chars().filter(|c| !c.is_whitespace()).collect();
+ let t = t.trim_start_matches("kotlin.collections.").trim_start_matches("kotlin.");
+ if let Some(inner) = t.strip_suffix('?') {
+ return rust_type(inner).map(|r| format!("Option<{r}>"));
+ }
+ Some(match t {
+ "Int" => "i32".into(),
+ "Long" => "i64".into(),
+ "Short" => "i16".into(),
+ "Byte" => "i8".into(),
+ "Double" => "f64".into(),
+ "Float" => "f32".into(),
+ "Boolean" => "bool".into(),
+ "String" => "String".into(),
+ "IntArray" => "Vec<i32>".into(),
+ "LongArray" => "Vec<i64>".into(),
+ "ShortArray" => "Vec<i16>".into(),
+ "ByteArray" => "Vec<i8>".into(),
+ "DoubleArray" => "Vec<f64>".into(),
+ "FloatArray" => "Vec<f32>".into(),
+ "BooleanArray" => "Vec<bool>".into(),
+ _ => {
+ let (raw, inner) = t.strip_suffix('>')?.split_once('<')?;
+ match raw {
+ "List" | "MutableList" | "ArrayList" | "Collection" | "Iterable" | "Array" => {
+ format!("Vec<{}>", rust_type(inner)?)
+ }
+ "Map" | "MutableMap" | "HashMap" | "LinkedHashMap" => {
+ format!("std::collections::HashMap<String, {}>", rust_type(inner.strip_prefix("String,")?)?)
+ }
+ _ => return None,
+ }
+ }
+ })
+}
+
+/// A Kotlin type in Java syntax, for the bridge to convert the port's result back to.
+fn java_type(t: &str, boxed: bool) -> String {
+ let t: String = t.chars().filter(|c| !c.is_whitespace()).collect();
+ let t = t.trim_start_matches("kotlin.collections.").trim_start_matches("kotlin.");
+ if let Some(inner) = t.strip_suffix('?') {
+ return java_type(inner, true);
+ }
+ let primitive = |p: &str, b: &str| if boxed { b.to_string() } else { p.to_string() };
+ match t {
+ "Int" => primitive("int", "Integer"),
+ "Long" => primitive("long", "Long"),
+ "Short" => primitive("short", "Short"),
+ "Byte" => primitive("byte", "Byte"),
+ "Double" => primitive("double", "Double"),
+ "Float" => primitive("float", "Float"),
+ "Boolean" => primitive("boolean", "Boolean"),
+ "String" => "String".into(),
+ "IntArray" => "int[]".into(),
+ "LongArray" => "long[]".into(),
+ "ShortArray" => "short[]".into(),
+ "ByteArray" => "byte[]".into(),
+ "DoubleArray" => "double[]".into(),
+ "FloatArray" => "float[]".into(),
+ "BooleanArray" => "boolean[]".into(),
+ _ => match t.strip_suffix('>').and_then(|x| x.split_once('<')) {
+ Some(("List" | "MutableList" | "ArrayList" | "Collection" | "Iterable", inner)) => {
+ format!("java.util.List<{}>", java_type(inner, true))
+ }
+ Some(("Array", inner)) => format!("{}[]", java_type(inner, true)),
+ Some(("Map" | "MutableMap" | "HashMap" | "LinkedHashMap", inner)) => match split_top(inner).as_slice() {
+ [k, v] => format!("java.util.Map<{}, {}>", java_type(k, true), java_type(v, true)),
+ _ => "Object".into(),
+ },
+ _ => "Object".into(),
+ },
+ }
+}
+
+pub const FLAVOR: Flavor = Flavor {
+ lang: Lang::Kotlin,
+ fence: "kotlin",
+ rust_type,
+ default_json: |_| None,
+ rules: &[
+ "Kotlin `Int` is `i32` and wraps on overflow (as on the JVM): use `wrapping_*` where the original can overflow. `Long` is `i64`.",
+ "Kotlin Strings are UTF-16: `length`, indexes and `Char` count UTF-16 units. If the code indexes strings, work on `encode_utf16()` (or take a fast path when `is_ascii()`).",
+ "`lowercase()` / `uppercase()` don't depend on the locale; integer `/` and `%` truncate toward zero, as in Rust.",
+ "Lists and arrays arrive as JSON arrays, Maps as objects with String keys, null as null (`Option<T>` for `T?`). For an extension function, the receiver is the first argument.",
+ ],
+};
+
+pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
+ native::scaffold(ctx, hs, helpers, port, &FLAVOR)
+}
+
+pub fn verify(ctx: &Ctx, port: &Port) -> Result<Verify> {
+ native::verify(ctx, port, false)
+}
+
+// ------------------------------------------------------------------ the bridge
+
+fn bridge_class(port: &Port) -> String {
+ format!("FastAnything_{}", util::slug(&port.id))
+}
+
+fn hotspot_of<'a>(ctx: &'a Ctx, port: &Port) -> Result<&'a Hotspot> {
+ ctx.state
+ .hotspots
+ .iter()
+ .find(|h| h.id == port.id)
+ .with_context(|| format!("{} isn't in the last scan: scan again", port.id))
+}
+
+pub fn enable(ctx: &Ctx, port: &Port) -> Result<()> {
+ if let Some(release) = java::declared_release(&ctx.root).filter(|r| *r < 22) {
+ bail!(
+ "the build compiles for Java {release}; the Rust bridge uses the FFM API from Java 22. Raise the JVM toolchain to 22+"
+ );
+ }
+ let hs = hotspot_of(ctx, port)?;
+ let file = ctx.root.join(&port.file);
+ let text = fs::read_to_string(&file).with_context(|| format!("reading {}", port.file))?;
+ let package = package_of(&text);
+ let dir = java_dir(&file, &package)?;
+ let class = bridge_class(port);
+ let (_, lib) = port.lang.artifact(&port.ext);
+ let lib = util::forward_slashes(&ctx.lib_dir().join(lib));
+ let package_line = if package.is_empty() { String::new() } else { format!("package {package};") };
+ let bridge = java::BRIDGE
+ .replace("/*PACKAGE*/", &package_line)
+ .replace("/*IMPORTS*/", "")
+ .replace("/*CLASS*/", &class)
+ .replace("/*RET*/", &java_type(&ret(hs), false))
+ .replace("/*LIB*/", &serde_json::to_string(&lib)?)
+ .replace("/*ID*/", &port.id)
+ .replace("/*JSON*/", java::JSON);
+ let prologue = format!(
+ "if ({class}.ON) {{\n @Suppress(\"UNCHECKED_CAST\")\n val __faR = {class}.call({args}) as {ret}\n return __faR\n}}",
+ args = args_array(hs),
+ ret = ret(hs),
+ );
+ let patched = native::insert_in_body(&text, "//", &port.id, &prologue, find_body(&port.func, hs.line))?;
+ create_dirs(&dir)?;
+ fs::write(dir.join(format!("{class}.java")), bridge)?;
+ fs::write(&file, patched).with_context(|| format!("writing {}", port.file))
+}
+
+pub fn disable(ctx: &Ctx, port: &Port) -> Result<()> {
+ let file = ctx.root.join(&port.file);
+ let text = fs::read_to_string(&file).with_context(|| format!("reading {}", port.file))?;
+ fs::write(&file, native::strip_in_body(&text, "//", &port.id)).with_context(|| format!("writing {}", port.file))?;
+ let dir = java_dir(&file, &package_of(&text))?;
+ let bridge = dir.join(format!("{}.java", bridge_class(port)));
+ if bridge.exists() {
+ fs::remove_file(bridge)?;
+ prune(&dir);
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn describe_text(name: &str, text: &str) -> Vec<Method> {
+ let dir =
+ std::env::temp_dir().join(format!("fa-kt-{}-{name}", std::process::id())).join("src/main/kotlin/demo");
+ fs::create_dir_all(&dir).unwrap();
+ let file = dir.join(name);
+ fs::write(&file, text).unwrap();
+ let out = describe_file(&file);
+ let _ = fs::remove_dir_all(dir.ancestors().nth(4).unwrap());
+ out
+ }
+
+ #[test]
+ fn reads_kotlin_functions() {
+ let ms = describe_text(
+ "fuzzy.kt",
+ "package demo\n\n// fun commented(x: Int): Int { return x }\nval s = \"fun inString(x: Int) { ${ \"}\" }\"\n\n\
+ fun levenshtein(a: String, b: String): Int {\n return a.length + helper(b)\n}\n\n\
+ private fun helper(s: String) = s.length\n\n\
+ fun String.shout(times: Int = 2): String {\n return this.repeat(times)\n}\n\n\
+ object Stats {\n @JvmStatic\n fun mean(xs: List<Double>): Double {\n val f = fun(y: Int): Int { return y }\n return xs.sum() / xs.size\n }\n}\n\n\
+ class Box(val v: Int) {\n fun get(): Int { return v }\n companion object {\n fun make(v: Int): Box { return Box(v) }\n }\n}\n\n\
+ suspend fun later(x: Int): Int { return x }\nfun log(x: Int) { println(x) }\n",
+ );
+ let by = |n: &str| {
+ ms.iter()
+ .find(|m| m.name == n)
+ .unwrap_or_else(|| panic!("no {n}: {:?}", ms.iter().map(|m| &m.name).collect::<Vec<_>>()))
+ };
+ let names: Vec<&str> = ms.iter().map(|m| m.name.as_str()).collect();
+ assert!(!names.contains(&"commented") && !names.contains(&"inString") && !names.contains(&"get"), "{names:?}");
+ let lev = by("levenshtein");
+ assert_eq!((lev.class.as_str(), lev.module.as_deref(), lev.line), ("demo.FuzzyKt", Some("demo"), 6));
+ assert_eq!(
+ lev.params.iter().map(|p| (p.name.as_str(), p.annotation.as_deref().unwrap())).collect::<Vec<_>>(),
+ [("a", "String"), ("b", "String")]
+ );
+ assert!(lev.blockers.is_empty() && lev.calls == ["helper"], "{:?} {:?}", lev.blockers, lev.calls);
+ assert!(by("helper").blockers.iter().any(|b| b.starts_with("expression body")));
+ let shout = by("shout");
+ assert_eq!(shout.params[0].annotation.as_deref(), Some("String"));
+ assert_eq!(shout.params[1].default.as_deref(), Some("2"));
+ assert_eq!((by("mean").class.as_str(), by("mean").module.as_deref()), ("demo.Stats", Some("demo.Stats")));
+ assert_eq!(by("make").class, "demo.Box$Companion");
+ assert!(by("later").blockers.contains(&"suspend function".to_string()));
+ assert!(
+ by("log").blockers.iter().any(|b| b.contains("I/O"))
+ && by("log").blockers.iter().any(|b| b.contains("Unit"))
+ );
+ let named = describe_text("tools.kt", "@file:JvmName(\"Tools\")\npackage demo\nfun one(): Int { return 1 }\n");
+ assert_eq!(named[0].class, "demo.Tools");
+ }
+
+ #[test]
+ fn kotlin_types() {
+ assert_eq!(rust_type("List<String>").as_deref(), Some("Vec<String>"));
+ assert_eq!(rust_type("Map<String, List<Int>>").as_deref(), Some("std::collections::HashMap<String, Vec<i32>>"));
+ assert_eq!(rust_type("IntArray?").as_deref(), Some("Option<Vec<i32>>"));
+ assert_eq!(rust_type("Point"), None);
+ assert_eq!(java_type("List<List<Int>>", false), "java.util.List<java.util.List<Integer>>");
+ assert_eq!(java_type("Int", false), "int");
+ assert_eq!(java_type("Double?", false), "Double");
+ assert_eq!(java_type("Map<String, Long>", false), "java.util.Map<String, Long>");
+ assert_eq!(java_type("Array<String>", false), "String[]");
+ }
+
+ #[test]
+ fn prologues_call_the_function_again() {
+ let hs = |params: Vec<Param>, ret: &str| Hotspot {
+ name: "f".into(),
+ params,
+ returns: Some(ret.into()),
+ ..Hotspot::default()
+ };
+ let p = |n: &str, t: &str| Param { name: n.into(), annotation: Some(t.into()), ..Param::default() };
+ let plain = hs(vec![p("a", "List<String>"), p("n", "Int")], "Int");
+ assert_eq!(args_array(&plain), "arrayOf<Any?>(a, n)");
+ assert_eq!(call_with(&plain), "f(__faA[0] as List<String>, __faA[1] as Int)");
+ let ext = hs(vec![p("receiver", "String"), p("n", "Int")], "String");
+ assert_eq!(args_array(&ext), "arrayOf<Any?>(this, n)");
+ assert_eq!(call_with(&ext), "(__faA[0] as String).f(__faA[1] as Int)");
+ assert!(record_prologue(&plain).contains("as Int\n return __faR"));
+ assert!(
+ in_kotlin_sources(Path::new("app/src/main/kotlin/demo/A.kt"))
+ && !in_kotlin_sources(Path::new("src/test/kotlin/A.kt"))
+ );
+ assert_eq!(
+ java_dir(Path::new("/p/app/src/main/kotlin/demo/A.kt"), "demo.x").unwrap(),
+ Path::new("/p/app/src/main/java/demo/x")
+ );
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 9089878..9c5fd90 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,7 @@ mod doctor;
mod golang;
mod hotspot;
mod java;
+mod kotlin;
mod mcp;
mod native;
mod node;
diff --git a/src/pipeline.rs b/src/pipeline.rs
index 9b99747..84a00e7 100644
--- a/src/pipeline.rs
+++ b/src/pipeline.rs
@@ -32,6 +32,7 @@ pub fn scan(root: &Path, command: Vec<String>) -> Result<ScanOut> {
None => command,
};
let lang = crate::cfamily::refine(Lang::detect(&command[0])?, &command[1..]);
+ let lang = crate::kotlin::refine(lang, &command[0], &ctx.root);
let exe = lang.resolve(&command[0], &ctx.root)?;
ui::step(format!("profiling {}", ui::bold(command.join(" "))));
let profile = lang.profile(&ctx, &exe, &command[1..])?;