diff --git a/build.rs b/build.rs index 1dec803..fd4f1d2 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,17 @@ extern crate gcc; +use std::env; + fn main() { - gcc::Config::new() - .define("LUA_USE_APICHECK", None) + let mut config = gcc::Config::new(); + + if env::var("CARGO_CFG_TARGET_OS") == Ok("linux".to_string()) { + // Among other things, this enables `io.popen` support. + // Despite the name, we could enable this on more platforms. + config.define("LUA_USE_LINUX", None); + } + + config.define("LUA_USE_APICHECK", None) .include("lua") .file("lua/lapi.c") .file("lua/lauxlib.c") diff --git a/examples/repl.rs b/examples/repl.rs new file mode 100644 index 0000000..17b9e3a --- /dev/null +++ b/examples/repl.rs @@ -0,0 +1,40 @@ +//! This example shows a simple read-evaluate-print-loop (REPL). + +extern crate rlua; + +use rlua::*; +use std::io::prelude::*; +use std::io::{stdin, stdout, stderr, BufReader}; + +fn main() { + let lua = Lua::new(); + let mut stdout = stdout(); + let mut stdin = BufReader::new(stdin()); + + loop { + write!(stdout, "> ").unwrap(); + stdout.flush().unwrap(); + + let mut line = String::new(); + + loop { + stdin.read_line(&mut line).unwrap(); + + match lua.eval::(&line) { + Ok(values) => { + println!("{}", values.iter().map(|value| format!("{:?}", value)).collect::>().join("\t")); + break; + } + Err(LuaError(LuaErrorKind::IncompleteStatement(_), _)) => { + // continue reading input and append it to `line` + write!(stdout, ">> ").unwrap(); + stdout.flush().unwrap(); + } + Err(e) => { + writeln!(stderr(), "error: {}", e).unwrap(); + break; + } + } + } + } +}