Compare commits

...

2 Commits

Author SHA1 Message Date
Alex Orlenko 52be96677b 0.2.1 release 2019-12-26 23:09:11 +00:00
Alex Orlenko 42b33849e1 Add support of loading a specified set of standard libraries 2019-12-26 23:04:21 +00:00
7 changed files with 241 additions and 37 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "mlua"
version = "0.2.0"
version = "0.2.1"
authors = ["Aleksandr Orlenko <zxteam@pm.me>", "kyren <catherine@chucklefish.org>"]
edition = "2018"
repository = "https://github.com/khvzak/mlua"
+8 -2
View File
@@ -263,15 +263,21 @@ int main(int argc, const char **argv) {
RS_STR("LUA_IOLIBNAME", LUA_IOLIBNAME),
RS_STR("LUA_OSLIBNAME", LUA_OSLIBNAME),
RS_STR("LUA_STRLIBNAME", LUA_STRLIBNAME),
#if LUA_VERSION_NUM == 503
#ifdef LUA_UTF8LIBNAME
RS_STR("LUA_UTF8LIBNAME", LUA_UTF8LIBNAME),
#endif
#if LUA_VERSION_NUM >= 502
#ifdef LUA_BITLIBNAME
RS_STR("LUA_BITLIBNAME", LUA_BITLIBNAME),
#endif
RS_STR("LUA_MATHLIBNAME", LUA_MATHLIBNAME),
RS_STR("LUA_DBLIBNAME", LUA_DBLIBNAME),
RS_STR("LUA_LOADLIBNAME", LUA_LOADLIBNAME),
#ifdef LUA_JITLIBNAME
RS_STR("LUA_JITLIBNAME", LUA_JITLIBNAME),
#endif
#ifdef LUA_FFILIBNAME
RS_STR("LUA_FFILIBNAME", LUA_FFILIBNAME),
#endif
};
if (!write_items(f, glue_entries)) {
+13 -4
View File
@@ -32,12 +32,15 @@ pub use super::glue::{
LUA_STRLIBNAME, LUA_TABLIBNAME,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use super::glue::LUA_BITLIBNAME;
#[cfg(feature = "lua53")]
pub use super::glue::LUA_UTF8LIBNAME;
#[cfg(any(feature = "lua52", feature = "luajit"))]
pub use super::glue::LUA_BITLIBNAME;
#[cfg(feature = "luajit")]
pub use super::glue::{LUA_FFILIBNAME, LUA_JITLIBNAME};
extern "C" {
pub fn luaopen_base(L: *mut lua_State) -> c_int;
#[cfg(any(feature = "lua53", feature = "lua52"))]
@@ -48,11 +51,17 @@ extern "C" {
pub fn luaopen_string(L: *mut lua_State) -> c_int;
#[cfg(feature = "lua53")]
pub fn luaopen_utf8(L: *mut lua_State) -> c_int;
#[cfg(any(feature = "lua53", feature = "lua52"))]
#[cfg(feature = "lua52")]
pub fn luaopen_bit32(L: *mut lua_State) -> c_int;
pub fn luaopen_math(L: *mut lua_State) -> c_int;
pub fn luaopen_debug(L: *mut lua_State) -> c_int;
pub fn luaopen_package(L: *mut lua_State) -> c_int;
#[cfg(feature = "luajit")]
pub fn luaopen_bit(L: *mut lua_State) -> c_int;
#[cfg(feature = "luajit")]
pub fn luaopen_jit(L: *mut lua_State) -> c_int;
#[cfg(feature = "luajit")]
pub fn luaopen_ffi(L: *mut lua_State) -> c_int;
pub fn luaL_openlibs(L: *mut lua_State);
}
+18 -12
View File
@@ -155,12 +155,12 @@ pub use self::lua::{
lua_yield,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use self::lua::{lua_callk, lua_pcallk, lua_upvalueid, lua_upvaluejoin, lua_yieldk};
#[cfg(feature = "lua53")]
pub use self::lua::{lua_isyieldable, lua_version};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use self::lua::{lua_callk, lua_pcallk, lua_upvalueid, lua_upvaluejoin, lua_yieldk};
// auxiliary library types
pub use self::lauxlib::luaL_Reg;
@@ -186,11 +186,14 @@ pub use self::lualib::{
luaopen_package, luaopen_string, luaopen_table,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
#[cfg(feature = "lua53")]
pub use self::lualib::{luaopen_coroutine, luaopen_utf8};
#[cfg(feature = "lua52")]
pub use self::lualib::{luaopen_bit32, luaopen_coroutine};
#[cfg(feature = "lua53")]
pub use self::lualib::luaopen_utf8;
#[cfg(feature = "luajit")]
pub use self::lualib::{luaopen_bit, luaopen_ffi, luaopen_jit};
// constants from lua.h
pub use self::lua::{
@@ -203,14 +206,14 @@ pub use self::lua::{
LUA_TTABLE, LUA_TTHREAD, LUA_TUSERDATA, LUA_YIELD,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use self::lua::{LUA_ERRGCMM, LUA_GCISRUNNING, LUA_RIDX_GLOBALS, LUA_RIDX_MAINTHREAD};
#[cfg(feature = "lua53")]
pub use self::lua::{
LUA_OPBAND, LUA_OPBNOT, LUA_OPBOR, LUA_OPBXOR, LUA_OPIDIV, LUA_OPSHL, LUA_OPSHR,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use self::lua::{LUA_ERRGCMM, LUA_GCISRUNNING, LUA_RIDX_GLOBALS, LUA_RIDX_MAINTHREAD};
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use self::lua::{LUA_ENVIRONINDEX, LUA_GLOBALSINDEX};
@@ -223,12 +226,15 @@ pub use self::lualib::{
LUA_STRLIBNAME, LUA_TABLIBNAME,
};
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub use self::lualib::LUA_BITLIBNAME;
#[cfg(feature = "lua53")]
pub use self::lualib::LUA_UTF8LIBNAME;
#[cfg(any(feature = "lua52", feature = "luajit"))]
pub use self::lualib::LUA_BITLIBNAME;
#[cfg(feature = "luajit")]
pub use self::lualib::{LUA_FFILIBNAME, LUA_JITLIBNAME};
// Not actually defined in lua.h / luaconf.h
pub const LUA_MAX_UPVALUES: c_int = 255;
+2
View File
@@ -50,6 +50,7 @@ mod function;
mod lua;
mod multi;
mod scope;
mod stdlib;
mod string;
mod table;
mod thread;
@@ -65,6 +66,7 @@ pub use crate::function::Function;
pub use crate::lua::{Chunk, Lua};
pub use crate::multi::Variadic;
pub use crate::scope::Scope;
pub use crate::stdlib::StdLib;
pub use crate::string::String;
pub use crate::table::{Table, TablePairs, TableSequence};
pub use crate::thread::{Thread, ThreadStatus};
+127 -18
View File
@@ -11,6 +11,7 @@ use crate::error::{Error, Result};
use crate::ffi;
use crate::function::Function;
use crate::scope::Scope;
use crate::stdlib::StdLib;
use crate::string::String;
use crate::table::Table;
use crate::thread::Thread;
@@ -68,35 +69,48 @@ impl Drop for Lua {
impl Lua {
/// Creates a new Lua state and loads standard library without the `debug` library.
pub fn new() -> Lua {
Self::new_with(StdLib::ALL_NO_DEBUG)
}
/// Creates a new Lua state and loads the specified set of standard libraries.
///
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
///
/// [`StdLib`]: struct.StdLib.html
pub fn new_with(libs: StdLib) -> Lua {
unsafe {
let state = ffi::luaL_newstate();
ffi::luaL_requiref(state, cstr!("_G"), ffi::luaopen_base, 1);
#[cfg(any(feature = "lua53", feature = "lua52"))]
ffi::luaL_requiref(state, cstr!("coroutine"), ffi::luaopen_coroutine, 1);
ffi::luaL_requiref(state, cstr!("table"), ffi::luaopen_table, 1);
ffi::luaL_requiref(state, cstr!("io"), ffi::luaopen_io, 1);
ffi::luaL_requiref(state, cstr!("os"), ffi::luaopen_os, 1);
ffi::luaL_requiref(state, cstr!("string"), ffi::luaopen_string, 1);
#[cfg(feature = "lua53")]
ffi::luaL_requiref(state, cstr!("utf8"), ffi::luaopen_utf8, 1);
#[cfg(any(feature = "lua53", feature = "lua52"))]
ffi::luaL_requiref(state, cstr!("bit32"), ffi::luaopen_bit32, 1);
ffi::luaL_requiref(state, cstr!("math"), ffi::luaopen_math, 1);
ffi::luaL_requiref(state, cstr!("package"), ffi::luaopen_package, 1);
#[cfg(feature = "lua53")]
ffi::lua_pop(state, 10);
#[cfg(feature = "lua52")]
ffi::lua_pop(state, 9);
#[cfg(any(feature = "lua51", feature = "luajit"))]
ffi::lua_pop(state, 7);
ffi::lua_pop(state, 1);
let mut lua = Lua::init_from_ptr(state);
lua.ephemeral = false;
mlua_expect!(
protect_lua_closure(lua.main_state, 0, 0, |state| {
load_from_std_lib(state, libs);
}),
"Error during loading standard libraries"
);
lua
}
}
/// Loads the specified set of standard libraries into an existing Lua state.
///
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
///
/// [`StdLib`]: struct.StdLib.html
pub fn load_from_std_lib(&self, libs: StdLib) -> Result<()> {
unsafe {
protect_lua_closure(self.main_state, 0, 0, |state| {
load_from_std_lib(state, libs);
})
}
}
/// Constructs a new Lua instance from the existing state.
pub unsafe fn init_from_ptr(state: *mut ffi::lua_State) -> Lua {
#[cfg(any(feature = "lua53", feature = "lua52"))]
@@ -1216,6 +1230,101 @@ impl<'lua, 'a> Chunk<'lua, 'a> {
}
}
unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) {
#[cfg(any(feature = "lua53", feature = "lua52"))]
{
if libs.contains(StdLib::COROUTINE) {
let colib_name = CString::new(ffi::LUA_COLIBNAME).unwrap();
ffi::luaL_requiref(state, colib_name.as_ptr(), ffi::luaopen_coroutine, 1);
ffi::lua_pop(state, 1);
}
}
if libs.contains(StdLib::TABLE) {
let tablib_name = CString::new(ffi::LUA_TABLIBNAME).unwrap();
ffi::luaL_requiref(state, tablib_name.as_ptr(), ffi::luaopen_table, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::IO) {
let iolib_name = CString::new(ffi::LUA_IOLIBNAME).unwrap();
ffi::luaL_requiref(state, iolib_name.as_ptr(), ffi::luaopen_io, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::OS) {
let oslib_name = CString::new(ffi::LUA_OSLIBNAME).unwrap();
ffi::luaL_requiref(state, oslib_name.as_ptr(), ffi::luaopen_os, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::STRING) {
let strlib_name = CString::new(ffi::LUA_STRLIBNAME).unwrap();
ffi::luaL_requiref(state, strlib_name.as_ptr(), ffi::luaopen_string, 1);
ffi::lua_pop(state, 1);
}
#[cfg(feature = "lua53")]
{
if libs.contains(StdLib::UTF8) {
let utf8lib_name = CString::new(ffi::LUA_UTF8LIBNAME).unwrap();
ffi::luaL_requiref(state, utf8lib_name.as_ptr(), ffi::luaopen_utf8, 1);
ffi::lua_pop(state, 1);
}
}
#[cfg(feature = "lua52")]
{
if libs.contains(StdLib::BIT) {
let bitlib_name = CString::new(ffi::LUA_BITLIBNAME).unwrap();
ffi::luaL_requiref(state, bitlib_name.as_ptr(), ffi::luaopen_bit32, 1);
ffi::lua_pop(state, 1);
}
}
#[cfg(feature = "luajit")]
{
if libs.contains(StdLib::BIT) {
let bitlib_name = CString::new(ffi::LUA_BITLIBNAME).unwrap();
ffi::luaL_requiref(state, bitlib_name.as_ptr(), ffi::luaopen_bit, 1);
ffi::lua_pop(state, 1);
}
}
if libs.contains(StdLib::MATH) {
let mathlib_name = CString::new(ffi::LUA_MATHLIBNAME).unwrap();
ffi::luaL_requiref(state, mathlib_name.as_ptr(), ffi::luaopen_math, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::DEBUG) {
let dblib_name = CString::new(ffi::LUA_DBLIBNAME).unwrap();
ffi::luaL_requiref(state, dblib_name.as_ptr(), ffi::luaopen_debug, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::PACKAGE) {
let loadlib_name = CString::new(ffi::LUA_LOADLIBNAME).unwrap();
ffi::luaL_requiref(state, loadlib_name.as_ptr(), ffi::luaopen_package, 1);
ffi::lua_pop(state, 1);
}
#[cfg(feature = "luajit")]
{
if libs.contains(StdLib::JIT) {
let jitlib_name = CString::new(ffi::LUA_JITLIBNAME).unwrap();
ffi::luaL_requiref(state, jitlib_name.as_ptr(), ffi::luaopen_jit, 1);
ffi::lua_pop(state, 1);
}
if libs.contains(StdLib::FFI) {
let ffilib_name = CString::new(ffi::LUA_FFILIBNAME).unwrap();
ffi::luaL_requiref(state, ffilib_name.as_ptr(), ffi::luaopen_ffi, 1);
ffi::lua_pop(state, 1);
}
}
}
unsafe fn ref_stack_pop(extra: &mut ExtraData) -> c_int {
if let Some(free) = extra.ref_free.pop() {
ffi::lua_replace(extra.ref_thread, free);
+72
View File
@@ -0,0 +1,72 @@
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
use std::u32;
/// Flags describing the set of lua modules to load.
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct StdLib(u32);
impl StdLib {
#[cfg(any(feature = "lua53", feature = "lua52"))]
pub const COROUTINE: StdLib = StdLib(1 << 0);
pub const TABLE: StdLib = StdLib(1 << 1);
pub const IO: StdLib = StdLib(1 << 2);
pub const OS: StdLib = StdLib(1 << 3);
pub const STRING: StdLib = StdLib(1 << 4);
#[cfg(feature = "lua53")]
pub const UTF8: StdLib = StdLib(1 << 5);
#[cfg(any(feature = "lua52", feature = "luajit"))]
pub const BIT: StdLib = StdLib(1 << 6);
pub const MATH: StdLib = StdLib(1 << 7);
pub const PACKAGE: StdLib = StdLib(1 << 8);
#[cfg(feature = "luajit")]
pub const JIT: StdLib = StdLib(1 << 9);
#[cfg(feature = "luajit")]
pub const FFI: StdLib = StdLib(1 << 10);
pub const DEBUG: StdLib = StdLib(1 << 31); // always highest bit
pub const ALL: StdLib = StdLib(u32::MAX);
pub const ALL_NO_DEBUG: StdLib = StdLib((1 << 31) - 1);
pub fn contains(self, lib: Self) -> bool {
(self & lib).0 != 0
}
}
impl BitAnd for StdLib {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
StdLib(self.0 & rhs.0)
}
}
impl BitAndAssign for StdLib {
fn bitand_assign(&mut self, rhs: Self) {
*self = StdLib(self.0 & rhs.0)
}
}
impl BitOr for StdLib {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
StdLib(self.0 | rhs.0)
}
}
impl BitOrAssign for StdLib {
fn bitor_assign(&mut self, rhs: Self) {
*self = StdLib(self.0 | rhs.0)
}
}
impl BitXor for StdLib {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
StdLib(self.0 ^ rhs.0)
}
}
impl BitXorAssign for StdLib {
fn bitxor_assign(&mut self, rhs: Self) {
*self = StdLib(self.0 ^ rhs.0)
}
}