lua_next segfault on LUA_ENVIRONINDEX? - lua

The following C program that uses the Lua API lua_next function to try and begin iterating LUA_ENVIRONINDEX table crashes in call to lua_next...
#include <lua5.1/lua.hpp>
int main() {
lua_State* L = luaL_newstate();
lua_pushnil(L);
lua_next(L, LUA_ENVIRONINDEX);
}
Any idea why?

The manual says that LUA_ENVIRONINDEX "gives the environment of the running C function". In your code, there is no "running C function" in the sense of Lua: main was not called from Lua.
Take lua.c. If you put your code in main, then there is a crash, as you have found out. If you put your code in pmain, which is called from Lua, then there is no crash.

Related

FFI Metatype : "cannot change a protected metatable"

If a script uses ffi.metatype and crashes unexpectedly,the next script startup produces this error : “cannot change a protected metatable”,this makes debugging real hard as I have to restart my game each time,any way to circumvent this?
Here’s a test script that demonstrates this issue,make sure to run it twice.
ffi.cdef[[
typedef struct {
float x,y;
} Crash;
]]
local Crash_Metatype = ffi.metatype("Crash",{})
print(x + y)
You can't call ffi.metatype on the same C type twice. Either set a variable after doing so and don't do so again if it's already set, or wrap the call to it in pcall so you can ignore that error.

LuaBind and package.loadlib

I'm trying to go through the tutorial with luabind here, http://www.rasterbar.com/products/luabind/docs.html, however i'm having trouble loading the library. I'm currently using version 5.1 of lua, so I believe I would use package.loadlib instead of loadlib. I made a simple dll which is this:
#include <iostream>
#include <luabind\luabind.hpp>
void greet()
{
std::cout << "Hello world!\n";
}
extern "C" int init(lua_State* L)
{
luabind::open(L);
luabind::module(L)
[
luabind::def("greet", &greet)
];
return 0;
}
This builds just fine. However I get an error in lua when I try to run this code:
package.loadlib("LuaTestLib.dll", "init")
greet()
It states that greet is nil. How do I load the functions from the dll properly?
From the first two sentences of package.loadlib's documentation:
Dynamically links the host program with the C library libname. Inside this library, looks for a function funcname and returns this function as a C function.
(emphasis added)
This doesn't execute funcname. It simply returns it as a function for you to call. You still have to call it:
package.loadlib("LuaTestLib.dll", "init")()

D Lua doesn't get metatable

