lua-5.1

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

Convert signed IEEE 754 float to hexadecimal representation

眉间皱痕 提交于 2019-12-19 18:19:07
问题 I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... 回答1: The following functions uses some code from François Perrad's

Convert signed IEEE 754 float to hexadecimal representation

▼魔方 西西 提交于 2019-12-19 18:18:08
问题 I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... 回答1: The following functions uses some code from François Perrad's

Why is 'name' nil for debug.getinfo(1)

↘锁芯ラ 提交于 2019-12-10 16:10:53
问题 I'm trying to put together a lua testing framework that lets you know the function that had the problem, but when I switched from loadstring to _G , (I switched so my test harness could see the results of the function call) my functions started using 'nil' for the function name Why can _G not detect the name of the current function in the following code? Also, how can I get the return results from loadstring (ie the 'false' from the blah call) or set the function name when using _G (ie. Tell

How to create table in table in Lua 5.1 using C-API?

北城以北 提交于 2019-12-06 12:00:37
问题 I need to create construction like this in Lua 5.1 C-API, not in Lua 5.2 and above a = {["b"] = {["c"] = {["d"] = {["e"] = "GOOD"}}}} print(a.b.c.d.e); Expected Result: GOOD Thanks for answers! 回答1: The Lua C API is stack based. That means that the behavior of most of the C API functions depends not only on the arguments given, but also on the contents of the Lua stack (which is part of the lua_State* variable commonly called L ). What a particular API function expects in terms of stack

Lua 5.1 workaround for __gc metamethod for tables

♀尐吖头ヾ 提交于 2019-12-06 00:26:59
I'm currently facing the problem that you can't use the __gc method for tables in Lua 5.1, as they are implemented in Lua 5.2. However, I want to release allocated native resources once the lua table gets collected. Is it possible to make a workaround which gives me the functionality of __gc metamethod in Lua 5.2 for Lua 5.1? In lua 5.1 the only lua values that work with __gc metamethod is userdata . Naturally any hack or workaround will have to involve userdata in someway. Normally there is no way to just create newuserdata from the lua side but there is one " hidden " undocumented function

How to create table in table in Lua 5.1 using C-API?

别说谁变了你拦得住时间么 提交于 2019-12-04 17:08:39
I need to create construction like this in Lua 5.1 C-API, not in Lua 5.2 and above a = {["b"] = {["c"] = {["d"] = {["e"] = "GOOD"}}}} print(a.b.c.d.e); Expected Result: GOOD Thanks for answers! The Lua C API is stack based. That means that the behavior of most of the C API functions depends not only on the arguments given, but also on the contents of the Lua stack (which is part of the lua_State* variable commonly called L ). What a particular API function expects in terms of stack contents and how it affects the elements of the stack, you'll have to look up in the Lua manual . Let's start

Convert signed IEEE 754 float to hexadecimal representation

时间秒杀一切 提交于 2019-12-01 17:51:58
I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... The following functions uses some code from François Perrad's lua-MessagePack . A big thank you goes to him. function float2hex (n) if n == 0.0 then return 0.0 end local