use microsandbox_portal::portal::repl::{start_engines, Language};
use std::error::Error;
const TIMEOUT_SECONDS: u64 = 10;
fn print_output(prefix: &str, lines: &[microsandbox_portal::portal::repl::Line]) {
println!("{} execution result:", prefix);
if lines.is_empty() {
println!("No output produced");
} else {
for line in lines {
println!("[{:?}] {}", line.stream, line.text);
}
}
println!();
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let engine_handle = start_engines().await?;
println!("✅ Engines started successfully\n");
println!(
"🕒 Running examples with {}-second timeout\n",
TIMEOUT_SECONDS
);
#[cfg(feature = "python")]
{
println!("🐍 Python normal execution:");
let python_normal = r#"
import time
print("Counting from 1 to 5 with delays:")
for i in range(1, 6):
print(f"Count: {i}")
time.sleep(1) # Sleep for 1 second
print("Python counting complete!")
"#;
let result = engine_handle
.eval(
python_normal,
Language::Python,
"python_normal",
Some(TIMEOUT_SECONDS),
)
.await?;
print_output("Python normal", &result);
}
#[cfg(feature = "python")]
{
println!("🐍 Python infinite loop (should timeout):");
let python_infinite = r#"
import time
print("Starting infinite loop in Python...")
counter = 0
while True:
counter += 1
print(f"Iteration {counter}...")
time.sleep(0.5) # Sleep for half a second
"#;
let result = engine_handle
.eval(
python_infinite,
Language::Python,
"python_infinite",
Some(TIMEOUT_SECONDS),
)
.await?;
print_output("Python infinite loop", &result);
}
#[cfg(feature = "nodejs")]
{
println!("🟨 Node.js normal execution:");
let js_normal = r#"
console.log("Counting from 1 to 5 with delays:");
// Use a simple for loop with a delay function for consistency with Python example
function sleep(ms) {
const start = new Date().getTime();
while (new Date().getTime() < start + ms);
}
for (let i = 1; i <= 5; i++) {
console.log(`Count: ${i}`);
sleep(1000); // Sleep for 1 second
}
console.log("Node.js counting complete!");
"#;
let result = engine_handle
.eval(
js_normal,
Language::Node,
"js_normal",
Some(TIMEOUT_SECONDS),
)
.await?;
print_output("Node.js normal", &result);
}
#[cfg(feature = "nodejs")]
{
println!("🟨 Node.js infinite loop (should timeout):");
let js_infinite = r#"
console.log("Starting infinite loop in Node.js...");
// Use a while loop for consistency with Python example
function sleep(ms) {
const start = new Date().getTime();
while (new Date().getTime() < start + ms);
}
let counter = 0;
while (true) {
counter++;
console.log(`Iteration ${counter}...`);
sleep(500); // Sleep for half a second
}
"#;
let result = engine_handle
.eval(
js_infinite,
Language::Node,
"js_infinite",
Some(TIMEOUT_SECONDS),
)
.await?;
print_output("Node.js infinite loop", &result);
}
println!("✅ All examples completed!");
Ok(())
}