lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Apr 25, 2012 at 8:57 AM, Rena <hyperhacker@gmail.com> wrote:
> On Tue, Apr 24, 2012 at 22:50, Rebel Neurofog <rebelneurofog@gmail.com> wrote:
>> On Tue, Apr 24, 2012 at 11:00 AM, Rena <hyperhacker@gmail.com> wrote:
>>> Right now if you want to retrieve a field using the C API, it takes
>>> three lines per field:
>>> lua_getfield(L, index, "field name");
>>> lua_Integer val = lua_tointeger(L, -1);
>>> lua_pop(L, 1);
>>
>> static inline lua_Integer get_integer (lua_State *L, int idx, const char *field)
>> {
>>    lua_getfield(L, idx, field);
>>    lua_Integer val = lua_tointeger(L, -1);
>>    lua_pop(L, 1);
>>    return val;
>> }
>>
>> In modern compilers inlining avoids unnecessary data copying so this
>> is at least optimal.
>> Although inlining isn't ASNI C.
>>
>
> That's what I've done in my app. I was just proposing that such a
> function should be part of the API.
>

Well, once again, inlining isn't ANSI C.
And for now there's no inlining in Lua ()
So adding lua_popinteger () is either suboptimal or takes duplication of
big piece of code:

LUA_API lua_Integer lua_popinteger (lua_State *L, int idx) {
  TValue n;
  const TValue *o = index2adr(L, idx);
  if (tonumber(o, &n)) {
    lua_Integer res;
    lua_Number num = nvalue(o);
    lua_number2integer(res, num);
+  lua_pop(L, 1);
    return res;
  }
  else
+ {
+  lua_pop(L, 1);
    return 0;
+ }
}