lua-api

Test if Lua number is integer or float

这一生的挚爱 提交于 2020-01-03 01:58:26
问题 In my C++ program, I need to know if a Lua variable is an integer number or a floating-point number. The C API provides lua_isnumber() but this function does not distinguish between int/float/double . So far I have worked around this by using modf() : if (lua_isnumber(luaCtx, -1)) // int / unsigned int / float: { luaVarName = lua_tostring(luaCtx, -2); double n = static_cast<double>(lua_tonumber(luaCtx, -1)); // Figure out if int or float: double fractPart, intPart; fractPart = modf(n,

Iterating over table of tables with the Lua C API

喜你入骨 提交于 2019-12-30 07:07:08
问题 I'm trying to iterate over a table of tables in Lua and output: The key of each table. The key / value pair of each entry in each table. Here is the code: void print_table(lua_State *L) { lua_pushnil(L); while(lua_next(L, -2) != 0) { const char *key = lua_tostring(L, -2); if(lua_isstring(L, -1)) printf("%s = %s", key, lua_tostring(L, -1)); else if(lua_isnumber(L, -1)) printf("%s = %d", key, lua_tonumber(L, -1)); else if(lua_istable(L, -1)) { printf("%s", key); PrintTable(L); } lua_pop(L, 1);

Lua - Why are C functions returned as userdata?

≡放荡痞女 提交于 2019-12-24 08:31:00
问题 I'm working on game scripting for my engine and am using a metatable to redirect functions from a table (which stores custom functions and data for players) to a userdata object (which is the main implementation for my Player class) so that users may use self to refer to both. This is how I do my binding in C# in the Player class: state.NewTable("Player"); // Create Player wrapper table state["Player.data"] = this; // Bind Player.data to the Player class state.NewTable("mt"); // Create temp

Get Lua table size in C

徘徊边缘 提交于 2019-12-22 05:22:01
问题 How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); ... } My Lua Code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) It´s printing 0 always. What am I doing wrong? 回答1: lua_objlen returns the length of the object, it doesn't push anything on the stack. Even if it did

Get Lua table size in C

泪湿孤枕 提交于 2019-12-22 05:21:55
问题 How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); ... } My Lua Code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) It´s printing 0 always. What am I doing wrong? 回答1: lua_objlen returns the length of the object, it doesn't push anything on the stack. Even if it did

Create new C library in lua

不想你离开。 提交于 2019-12-20 03:56:08
问题 I want to know how i can create and use a new C library in lua 5.2.3. I can't use dynamic library (require, shared library, ...) due to I am on an embedded system. I found an answer but it is for lua 5.0 (http://www.lua.org/pil/26.2.html) and so it is not compatible. If someone have a idea ? 回答1: Edit linit.c and add your library entry point to it. Then add the modified linit.c to your project. The linker will use your copy instead of the one in the Lua library. This assumes your app calls

Alert messages for lua functions

℡╲_俬逩灬. 提交于 2019-12-13 22:13:55
问题 I have the following code: lua_getglobal(L, "lgd"); lua_getfield(L, -1, "value_pos_x"); cr->value_pos_x = lua_tointeger(L, -1); if (!lua_isinteger(L, -1)) printf("value_pos_x allows only numbers;"); lua_getfield(L, -2, "value_pos_y"); cr->value_pos_y = lua_tointeger(L, -1); if (!lua_isinteger(L, -1)) printf("value_pos_y allows only numbers;"); lua_getfield(L, -3, "time"); cr->time = lua_tointeger(L, -1); if (!lua_isinteger(L, -1)) printf("time allows only numbers;"); The code works perfectly.

lua c read nested tables

北慕城南 提交于 2019-12-12 09:39:08
问题 below is the lua table i need to read from C: listen = { { port = 1234, address = "192.168.1.1", userdata = "liunx" }, { port = 1235, address = "192.168.1.2", userdata = "liunx1" }, { port = 1236, address = "192.168.1.3", userdata = "liunx2" } } below is the c code: #include <lua.h> /* Always include this when calling Lua */ #include <lauxlib.h> /* Always include this when calling Lua */ #include <lualib.h> /* Prototype for luaL_openlibs(), */ /* always include this when calling Lua */

./lua/addtest.lua:9: attempt to index local 'testobj' (a userdata value)]]

帅比萌擦擦* 提交于 2019-12-11 03:19:34
问题 test.exe call addTest.lua and set the lua_testobj to the table, and addTest.lua call testobj.dll, but testobj.dll can not get the "lua_testobj" error msg is addTest.lua:9 attempt to index local 'testobj' (a userdata value) test.exe L = luaL_newstate(); // link lua lib luaL_openlibs(L); // addLuaCPath( L, "./clib/?.dll" ); // lua_pushlightuserdata(L, (void*)g_TestObj.get()); // g_TestObj is a global vars lua_setfield(L, LUA_REGISTRYINDEX, "lua_testobj"); // int err = 0; err = luaL_loadfile( L,

Lua userdata array access and methods

梦想的初衷 提交于 2019-12-10 15:36:47
问题 I am writing in C a userdata type for use in Lua. It has some array-type properties and various methods aswell. Right now if u is of this type, I use u:set(k,v) resp. u:get(k) to access data and e.g. u:sort() as method. For this I set __index to a table containing these methods. Now if I want to access the data using u[k] = v or u[k] , I need to set __newindex and __index to set resp get . But then the other methods are no longer accessible... What's the best way to deal with this in C? I am