Переглянути джерело

Parameters for 'lua_createtable' back to int

Tables don't accept sizes larger than int.
Roberto Ierusalimschy 6 місяців тому
батько
коміт
7d7ae8781e
7 змінених файлів з 18 додано та 16 видалено
  1. 2 2
      lapi.c
  2. 4 4
      ltablib.c
  3. 3 3
      ltests.c
  4. 1 1
      lua.c
  5. 1 1
      lua.h
  6. 3 3
      manual/manual.of
  7. 4 2
      testes/sort.lua

+ 2 - 2
lapi.c

@@ -782,14 +782,14 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
 }
 }
 
 
 
 
-LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) {
+LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
   Table *t;
   Table *t;
   lua_lock(L);
   lua_lock(L);
   t = luaH_new(L);
   t = luaH_new(L);
   sethvalue2s(L, L->top.p, t);
   sethvalue2s(L, L->top.p, t);
   api_incr_top(L);
   api_incr_top(L);
   if (narray > 0 || nrec > 0)
   if (narray > 0 || nrec > 0)
-    luaH_resize(L, t, narray, nrec);
+    luaH_resize(L, t, cast_uint(narray), cast_uint(nrec));
   luaC_checkGC(L);
   luaC_checkGC(L);
   lua_unlock(L);
   lua_unlock(L);
 }
 }

+ 4 - 4
ltablib.c

@@ -62,9 +62,9 @@ static void checktab (lua_State *L, int arg, int what) {
 static int tcreate (lua_State *L) {
 static int tcreate (lua_State *L) {
   lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1);
   lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1);
   lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0);
   lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0);
-  luaL_argcheck(L, sizeseq <= UINT_MAX, 1, "out of range");
-  luaL_argcheck(L, sizerest <= UINT_MAX, 2, "out of range");
-  lua_createtable(L, (unsigned)sizeseq, (unsigned)sizerest);
+  luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range");
+  luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range");
+  lua_createtable(L, cast_int(sizeseq), cast_int(sizerest));
   return 1;
   return 1;
 }
 }
 
 