I'm trying to get Lua to work with the new programming language D.
Everything works fine (library, lua52.dll, etc.) but luaL_getmetatable crashes.
Originally, the function was not defined in dlua, but I added it:
//C #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
void luaL_getmetatable(lua_State* L, const(char)* s) {
lua_getfield(L, LUA_REGISTRYINDEX, s);
}
But when I run:
L = lua_open();
luaL_openlibs(L);
// prevent script kiddies
luaL_dostring(L, "os = nil; io = nil");
// reprogram 'print'
luaL_newmetatable(L, "vector");
luaL_getmetatable(L, "vector"); // CRASH
it crashes. Any ideas why this is?
It sounds like you are using the ancient dlua bindings, not LuaD, which has always had luaL_getmetatable.
However, both these bindings as well as your code are for Lua 5.1, not 5.2; make sure you link to the correct version of Lua. There is no lua_open in Lua 5.2 (and it's deprecated in 5.1).
If you encounter the same problem after linking to the right library, I recommend compiling Lua with the macro LUA_USE_APICHECK set and trying again to see exactly what went wrong.
Maybe you should take a look at the existing Lua-Bindings for D, LuaD.

luaL_dostring() crashes when given script has syntax error

i try to integrate Lua in a embedded project using GCC on a Cortex-M4. i am able to load and run a Lua script, calling Lua functions from C, calling C functions from Lua. but the C program crashes (HardFault_Handler trap rises) when the given script passed as parameter in luaL_dostring() contains any Lua syntax errors.
here the relevant C code that crashes due to the syntax error in Lua:
//create Lua VM...
luaVm = lua_newstate(luaAlloc, NULL);
//load libraries...
luaopen_base(luaVm);
luaopen_math(luaVm);
luaopen_table(luaVm);
luaopen_string(luaVm);
//launch script...
luaL_dostring(luaVm, "function onTick()\n"
" locaal x = 7\n" //syntax error
"end\n"
"\n" );
when doing the same with correct Lua syntax, then it works:
luaL_dostring(luaVm, "function onTick()\n"
" local x = 7\n"
"end\n"
"\n" );
when debugging and stepping through luaL_dostring(), i can follow the Lua parsing line for line, and when reaching the line with the syntax error, then the C program crashes.
can anybody help? thanks.
have disabled setjmp/longjmp in Lua source code in the following way:
//#define LUAI_THROW(L,c) longjmp((c)->b, 1) //TODO oli4 orig
//#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } //TODO oli4 orig
#define LUAI_THROW(L,c) while(1) //TODO oli4 special
#define LUAI_TRY(L,c,a) { a } //TODO oli4 special
...so there is no setjmp/longjmp used anymore, but i still have the crash :-(
must have another cause???
found the problem: it is the sprintf function called on Lua syntax error. in fact, on my platform sprintf seems not support floating point presentation. so i changed luaconf.h the following way, limiting the presentation to integer format.
//#define LUA_NUMBER_FMT "%.14g"
#define LUA_NUMBER_FMT "%d"
must have another cause???
Yes: you can't use Lua here.
Lua's error handling system is built on a framework of setjmp/longjump. You can't just make LUAI_THROW and LUAI_TRY do nothing. That means lua_error and all internal error handling stops working. Syntax errors are part of Lua's internal error handling.
If your C compiler doesn't provide proper support for the C standard library, then Lua is simply not going to be functional in that environment. You might try LuaJIT, but I doubt that will be any better.
#define LUAI_THROW(L,c) c->throwed = true
#define LUAI_TRY(L,c,a) \
__try { a } __except(filter()) { if ((c)->status == 0 && ((c)->throwed)) (c)->status = -1; }
#define luai_jmpbuf int /* dummy variable */
struct lua_longjmp {
struct lua_longjmp *previous;
luai_jmpbuf b;
volatile int status; /* error code */
bool throwed;
};
Works as expected even you build without C++ exceptions

Calling Lua from C

I'm trying to call a user-defined Lua function from C. I've seen some discussion on this, and the solution seems clear. I need to grab the index of the function with luaL_ref(), and save the returned index for use later.
In my case, I've saved the value with luaL_ref, and I'm at a point where my C code needs to invoke the Lua function saved with luaL_ref. For that, I'm using lua_rawgeti as follows:
lua_rawgeti(l, LUA_REGISTRYINDEX, fIndex);
This causes a crash in lua_rawgeti.
The fIndex I'm using is the value I received from luaL_ref, so I'm not sure what's going on here.
EDIT:
I'm running a Lua script as follows:
function errorFunc()
print("Error")
end
function savedFunc()
print("Saved")
end
mylib.save(savedFunc, errorFunc)
I've defined my own Lua library 'mylib', with a C function:
static int save(lua_State *L)
{
int cIdx = myCIndex = luaL_ref(L, LUA_REGISTRYINDEX);
int eIdx = luaL_ref(L, LUA_REGISTRYINDEX);
I save cIdx and eIdx away until a later point in time when I receive some external event at which point I would like to invoke one of the functions set as parameters in my Lua script. Here, (on the same thread, using the same lua_State*), I call:
lua_rawgeti(L, LUA_REGISTRYINDEX, myCIndex);
Which is causing the crash.
My first suggestion is to get it working without storing the function in C at all. Just assign your function to a global in Lua, then in C use the Lua state (L) to get the global, push the args, call the function, and use the results. Once that's working, you've got the basics and know your function is working, you can change the way you get at the function to use the registry. Good luck!
As #Schollii mentioned, I was making this call after doing a lua_close(L).

Resources