lua_Integer 和 lua_createtable(table 大小限制)

lua_Integer and lua_createtable (table size limit)

在Lua5.3tableC中的相关函数API接收和returnlua_Integer.

void lua_rawgeti (lua_State *L, int idx, lua_Integer n);
void lua_rawseti (lua_State *L, int idx, lua_Integer n);
lua_Integer luaL_len (lua_State *L, int index);

但是,lua_createtable 仍然收到 int

void lua_createtable (lua_State *L, int narr, int nrec);

在下面的示例函数中,源 table 的长度用于创建大小相同的副本。

static int copy_sequence(lua_State *L) {
   lua_Integer len, i;
   luaL_checktype(L, 1, LUA_TTABLE);
   len = luaL_len(L, 1);
   lua_createtable(L, (int)len, 0); /* conversion warning */
   for (i = 1; i <= len; i++) {
      lua_rawgeti(L, 1, i);
      lua_rawseti(L, -2, i);
   }
   return 1;
}

但是,需要强制转换才能消除警告:

warning: conversion to ‘int’ from ‘lua_Integer’ may alter its value [-Wconversion]

在 Lua 邮件列表中搜索,我发现以下 thread 是关于 Lua 5.2(我假设也适用于早期版本):

Quote: Roberto Ierusalimschy (7 Aug 2012)

The size of tables is already limited to 2147483647 elements. Lua internally uses 'int' to index all its arrays (except for strings/byte arrays). It is a pain to work with unsigned values (such as size_t) everywhere; ptrdiff_t has no garanties at all.

Lua 5.3 中使用 long long 作为 lua_Integer 的情况是否仍然如此?上例中使用的 lua_Integerint 的转换在 Lua 5.3 中安全吗?

table 的大小(元素数量)仍然限制在 'int'。那 不会阻止 table 具有任意 lua_Integer 键(只要 因为 table 不是正确的顺序)。