use microsandbox_portal::portal::repl::start_engines;
#[cfg(any(feature = "python", feature = "nodejs"))]
use microsandbox_portal::portal::repl::Language;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _engine_handle = start_engines().await?;
println!("✅ Engines started successfully");
#[cfg(feature = "python")]
{
println!("\n🐍 Running Python example in REPL:");
let python_code = r#"
# Define a function
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
# Use the function
print("Factorial examples:")
for i in range(1, 6):
print(f"factorial({i}) = {factorial(i)}")
# Create a simple data structure
fruits = ["apple", "banana", "cherry"]
print("\nFruit list:")
for i, fruit in enumerate(fruits):
print(f"{i+1}. {fruit}")
"#;
let result = _engine_handle
.eval(python_code, Language::Python, "123", Some(60))
.await?;
for line in result {
println!("[{:?}] {}", line.stream, line.text);
}
}
#[cfg(feature = "nodejs")]
{
println!("\n🟨 Running Node.js example in REPL:");
let javascript_code = r#"
// Define a class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
// Use the class
const people = [
new Person("Alice", 28),
new Person("Bob", 32),
new Person("Charlie", 22)
];
console.log("People greetings:");
people.forEach(person => {
console.log(person.greet());
});
// Demonstrate async functionality
console.log("\nAsync example:");
async function fetchData() {
// Simulate fetching data
return new Promise(resolve => {
setTimeout(() => {
resolve({ success: true, data: [1, 2, 3, 4, 5] });
}, 500);
});
}
// We can't actually wait for this in a REPL, but we can start it
fetchData().then(result => {
console.log("Data fetched:", result);
});
console.log("Waiting for data...");
"#;
let result = _engine_handle
.eval(javascript_code, Language::Node, "123", Some(60))
.await?;
for line in result {
println!("[{:?}] {}", line.stream, line.text);
}
}
#[cfg(feature = "python")]
{
println!("\n🔄 Python stateful REPL session example:");
let python_step1 = "x = 10";
let result1 = _engine_handle
.eval(python_step1, Language::Python, "123", None)
.await?;
for line in result1 {
println!("[{:?}] {}", line.stream, line.text);
}
let python_step2 = "print(f'The value of x is {x}')";
let result2 = _engine_handle
.eval(python_step2, Language::Python, "123", None)
.await?;
for line in result2 {
println!("[{:?}] {}", line.stream, line.text);
}
}
#[cfg(feature = "nodejs")]
{
println!("\n🔄 Node.js stateful REPL session example:");
let nodejs_step1 = "const greeting = 'Hello from JavaScript!';";
let result1 = _engine_handle
.eval(nodejs_step1, Language::Node, "123", None)
.await?;
for line in result1 {
println!("[{:?}] {}", line.stream, line.text);
}
let nodejs_step2 = "console.log(greeting);";
let result2 = _engine_handle
.eval(nodejs_step2, Language::Node, "123", None)
.await?;
for line in result2 {
println!("[{:?}] {}", line.stream, line.text);
}
}
println!("\nExample completed successfully!");
Ok(())
}