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.

Let each C-bridge language opt in to tagged NaN and infinity

Mohit Mishrad0b46f68bc21
diff --git a/src/cfamily.rs b/src/cfamily.rs
index 4a52a4d..9e464f3 100644
--- a/src/cfamily.rs
+++ b/src/cfamily.rs
@@ -1089,6 +1089,7 @@ pub const FLAVOR_C: Flavor = Flavor {
"Floats: compilers may fuse `a * b + c` into one FMA instruction (clang does on ARM), which rounds differently. If recorded results differ in the last digits, the original was built that way: use `f64::mul_add` there, or build the app with `-ffp-contract=off`.",
"Returns are numbers or bools. Undefined behavior in the original (overflow of signed ints, out-of-bounds reads) isn't a spec: match the defined cases.",
],
+ floats: None,
};
pub const FLAVOR_CPP: Flavor = Flavor {
@@ -1102,6 +1103,7 @@ pub const FLAVOR_CPP: Flavor = Flavor {
"`std::vector` is Vec, `std::array<T, N>` is `[T; N]`, `std::map<std::string, T>` is BTreeMap (same key order), `std::pair` is a tuple.",
"Return `Err(message)` where the original throws.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
diff --git a/src/csharp.rs b/src/csharp.rs
index 381eafa..fa3ed75 100644
--- a/src/csharp.rs
+++ b/src/csharp.rs
@@ -650,6 +650,7 @@ pub const FLAVOR: Flavor = Flavor {
"`ToLowerInvariant` maps one UTF-16 unit at a time; Rust's `to_lowercase` can differ on a few characters (İ, final sigma). `Split((char[]?)null)` splits on Unicode whitespace, like `split_whitespace`.",
"Arrays and lists arrive as JSON arrays, dictionaries as objects with string keys.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
diff --git a/src/golang.rs b/src/golang.rs
index 7ed9fa6..4f2363a 100644
--- a/src/golang.rs
+++ b/src/golang.rs
@@ -437,6 +437,7 @@ pub const FLAVOR: Flavor = Flavor {
"A nil slice or map arrives (and may be returned) as an empty Vec or HashMap: fast-anything treats them as equal.",
"Map iteration order is random in Go: return results in the order the original does when it's deterministic.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
diff --git a/src/java.rs b/src/java.rs
index a737388..9a2e0b0 100644
--- a/src/java.rs
+++ b/src/java.rs
@@ -478,6 +478,7 @@ pub const FLAVOR: Flavor = Flavor {
"`/` and `%` on integers truncate toward zero, as in Rust. `Math.round` rounds half up: `(x + 0.5).floor()`.",
"Arrays and Lists arrive as JSON arrays, Maps as objects with String keys.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
diff --git a/src/kotlin.rs b/src/kotlin.rs
index 7517ba5..7430c97 100644
--- a/src/kotlin.rs
+++ b/src/kotlin.rs
@@ -777,6 +777,7 @@ pub const FLAVOR: Flavor = Flavor {
"`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.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {
diff --git a/src/native.rs b/src/native.rs
index 44e0c80..6a7cded 100644
--- a/src/native.rs
+++ b/src/native.rs
@@ -30,6 +30,8 @@ pub struct Flavor {
pub default_json: fn(&str) -> Option<String>,
/// Language-specific rules for TASK.md.
pub rules: &'static [&'static str],
+ /// How NaN and infinity travel (see glue_rs), if the language's bridge and recorder carry them.
+ pub floats: Option<[&'static str; 4]>,
}
// ------------------------------------------------------------------ the crate
@@ -229,7 +231,7 @@ pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port, f: &F
fs::create_dir_all(dir.join(".cargo"))?;
fs::write(dir.join("Cargo.toml"), cargo_toml(port))?;
fs::write(dir.join(".cargo/config.toml"), backend::CARGO_CONFIG)?;
- fs::write(dir.join("src/lib.rs"), glue_rs(hs, port, f.default_json, None))?;
+ fs::write(dir.join("src/lib.rs"), glue_rs(hs, port, f.default_json, f.floats))?;
let body = dir.join("src/port.rs");
if !body.exists() {
fs::write(&body, port_rs(hs, port, f))?;
diff --git a/src/php.rs b/src/php.rs
index 2a05ff3..ef9effb 100644
--- a/src/php.rs
+++ b/src/php.rs
@@ -287,6 +287,7 @@ pub const FLAVOR: Flavor = Flavor {
"PHP ints are `i64` and turn into floats on overflow; `/` gives a float unless the division is exact, `intdiv` truncates.",
"Untyped parameters arrive as `serde_json::Value`: pick concrete types from the recorded calls.",
],
+ floats: None,
};
pub fn scaffold(ctx: &Ctx, hs: &Hotspot, helpers: &[Hotspot], port: &Port) -> Result<String> {