@@ -192,7 +192,7 @@ static int tconcat (lua_State *L) {
 static int tpack (lua_State *L) {
 static int tpack (lua_State *L) {
   int i;
   int i;
   int n = lua_gettop(L);  /* number of elements to pack */
   int n = lua_gettop(L);  /* number of elements to pack */
-  lua_createtable(L, cast_uint(n), 1);  /* create result table */
+  lua_createtable(L, n, 1);  /* create result table */
   lua_insert(L, 1);  /* put it at index 1 */
   lua_insert(L, 1);  /* put it at index 1 */
   for (i = n; i >= 1; i--)  /* assign elements */
   for (i = n; i >= 1; i--)  /* assign elements */
     lua_seti(L, 1, i);
     lua_seti(L, 1, i);

+ 3 - 3
ltests.c

@@ -809,7 +809,7 @@ static int listk (lua_State *L) {
   luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
   luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
                  1, "Lua function expected");
                  1, "Lua function expected");
   p = getproto(obj_at(L, 1));
   p = getproto(obj_at(L, 1));
-  lua_createtable(L, cast_uint(p->sizek), 0);
+  lua_createtable(L, p->sizek, 0);
   for (i=0; i<p->sizek; i++) {
   for (i=0; i<p->sizek; i++) {
     pushobject(L, p->k+i);
     pushobject(L, p->k+i);
     lua_rawseti(L, -2, i+1);
     lua_rawseti(L, -2, i+1);
@@ -825,7 +825,7 @@ static int listabslineinfo (lua_State *L) {
                  1, "Lua function expected");
                  1, "Lua function expected");
   p = getproto(obj_at(L, 1));
   p = getproto(obj_at(L, 1));
   luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
   luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
-  lua_createtable(L, 2u * cast_uint(p->sizeabslineinfo), 0);
+  lua_createtable(L, 2 * p->sizeabslineinfo, 0);
   for (i=0; i < p->sizeabslineinfo; i++) {
   for (i=0; i < p->sizeabslineinfo; i++) {
     lua_pushinteger(L, p->abslineinfo[i].pc);
     lua_pushinteger(L, p->abslineinfo[i].pc);
     lua_rawseti(L, -2, 2 * i + 1);
     lua_rawseti(L, -2, 2 * i + 1);
@@ -867,7 +867,7 @@ void lua_printstack (lua_State *L) {
 
 
 
 
 static int get_limits (lua_State *L) {
 static int get_limits (lua_State *L) {
-  lua_createtable(L, 0, 6);
+  lua_createtable(L, 0, 5);
   setnameval(L, "IS32INT", LUAI_IS32INT);
   setnameval(L, "IS32INT", LUAI_IS32INT);
   setnameval(L, "MAXARG_Ax", MAXARG_Ax);
   setnameval(L, "MAXARG_Ax", MAXARG_Ax);
   setnameval(L, "MAXARG_Bx", MAXARG_Bx);
   setnameval(L, "MAXARG_Bx", MAXARG_Bx);

+ 1 - 1
lua.c

@@ -185,7 +185,7 @@ static void print_version (void) {
 static void createargtable (lua_State *L, char **argv, int argc, int script) {
 static void createargtable (lua_State *L, char **argv, int argc, int script) {
   int i, narg;
   int i, narg;
   narg = argc - (script + 1);  /* number of positive indices */
   narg = argc - (script + 1);  /* number of positive indices */
-  lua_createtable(L, cast_uint(narg), cast_uint(script + 1));
+  lua_createtable(L, narg, script + 1);
   for (i = 0; i < argc; i++) {
   for (i = 0; i < argc; i++) {
     lua_pushstring(L, argv[i]);
     lua_pushstring(L, argv[i]);
     lua_rawseti(L, -2, i - script);
     lua_rawseti(L, -2, i - script);

+ 1 - 1
lua.h

@@ -267,7 +267,7 @@ LUA_API int (lua_rawget) (lua_State *L, int idx);
 LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
 LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
 LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
 LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
 
 
-LUA_API void  (lua_createtable) (lua_State *L, unsigned narr, unsigned nrec);
+LUA_API void  (lua_createtable) (lua_State *L, int narr, int nrec);
 LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
 LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
 LUA_API int   (lua_getmetatable) (lua_State *L, int objindex);
 LUA_API int   (lua_getmetatable) (lua_State *L, int objindex);
 LUA_API int  (lua_getiuservalue) (lua_State *L, int idx, int n);
 LUA_API int  (lua_getiuservalue) (lua_State *L, int idx, int n);

+ 3 - 3
manual/manual.of

@@ -3238,7 +3238,7 @@ Values at other positions are not affected.
 
 
 }
 }
 
 
-@APIEntry{void lua_createtable (lua_State *L, unsigned nseq, unsigned nrec);|
+@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);|
 @apii{0,1,m}
 @apii{0,1,m}
 
 
 Creates a new empty table and pushes it onto the stack.
 Creates a new empty table and pushes it onto the stack.
@@ -3249,7 +3249,7 @@ the table will have.
 Lua may use these hints to preallocate memory for the new table.
 Lua may use these hints to preallocate memory for the new table.
 This preallocation may help performance when you know in advance
 This preallocation may help performance when you know in advance
 how many elements the table will have.
 how many elements the table will have.
-Otherwise you can use the function @Lid{lua_newtable}.
+Otherwise you should use the function @Lid{lua_newtable}.
 
 
 }
 }
 
 
@@ -3351,7 +3351,7 @@ Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}).
 
 
 @item{@defid{LUA_GCPARAM} (int param, int val)|
 @item{@defid{LUA_GCPARAM} (int param, int val)|
 Changes and/or returns the value of a parameter of the collector.
 Changes and/or returns the value of a parameter of the collector.
-If @id{val} is negative, the call only returns the current value.
+If @id{val} is -1, the call only returns the current value.
 The argument @id{param} must have one of the following values:
 The argument @id{param} must have one of the following values:
 @description{
 @description{
 @item{@defid{LUA_GCPMINORMUL}| The minor multiplier. }
 @item{@defid{LUA_GCPMINORMUL}| The minor multiplier. }

+ 4 - 2
testes/sort.lua

@@ -35,8 +35,10 @@ do print "testing 'table.create'"
   assert(memdiff > 1024 * 12)
   assert(memdiff > 1024 * 12)
   assert(not T or select(2, T.querytab(t)) == 1024)
   assert(not T or select(2, T.querytab(t)) == 1024)
 
 
-  checkerror("table overflow", table.create, (1<<31) + 1)
-  checkerror("table overflow", table.create, 0, (1<<31) + 1)
+  local maxint1 = 1 << (string.packsize("i") * 8 - 1)
+  checkerror("out of range", table.create, maxint1)
+  checkerror("out of range", table.create, 0, maxint1)
+  checkerror("table overflow", table.create, 0, maxint1 - 1)
 end
 end