summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build/main.rs9
-rw-r--r--src/ffi/mod.rs2
-rw-r--r--src/ffi/safe.rs265
-rw-r--r--src/ffi/shim/compat-5.3.c953
-rw-r--r--src/ffi/shim/compat-5.3.h424
-rw-r--r--src/ffi/shim/shim.c432
-rw-r--r--src/function.rs35
-rw-r--r--src/hook.rs7
-rw-r--r--src/lua.rs322
-rw-r--r--src/scope.rs138
-rw-r--r--src/serde/mod.rs36
-rw-r--r--src/serde/ser.rs14
-rw-r--r--src/table.rs107
-rw-r--r--src/thread.rs19
-rw-r--r--src/types.rs1
-rw-r--r--src/userdata.rs56
-rw-r--r--src/util.rs561
-rw-r--r--src/value.rs6
-rw-r--r--tests/tests.rs2
19 files changed, 2538 insertions, 851 deletions
diff --git a/build/main.rs b/build/main.rs
index d3f13bf..11d805b 100644
--- a/build/main.rs
+++ b/build/main.rs
@@ -239,5 +239,14 @@ fn main() {
println!("cargo:rerun-if-changed=src/ffi/glue/glue.c");
}
+ let mut shim_cc = cc::Build::new();
+ shim_cc
+ .include(include_dir)
+ .define("COMPAT53_INCLUDE_SOURCE", None);
+ #[cfg(feature = "luajit")]
+ shim_cc.define("COMPAT53_LUAJIT", None);
+ shim_cc.file("src/ffi/shim/shim.c").compile("shim");
+
+ println!("cargo:rerun-if-changed=src/ffi/shim");
println!("cargo:rerun-if-changed=build");
}
diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs
index 3157944..b0d1e20 100644
--- a/src/ffi/mod.rs
+++ b/src/ffi/mod.rs
@@ -288,3 +288,5 @@ mod lauxlib;
mod lua;
mod luaconf;
mod lualib;
+
+pub mod safe;
diff --git a/src/ffi/safe.rs b/src/ffi/safe.rs
new file mode 100644
index 0000000..59f6383
--- /dev/null
+++ b/src/ffi/safe.rs
@@ -0,0 +1,265 @@
+use std::ffi::CString;
+use std::os::raw::{c_char, c_int, c_void};
+
+use crate::error::Result;
+use crate::util::protect_lua;
+
+use super::lua::{lua_CFunction, lua_Debug, lua_Integer, lua_State};
+
+extern "C" {
+ #[link_name = "MLUA_WRAPPED_ERROR_SIZE"]
+ pub static mut WRAPPED_ERROR_SIZE: usize;
+ #[link_name = "MLUA_WRAPPED_PANIC_SIZE"]
+ pub static mut WRAPPED_PANIC_SIZE: usize;
+ #[link_name = "MLUA_WRAPPED_ERROR_KEY"]
+ pub static mut WRAPPED_ERROR_KEY: *const c_void;
+ #[link_name = "MLUA_WRAPPED_PANIC_KEY"]
+ pub static mut WRAPPED_PANIC_KEY: *const c_void;
+
+ pub fn lua_call_mlua_hook_proc(L: *mut lua_State, ar: *mut lua_Debug);
+
+ pub fn meta_index_impl(state: *mut lua_State) -> c_int;
+ pub fn meta_newindex_impl(state: *mut lua_State) -> c_int;
+ pub fn bind_call_impl(state: *mut lua_State) -> c_int;
+ pub fn error_traceback(state: *mut lua_State) -> c_int;
+
+ fn lua_gc_s(L: *mut lua_State) -> c_int;
+ fn luaL_ref_s(L: *mut lua_State) -> c_int;
+ fn lua_pushlstring_s(L: *mut lua_State) -> c_int;
+ fn lua_tolstring_s(L: *mut lua_State) -> c_int;
+ fn lua_newthread_s(L: *mut lua_State) -> c_int;
+ fn lua_newuserdata_s(L: *mut lua_State) -> c_int;
+ fn lua_pushcclosure_s(L: *mut lua_State) -> c_int;
+ fn lua_pushrclosure_s(L: *mut lua_State) -> c_int;
+ fn luaL_requiref_s(L: *mut lua_State) -> c_int;
+ fn error_traceback_s(L: *mut lua_State) -> c_int;
+
+ fn lua_createtable_s(L: *mut lua_State) -> c_int;
+ fn lua_gettable_s(L: *mut lua_State) -> c_int;
+ fn lua_settable_s(L: *mut lua_State) -> c_int;
+ fn lua_geti_s(L: *mut lua_State) -> c_int;
+ fn lua_rawset_s(L: *mut lua_State) -> c_int;
+ fn lua_rawseti_s(L: *mut lua_State) -> c_int;
+ fn lua_rawsetp_s(L: *mut lua_State) -> c_int;
+ fn lua_rawsetfield_s(L: *mut lua_State) -> c_int;
+ fn lua_rawinsert_s(L: *mut lua_State) -> c_int;
+ fn lua_rawremove_s(L: *mut lua_State) -> c_int;
+ fn luaL_len_s(L: *mut lua_State) -> c_int;
+ fn lua_next_s(L: *mut lua_State) -> c_int;
+}
+
+#[repr(C)]
+struct StringArg {
+ data: *const c_char,
+ len: usize,
+}
+
+//
+// Common functions
+//
+
+// Uses 4 stack spaces
+pub unsafe fn lua_gc(state: *mut lua_State, what: c_int, data: c_int) -> Result<c_int> {
+ super::lua_pushinteger(state, what as lua_Integer);
+ super::lua_pushinteger(state, data as lua_Integer);
+ protect_lua(state, 2, lua_gc_s)?;
+ let ret = super::lua_tointeger(state, -1) as c_int;
+ super::lua_pop(state, 1);
+ Ok(ret)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn luaL_ref(state: *mut lua_State, table: c_int) -> Result<c_int> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ protect_lua(state, 2, luaL_ref_s)?;
+ let ret = super::lua_tointeger(state, -1) as c_int;
+ super::lua_pop(state, 1);
+ Ok(ret)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_pushstring<S: AsRef<[u8]> + ?Sized>(state: *mut lua_State, s: &S) -> Result<()> {
+ let s = s.as_ref();
+ let s = StringArg {
+ data: s.as_ptr() as *const c_char,
+ len: s.len(),
+ };
+ super::lua_pushlightuserdata(state, &s as *const StringArg as *mut c_void);
+ protect_lua(state, 1, lua_pushlstring_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_tolstring(
+ state: *mut lua_State,
+ index: c_int,
+ len: *mut usize,
+) -> Result<*const c_char> {
+ let index = super::lua_absindex(state, index);
+ super::lua_pushvalue(state, index);
+ super::lua_pushlightuserdata(state, len as *mut c_void);
+ protect_lua(state, 2, lua_tolstring_s)?;
+ let s = super::lua_touserdata(state, -1);
+ super::lua_pop(state, 1);
+ super::lua_replace(state, index);
+ Ok(s as *const c_char)
+}
+
+// Uses 2 stack spaces
+pub unsafe fn lua_newthread(state: *mut lua_State) -> Result<*mut lua_State> {
+ protect_lua(state, 0, lua_newthread_s)?;
+ Ok(super::lua_tothread(state, -1))
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_newuserdata(state: *mut lua_State, size: usize) -> Result<*mut c_void> {
+ super::lua_pushinteger(state, size as lua_Integer);
+ protect_lua(state, 1, lua_newuserdata_s)?;
+ Ok(super::lua_touserdata(state, -1))
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_pushcclosure(state: *mut lua_State, f: lua_CFunction, n: c_int) -> Result<()> {
+ super::lua_pushlightuserdata(state, f as *mut c_void);
+ super::lua_pushinteger(state, n as lua_Integer);
+ protect_lua(state, n + 2, lua_pushcclosure_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_pushrclosure(state: *mut lua_State, f: lua_CFunction, n: c_int) -> Result<()> {
+ super::lua_pushlightuserdata(state, f as *mut c_void);
+ if n > 0 {
+ super::lua_rotate(state, -n - 1, 1);
+ }
+ super::lua_pushinteger(state, n as lua_Integer + 1);
+ protect_lua(state, n + 2, lua_pushrclosure_s)
+}
+
+// Uses 5 stack spaces
+pub unsafe fn luaL_requiref<S: AsRef<[u8]> + ?Sized>(
+ state: *mut lua_State,
+ modname: &S,
+ openf: lua_CFunction,
+ glb: c_int,
+) -> Result<()> {
+ let modname = mlua_expect!(CString::new(modname.as_ref()), "modname contains nil bytes");
+ super::lua_pushlightuserdata(state, modname.as_ptr() as *mut c_void);
+ super::lua_pushlightuserdata(state, openf as *mut c_void);
+ super::lua_pushinteger(state, glb as lua_Integer);
+ protect_lua(state, 3, luaL_requiref_s)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn error_traceback2(state: *mut lua_State, state2: *mut lua_State) -> Result<()> {
+ mlua_assert!(
+ state != state2,
+ "error_traceback2 must be used with two different states"
+ );
+ super::lua_pushlightuserdata(state, state2);
+ protect_lua(state, 1, error_traceback_s)
+}
+
+//
+// Table functions
+//
+
+// Uses 4 stack spaces
+pub unsafe fn lua_createtable(state: *mut lua_State, narr: c_int, nrec: c_int) -> Result<()> {
+ super::lua_pushinteger(state, narr as lua_Integer);
+ super::lua_pushinteger(state, nrec as lua_Integer);
+ protect_lua(state, 2, lua_createtable_s)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_gettable(state: *mut lua_State, table: c_int) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ protect_lua(state, 2, lua_gettable_s)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_settable(state: *mut lua_State, table: c_int) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -3, 1);
+ protect_lua(state, 3, lua_settable_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_geti(state: *mut lua_State, table: c_int, i: lua_Integer) -> Result<c_int> {
+ super::lua_pushvalue(state, table);
+ super::lua_pushinteger(state, i);
+ protect_lua(state, 2, lua_geti_s).map(|_| super::lua_type(state, -1))
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_rawset(state: *mut lua_State, table: c_int) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -3, 1);
+ protect_lua(state, 3, lua_rawset_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_rawseti(state: *mut lua_State, table: c_int, i: lua_Integer) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ super::lua_pushinteger(state, i);
+ protect_lua(state, 3, lua_rawseti_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_rawsetp(state: *mut lua_State, table: c_int, ptr: *const c_void) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ super::lua_pushlightuserdata(state, ptr as *mut c_void);
+ protect_lua(state, 3, lua_rawsetp_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_rawsetfield<S>(state: *mut lua_State, table: c_int, field: &S) -> Result<()>
+where
+ S: AsRef<[u8]> + ?Sized,
+{
+ let field = field.as_ref();
+ let s = StringArg {
+ data: field.as_ptr() as *const c_char,
+ len: field.len(),
+ };
+ super::lua_pushvalue(state, table);
+ super::lua_pushlightuserdata(state, &s as *const StringArg as *mut c_void);
+ super::lua_rotate(state, -3, 2);
+ protect_lua(state, 3, lua_rawsetfield_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_rawinsert(state: *mut lua_State, table: c_int, i: lua_Integer) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ super::lua_pushinteger(state, i);
+ protect_lua(state, 3, lua_rawinsert_s)
+}
+
+// Uses 4 stack spaces
+pub unsafe fn lua_rawremove(state: *mut lua_State, table: c_int, i: lua_Integer) -> Result<()> {
+ super::lua_pushvalue(state, table);
+ super::lua_pushinteger(state, i);
+ protect_lua(state, 2, lua_rawremove_s)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn luaL_len(state: *mut lua_State, table: c_int) -> Result<lua_Integer> {
+ super::lua_pushvalue(state, table);
+ protect_lua(state, 1, luaL_len_s)?;
+ let ret = super::lua_tointeger(state, -1);
+ super::lua_pop(state, 1);
+ Ok(ret)
+}
+
+// Uses 3 stack spaces
+pub unsafe fn lua_next(state: *mut lua_State, table: c_int) -> Result<lua_Integer> {
+ super::lua_pushvalue(state, table);
+ super::lua_rotate(state, -2, 1);
+ protect_lua(state, 2, lua_next_s)?;
+ let ret = super::lua_tointeger(state, -1);
+ super::lua_pop(state, 1);
+ Ok(ret)
+}
diff --git a/src/ffi/shim/compat-5.3.c b/src/ffi/shim/compat-5.3.c
new file mode 100644
index 0000000..4d7390d
--- /dev/null
+++ b/src/ffi/shim/compat-5.3.c
@@ -0,0 +1,953 @@
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include "compat-5.3.h"
+
+/* don't compile it again if it already is included via compat53.h */
+#ifndef COMPAT53_C_
+#define COMPAT53_C_
+
+
+
+/* definitions for Lua 5.1 only */
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
+
+#ifndef COMPAT53_FOPEN_NO_LOCK
+# if defined(_MSC_VER)
+# define COMPAT53_FOPEN_NO_LOCK 1
+# else /* otherwise */
+# define COMPAT53_FOPEN_NO_LOCK 0
+# endif /* VC++ only so far */
+#endif /* No-lock fopen_s usage if possible */
+
+#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
+# include <share.h>
+#endif /* VC++ _fsopen for share-allowed file read */
+
+#ifndef COMPAT53_HAVE_STRERROR_R
+# if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
+ (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) || \
+ defined(__APPLE__)
+# define COMPAT53_HAVE_STRERROR_R 1
+# else /* none of the defines matched: define to 0 */
+# define COMPAT53_HAVE_STRERROR_R 0
+# endif /* have strerror_r of some form */
+#endif /* strerror_r */
+
+#ifndef COMPAT53_HAVE_STRERROR_S
+# if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
+ defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
+# define COMPAT53_HAVE_STRERROR_S 1
+# else /* not VC++ or C11 */
+# define COMPAT53_HAVE_STRERROR_S 0
+# endif /* strerror_s from VC++ or C11 */
+#endif /* strerror_s */
+
+#ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
+# define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
+#endif /* Lua File Buffer Size */
+
+
+static char* compat53_strerror (int en, char* buff, size_t sz) {
+#if COMPAT53_HAVE_STRERROR_R
+ /* use strerror_r here, because it's available on these specific platforms */
+ if (sz > 0) {
+ buff[0] = '\0';
+ /* we don't care whether the GNU version or the XSI version is used: */
+ if (strerror_r(en, buff, sz)) {
+ /* Yes, we really DO want to ignore the return value!
+ * GCC makes that extra hard, not even a (void) cast will do. */
+ }
+ if (buff[0] == '\0') {
+ /* Buffer is unchanged, so we probably have called GNU strerror_r which
+ * returned a static constant string. Chances are that strerror will
+ * return the same static constant string and therefore be thread-safe. */
+ return strerror(en);
+ }
+ }
+ return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
+#elif COMPAT53_HAVE_STRERROR_S
+ /* for MSVC and other C11 implementations, use strerror_s since it's
+ * provided by default by the libraries */
+ strerror_s(buff, sz, en);
+ return buff;
+#else
+ /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
+ * errno itself and some impls not locking a static buffer for it ... but most
+ * known systems have threadsafe errno: this might only change if the locale
+ * is changed out from under someone while this function is being called */
+ (void)buff;
+ (void)sz;
+ return strerror(en);
+#endif
+}
+
+
+COMPAT53_API int lua_absindex (lua_State *L, int i) {
+ if (i < 0 && i > LUA_REGISTRYINDEX)
+ i += lua_gettop(L) + 1;
+ return i;
+}
+
+
+static void compat53_call_lua (lua_State *L, char const code[], size_t len,
+ int nargs, int nret) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
+ if (lua_type(L, -1) != LUA_TFUNCTION) {
+ lua_pop(L, 1);
+ if (luaL_loadbuffer(L, code, len, "=none"))
+ lua_error(L);
+ lua_pushvalue(L, -1);
+ lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
+ }
+ lua_insert(L, -nargs-1);
+ lua_call(L, nargs, nret);
+}
+
+
+static const char compat53_arith_code[] =
+ "local op,a,b=...\n"
+ "if op==0 then return a+b\n"
+ "elseif op==1 then return a-b\n"
+ "elseif op==2 then return a*b\n"
+ "elseif op==3 then return a/b\n"
+ "elseif op==4 then return a%b\n"
+ "elseif op==5 then return a^b\n"
+ "elseif op==6 then return -a\n"
+ "end\n";
+
+COMPAT53_API void lua_arith (lua_State *L, int op) {
+ if (op < LUA_OPADD || op > LUA_OPUNM)
+ luaL_error(L, "invalid 'op' argument for lua_arith");
+ luaL_checkstack(L, 5, "not enough stack slots");
+ if (op == LUA_OPUNM)
+ lua_pushvalue(L, -1);
+ lua_pushnumber(L, op);
+ lua_insert(L, -3);
+ compat53_call_lua(L, compat53_arith_code,
+ sizeof(compat53_arith_code)-1, 3, 1);
+}
+
+
+static const char compat53_compare_code[] =
+ "local a,b=...\n"
+ "return a<=b\n";
+
+COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op) {
+ int result = 0;
+ switch (op) {
+ case LUA_OPEQ:
+ return lua_equal(L, idx1, idx2);
+ case LUA_OPLT:
+ return lua_lessthan(L, idx1, idx2);
+ case LUA_OPLE:
+ luaL_checkstack(L, 5, "not enough stack slots");
+ idx1 = lua_absindex(L, idx1);
+ idx2 = lua_absindex(L, idx2);
+ lua_pushvalue(L, idx1);
+ lua_pushvalue(L, idx2);
+ compat53_call_lua(L, compat53_compare_code,
+ sizeof(compat53_compare_code)-1, 2, 1);
+ result = lua_toboolean(L, -1);
+ lua_pop(L, 1);
+ return result;
+ default:
+ luaL_error(L, "invalid 'op' argument for lua_compare");
+ }
+ return 0;
+}
+
+
+COMPAT53_API void lua_copy (lua_State *L, int from, int to) {
+ int abs_to = lua_absindex(L, to);
+ luaL_checkstack(L, 1, "not enough stack slots");
+ lua_pushvalue(L, from);
+ lua_replace(L, abs_to);
+}
+
+
+COMPAT53_API void lua_len (lua_State *L, int i) {
+ switch (lua_type(L, i)) {
+ case LUA_TSTRING:
+ lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
+ break;
+ case LUA_TTABLE:
+ if (!luaL_callmeta(L, i, "__len"))
+ lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
+ break;
+ case LUA_TUSERDATA:
+ if (luaL_callmeta(L, i, "__len"))
+ break;
+ /* FALLTHROUGH */
+ default:
+ luaL_error(L, "attempt to get length of a %s value",
+ lua_typename(L, lua_type(L, i)));
+ }
+}
+
+
+COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p) {
+ int abs_i = lua_absindex(L, i);
+ lua_pushlightuserdata(L, (void*)p);
+ lua_rawget(L, abs_i);
+ return lua_type(L, -1);
+}
+
+COMPAT53_API void lua_rawsetp (lua_State *L, int i, const void *p) {
+ int abs_i = lua_absindex(L, i);
+ luaL_checkstack(L, 1, "not enough stack slots");
+ lua_pushlightuserdata(L, (void*)p);
+ lua_insert(L, -2);
+ lua_rawset(L, abs_i);
+}
+
+
+COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
+ lua_Number n = lua_tonumber(L, i);
+ if (isnum != NULL) {
+ *isnum = (n != 0 || lua_isnumber(L, i));
+ }
+ return n;
+}
+
+
+COMPAT53_API void luaL_checkversion (lua_State *L) {
+ (void)L;
+}
+
+
+COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg) {
+ if (!lua_checkstack(L, sp+LUA_MINSTACK)) {
+ if (msg != NULL)
+ luaL_error(L, "stack overflow (%s)", msg);
+ else {
+ lua_pushliteral(L, "stack overflow");
+ lua_error(L);
+ }
+ }
+}
+
+
+COMPAT53_API int luaL_getsubtable (lua_State *L, int i, const char *name) {
+ int abs_i = lua_absindex(L, i);
+ luaL_checkstack(L, 3, "not enough stack slots");
+ lua_pushstring(L, name);
+ lua_gettable(L, abs_i);
+ if (lua_istable(L, -1))
+ return 1;
+ lua_pop(L, 1);
+ lua_newtable(L);
+ lua_pushstring(L, name);
+ lua_pushvalue(L, -2);
+ lua_settable(L, abs_i);
+ return 0;
+}
+
+
+COMPAT53_API lua_Integer luaL_len (lua_State *L, int i) {
+ lua_Integer res = 0;
+ int isnum = 0;
+ luaL_checkstack(L, 1, "not enough stack slots");
+ lua_len(L, i);
+ res = lua_tointegerx(L, -1, &isnum);
+ lua_pop(L, 1);
+ if (!isnum)
+ luaL_error(L, "object length is not an integer");
+ return res;
+}
+
+
+COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
+ luaL_checkstack(L, nup+1, "too many upvalues");
+ for (; l->name != NULL; l++) { /* fill the table with given functions */
+ int i;
+ lua_pushstring(L, l->name);
+ for (i = 0; i < nup; i++) /* copy upvalues to the top */
+ lua_pushvalue(L, -(nup + 1));
+ lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
+ lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
+ }
+ lua_pop(L, nup); /* remove upvalues */
+}
+
+
+COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname) {
+ luaL_checkstack(L, 1, "not enough stack slots");
+ luaL_getmetatable(L, tname);
+ lua_setmetatable(L, -2);
+}
+
+
+COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) {
+ void *p = lua_touserdata(L, i);
+ luaL_checkstack(L, 2, "not enough stack slots");
+ if (p == NULL || !lua_getmetatable(L, i))
+ return NULL;
+ else {
+ int res = 0;
+ luaL_getmetatable(L, tname);
+ res = lua_rawequal(L, -1, -2);
+ lua_pop(L, 2);
+ if (!res)
+ p = NULL;
+ }
+ return p;
+}
+
+
+static int compat53_countlevels (lua_State *L) {
+ lua_Debug ar;
+ int li = 1, le = 1;
+ /* find an upper bound */
+ while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
+ /* do a binary search */
+ while (li < le) {
+ int m = (li + le)/2;
+ if (lua_getstack(L, m, &ar)) li = m + 1;
+ else le = m;
+ }
+ return le - 1;
+}
+
+static int compat53_findfield (lua_State *L, int objidx, int level) {
+ if (level == 0 || !lua_istable(L, -1))
+ return 0; /* not found */
+ lua_pushnil(L); /* start 'next' loop */
+ while (lua_next(L, -2)) { /* for each pair in table */
+ if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
+ if (lua_rawequal(L, objidx, -1)) { /* found object? */
+ lua_pop(L, 1); /* remove value (but keep name) */
+ return 1;
+ }
+ else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
+ lua_remove(L, -2); /* remove table (but keep name) */
+ lua_pushliteral(L, ".");
+ lua_insert(L, -2); /* place '.' between the two names */
+ lua_concat(L, 3);
+ return 1;
+ }
+ }
+ lua_pop(L, 1); /* remove value */
+ }
+ return 0; /* not found */
+}
+
+static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) {
+ int top = lua_gettop(L);
+ lua_getinfo(L, "f", ar); /* push function */
+ lua_pushvalue(L, LUA_GLOBALSINDEX);
+ if (compat53_findfield(L, top + 1, 2)) {
+ lua_copy(L, -1, top + 1); /* move name to proper place */
+ lua_pop(L, 2); /* remove pushed values */
+ return 1;
+ }
+ else {
+ lua_settop(L, top); /* remove function and global table */
+ return 0;
+ }
+}
+
+static void compat53_pushfuncname (lua_State *L, lua_Debug *ar) {
+ if (*ar->namewhat != '\0') /* is there a name? */
+ lua_pushfstring(L, "function " LUA_QS, ar->name);
+ else if (*ar->what == 'm') /* main? */
+ lua_pushliteral(L, "main chunk");
+ else if (*ar->what == 'C') {
+ if (compat53_pushglobalfuncname(L, ar)) {
+ lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
+ lua_remove(L, -2); /* remove name */
+ }
+ else
+ lua_pushliteral(L, "?");
+ }
+ else
+ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
+}
+
+#define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
+#define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
+
+COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1,
+ const char *msg, int level) {
+ lua_Debug ar;
+ int top = lua_gettop(L);
+ int numlevels = compat53_countlevels(L1);
+ int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
+ if (msg) lua_pushfstring(L, "%s\n", msg);
+ lua_pushliteral(L, "stack traceback:");
+ while (lua_getstack(L1, level++, &ar)) {
+ if (level == mark) { /* too many levels? */
+ lua_pushliteral(L, "\n\t..."); /* add a '...' */
+ level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
+ }
+ else {
+ lua_getinfo(L1, "Slnt", &ar);
+ lua_pushfstring(L, "\n\t%s:", ar.short_src);
+ if (ar.currentline > 0)
+ lua_pushfstring(L, "%d:", ar.currentline);
+ lua_pushliteral(L, " in ");
+ compat53_pushfuncname(L, &ar);
+ lua_concat(L, lua_gettop(L) - top);
+ }
+ }
+ lua_concat(L, lua_gettop(L) - top);
+}
+
+
+COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
+ const char *serr = NULL;
+ int en = errno; /* calls to Lua API may change this value */
+ char buf[512] = { 0 };
+ if (stat) {
+ lua_pushboolean(L, 1);
+ return 1;
+ }
+ else {
+ lua_pushnil(L);
+ serr = compat53_strerror(en, buf, sizeof(buf));
+ if (fname)
+ lua_pushfstring(L, "%s: %s", fname, serr);
+ else
+ lua_pushstring(L, serr);
+ lua_pushnumber(L, (lua_Number)en);
+ return 3;
+ }
+}
+
+
+static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) {
+ if (mode && strchr(mode, modename[0]) == NULL) {
+ lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
+ return err;
+ }
+ return LUA_OK;
+}
+
+
+typedef struct {
+ lua_Reader reader;
+ void *ud;
+ int has_peeked_data;
+ const char *peeked_data;
+ size_t peeked_data_size;
+} compat53_reader_data;
+
+
+static const char *compat53_reader (lua_State *L, void *ud, size_t *size) {
+ compat53_reader_data *data = (compat53_reader_data *)ud;
+ if (data->has_peeked_data) {
+ data->has_peeked_data = 0;
+ *size = data->peeked_data_size;
+ return data->peeked_data;
+ } else
+ return data->reader(L, data->ud, size);
+}
+
+
+COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) {
+ int status = LUA_OK;
+ compat53_reader_data compat53_data = { 0, NULL, 1, 0, 0 };
+ compat53_data.reader = reader;
+ compat53_data.ud = data;
+ compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
+ if (compat53_data.peeked_data && compat53_data.peeked_data_size &&
+ compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
+ status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
+ else
+ status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
+ if (status != LUA_OK)
+ return status;
+ /* we need to call the original 5.1 version of lua_load! */
+#undef lua_load
+ return lua_load(L, compat53_reader, &compat53_data, source);
+#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
+}
+
+
+typedef struct {
+ int n; /* number of pre-read characters */
+ FILE *f; /* file being read */
+ char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
+} compat53_LoadF;
+
+
+static const char *compat53_getF (lua_State *L, void *ud, size_t *size) {
+ compat53_LoadF *lf = (compat53_LoadF *)ud;
+ (void)L; /* not used */
+ if (lf->n > 0) { /* are there pre-read characters to be read? */
+ *size = lf->n; /* return them (chars already in buffer) */
+ lf->n = 0; /* no more pre-read characters */
+ }
+ else { /* read a block from file */
+ /* 'fread' can return > 0 *and* set the EOF flag. If next call to
+ 'compat53_getF' called 'fread', it might still wait for user input.
+ The next check avoids this problem. */
+ if (feof(lf->f)) return NULL;
+ *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
+ }
+ return lf->buff;
+}
+
+
+static int compat53_errfile (lua_State *L, const char *what, int fnameindex) {
+ char buf[512] = {0};
+ const char *serr = compat53_strerror(errno, buf, sizeof(buf));
+ const char *filename = lua_tostring(L, fnameindex) + 1;
+ lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
+ lua_remove(L, fnameindex);
+ return LUA_ERRFILE;
+}
+
+
+static int compat53_skipBOM (compat53_LoadF *lf) {
+ const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
+ int c;
+ lf->n = 0;
+ do {
+ c = getc(lf->f);
+ if (c == EOF || c != *(const unsigned char *)p++) return c;
+ lf->buff[lf->n++] = (char)c; /* to be read by the parser */
+ } while (*p != '\0');
+ lf->n = 0; /* prefix matched; discard it */
+ return getc(lf->f); /* return next character */
+}
+
+
+/*
+** reads the first character of file 'f' and skips an optional BOM mark
+** in its beginning plus its first line if it starts with '#'. Returns
+** true if it skipped the first line. In any case, '*cp' has the
+** first "valid" character of the file (after the optional BOM and
+** a first-line comment).
+*/
+static int compat53_skipcomment (compat53_LoadF *lf, int *cp) {
+ int c = *cp = compat53_skipBOM(lf);
+ if (c == '#') { /* first line is a comment (Unix exec. file)? */
+ do { /* skip first line */
+ c = getc(lf->f);
+ } while (c != EOF && c != '\n');
+ *cp = getc(lf->f); /* skip end-of-line, if present */
+ return 1; /* there was a comment */
+ }
+ else return 0; /* no comment */
+}
+
+
+COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) {
+ compat53_LoadF lf;
+ int status, readstatus;
+ int c;
+ int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
+ if (filename == NULL) {
+ lua_pushliteral(L, "=stdin");
+ lf.f = stdin;
+ }
+ else {
+ lua_pushfstring(L, "@%s", filename);
+#if defined(_MSC_VER)
+ /* This code is here to stop a deprecation error that stops builds
+ * if a certain macro is defined. While normally not caring would
+ * be best, some header-only libraries and builds can't afford to
+ * dictate this to the user. A quick check shows that fopen_s this
+ * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
+ * possibly even before that so we don't need to do any version
+ * number checks, since this has been there since forever. */
+
+ /* TO USER: if you want the behavior of typical fopen_s/fopen,
+ * which does lock the file on VC++, define the macro used below to 0 */
+#if COMPAT53_FOPEN_NO_LOCK
+ lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
+ if (lf.f == NULL)
+ return compat53_errfile(L, "open", fnameindex);
+#else /* use default locking version */
+ if (fopen_s(&lf.f, filename, "r") != 0)
+ return compat53_errfile(L, "open", fnameindex);
+#endif /* Locking vs. No-locking fopen variants */
+#else
+ lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
+ if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
+#endif
+ }
+ if (compat53_skipcomment(&lf, &c)) /* read initial portion */
+ lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
+ if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
+#if defined(_MSC_VER)
+ if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
+ return compat53_errfile(L, "reopen", fnameindex);
+#else
+ lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
+ if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex);
+#endif
+ compat53_skipcomment(&lf, &c); /* re-read initial portion */
+ }
+ if (c != EOF)
+ lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
+ status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
+ readstatus = ferror(lf.f);
+ if (filename) fclose(lf.f); /* close file (even in case of errors) */
+ if (readstatus) {
+ lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
+ return compat53_errfile(L, "read", fnameindex);
+ }
+ lua_remove(L, fnameindex);
+ return status;
+}
+
+
+COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) {
+ int status = LUA_OK;
+ if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
+ status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
+ }
+ else {
+ status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
+ }
+ if (status != LUA_OK)
+ return status;
+ return luaL_loadbuffer(L, buff, sz, name);
+}
+
+
+#if !defined(l_inspectstat) && \
+ (defined(unix) || defined(__unix) || defined(__unix__) || \
+ defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \
+ (defined(__APPLE__) && defined(__MACH__)))
+/* some form of unix; check feature macros in unistd.h for details */
+# include <unistd.h>
+/* check posix version; the relevant include files and macros probably
+ * were available before 2001, but I'm not sure */
+# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
+# include <sys/wait.h>
+# define l_inspectstat(stat,what) \
+ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
+ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
+# endif
+#endif
+
+/* provide default (no-op) version */
+#if !defined(l_inspectstat)
+# define l_inspectstat(stat,what) ((void)0)
+#endif
+
+
+COMPAT53_API int luaL_execresult (lua_State *L, int stat) {
+ const char *what = "exit";
+ if (stat == -1)
+ return luaL_fileresult(L, 0, NULL);
+ else {
+ l_inspectstat(stat, what);
+ if (*what == 'e' && stat == 0)
+ lua_pushboolean(L, 1);
+ else
+ lua_pushnil(L);
+ lua_pushstring(L, what);
+ lua_pushinteger(L, stat);
+ return 3;
+ }
+}
+
+
+COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B) {
+ /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
+ B->b.p = NULL;
+ B->b.L = NULL;
+ B->b.lvl = 0;
+ /* reuse the buffer from the 5.1-style luaL_Buffer though! */
+ B->ptr = B->b.buffer;
+ B->capacity = LUAL_BUFFERSIZE;
+ B->nelems = 0;
+ B->L2 = L;
+}
+
+
+COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s) {
+ if (B->capacity - B->nelems < s) { /* needs to grow */
+ char* newptr = NULL;
+ size_t newcap = B->capacity * 2;
+ if (newcap - B->nelems < s)
+ newcap = B->nelems + s;
+ if (newcap < B->capacity) /* overflow */
+ luaL_error(B->L2, "buffer too large");
+ newptr = (char*)lua_newuserdata(B->L2, newcap);
+ memcpy(newptr, B->ptr, B->nelems);
+ if (B->ptr != B->b.buffer)
+ lua_replace(B->L2, -2); /* remove old buffer */
+ B->ptr = newptr;
+ B->capacity = newcap;
+ }
+ return B->ptr+B->nelems;
+}
+
+
+COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l) {
+ memcpy(luaL_prepbuffsize(B, l), s, l);
+ luaL_addsize(B, l);
+}
+
+
+COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B) {
+ size_t len = 0;
+ const char *s = lua_tolstring(B->L2, -1, &len);
+ if (!s)
+ luaL_error(B->L2, "cannot convert value to string");
+ if (B->ptr != B->b.buffer)
+ lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
+ luaL_addlstring(B, s, len);
+ lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
+}
+
+
+void luaL_pushresult (luaL_Buffer_53 *B) {
+ lua_pushlstring(B->L2, B->ptr, B->nelems);
+ if (B->ptr != B->b.buffer)
+ lua_replace(B->L2, -2); /* remove userdata buffer */
+}
+
+
+#endif /* Lua 5.1 */
+
+
+
+/* definitions for Lua 5.1 and Lua 5.2 */
+#if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502
+
+
+COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i) {
+ index = lua_absindex(L, index);
+ lua_pushinteger(L, i);
+ lua_gettable(L, index);
+ return lua_type(L, -1);
+}
+
+
+#ifndef LUA_EXTRASPACE
+#define LUA_EXTRASPACE (sizeof(void*))
+#endif
+
+COMPAT53_API void *lua_getextraspace (lua_State *L) {
+ int is_main = 0;
+ void *ptr = NULL;
+ luaL_checkstack(L, 4, "not enough stack slots available");
+ lua_pushliteral(L, "__compat53_extraspace");
+ lua_pushvalue(L, -1);
+ lua_rawget(L, LUA_REGISTRYINDEX);
+ if (!lua_istable(L, -1)) {
+ lua_pop(L, 1);
+ lua_createtable(L, 0, 2);
+ lua_createtable(L, 0, 1);
+ lua_pushliteral(L, "k");
+ lua_setfield(L, -2, "__mode");
+ lua_setmetatable(L, -2);
+ lua_pushvalue(L, -2);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, LUA_REGISTRYINDEX);
+ }
+ lua_replace(L, -2);
+ is_main = lua_pushthread(L);
+ lua_rawget(L, -2);
+ ptr = lua_touserdata(L, -1);
+ if (!ptr) {
+ lua_pop(L, 1);
+ ptr = lua_newuserdata(L, LUA_EXTRASPACE);
+ if (is_main) {
+ memset(ptr, '\0', LUA_EXTRASPACE);
+ lua_pushthread(L);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, -4);
+ lua_pushboolean(L, 1);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, -4);
+ } else {
+ void* mptr = NULL;
+ lua_pushboolean(L, 1);
+ lua_rawget(L, -3);
+ mptr = lua_touserdata(L, -1);
+ if (mptr)
+ memcpy(ptr, mptr, LUA_EXTRASPACE);
+ else
+ memset(ptr, '\0', LUA_EXTRASPACE);
+ lua_pop(L, 1);
+ lua_pushthread(L);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, -4);
+ }
+ }
+ lua_pop(L, 2);
+ return ptr;
+}
+
+
+COMPAT53_API int lua_isinteger (lua_State *L, int index) {
+ if (lua_type(L, index) == LUA_TNUMBER) {
+ lua_Number n = lua_tonumber(L, index);
+ lua_Integer i = lua_tointeger(L, index);
+ if (i == n)
+ return 1;
+ }
+ return 0;
+}
+
+
+COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
+ int ok = 0;
+ lua_Number n = lua_tonumberx(L, i, &ok);
+ if (ok) {
+ if (n == (lua_Integer)n) {
+ if (isnum)
+ *isnum = 1;
+ return (lua_Integer)n;
+ }
+ }
+ if (isnum)
+ *isnum = 0;
+ return 0;
+}
+
+
+static void compat53_reverse (lua_State *L, int a, int b) {
+ for (; a < b; ++a, --b) {
+ lua_pushvalue(L, a);
+ lua_pushvalue(L, b);
+ lua_replace(L, a);
+ lua_replace(L, b);
+ }
+}
+
+
+COMPAT53_API void lua_rotate (lua_State *L, int idx, int n) {
+ int n_elems = 0;
+ idx = lua_absindex(L, idx);
+ n_elems = lua_gettop(L)-idx+1;
+ if (n < 0)
+ n += n_elems;
+ if ( n > 0 && n < n_elems) {
+ luaL_checkstack(L, 2, "not enough stack slots available");
+ n = n_elems - n;
+ compat53_reverse(L, idx, idx+n-1);
+ compat53_reverse(L, idx+n, idx+n_elems-1);
+ compat53_reverse(L, idx, idx+n_elems-1);
+ }
+}
+
+
+COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i) {
+ luaL_checkstack(L, 1, "not enough stack slots available");
+ index = lua_absindex(L, index);
+ lua_pushinteger(L, i);
+ lua_insert(L, -2);
+ lua_settable(L, index);
+}
+
+
+#if !defined(lua_str2number)
+# define lua_str2number(s, p) strtod((s), (p))
+#endif
+
+COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) {
+ char* endptr;
+ lua_Number n = lua_str2number(s, &endptr);
+ if (endptr != s) {
+ while (*endptr != '\0' && isspace((unsigned char)*endptr))
+ ++endptr;
+ if (*endptr == '\0') {
+ lua_pushnumber(L, n);
+ return endptr - s + 1;
+ }
+ }
+ return 0;
+}
+
+
+COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
+ if (!luaL_callmeta(L, idx, "__tostring")) {
+ int t = lua_type(L, idx), tt = 0;
+ char const* name = NULL;
+ switch (t) {
+ case LUA_TNIL:
+ lua_pushliteral(L, "nil");
+ break;
+ case LUA_TSTRING:
+ case LUA_TNUMBER:
+ lua_pushvalue(L, idx);
+ break;
+ case LUA_TBOOLEAN:
+ if (lua_toboolean(L, idx))
+ lua_pushliteral(L, "true");
+ else
+ lua_pushliteral(L, "false");
+ break;
+ default:
+ tt = luaL_getmetafield(L, idx, "__name");
+ name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
+ lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
+ if (tt != LUA_TNIL)
+ lua_replace(L, -2);
+ break;
+ }
+ } else {
+ if (!lua_isstring(L, -1))
+ luaL_error(L, "'__tostring' must return a string");
+ }
+ return lua_tolstring(L, -1, len);
+}
+
+
+COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
+ lua_CFunction openf, int glb) {
+ luaL_checkstack(L, 3, "not enough stack slots available");
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
+ if (lua_getfield(L, -1, modname) == LUA_TNIL) {
+ lua_pop(L, 1);
+ lua_pushcfunction(L, openf);
+ lua_pushstring(L, modname);
+#ifndef COMPAT53_LUAJIT
+ lua_call(L, 1, 1);
+ lua_pushvalue(L, -1);
+ lua_setfield(L, -3, modname);
+#else
+ lua_call(L, 1, 0);
+ lua_getfield(L, -1, modname);
+#endif /* COMPAT53_LUAJIT */
+ }
+ if (glb) {
+ lua_pushvalue(L, -1);
+ lua_setglobal(L, modname);
+ }
+ lua_replace(L, -2);
+}
+
+
+#endif /* Lua 5.1 and 5.2 */
+
+
+#endif /* COMPAT53_C_ */
+
+
+/*********************************************************************
+* This file contains parts of Lua 5.2's and Lua 5.3's source code:
+*
+* Copyright (C) 1994-2014 Lua.org, PUC-Rio.
+*
+* Permission is hereby granted, free of charge, to any person obtaining
+* a copy of this software and associated documentation files (the
+* "Software"), to deal in the Software without restriction, including
+* without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to
+* permit persons to whom the Software is furnished to do so, subject to
+* the following conditions:
+*
+* The above copyright notice and this permission notice shall be
+* included in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*********************************************************************/
+
diff --git a/src/ffi/shim/compat-5.3.h b/src/ffi/shim/compat-5.3.h
new file mode 100644
index 0000000..b730a4b
--- /dev/null
+++ b/src/ffi/shim/compat-5.3.h
@@ -0,0 +1,424 @@
+#ifndef COMPAT53_H_
+#define COMPAT53_H_
+
+#include <stddef.h>
+#include <limits.h>
+#include <string.h>
+#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
+extern "C" {
+#endif
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
+}
+#endif
+
+
+#undef COMPAT53_INCLUDE_SOURCE
+#if defined(COMPAT53_PREFIX)
+/* - change the symbol names of functions to avoid linker conflicts
+ * - compat-5.3.c needs to be compiled (and linked) separately
+ */
+# if !defined(COMPAT53_API)
+# define COMPAT53_API extern
+# endif
+#else /* COMPAT53_PREFIX */
+/* - make all functions static and include the source.
+ * - compat-5.3.c doesn't need to be compiled (and linked) separately
+ */
+# define COMPAT53_PREFIX compat53
+# undef COMPAT53_API
+# if defined(__GNUC__) || defined(__clang__)
+# define COMPAT53_API __attribute__((__unused__)) static
+# else
+# define COMPAT53_API static
+# endif
+# define COMPAT53_INCLUDE_SOURCE
+#endif /* COMPAT53_PREFIX */
+
+#define COMPAT53_CONCAT_HELPER(a, b) a##b
+#define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
+
+
+
+/* declarations for Lua 5.1 */
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
+
+/* XXX not implemented:
+ * lua_arith (new operators)
+ * lua_upvalueid
+ * lua_upvaluejoin
+ * lua_version
+ * lua_yieldk
+ */
+
+#ifndef LUA_OK
+# define LUA_OK 0
+#endif
+#ifndef LUA_OPADD
+# define LUA_OPADD 0
+#endif
+#ifndef LUA_OPSUB
+# define LUA_OPSUB 1
+#endif
+#ifndef LUA_OPMUL
+# define LUA_OPMUL 2
+#endif
+#ifndef LUA_OPDIV
+# define LUA_OPDIV 3
+#endif
+#ifndef LUA_OPMOD
+# define LUA_OPMOD 4
+#endif
+#ifndef LUA_OPPOW
+# define LUA_OPPOW 5
+#endif
+#ifndef LUA_OPUNM
+# define LUA_OPUNM 6
+#endif
+#ifndef LUA_OPEQ
+# define LUA_OPEQ 0
+#endif
+#ifndef LUA_OPLT
+# define LUA_OPLT 1
+#endif
+#ifndef LUA_OPLE
+# define LUA_OPLE 2
+#endif
+
+/* LuaJIT/Lua 5.1 does not have the updated
+ * error codes for thread status/function returns (but some patched versions do)
+ * define it only if it's not found
+ */
+#if !defined(LUA_ERRGCMM)
+/* Use + 2 because in some versions of Lua (Lua 5.1)
+ * LUA_ERRFILE is defined as (LUA_ERRERR+1)
+ * so we need to avoid it (LuaJIT might have something at this
+ * integer value too)
+ */
+# define LUA_ERRGCMM (LUA_ERRERR + 2)
+#endif /* LUA_ERRGCMM define */
+
+typedef size_t lua_Unsigned;
+
+typedef struct luaL_Buffer_53 {
+ luaL_Buffer b; /* make incorrect code crash! */
+ char *ptr;
+ size_t nelems;
+ size_t capacity;
+ lua_State *L2;
+} luaL_Buffer_53;
+#define luaL_Buffer luaL_Buffer_53
+
+/* In PUC-Rio 5.1, userdata is a simple FILE*
+ * In LuaJIT, it's a struct where the first member is a FILE*
+ * We can't support the `closef` member
+ */
+typedef struct luaL_Stream {
+ FILE *f;
+} luaL_Stream;
+
+#define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
+COMPAT53_API int lua_absindex (lua_State *L, int i);
+
+#define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
+COMPAT53_API void lua_arith (lua_State *L, int op);
+
+#define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
+COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op);
+
+#define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
+COMPAT53_API void lua_copy (lua_State *L, int from, int to);
+
+#define lua_getuservalue(L, i) \
+ (lua_getfenv((L), (i)), lua_type((L), -1))
+#define lua_setuservalue(L, i) \
+ (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
+
+#define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
+COMPAT53_API void lua_len (lua_State *L, int i);
+
+#define lua_pushstring(L, s) \
+ (lua_pushstring((L), (s)), lua_tostring((L), -1))
+
+#define lua_pushlstring(L, s, len) \
+ ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
+
+#ifndef luaL_newlibtable
+# define luaL_newlibtable(L, l) \
+ (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
+#endif
+#ifndef luaL_newlib
+# define luaL_newlib(L, l) \
+ (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
+#endif
+
+#define lua_pushglobaltable(L) \
+ lua_pushvalue((L), LUA_GLOBALSINDEX)
+
+#define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
+COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p);
+
+#define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
+COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
+
+#define lua_rawlen(L, i) lua_objlen((L), (i))
+
+#define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
+
+#define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
+COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum);
+
+#define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
+COMPAT53_API void luaL_checkversion (lua_State *L);
+
+#define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
+COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode);
+
+#define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
+COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode);
+
+#define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
+COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
+
+#define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
+COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg);
+
+#define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
+COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name);
+
+#define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
+COMPAT53_API lua_Integer luaL_len (lua_State *L, int i);
+
+#define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
+COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
+
+#define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
+COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname);
+
+#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
+COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname);
+
+#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
+COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);
+
+#define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
+COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname);
+
+#define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
+COMPAT53_API int luaL_execresult (lua_State *L, int stat);
+
+#define lua_callk(L, na, nr, ctx, cont) \
+ ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
+#define lua_pcallk(L, na, nr, err, ctx, cont) \
+ ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
+
+#define lua_resume(L, from, nargs) \
+ ((void)(from), lua_resume((L), (nargs)))
+
+#define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
+COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B);
+
+#define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
+COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s);
+
+#define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
+COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l);
+
+#define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
+COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B);
+
+#define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
+COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B);
+
+#undef luaL_buffinitsize
+#define luaL_buffinitsize(L, B, s) \
+ (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
+
+#undef luaL_prepbuffer
+#define luaL_prepbuffer(B) \
+ luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
+
+#undef luaL_addchar
+#define luaL_addchar(B, c) \
+ ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
+ ((B)->ptr[(B)->nelems++] = (c)))
+
+#undef luaL_addsize
+#define luaL_addsize(B, s) \
+ ((B)->nelems += (s))
+
+#undef luaL_addstring
+#define luaL_addstring(B, s) \
+ luaL_addlstring((B), (s), strlen((s)))
+
+#undef luaL_pushresultsize
+#define luaL_pushresultsize(B, s) \
+ (luaL_addsize((B), (s)), luaL_pushresult((B)))
+
+#if defined(LUA_COMPAT_APIINTCASTS)
+#define lua_pushunsigned(L, n) \
+ lua_pushinteger((L), (lua_Integer)(n))
+#define lua_tounsignedx(L, i, is) \
+ ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
+#define lua_tounsigned(L, i) \
+ lua_tounsignedx((L), (i), NULL)
+#define luaL_checkunsigned(L, a) \
+ ((lua_Unsigned)luaL_checkinteger((L), (a)))
+#define luaL_optunsigned(L, a, d) \
+ ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
+#endif
+
+#endif /* Lua 5.1 only */
+
+
+
+/* declarations for Lua 5.1 and 5.2 */
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
+
+typedef int lua_KContext;
+
+typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
+
+#define lua_dump(L, w, d, s) \
+ ((void)(s), lua_dump((L), (w), (d)))
+
+#define lua_getfield(L, i, k) \
+ (lua_getfield((L), (i), (k)), lua_type((L), -1))
+
+#define lua_gettable(L, i) \
+ (lua_gettable((L), (i)), lua_type((L), -1))
+
+#define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
+COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i);
+
+#define lua_getextraspace COMPAT53_CONCAT(COMPAT53_PREFIX, _getextraspace)
+COMPAT53_API void *lua_getextraspace (lua_State *L);
+
+#define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
+COMPAT53_API int lua_isinteger (lua_State *L, int index);
+
+#define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
+COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum);
+
+#define lua_numbertointeger(n, p) \
+ ((*(p) = (lua_Integer)(n)), 1)
+
+#define lua_rawget(L, i) \
+ (lua_rawget((L), (i)), lua_type((L), -1))
+
+#define lua_rawgeti(L, i, n) \
+ (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
+
+#define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
+COMPAT53_API void lua_rotate (lua_State *L, int idx, int n);
+
+#define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
+COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i);
+
+#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
+COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s);
+
+#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
+COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
+
+#define luaL_getmetafield(L, o, e) \
+ (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
+
+#define luaL_newmetatable(L, tn) \
+ (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
+
+#define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
+COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
+ lua_CFunction openf, int glb );
+
+#endif /* Lua 5.1 and Lua 5.2 */
+
+
+
+/* declarations for Lua 5.2 */
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
+
+/* XXX not implemented:
+ * lua_isyieldable
+ * lua_arith (new operators)
+ * lua_pushfstring (new formats)
+ */
+
+#define lua_getglobal(L, n) \
+ (lua_getglobal((L), (n)), lua_type((L), -1))
+
+#define lua_getuservalue(L, i) \
+ (lua_getuservalue((L), (i)), lua_type((L), -1))
+
+#define lua_pushlstring(L, s, len) \
+ (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
+
+#define lua_rawgetp(L, i, p) \
+ (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
+
+#define LUA_KFUNCTION(_name) \
+ static int (_name)(lua_State *L, int status, lua_KContext ctx); \
+ static int (_name ## _52)(lua_State *L) { \
+ lua_KContext ctx; \
+ int status = lua_getctx(L, &ctx); \
+ return (_name)(L, status, ctx); \
+ } \
+ static int (_name)(lua_State *L, int status, lua_KContext ctx)
+
+#define lua_pcallk(L, na, nr, err, ctx, cont) \
+ lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
+
+#define lua_callk(L, na, nr, ctx, cont) \
+ lua_callk((L), (na), (nr), (ctx), cont ## _52)
+
+#define lua_yieldk(L, nr, ctx, cont) \
+ lua_yieldk((L), (nr), (ctx), cont ## _52)
+
+#ifdef lua_call
+# undef lua_call
+# define lua_call(L, na, nr) \
+ (lua_callk)((L), (na), (nr), 0, NULL)
+#endif
+
+#ifdef lua_pcall
+# undef lua_pcall
+# define lua_pcall(L, na, nr, err) \
+ (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
+#endif
+
+#ifdef lua_yield
+# undef lua_yield
+# define lua_yield(L, nr) \
+ (lua_yieldk)((L), (nr), 0, NULL)
+#endif
+
+#endif /* Lua 5.2 only */
+
+
+
+/* other Lua versions */
+#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
+
+# error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
+
+#endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */
+
+
+
+/* helper macro for defining continuation functions (for every version
+ * *except* Lua 5.2) */
+#ifndef LUA_KFUNCTION
+#define LUA_KFUNCTION(_name) \
+ static int (_name)(lua_State *L, int status, lua_KContext ctx)
+#endif
+
+
+#if defined(COMPAT53_INCLUDE_SOURCE)
+# include "compat-5.3.c"
+#endif
+
+
+#endif /* COMPAT53_H_ */
+
diff --git a/src/ffi/shim/shim.c b/src/ffi/shim/shim.c
new file mode 100644
index 0000000..7c16f52
--- /dev/null
+++ b/src/ffi/shim/shim.c
@@ -0,0 +1,432 @@
+// The MIT License (MIT)
+//
+// Copyright (c) 2019-2021 A. Orlenko
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#include <lauxlib.h>
+#include <lua.h>
+
+#include "compat-5.3.h"
+
+size_t MLUA_WRAPPED_ERROR_SIZE = 0;
+size_t MLUA_WRAPPED_PANIC_SIZE = 0;
+
+const void *MLUA_WRAPPED_ERROR_KEY = NULL;
+const void *MLUA_WRAPPED_PANIC_KEY = NULL;
+
+extern void wrapped_error_traceback(lua_State *L, int error_idx, void *error_ud,
+ int has_traceback);
+
+extern int mlua_hook_proc(lua_State *L, lua_Debug *ar);
+
+#define max(a, b) (a > b ? a : b)
+
+typedef struct {
+ const char *data;
+ size_t len;
+} StringArg;
+
+// A wrapper around Rust function to protect from triggering longjmp in Rust.
+// Rust callback expected to return -1 in case of errors or number of output
+// values.
+static int lua_call_rust(lua_State *L) {
+ int nargs = lua_gettop(L);
+
+ // We need one extra stack space to store preallocated memory, and at least 2
+ // stack spaces overall for handling error metatables in rust fn
+ int extra_stack = 1;
+ if (nargs < 2) {
+ extra_stack = 2 - nargs;
+ }
+
+ luaL_checkstack(L, extra_stack,
+ "not enough stack space for callback error handling");
+
+ // We cannot shadow rust errors with Lua ones, we pre-allocate enough memory
+ // to store a wrapped error or panic *before* we proceed.
+ lua_newuserdata(L, max(MLUA_WRAPPED_ERROR_SIZE, MLUA_WRAPPED_PANIC_SIZE));
+ lua_rotate(L, 1, 1);
+
+ lua_CFunction rust_callback = lua_touserdata(L, lua_upvalueindex(1));
+
+ int ret = rust_callback(L);
+ if (ret == -1) {
+ lua_error(L);
+ }
+
+ return ret;
+}
+
+void lua_call_mlua_hook_proc(lua_State *L, lua_Debug *ar) {
+ luaL_checkstack(L, 2, "not enough stack space for callback error handling");
+ lua_newuserdata(L, max(MLUA_WRAPPED_ERROR_SIZE, MLUA_WRAPPED_PANIC_SIZE));
+ lua_rotate(L, 1, 1);
+ int ret = mlua_hook_proc(L, ar);
+ if (ret == -1) {
+ lua_error(L);
+ }
+}
+
+static inline lua_Integer lua_popinteger(lua_State *L) {
+ lua_Integer index = lua_tointeger(L, -1);
+ lua_pop(L, 1);
+ return index;
+}
+
+//
+// Common functions
+//
+
+int lua_gc_s(lua_State *L) {
+ int data = lua_popinteger(L);
+ int what = lua_popinteger(L);
+ int ret = lua_gc(L, what, data);
+ lua_pushinteger(L, ret);
+ return 1;
+}
+
+int luaL_ref_s(lua_State *L) {
+ int ret = luaL_ref(L, -2);
+ lua_pushinteger(L, ret);
+ return 1;
+}
+
+int lua_pushlstring_s(lua_State *L) {
+ StringArg *s = lua_touserdata(L, -1);
+ lua_pop(L, 1);
+ lua_pushlstring(L, s->data, s->len);
+ return 1;
+}
+
+int lua_tolstring_s(lua_State *L) {
+ void *len = lua_touserdata(L, -1);
+ lua_pop(L, 1);
+ const char *s = lua_tolstring(L, -1, len);
+ lua_pushlightuserdata(L, (void *)s);
+ return 2;
+}
+
+int lua_newthread_s(lua_State *L) {
+ lua_newthread(L);
+ return 1;
+}
+
+int lua_newuserdata_s(lua_State *L) {
+ size_t size = lua_tointeger(L, -1);
+ lua_pop(L, 1);
+ lua_newuserdata(L, size);
+ return 1;
+}
+
+int lua_pushcclosure_s(lua_State *L) {
+ lua_CFunction fn = lua_touserdata(L, -2);
+ lua_Integer n = lua_tointeger(L, -1);
+ lua_pop(L, 2);
+ lua_pushcclosure(L, fn, n);
+ return 1;
+}
+
+int lua_pushrclosure_s(lua_State *L) {
+ lua_Integer n = lua_popinteger(L);
+ lua_pushcclosure(L, lua_call_rust, n);
+ return 1;
+}
+
+int luaL_requiref_s(lua_State *L) {
+ const char *modname = lua_touserdata(L, -3);
+ lua_CFunction openf = lua_touserdata(L, -2);
+ int glb = lua_tointeger(L, -1);
+ lua_pop(L, 3);
+ luaL_requiref(L, modname, openf, glb);
+ return 1;
+}
+
+//
+// Table functions
+//
+
+int lua_createtable_s(lua_State *L) {
+ int nrec = lua_popinteger(L);
+ int narr = lua_popinteger(L);
+ lua_createtable(L, narr, nrec);
+ return 1;
+}
+
+int lua_gettable_s(lua_State *L) {
+ lua_gettable(L, -2);
+ return 1;
+}
+
+int lua_settable_s(lua_State *L) {
+ lua_settable(L, -3);
+ return 0;
+}
+
+int lua_geti_s(lua_State *L) {
+ lua_Integer index = lua_popinteger(L);
+ lua_geti(L, -1, index);
+ return 1;
+}
+
+int lua_rawset_s(lua_State *L) {
+ lua_rawset(L, -3);
+ return 0;
+}
+
+int lua_rawseti_s(lua_State *L) {
+ lua_Integer index = lua_popinteger(L);
+ lua_rawseti(L, -2, index);
+ return 0;
+}
+
+int lua_rawsetp_s(lua_State *L) {
+ void *p = lua_touserdata(L, -1);
+ lua_pop(L, 1);
+ lua_rawsetp(L, -2, p);
+ return 0;
+}
+
+int lua_rawsetfield_s(lua_State *L) {
+ StringArg *s = lua_touserdata(L, -2);
+ lua_pushlstring(L, s->data, s->len);
+ lua_replace(L, -3);
+ lua_rawset(L, -3);
+ return 0;
+}
+
+int lua_rawinsert_s(lua_State *L) {
+ lua_Integer index = lua_popinteger(L);
+ lua_Integer size = lua_rawlen(L, -2);
+
+ for (lua_Integer i = size; i >= index; i--) {
+ // table[i+1] = table[i]
+ lua_rawgeti(L, -2, i);
+ lua_rawseti(L, -3, i + 1);
+ }
+ lua_rawseti(L, -2, index);
+
+ return 0;
+}
+
+int lua_rawremove_s(lua_State *L) {
+ lua_Integer index = lua_popinteger(L);
+ lua_Integer size = lua_rawlen(L, -1);
+
+ for (lua_Integer i = index; i < size; i++) {
+ lua_rawgeti(L, -1, i + 1);
+ lua_rawseti(L, -2, i);
+ }
+ lua_pushnil(L);
+ lua_rawseti(L, -2, size);
+
+ return 0;
+}
+
+int luaL_len_s(lua_State *L) {
+ lua_pushinteger(L, luaL_len(L, -1));
+ return 1;
+}
+
+int lua_next_s(lua_State *L) {
+ int ret = lua_next(L, -2);
+ lua_pushinteger(L, ret);
+ return ret == 0 ? 1 : 3;
+}
+
+//
+// Moved from Rust to C
+//
+
+// Wrapper to lookup in `field_getters` first, then `methods`, ending
+// original `__index`. Used only if `field_getters` or `methods` set.
+int meta_index_impl(lua_State *state) {
+ // stack: self, key
+ luaL_checkstack(state, 2, NULL);
+
+ // lookup in `field_getters` table
+ if (lua_isnil(state, lua_upvalueindex(2)) == 0) {
+ lua_pushvalue(state, -1); // `key` arg
+ if (lua_rawget(state, lua_upvalueindex(2)) != LUA_TNIL) {
+ lua_insert(state, -3); // move function
+ lua_pop(state, 1); // remove `key`
+ lua_call(state, 1, 1);
+ return 1;
+ }
+ lua_pop(state, 1); // pop the nil value
+ }
+ // lookup in `methods` table
+ if (lua_isnil(state, lua_upvalueindex(3)) == 0) {
+ lua_pushvalue(state, -1); // `key` arg
+ if (lua_rawget(state, lua_upvalueindex(3)) != LUA_TNIL) {
+ lua_insert(state, -3);
+ lua_pop(state, 2);
+ return 1;
+ }
+ lua_pop(state, 1); // pop the nil value
+ }
+
+ // lookup in `__index`
+ lua_pushvalue(state, lua_upvalueindex(1));
+ switch (lua_type(state, -1)) {
+ case LUA_TNIL:
+ lua_pop(state, 1); // pop the nil value
+ const char *field = lua_tostring(state, -1);
+ luaL_error(state, "attempt to get an unknown field '%s'", field);
+ break;
+
+ case LUA_TTABLE:
+ lua_insert(state, -2);
+ lua_gettable(state, -2);
+ break;
+
+ case LUA_TFUNCTION:
+ lua_insert(state, -3);
+ lua_call(state, 2, 1);
+ break;
+ }
+
+ return 1;
+}
+
+// Similar to `meta_index_impl`, checks `field_setters` table first, then
+// `__newindex` metamethod. Used only if `field_setters` set.
+int meta_newindex_impl(lua_State *state) {
+ // stack: self, key, value
+ luaL_checkstack(state, 2, NULL);
+
+ // lookup in `field_setters` table
+ lua_pushvalue(state, -2); // `key` arg
+ if (lua_rawget(state, lua_upvalueindex(2)) != LUA_TNIL) {
+ lua_remove(state, -3); // remove `key`
+ lua_insert(state, -3); // move function
+ lua_call(state, 2, 0);
+ return 0;
+ }
+ lua_pop(state, 1); // pop the nil value
+
+ // lookup in `__newindex`
+ lua_pushvalue(state, lua_upvalueindex(1));
+ switch (lua_type(state, -1)) {
+ case LUA_TNIL:
+ lua_pop(state, 1); // pop the nil value
+ const char *field = lua_tostring(state, -2);
+ luaL_error(state, "attempt to set an unknown field '%s'", field);
+ break;
+
+ case LUA_TTABLE:
+ lua_insert(state, -3);
+ lua_settable(state, -3);
+ break;
+
+ case LUA_TFUNCTION:
+ lua_insert(state, -4);
+ lua_call(state, 3, 0);
+ break;
+ }
+
+ return 0;
+}
+
+// See function::bind
+int bind_call_impl(lua_State *state) {
+ int nargs = lua_gettop(state);
+ int nbinds = lua_tointeger(state, lua_upvalueindex(2));
+ luaL_checkstack(state, nbinds + 2, NULL);
+
+ lua_settop(state, nargs + nbinds + 1);
+ lua_rotate(state, -(nargs + nbinds + 1), nbinds + 1);
+
+ lua_pushvalue(state, lua_upvalueindex(1));
+ lua_replace(state, 1);
+
+ for (int i = 0; i < nbinds; i++) {
+ lua_pushvalue(state, lua_upvalueindex(i + 3));
+ lua_replace(state, i + 2);
+ }
+
+ lua_call(state, nargs + nbinds, LUA_MULTRET);
+ return lua_gettop(state);
+}
+
+// Returns 1 if a value at index `index` is a special wrapped struct identified
+// by `key`
+int is_wrapped_struct(lua_State *state, int index, const void *key) {
+ if (key == NULL) {
+ // Not yet initialized?
+ return 0;
+ }
+
+ void *ud = lua_touserdata(state, index);
+ if (ud == NULL || lua_getmetatable(state, index) == 0) {
+ return 0;
+ }
+ lua_rawgetp(state, LUA_REGISTRYINDEX, key);
+ int res = lua_rawequal(state, -1, -2);
+ lua_pop(state, 2);
+ return res;
+}
+
+// Takes an error at the top of the stack, and if it is a WrappedError, converts
+// it to an Error::CallbackError with a traceback, if it is some lua type,
+// prints the error along with a traceback, and if it is a WrappedPanic, does
+// not modify it. This function does its best to avoid triggering another error
+// and shadowing previous rust errors, but it may trigger Lua errors that shadow
+// rust errors under certain memory conditions. This function ensures that such
+// behavior will *never* occur with a rust panic, however.
+int error_traceback(lua_State *state) {
+ // I believe luaL_traceback < 5.4 requires this much free stack to not error.
+ // 5.4 uses luaL_Buffer
+ const int LUA_TRACEBACK_STACK = 11;
+
+ if (lua_checkstack(state, 2) == 0) {
+ // If we don't have enough stack space to even check the error type, do
+ // nothing so we don't risk shadowing a rust panic.
+ return 1;
+ }
+
+ if (is_wrapped_struct(state, -1, MLUA_WRAPPED_ERROR_KEY) != 0) {
+ int error_idx = lua_absindex(state, -1);
+ // lua_newuserdata and luaL_traceback may error
+ void *error_ud = lua_newuserdata(state, MLUA_WRAPPED_ERROR_SIZE);
+ int has_traceback = 0;
+ if (lua_checkstack(state, LUA_TRACEBACK_STACK) != 0) {
+ luaL_traceback(state, state, NULL, 0);
+ has_traceback = 1;
+ }
+ wrapped_error_traceback(state, error_idx, error_ud, has_traceback);
+ return 1;
+ }
+
+ if (MLUA_WRAPPED_PANIC_KEY != NULL &&
+ !is_wrapped_struct(state, -1, MLUA_WRAPPED_PANIC_KEY) &&
+ lua_checkstack(state, LUA_TRACEBACK_STACK) != 0) {
+ const char *s = luaL_tolstring(state, -1, NULL);
+ luaL_traceback(state, state, s, 0);
+ lua_remove(state, -2);
+ }
+
+ return 1;
+}
+
+int error_traceback_s(lua_State *L) {
+ lua_State *L1 = lua_touserdata(L, -1);
+ lua_pop(L, 1);
+ return error_traceback(L1);
+}
diff --git a/src/function.rs b/src/function.rs
index 9a9c205..150082e 100644
--- a/src/function.rs
+++ b/src/function.rs
@@ -1,12 +1,10 @@
use std::os::raw::{c_int, c_void};
-use std::{ptr, slice};
+use std::slice;
use crate::error::{Error, Result};
use crate::ffi;
use crate::types::LuaRef;
-use crate::util::{
- assert_stack, check_stack, error_traceback, pop_error, protect_lua_closure, StackGuard,
-};
+use crate::util::{assert_stack, check_stack, pop_error, StackGuard};
use crate::value::{FromLuaMulti, MultiValue, ToLuaMulti};
#[cfg(feature = "async")]
@@ -67,7 +65,7 @@ impl<'lua> Function<'lua> {
let _sg = StackGuard::new(lua.state);
check_stack(lua.state, nargs + 3)?;
- ffi::lua_pushcfunction(lua.state, error_traceback);
+ ffi::lua_pushcfunction(lua.state, ffi::safe::error_traceback);
let stack_start = ffi::lua_gettop(lua.state);
lua.push_ref(&self.0);
for arg in args {
@@ -161,26 +159,6 @@ impl<'lua> Function<'lua> {
/// # }
/// ```
pub fn bind<A: ToLuaMulti<'lua>>(&self, args: A) -> Result<Function<'lua>> {
- unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int {
- let nargs = ffi::lua_gettop(state);
- let nbinds = ffi::lua_tointeger(state, ffi::lua_upvalueindex(2)) as c_int;
- ffi::luaL_checkstack(state, nbinds + 2, ptr::null());
-
- ffi::lua_settop(state, nargs + nbinds + 1);
- ffi::lua_rotate(state, -(nargs + nbinds + 1), nbinds + 1);
-
- ffi::lua_pushvalue(state, ffi::lua_upvalueindex(1));
- ffi::lua_replace(state, 1);
-
- for i in 0..nbinds {
- ffi::lua_pushvalue(state, ffi::lua_upvalueindex(i + 3));
- ffi::lua_replace(state, i + 2);
- }
-
- ffi::lua_call(state, nargs + nbinds, ffi::LUA_MULTRET);
- ffi::lua_gettop(state)
- }
-
let lua = self.0.lua;
let args = args.to_lua_multi(lua)?;
@@ -193,15 +171,13 @@ impl<'lua> Function<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
check_stack(lua.state, nargs + 5)?;
+
lua.push_ref(&self.0);
ffi::lua_pushinteger(lua.state, nargs as ffi::lua_Integer);
for arg in args {
lua.push_value(arg)?;
}
-
- protect_lua_closure(lua.state, nargs + 2, 1, |state| {
- ffi::lua_pushcclosure(state, bind_call_impl, nargs + 2);
- })?;
+ ffi::safe::lua_pushcclosure(lua.state, ffi::safe::bind_call_impl, nargs + 2)?;
Ok(Function(lua.pop_ref()))
}
@@ -229,6 +205,7 @@ impl<'lua> Function<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
assert_stack(lua.state, 1);
+
lua.push_ref(&self.0);
let strip = if strip { 1 } else { 0 };
ffi::lua_dump(
diff --git a/src/hook.rs b/src/hook.rs
index f00e09b..f56adfe 100644
--- a/src/hook.rs
+++ b/src/hook.rs
@@ -165,7 +165,8 @@ impl HookTriggers {
}
}
-pub(crate) unsafe extern "C" fn hook_proc(state: *mut lua_State, ar: *mut lua_Debug) {
+#[no_mangle]
+pub unsafe extern "C" fn mlua_hook_proc(state: *mut lua_State, ar: *mut lua_Debug) -> c_int {
callback_error(state, |_| {
let debug = Debug {
ar,
@@ -182,8 +183,8 @@ pub(crate) unsafe extern "C" fn hook_proc(state: *mut lua_State, ar: *mut lua_De
Err(_) => mlua_panic!("Lua should not allow hooks to be called within another hook"),
}?;
- Ok(())
- });
+ Ok(0)
+ })
}
unsafe fn ptr_to_str<'a>(input: *const c_char) -> Option<&'a [u8]> {
diff --git a/src/lua.rs b/src/lua.rs
index d00dc47..3d8fc09 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -11,7 +11,7 @@ use std::{mem, ptr, str};
use crate::error::{Error, Result};
use crate::ffi;
use crate::function::Function;
-use crate::hook::{hook_proc, Debug, HookTriggers};
+use crate::hook::{Debug, HookTriggers};
use crate::scope::Scope;
use crate::stdlib::StdLib;
use crate::string::String;
@@ -27,8 +27,8 @@ use crate::userdata::{
use crate::util::{
assert_stack, callback_error, check_stack, get_gc_userdata, get_main_state, get_userdata,
get_wrapped_error, init_error_registry, init_gc_metatable_for, init_userdata_metatable,
- pop_error, protect_lua, protect_lua_closure, push_gc_userdata, push_meta_gc_userdata,
- push_string, push_userdata, push_wrapped_error, StackGuard, WrappedPanic,
+ pop_error, push_gc_userdata, push_userdata, push_wrapped_error, StackGuard, WrappedError,
+ WrappedPanic,
};
use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Value};
@@ -119,7 +119,7 @@ impl Drop for Lua {
let mut unref_list =
mlua_expect!(extra.registry_unref_list.lock(), "unref list poisoned");
*unref_list = None;
- ffi::lua_close(self.main_state.expect("main_state is null"));
+ ffi::lua_close(mlua_expect!(self.main_state, "main_state is null"));
if !extra.mem_info.is_null() {
Box::from_raw(extra.mem_info);
}
@@ -261,7 +261,10 @@ impl Lua {
#[cfg(any(feature = "lua51", feature = "luajit"))]
let state = ffi::luaL_newstate();
- ffi::luaL_requiref(state, cstr!("_G"), ffi::luaopen_base, 1);
+ mlua_expect!(
+ ffi::safe::luaL_requiref(state, "_G", ffi::luaopen_base, 1),
+ "Error during loading base lib"
+ );
ffi::lua_pop(state, 1);
let mut lua = Lua::init_from_ptr(state);
@@ -272,9 +275,7 @@ impl Lua {
}
mlua_expect!(
- protect_lua_closure(lua.main_state.expect("main_state is null"), 0, 0, |state| {
- load_from_std_lib(state, libs);
- }),
+ load_from_std_lib(state, libs),
"Error during loading standard libraries"
);
mlua_expect!(lua.extra.lock(), "extra is poisoned").libs |= libs;
@@ -289,35 +290,36 @@ impl Lua {
let main_state = maybe_main_state.unwrap_or(state);
let main_state_top = ffi::lua_gettop(main_state);
- let ref_thread = mlua_expect!(
- protect_lua_closure(main_state, 0, 0, |state| {
- init_error_registry(state);
+ let (ref_thread, wrapped_error_key, wrapped_panic_key) = mlua_expect!(
+ (|state| {
+ let (wrapped_error_key, wrapped_panic_key) = init_error_registry(state)?;
// Create the internal metatables and place them in the registry
// to prevent them from being garbage collected.
- init_gc_metatable_for::<Callback>(state, None);
- init_gc_metatable_for::<Lua>(state, None);
- init_gc_metatable_for::<Weak<Mutex<ExtraData>>>(state, None);
+ init_gc_metatable_for::<Callback>(state, None)?;
+ init_gc_metatable_for::<Lua>(state, None)?;
+ init_gc_metatable_for::<Weak<Mutex<ExtraData>>>(state, None)?;
#[cfg(feature = "async")]
{
- init_gc_metatable_for::<AsyncCallback>(state, None);
- init_gc_metatable_for::<LocalBoxFuture<Result<MultiValue>>>(state, None);
- init_gc_metatable_for::<AsyncPollPending>(state, None);
- init_gc_metatable_for::<Waker>(state, None);
+ init_gc_metatable_for::<AsyncCallback>(state, None)?;
+ init_gc_metatable_for::<LocalBoxFuture<Result<MultiValue>>>(state, None)?;
+ init_gc_metatable_for::<AsyncPollPending>(state, None)?;
+ init_gc_metatable_for::<Waker>(state, None)?;
}
// Init serde metatables
#[cfg(feature = "serialize")]
- crate::serde::init_metatables(state);
+ crate::serde::init_metatables(state)?;
// Create ref stack thread and place it in the registry to prevent it from being garbage
// collected.
- let _ref_thread = ffi::lua_newthread(state);
- ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX);
- _ref_thread
- }),
+ let ref_thread = ffi::safe::lua_newthread(state)?;
+ ffi::safe::luaL_ref(state, ffi::LUA_REGISTRYINDEX)?;
+
+ Ok::<_, Error>((ref_thread, wrapped_error_key, wrapped_panic_key))
+ })(main_state),
"Error during Lua construction",
);
@@ -342,13 +344,11 @@ impl Lua {
"Error while storing extra data",
);
mlua_expect!(
- protect_lua_closure(main_state, 1, 0, |state| {
- ffi::lua_rawsetp(
- state,
- ffi::LUA_REGISTRYINDEX,
- &EXTRA_REGISTRY_KEY as *const u8 as *mut c_void,
- );
- }),
+ ffi::safe::lua_rawsetp(
+ main_state,
+ ffi::LUA_REGISTRYINDEX,
+ &EXTRA_REGISTRY_KEY as *const u8 as *const c_void
+ ),
"Error while storing extra data"
);
@@ -358,6 +358,11 @@ impl Lua {
);
assert_stack(main_state, ffi::LUA_MINSTACK);
+ ffi::safe::WRAPPED_ERROR_SIZE = mem::size_of::<WrappedError>();
+ ffi::safe::WRAPPED_PANIC_SIZE = mem::size_of::<WrappedPanic>();
+ ffi::safe::WRAPPED_ERROR_KEY = wrapped_error_key as *const c_void;
+ ffi::safe::WRAPPED_PANIC_KEY = wrapped_panic_key as *const c_void;
+
Lua {
state,
main_state: maybe_main_state,
@@ -389,11 +394,7 @@ impl Lua {
}
let state = self.main_state.unwrap_or(self.state);
- let res = unsafe {
- protect_lua_closure(state, 0, 0, |state| {
- load_from_std_lib(state, libs);
- })
- };
+ let res = unsafe { load_from_std_lib(state, libs) };
// If `package` library loaded into a safe lua state then disable C modules
let curr_libs = mlua_expect!(self.extra.lock(), "extra is poisoned").libs;
@@ -485,7 +486,12 @@ impl Lua {
unsafe {
let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
extra.hook_callback = Some(Arc::new(RefCell::new(callback)));
- ffi::lua_sethook(state, Some(hook_proc), triggers.mask(), triggers.count());
+ ffi::lua_sethook(
+ state,
+ Some(ffi::safe::lua_call_mlua_hook_proc),
+ triggers.mask(),
+ triggers.count(),
+ );
}
Ok(())
}
@@ -569,11 +575,7 @@ impl Lua {
/// objects. Once to finish the current gc cycle, and once to start and finish the next cycle.
pub fn gc_collect(&self) -> Result<()> {
let state = self.main_state.unwrap_or(self.state);
- unsafe {
- protect_lua_closure(state, 0, 0, |state| {
- ffi::lua_gc(state, ffi::LUA_GCCOLLECT, 0);
- })
- }
+ unsafe { ffi::safe::lua_gc(state, ffi::LUA_GCCOLLECT, 0).map(|_| ()) }
}
/// Steps the garbage collector one indivisible step.
@@ -589,11 +591,7 @@ impl Lua {
/// finished a collection cycle.
pub fn gc_step_kbytes(&self, kbytes: c_int) -> Result<bool> {
let state = self.main_state.unwrap_or(self.state);
- unsafe {
- protect_lua_closure(state, 0, 0, |state| {
- ffi::lua_gc(state, ffi::LUA_GCSTEP, kbytes) != 0
- })
- }
+ unsafe { Ok(ffi::safe::lua_gc(state, ffi::LUA_GCSTEP, kbytes)? != 0) }
}
/// Sets the 'pause' value of the collector.
@@ -764,23 +762,14 @@ impl Lua {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
- push_string(self.state, s)?;
+ ffi::safe::lua_pushstring(self.state, s)?;
Ok(String(self.pop_ref()))
}
}
/// Creates and returns a new empty table.
pub fn create_table(&self) -> Result<Table> {
- unsafe {
- let _sg = StackGuard::new(self.state);
- assert_stack(self.state, 3);
- unsafe extern "C" fn new_table(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_newtable(state);
- 1
- }
- protect_lua(self.state, 0, new_table)?;
- Ok(Table(self.pop_ref()))
- }
+ self.create_table_with_capacity(0, 0)
}
/// Creates and returns a new empty table, with the specified capacity.
@@ -791,9 +780,7 @@ impl Lua {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
- protect_lua_closure(self.state, 0, 1, |state| {
- ffi::lua_createtable(state, narr, nrec)
- })?;
+ ffi::safe::lua_createtable(self.state, narr, nrec)?;
Ok(Table(self.pop_ref()))
}
}
@@ -811,21 +798,13 @@ impl Lua {
// slots available to avoid panics.
check_stack(self.state, 5 + ffi::LUA_MINSTACK)?;
- unsafe extern "C" fn new_table(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_newtable(state);
- 1
- }
- protect_lua(self.state, 0, new_table)?;
-
+ ffi::safe::lua_createtable(self.state, 0, 0)?;
for (k, v) in cont {
self.push_value(k.to_lua(self)?)?;
self.push_value(v.to_lua(self)?)?;
- unsafe extern "C" fn raw_set(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_rawset(state, -3);
- 1
- }
- protect_lua(self.state, 3, raw_set)?;
+ ffi::safe::lua_rawset(self.state, -3)?;
}
+
Ok(Table(self.pop_ref()))
}
}
@@ -990,8 +969,7 @@ impl Lua {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
- let thread_state =
- protect_lua_closure(self.state, 0, 1, |state| ffi::lua_newthread(state))?;
+ let thread_state = ffi::safe::lua_newthread(self.state)?;
self.push_ref(&func.0);
ffi::lua_xmove(self.state, thread_state, 1);
@@ -1104,10 +1082,7 @@ impl Lua {
assert_stack(self.state, 4);
self.push_value(v)?;
- let ok = protect_lua_closure(self.state, 1, 1, |state| {
- !ffi::lua_tostring(state, -1).is_null()
- })?;
- if ok {
+ if !ffi::safe::lua_tolstring(self.state, -1, ptr::null_mut())?.is_null() {
Some(String(self.pop_ref()))
} else {
None
@@ -1202,14 +1177,8 @@ impl Lua {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 5);
- push_string(self.state, name)?;
self.push_value(t)?;
-
- unsafe extern "C" fn set_registry(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
- 0
- }
- protect_lua(self.state, 2, set_registry)
+ ffi::safe::lua_rawsetfield(self.state, ffi::LUA_REGISTRYINDEX, name)
}
}
@@ -1228,12 +1197,8 @@ impl Lua {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
- push_string(self.state, name)?;
- unsafe extern "C" fn get_registry(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
- 1
- }
- protect_lua(self.state, 1, get_registry)?;
+ ffi::safe::lua_pushstring(self.state, name)?;
+ ffi::lua_rawget(self.state, ffi::LUA_REGISTRYINDEX);
self.pop_value()
};
@@ -1268,9 +1233,7 @@ impl Lua {
assert_stack(self.state, 2);
self.push_value(t)?;
- let registry_id = protect_lua_closure(self.state, 1, 0, |state| {
- ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
- })?;
+ let registry_id = ffi::safe::luaL_ref(self.state, ffi::LUA_REGISTRYINDEX)?;
let extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
@@ -1299,7 +1262,7 @@ impl Lua {
ffi::lua_rawgeti(
self.state,
ffi::LUA_REGISTRYINDEX,
- key.registry_id as ffi::lua_Integer,
+ key.registry_id as Integer,
);
self.pop_value()
};
@@ -1533,60 +1496,39 @@ impl Lua {
T::add_methods(&mut methods);
// Prepare metatable, add meta methods first and then meta fields
- protect_lua_closure(self.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let metatable_nrec = methods.meta_methods.len() + fields.meta_fields.len();
+ ffi::safe::lua_createtable(self.state, 0, metatable_nrec as c_int)?;
for (k, m) in methods.meta_methods {
- push_string(self.state, k.validate()?.name())?;
self.push_value(Value::Function(self.create_callback(m)?))?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, k.validate()?.name())?;
}
for (k, f) in fields.meta_fields {
- push_string(self.state, k.validate()?.name())?;
self.push_value(f(self)?)?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, k.validate()?.name())?;
}
let metatable_index = ffi::lua_absindex(self.state, -1);
let mut extra_tables_count = 0;
let mut field_getters_index = None;
- let has_field_getters = !fields.field_getters.is_empty();
- if has_field_getters {
- protect_lua_closure(self.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let field_getters_nrec = fields.field_getters.len();
+ if field_getters_nrec > 0 {
+ ffi::safe::lua_createtable(self.state, 0, field_getters_nrec as c_int)?;
for (k, m) in fields.field_getters {
- push_string(self.state, &k)?;
self.push_value(Value::Function(self.create_callback(m)?))?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, &k)?;
}
field_getters_index = Some(ffi::lua_absindex(self.state, -1));
extra_tables_count += 1;
}
let mut field_setters_index = None;
- let has_field_setters = !fields.field_setters.is_empty();
- if has_field_setters {
- protect_lua_closure(self.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let field_setters_nrec = fields.field_setters.len();
+ if field_setters_nrec > 0 {
+ ffi::safe::lua_createtable(self.state, 0, field_setters_nrec as c_int)?;
for (k, m) in fields.field_setters {
- push_string(self.state, &k)?;
self.push_value(Value::Function(self.create_callback(m)?))?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, &k)?;
}
field_setters_index = Some(ffi::lua_absindex(self.state, -1));
extra_tables_count += 1;
@@ -1594,29 +1536,19 @@ impl Lua {
let mut methods_index = None;
#[cfg(feature = "async")]
- let has_methods = !methods.methods.is_empty() || !methods.async_methods.is_empty();
+ let methods_nrec = methods.methods.len() + methods.async_methods.len();
#[cfg(not(feature = "async"))]
- let has_methods = !methods.methods.is_empty();
- if has_methods {
- protect_lua_closure(self.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let methods_nrec = methods.methods.len();
+ if methods_nrec > 0 {
+ ffi::safe::lua_createtable(self.state, 0, methods_nrec as c_int)?;
for (k, m) in methods.methods {
- push_string(self.state, &k)?;
self.push_value(Value::Function(self.create_callback(m)?))?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, &k)?;
}
#[cfg(feature = "async")]
for (k, m) in methods.async_methods {
- push_string(self.state, &k)?;
self.push_value(Value::Function(self.create_async_callback(m)?))?;
-
- protect_lua_closure(self.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(self.state, -2, &k)?;
}
methods_index = Some(ffi::lua_absindex(self.state, -1));
extra_tables_count += 1;
@@ -1634,9 +1566,7 @@ impl Lua {
ffi::lua_pop(self.state, extra_tables_count);
let ptr = ffi::lua_topointer(self.state, -1);
- let id = protect_lua_closure(self.state, 1, 0, |state| {
- ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
- })?;
+ let id = ffi::safe::luaL_ref(self.state, ffi::LUA_REGISTRYINDEX)?;
let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
extra.registered_userdata.insert(type_id, id);
@@ -1657,7 +1587,7 @@ impl Lua {
// Pushes a LuaRef value onto the stack, checking that it's a registered
// and not destructed UserData.
- // Uses 2 stack spaces, does not call checkstack
+ // Uses 3 stack spaces, does not call checkstack
#[cfg(feature = "serialize")]
pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result<()> {
self.push_ref(lref);
@@ -1698,13 +1628,15 @@ impl Lua {
{
unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
callback_error(state, |nargs| {
- if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
- || ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
+ let upvalue_idx1 = ffi::lua_upvalueindex(2);
+ let upvalue_idx2 = ffi::lua_upvalueindex(3);
+ if ffi::lua_type(state, upvalue_idx1) == ffi::LUA_TNIL
+ || ffi::lua_type(state, upvalue_idx2) == ffi::LUA_TNIL
{
return Err(Error::CallbackDestructed);
}
- let func = get_userdata::<Callback>(state, ffi::lua_upvalueindex(1));
- let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
+ let func = get_userdata::<Callback>(state, upvalue_idx1);
+ let lua = get_userdata::<Lua>(state, upvalue_idx2);
if nargs < ffi::LUA_MINSTACK {
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1735,12 +1667,9 @@ impl Lua {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 6);
- push_meta_gc_userdata::<Callback, _>(self.state, func)?;
+ push_gc_userdata::<Callback>(self.state, mem::transmute(func))?;
push_gc_userdata(self.state, self.clone())?;
-
- protect_lua_closure(self.state, 2, 1, |state| {
- ffi::lua_pushcclosure(state, call_callback, 2);
- })?;
+ ffi::safe::lua_pushrclosure(self.state, call_callback, 2)?;
Ok(Function(self.pop_ref()))
}
@@ -1764,13 +1693,15 @@ impl Lua {
unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
callback_error(state, |nargs| {
- if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
- || ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
+ let upvalue_idx1 = ffi::lua_upvalueindex(2);
+ let upvalue_idx2 = ffi::lua_upvalueindex(3);
+ if ffi::lua_type(state, upvalue_idx1) == ffi::LUA_TNIL
+ || ffi::lua_type(state, upvalue_idx2) == ffi::LUA_TNIL
{
return Err(Error::CallbackDestructed);
}
- let func = get_userdata::<AsyncCallback>(state, ffi::lua_upvalueindex(1));
- let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
+ let func = get_userdata::<AsyncCallback>(state, upvalue_idx1);
+ let lua = get_userdata::<Lua>(state, upvalue_idx2);
if nargs < ffi::LUA_MINSTACK {
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1789,7 +1720,7 @@ impl Lua {
push_gc_userdata(state, fut)?;
push_gc_userdata(state, lua.clone())?;
- ffi::lua_pushcclosure(state, poll_future, 2);
+ ffi::safe::lua_pushrclosure(state, poll_future, 2)?;
Ok(1)
})
@@ -1797,16 +1728,15 @@ impl Lua {
unsafe extern "C" fn poll_future(state: *mut ffi::lua_State) -> c_int {
callback_error(state, |nargs| {
- if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
- || ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
+ let upvalue_idx1 = ffi::lua_upvalueindex(2);
+ let upvalue_idx2 = ffi::lua_upvalueindex(3);
+ if ffi::lua_type(state, upvalue_idx1) == ffi::LUA_TNIL
+ || ffi::lua_type(state, upvalue_idx2) == ffi::LUA_TNIL
{
return Err(Error::CallbackDestructed);
}
- let fut = get_userdata::<LocalBoxFuture<Result<MultiValue>>>(
- state,
- ffi::lua_upvalueindex(1),
- );
- let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
+ let fut = get_userdata::<LocalBoxFuture<Result<MultiValue>>>(state, upvalue_idx1);
+ let lua = get_userdata::<Lua>(state, upvalue_idx2);
if nargs < ffi::LUA_MINSTACK {
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1849,12 +1779,9 @@ impl Lua {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 6);
- push_meta_gc_userdata::<AsyncCallback, _>(self.state, func)?;
+ push_gc_userdata::<AsyncCallback>(self.state, mem::transmute(func))?;
push_gc_userdata(self.state, self.clone())?;
-
- protect_lua_closure(self.state, 2, 1, |state| {
- ffi::lua_pushcclosure(state, call_callback, 2);
- })?;
+ ffi::safe::lua_pushrclosure(self.state, call_callback, 2)?;
Function(self.pop_ref())
};
@@ -1909,11 +1836,7 @@ impl Lua {
let ud_index = self.userdata_metatable::<T>()?;
push_userdata::<UserDataCell<T>>(self.state, RefCell::new(data))?;
- ffi::lua_rawgeti(
- self.state,
- ffi::LUA_REGISTRYINDEX,
- ud_index as ffi::lua_Integer,
- );
+ ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, ud_index as Integer);
ffi::lua_setmetatable(self.state, -2);
Ok(AnyUserData(self.pop_ref()))
@@ -2166,7 +2089,7 @@ impl<'lua, 'a> Chunk<'lua, 'a> {
}
}
-unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) {
+unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result<()> {
#[cfg(feature = "luajit")]
// Stop collector during library initialization
ffi::lua_gc(state, ffi::LUA_GCSTOP, 0);
@@ -2174,41 +2097,35 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) {
#[cfg(any(feature = "lua54", 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::safe::luaL_requiref(state, ffi::LUA_COLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_TABLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_IOLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_OSLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_STRLIBNAME, ffi::luaopen_string, 1)?;
ffi::lua_pop(state, 1);
}
#[cfg(any(feature = "lua54", 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::safe::luaL_requiref(state, ffi::LUA_UTF8LIBNAME, ffi::luaopen_utf8, 1)?;
ffi::lua_pop(state, 1);
}
}
@@ -2216,8 +2133,7 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) {
#[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::safe::luaL_requiref(state, ffi::LUA_BITLIBNAME, ffi::luaopen_bit32, 1)?;
ffi::lua_pop(state, 1);
}
}
@@ -2225,47 +2141,43 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) {
#[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::safe::luaL_requiref(state, ffi::LUA_BITLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_MATHLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_DBLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_LOADLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_JITLIBNAME, 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::safe::luaL_requiref(state, ffi::LUA_FFILIBNAME, ffi::luaopen_ffi, 1)?;
ffi::lua_pop(state, 1);
}
}
#[cfg(feature = "luajit")]
ffi::lua_gc(state, ffi::LUA_GCRESTART, -1);
+
+ Ok(())
}
unsafe fn ref_stack_pop(extra: &mut ExtraData) -> c_int {
diff --git a/src/scope.rs b/src/scope.rs
index 40ad590..640886f 100644
--- a/src/scope.rs
+++ b/src/scope.rs
@@ -2,7 +2,7 @@ use std::any::Any;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::marker::PhantomData;
use std::mem;
-use std::os::raw::c_void;
+use std::os::raw::{c_int, c_void};
use std::rc::Rc;
#[cfg(feature = "serialize")]
@@ -17,8 +17,7 @@ use crate::userdata::{
AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMethods, UserDataWrapped,
};
use crate::util::{
- assert_stack, init_userdata_metatable, protect_lua_closure, push_string, push_userdata,
- take_userdata, StackGuard,
+ assert_stack, init_userdata_metatable, push_userdata, take_userdata, StackGuard,
};
use crate::value::{FromLua, FromLuaMulti, MultiValue, ToLua, ToLuaMulti, Value};
@@ -38,7 +37,7 @@ use {
#[allow(clippy::type_complexity)]
pub struct Scope<'lua, 'scope> {
lua: &'lua Lua,
- destructors: RefCell<Vec<(LuaRef<'lua>, fn(LuaRef<'lua>) -> Vec<Box<dyn Any>>)>>,
+ destructors: RefCell<Vec<(LuaRef<'lua>, Box<dyn Fn(LuaRef<'lua>) -> Vec<Box<dyn Any>> + 'lua>)>>,
_scope_invariant: PhantomData<Cell<&'scope ()>>,
}
@@ -181,8 +180,11 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
// Safe even though T may not be Send, because the parent Lua cannot be sent to another
// thread while the Scope is alive (or the returned AnyUserData handle even).
unsafe {
- let u = self.lua.make_userdata(data)?;
- self.destructors.borrow_mut().push((u.0.clone(), |u| {
+ let ud = self.lua.make_userdata(data)?;
+
+ #[cfg(any(feature = "lua51", feature = "luajit"))]
+ let newtable = self.lua.create_table()?;
+ self.destructors.borrow_mut().push((ud.0.clone(), Box::new(move |u| {
let state = u.lua.state;
assert_stack(state, 2);
u.lua.push_ref(&u);
@@ -191,14 +193,15 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
ffi::lua_pushnil(state);
#[cfg(any(feature = "lua51", feature = "luajit"))]
- ffi::lua_newtable(state);
+ u.lua.push_ref(&newtable.0);
ffi::lua_setuservalue(state, -2);
// We know the destructor has not run yet because we hold a reference to the
// userdata.
vec![Box::new(take_userdata::<UserDataCell<T>>(state))]
- }));
- Ok(u)
+ })));
+
+ Ok(ud)
}
}
@@ -258,7 +261,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
if ffi::lua_getmetatable(lua.state, -1) == 0 {
return Err(Error::UserDataTypeMismatch);
}
- ffi::lua_pushstring(lua.state, cstr!("__mlua_ptr"));
+ ffi::safe::lua_pushstring(lua.state, "__mlua_ptr")?;
if ffi::lua_rawget(lua.state, -2) == ffi::LUA_TLIGHTUSERDATA {
let ud_ptr = ffi::lua_touserdata(lua.state, -1);
if ud_ptr == check_data.as_ptr() as *mut c_void {
@@ -326,77 +329,52 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
push_userdata(lua.state, UserDataCell::new(UserDataWrapped::new(())))?;
// Prepare metatable, add meta methods first and then meta fields
- protect_lua_closure(lua.state, 0, 1, |state| {
- ffi::lua_newtable(state);
-
- // Add internal metamethod to store reference to the data
- ffi::lua_pushstring(state, cstr!("__mlua_ptr"));
- ffi::lua_pushlightuserdata(lua.state, data.as_ptr() as *mut c_void);
- ffi::lua_rawset(state, -3);
- })?;
+ let meta_methods_nrec = ud_methods.meta_methods.len() + ud_fields.meta_fields.len() + 1;
+ ffi::safe::lua_createtable(lua.state, 0, meta_methods_nrec as c_int)?;
+
+ ffi::lua_pushlightuserdata(lua.state, data.as_ptr() as *mut c_void);
+ ffi::safe::lua_rawsetfield(lua.state, -2, "__mlua_ptr")?;
+
for (k, m) in ud_methods.meta_methods {
- push_string(lua.state, k.validate()?.name())?;
lua.push_value(Value::Function(wrap_method(self, data.clone(), m)?))?;
-
- protect_lua_closure(lua.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(lua.state, -2, k.validate()?.name())?;
}
for (k, f) in ud_fields.meta_fields {
- push_string(lua.state, k.validate()?.name())?;
lua.push_value(f(mem::transmute(lua))?)?;
-
- protect_lua_closure(lua.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(lua.state, -2, k.validate()?.name())?;
}
let metatable_index = ffi::lua_absindex(lua.state, -1);
let mut field_getters_index = None;
- if !ud_fields.field_getters.is_empty() {
- protect_lua_closure(lua.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let field_getters_nrec = ud_fields.field_getters.len();
+ if field_getters_nrec > 0 {
+ ffi::safe::lua_createtable(lua.state, 0, field_getters_nrec as c_int)?;
for (k, m) in ud_fields.field_getters {
- push_string(lua.state, &k)?;
lua.push_value(Value::Function(wrap_method(self, data.clone(), m)?))?;
-
- protect_lua_closure(lua.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(lua.state, -2, &k)?;
}
field_getters_index = Some(ffi::lua_absindex(lua.state, -1));
}
let mut field_setters_index = None;
- if !ud_fields.field_setters.is_empty() {
- protect_lua_closure(lua.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ let field_setters_nrec = ud_fields.field_setters.len();
+ if field_setters_nrec > 0 {
+ ffi::safe::lua_createtable(lua.state, 0, field_setters_nrec as c_int)?;
for (k, m) in ud_fields.field_setters {
- push_string(lua.state, &k)?;
lua.push_value(Value::Function(wrap_method(self, data.clone(), m)?))?;
-
- protect_lua_closure(lua.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(lua.state, -2, &k)?;
}
field_setters_index = Some(ffi::lua_absindex(lua.state, -1));
}
let mut methods_index = None;
- if !ud_methods.methods.is_empty() {
+ let methods_nrec = ud_methods.methods.len();
+ if methods_nrec > 0 {
// Create table used for methods lookup
- protect_lua_closure(lua.state, 0, 1, |state| {
- ffi::lua_newtable(state);
- })?;
+ ffi::safe::lua_createtable(lua.state, 0, methods_nrec as c_int)?;
for (k, m) in ud_methods.methods {
- push_string(lua.state, &k)?;
lua.push_value(Value::Function(wrap_method(self, data.clone(), m)?))?;
-
- protect_lua_closure(lua.state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(lua.state, -2, &k)?;
}
methods_index = Some(ffi::lua_absindex(lua.state, -1));
}
@@ -419,7 +397,9 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
let ud = AnyUserData(lua.pop_ref());
lua.register_userdata_metatable(mt_id as isize);
- self.destructors.borrow_mut().push((ud.0.clone(), |ud| {
+ #[cfg(any(feature = "lua51", feature = "luajit"))]
+ let newtable = lua.create_table()?;
+ self.destructors.borrow_mut().push((ud.0.clone(), Box::new(move |ud| {
// We know the destructor has not run yet because we hold a reference to the userdata.
let state = ud.lua.state;
assert_stack(state, 2);
@@ -435,11 +415,11 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
ffi::lua_pushnil(state);
#[cfg(any(feature = "lua51", feature = "luajit"))]
- ffi::lua_newtable(state);
+ u.lua.push_ref(&newtable.0);
ffi::lua_setuservalue(state, -2);
vec![Box::new(take_userdata::<UserDataCell<()>>(state))]
- }));
+ })));
Ok(ud)
}
@@ -457,27 +437,26 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
let f = mem::transmute::<Callback<'callback, 'scope>, Callback<'lua, 'static>>(f);
let f = self.lua.create_callback(f)?;
- let mut destructors = self.destructors.borrow_mut();
- destructors.push((f.0.clone(), |f| {
+ self.destructors.borrow_mut().push((f.0.clone(), Box::new(|f| {
let state = f.lua.state;
assert_stack(state, 3);
f.lua.push_ref(&f);
// We know the destructor has not run yet because we hold a reference to the callback.
- ffi::lua_getupvalue(state, -1, 1);
+ ffi::lua_getupvalue(state, -1, 2);
let ud1 = take_userdata::<Callback>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 1);
+ ffi::lua_setupvalue(state, -2, 2);
- ffi::lua_getupvalue(state, -1, 2);
+ ffi::lua_getupvalue(state, -1, 3);
let ud2 = take_userdata::<Lua>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 2);
+ ffi::lua_setupvalue(state, -2, 3);
ffi::lua_pop(state, 1);
vec![Box::new(ud1), Box::new(ud2)]
- }));
+ })));
Ok(f)
}
@@ -489,8 +468,11 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
let f = mem::transmute::<AsyncCallback<'callback, 'scope>, AsyncCallback<'lua, 'static>>(f);
let f = self.lua.create_async_callback(f)?;
- let mut destructors = self.destructors.borrow_mut();
- destructors.push((f.0.clone(), |f| {
+ // We need to pre-allocate strings to avoid failures in destructor.
+ let get_poll_str = self.lua.create_string("get_poll")?;
+ let poll_str = self.lua.create_string("poll")?;
+
+ self.destructors.borrow_mut().push((f.0.clone(), Box::new(move |f| {
let state = f.lua.state;
assert_stack(state, 4);
f.lua.push_ref(&f);
@@ -504,41 +486,41 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
ffi::lua_getfenv(state, -1);
// Second, get the `get_poll()` closure using the corresponding key
- ffi::lua_pushstring(state, cstr!("get_poll"));
+ f.lua.push_ref(&get_poll_str.0);
ffi::lua_rawget(state, -2);
// Destroy all upvalues
- ffi::lua_getupvalue(state, -1, 1);
+ ffi::lua_getupvalue(state, -1, 2);
let ud1 = take_userdata::<AsyncCallback>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 1);
+ ffi::lua_setupvalue(state, -2, 2);
- ffi::lua_getupvalue(state, -1, 2);
+ ffi::lua_getupvalue(state, -1, 3);
let ud2 = take_userdata::<Lua>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 2);
+ ffi::lua_setupvalue(state, -2, 3);
ffi::lua_pop(state, 1);
let mut data: Vec<Box<dyn Any>> = vec![Box::new(ud1), Box::new(ud2)];
// Finally, get polled future and destroy it
- ffi::lua_pushstring(state, cstr!("poll"));
+ f.lua.push_ref(&poll_str.0);
if ffi::lua_rawget(state, -2) == ffi::LUA_TFUNCTION {
- ffi::lua_getupvalue(state, -1, 1);
+ ffi::lua_getupvalue(state, -1, 2);
let ud3 = take_userdata::<LocalBoxFuture<Result<MultiValue>>>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 1);
+ ffi::lua_setupvalue(state, -2, 2);
data.push(Box::new(ud3));
- ffi::lua_getupvalue(state, -1, 2);
+ ffi::lua_getupvalue(state, -1, 3);
let ud4 = take_userdata::<Lua>(state);
ffi::lua_pushnil(state);
- ffi::lua_setupvalue(state, -2, 2);
+ ffi::lua_setupvalue(state, -2, 3);
data.push(Box::new(ud4));
}
data
- }));
+ })));
Ok(f)
}
diff --git a/src/serde/mod.rs b/src/serde/mod.rs
index 3163d3d..8d6945e 100644
--- a/src/serde/mod.rs
+++ b/src/serde/mod.rs
@@ -1,6 +1,6 @@
//! (De)Serialization support using serde.
-use std::os::raw::{c_int, c_void};
+use std::os::raw::c_void;
use std::ptr;
use serde::{Deserialize, Serialize};
@@ -9,7 +9,8 @@ use crate::error::Result;
use crate::ffi;
use crate::lua::Lua;
use crate::table::Table;
-use crate::util::{assert_stack, protect_lua, StackGuard};
+use crate::types::LightUserData;
+use crate::util::{assert_stack, StackGuard};
use crate::value::Value;
pub trait LuaSerdeExt<'lua> {
@@ -133,29 +134,17 @@ pub trait LuaSerdeExt<'lua> {
impl<'lua> LuaSerdeExt<'lua> for Lua {
fn null(&'lua self) -> Result<Value<'lua>> {
- unsafe {
- let _sg = StackGuard::new(self.state);
- assert_stack(self.state, 3);
-
- unsafe extern "C" fn push_null(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_pushlightuserdata(state, ptr::null_mut());
- 1
- }
- protect_lua(self.state, 0, push_null)?;
- Ok(self.pop_value())
- }
+ // TODO: Remove Result?
+ Ok(Value::LightUserData(LightUserData(ptr::null_mut())))
}
fn array_metatable(&'lua self) -> Result<Table<'lua>> {
unsafe {
let _sg = StackGuard::new(self.state);
- assert_stack(self.state, 3);
+ assert_stack(self.state, 1);
+
+ push_array_metatable(self.state);
- unsafe extern "C" fn get_array_mt(state: *mut ffi::lua_State) -> c_int {
- push_array_metatable(state);
- 1
- }
- protect_lua(self.state, 0, get_array_mt)?;
Ok(Table(self.pop_ref()))
}
}
@@ -175,18 +164,17 @@ impl<'lua> LuaSerdeExt<'lua> for Lua {
}
}
-pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) {
+pub(crate) unsafe fn init_metatables(state: *mut ffi::lua_State) -> Result<()> {
ffi::lua_pushlightuserdata(
state,
&ARRAY_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
);
- ffi::lua_newtable(state);
+ ffi::safe::lua_createtable(state, 0, 1)?;
- ffi::lua_pushstring(state, cstr!("__metatable"));
ffi::lua_pushboolean(state, 0);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_rawsetfield(state, -2, "__metatable")?;
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
+ ffi::safe::lua_rawset(state, ffi::LUA_REGISTRYINDEX)
}
pub(crate) unsafe fn push_array_metatable(state: *mut ffi::lua_State) {
diff --git a/src/serde/ser.rs b/src/serde/ser.rs
index e6f619c..aec0caa 100644
--- a/src/serde/ser.rs
+++ b/src/serde/ser.rs
@@ -9,7 +9,7 @@ use crate::lua::Lua;
use crate::string::String;
use crate::table::Table;
use crate::types::Integer;
-use crate::util::{assert_stack, protect_lua, StackGuard};
+use crate::util::{assert_stack, StackGuard};
use crate::value::{ToLua, Value};
/// A struct for serializing Rust values into Lua values.
@@ -208,18 +208,12 @@ impl<'lua> ser::SerializeSeq for SerializeVec<'lua> {
let value = lua.to_value(value)?;
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 4);
+ assert_stack(lua.state, 5);
lua.push_ref(&self.table.0);
lua.push_value(value)?;
-
- unsafe extern "C" fn push_to_table(state: *mut ffi::lua_State) -> c_int {
- let len = ffi::lua_rawlen(state, -2) as Integer;
- ffi::lua_rawseti(state, -2, len + 1);
- 1
- }
-
- protect_lua(lua.state, 2, push_to_table)
+ let len = ffi::lua_rawlen(lua.state, -2) as Integer;
+ ffi::safe::lua_rawseti(lua.state, -2, len + 1)
}
}
diff --git a/src/table.rs b/src/table.rs
index 00e3aab..1e7a5fc 100644
--- a/src/table.rs
+++ b/src/table.rs
@@ -1,5 +1,4 @@
use std::marker::PhantomData;
-use std::os::raw::c_int;
#[cfg(feature = "serialize")]
use {
@@ -11,7 +10,7 @@ use crate::error::{Error, Result};
use crate::ffi;
use crate::function::Function;
use crate::types::{Integer, LuaRef};
-use crate::util::{assert_stack, protect_lua, protect_lua_closure, StackGuard};
+use crate::util::{assert_stack, StackGuard};
use crate::value::{FromLua, FromLuaMulti, Nil, ToLua, ToLuaMulti, Value};
#[cfg(feature = "async")]
@@ -60,19 +59,15 @@ impl<'lua> Table<'lua> {
let lua = self.0.lua;
let key = key.to_lua(lua)?;
let value = value.to_lua(lua)?;
+
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 6);
+ assert_stack(lua.state, 5);
lua.push_ref(&self.0);
lua.push_value(key)?;
lua.push_value(value)?;
-
- unsafe extern "C" fn set_table(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_settable(state, -3);
- 1
- }
- protect_lua(lua.state, 3, set_table)
+ ffi::safe::lua_settable(lua.state, -3)
}
}
@@ -103,18 +98,15 @@ impl<'lua> Table<'lua> {
pub fn get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V> {
let lua = self.0.lua;
let key = key.to_lua(lua)?;
+
let value = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 5);
+ assert_stack(lua.state, 4);
lua.push_ref(&self.0);
lua.push_value(key)?;
+ ffi::safe::lua_gettable(lua.state, -2)?;
- unsafe extern "C" fn get_table(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_gettable(state, -2);
- 1
- }
- protect_lua(lua.state, 2, get_table)?;
lua.pop_value()
};
V::from_lua(value, lua)
@@ -127,19 +119,13 @@ impl<'lua> Table<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 5);
+ assert_stack(lua.state, 4);
lua.push_ref(&self.0);
lua.push_value(key)?;
+ ffi::safe::lua_gettable(lua.state, -2)?;
- unsafe extern "C" fn get_table(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_gettable(state, -2);
- 1
- }
- protect_lua(lua.state, 2, get_table)?;
-
- let has = ffi::lua_isnil(lua.state, -1) == 0;
- Ok(has)
+ Ok(ffi::lua_isnil(lua.state, -1) == 0)
}
}
@@ -207,19 +193,12 @@ impl<'lua> Table<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 6);
+ assert_stack(lua.state, 5);
lua.push_ref(&self.0);
lua.push_value(key)?;
lua.push_value(value)?;
-
- unsafe extern "C" fn raw_set(state: *mut ffi::lua_State) -> c_int {
- ffi::lua_rawset(state, -3);
- 0
- }
- protect_lua(lua.state, 3, raw_set)?;
-
- Ok(())
+ ffi::safe::lua_rawset(lua.state, -3)
}
}
@@ -227,13 +206,15 @@ impl<'lua> Table<'lua> {
pub fn raw_get<K: ToLua<'lua>, V: FromLua<'lua>>(&self, key: K) -> Result<V> {
let lua = self.0.lua;
let key = key.to_lua(lua)?;
+
let value = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 3);
+ assert_stack(lua.state, 2);
lua.push_ref(&self.0);
lua.push_value(key)?;
ffi::lua_rawget(lua.state, -2);
+
lua.pop_value()
};
V::from_lua(value, lua)
@@ -251,19 +232,11 @@ impl<'lua> Table<'lua> {
let value = value.to_lua(lua)?;
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 6);
+ assert_stack(lua.state, 5);
lua.push_ref(&self.0);
lua.push_value(value)?;
-
- protect_lua_closure(lua.state, 2, 0, |state| {
- for i in (idx..size + 1).rev() {
- // table[i+1] = table[i]
- ffi::lua_rawgeti(state, -2, i);
- ffi::lua_rawseti(state, -3, i + 1);
- }
- ffi::lua_rawseti(state, -2, idx);
- })
+ ffi::safe::lua_rawinsert(lua.state, -2, idx)
}
}
@@ -285,18 +258,10 @@ impl<'lua> Table<'lua> {
}
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 6);
+ assert_stack(lua.state, 4);
lua.push_ref(&self.0);
-
- protect_lua_closure(lua.state, 1, 0, |state| {
- for i in idx..size {
- ffi::lua_rawgeti(state, -1, i + 1);
- ffi::lua_rawseti(state, -2, i);
- }
- ffi::lua_pushnil(state);
- ffi::lua_rawseti(state, -2, size);
- })
+ ffi::safe::lua_rawremove(lua.state, -1, idx)
}
}
_ => self.raw_set(key, Nil),
@@ -313,8 +278,9 @@ impl<'lua> Table<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
assert_stack(lua.state, 4);
+
lua.push_ref(&self.0);
- protect_lua_closure(lua.state, 1, 0, |state| ffi::luaL_len(state, -1))
+ ffi::safe::luaL_len(lua.state, -1)
}
}
@@ -324,9 +290,9 @@ impl<'lua> Table<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
assert_stack(lua.state, 1);
+
lua.push_ref(&self.0);
- let len = ffi::lua_rawlen(lua.state, -1);
- len as Integer
+ ffi::lua_rawlen(lua.state, -1) as Integer
}
}
@@ -337,13 +303,13 @@ impl<'lua> Table<'lua> {
let lua = self.0.lua;
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 1);
+ assert_stack(lua.state, 2);
+
lua.push_ref(&self.0);
if ffi::lua_getmetatable(lua.state, -1) == 0 {
None
} else {
- let table = Table(lua.pop_ref());
- Some(table)
+ Some(Table(lua.pop_ref()))
}
}
}
@@ -356,7 +322,8 @@ impl<'lua> Table<'lua> {
let lua = self.0.lua;
unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 1);
+ assert_stack(lua.state, 2);
+
lua.push_ref(&self.0);
if let Some(metatable) = metatable {
lua.push_ref(&metatable.0);
@@ -495,6 +462,7 @@ impl<'lua> Table<'lua> {
unsafe {
let _sg = StackGuard::new(lua.state);
assert_stack(lua.state, 3);
+
lua.push_ref(&self.0);
if ffi::lua_getmetatable(lua.state, -1) == 0 {
return false;
@@ -685,15 +653,12 @@ where
let res = (|| {
let res = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 6);
+ assert_stack(lua.state, 4);
lua.push_ref(&self.table);
lua.push_value(next_key)?;
- let next = protect_lua_closure(lua.state, 2, ffi::LUA_MULTRET, |state| {
- ffi::lua_next(state, -2) != 0
- })?;
- if next {
+ if ffi::safe::lua_next(lua.state, -2)? != 0 {
ffi::lua_pushvalue(lua.state, -2);
let key = lua.pop_value();
let value = lua.pop_value();
@@ -748,15 +713,15 @@ where
let res = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 5);
+ assert_stack(lua.state, 4);
lua.push_ref(&self.table);
- let lua_geti = if self.raw {
- ffi::lua_rawgeti
+ let res = if self.raw {
+ Ok(ffi::lua_rawgeti(lua.state, -1, index))
} else {
- ffi::lua_geti
+ ffi::safe::lua_geti(lua.state, -1, index)
};
- match protect_lua_closure(lua.state, 1, 1, |state| lua_geti(state, -1, index)) {
+ match res {
Ok(ffi::LUA_TNIL) if index > self.len.unwrap_or(0) => None,
Ok(_) => {
let value = lua.pop_value();
diff --git a/src/thread.rs b/src/thread.rs
index f26eca1..edd217e 100644
--- a/src/thread.rs
+++ b/src/thread.rs
@@ -3,9 +3,7 @@ use std::os::raw::c_int;
use crate::error::{Error, Result};
use crate::ffi;
use crate::types::LuaRef;
-use crate::util::{
- assert_stack, check_stack, error_traceback, pop_error, protect_lua_closure, StackGuard,
-};
+use crate::util::{assert_stack, check_stack, pop_error, StackGuard};
use crate::value::{FromLuaMulti, MultiValue, ToLuaMulti};
#[cfg(feature = "async")]
@@ -111,18 +109,17 @@ impl<'lua> Thread<'lua> {
let args = args.to_lua_multi(lua)?;
let results = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 3);
+ assert_stack(lua.state, 2);
lua.push_ref(&self.0);
let thread_state = ffi::lua_tothread(lua.state, -1);
+ ffi::lua_pop(lua.state, 1);
let status = ffi::lua_status(thread_state);
if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 {
return Err(Error::CoroutineInactive);
}
- ffi::lua_pop(lua.state, 1);
-
let nargs = args.len() as c_int;
check_stack(lua.state, nargs)?;
check_stack(thread_state, nargs + 1)?;
@@ -136,14 +133,12 @@ impl<'lua> Thread<'lua> {
let ret = ffi::lua_resume(thread_state, lua.state, nargs, &mut nresults as *mut c_int);
if ret != ffi::LUA_OK && ret != ffi::LUA_YIELD {
- protect_lua_closure(lua.state, 0, 0, |_| {
- error_traceback(thread_state);
- 0
- })?;
+ ffi::safe::error_traceback2(lua.state, thread_state)?;
return Err(pop_error(thread_state, ret));
}
let mut results = MultiValue::new();
+ check_stack(lua.state, nresults)?;
ffi::lua_xmove(thread_state, lua.state, nresults);
assert_stack(lua.state, 2);
@@ -343,7 +338,7 @@ impl WakerGuard {
ffi::lua_pushlightuserdata(state, &WAKER_REGISTRY_KEY as *const u8 as *mut c_void);
push_gc_userdata(state, waker)?;
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
+ ffi::safe::lua_rawset(state, ffi::LUA_REGISTRYINDEX)?;
Ok(WakerGuard(state))
}
@@ -360,7 +355,7 @@ impl Drop for WakerGuard {
ffi::lua_pushlightuserdata(state, &WAKER_REGISTRY_KEY as *const u8 as *mut c_void);
ffi::lua_pushnil(state);
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
+ ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX); // TODO: make safe
}
}
}
diff --git a/src/types.rs b/src/types.rs
index 7621c43..79b6293 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,3 +1,4 @@
+// OK
use std::cell::RefCell;
use std::os::raw::{c_int, c_void};
use std::sync::{Arc, Mutex};
diff --git a/src/userdata.rs b/src/userdata.rs
index 12b4518..f3d01c9 100644
--- a/src/userdata.rs
+++ b/src/userdata.rs
@@ -1,3 +1,4 @@
+// OK
use std::cell::{Ref, RefMut};
use std::fmt;
use std::hash::{Hash, Hasher};
@@ -17,7 +18,7 @@ use crate::ffi;
use crate::function::Function;
use crate::lua::Lua;
use crate::table::{Table, TablePairs};
-use crate::types::{LuaRef, MaybeSend, UserDataCell};
+use crate::types::{Integer, LuaRef, MaybeSend, UserDataCell};
use crate::util::{assert_stack, get_destructed_userdata_metatable, get_userdata, StackGuard};
use crate::value::{FromLua, FromLuaMulti, ToLua, ToLuaMulti, Value};
@@ -679,7 +680,7 @@ impl<'lua> AnyUserData<'lua> {
// Lua 5.2/5.1 allows to store only a table. Then we will wrap the value.
let t = lua.create_table()?;
t.raw_set(1, v)?;
- crate::Value::Table(t)
+ Value::Table(t)
};
#[cfg(any(feature = "lua54", feature = "lua53"))]
let v = v.to_lua(lua)?;
@@ -702,13 +703,13 @@ impl<'lua> AnyUserData<'lua> {
let lua = self.0.lua;
let res = unsafe {
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 3);
+ assert_stack(lua.state, 2);
lua.push_ref(&self.0);
ffi::lua_getuservalue(lua.state, -1);
lua.pop_value()
};
#[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))]
- return crate::Table::from_lua(res, lua)?.get(1);
+ return Table::from_lua(res, lua)?.get(1);
#[cfg(any(feature = "lua54", feature = "lua53"))]
V::from_lua(res, lua)
}
@@ -754,10 +755,9 @@ impl<'lua> AnyUserData<'lua> {
unsafe {
let lua = self.0.lua;
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 3);
+ assert_stack(lua.state, 2);
lua.push_ref(&self.0);
-
if ffi::lua_getmetatable(lua.state, -1) == 0 {
return Err(Error::UserDataTypeMismatch);
}
@@ -798,28 +798,26 @@ impl<'lua> AnyUserData<'lua> {
assert_stack(lua.state, 3);
lua.push_ref(&self.0);
-
if ffi::lua_getmetatable(lua.state, -1) == 0 {
- Err(Error::UserDataTypeMismatch)
- } else {
- ffi::lua_rawgeti(
- lua.state,
- ffi::LUA_REGISTRYINDEX,
- lua.userdata_metatable::<T>()? as ffi::lua_Integer,
- );
-
- if ffi::lua_rawequal(lua.state, -1, -2) == 0 {
- // Maybe UserData destructed?
- ffi::lua_pop(lua.state, 1);
- get_destructed_userdata_metatable(lua.state);
- if ffi::lua_rawequal(lua.state, -1, -2) == 1 {
- Err(Error::UserDataDestructed)
- } else {
- Err(Error::UserDataTypeMismatch)
- }
+ return Err(Error::UserDataTypeMismatch);
+ }
+ ffi::lua_rawgeti(
+ lua.state,
+ ffi::LUA_REGISTRYINDEX,
+ lua.userdata_metatable::<T>()? as Integer,
+ );
+
+ if ffi::lua_rawequal(lua.state, -1, -2) == 0 {
+ // Maybe UserData destructed?
+ ffi::lua_pop(lua.state, 1);
+ get_destructed_userdata_metatable(lua.state);
+ if ffi::lua_rawequal(lua.state, -1, -2) == 1 {
+ Err(Error::UserDataDestructed)
} else {
- func(&*get_userdata::<UserDataCell<T>>(lua.state, -3))
+ Err(Error::UserDataTypeMismatch)
}
+ } else {
+ func(&*get_userdata::<UserDataCell<T>>(lua.state, -3))
}
}
}
@@ -918,17 +916,17 @@ impl<'lua> Serialize for AnyUserData<'lua> {
where
S: Serializer,
{
- let f = || unsafe {
+ let res = (|| unsafe {
let lua = self.0.lua;
let _sg = StackGuard::new(lua.state);
- assert_stack(lua.state, 2);
+ assert_stack(lua.state, 3);
lua.push_userdata_ref(&self.0)?;
let ud = &*get_userdata::<UserDataCell<()>>(lua.state, -1);
(*ud.try_borrow().map_err(|_| Error::UserDataBorrowError)?.ser)
.serialize(serializer)
.map_err(|err| Error::SerializeError(err.to_string()))
- };
- f().map_err(ser::Error::custom)
+ })();
+ res.map_err(ser::Error::custom)
}
}
diff --git a/src/util.rs b/src/util.rs
index 8b29a91..cf351dc 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,8 +1,7 @@
use std::any::{Any, TypeId};
-use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Write;
-use std::os::raw::{c_char, c_int, c_void};
+use std::os::raw::{c_int, c_void};
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
use std::sync::{Arc, Mutex};
use std::{mem, ptr, slice};
@@ -69,17 +68,17 @@ impl Drop for StackGuard {
// Call a function that calls into the Lua API and may trigger a Lua error (longjmp) in a safe way.
// Wraps the inner function in a call to `lua_pcall`, so the inner function only has access to a
-// limited lua stack. `nargs` is the same as the the parameter to `lua_pcall`, and `nresults` is
-// always LUA_MULTRET. Internally uses 2 extra stack spaces, and does not call checkstack.
+// limited lua stack. `nargs` is the same as the the parameter to `lua_pcall`, and `nresults` is
+// always LUA_MULTRET. Internally uses 2 extra stack spaces, and does not call checkstack.
// Provided function must *never* panic.
pub unsafe fn protect_lua(
state: *mut ffi::lua_State,
nargs: c_int,
- f: unsafe extern "C" fn(*mut ffi::lua_State) -> c_int,
+ f: unsafe extern "C" fn(*mut ffi::lua_State) -> c_int, // Must be "C-unwind" after stabilizing
) -> Result<()> {
let stack_start = ffi::lua_gettop(state) - nargs;
- ffi::lua_pushcfunction(state, error_traceback);
+ ffi::lua_pushcfunction(state, ffi::safe::error_traceback);
ffi::lua_pushcfunction(state, f);
if nargs > 0 {
ffi::lua_rotate(state, stack_start + 1, 2);
@@ -95,84 +94,12 @@ pub unsafe fn protect_lua(
}
}
-// Call a function that calls into the Lua API and may trigger a Lua error (longjmp) in a safe way.
-// Wraps the inner function in a call to `lua_pcall`, so the inner function only has access to a
-// limited lua stack. `nargs` and `nresults` are similar to the parameters of `lua_pcall`, but the
-// given function return type is not the return value count, instead the inner function return
-// values are assumed to match the `nresults` param. Internally uses 3 extra stack spaces, and does
-// not call checkstack. Provided function must *not* panic, and since it will generally be
-// lonjmping, should not contain any values that implement Drop.
-pub unsafe fn protect_lua_closure<F, R>(
- state: *mut ffi::lua_State,
- nargs: c_int,
- nresults: c_int,
- f: F,
-) -> Result<R>
-where
- F: Fn(*mut ffi::lua_State) -> R,
- R: Copy,
-{
- union URes<R: Copy> {
- uninit: (),
- init: R,
- }
-
- struct Params<F, R: Copy> {
- function: F,
- result: URes<R>,
- nresults: c_int,
- }
-
- unsafe extern "C" fn do_call<F, R>(state: *mut ffi::lua_State) -> c_int
- where
- R: Copy,
- F: Fn(*mut ffi::lua_State) -> R,
- {
- let params = ffi::lua_touserdata(state, -1) as *mut Params<F, R>;
- ffi::lua_pop(state, 1);
-
- (*params).result.init = ((*params).function)(state);
-
- if (*params).nresults == ffi::LUA_MULTRET {
- ffi::lua_gettop(state)
- } else {
- (*params).nresults
- }
- }
-
- let stack_start = ffi::lua_gettop(state) - nargs;
-
- ffi::lua_pushcfunction(state, error_traceback);
- ffi::lua_pushcfunction(state, do_call::<F, R>);
- if nargs > 0 {
- ffi::lua_rotate(state, stack_start + 1, 2);
- }
-
- let mut params = Params {
- function: f,
- result: URes { uninit: () },
- nresults,
- };
-
- ffi::lua_pushlightuserdata(state, &mut params as *mut Params<F, R> as *mut c_void);
- let ret = ffi::lua_pcall(state, nargs + 1, nresults, stack_start + 1);
- ffi::lua_remove(state, stack_start + 1);
-
- if ret == ffi::LUA_OK {
- // LUA_OK is only returned when the do_call function has completed successfully, so
- // params.result is definitely initialized.
- Ok(params.result.init)
- } else {
- Err(pop_error(state, ret))
- }
-}
-
-// Pops an error off of the stack and returns it. The specific behavior depends on the type of the
+// Pops an error off of the stack and returns it. The specific behavior depends on the type of the
// error at the top of the stack:
// 1) If the error is actually a WrappedPanic, this will continue the panic.
// 2) If the error on the top of the stack is actually a WrappedError, just returns it.
// 3) Otherwise, interprets the error as the appropriate lua error.
-// Uses 2 stack spaces, does not call lua_checkstack.
+// Uses 2 stack spaces, does not call checkstack.
pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error {
mlua_debug_assert!(
err_code != ffi::LUA_OK && err_code != ffi::LUA_YIELD,
@@ -189,7 +116,7 @@ pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error {
Error::PreviouslyResumedPanic
}
} else {
- let err_string = to_string(state, -1).into_owned();
+ let err_string = to_string(state, -1);
ffi::lua_pop(state, 1);
match err_code {
@@ -218,22 +145,9 @@ pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error {
}
}
-// Internally uses 4 stack spaces, does not call checkstack
-pub unsafe fn push_string<S: ?Sized + AsRef<[u8]>>(
- state: *mut ffi::lua_State,
- s: &S,
-) -> Result<()> {
- protect_lua_closure(state, 0, 1, |state| {
- let s = s.as_ref();
- ffi::lua_pushlstring(state, s.as_ptr() as *const c_char, s.len());
- })
-}
-
-// Internally uses 4 stack spaces, does not call checkstack
+// Internally uses 3 stack spaces, does not call checkstack
pub unsafe fn push_userdata<T>(state: *mut ffi::lua_State, t: T) -> Result<()> {
- let ud = protect_lua_closure(state, 0, 1, move |state| {
- ffi::lua_newuserdata(state, mem::size_of::<T>()) as *mut T
- })?;
+ let ud = ffi::safe::lua_newuserdata(state, mem::size_of::<T>())? as *mut T;
ptr::write(ud, t);
Ok(())
}
@@ -245,9 +159,9 @@ pub unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut
}
// Pops the userdata off of the top of the stack and returns it to rust, invalidating the lua
-// userdata and gives it the special "destructed" userdata metatable. Userdata must not have been
-// previously invalidated, and this method does not check for this. Uses 1 extra stack space and
-// does not call checkstack
+// userdata and gives it the special "destructed" userdata metatable. Userdata must not have been
+// previously invalidated, and this method does not check for this.
+// Uses 1 extra stack space and does not call checkstack.
pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
// We set the metatable of userdata on __gc to a special table with no __gc method and with
// metamethods that trigger an error on access. We do this so that it will not be double
@@ -261,18 +175,11 @@ pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
ptr::read(ud)
}
-// Pushes the userdata and attaches a metatable with __gc method
-// Internally uses 5 stack spaces, does not call checkstack
+// Pushes the userdata and attaches a metatable with __gc method.
+// Internally uses 4 stack spaces, does not call checkstack.
pub unsafe fn push_gc_userdata<T: Any>(state: *mut ffi::lua_State, t: T) -> Result<()> {
- push_meta_gc_userdata::<T, T>(state, t)
-}
-
-pub unsafe fn push_meta_gc_userdata<MT: Any, T>(state: *mut ffi::lua_State, t: T) -> Result<()> {
- let ud = protect_lua_closure(state, 0, 1, move |state| {
- ffi::lua_newuserdata(state, mem::size_of::<T>()) as *mut T
- })?;
- ptr::write(ud, t);
- get_gc_metatable_for::<MT>(state);
+ push_userdata(state, t)?;
+ get_gc_metatable_for::<T>(state);
ffi::lua_setmetatable(state, -2);
Ok(())
}
@@ -292,9 +199,9 @@ pub unsafe fn get_gc_userdata<T: Any>(state: *mut ffi::lua_State, index: c_int)
ud
}
-// Populates the given table with the appropriate members to be a userdata metatable for the given
-// type. This function takes the given table at the `metatable` index, and adds an appropriate `__gc`
-// member to it for the given type and a `__metatable` entry to protect the table from script access.
+// Populates the given table with the appropriate members to be a userdata metatable for the given type.
+// This function takes the given table at the `metatable` index, and adds an appropriate `__gc` member
+// to it for the given type and a `__metatable` entry to protect the table from script access.
// The function also, if given a `field_getters` or `methods` tables, will create an `__index` metamethod
// (capturing previous one) to lookup in `field_getters` first, then `methods` and falling back to the
// captured `__index` if no matches found.
@@ -307,98 +214,10 @@ pub unsafe fn init_userdata_metatable<T>(
field_setters: Option<c_int>,
methods: Option<c_int>,
) -> Result<()> {
- // Wrapper to lookup in `field_getters` first, then `methods`, ending original `__index`.
- // Used only if `field_getters` or `methods` set.
- unsafe extern "C" fn meta_index_impl(state: *mut ffi::lua_State) -> c_int {
- // stack: self, key
- ffi::luaL_checkstack(state, 2, ptr::null());
-
- // lookup in `field_getters` table
- if ffi::lua_isnil(state, ffi::lua_upvalueindex(2)) == 0 {
- ffi::lua_pushvalue(state, -1); // `key` arg
- if ffi::lua_rawget(state, ffi::lua_upvalueindex(2)) != ffi::LUA_TNIL {
- ffi::lua_insert(state, -3); // move function
- ffi::lua_pop(state, 1); // remove `key`
- ffi::lua_call(state, 1, 1);
- return 1;
- }
- ffi::lua_pop(state, 1); // pop the nil value
- }
- // lookup in `methods` table
- if ffi::lua_isnil(state, ffi::lua_upvalueindex(3)) == 0 {
- ffi::lua_pushvalue(state, -1); // `key` arg
- if ffi::lua_rawget(state, ffi::lua_upvalueindex(3)) != ffi::LUA_TNIL {
- ffi::lua_insert(state, -3);
- ffi::lua_pop(state, 2);
- return 1;
- }
- ffi::lua_pop(state, 1); // pop the nil value
- }
-
- // lookup in `__index`
- ffi::lua_pushvalue(state, ffi::lua_upvalueindex(1));
- match ffi::lua_type(state, -1) {
- ffi::LUA_TNIL => {
- ffi::lua_pop(state, 1); // pop the nil value
- let field = ffi::lua_tostring(state, -1);
- ffi::luaL_error(state, cstr!("attempt to get an unknown field '%s'"), field);
- }
- ffi::LUA_TTABLE => {
- ffi::lua_insert(state, -2);
- ffi::lua_gettable(state, -2);
- }
- ffi::LUA_TFUNCTION => {
- ffi::lua_insert(state, -3);
- ffi::lua_call(state, 2, 1);
- }
- _ => unreachable!(),
- }
-
- 1
- }
-
- // Similar to `meta_index_impl`, checks `field_setters` table first, then `__newindex` metamethod.
- // Used only if `field_setters` set.
- unsafe extern "C" fn meta_newindex_impl(state: *mut ffi::lua_State) -> c_int {
- // stack: self, key, value
- ffi::luaL_checkstack(state, 2, ptr::null());
-
- // lookup in `field_setters` table
- ffi::lua_pushvalue(state, -2); // `key` arg
- if ffi::lua_rawget(state, ffi::lua_upvalueindex(2)) != ffi::LUA_TNIL {
- ffi::lua_remove(state, -3); // remove `key`
- ffi::lua_insert(state, -3); // move function
- ffi::lua_call(state, 2, 0);
- return 0;
- }
- ffi::lua_pop(state, 1); // pop the nil value
-
- // lookup in `__newindex`
- ffi::lua_pushvalue(state, ffi::lua_upvalueindex(1));
- match ffi::lua_type(state, -1) {
- ffi::LUA_TNIL => {
- ffi::lua_pop(state, 1); // pop the nil value
- let field = ffi::lua_tostring(state, -2);
- ffi::luaL_error(state, cstr!("attempt to set an unknown field '%s'"), field);
- }
- ffi::LUA_TTABLE => {
- ffi::lua_insert(state, -3);
- ffi::lua_settable(state, -3);
- }
- ffi::LUA_TFUNCTION => {
- ffi::lua_insert(state, -4);
- ffi::lua_call(state, 3, 0);
- }
- _ => unreachable!(),
- }
-
- 0
- }
-
ffi::lua_pushvalue(state, metatable);
if field_getters.is_some() || methods.is_some() {
- push_string(state, "__index")?;
+ ffi::safe::lua_pushstring(state, "__index")?;
ffi::lua_pushvalue(state, -1);
let index_type = ffi::lua_rawget(state, -3);
@@ -411,49 +230,35 @@ pub unsafe fn init_userdata_metatable<T>(
ffi::lua_pushnil(state);
}
}
- protect_lua_closure(state, 3, 1, |state| {
- ffi::lua_pushcclosure(state, meta_index_impl, 3);
- })?;
+ ffi::safe::lua_pushcclosure(state, ffi::safe::meta_index_impl, 3)?;
}
_ => mlua_panic!("improper __index type {}", index_type),
}
- protect_lua_closure(state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawset(state, -3)?;
}
if let Some(field_setters) = field_setters {
- push_string(state, "__newindex")?;
+ ffi::safe::lua_pushstring(state, "__newindex")?;
ffi::lua_pushvalue(state, -1);
let newindex_type = ffi::lua_rawget(state, -3);
match newindex_type {
ffi::LUA_TNIL | ffi::LUA_TTABLE | ffi::LUA_TFUNCTION => {
ffi::lua_pushvalue(state, field_setters);
- protect_lua_closure(state, 2, 1, |state| {
- ffi::lua_pushcclosure(state, meta_newindex_impl, 2);
- })?;
+ ffi::safe::lua_pushcclosure(state, ffi::safe::meta_newindex_impl, 2)?;
}
_ => mlua_panic!("improper __newindex type {}", newindex_type),
}
- protect_lua_closure(state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawset(state, -3)?;
}
- push_string(state, "__gc")?;
- ffi::lua_pushcfunction(state, userdata_destructor::<T>);
- protect_lua_closure(state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_pushrclosure(state, userdata_destructor::<T>, 0)?;
+ ffi::safe::lua_rawsetfield(state, -2, "__gc")?;
- push_string(state, "__metatable")?;
ffi::lua_pushboolean(state, 0);
- protect_lua_closure(state, 3, 1, |state| {
- ffi::lua_rawset(state, -3);
- })?;
+ ffi::safe::lua_rawsetfield(state, -2, "__metatable")?;
ffi::lua_pop(state, 1);
@@ -479,29 +284,11 @@ pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c
// This function uses some of the bottom of the stack for error handling, the given callback will be
// given the number of arguments available as an argument, and should return the number of returns
// as normal, but cannot assume that the arguments available start at 0.
-pub unsafe fn callback_error<R, F>(state: *mut ffi::lua_State, f: F) -> R
+pub unsafe fn callback_error<F>(state: *mut ffi::lua_State, f: F) -> c_int
where
- F: FnOnce(c_int) -> Result<R>,
+ F: FnOnce(c_int) -> Result<c_int>,
{
- let nargs = ffi::lua_gettop(state);
-
- // We need one extra stack space to store preallocated memory, and at least 3 stack spaces
- // overall for handling error metatables
- let extra_stack = if nargs < 3 { 3 - nargs } else { 1 };
- ffi::luaL_checkstack(
- state,
- extra_stack,
- cstr!("not enough stack space for callback error handling"),
- );
-
- // We cannot shadow rust errors with Lua ones, we pre-allocate enough memory to store a wrapped
- // error or panic *before* we proceed.
- let ud = ffi::lua_newuserdata(
- state,
- mem::size_of::<WrappedError>().max(mem::size_of::<WrappedPanic>()),
- );
- ffi::lua_rotate(state, 1, 1);
-
+ let nargs = ffi::lua_gettop(state) - 1;
match catch_unwind(AssertUnwindSafe(|| f(nargs))) {
Ok(Ok(r)) => {
ffi::lua_remove(state, 1);
@@ -509,68 +296,54 @@ where
}
Ok(Err(err)) => {
ffi::lua_settop(state, 1);
- ptr::write(ud as *mut WrappedError, WrappedError(err));
+ let error_ud = ffi::lua_touserdata(state, 1);
+ ptr::write(error_ud as *mut WrappedError, WrappedError(err));
get_gc_metatable_for::<WrappedError>(state);
ffi::lua_setmetatable(state, -2);
- ffi::lua_error(state)
+ -1
}
Err(p) => {
ffi::lua_settop(state, 1);
- ptr::write(ud as *mut WrappedPanic, WrappedPanic(Some(p)));
+ let error_ud = ffi::lua_touserdata(state, 1);
+ ptr::write(error_ud as *mut WrappedPanic, WrappedPanic(Some(p)));
get_gc_metatable_for::<WrappedPanic>(state);
ffi::lua_setmetatable(state, -2);
- ffi::lua_error(state)
+ -1
}
}
}
-// Takes an error at the top of the stack, and if it is a WrappedError, converts it to an
-// Error::CallbackError with a traceback, if it is some lua type, prints the error along with a
-// traceback, and if it is a WrappedPanic, does not modify it. This function does its best to avoid
-// triggering another error and shadowing previous rust errors, but it may trigger Lua errors that
-// shadow rust errors under certain memory conditions. This function ensures that such behavior
-// will *never* occur with a rust panic, however.
-pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
- // I believe luaL_traceback requires this much free stack to not error.
- const LUA_TRACEBACK_STACK: c_int = 11;
-
- if ffi::lua_checkstack(state, 2) == 0 {
- // If we don't have enough stack space to even check the error type, do nothing so we don't
- // risk shadowing a rust panic.
- } else if let Some(error) = get_wrapped_error(state, -1).as_ref() {
- // lua_newuserdata and luaL_traceback may error, but nothing that implements Drop should be
- // on the rust stack at this time.
- let ud = ffi::lua_newuserdata(state, mem::size_of::<WrappedError>()) as *mut WrappedError;
- let traceback = if ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0 {
- ffi::luaL_traceback(state, state, ptr::null(), 0);
-
- let traceback = to_string(state, -1).into_owned();
- ffi::lua_pop(state, 1);
- traceback
- } else {
- "<not enough stack space for traceback>".to_owned()
- };
-
- let error = error.clone();
- ffi::lua_remove(state, -2);
-
- ptr::write(
- ud,
- WrappedError(Error::CallbackError {
- traceback,
- cause: Arc::new(error),
- }),
- );
- get_gc_metatable_for::<WrappedError>(state);
- ffi::lua_setmetatable(state, -2);
- } else if get_gc_userdata::<WrappedPanic>(state, -1).is_null()
- && ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0
- {
- let s = ffi::luaL_tolstring(state, -1, ptr::null_mut());
- ffi::luaL_traceback(state, state, s, 0);
- ffi::lua_remove(state, -2);
- }
- 1
+#[no_mangle]
+pub unsafe extern "C" fn wrapped_error_traceback(
+ state: *mut ffi::lua_State,
+ error_idx: c_int,
+ error_ud: *mut c_void,
+ has_traceback: c_int,
+) {
+ let error = mlua_expect!(
+ get_wrapped_error(state, error_idx).as_ref(),
+ "cannot get <WrappedError>"
+ );
+ let traceback = if has_traceback != 0 {
+ let traceback = to_string(state, -1);
+ ffi::lua_pop(state, 1);
+ traceback
+ } else {
+ "<not enough stack space for traceback>".to_owned()
+ };
+
+ let error = error.clone();
+ ffi::lua_remove(state, -2); // Remove original error
+
+ ptr::write(
+ error_ud as *mut WrappedError,
+ WrappedError(Error::CallbackError {
+ traceback,
+ cause: Arc::new(error),
+ }),
+ );
+ get_gc_metatable_for::<WrappedError>(state);
+ ffi::lua_setmetatable(state, -2);
}
// Does not call lua_checkstack, uses 1 stack space.
@@ -602,7 +375,8 @@ pub unsafe fn push_wrapped_error(state: *mut ffi::lua_State, err: Error) -> Resu
}
// Checks if the value at the given index is a WrappedError, and if it is returns a pointer to it,
-// otherwise returns null. Uses 2 stack spaces and does not call lua_checkstack.
+// otherwise returns null.
+// Uses 2 stack spaces and does not call checkstack.
pub unsafe fn get_wrapped_error(state: *mut ffi::lua_State, index: c_int) -> *const Error {
let ud = get_gc_userdata::<WrappedError>(state, index);
if ud.is_null() {
@@ -611,11 +385,19 @@ pub unsafe fn get_wrapped_error(state: *mut ffi::lua_State, index: c_int) -> *co
&(*ud).0
}
+#[no_mangle]
+pub unsafe extern "C" fn mlua_get_wrapped_error(
+ state: *mut ffi::lua_State,
+ index: c_int,
+) -> *const c_void {
+ get_wrapped_error(state, index) as *const c_void
+}
+
// Initialize the internal (with __gc) metatable for a type T
pub unsafe fn init_gc_metatable_for<T: Any>(
state: *mut ffi::lua_State,
- customize_fn: Option<fn(*mut ffi::lua_State)>,
-) {
+ customize_fn: Option<fn(*mut ffi::lua_State) -> Result<()>>,
+) -> Result<*const u8> {
let type_id = TypeId::of::<T>();
let ref_addr = {
@@ -628,21 +410,21 @@ pub unsafe fn init_gc_metatable_for<T: Any>(
&mt_cache[&type_id] as *const u8
};
- ffi::lua_newtable(state);
+ ffi::safe::lua_createtable(state, 0, 3)?;
- ffi::lua_pushstring(state, cstr!("__gc"));
- ffi::lua_pushcfunction(state, userdata_destructor::<T>);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_pushrclosure(state, userdata_destructor::<T>, 0)?;
+ ffi::safe::lua_rawsetfield(state, -2, "__gc")?;
- ffi::lua_pushstring(state, cstr!("__metatable"));
ffi::lua_pushboolean(state, 0);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_rawsetfield(state, -2, "__metatable")?;
if let Some(f) = customize_fn {
- f(state)
+ f(state)?;
}
- ffi::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, ref_addr as *mut c_void);
+ ffi::safe::lua_rawsetp(state, ffi::LUA_REGISTRYINDEX, ref_addr as *mut c_void)?;
+
+ Ok(ref_addr)
}
pub unsafe fn get_gc_metatable_for<T: Any>(state: *mut ffi::lua_State) {
@@ -651,19 +433,21 @@ pub unsafe fn get_gc_metatable_for<T: Any>(state: *mut ffi::lua_State) {
let mt_cache = mlua_expect!(METATABLE_CACHE.lock(), "cannot lock metatable cache");
mlua_expect!(mt_cache.get(&type_id), "gc metatable does not exist") as *const u8
};
- ffi::lua_rawgetp(state, ffi::LUA_REGISTRYINDEX, ref_addr as *mut c_void);
+ ffi::lua_rawgetp(state, ffi::LUA_REGISTRYINDEX, ref_addr as *const c_void);
}
// Initialize the error, panic, and destructed userdata metatables.
-pub unsafe fn init_error_registry(state: *mut ffi::lua_State) {
+// Returns address of WrappedError and WrappedPanic metatables in Lua registry.
+pub unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(*const u8, *const u8)> {
assert_stack(state, 8);
// Create error and panic metatables
unsafe extern "C" fn error_tostring(state: *mut ffi::lua_State) -> c_int {
- let err_buf = callback_error(state, |_| {
+ callback_error(state, |_| {
check_stack(state, 3)?;
- if let Some(error) = get_wrapped_error(state, -1).as_ref() {
+
+ let err_buf = if let Some(error) = get_wrapped_error(state, -1).as_ref() {
ffi::lua_pushlightuserdata(
state,
&ERROR_PRINT_BUFFER_KEY as *const u8 as *mut c_void,
@@ -700,146 +484,149 @@ pub unsafe fn init_error_registry(state: *mut ffi::lua_State) {
} else {
// I'm not sure whether this is possible to trigger without bugs in mlua?
Err(Error::UserDataTypeMismatch)
- }
- });
+ }?;
- ffi::lua_pushlstring(
- state,
- (*err_buf).as_ptr() as *const c_char,
- (*err_buf).len(),
- );
- (*err_buf).clear();
- 1
+ ffi::safe::lua_pushstring(state, &*err_buf)?;
+ (*err_buf).clear();
+
+ Ok(1)
+ })
}
- init_gc_metatable_for::<WrappedError>(
+ let wrapped_error_key = init_gc_metatable_for::<WrappedError>(
state,
Some(|state| {
- ffi::lua_pushstring(state, cstr!("__tostring"));
- ffi::lua_pushcfunction(state, error_tostring);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_pushrclosure(state, error_tostring, 0)?;
+ ffi::safe::lua_rawsetfield(state, -2, "__tostring")
}),
- );
+ )?;
- init_gc_metatable_for::<WrappedPanic>(
+ let wrapped_panic_key = init_gc_metatable_for::<WrappedPanic>(
state,
Some(|state| {
- ffi::lua_pushstring(state, cstr!("__tostring"));
- ffi::lua_pushcfunction(state, error_tostring);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_pushrclosure(state, error_tostring, 0)?;
+ ffi::safe::lua_rawsetfield(state, -2, "__tostring")
}),
- );
+ )?;
// Create destructed userdata metatable
unsafe extern "C" fn destructed_error(state: *mut ffi::lua_State) -> c_int {
- ffi::luaL_checkstack(state, 2, ptr::null());
- let ud = ffi::lua_newuserdata(state, mem::size_of::<WrappedError>()) as *mut WrappedError;
- ptr::write(ud, WrappedError(Error::CallbackDestructed));
- get_gc_metatable_for::<WrappedError>(state);
- ffi::lua_setmetatable(state, -2);
- ffi::lua_error(state)
+ callback_error(state, |_| {
+ check_stack(state, 3)?;
+ let ud = ffi::safe::lua_newuserdata(state, mem::size_of::<WrappedError>())?
+ as *mut WrappedError;
+ ptr::write(ud, WrappedError(Error::CallbackDestructed));
+ get_gc_metatable_for::<WrappedError>(state);
+ ffi::lua_setmetatable(state, -2);
+ Ok(-1) // to trigger lua_error
+ })
}
ffi::lua_pushlightuserdata(
state,
&DESTRUCTED_USERDATA_METATABLE as *const u8 as *mut c_void,
);
- ffi::lua_newtable(state);
+ ffi::safe::lua_createtable(state, 0, 26)?;
+ ffi::safe::lua_pushrclosure(state, destructed_error, 0)?;
for &method in &[
- cstr!("__add"),
- cstr!("__sub"),
- cstr!("__mul"),
- cstr!("__div"),
- cstr!("__mod"),
- cstr!("__pow"),
- cstr!("__unm"),
+ "__add",
+ "__sub",
+ "__mul",
+ "__div",
+ "__mod",
+ "__pow",
+ "__unm",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__idiv"),
+ "__idiv",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__band"),
+ "__band",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__bor"),
+ "__bor",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__bxor"),
+ "__bxor",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__bnot"),
+ "__bnot",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__shl"),
+ "__shl",
#[cfg(any(feature = "lua54", feature = "lua53"))]
- cstr!("__shr"),
- cstr!("__concat"),
- cstr!("__len"),
- cstr!("__eq"),
- cstr!("__lt"),
- cstr!("__le"),
- cstr!("__index"),
- cstr!("__newindex"),
- cstr!("__call"),
- cstr!("__tostring"),
+ "__shr",
+ "__concat",
+ "__len",
+ "__eq",
+ "__lt",
+ "__le",
+ "__index",
+ "__newindex",
+ "__call",
+ "__tostring",
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
- cstr!("__pairs"),
+ "__pairs",
#[cfg(any(feature = "lua53", feature = "lua52"))]
- cstr!("__ipairs"),
+ "__ipairs",
#[cfg(feature = "lua54")]
- cstr!("__close"),
+ "__close",
] {
- ffi::lua_pushstring(state, method);
- ffi::lua_pushcfunction(state, destructed_error);
- ffi::lua_rawset(state, -3);
+ ffi::lua_pushvalue(state, -1);
+ ffi::safe::lua_rawsetfield(state, -3, method)?;
}
+ ffi::lua_pop(state, 1);
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
+ ffi::safe::lua_rawset(state, ffi::LUA_REGISTRYINDEX)?;
// Create error print buffer
ffi::lua_pushlightuserdata(state, &ERROR_PRINT_BUFFER_KEY as *const u8 as *mut c_void);
- let ud = ffi::lua_newuserdata(state, mem::size_of::<String>()) as *mut String;
+ let ud = ffi::safe::lua_newuserdata(state, mem::size_of::<String>())? as *mut String;
ptr::write(ud, String::new());
- ffi::lua_newtable(state);
- ffi::lua_pushstring(state, cstr!("__gc"));
- ffi::lua_pushcfunction(state, userdata_destructor::<String>);
- ffi::lua_rawset(state, -3);
+ ffi::safe::lua_createtable(state, 0, 1)?;
+ ffi::safe::lua_pushrclosure(state, userdata_destructor::<String>, 0)?;
+ ffi::safe::lua_rawsetfield(state, -2, "__gc")?;
+
ffi::lua_setmetatable(state, -2);
- ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
+ ffi::safe::lua_rawset(state, ffi::LUA_REGISTRYINDEX)?;
+
+ Ok((wrapped_error_key, wrapped_panic_key))
}
-struct WrappedError(pub Error);
+pub(crate) struct WrappedError(pub Error);
pub(crate) struct WrappedPanic(pub Option<Box<dyn Any + Send + 'static>>);
// Converts the given lua value to a string in a reasonable format without causing a Lua error or
// panicking.
-unsafe fn to_string<'a>(state: *mut ffi::lua_State, index: c_int) -> Cow<'a, str> {
+unsafe fn to_string(state: *mut ffi::lua_State, index: c_int) -> String {
match ffi::lua_type(state, index) {
- ffi::LUA_TNONE => "<none>".into(),
- ffi::LUA_TNIL => "<nil>".into(),
- ffi::LUA_TBOOLEAN => (ffi::lua_toboolean(state, index) != 1).to_string().into(),
+ ffi::LUA_TNONE => "<none>".to_string(),
+ ffi::LUA_TNIL => "<nil>".to_string(),
+ ffi::LUA_TBOOLEAN => (ffi::lua_toboolean(state, index) != 1).to_string(),
ffi::LUA_TLIGHTUSERDATA => {
- format!("<lightuserdata {:?}>", ffi::lua_topointer(state, index)).into()
+ format!("<lightuserdata {:?}>", ffi::lua_topointer(state, index))
}
ffi::LUA_TNUMBER => {
let mut isint = 0;
let i = ffi::lua_tointegerx(state, -1, &mut isint);
if isint == 0 {
- ffi::lua_tonumber(state, index).to_string().into()
+ ffi::lua_tonumber(state, index).to_string()
} else {
- i.to_string().into()
+ i.to_string()
}
}
ffi::LUA_TSTRING => {
let mut size = 0;
+ // This will not trigger a 'm' error, because the reference is guaranteed to be of
+ // string type
let data = ffi::lua_tolstring(state, index, &mut size);
- String::from_utf8_lossy(slice::from_raw_parts(data as *const u8, size))
+ String::from_utf8_lossy(slice::from_raw_parts(data as *const u8, size)).into_owned()
}
- ffi::LUA_TTABLE => format!("<table {:?}>", ffi::lua_topointer(state, index)).into(),
- ffi::LUA_TFUNCTION => format!("<function {:?}>", ffi::lua_topointer(state, index)).into(),
- ffi::LUA_TUSERDATA => format!("<userdata {:?}>", ffi::lua_topointer(state, index)).into(),
- ffi::LUA_TTHREAD => format!("<thread {:?}>", ffi::lua_topointer(state, index)).into(),
- _ => "<unknown>".into(),
+ ffi::LUA_TTABLE => format!("<table {:?}>", ffi::lua_topointer(state, index)),
+ ffi::LUA_TFUNCTION => format!("<function {:?}>", ffi::lua_topointer(state, index)),
+ ffi::LUA_TUSERDATA => format!("<userdata {:?}>", ffi::lua_topointer(state, index)),
+ ffi::LUA_TTHREAD => format!("<thread {:?}>", ffi::lua_topointer(state, index)),
+ _ => "<unknown>".to_string(),
}
}
diff --git a/src/value.rs b/src/value.rs
index 15da2ec..082436e 100644
--- a/src/value.rs
+++ b/src/value.rs
@@ -1,3 +1,4 @@
+// OK
use std::iter::{self, FromIterator};
use std::{slice, str, vec};
@@ -8,7 +9,6 @@ use {
};
use crate::error::{Error, Result};
-use crate::ffi;
use crate::function::Function;
use crate::lua::Lua;
use crate::string::String;
@@ -96,8 +96,8 @@ impl<'lua> PartialEq for Value<'lua> {
(Value::Boolean(a), Value::Boolean(b)) => a == b,
(Value::LightUserData(a), Value::LightUserData(b)) => a == b,
(Value::Integer(a), Value::Integer(b)) => *a == *b,
- (Value::Integer(a), Value::Number(b)) => *a as ffi::lua_Number == *b,
- (Value::Number(a), Value::Integer(b)) => *a == *b as ffi::lua_Number,
+ (Value::Integer(a), Value::Number(b)) => *a as Number == *b,
+ (Value::Number(a), Value::Integer(b)) => *a == *b as Number,
(Value::Number(a), Value::Number(b)) => *a == *b,
(Value::String(a), Value::String(b)) => a == b,
(Value::Table(a), Value::Table(b)) => a == b,
diff --git a/tests/tests.rs b/tests/tests.rs
index dc16c15..f2b2d4a 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -221,6 +221,7 @@ fn test_coercion() -> Result<()> {
int = 123
str = "123"
num = 123.0
+ func = function() end
"#,
)
.exec()?;
@@ -229,6 +230,7 @@ fn test_coercion() -> Result<()> {
assert_eq!(globals.get::<_, String>("int")?, "123");
assert_eq!(globals.get::<_, i32>("str")?, 123);
assert_eq!(globals.get::<_, i32>("num")?, 123);
+ assert!(globals.get::<_, String>("func").is_err());
Ok(())
}