Skip to content

Commit

Permalink
configure: detect lua integer size
Browse files Browse the repository at this point in the history
Lua 5.1 and 5.3 use a different integer size. Run a test program
to set the integer size used in the Rust FFI layer to Rust.
  • Loading branch information
jasonish authored and victorjulien committed Oct 22, 2019
1 parent a946c6e commit 2abcd5d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
19 changes: 19 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,25 @@

AM_CONDITIONAL([HAVE_LUA], [test "x$enable_lua" != "xno"])

# If Lua is enabled, test the integer size.
if test "x$enable_lua" = "xyes"; then
AC_MSG_CHECKING([size of lua integer])
AC_RUN_IFELSE([AC_LANG_PROGRAM([[ #include <lua.h> ]],
[[
if (sizeof(lua_Integer) == 8) {
exit(1);
}
exit(0);
]])],
[
AC_MSG_RESULT([4])
],
[
AC_MSG_RESULT([8])
AC_SUBST([LUA_INT8], ["lua_int8"])
])
fi

# libmaxminddb
AC_ARG_ENABLE(geoip,
AS_HELP_STRING([--enable-geoip],[Enable GeoIP support]),
Expand Down
1 change: 1 addition & 0 deletions rust/Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ debug = true

[features]
lua = []
lua_int8 = ["lua"]
strict = []
debug = []

Expand Down
2 changes: 1 addition & 1 deletion rust/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif
endif

if HAVE_LUA
RUST_FEATURES += lua
RUST_FEATURES += lua $(LUA_INT8)
endif

if DEBUG
Expand Down
10 changes: 8 additions & 2 deletions rust/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ use std::os::raw::c_char;
use std::os::raw::c_int;
use std::os::raw::c_long;

#[cfg(feature = "lua_int8")]
type LuaInteger = i64;

#[cfg(not(feature = "lua_int8"))]
type LuaInteger = i32;

/// The Rust place holder for lua_State.
pub enum CLuaState {}

extern {
fn lua_createtable(lua: *mut CLuaState, narr: c_int, nrec: c_int);
fn lua_settable(lua: *mut CLuaState, idx: c_long);
fn lua_pushlstring(lua: *mut CLuaState, s: *const c_char, len: usize);
fn lua_pushinteger(lua: *mut CLuaState, n: c_long);
fn lua_pushinteger(lua: *mut CLuaState, n: LuaInteger);
}

pub struct LuaState {
Expand Down Expand Up @@ -55,7 +61,7 @@ impl LuaState {

pub fn pushinteger(&self, val: i64) {
unsafe {
lua_pushinteger(self.lua, val as c_long);
lua_pushinteger(self.lua, val as LuaInteger);
}
}
}

0 comments on commit 2abcd5d

Please sign in to comment.