pub use luaur_analysis as analysis;
pub use luaur_ast as ast;
pub use luaur_bytecode as bytecode;
pub use luaur_common as common;
pub use luaur_compiler as compiler;
pub use luaur_config as config;
pub use luaur_require as require;
pub use luaur_rt as rt;
pub use luaur_vm as vm;
#[cfg(feature = "typecheck")]
pub use luaur_rt::{check, check_with_definitions, TypeDiagnostic};
pub use luaur_rt::{
AnyUserData, AppDataRef, AppDataRefMut, Buffer, Chunk, ChunkMode, Compiler, Debug, DebugWhat,
Error, ExternalError, ExternalResult, FromLua, FromLuaMulti, Function, FunctionInfo, Integer,
IntoLua, IntoLuaMulti, LightUserData, Lua, LuaNativeFn, LuaOptions, LuaString, MaybeSend,
MaybeSync, MetaMethod, MultiValue, Nil, Number, RegistryKey, Result, Scope, StdLib, Table,
TablePairs, TableSequence, Thread, ThreadStatus, TypeMetatable, UserData, UserDataFields,
UserDataMethods, UserDataRef, UserDataRefMut, Value, Variadic, Vector, VmState, WeakLua,
};
#[cfg(feature = "async")]
pub use luaur_rt::AsyncThread;
#[cfg(feature = "serde")]
pub use luaur_rt::{
DeserializeOptions, LuaDeserializer, LuaSerdeExt, LuaSerializer, SerializableTable,
SerializableValue, SerializeOptions,
};
pub mod prelude {
#[cfg(feature = "typecheck")]
pub use crate::{check, check_with_definitions};
pub use crate::{compile, eval};
pub use luaur_ast::records::parse_options::ParseOptions;
pub use luaur_compiler::records::compile_options::CompileOptions;
pub use luaur_rt::prelude::*;
}
use luaur_ast::records::parse_options::ParseOptions;
use luaur_bytecode::records::bytecode_encoder::BytecodeEncoder;
use luaur_compiler::functions::compile::compile as compiler_compile;
use luaur_compiler::records::compile_options::CompileOptions;
struct NoopEncoder;
impl BytecodeEncoder for NoopEncoder {
fn encode(&mut self, _data: &mut [u32]) {}
}
pub fn compile(source: &str) -> core::result::Result<Vec<u8>, String> {
let options = CompileOptions::default();
let parse_options = ParseOptions::default();
let mut encoder = NoopEncoder;
let owned = source.to_string();
let blob = compiler_compile(
&owned,
&options,
&parse_options,
&mut encoder as *mut dyn BytecodeEncoder,
);
let bytes = blob.into_bytes();
if bytes.first() == Some(&0u8) {
let message = String::from_utf8_lossy(&bytes[1..]).into_owned();
return Err(message);
}
Ok(bytes)
}
pub fn eval(source: &str) -> core::result::Result<(), String> {
use luaur_vm::functions::lua_l_newstate::lua_l_newstate;
use luaur_vm::functions::lua_l_openlibs::lua_l_openlibs;
use luaur_vm::functions::lua_newthread::lua_newthread;
use luaur_vm::functions::lua_resume::lua_resume;
use luaur_vm::functions::lua_tolstring::lua_tolstring;
use luaur_vm::functions::luau_load::luau_load;
let bytecode = compile(source)?;
luaur_common::set_all_flags(true);
unsafe {
let l = lua_l_newstate();
if l.is_null() {
return Err("lua_l_newstate returned null".to_string());
}
lua_l_openlibs(l);
let t = lua_newthread(l);
if t.is_null() {
return Err("lua_newthread returned null".to_string());
}
let rc = luau_load(
t,
c"=eval".as_ptr(),
bytecode.as_ptr() as *const core::ffi::c_char,
bytecode.len(),
0,
);
if rc != 0 {
return Err(format!("luau_load failed: rc={rc}"));
}
let status = lua_resume(t, core::ptr::null_mut(), 0);
if status != 0 {
let mut len = 0usize;
let s = lua_tolstring(t, -1, &mut len);
let msg = if s.is_null() {
"<non-string error>".to_string()
} else {
let bytes = core::slice::from_raw_parts(s as *const u8, len);
String::from_utf8_lossy(bytes).into_owned()
};
return Err(msg);
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{compile, eval};
#[test]
fn compile_returns_nonempty_bytecode() {
let bytecode = compile("return 1 + 1").expect("compile should succeed");
assert!(!bytecode.is_empty(), "bytecode must be non-empty");
}
#[test]
fn eval_runs_passing_assertion() {
eval("assert(1 + 1 == 2)").expect("eval should succeed");
}
#[test]
fn eval_reports_runtime_error() {
let err = eval("error('boom')").expect_err("eval should fail");
assert!(
err.contains("boom"),
"error message should mention boom: {err}"
);
}
}