aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-12-17 18:48:58 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-12-17 18:48:58 -0200
commit502343b40230dfb00efc37bdbaa5c5576f3a5aa5 (patch)
treeb75f9e296ad3229a607fb2b7152dd9efc3706ef6
parent82d09fbf0dbd5aee890f033b25b09dc48ce58a48 (diff)
downloadlua-502343b40230dfb00efc37bdbaa5c5576f3a5aa5.tar.gz
lua-502343b40230dfb00efc37bdbaa5c5576f3a5aa5.tar.bz2
lua-502343b40230dfb00efc37bdbaa5c5576f3a5aa5.zip
new scheme for buffers, centralized in auxlib.
-rw-r--r--lauxlib.c71
-rw-r--r--lauxlib.h9
-rw-r--r--lbuiltin.c4
-rw-r--r--lgc.c3
-rw-r--r--liolib.c9
-rw-r--r--llex.c64
-rw-r--r--llex.h9
-rw-r--r--lmem.c19
-rw-r--r--lmem.h4
-rw-r--r--lstate.c4
-rw-r--r--lstate.h8
-rw-r--r--lstrlib.c125
-rw-r--r--lualib.h5
-rw-r--r--lvm.c6
-rw-r--r--makefile13
15 files changed, 181 insertions, 172 deletions
diff --git a/lauxlib.c b/lauxlib.c
index ed893f70..ce6c2c78 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.4 1997/11/21 19:00:46 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.5 1997/12/09 13:35:19 roberto Exp roberto $
3** Auxiliar functions for building Lua libraries 3** Auxiliar functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -10,6 +10,8 @@
10#include <string.h> 10#include <string.h>
11 11
12#include "lauxlib.h" 12#include "lauxlib.h"
13#include "lmem.h"
14#include "lstate.h"
13#include "lua.h" 15#include "lua.h"
14#include "luadebug.h" 16#include "luadebug.h"
15 17
@@ -99,3 +101,70 @@ void luaL_verror (char *fmt, ...)
99} 101}
100 102
101 103
104/*-------------------------------------------------------
105** Auxiliar buffer
106-------------------------------------------------------*/
107
108#define BUFF_STEP 32
109
110#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
111
112static void Openspace (int size)
113{
114 LState *l = L; /* to optimize */
115 int base = l->Mbuffbase-l->Mbuffer;
116 l->Mbuffsize *= 2;
117 if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */
118 l->Mbuffsize = l->Mbuffnext+size;
119 l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize);
120 l->Mbuffbase = l->Mbuffer+base;
121}
122
123
124char *luaL_openspace (int size)
125{
126 openspace(size);
127 return L->Mbuffer+L->Mbuffnext;
128}
129
130
131void luaL_addchar (int c)
132{
133 openspace(BUFF_STEP);
134 L->Mbuffer[L->Mbuffnext++] = c;
135}
136
137
138void luaL_resetbuffer (void)
139{
140 L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
141}
142
143
144void luaL_addsize (int n)
145{
146 L->Mbuffnext += n;
147}
148
149
150int luaL_newbuffer (int size)
151{
152 int old = L->Mbuffbase-L->Mbuffer;
153 openspace(size);
154 L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
155 return old;
156}
157
158
159void luaL_oldbuffer (int old)
160{
161 L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
162 L->Mbuffbase = L->Mbuffer+old;
163}
164
165
166char *luaL_buffer (void)
167{
168 return L->Mbuffbase;
169}
170
diff --git a/lauxlib.h b/lauxlib.h
index 712c484c..ae043322 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.h,v 1.3 1997/11/21 19:00:46 roberto Exp roberto $ 2** $Id: lauxlib.h,v 1.4 1997/12/09 13:35:19 roberto Exp roberto $
3** Auxiliar functions for building Lua libraries 3** Auxiliar functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,6 +27,13 @@ lua_Object luaL_functionarg (int arg);
27lua_Object luaL_tablearg (int arg); 27lua_Object luaL_tablearg (int arg);
28lua_Object luaL_nonnullarg (int numArg); 28lua_Object luaL_nonnullarg (int numArg);
29void luaL_verror (char *fmt, ...); 29void luaL_verror (char *fmt, ...);
30char *luaL_openspace (int size);
31void luaL_resetbuffer (void);
32void luaL_addchar (int c);
33void luaL_addsize (int n);
34int luaL_newbuffer (int size);
35void luaL_oldbuffer (int old);
36char *luaL_buffer (void);
30 37
31 38
32#endif 39#endif
diff --git a/lbuiltin.c b/lbuiltin.c
index c43a230b..fa83e58a 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.16 1997/12/11 17:21:11 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.17 1997/12/15 16:17:20 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*/
@@ -131,7 +131,7 @@ static void internaldofile (void)
131 131
132static char *to_string (lua_Object obj) 132static char *to_string (lua_Object obj)
133{ 133{
134 char *buff = luaM_buffer(30); 134 char *buff = luaL_openspace(30);
135 TObject *o = luaA_Address(obj); 135 TObject *o = luaA_Address(obj);
136 switch (ttype(o)) { 136 switch (ttype(o)) {
137 case LUA_T_NUMBER: case LUA_T_STRING: 137 case LUA_T_NUMBER: case LUA_T_STRING:
diff --git a/lgc.c b/lgc.c
index 1af3cb85..c5a99e62 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.12 1997/12/11 14:48:46 roberto Exp roberto $ 2** $Id: lgc.c,v 1.13 1997/12/15 16:17:20 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*/
@@ -272,7 +272,6 @@ long lua_collectgarbage (long limit)
272 luaS_free(freestr); 272 luaS_free(freestr);
273 luaF_freeproto(freefunc); 273 luaF_freeproto(freefunc);
274 luaF_freeclosure(freeclos); 274 luaF_freeclosure(freeclos);
275 luaM_clearbuffer();
276 recovered = recovered-L->nblocks; 275 recovered = recovered-L->nblocks;
277 L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit; 276 L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
278 return recovered; 277 return recovered;
diff --git a/liolib.c b/liolib.c
index 4befcfa6..46c4e87c 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.8 1997/11/28 12:40:37 roberto Exp roberto $ 2** $Id: liolib.c,v 1.9 1997/12/09 13:50:08 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*/
@@ -190,7 +190,7 @@ static void io_read (void)
190 char *p = luaL_opt_string(arg, "[^\n]*{\n}"); 190 char *p = luaL_opt_string(arg, "[^\n]*{\n}");
191 int inskip = 0; /* to control {skips} */ 191 int inskip = 0; /* to control {skips} */
192 int c = NEED_OTHER; 192 int c = NEED_OTHER;
193 luaI_emptybuff(); 193 luaL_resetbuffer();
194 while (*p) { 194 while (*p) {
195 if (*p == '{') { 195 if (*p == '{') {
196 inskip++; 196 inskip++;
@@ -208,7 +208,7 @@ static void io_read (void)
208 if (c == NEED_OTHER) c = getc(f); 208 if (c == NEED_OTHER) c = getc(f);
209 m = luaI_singlematch((c == EOF) ? 0 : (char)c, p, &ep); 209 m = luaI_singlematch((c == EOF) ? 0 : (char)c, p, &ep);
210 if (m) { 210 if (m) {
211 if (inskip == 0) luaI_addchar(c); 211 if (inskip == 0) luaL_addchar(c);
212 c = NEED_OTHER; 212 c = NEED_OTHER;
213 } 213 }
214 switch (*ep) { 214 switch (*ep) {
@@ -227,7 +227,8 @@ static void io_read (void)
227 } break_while: 227 } break_while:
228 if (c >= 0) /* not EOF nor NEED_OTHER? */ 228 if (c >= 0) /* not EOF nor NEED_OTHER? */
229 ungetc(c, f); 229 ungetc(c, f);
230 buff = luaI_addchar(0); 230 luaL_addchar(0);
231 buff = luaL_buffer();
231 if (*buff != 0 || *p == 0) /* read something or did not fail? */ 232 if (*buff != 0 || *p == 0) /* read something or did not fail? */
232 lua_pushstring(buff); 233 lua_pushstring(buff);
233} 234}
diff --git a/llex.c b/llex.c
index 9c8bcb3d..2fd65496 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.9 1997/12/02 12:43:54 roberto Exp roberto $ 2** $Id: llex.c,v 1.10 1997/12/09 13:35:19 roberto Exp roberto $
3** Lexical Analizer 3** Lexical Analizer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -8,6 +8,7 @@
8#include <ctype.h> 8#include <ctype.h>
9#include <string.h> 9#include <string.h>
10 10
11#include "lauxlib.h"
11#include "llex.h" 12#include "llex.h"
12#include "lmem.h" 13#include "lmem.h"
13#include "lobject.h" 14#include "lobject.h"
@@ -66,8 +67,7 @@ void luaX_setinput (ZIO *z)
66 LS->ifstate[0].elsepart = 1; /* to avoid a free $else */ 67 LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
67 LS->lex_z = z; 68 LS->lex_z = z;
68 firstline(LS); 69 firstline(LS);
69 LS->textbuff.buffsize = 20; 70 luaL_resetbuffer();
70 LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize);
71} 71}
72 72
73 73
@@ -201,31 +201,24 @@ static void inclinenumber (LexState *LS)
201 201
202 202
203 203
204static void save (LexState *LS, int c) 204#define save(c) luaL_addchar(c)
205{ 205#define save_and_next(LS) (save(LS->current), next(LS))
206 if (LS->textbuff.tokensize >= LS->textbuff.buffsize)
207 LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize *= 2);
208 LS->textbuff.text[LS->textbuff.tokensize++] = c;
209}
210 206
211 207
212char *luaX_lasttoken (void) 208char *luaX_lasttoken (void)
213{ 209{
214 save(L->lexstate, 0); 210 save(0);
215 return L->lexstate->textbuff.text; 211 return luaL_buffer();
216} 212}
217 213
218 214
219#define save_and_next(LS) (save(LS, LS->current), next(LS))
220
221
222static int read_long_string (LexState *LS, YYSTYPE *l) 215static int read_long_string (LexState *LS, YYSTYPE *l)
223{ 216{
224 int cont = 0; 217 int cont = 0;
225 while (1) { 218 while (1) {
226 switch (LS->current) { 219 switch (LS->current) {
227 case EOZ: 220 case EOZ:
228 save(LS, 0); 221 save(0);
229 return WRONGTOKEN; 222 return WRONGTOKEN;
230 case '[': 223 case '[':
231 save_and_next(LS); 224 save_and_next(LS);
@@ -243,7 +236,7 @@ static int read_long_string (LexState *LS, YYSTYPE *l)
243 } 236 }
244 continue; 237 continue;
245 case '\n': 238 case '\n':
246 save(LS, '\n'); 239 save('\n');
247 inclinenumber(LS); 240 inclinenumber(LS);
248 continue; 241 continue;
249 default: 242 default:
@@ -251,9 +244,9 @@ static int read_long_string (LexState *LS, YYSTYPE *l)
251 } 244 }
252 } endloop: 245 } endloop:
253 save_and_next(LS); /* pass the second ']' */ 246 save_and_next(LS); /* pass the second ']' */
254 LS->textbuff.text[LS->textbuff.tokensize-2] = 0; /* erases ']]' */ 247 L->Mbuffer[L->Mbuffnext-2] = 0; /* erases ']]' */
255 l->pTStr = luaS_new(LS->textbuff.text+2); 248 l->pTStr = luaS_new(L->Mbuffbase+2);
256 LS->textbuff.text[LS->textbuff.tokensize-2] = ']'; /* restores ']]' */ 249 L->Mbuffer[L->Mbuffnext-2] = ']'; /* restores ']]' */
257 return STRING; 250 return STRING;
258} 251}
259 252
@@ -267,7 +260,7 @@ int luaY_lex (YYSTYPE *l)
267{ 260{
268 LexState *LS = L->lexstate; 261 LexState *LS = L->lexstate;
269 double a; 262 double a;
270 LS->textbuff.tokensize = 0; 263 luaL_resetbuffer();
271 if (lua_debug) 264 if (lua_debug)
272 luaY_codedebugline(LS->linelasttoken); 265 luaY_codedebugline(LS->linelasttoken);
273 LS->linelasttoken = LS->linenumber; 266 LS->linelasttoken = LS->linenumber;
@@ -286,7 +279,7 @@ int luaY_lex (YYSTYPE *l)
286 save_and_next(LS); 279 save_and_next(LS);
287 if (LS->current != '-') return '-'; 280 if (LS->current != '-') return '-';
288 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); 281 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
289 LS->textbuff.tokensize = 0; 282 luaL_resetbuffer();
290 continue; 283 continue;
291 284
292 case '[': 285 case '[':
@@ -325,15 +318,15 @@ int luaY_lex (YYSTYPE *l)
325 switch (LS->current) { 318 switch (LS->current) {
326 case EOZ: 319 case EOZ:
327 case '\n': 320 case '\n':
328 save(LS, 0); 321 save(0);
329 return WRONGTOKEN; 322 return WRONGTOKEN;
330 case '\\': 323 case '\\':
331 next(LS); /* do not save the '\' */ 324 next(LS); /* do not save the '\' */
332 switch (LS->current) { 325 switch (LS->current) {
333 case 'n': save(LS, '\n'); next(LS); break; 326 case 'n': save('\n'); next(LS); break;
334 case 't': save(LS, '\t'); next(LS); break; 327 case 't': save('\t'); next(LS); break;
335 case 'r': save(LS, '\r'); next(LS); break; 328 case 'r': save('\r'); next(LS); break;
336 case '\n': save(LS, '\n'); inclinenumber(LS); break; 329 case '\n': save('\n'); inclinenumber(LS); break;
337 default : save_and_next(LS); break; 330 default : save_and_next(LS); break;
338 } 331 }
339 break; 332 break;
@@ -342,9 +335,9 @@ int luaY_lex (YYSTYPE *l)
342 } 335 }
343 } 336 }
344 next(LS); /* skip delimiter */ 337 next(LS); /* skip delimiter */
345 save(LS, 0); 338 save(0);
346 l->pTStr = luaS_new(LS->textbuff.text+1); 339 l->pTStr = luaS_new(L->Mbuffbase+1);
347 LS->textbuff.text[LS->textbuff.tokensize-1] = del; /* restore delimiter */ 340 L->Mbuffer[L->Mbuffnext-1] = del; /* restore delimiter */
348 return STRING; 341 return STRING;
349 } 342 }
350 343
@@ -375,7 +368,7 @@ int luaY_lex (YYSTYPE *l)
375 if (LS->current == '.') { 368 if (LS->current == '.') {
376 save_and_next(LS); 369 save_and_next(LS);
377 if (LS->current == '.') { 370 if (LS->current == '.') {
378 save(LS, 0); 371 save(0);
379 luaY_error( 372 luaY_error(
380 "ambiguous syntax (decimal point x string concatenation)"); 373 "ambiguous syntax (decimal point x string concatenation)");
381 } 374 }
@@ -396,7 +389,7 @@ int luaY_lex (YYSTYPE *l)
396 neg=(LS->current=='-'); 389 neg=(LS->current=='-');
397 if (LS->current == '+' || LS->current == '-') save_and_next(LS); 390 if (LS->current == '+' || LS->current == '-') save_and_next(LS);
398 if (!isdigit(LS->current)) { 391 if (!isdigit(LS->current)) {
399 save(LS, 0); return WRONGTOKEN; } 392 save(0); return WRONGTOKEN; }
400 do { 393 do {
401 e=10.0*e+(LS->current-'0'); 394 e=10.0*e+(LS->current-'0');
402 save_and_next(LS); 395 save_and_next(LS);
@@ -412,23 +405,24 @@ int luaY_lex (YYSTYPE *l)
412 } 405 }
413 406
414 case EOZ: 407 case EOZ:
415 save(LS, 0); 408 save(0);
416 if (LS->iflevel > 0) 409 if (LS->iflevel > 0)
417 luaY_syntaxerror("input ends inside a $if", ""); 410 luaY_syntaxerror("input ends inside a $if", "");
418 return 0; 411 return 0;
419 412
420 default: 413 default:
421 if (LS->current != '_' && !isalpha(LS->current)) { 414 if (LS->current != '_' && !isalpha(LS->current)) {
415 int c = LS->current;
422 save_and_next(LS); 416 save_and_next(LS);
423 return LS->textbuff.text[0]; 417 return c;
424 } 418 }
425 else { /* identifier or reserved word */ 419 else { /* identifier or reserved word */
426 TaggedString *ts; 420 TaggedString *ts;
427 do { 421 do {
428 save_and_next(LS); 422 save_and_next(LS);
429 } while (isalnum(LS->current) || LS->current == '_'); 423 } while (isalnum(LS->current) || LS->current == '_');
430 save(LS, 0); 424 save(0);
431 ts = luaS_new(LS->textbuff.text); 425 ts = luaS_new(L->Mbuffbase);
432 if (ts->head.marked > 255) 426 if (ts->head.marked > 255)
433 return ts->head.marked; /* reserved word */ 427 return ts->head.marked; /* reserved word */
434 l->pTStr = ts; 428 l->pTStr = ts;
diff --git a/llex.h b/llex.h
index eba9c351..7ebfa378 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.4 1997/11/26 18:53:45 roberto Exp roberto $ 2** $Id: llex.h,v 1.5 1997/12/02 12:43:44 roberto Exp roberto $
3** Lexical Analizer 3** Lexical Analizer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -21,18 +21,11 @@ struct ifState {
21 int skip; /* true if part must be skiped */ 21 int skip; /* true if part must be skiped */
22}; 22};
23 23
24struct textBuff {
25 char *text; /* always points to luaM_buffer */
26 int tokensize;
27 int buffsize;
28};
29
30 24
31typedef struct LexState { 25typedef struct LexState {
32 int current; /* look ahead character */ 26 int current; /* look ahead character */
33 struct zio *lex_z; /* input stream */ 27 struct zio *lex_z; /* input stream */
34 int linenumber; /* input line counter */ 28 int linenumber; /* input line counter */
35 struct textBuff textbuff; /* buffer for tokens */
36 int linelasttoken; /* line where last token was read */ 29 int linelasttoken; /* line where last token was read */
37 int lastline; /* last line wherein a SETLINE was generated */ 30 int lastline; /* last line wherein a SETLINE was generated */
38 struct ifState ifstate[MAX_IFS]; 31 struct ifState ifstate[MAX_IFS];
diff --git a/lmem.c b/lmem.c
index 069c4d0d..15ea9fc3 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.2 1997/11/19 17:29:23 roberto Exp roberto $ 2** $Id: lmem.c,v 1.3 1997/12/01 20:30:44 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*/
@@ -27,23 +27,6 @@ int luaM_growaux (void **block, unsigned long nelems, int size,
27 27
28 28
29 29
30void *luaM_buffer (unsigned long size)
31{
32 if (size > L->Mbuffsize) {
33 L->Mbuffsize = size;
34 L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
35 }
36 return L->Mbuffer;
37}
38
39
40void luaM_clearbuffer (void)
41{
42 L->Mbuffsize /= 2;
43 L->Mbuffer = luaM_realloc(L->Mbuffer, L->Mbuffsize);
44}
45
46
47#ifndef DEBUG 30#ifndef DEBUG
48 31
49/* 32/*
diff --git a/lmem.h b/lmem.h
index ac7ba9ae..94e81515 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.3 1997/11/26 20:44:52 roberto Exp roberto $ 2** $Id: lmem.h,v 1.4 1997/12/01 20:30:44 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*/
@@ -20,8 +20,6 @@
20#define tableEM "table overflow" 20#define tableEM "table overflow"
21#define memEM "not enough memory" 21#define memEM "not enough memory"
22 22
23void *luaM_buffer (unsigned long size);
24void luaM_clearbuffer (void);
25void *luaM_realloc (void *oldblock, unsigned long size); 23void *luaM_realloc (void *oldblock, unsigned long size);
26int luaM_growaux (void **block, unsigned long nelems, int size, 24int luaM_growaux (void **block, unsigned long nelems, int size,
27 char *errormsg, unsigned long limit); 25 char *errormsg, unsigned long limit);
diff --git a/lstate.c b/lstate.c
index e1011a1a..c1a1b43d 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.3 1997/12/01 20:31:25 roberto Exp roberto $ 2** $Id: lstate.c,v 1.4 1997/12/11 14:48:46 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*/
@@ -40,6 +40,8 @@ void lua_open (void)
40 L->refArray = NULL; 40 L->refArray = NULL;
41 L->refSize = 0; 41 L->refSize = 0;
42 L->Mbuffsize = 0; 42 L->Mbuffsize = 0;
43 L->Mbuffnext = 0;
44 L->Mbuffbase = NULL;
43 L->Mbuffer = NULL; 45 L->Mbuffer = NULL;
44 L->GCthreshold = GARBAGE_BLOCK; 46 L->GCthreshold = GARBAGE_BLOCK;
45 L->nblocks = 0; 47 L->nblocks = 0;
diff --git a/lstate.h b/lstate.h
index 9388b444..3da2e2ef 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.4 1997/11/27 15:59:25 roberto Exp roberto $ 2** $Id: lstate.h,v 1.5 1997/11/28 16:56:05 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*/
@@ -65,8 +65,10 @@ typedef struct LState {
65 int refSize; /* size of refArray */ 65 int refSize; /* size of refArray */
66 unsigned long GCthreshold; 66 unsigned long GCthreshold;
67 unsigned long nblocks; /* number of 'blocks' currently allocated */ 67 unsigned long nblocks; /* number of 'blocks' currently allocated */
68 char *Mbuffer; /* global buffer, used by luaM_buffer */ 68 char *Mbuffer; /* global buffer */
69 unsigned long Mbuffsize; /* size of Mbuffer */ 69 char *Mbuffbase; /* current first position of Mbuffer */
70 int Mbuffsize; /* size of Mbuffer */
71 int Mbuffnext; /* next position to fill in Mbuffer */
70} LState; 72} LState;
71 73
72 74
diff --git a/lstrlib.c b/lstrlib.c
index 9f1ae82d..cca70bc5 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.3 1997/12/09 13:35:19 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.4 1997/12/15 17:58:49 roberto Exp roberto $
3** Standard library for strings and pattern-matching 3** Standard library for strings and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -15,56 +15,12 @@
15#include "lualib.h" 15#include "lualib.h"
16 16
17 17
18struct lbuff {
19 char *b;
20 size_t max;
21 size_t size;
22};
23
24static struct lbuff lbuffer = {NULL, 0, 0};
25
26
27static char *strbuffer (unsigned long size)
28{
29 if (size > lbuffer.max) {
30 /* ANSI "realloc" doesn't need this test, but some machines (Sun!)
31 don't follow ANSI */
32 lbuffer.b = (lbuffer.b) ? realloc(lbuffer.b, lbuffer.max=size) :
33 malloc(lbuffer.max=size);
34 if (lbuffer.b == NULL)
35 lua_error("memory overflow");
36 }
37 return lbuffer.b;
38}
39
40
41static char *openspace (unsigned long size)
42{
43 char *buff = strbuffer(lbuffer.size+size);
44 return buff+lbuffer.size;
45}
46
47
48char *luaI_addchar (int c)
49{
50 if (lbuffer.size >= lbuffer.max)
51 strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2);
52 lbuffer.b[lbuffer.size++] = c;
53 return lbuffer.b;
54}
55
56
57void luaI_emptybuff (void)
58{
59 lbuffer.size = 0; /* prepare for next string */
60}
61
62 18
63static void addnchar (char *s, int n) 19static void addnchar (char *s, int n)
64{ 20{
65 char *b = openspace(n); 21 char *b = luaL_openspace(n);
66 strncpy(b, s, n); 22 strncpy(b, s, n);
67 lbuffer.size += n; 23 luaL_addsize(n);
68} 24}
69 25
70 26
@@ -80,6 +36,13 @@ static void str_len (void)
80} 36}
81 37
82 38
39static void closeandpush (void)
40{
41 luaL_addchar(0);
42 lua_pushstring(luaL_buffer());
43}
44
45
83static void str_sub (void) 46static void str_sub (void)
84{ 47{
85 char *s = luaL_check_string(1); 48 char *s = luaL_check_string(1);
@@ -89,9 +52,9 @@ static void str_sub (void)
89 if (start < 0) start = l+start+1; 52 if (start < 0) start = l+start+1;
90 if (end < 0) end = l+end+1; 53 if (end < 0) end = l+end+1;
91 if (1 <= start && start <= end && end <= l) { 54 if (1 <= start && start <= end && end <= l) {
92 luaI_emptybuff(); 55 luaL_resetbuffer();
93 addnchar(s+start-1, end-start+1); 56 addnchar(s+start-1, end-start+1);
94 lua_pushstring(luaI_addchar(0)); 57 closeandpush();
95 } 58 }
96 else lua_pushstring(""); 59 else lua_pushstring("");
97} 60}
@@ -100,30 +63,30 @@ static void str_sub (void)
100static void str_lower (void) 63static void str_lower (void)
101{ 64{
102 char *s; 65 char *s;
103 luaI_emptybuff(); 66 luaL_resetbuffer();
104 for (s = luaL_check_string(1); *s; s++) 67 for (s = luaL_check_string(1); *s; s++)
105 luaI_addchar(tolower((unsigned char)*s)); 68 luaL_addchar(tolower((unsigned char)*s));
106 lua_pushstring(luaI_addchar(0)); 69 closeandpush();
107} 70}
108 71
109 72
110static void str_upper (void) 73static void str_upper (void)
111{ 74{
112 char *s; 75 char *s;
113 luaI_emptybuff(); 76 luaL_resetbuffer();
114 for (s = luaL_check_string(1); *s; s++) 77 for (s = luaL_check_string(1); *s; s++)
115 luaI_addchar(toupper((unsigned char)*s)); 78 luaL_addchar(toupper((unsigned char)*s));
116 lua_pushstring(luaI_addchar(0)); 79 closeandpush();
117} 80}
118 81
119static void str_rep (void) 82static void str_rep (void)
120{ 83{
121 char *s = luaL_check_string(1); 84 char *s = luaL_check_string(1);
122 int n = (int)luaL_check_number(2); 85 int n = (int)luaL_check_number(2);
123 luaI_emptybuff(); 86 luaL_resetbuffer();
124 while (n-- > 0) 87 while (n-- > 0)
125 addstr(s); 88 addstr(s);
126 lua_pushstring(luaI_addchar(0)); 89 closeandpush();
127} 90}
128 91
129 92
@@ -163,7 +126,7 @@ static void push_captures (struct Capture *cap)
163 int i; 126 int i;
164 for (i=0; i<cap->level; i++) { 127 for (i=0; i<cap->level; i++) {
165 int l = cap->capture[i].len; 128 int l = cap->capture[i].len;
166 char *buff = openspace(l+1); 129 char *buff = luaL_openspace(l+1);
167 if (l == -1) lua_error("unfinished capture"); 130 if (l == -1) lua_error("unfinished capture");
168 strncpy(buff, cap->capture[i].init, l); 131 strncpy(buff, cap->capture[i].init, l);
169 buff[l] = 0; 132 buff[l] = 0;
@@ -395,7 +358,7 @@ static void add_s (lua_Object newp, struct Capture *cap)
395 char *news = lua_getstring(newp); 358 char *news = lua_getstring(newp);
396 while (*news) { 359 while (*news) {
397 if (*news != ESC || !isdigit((unsigned char)*++news)) 360 if (*news != ESC || !isdigit((unsigned char)*++news))
398 luaI_addchar(*news++); 361 luaL_addchar(*news++);
399 else { 362 else {
400 int l = check_cap(*news++, cap); 363 int l = check_cap(*news++, cap);
401 addnchar(cap->capture[l].init, cap->capture[l].len); 364 addnchar(cap->capture[l].init, cap->capture[l].len);
@@ -404,24 +367,24 @@ static void add_s (lua_Object newp, struct Capture *cap)
404 } 367 }
405 else if (lua_isfunction(newp)) { 368 else if (lua_isfunction(newp)) {
406 lua_Object res; 369 lua_Object res;
407 struct lbuff oldbuff;
408 int status; 370 int status;
371 int oldbuff;
409 lua_beginblock(); 372 lua_beginblock();
410 push_captures(cap); 373 push_captures(cap);
411 /* function may use lbuffer, so save it and create a luaM_new one */ 374 /* function may use buffer, so save it and create a new one */
412 oldbuff = lbuffer; 375 oldbuff = luaL_newbuffer(0);
413 lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
414 status = lua_callfunction(newp); 376 status = lua_callfunction(newp);
415 /* restore old buffer */ 377 /* restore old buffer */
416 free(lbuffer.b); 378 luaL_oldbuffer(oldbuff);
417 lbuffer = oldbuff; 379 if (status != 0) {
418 if (status != 0) 380 lua_endblock();
419 lua_error(NULL); 381 lua_error(NULL);
382 }
420 res = lua_getresult(1); 383 res = lua_getresult(1);
421 addstr(lua_isstring(res) ? lua_getstring(res) : ""); 384 addstr(lua_isstring(res) ? lua_getstring(res) : "");
422 lua_endblock(); 385 lua_endblock();
423 } 386 }
424 else luaL_arg_check(0, 3, NULL); 387 else luaL_arg_check(0, 3, "string or function expected");
425} 388}
426 389
427 390
@@ -433,7 +396,7 @@ static void str_gsub (void)
433 int max_s = (int)luaL_opt_number(4, strlen(src)+1); 396 int max_s = (int)luaL_opt_number(4, strlen(src)+1);
434 int anchor = (*p == '^') ? (p++, 1) : 0; 397 int anchor = (*p == '^') ? (p++, 1) : 0;
435 int n = 0; 398 int n = 0;
436 luaI_emptybuff(); 399 luaL_resetbuffer();
437 while (n < max_s) { 400 while (n < max_s) {
438 struct Capture cap; 401 struct Capture cap;
439 char *e; 402 char *e;
@@ -446,25 +409,25 @@ static void str_gsub (void)
446 if (e && e>src) /* non empty match? */ 409 if (e && e>src) /* non empty match? */
447 src = e; /* skip it */ 410 src = e; /* skip it */
448 else if (*src) 411 else if (*src)
449 luaI_addchar(*src++); 412 luaL_addchar(*src++);
450 else break; 413 else break;
451 if (anchor) break; 414 if (anchor) break;
452 } 415 }
453 addstr(src); 416 addstr(src);
454 lua_pushstring(luaI_addchar(0)); 417 closeandpush();
455 lua_pushnumber(n); /* number of substitutions */ 418 lua_pushnumber(n); /* number of substitutions */
456} 419}
457 420
458 421
459static void luaI_addquoted (char *s) 422static void luaI_addquoted (char *s)
460{ 423{
461 luaI_addchar('"'); 424 luaL_addchar('"');
462 for (; *s; s++) { 425 for (; *s; s++) {
463 if (strchr("\"\\\n", *s)) 426 if (strchr("\"\\\n", *s))
464 luaI_addchar('\\'); 427 luaL_addchar('\\');
465 luaI_addchar(*s); 428 luaL_addchar(*s);
466 } 429 }
467 luaI_addchar('"'); 430 luaL_addchar('"');
468} 431}
469 432
470#define MAX_FORMAT 200 433#define MAX_FORMAT 200
@@ -473,12 +436,12 @@ static void str_format (void)
473{ 436{
474 int arg = 1; 437 int arg = 1;
475 char *strfrmt = luaL_check_string(arg); 438 char *strfrmt = luaL_check_string(arg);
476 luaI_emptybuff(); /* initialize */ 439 luaL_resetbuffer();
477 while (*strfrmt) { 440 while (*strfrmt) {
478 if (*strfrmt != '%') 441 if (*strfrmt != '%')
479 luaI_addchar(*strfrmt++); 442 luaL_addchar(*strfrmt++);
480 else if (*++strfrmt == '%') 443 else if (*++strfrmt == '%')
481 luaI_addchar(*strfrmt++); /* %% */ 444 luaL_addchar(*strfrmt++); /* %% */
482 else { /* format item */ 445 else { /* format item */
483 char form[MAX_FORMAT]; /* store the format ('%...') */ 446 char form[MAX_FORMAT]; /* store the format ('%...') */
484 struct Capture cap; 447 struct Capture cap;
@@ -496,14 +459,14 @@ static void str_format (void)
496 arg++; 459 arg++;
497 strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include convertion */ 460 strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include convertion */
498 form[strfrmt-initf+2] = 0; 461 form[strfrmt-initf+2] = 0;
499 buff = openspace(1000); /* to store the formatted value */ 462 buff = luaL_openspace(1000); /* to store the formatted value */
500 switch (*strfrmt++) { 463 switch (*strfrmt++) {
501 case 'q': 464 case 'q':
502 luaI_addquoted(luaL_check_string(arg)); 465 luaI_addquoted(luaL_check_string(arg));
503 continue; 466 continue;
504 case 's': { 467 case 's': {
505 char *s = luaL_check_string(arg); 468 char *s = luaL_check_string(arg);
506 buff = openspace(strlen(s)); 469 buff = luaL_openspace(strlen(s));
507 sprintf(buff, form, s); 470 sprintf(buff, form, s);
508 break; 471 break;
509 } 472 }
@@ -517,10 +480,10 @@ static void str_format (void)
517 default: /* also treat cases 'pnLlh' */ 480 default: /* also treat cases 'pnLlh' */
518 lua_error("invalid option in `format'"); 481 lua_error("invalid option in `format'");
519 } 482 }
520 lbuffer.size += strlen(buff); 483 luaL_addsize(strlen(buff));
521 } 484 }
522 } 485 }
523 lua_pushstring(luaI_addchar(0)); /* push the result */ 486 closeandpush(); /* push the result */
524} 487}
525 488
526 489
diff --git a/lualib.h b/lualib.h
index 9e9b0193..dabe8f7a 100644
--- a/lualib.h
+++ b/lualib.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lualib.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lualib.h,v 1.2 1997/11/26 18:53:45 roberto Exp roberto $
3** Lua standard libraries 3** Lua standard libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,9 +28,6 @@ void lua_mathlibopen (void);
28 28
29/* auxiliar functions (private) */ 29/* auxiliar functions (private) */
30 30
31char *luaI_addchar (int c);
32void luaI_emptybuff (void);
33
34int luaI_singlematch (int c, char *p, char **ep); 31int luaI_singlematch (int c, char *p, char **ep);
35 32
36#endif 33#endif
diff --git a/lvm.c b/lvm.c
index 7cf995f1..5e160f71 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.16 1997/12/09 13:35:19 roberto Exp roberto $ 2** $Id: lvm.c,v 1.17 1997/12/15 16:17:20 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*/
@@ -27,7 +27,7 @@
27#define next_word(pc) (pc+=2, get_word(pc-2)) 27#define next_word(pc) (pc+=2, get_word(pc-2))
28 28
29 29
30/* Extra stack to run a function: LUA_T_LINE(1), TM calls(2), ... */ 30/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
31#define EXTRA_STACK 5 31#define EXTRA_STACK 5
32 32
33 33
@@ -35,7 +35,7 @@
35static TaggedString *strconc (char *l, char *r) 35static TaggedString *strconc (char *l, char *r)
36{ 36{
37 size_t nl = strlen(l); 37 size_t nl = strlen(l);
38 char *buffer = luaM_buffer(nl+strlen(r)+1); 38 char *buffer = luaL_openspace(nl+strlen(r)+1);
39 strcpy(buffer, l); 39 strcpy(buffer, l);
40 strcpy(buffer+nl, r); 40 strcpy(buffer+nl, r);
41 return luaS_new(buffer); 41 return luaS_new(buffer);
diff --git a/makefile b/makefile
index 234cf701..9e450230 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
1# 1#
2## $Id: makefile,v 1.5 1997/11/07 15:09:49 roberto Exp roberto $ 2## $Id: makefile,v 1.6 1997/11/19 17:29:23 roberto Exp roberto $
3## Makefile 3## Makefile
4## See Copyright Notice in lua.h 4## See Copyright Notice in lua.h
5# 5#
@@ -94,7 +94,8 @@ clear :
94 94
95lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \ 95lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \
96 lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h 96 lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h
97lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h 97lauxlib.o: lauxlib.c lauxlib.h lua.h lmem.h lstate.h lobject.h \
98 luadebug.h
98lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \ 99lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
99 ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h 100 ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h
100ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \ 101ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
@@ -103,13 +104,13 @@ lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
103lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \ 104lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
104 lstring.h ltable.h ltm.h 105 lstring.h ltable.h ltm.h
105liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h 106liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
106llex.o: llex.c llex.h lobject.h lua.h lzio.h lmem.h lparser.h lstate.h \ 107llex.o: llex.c lauxlib.h lua.h llex.h lobject.h lzio.h lmem.h \
107 lstring.h lstx.h luadebug.h 108 lparser.h lstate.h lstring.h lstx.h luadebug.h
108lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h 109lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
109lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h 110lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h
110lobject.o: lobject.c lobject.h lua.h 111lobject.o: lobject.c lobject.h lua.h
111lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h llex.h \ 112lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h lfunc.h \
112 lzio.h lmem.h lstring.h ltable.h ltm.h 113 lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
113lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h 114lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h
114lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h 115lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
115lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \ 116lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \