summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c6
-rw-r--r--lauxlib.c10
-rw-r--r--lauxlib.h26
-rw-r--r--lbuffer.c23
-rw-r--r--lbuiltin.c5
-rw-r--r--lcode.c8
-rw-r--r--ldblib.c4
-rw-r--r--ldo.c9
-rw-r--r--lfunc.c4
-rw-r--r--lgc.c4
-rw-r--r--liolib.c28
-rw-r--r--llex.c13
-rw-r--r--llex.h3
-rw-r--r--llimits.h8
-rw-r--r--lmem.c15
-rw-r--r--lmem.h28
-rw-r--r--lobject.c6
-rw-r--r--lobject.h7
-rw-r--r--lparser.c30
-rw-r--r--lref.c5
-rw-r--r--lstate.h10
-rw-r--r--lstring.c14
-rw-r--r--lstring.h4
-rw-r--r--lstrlib.c67
-rw-r--r--ltable.c23
-rw-r--r--ltm.c7
-rw-r--r--lua.h15
-rw-r--r--lvm.c34
-rw-r--r--lzio.c10
-rw-r--r--lzio.h10
-rw-r--r--manual.tex10
31 files changed, 247 insertions, 199 deletions
diff --git a/lapi.c b/lapi.c
index 95e4faba..dfe05ea5 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.79 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: lapi.c,v 1.80 2000/05/08 20:49:05 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -250,7 +250,7 @@ const char *lua_getstring (lua_State *L, lua_Object obj) {
250 else return (svalue(obj)); 250 else return (svalue(obj));
251} 251}
252 252
253long lua_strlen (lua_State *L, lua_Object obj) { 253size_t lua_strlen (lua_State *L, lua_Object obj) {
254 if (obj == LUA_NOOBJECT || tostring(L, obj)) 254 if (obj == LUA_NOOBJECT || tostring(L, obj))
255 return 0L; 255 return 0L;
256 else return (tsvalue(obj)->u.s.len); 256 else return (tsvalue(obj)->u.s.len);
@@ -281,7 +281,7 @@ void lua_pushnumber (lua_State *L, double n) {
281 incr_top; 281 incr_top;
282} 282}
283 283
284void lua_pushlstring (lua_State *L, const char *s, long len) { 284void lua_pushlstring (lua_State *L, const char *s, size_t len) {
285 tsvalue(L->top) = luaS_newlstr(L, s, len); 285 tsvalue(L->top) = luaS_newlstr(L, s, len);
286 ttype(L->top) = TAG_STRING; 286 ttype(L->top) = TAG_STRING;
287 incr_top; 287 incr_top;
diff --git a/lauxlib.c b/lauxlib.c
index f0620236..b9976053 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.26 2000/02/08 16:34:31 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.27 2000/03/30 17:19:48 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -51,18 +51,20 @@ static void type_error (lua_State *L, int narg, const char *type_name,
51} 51}
52 52
53 53
54static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) { 54static const char *checkstr (lua_State *L, lua_Object o, int narg,
55 size_t *len) {
55 const char *s = lua_getstring(L, o); 56 const char *s = lua_getstring(L, o);
56 if (!s) type_error(L, narg, "string", o); 57 if (!s) type_error(L, narg, "string", o);
57 if (len) *len = lua_strlen(L, o); 58 if (len) *len = lua_strlen(L, o);
58 return s; 59 return s;
59} 60}
60 61
61const char *luaL_check_lstr (lua_State *L, int narg, long *len) { 62const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
62 return checkstr(L, lua_getparam(L, narg), narg, len); 63 return checkstr(L, lua_getparam(L, narg), narg, len);
63} 64}
64 65
65const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) { 66const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
67 size_t *len) {
66 lua_Object o = lua_getparam(L, narg); 68 lua_Object o = lua_getparam(L, narg);
67 if (o == LUA_NOOBJECT) { 69 if (o == LUA_NOOBJECT) {
68 if (len) *len = def ? strlen(def) : 0; 70 if (len) *len = def ? strlen(def) : 0;
diff --git a/lauxlib.h b/lauxlib.h
index 0e88efc2..70b5d26a 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.16 1999/12/03 11:26:23 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.17 1999/12/29 16:24:03 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -9,6 +9,8 @@
9#define lauxlib_h 9#define lauxlib_h
10 10
11 11
12#include <stddef.h>
13
12#include "lua.h" 14#include "lua.h"
13 15
14 16
@@ -20,26 +22,28 @@ struct luaL_reg {
20 22
21void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n); 23void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
22void luaL_argerror (lua_State *L, int numarg, const char *extramsg); 24void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
23const char *luaL_check_lstr (lua_State *L, int numArg, long *len); 25const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
24const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def, long *len); 26const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
27 size_t *len);
25double luaL_check_number (lua_State *L, int numArg); 28double luaL_check_number (lua_State *L, int numArg);
26double luaL_opt_number (lua_State *L, int numArg, double def); 29double luaL_opt_number (lua_State *L, int numArg, double def);
27lua_Object luaL_functionarg (lua_State *L, int arg); 30lua_Object luaL_functionarg (lua_State *L, int arg);
28lua_Object luaL_tablearg (lua_State *L, int arg); 31lua_Object luaL_tablearg (lua_State *L, int arg);
29lua_Object luaL_nonnullarg (lua_State *L, int numArg); 32lua_Object luaL_nonnullarg (lua_State *L, int numArg);
30void luaL_verror (lua_State *L, const char *fmt, ...); 33void luaL_verror (lua_State *L, const char *fmt, ...);
31char *luaL_openspace (lua_State *L, int size);
32void luaL_resetbuffer (lua_State *L);
33void luaL_addchar (lua_State *L, int c);
34int luaL_getsize (lua_State *L);
35void luaL_addsize (lua_State *L, int n);
36int luaL_newbuffer (lua_State *L, int size);
37void luaL_oldbuffer (lua_State *L, int old);
38char *luaL_buffer (lua_State *L);
39int luaL_findstring (const char *name, const char *const list[]); 34int luaL_findstring (const char *name, const char *const list[]);
40void luaL_chunkid (char *out, const char *source, int len); 35void luaL_chunkid (char *out, const char *source, int len);
41void luaL_filesource (char *out, const char *filename, int len); 36void luaL_filesource (char *out, const char *filename, int len);
42 37
38char *luaL_openspace (lua_State *L, size_t size);
39void luaL_resetbuffer (lua_State *L);
40void luaL_addchar (lua_State *L, int c);
41size_t luaL_getsize (lua_State *L);
42void luaL_addsize (lua_State *L, size_t n);
43size_t luaL_newbuffer (lua_State *L, size_t size);
44void luaL_oldbuffer (lua_State *L, size_t old);
45char *luaL_buffer (lua_State *L);
46
43 47
44#ifdef LUA_REENTRANT 48#ifdef LUA_REENTRANT
45 49
diff --git a/lbuffer.c b/lbuffer.c
index f584936d..043ed5a3 100644
--- a/lbuffer.c
+++ b/lbuffer.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lbuffer.c,v 1.12 2000/03/03 14:58:26 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -25,16 +25,17 @@
25#define EXTRABUFF 32 25#define EXTRABUFF 32
26 26
27 27
28#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \ 28#define openspace(L, size) if ((size_t)(size) > L->Mbuffsize-L->Mbuffnext) \
29 Openspace(L, size) 29 Openspace(L, size)
30 30
31static void Openspace (lua_State *L, int size) { 31static void Openspace (lua_State *L, size_t size) {
32 L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2; 32 lint32 newsize = ((lint32)L->Mbuffnext+size+EXTRABUFF)*2;
33 luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char); 33 luaM_reallocvector(L, L->Mbuffer, newsize, char);
34 L->Mbuffsize = newsize;
34} 35}
35 36
36 37
37char *luaL_openspace (lua_State *L, int size) { 38char *luaL_openspace (lua_State *L, size_t size) {
38 openspace(L, size); 39 openspace(L, size);
39 return L->Mbuffer+L->Mbuffnext; 40 return L->Mbuffer+L->Mbuffnext;
40} 41}
@@ -51,23 +52,23 @@ void luaL_resetbuffer (lua_State *L) {
51} 52}
52 53
53 54
54void luaL_addsize (lua_State *L, int n) { 55void luaL_addsize (lua_State *L, size_t n) {
55 L->Mbuffnext += n; 56 L->Mbuffnext += n;
56} 57}
57 58
58int luaL_getsize (lua_State *L) { 59size_t luaL_getsize (lua_State *L) {
59 return L->Mbuffnext-L->Mbuffbase; 60 return L->Mbuffnext-L->Mbuffbase;
60} 61}
61 62
62int luaL_newbuffer (lua_State *L, int size) { 63size_t luaL_newbuffer (lua_State *L, size_t size) {
63 int old = L->Mbuffbase; 64 size_t old = L->Mbuffbase;
64 openspace(L, size); 65 openspace(L, size);
65 L->Mbuffbase = L->Mbuffnext; 66 L->Mbuffbase = L->Mbuffnext;
66 return old; 67 return old;
67} 68}
68 69
69 70
70void luaL_oldbuffer (lua_State *L, int old) { 71void luaL_oldbuffer (lua_State *L, size_t old) {
71 L->Mbuffnext = L->Mbuffbase; 72 L->Mbuffnext = L->Mbuffbase;
72 L->Mbuffbase = old; 73 L->Mbuffbase = old;
73} 74}
diff --git a/lbuiltin.c b/lbuiltin.c
index 22b23e5a..77a4d388 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.108 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.109 2000/05/09 14:50:16 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -273,7 +273,7 @@ static void passresults (lua_State *L) {
273} 273}
274 274
275void luaB_dostring (lua_State *L) { 275void luaB_dostring (lua_State *L) {
276 long l; 276 size_t l;
277 const char *s = luaL_check_lstr(L, 1, &l); 277 const char *s = luaL_check_lstr(L, 1, &l);
278 if (*s == ID_CHUNK) 278 if (*s == ID_CHUNK)
279 lua_error(L, "`dostring' cannot run pre-compiled code"); 279 lua_error(L, "`dostring' cannot run pre-compiled code");
@@ -633,7 +633,6 @@ static const struct luaL_reg builtin_funcs[] = {
633 633
634void luaB_predefine (lua_State *L) { 634void luaB_predefine (lua_State *L) {
635 /* pre-register mem error messages, to avoid loop when error arises */ 635 /* pre-register mem error messages, to avoid loop when error arises */
636 luaS_newfixed(L, tableEM);
637 luaS_newfixed(L, memEM); 636 luaS_newfixed(L, memEM);
638 luaL_openl(L, builtin_funcs); 637 luaL_openl(L, builtin_funcs);
639#ifdef DEBUG 638#ifdef DEBUG
diff --git a/lcode.c b/lcode.c
index 8ca482ec..8577824b 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 1.30 2000/05/15 19:48:04 roberto Exp roberto $ 2** $Id: lcode.c,v 1.31 2000/05/22 18:44:46 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -107,7 +107,8 @@ static int real_constant (FuncState *fs, Number r) {
107 while (--c >= lim) 107 while (--c >= lim)
108 if (f->knum[c] == r) return c; 108 if (f->knum[c] == r) return c;
109 /* not found; create a new entry */ 109 /* not found; create a new entry */
110 luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U); 110 luaM_growvector(fs->L, f->knum, f->nknum, 1, Number,
111 "constant table overflow", MAXARG_U);
111 c = f->nknum++; 112 c = f->nknum++;
112 f->knum[c] = r; 113 f->knum[c] = r;
113 return c; 114 return c;
@@ -567,7 +568,8 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
567 case iAB: i = CREATE_AB(o, arg1, arg2); break; 568 case iAB: i = CREATE_AB(o, arg1, arg2); break;
568 } 569 }
569 /* actually create the new instruction */ 570 /* actually create the new instruction */
570 luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT); 571 luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction,
572 "code size overflow", MAX_INT);
571 fs->f->code[fs->pc] = i; 573 fs->f->code[fs->pc] = i;
572 return fs->pc++; 574 return fs->pc++;
573} 575}
diff --git a/ldblib.c b/ldblib.c
index c3ca748e..2593ba56 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.14 2000/05/08 13:21:35 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.15 2000/05/12 19:49:18 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -49,7 +49,7 @@ static void getinfo (lua_State *L) {
49 const char *options = luaL_opt_string(L, 2, "flnSu"); 49 const char *options = luaL_opt_string(L, 2, "flnSu");
50 char buff[20]; 50 char buff[20];
51 if (lua_isnumber(L, func)) { 51 if (lua_isnumber(L, func)) {
52 if (!lua_getstack(L, lua_getnumber(L, func), &ar)) { 52 if (!lua_getstack(L, (int)lua_getnumber(L, func), &ar)) {
53 lua_pushnil(L); /* level out of range */ 53 lua_pushnil(L); /* level out of range */
54 return; 54 return;
55 } 55 }
diff --git a/ldo.c b/ldo.c
index c65ac8d7..60f0dc65 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.74 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: ldo.c,v 1.75 2000/05/09 14:50:16 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -311,7 +311,9 @@ static int do_main (lua_State *L, ZIO *z, int bin) {
311 int status; 311 int status;
312 int debug = L->debug; /* save debug status */ 312 int debug = L->debug; /* save debug status */
313 do { 313 do {
314 long old_blocks = (luaC_checkGC(L), L->nblocks); 314 unsigned long old_blocks;
315 luaC_checkGC(L);
316 old_blocks = L->nblocks;
315 status = protectedparser(L, z, bin); 317 status = protectedparser(L, z, bin);
316 if (status == 1) return 1; /* error */ 318 if (status == 1) return 1; /* error */
317 else if (status == 2) return 0; /* `natural' end */ 319 else if (status == 2) return 0; /* `natural' end */
@@ -358,7 +360,8 @@ int lua_dostring (lua_State *L, const char *str) {
358} 360}
359 361
360 362
361int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) { 363int lua_dobuffer (lua_State *L, const char *buff, size_t size,
364 const char *name) {
362 ZIO z; 365 ZIO z;
363 if (!name) name = "?"; 366 if (!name) name = "?";
364 luaZ_mopen(&z, buff, size, name); 367 luaZ_mopen(&z, buff, size, name);
diff --git a/lfunc.c b/lfunc.c
index ffb456d5..27143635 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.20 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.21 2000/03/29 20:19:20 roberto Exp roberto $
3** Auxiliary functions to manipulate prototypes and closures 3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -20,7 +20,7 @@
20 20
21Closure *luaF_newclosure (lua_State *L, int nelems) { 21Closure *luaF_newclosure (lua_State *L, int nelems) {
22 Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) + 22 Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
23 sizeof(TObject)*(nelems-1)); 23 (lint32)sizeof(TObject)*(nelems-1));
24 c->next = L->rootcl; 24 c->next = L->rootcl;
25 L->rootcl = c; 25 L->rootcl = c;
26 c->marked = 0; 26 c->marked = 0;
diff --git a/lgc.c b/lgc.c
index f3ad12e7..cc984992 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.49 2000/05/10 16:33:20 roberto Exp roberto $ 2** $Id: lgc.c,v 1.50 2000/05/11 18:57:19 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -193,7 +193,7 @@ static void collectstringtab (lua_State *L, int limit, stringtable *tb) {
193 } 193 }
194 } 194 }
195 } 195 }
196 if (tb->nuse < (tb->size/4) && tb->size > 10) 196 if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
197 luaS_resize(L, tb, tb->size/2); /* table is too big */ 197 luaS_resize(L, tb, tb->size/2); /* table is too big */
198} 198}
199 199
diff --git a/liolib.c b/liolib.c
index f8e6a9b5..dbf853ad 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.62 2000/04/24 21:05:11 roberto Exp roberto $ 2** $Id: liolib.c,v 1.63 2000/05/09 14:50:16 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -322,19 +322,23 @@ static void read_word (lua_State *L, FILE *f) {
322static int read_line (lua_State *L, FILE *f) { 322static int read_line (lua_State *L, FILE *f) {
323 int n; 323 int n;
324 char *b; 324 char *b;
325 do { 325 for (;;) {
326 b = luaL_openspace(L, HUNK_LINE); 326 b = luaL_openspace(L, HUNK_LINE);
327 if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */ 327 if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
328 n = strlen(b); 328 n = strlen(b);
329 luaL_addsize(L, n); 329 if (b[n-1] != '\n')
330 } while (b[n-1] != '\n'); 330 luaL_addsize(L, n);
331 luaL_addsize(L, -1); /* remove '\n' */ 331 else {
332 luaL_addsize(L, n-1); /* do not add the `\n' */
333 break;
334 }
335 }
332 return 1; 336 return 1;
333} 337}
334 338
335 339
336static void read_file (lua_State *L, FILE *f) { 340static void read_file (lua_State *L, FILE *f) {
337 int n; 341 size_t n;
338 do { 342 do {
339 char *b = luaL_openspace(L, HUNK_FILE); 343 char *b = luaL_openspace(L, HUNK_FILE);
340 n = fread(b, sizeof(char), HUNK_FILE, f); 344 n = fread(b, sizeof(char), HUNK_FILE, f);
@@ -343,9 +347,9 @@ static void read_file (lua_State *L, FILE *f) {
343} 347}
344 348
345 349
346static int read_chars (lua_State *L, FILE *f, int n) { 350static int read_chars (lua_State *L, FILE *f, size_t n) {
347 char *b = luaL_openspace(L, n); 351 char *b = luaL_openspace(L, n);
348 int n1 = fread(b, sizeof(char), n, f); 352 size_t n1 = fread(b, sizeof(char), n, f);
349 luaL_addsize(L, n1); 353 luaL_addsize(L, n1);
350 return (n == n1); 354 return (n == n1);
351} 355}
@@ -357,11 +361,11 @@ static void io_read (lua_State *L) {
357 FILE *f = getfileparam(L, ctrl, &arg, INFILE); 361 FILE *f = getfileparam(L, ctrl, &arg, INFILE);
358 lua_Object op = lua_getparam(L, arg); 362 lua_Object op = lua_getparam(L, arg);
359 do { /* repeat for each part */ 363 do { /* repeat for each part */
360 long l; 364 size_t l;
361 int success; 365 int success;
362 luaL_resetbuffer(L); 366 luaL_resetbuffer(L);
363 if (lua_isnumber(L, op)) 367 if (lua_isnumber(L, op))
364 success = read_chars(L, f, (int)lua_getnumber(L, op)); 368 success = read_chars(L, f, (size_t)lua_getnumber(L, op));
365 else { 369 else {
366 const char *p = luaL_opt_string(L, arg, "*l"); 370 const char *p = luaL_opt_string(L, arg, "*l");
367 if (p[0] != '*') 371 if (p[0] != '*')
@@ -409,9 +413,9 @@ static void io_write (lua_State *L) {
409 status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0; 413 status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
410 } 414 }
411 else { 415 else {
412 long l; 416 size_t l;
413 const char *s = luaL_check_lstr(L, arg, &l); 417 const char *s = luaL_check_lstr(L, arg, &l);
414 status = status && ((long)fwrite(s, sizeof(char), l, f) == l); 418 status = status && (fwrite(s, sizeof(char), l, f) == l);
415 } 419 }
416 arg++; 420 arg++;
417 } 421 }
diff --git a/llex.c b/llex.c
index ae498715..00a248a9 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.57 2000/04/12 18:57:19 roberto Exp roberto $ 2** $Id: llex.c,v 1.58 2000/05/08 19:32:53 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -49,6 +49,16 @@ void luaX_init (lua_State *L) {
49 49
50#define MAXSRC 80 50#define MAXSRC 80
51 51
52
53void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
54 if (val > limit) {
55 char buff[100];
56 sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
57 luaX_error(ls, buff, ls->token);
58 }
59}
60
61
52void luaX_syntaxerror (LexState *ls, const char *s, const char *token) { 62void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
53 char buff[MAXSRC]; 63 char buff[MAXSRC];
54 luaL_chunkid(buff, zname(ls->z), sizeof(buff)); 64 luaL_chunkid(buff, zname(ls->z), sizeof(buff));
@@ -175,6 +185,7 @@ static void inclinenumber (lua_State *L, LexState *LS) {
175 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; 185 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
176 next(LS); /* skip '\n' */ 186 next(LS); /* skip '\n' */
177 ++LS->linenumber; 187 ++LS->linenumber;
188 luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
178 if (LS->current == '$') { /* is a pragma? */ 189 if (LS->current == '$') { /* is a pragma? */
179 char buff[PRAGMASIZE+1]; 190 char buff[PRAGMASIZE+1];
180 int ifnot = 0; 191 int ifnot = 0;
diff --git a/llex.h b/llex.h
index fd1daf00..b0f0fc8c 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.23 2000/04/07 13:11:49 roberto Exp roberto $ 2** $Id: llex.h,v 1.24 2000/04/12 18:57:19 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -63,6 +63,7 @@ typedef struct LexState {
63void luaX_init (lua_State *L); 63void luaX_init (lua_State *L);
64void luaX_setinput (lua_State *L, LexState *LS, ZIO *z); 64void luaX_setinput (lua_State *L, LexState *LS, ZIO *z);
65int luaX_lex (LexState *LS); 65int luaX_lex (LexState *LS);
66void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
66void luaX_syntaxerror (LexState *ls, const char *s, const char *token); 67void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
67void luaX_error (LexState *ls, const char *s, int token); 68void luaX_error (LexState *ls, const char *s, int token);
68void luaX_token2str (int token, char *s); 69void luaX_token2str (int token, char *s);
diff --git a/llimits.h b/llimits.h
index df5d6fff..b5a4bca1 100644
--- a/llimits.h
+++ b/llimits.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llimits.h,v 1.5 2000/04/04 20:48:44 roberto Exp roberto $ 2** $Id: llimits.h,v 1.6 2000/04/26 13:43:25 roberto Exp roberto $
3** Limits, basic types, and some other "instalation-dependent" definitions 3** Limits, basic types, and some other "instalation-dependent" definitions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -9,6 +9,7 @@
9 9
10 10
11#include <limits.h> 11#include <limits.h>
12#include <stddef.h>
12 13
13 14
14/* 15/*
@@ -22,6 +23,11 @@
22typedef LUA_NUM_TYPE Number; 23typedef LUA_NUM_TYPE Number;
23 24
24 25
26typedef unsigned long lint32; /* unsigned int with at least 32 bits */
27
28
29#define MAX_SIZET ((size_t)(~0)-2)
30
25 31
26#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 32#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
27 33
diff --git a/lmem.c b/lmem.c
index d1576782..8f6b110b 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.28 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: lmem.c,v 1.29 2000/03/16 20:35:07 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -88,6 +88,7 @@ static void *debug_realloc (void *block, size_t size) {
88 size_t realsize = HEADER+size+MARKSIZE; 88 size_t realsize = HEADER+size+MARKSIZE;
89 char *newblock = (char *)(malloc)(realsize); /* alloc a new block */ 89 char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
90 int i; 90 int i;
91 if (realsize < size) return NULL; /* overflow! */
91 if (newblock == NULL) return NULL; 92 if (newblock == NULL) return NULL;
92 if (block) { 93 if (block) {
93 size_t oldsize = *blocksize(block); 94 size_t oldsize = *blocksize(block);
@@ -111,10 +112,10 @@ static void *debug_realloc (void *block, size_t size) {
111 112
112 113
113 114
114void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, 115void *luaM_growaux (lua_State *L, void *block, size_t nelems,
115 int inc, int size, const char *errormsg, unsigned long limit) { 116 int inc, size_t size, const char *errormsg, size_t limit) {
116 unsigned long newn = nelems+inc; 117 size_t newn = nelems+inc;
117 if (newn >= limit) lua_error(L, errormsg); 118 if (nelems >= limit-inc) lua_error(L, errormsg);
118 if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */ 119 if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
119 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */ 120 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
120 return block; /* do not need to reallocate */ 121 return block; /* do not need to reallocate */
@@ -126,12 +127,12 @@ void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
126/* 127/*
127** generic allocation routine. 128** generic allocation routine.
128*/ 129*/
129void *luaM_realloc (lua_State *L, void *block, unsigned long size) { 130void *luaM_realloc (lua_State *L, void *block, lint32 size) {
130 if (size == 0) { 131 if (size == 0) {
131 free(block); /* block may be NULL; that is OK for free */ 132 free(block); /* block may be NULL; that is OK for free */
132 return NULL; 133 return NULL;
133 } 134 }
134 else if ((size_t)size != size) 135 else if (size >= MAX_SIZET)
135 lua_error(L, "memory allocation error: block too big"); 136 lua_error(L, "memory allocation error: block too big");
136 block = realloc(block, size); 137 block = realloc(block, size);
137 if (block == NULL) 138 if (block == NULL)
diff --git a/lmem.h b/lmem.h
index e5811354..55e2feab 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.12 2000/01/13 16:30:47 roberto Exp roberto $ 2** $Id: lmem.h,v 1.13 2000/03/16 20:35:07 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,29 +8,29 @@
8#define lmem_h 8#define lmem_h
9 9
10 10
11#include <stdlib.h> 11#include <stddef.h>
12 12
13#include "llimits.h"
13#include "lua.h" 14#include "lua.h"
14 15
15/* memory error messages */ 16/* memory error message */
16#define codeEM "code size overflow"
17#define constantEM "constant table overflow"
18#define refEM "reference table overflow"
19#define tableEM "table overflow"
20#define memEM "not enough memory" 17#define memEM "not enough memory"
21#define arrEM "internal array larger than `int' limit"
22 18
23void *luaM_realloc (lua_State *L, void *oldblock, unsigned long size); 19void *luaM_realloc (lua_State *L, void *oldblock, lint32 size);
24void *luaM_growaux (lua_State *L, void *block, unsigned long nelems, int inc, int size, 20void *luaM_growaux (lua_State *L, void *block, size_t nelems,
25 const char *errormsg, unsigned long limit); 21 int inc, size_t size, const char *errormsg,
22 size_t limit);
26 23
27#define luaM_free(L, b) luaM_realloc(L, (b), 0) 24#define luaM_free(L, b) luaM_realloc(L, (b), 0)
28#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t)) 25#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
29#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t))) 26#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
30#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*sizeof(t))) 27#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t)))
28
31#define luaM_growvector(L, v,nelems,inc,t,e,l) \ 29#define luaM_growvector(L, v,nelems,inc,t,e,l) \
32 ((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l)) 30 ((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
33#define luaM_reallocvector(L, v,n,t) ((v)=(t *)luaM_realloc(L, v,(n)*sizeof(t))) 31
32#define luaM_reallocvector(L, v,n,t) \
33 ((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
34 34
35 35
36#ifdef DEBUG 36#ifdef DEBUG
diff --git a/lobject.c b/lobject.c
index e134c289..627ada16 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.37 2000/04/25 16:55:09 roberto Exp roberto $ 2** $Id: lobject.c,v 1.38 2000/04/26 13:43:10 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -25,8 +25,8 @@ const TObject luaO_nilobject = {TAG_NIL, {NULL}};
25/* 25/*
26** returns smaller power of 2 larger than `n' (minimum is MINPOWER2) 26** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
27*/ 27*/
28unsigned long luaO_power2 (unsigned long n) { 28lint32 luaO_power2 (lint32 n) {
29 unsigned long p = MINPOWER2; 29 lint32 p = MINPOWER2;
30 while (p<=n) p<<=1; 30 while (p<=n) p<<=1;
31 return p; 31 return p;
32} 32}
diff --git a/lobject.h b/lobject.h
index cb9670ed..f667a103 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.63 2000/05/08 19:37:10 roberto Exp roberto $ 2** $Id: lobject.h,v 1.64 2000/05/10 16:33:20 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -92,7 +92,7 @@ typedef struct TString {
92 union { 92 union {
93 struct { /* for strings */ 93 struct { /* for strings */
94 unsigned long hash; 94 unsigned long hash;
95 long len; 95 size_t len;
96 int constindex; /* hint to reuse constants */ 96 int constindex; /* hint to reuse constants */
97 } s; 97 } s;
98 struct { /* for userdata */ 98 struct { /* for userdata */
@@ -170,10 +170,9 @@ extern const TObject luaO_nilobject;
170 170
171#define luaO_typename(o) luaO_typenames[ttype(o)] 171#define luaO_typename(o) luaO_typenames[ttype(o)]
172 172
173unsigned long luaO_power2 (unsigned long n); 173lint32 luaO_power2 (lint32 n);
174 174
175int luaO_equalObj (const TObject *t1, const TObject *t2); 175int luaO_equalObj (const TObject *t1, const TObject *t2);
176int luaO_redimension (lua_State *L, int oldsize);
177int luaO_str2d (const char *s, Number *result); 176int luaO_str2d (const char *s, Number *result);
178 177
179 178
diff --git a/lparser.c b/lparser.c
index b155ed53..72fe2f13 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.87 2000/05/15 19:48:04 roberto Exp roberto $ 2** $Id: lparser.c,v 1.88 2000/05/22 18:44:46 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -79,19 +79,10 @@ static void check (LexState *ls, int c) {
79} 79}
80 80
81 81
82static void checklimit (LexState *ls, int val, int limit, const char *msg) {
83 if (val > limit) {
84 char buff[100];
85 sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
86 luaK_error(ls, buff);
87 }
88}
89
90
91static void setline (LexState *ls) { 82static void setline (LexState *ls) {
92 FuncState *fs = ls->fs; 83 FuncState *fs = ls->fs;
93 if (ls->L->debug && ls->linenumber != fs->lastsetline) { 84 if (ls->L->debug && ls->linenumber != fs->lastsetline) {
94 checklimit(ls, ls->linenumber, MAXARG_U, "lines in a chunk"); 85 luaX_checklimit(ls, ls->linenumber, MAXARG_U, "lines in a chunk");
95 luaK_code1(fs, OP_SETLINE, ls->linenumber); 86 luaK_code1(fs, OP_SETLINE, ls->linenumber);
96 fs->lastsetline = ls->linenumber; 87 fs->lastsetline = ls->linenumber;
97 } 88 }
@@ -142,7 +133,7 @@ static int string_constant (FuncState *fs, TString *s) {
142 int c = s->u.s.constindex; 133 int c = s->u.s.constindex;
143 if (c >= f->nkstr || f->kstr[c] != s) { 134 if (c >= f->nkstr || f->kstr[c] != s) {
144 luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *, 135 luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *,
145 constantEM, MAXARG_U); 136 "constant table overflow", MAXARG_U);
146 c = f->nkstr++; 137 c = f->nkstr++;
147 f->kstr[c] = s; 138 f->kstr[c] = s;
148 s->u.s.constindex = c; /* hint for next time */ 139 s->u.s.constindex = c; /* hint for next time */
@@ -200,7 +191,7 @@ static void luaI_unregisterlocalvar (LexState *ls, int line) {
200 191
201static void store_localvar (LexState *ls, TString *name, int n) { 192static void store_localvar (LexState *ls, TString *name, int n) {
202 FuncState *fs = ls->fs; 193 FuncState *fs = ls->fs;
203 checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables"); 194 luaX_checklimit(ls, fs->nlocalvar+n+1, MAXLOCALS, "local variables");
204 fs->localvar[fs->nlocalvar+n] = name; 195 fs->localvar[fs->nlocalvar+n] = name;
205} 196}
206 197
@@ -266,7 +257,7 @@ static int indexupvalue (LexState *ls, TString *n) {
266 } 257 }
267 /* new one */ 258 /* new one */
268 ++(fs->nupvalues); 259 ++(fs->nupvalues);
269 checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues"); 260 luaX_checklimit(ls, fs->nupvalues, MAXUPVALUES, "upvalues");
270 fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */ 261 fs->upvalues[i] = v; /* i = fs->nupvalues - 1 */
271 return i; 262 return i;
272} 263}
@@ -306,7 +297,7 @@ static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
306static void code_args (LexState *ls, int nparams, int dots) { 297static void code_args (LexState *ls, int nparams, int dots) {
307 FuncState *fs = ls->fs; 298 FuncState *fs = ls->fs;
308 adjustlocalvars(ls, nparams); 299 adjustlocalvars(ls, nparams);
309 checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters"); 300 luaX_checklimit(ls, fs->nlocalvar, MAXPARAMS, "parameters");
310 nparams = fs->nlocalvar; /* `self' could be there already */ 301 nparams = fs->nlocalvar; /* `self' could be there already */
311 fs->f->numparams = nparams; 302 fs->f->numparams = nparams;
312 fs->f->is_vararg = dots; 303 fs->f->is_vararg = dots;
@@ -369,7 +360,7 @@ static void func_onstack (LexState *ls, FuncState *func) {
369 for (i=0; i<func->nupvalues; i++) 360 for (i=0; i<func->nupvalues; i++)
370 luaK_tostack(ls, &func->upvalues[i], 1); 361 luaK_tostack(ls, &func->upvalues[i], 1);
371 luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *, 362 luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
372 constantEM, MAXARG_A); 363 "constant table overflow", MAXARG_A);
373 f->kproto[f->nkproto++] = func->f; 364 f->kproto[f->nkproto++] = func->f;
374 luaK_code2(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues); 365 luaK_code2(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues);
375} 366}
@@ -623,8 +614,8 @@ static int listfields (LexState *ls) {
623 break; 614 break;
624 exp1(ls); 615 exp1(ls);
625 n++; 616 n++;
626 checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH, 617 luaX_checklimit(ls, n/LFIELDS_PER_FLUSH, MAXARG_A,
627 "items in a list initializer"); 618 "`item groups' in a list initializer");
628 if (++mod_n == LFIELDS_PER_FLUSH) { 619 if (++mod_n == LFIELDS_PER_FLUSH) {
629 luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH); 620 luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
630 mod_n = 0; 621 mod_n = 0;
@@ -697,6 +688,7 @@ static void constructor (LexState *ls) {
697 } 688 }
698 check_match(ls, '}', '{', line); 689 check_match(ls, '}', '{', line);
699 /* set initial table size */ 690 /* set initial table size */
691 luaX_checklimit(ls, nelems, MAXARG_U, "elements in a table constructor");
700 SETARG_U(fs->f->code[pc], nelems); 692 SETARG_U(fs->f->code[pc], nelems);
701} 693}
702 694
@@ -846,7 +838,7 @@ static void block (LexState *ls) {
846 838
847static int assignment (LexState *ls, expdesc *v, int nvars) { 839static int assignment (LexState *ls, expdesc *v, int nvars) {
848 int left = 0; 840 int left = 0;
849 checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment"); 841 luaX_checklimit(ls, nvars, MAXVARSLH, "variables in a multiple assignment");
850 if (ls->token == ',') { /* assignment -> ',' NAME assignment */ 842 if (ls->token == ',') { /* assignment -> ',' NAME assignment */
851 expdesc nv; 843 expdesc nv;
852 next(ls); 844 next(ls);
diff --git a/lref.c b/lref.c
index 99b6fda5..fdfdc5df 100644
--- a/lref.c
+++ b/lref.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lref.c,v 1.10 2000/03/27 20:10:21 roberto Exp roberto $ 2** $Id: lref.c,v 1.11 2000/03/29 20:19:20 roberto Exp roberto $
3** reference mechanism 3** reference mechanism
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -25,7 +25,8 @@ int lua_ref (lua_State *L, int lock) {
25 L->refFree = L->refArray[ref].st; 25 L->refFree = L->refArray[ref].st;
26 } 26 }
27 else { /* no more free places */ 27 else { /* no more free places */
28 luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref, refEM, MAX_INT); 28 luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
29 "reference table overflow", MAX_INT);
29 ref = L->refSize++; 30 ref = L->refSize++;
30 } 31 }
31 L->refArray[ref].o = *(L->top-1); 32 L->refArray[ref].o = *(L->top-1);
diff --git a/lstate.h b/lstate.h
index 6c0cee86..36cecb0b 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.32 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: lstate.h,v 1.33 2000/05/10 16:33:20 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -42,7 +42,7 @@ struct C_Lua_Stack {
42 42
43typedef struct stringtable { 43typedef struct stringtable {
44 int size; 44 int size;
45 long nuse; /* number of elements */ 45 lint32 nuse; /* number of elements */
46 TString **hash; 46 TString **hash;
47} stringtable; 47} stringtable;
48 48
@@ -57,9 +57,9 @@ struct lua_State {
57 struct C_Lua_Stack Cstack; /* C2lua struct */ 57 struct C_Lua_Stack Cstack; /* C2lua struct */
58 struct lua_longjmp *errorJmp; /* current error recover point */ 58 struct lua_longjmp *errorJmp; /* current error recover point */
59 char *Mbuffer; /* global buffer */ 59 char *Mbuffer; /* global buffer */
60 int Mbuffbase; /* current first position of Mbuffer */ 60 size_t Mbuffbase; /* current first position of Mbuffer */
61 int Mbuffsize; /* size of Mbuffer */ 61 size_t Mbuffsize; /* size of Mbuffer */
62 int Mbuffnext; /* next position to fill in Mbuffer */ 62 size_t Mbuffnext; /* next position to fill in Mbuffer */
63 struct C_Lua_Stack *Cblocks; 63 struct C_Lua_Stack *Cblocks;
64 int numCblocks; /* number of nested Cblocks */ 64 int numCblocks; /* number of nested Cblocks */
65 /* global state */ 65 /* global state */
diff --git a/lstring.c b/lstring.c
index 507e945a..ebae208a 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.35 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: lstring.c,v 1.36 2000/05/10 16:33:20 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -34,10 +34,10 @@ void luaS_freeall (lua_State *L) {
34} 34}
35 35
36 36
37static unsigned long hash_s (const char *s, long l) { 37static unsigned long hash_s (const char *s, size_t l) {
38 unsigned long h = l; /* seed */ 38 unsigned long h = l; /* seed */
39 long step = (l>>6)+1; /* if string is too long, don't hash all its chars */ 39 size_t step = (l>>6)|1; /* if string is too long, don't hash all its chars */
40 for (; l>0; l-=step) 40 for (; l>=step; l-=step)
41 h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++)); 41 h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
42 return h; 42 return h;
43} 43}
@@ -71,13 +71,13 @@ static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
71 ts->nexthash = tb->hash[h]; /* chain new entry */ 71 ts->nexthash = tb->hash[h]; /* chain new entry */
72 tb->hash[h] = ts; 72 tb->hash[h] = ts;
73 tb->nuse++; 73 tb->nuse++;
74 if (tb->nuse > tb->size && tb->size < MAX_INT/2) /* too crowded? */ 74 if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
75 luaS_resize(L, tb, tb->size*2); 75 luaS_resize(L, tb, tb->size*2);
76} 76}
77 77
78 78
79 79
80TString *luaS_newlstr (lua_State *L, const char *str, long l) { 80TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
81 unsigned long h = hash_s(str, l); 81 unsigned long h = hash_s(str, l);
82 int h1 = h&(L->strt.size-1); 82 int h1 = h&(L->strt.size-1);
83 TString *ts; 83 TString *ts;
@@ -86,7 +86,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, long l) {
86 return ts; 86 return ts;
87 } 87 }
88 /* not found */ 88 /* not found */
89 ts = (TString *)luaM_malloc(L, sizeof(TString)+l*sizeof(char)); 89 ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
90 ts->marked = 0; 90 ts->marked = 0;
91 ts->nexthash = NULL; 91 ts->nexthash = NULL;
92 ts->u.s.len = l; 92 ts->u.s.len = l;
diff --git a/lstring.h b/lstring.h
index 5d6ff49f..2b853a0f 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.19 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: lstring.h,v 1.20 2000/05/10 16:33:20 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,7 +28,7 @@ void luaS_init (lua_State *L);
28void luaS_resize (lua_State *L, stringtable *tb, int newsize); 28void luaS_resize (lua_State *L, stringtable *tb, int newsize);
29TString *luaS_createudata (lua_State *L, void *udata, int tag); 29TString *luaS_createudata (lua_State *L, void *udata, int tag);
30void luaS_freeall (lua_State *L); 30void luaS_freeall (lua_State *L);
31TString *luaS_newlstr (lua_State *L, const char *str, long l); 31TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
32TString *luaS_new (lua_State *L, const char *str); 32TString *luaS_new (lua_State *L, const char *str);
33TString *luaS_newfixed (lua_State *L, const char *str); 33TString *luaS_newfixed (lua_State *L, const char *str);
34 34
diff --git a/lstrlib.c b/lstrlib.c
index 6a38fd01..da75bbd7 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,11 +1,12 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.41 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.42 2000/05/02 18:32:22 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <ctype.h> 8#include <ctype.h>
9#include <stddef.h>
9#include <stdio.h> 10#include <stdio.h>
10#include <stdlib.h> 11#include <stdlib.h>
11#include <string.h> 12#include <string.h>
@@ -18,7 +19,7 @@
18 19
19 20
20 21
21static void addnchar (lua_State *L, const char *s, int n) { 22static void addnchar (lua_State *L, const char *s, size_t n) {
22 char *b = luaL_openspace(L, n); 23 char *b = luaL_openspace(L, n);
23 memcpy(b, s, n); 24 memcpy(b, s, n);
24 luaL_addsize(L, n); 25 luaL_addsize(L, n);
@@ -26,7 +27,7 @@ static void addnchar (lua_State *L, const char *s, int n) {
26 27
27 28
28static void str_len (lua_State *L) { 29static void str_len (lua_State *L) {
29 long l; 30 size_t l;
30 luaL_check_lstr(L, 1, &l); 31 luaL_check_lstr(L, 1, &l);
31 lua_pushnumber(L, l); 32 lua_pushnumber(L, l);
32} 33}
@@ -37,19 +38,19 @@ static void closeandpush (lua_State *L) {
37} 38}
38 39
39 40
40static long posrelat (long pos, long len) { 41static long posrelat (long pos, size_t len) {
41 /* relative string position: negative means back from end */ 42 /* relative string position: negative means back from end */
42 return (pos>=0) ? pos : len+pos+1; 43 return (pos>=0) ? pos : (long)len+pos+1;
43} 44}
44 45
45 46
46static void str_sub (lua_State *L) { 47static void str_sub (lua_State *L) {
47 long l; 48 size_t l;
48 const char *s = luaL_check_lstr(L, 1, &l); 49 const char *s = luaL_check_lstr(L, 1, &l);
49 long start = posrelat(luaL_check_long(L, 2), l); 50 long start = posrelat(luaL_check_long(L, 2), l);
50 long end = posrelat(luaL_opt_long(L, 3, -1), l); 51 long end = posrelat(luaL_opt_long(L, 3, -1), l);
51 if (start < 1) start = 1; 52 if (start < 1) start = 1;
52 if (end > l) end = l; 53 if (end > (long)l) end = l;
53 if (start <= end) 54 if (start <= end)
54 lua_pushlstring(L, s+start-1, end-start+1); 55 lua_pushlstring(L, s+start-1, end-start+1);
55 else lua_pushstring(L, ""); 56 else lua_pushstring(L, "");
@@ -57,8 +58,8 @@ static void str_sub (lua_State *L) {
57 58
58 59
59static void str_lower (lua_State *L) { 60static void str_lower (lua_State *L) {
60 long l; 61 size_t l;
61 int i; 62 size_t i;
62 const char *s = luaL_check_lstr(L, 1, &l); 63 const char *s = luaL_check_lstr(L, 1, &l);
63 luaL_resetbuffer(L); 64 luaL_resetbuffer(L);
64 for (i=0; i<l; i++) 65 for (i=0; i<l; i++)
@@ -68,8 +69,8 @@ static void str_lower (lua_State *L) {
68 69
69 70
70static void str_upper (lua_State *L) { 71static void str_upper (lua_State *L) {
71 long l; 72 size_t l;
72 int i; 73 size_t i;
73 const char *s = luaL_check_lstr(L, 1, &l); 74 const char *s = luaL_check_lstr(L, 1, &l);
74 luaL_resetbuffer(L); 75 luaL_resetbuffer(L);
75 for (i=0; i<l; i++) 76 for (i=0; i<l; i++)
@@ -78,7 +79,7 @@ static void str_upper (lua_State *L) {
78} 79}
79 80
80static void str_rep (lua_State *L) { 81static void str_rep (lua_State *L) {
81 long l; 82 size_t l;
82 const char *s = luaL_check_lstr(L, 1, &l); 83 const char *s = luaL_check_lstr(L, 1, &l);
83 int n = luaL_check_int(L, 2); 84 int n = luaL_check_int(L, 2);
84 luaL_resetbuffer(L); 85 luaL_resetbuffer(L);
@@ -89,10 +90,10 @@ static void str_rep (lua_State *L) {
89 90
90 91
91static void str_byte (lua_State *L) { 92static void str_byte (lua_State *L) {
92 long l; 93 size_t l;
93 const char *s = luaL_check_lstr(L, 1, &l); 94 const char *s = luaL_check_lstr(L, 1, &l);
94 long pos = posrelat(luaL_opt_long(L, 2, 1), l); 95 long pos = posrelat(luaL_opt_long(L, 2, 1), l);
95 luaL_arg_check(L, 0<pos && pos<=l, 2, "out of range"); 96 luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
96 lua_pushnumber(L, (unsigned char)s[pos-1]); 97 lua_pushnumber(L, (unsigned char)s[pos-1]);
97} 98}
98 99
@@ -126,7 +127,7 @@ struct Capture {
126 int level; /* total number of captures (finished or unfinished) */ 127 int level; /* total number of captures (finished or unfinished) */
127 struct { 128 struct {
128 const char *init; 129 const char *init;
129 int len; /* -1 signals unfinished capture */ 130 long len; /* -1 signals unfinished capture */
130 } capture[MAX_CAPTURES]; 131 } capture[MAX_CAPTURES];
131}; 132};
132 133
@@ -238,7 +239,8 @@ int luaI_singlematch (int c, const char *p, const char *ep) {
238} 239}
239 240
240 241
241static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap); 242static const char *match (lua_State *L, const char *s, const char *p,
243 struct Capture *cap);
242 244
243 245
244static const char *matchbalance (lua_State *L, const char *s, const char *p, 246static const char *matchbalance (lua_State *L, const char *s, const char *p,
@@ -261,9 +263,9 @@ static const char *matchbalance (lua_State *L, const char *s, const char *p,
261} 263}
262 264
263 265
264static const char *max_expand (lua_State *L, const char *s, const char *p, const char *ep, 266static const char *max_expand (lua_State *L, const char *s, const char *p,
265 struct Capture *cap) { 267 const char *ep, struct Capture *cap) {
266 int i = 0; /* counts maximum expand for item */ 268 long i = 0; /* counts maximum expand for item */
267 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) 269 while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
268 i++; 270 i++;
269 /* keeps trying to match mith the maximum repetitions */ 271 /* keeps trying to match mith the maximum repetitions */
@@ -276,8 +278,8 @@ static const char *max_expand (lua_State *L, const char *s, const char *p, const
276} 278}
277 279
278 280
279static const char *min_expand (lua_State *L, const char *s, const char *p, const char *ep, 281static const char *min_expand (lua_State *L, const char *s, const char *p,
280 struct Capture *cap) { 282 const char *ep, struct Capture *cap) {
281 for (;;) { 283 for (;;) {
282 const char *res = match(L, s, ep+1, cap); 284 const char *res = match(L, s, ep+1, cap);
283 if (res != NULL) 285 if (res != NULL)
@@ -317,15 +319,16 @@ static const char *end_capture (lua_State *L, const char *s, const char *p,
317static const char *match_capture (lua_State *L, const char *s, int level, 319static const char *match_capture (lua_State *L, const char *s, int level,
318 struct Capture *cap) { 320 struct Capture *cap) {
319 int l = check_capture(L, level, cap); 321 int l = check_capture(L, level, cap);
320 int len = cap->capture[l].len; 322 size_t len = cap->capture[l].len;
321 if (cap->src_end-s >= len && 323 if ((size_t)(cap->src_end-s) >= len &&
322 memcmp(cap->capture[l].init, s, len) == 0) 324 memcmp(cap->capture[l].init, s, len) == 0)
323 return s+len; 325 return s+len;
324 else return NULL; 326 else return NULL;
325} 327}
326 328
327 329
328static const char *match (lua_State *L, const char *s, const char *p, struct Capture *cap) { 330static const char *match (lua_State *L, const char *s, const char *p,
331 struct Capture *cap) {
329 init: /* using goto's to optimize tail recursion */ 332 init: /* using goto's to optimize tail recursion */
330 switch (*p) { 333 switch (*p) {
331 case '(': /* start capture */ 334 case '(': /* start capture */
@@ -397,12 +400,12 @@ static const char *memfind (const char *s1, long l1, const char *s2, long l2) {
397 400
398 401
399static void str_find (lua_State *L) { 402static void str_find (lua_State *L) {
400 long l1, l2; 403 size_t l1, l2;
401 const char *s = luaL_check_lstr(L, 1, &l1); 404 const char *s = luaL_check_lstr(L, 1, &l1);
402 const char *p = luaL_check_lstr(L, 2, &l2); 405 const char *p = luaL_check_lstr(L, 2, &l2);
403 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; 406 long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
404 struct Capture cap; 407 struct Capture cap;
405 luaL_arg_check(L, 0 <= init && init <= l1, 3, "out of range"); 408 luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
406 if (lua_getparam(L, 4) != LUA_NOOBJECT || 409 if (lua_getparam(L, 4) != LUA_NOOBJECT ||
407 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */ 410 strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
408 const char *s2 = memfind(s+init, l1-init, p, l2); 411 const char *s2 = memfind(s+init, l1-init, p, l2);
@@ -434,8 +437,8 @@ static void str_find (lua_State *L) {
434static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) { 437static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
435 if (lua_isstring(L, newp)) { 438 if (lua_isstring(L, newp)) {
436 const char *news = lua_getstring(L, newp); 439 const char *news = lua_getstring(L, newp);
437 int l = lua_strlen(L, newp); 440 size_t l = lua_strlen(L, newp);
438 int i; 441 size_t i;
439 for (i=0; i<l; i++) { 442 for (i=0; i<l; i++) {
440 if (news[i] != ESC) 443 if (news[i] != ESC)
441 luaL_addchar(L, news[i]); 444 luaL_addchar(L, news[i]);
@@ -453,7 +456,7 @@ static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
453 else { /* is a function */ 456 else { /* is a function */
454 lua_Object res; 457 lua_Object res;
455 int status; 458 int status;
456 int oldbuff; 459 size_t oldbuff;
457 lua_beginblock(L); 460 lua_beginblock(L);
458 push_captures(L, cap); 461 push_captures(L, cap);
459 /* function may use buffer, so save it and create a new one */ 462 /* function may use buffer, so save it and create a new one */
@@ -474,7 +477,7 @@ static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) {
474 477
475 478
476static void str_gsub (lua_State *L) { 479static void str_gsub (lua_State *L) {
477 long srcl; 480 size_t srcl;
478 const char *src = luaL_check_lstr(L, 1, &srcl); 481 const char *src = luaL_check_lstr(L, 1, &srcl);
479 const char *p = luaL_check_string(L, 2); 482 const char *p = luaL_check_string(L, 2);
480 lua_Object newp = lua_getparam(L, 3); 483 lua_Object newp = lua_getparam(L, 3);
@@ -510,7 +513,7 @@ static void str_gsub (lua_State *L) {
510 513
511 514
512static void luaI_addquoted (lua_State *L, int arg) { 515static void luaI_addquoted (lua_State *L, int arg) {
513 long l; 516 size_t l;
514 const char *s = luaL_check_lstr(L, arg, &l); 517 const char *s = luaL_check_lstr(L, arg, &l);
515 luaL_addchar(L, '"'); 518 luaL_addchar(L, '"');
516 while (l--) { 519 while (l--) {
@@ -573,7 +576,7 @@ static void str_format (lua_State *L) {
573 luaI_addquoted(L, arg); 576 luaI_addquoted(L, arg);
574 continue; /* skip the "addsize" at the end */ 577 continue; /* skip the "addsize" at the end */
575 case 's': { 578 case 's': {
576 long l; 579 size_t l;
577 const char *s = luaL_check_lstr(L, arg, &l); 580 const char *s = luaL_check_lstr(L, arg, &l);
578 if (cap.capture[1].len == 0 && l >= 100) { 581 if (cap.capture[1].len == 0 && l >= 100) {
579 /* no precision and string is too long to be formatted; 582 /* no precision and string is too long to be formatted;
diff --git a/ltable.c b/ltable.c
index 3af51b62..a7d4f36a 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.41 2000/05/08 19:32:53 roberto Exp roberto $ 2** $Id: ltable.c,v 1.42 2000/05/11 18:57:19 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -122,10 +122,12 @@ int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
122} 122}
123 123
124 124
125static void setnodevector (lua_State *L, Hash *t, int size) { 125static void setnodevector (lua_State *L, Hash *t, lint32 size) {
126 int i; 126 int i;
127 if (size > MAX_INT)
128 lua_error(L, "table overflow");
127 t->node = luaM_newvector(L, size, Node); 129 t->node = luaM_newvector(L, size, Node);
128 for (i=0; i<size; i++) { 130 for (i=0; i<(int)size; i++) {
129 ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL; 131 ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
130 t->node[i].next = NULL; 132 t->node[i].next = NULL;
131 } 133 }
@@ -153,7 +155,7 @@ void luaH_free (lua_State *L, Hash *t) {
153} 155}
154 156
155 157
156static int newsize (const Hash *t) { 158static int numuse (const Hash *t) {
157 Node *v = t->node; 159 Node *v = t->node;
158 int size = t->size; 160 int size = t->size;
159 int realuse = 0; 161 int realuse = 0;
@@ -162,16 +164,24 @@ static int newsize (const Hash *t) {
162 if (ttype(&v[i].val) != TAG_NIL) 164 if (ttype(&v[i].val) != TAG_NIL)
163 realuse++; 165 realuse++;
164 } 166 }
165 return luaO_power2(realuse+realuse/4+1); 167 return realuse;
166} 168}
167 169
168 170
169static void rehash (lua_State *L, Hash *t) { 171static void rehash (lua_State *L, Hash *t) {
170 int oldsize = t->size; 172 int oldsize = t->size;
171 Node *nold = t->node; 173 Node *nold = t->node;
174 int newsize = numuse(t);
172 int i; 175 int i;
176 LUA_ASSERT(L, newsize<=oldsize, "wrong count");
177 if (newsize >= oldsize-oldsize/4) /* using more than 3/4? */
178 setnodevector(L, t, (lint32)oldsize*2);
179 else if (newsize <= oldsize/4 && /* less than 1/4? */
180 oldsize > MINPOWER2)
181 setnodevector(L, t, oldsize/2);
182 else
183 setnodevector(L, t, oldsize);
173 L->nblocks -= gcsize(L, oldsize); 184 L->nblocks -= gcsize(L, oldsize);
174 setnodevector(L, t, newsize(t)); /* create new array of nodes */
175 for (i=0; i<oldsize; i++) { 185 for (i=0; i<oldsize; i++) {
176 Node *old = nold+i; 186 Node *old = nold+i;
177 if (ttype(&old->val) != TAG_NIL) 187 if (ttype(&old->val) != TAG_NIL)
@@ -249,3 +259,4 @@ void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
249const TObject *luaH_getglobal (lua_State *L, const char *name) { 259const TObject *luaH_getglobal (lua_State *L, const char *name) {
250 return luaH_getstr(L->gt, luaS_new(L, name)); 260 return luaH_getstr(L->gt, luaS_new(L, name));
251} 261}
262
diff --git a/ltm.c b/ltm.c
index 29053d94..410d23c0 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $ 2** $Id: ltm.c,v 1.39 2000/03/30 16:41:51 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -67,7 +67,7 @@ static void init_entry (lua_State *L, int tag) {
67void luaT_init (lua_State *L) { 67void luaT_init (lua_State *L) {
68 int t; 68 int t;
69 L->last_tag = NUM_TAGS-1; 69 L->last_tag = NUM_TAGS-1;
70 luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT); 70 luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
71 for (t=0; t<=L->last_tag; t++) 71 for (t=0; t<=L->last_tag; t++)
72 init_entry(L, t); 72 init_entry(L, t);
73} 73}
@@ -75,7 +75,8 @@ void luaT_init (lua_State *L) {
75 75
76int lua_newtag (lua_State *L) { 76int lua_newtag (lua_State *L) {
77 ++L->last_tag; 77 ++L->last_tag;
78 luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, arrEM, MAX_INT); 78 luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
79 "tag table overflow", MAX_INT);
79 init_entry(L, L->last_tag); 80 init_entry(L, L->last_tag);
80 return L->last_tag; 81 return L->last_tag;
81} 82}
diff --git a/lua.h b/lua.h
index 4cf3bf6d..f2c19465 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.51 2000/05/09 14:50:16 roberto Exp roberto $ 2** $Id: lua.h,v 1.52 2000/05/10 16:35:18 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 4** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
5** e-mail: lua@tecgraf.puc-rio.br 5** e-mail: lua@tecgraf.puc-rio.br
@@ -11,6 +11,11 @@
11#ifndef lua_h 11#ifndef lua_h
12#define lua_h 12#define lua_h
13 13
14
15/* definition of `size_t' */
16#include <stddef.h>
17
18
14#define LUA_VERSION "Lua 4.0 (alpha)" 19#define LUA_VERSION "Lua 4.0 (alpha)"
15#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio" 20#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
16#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo" 21#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
@@ -50,8 +55,8 @@ int lua_dofile (lua_State *L, const char *filename);
50 /* Out: returns */ 55 /* Out: returns */
51int lua_dostring (lua_State *L, const char *str); 56int lua_dostring (lua_State *L, const char *str);
52 /* Out: returns */ 57 /* Out: returns */
53int lua_dobuffer (lua_State *L, const char *buff, int size, 58int lua_dobuffer (lua_State *L, const char *buff, size_t size,
54 const char *name); /* Out: returns */ 59 const char *name); /* Out: returns */
55int lua_callfunction (lua_State *L, lua_Object f); 60int lua_callfunction (lua_State *L, lua_Object f);
56 /* In: parameters; Out: returns */ 61 /* In: parameters; Out: returns */
57 62
@@ -79,14 +84,14 @@ int lua_equal (lua_State *L, lua_Object o1, lua_Object o2);
79 84
80double lua_getnumber (lua_State *L, lua_Object obj); 85double lua_getnumber (lua_State *L, lua_Object obj);
81const char *lua_getstring (lua_State *L, lua_Object obj); 86const char *lua_getstring (lua_State *L, lua_Object obj);
82long lua_strlen (lua_State *L, lua_Object obj); 87size_t lua_strlen (lua_State *L, lua_Object obj);
83lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj); 88lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj);
84void *lua_getuserdata (lua_State *L, lua_Object obj); 89void *lua_getuserdata (lua_State *L, lua_Object obj);
85 90
86 91
87void lua_pushnil (lua_State *L); 92void lua_pushnil (lua_State *L);
88void lua_pushnumber (lua_State *L, double n); 93void lua_pushnumber (lua_State *L, double n);
89void lua_pushlstring (lua_State *L, const char *s, long len); 94void lua_pushlstring (lua_State *L, const char *s, size_t len);
90void lua_pushstring (lua_State *L, const char *s); 95void lua_pushstring (lua_State *L, const char *s);
91void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); 96void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
92void lua_pushusertag (lua_State *L, void *u, int tag); 97void lua_pushusertag (lua_State *L, void *u, int tag);
diff --git a/lvm.c b/lvm.c
index f3d21c0d..787130ff 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.106 2000/05/15 19:48:04 roberto Exp roberto $ 2** $Id: lvm.c,v 1.107 2000/05/22 18:44:46 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -187,7 +187,7 @@ void luaV_setglobal (lua_State *L, TString *s, StkId top) {
187 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); 187 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
188 if (ttype(im) == TAG_NIL) { /* is there a tag method? */ 188 if (ttype(im) == TAG_NIL) { /* is there a tag method? */
189 if (oldvalue != &luaO_nilobject) 189 if (oldvalue != &luaO_nilobject)
190 *oldvalue = *(top-1); 190 *(TObject *)oldvalue = *(top-1);
191 else { 191 else {
192 TObject key; 192 TObject key;
193 ttype(&key) = TAG_STRING; 193 ttype(&key) = TAG_STRING;
@@ -239,21 +239,22 @@ static void addK (lua_State *L, StkId top, int k) {
239 239
240static int luaV_strcomp (const TString *ls, const TString *rs) { 240static int luaV_strcomp (const TString *ls, const TString *rs) {
241 const char *l = ls->str; 241 const char *l = ls->str;
242 long ll = ls->u.s.len; 242 size_t ll = ls->u.s.len;
243 const char *r = rs->str; 243 const char *r = rs->str;
244 long lr = rs->u.s.len; 244 size_t lr = rs->u.s.len;
245 for (;;) { 245 for (;;) {
246 long temp = strcoll(l, r); 246 int temp = strcoll(l, r);
247 if (temp != 0) return temp; 247 if (temp != 0) return temp;
248 /* strings are equal up to a '\0' */ 248 else { /* strings are equal up to a '\0' */
249 temp = strlen(l); /* index of first '\0' in both strings */ 249 size_t len = strlen(l); /* index of first '\0' in both strings */
250 if (temp == ll) /* l is finished? */ 250 if (len == ll) /* l is finished? */
251 return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */ 251 return (len == lr) ? 0 : -1; /* l is equal or smaller than r */
252 else if (temp == lr) /* r is finished? */ 252 else if (len == lr) /* r is finished? */
253 return 1; /* l is greater than r (because l is not finished) */ 253 return 1; /* l is greater than r (because l is not finished) */
254 /* both strings longer than temp; go on comparing (after the '\0') */ 254 /* both strings longer than `len'; go on comparing (after the '\0') */
255 temp++; 255 len++;
256 l += temp; ll -= temp; r += temp; lr -= temp; 256 l += len; ll -= len; r += len; lr -= len;
257 }
257 } 258 }
258} 259}
259 260
@@ -281,17 +282,18 @@ static void strconc (lua_State *L, int total, StkId top) {
281 call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation"); 282 call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
282 else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */ 283 else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
283 /* at least two string values; get as many as possible */ 284 /* at least two string values; get as many as possible */
284 long tl = tsvalue(top-1)->u.s.len + tsvalue(top-2)->u.s.len; 285 lint32 tl = tsvalue(top-1)->u.s.len + tsvalue(top-2)->u.s.len;
285 char *buffer; 286 char *buffer;
286 int i; 287 int i;
287 while (n < total && !tostring(L, top-n-1)) { /* collect total length */ 288 while (n < total && !tostring(L, top-n-1)) { /* collect total length */
288 tl += tsvalue(top-n-1)->u.s.len; 289 tl += tsvalue(top-n-1)->u.s.len;
289 n++; 290 n++;
290 } 291 }
292 if (tl > MAX_SIZET) lua_error(L, "string size overflow");
291 buffer = luaL_openspace(L, tl); 293 buffer = luaL_openspace(L, tl);
292 tl = 0; 294 tl = 0;
293 for (i=n; i>0; i--) { /* concat all strings */ 295 for (i=n; i>0; i--) { /* concat all strings */
294 long l = tsvalue(top-i)->u.s.len; 296 lint32 l = tsvalue(top-i)->u.s.len;
295 memcpy(buffer+tl, tsvalue(top-i)->str, l); 297 memcpy(buffer+tl, tsvalue(top-i)->str, l);
296 tl += l; 298 tl += l;
297 } 299 }
diff --git a/lzio.c b/lzio.c
index 7a05dba4..cecd16a1 100644
--- a/lzio.c
+++ b/lzio.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lzio.c,v 1.10 2000/02/08 16:39:42 roberto Exp roberto $ 2** $Id: lzio.c,v 1.11 2000/03/03 14:58:26 roberto Exp roberto $
3** a generic input stream interface 3** a generic input stream interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -21,7 +21,7 @@ static int zmfilbuf (ZIO* z) {
21} 21}
22 22
23 23
24ZIO* zmopen (ZIO* z, const char* b, int size, const char *name) { 24ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
25 if (b==NULL) return NULL; 25 if (b==NULL) return NULL;
26 z->n = size; 26 z->n = size;
27 z->p = (const unsigned char *)b; 27 z->p = (const unsigned char *)b;
@@ -41,7 +41,7 @@ ZIO* zsopen (ZIO* z, const char* s, const char *name) {
41/* -------------------------------------------------------------- FILEs --- */ 41/* -------------------------------------------------------------- FILEs --- */
42 42
43static int zffilbuf (ZIO* z) { 43static int zffilbuf (ZIO* z) {
44 int n; 44 size_t n;
45 if (feof((FILE *)z->u)) return EOZ; 45 if (feof((FILE *)z->u)) return EOZ;
46 n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u); 46 n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
47 if (n==0) return EOZ; 47 if (n==0) return EOZ;
@@ -63,9 +63,9 @@ ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
63 63
64 64
65/* --------------------------------------------------------------- read --- */ 65/* --------------------------------------------------------------- read --- */
66int zread (ZIO *z, void *b, int n) { 66size_t zread (ZIO *z, void *b, size_t n) {
67 while (n) { 67 while (n) {
68 int m; 68 size_t m;
69 if (z->n == 0) { 69 if (z->n == 0) {
70 if (z->filbuf(z) == EOZ) 70 if (z->filbuf(z) == EOZ)
71 return n; /* return number of missing bytes */ 71 return n; /* return number of missing bytes */
diff --git a/lzio.h b/lzio.h
index 448ef059..bcecd0d9 100644
--- a/lzio.h
+++ b/lzio.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lzio.h,v 1.4 1998/01/09 14:57:43 roberto Exp roberto $ 2** $Id: lzio.h,v 1.5 1999/08/16 20:52:00 roberto Exp roberto $
3** Buffered streams 3** Buffered streams
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -24,11 +24,11 @@ typedef struct zio ZIO;
24 24
25ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */ 25ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
26ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */ 26ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */
27ZIO* zmopen (ZIO* z, const char* b, int size, const char *name); /* memory */ 27ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
28 28
29int zread (ZIO* z, void* b, int n); /* read next n bytes */ 29size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
30 30
31#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z)) 31#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
32#define zungetc(z) (++(z)->n,--(z)->p) 32#define zungetc(z) (++(z)->n,--(z)->p)
33#define zname(z) ((z)->name) 33#define zname(z) ((z)->name)
34 34
@@ -38,7 +38,7 @@ int zread (ZIO* z, void* b, int n); /* read next n bytes */
38#define ZBSIZE 256 /* buffer size */ 38#define ZBSIZE 256 /* buffer size */
39 39
40struct zio { 40struct zio {
41 int n; /* bytes still unread */ 41 size_t n; /* bytes still unread */
42 const unsigned char* p; /* current position in buffer */ 42 const unsigned char* p; /* current position in buffer */
43 int (*filbuf)(ZIO* z); 43 int (*filbuf)(ZIO* z);
44 void* u; /* additional data */ 44 void* u; /* additional data */
diff --git a/manual.tex b/manual.tex
index 80ed4504..3d098d32 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.37 2000/05/12 19:19:18 roberto Exp roberto $ 1% $Id: manual.tex,v 1.38 2000/05/12 19:49:18 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -122,7 +122,7 @@ Waldemar Celes
122\tecgraf\ --- Computer Science Department --- PUC-Rio 122\tecgraf\ --- Computer Science Department --- PUC-Rio
123} 123}
124 124
125\date{{\small \tt\$Date: 2000/05/12 19:19:18 $ $}} 125\date{{\small \tt\$Date: 2000/05/12 19:49:18 $ $}}
126 126
127\maketitle 127\maketitle
128 128
@@ -1695,7 +1695,7 @@ you can use the following conversion functions:
1695\begin{verbatim} 1695\begin{verbatim}
1696double lua_getnumber (lua_Object object); 1696double lua_getnumber (lua_Object object);
1697const char *lua_getstring (lua_Object object); 1697const char *lua_getstring (lua_Object object);
1698long lua_strlen (lua_Object object); 1698size_t lua_strlen (lua_Object object);
1699lua_CFunction lua_getcfunction (lua_Object object); 1699lua_CFunction lua_getcfunction (lua_Object object);
1700void *lua_getuserdata (lua_Object object); 1700void *lua_getuserdata (lua_Object object);
1701\end{verbatim} 1701\end{verbatim}
@@ -1765,7 +1765,7 @@ is done with the following functions:
1765\Deffunc{lua_pushuserdata}\label{pushing} 1765\Deffunc{lua_pushuserdata}\label{pushing}
1766\begin{verbatim} 1766\begin{verbatim}
1767void lua_pushnumber (double n); 1767void lua_pushnumber (double n);
1768void lua_pushlstring (const char *s, long len); 1768void lua_pushlstring (const char *s, size_t len);
1769void lua_pushstring (const char *s); 1769void lua_pushstring (const char *s);
1770void lua_pushusertag (void *u, int tag); 1770void lua_pushusertag (void *u, int tag);
1771void lua_pushnil (void); 1771void lua_pushnil (void);
@@ -1864,7 +1864,7 @@ using the following functions:%
1864\begin{verbatim} 1864\begin{verbatim}
1865int lua_dofile (const char *filename); 1865int lua_dofile (const char *filename);
1866int lua_dostring (const char *string); 1866int lua_dostring (const char *string);
1867int lua_dobuffer (const char *buff, int size, const char *name); 1867int lua_dobuffer (const char *buff, size_t size, const char *name);
1868\end{verbatim} 1868\end{verbatim}
1869All these functions return an error code: 1869All these functions return an error code:
18700, in case of success; non zero, in case of errors. 18700, in case of success; non zero, in case of errors.