summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c245
-rw-r--r--lapi.h7
-rw-r--r--lbuiltin.c330
-rw-r--r--lbuiltin.h38
-rw-r--r--lgc.c4
-rw-r--r--llex.c8
-rw-r--r--lmathlib.c4
-rw-r--r--lmem.c6
-rw-r--r--lobject.c4
-rw-r--r--lobject.h4
-rw-r--r--lref.c40
-rw-r--r--lref.h5
-rw-r--r--lvm.c4
13 files changed, 170 insertions, 529 deletions
diff --git a/lapi.c b/lapi.c
index 953c066e..caeb6318 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.62 1999/12/02 16:24:45 roberto Exp roberto $ 2** $Id: lapi.c,v 1.63 1999/12/06 12:03:45 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*/
@@ -22,16 +22,15 @@
22#include "ltable.h" 22#include "ltable.h"
23#include "ltm.h" 23#include "ltm.h"
24#include "lua.h" 24#include "lua.h"
25#include "luadebug.h"
26#include "lvm.h" 25#include "lvm.h"
27 26
28 27
29const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" 28const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
30 "$Authors: " LUA_AUTHORS " $"; 29 "$Authors: " LUA_AUTHORS " $";
31 30
32 31
33 32
34static lua_Type normalized_type (const TObject *o) { 33lua_Type luaA_normalizedtype (const TObject *o) {
35 int t = ttype(o); 34 int t = ttype(o);
36 switch (t) { 35 switch (t) {
37 case LUA_T_PMARK: 36 case LUA_T_PMARK:
@@ -46,24 +45,24 @@ static lua_Type normalized_type (const TObject *o) {
46} 45}
47 46
48 47
49static void set_normalized (TObject *d, const TObject *s) { 48void luaA_setnormalized (TObject *d, const TObject *s) {
50 d->value = s->value; 49 d->value = s->value;
51 d->ttype = normalized_type(s); 50 d->ttype = luaA_normalizedtype(s);
52} 51}
53 52
54 53
55static const TObject *luaA_protovalue (const TObject *o) { 54const TObject *luaA_protovalue (const TObject *o) {
56 return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o; 55 return (luaA_normalizedtype(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
57} 56}
58 57
59 58
60static void checkCparams (lua_State *L, int nParams) { 59void luaA_checkCparams (lua_State *L, int nParams) {
61 if (nParams > L->top-L->Cstack.base) 60 if (nParams > L->top-L->Cstack.base)
62 lua_error(L, "API error - wrong number of arguments in C2lua stack"); 61 lua_error(L, "API error - wrong number of arguments in C2lua stack");
63} 62}
64 63
65 64
66static lua_Object put_luaObject (lua_State *L, const TObject *o) { 65lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
67 luaD_openstack(L, L->Cstack.base); 66 luaD_openstack(L, L->Cstack.base);
68 *L->Cstack.base++ = *o; 67 *L->Cstack.base++ = *o;
69 return L->Cstack.base-1; 68 return L->Cstack.base-1;
@@ -86,7 +85,7 @@ static void top2LC (lua_State *L, int n) {
86 85
87 86
88lua_Object lua_pop (lua_State *L) { 87lua_Object lua_pop (lua_State *L) {
89 checkCparams(L, 1); 88 luaA_checkCparams(L, 1);
90 return luaA_putObjectOnTop(L); 89 return luaA_putObjectOnTop(L);
91} 90}
92 91
@@ -106,19 +105,19 @@ int lua_callfunction (lua_State *L, lua_Object function) {
106 return 1; 105 return 1;
107 else { 106 else {
108 luaD_openstack(L, L->Cstack.base); 107 luaD_openstack(L, L->Cstack.base);
109 set_normalized(L->Cstack.base, function); 108 luaA_setnormalized(L->Cstack.base, function);
110 return luaD_protectedrun(L); 109 return luaD_protectedrun(L);
111 } 110 }
112} 111}
113 112
114 113
115lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) { 114lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
116 return put_luaObject(L, luaT_gettagmethod(L, tag, event)); 115 return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
117} 116}
118 117
119 118
120lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) { 119lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
121 checkCparams(L, 1); 120 luaA_checkCparams(L, 1);
122 luaT_settagmethod(L, tag, event, L->top-1); 121 luaT_settagmethod(L, tag, event, L->top-1);
123 return luaA_putObjectOnTop(L); 122 return luaA_putObjectOnTop(L);
124} 123}
@@ -126,7 +125,7 @@ lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
126 125
127lua_Object lua_seterrormethod (lua_State *L) { 126lua_Object lua_seterrormethod (lua_State *L) {
128 lua_Object temp; 127 lua_Object temp;
129 checkCparams(L, 1); 128 luaA_checkCparams(L, 1);
130 temp = lua_getglobal(L, "_ERRORMESSAGE"); 129 temp = lua_getglobal(L, "_ERRORMESSAGE");
131 lua_setglobal(L, "_ERRORMESSAGE"); 130 lua_setglobal(L, "_ERRORMESSAGE");
132 return temp; 131 return temp;
@@ -134,14 +133,14 @@ lua_Object lua_seterrormethod (lua_State *L) {
134 133
135 134
136lua_Object lua_gettable (lua_State *L) { 135lua_Object lua_gettable (lua_State *L) {
137 checkCparams(L, 2); 136 luaA_checkCparams(L, 2);
138 luaV_gettable(L); 137 luaV_gettable(L);
139 return luaA_putObjectOnTop(L); 138 return luaA_putObjectOnTop(L);
140} 139}
141 140
142 141
143lua_Object lua_rawgettable (lua_State *L) { 142lua_Object lua_rawgettable (lua_State *L) {
144 checkCparams(L, 2); 143 luaA_checkCparams(L, 2);
145 if (ttype(L->top-2) != LUA_T_ARRAY) 144 if (ttype(L->top-2) != LUA_T_ARRAY)
146 lua_error(L, "indexed expression not a table in rawgettable"); 145 lua_error(L, "indexed expression not a table in rawgettable");
147 *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1); 146 *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1);
@@ -151,14 +150,14 @@ lua_Object lua_rawgettable (lua_State *L) {
151 150
152 151
153void lua_settable (lua_State *L) { 152void lua_settable (lua_State *L) {
154 checkCparams(L, 3); 153 luaA_checkCparams(L, 3);
155 luaV_settable(L, L->top-3); 154 luaV_settable(L, L->top-3);
156 L->top -= 2; /* pop table and index */ 155 L->top -= 2; /* pop table and index */
157} 156}
158 157
159 158
160void lua_rawsettable (lua_State *L) { 159void lua_rawsettable (lua_State *L) {
161 checkCparams(L, 3); 160 luaA_checkCparams(L, 3);
162 luaV_rawsettable(L, L->top-3); 161 luaV_rawsettable(L, L->top-3);
163} 162}
164 163
@@ -168,12 +167,12 @@ lua_Object lua_createtable (lua_State *L) {
168 luaC_checkGC(L); 167 luaC_checkGC(L);
169 avalue(&o) = luaH_new(L, 0); 168 avalue(&o) = luaH_new(L, 0);
170 ttype(&o) = LUA_T_ARRAY; 169 ttype(&o) = LUA_T_ARRAY;
171 return put_luaObject(L, &o); 170 return luaA_putluaObject(L, &o);
172} 171}
173 172
174 173
175lua_Object lua_getglobal (lua_State *L, const char *name) { 174lua_Object lua_getglobal (lua_State *L, const char *name) {
176 luaD_checkstack(L, 2); /* may need that to call T.M. */ 175 luaD_checkstack(L, 2); /* may need that to call a tag method */
177 luaV_getglobal(L, luaS_assertglobalbyname(L, name)); 176 luaV_getglobal(L, luaS_assertglobalbyname(L, name));
178 return luaA_putObjectOnTop(L); 177 return luaA_putObjectOnTop(L);
179} 178}
@@ -181,27 +180,27 @@ lua_Object lua_getglobal (lua_State *L, const char *name) {
181 180
182lua_Object lua_rawgetglobal (lua_State *L, const char *name) { 181lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
183 GlobalVar *gv = luaS_assertglobalbyname(L, name); 182 GlobalVar *gv = luaS_assertglobalbyname(L, name);
184 return put_luaObject(L, &gv->value); 183 return luaA_putluaObject(L, &gv->value);
185} 184}
186 185
187 186
188void lua_setglobal (lua_State *L, const char *name) { 187void lua_setglobal (lua_State *L, const char *name) {
189 checkCparams(L, 1); 188 luaA_checkCparams(L, 1);
190 luaD_checkstack(L, 2); /* may need that to call T.M. */ 189 luaD_checkstack(L, 2); /* may need that to call a tag method */
191 luaV_setglobal(L, luaS_assertglobalbyname(L, name)); 190 luaV_setglobal(L, luaS_assertglobalbyname(L, name));
192} 191}
193 192
194 193
195void lua_rawsetglobal (lua_State *L, const char *name) { 194void lua_rawsetglobal (lua_State *L, const char *name) {
196 GlobalVar *gv = luaS_assertglobalbyname(L, name); 195 GlobalVar *gv = luaS_assertglobalbyname(L, name);
197 checkCparams(L, 1); 196 luaA_checkCparams(L, 1);
198 gv->value = *(--L->top); 197 gv->value = *(--L->top);
199} 198}
200 199
201 200
202const char *lua_type (lua_State *L, lua_Object o) { 201const char *lua_type (lua_State *L, lua_Object o) {
203 UNUSED(L); 202 UNUSED(L);
204 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, o); 203 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
205} 204}
206 205
207int lua_isnil (lua_State *L, lua_Object o) { 206int lua_isnil (lua_State *L, lua_Object o) {
@@ -220,7 +219,6 @@ int lua_isuserdata (lua_State *L, lua_Object o) {
220} 219}
221 220
222int lua_iscfunction (lua_State *L, lua_Object o) { 221int lua_iscfunction (lua_State *L, lua_Object o) {
223 UNUSED(L);
224 return (lua_tag(L, o) == LUA_T_CPROTO); 222 return (lua_tag(L, o) == LUA_T_CPROTO);
225} 223}
226 224
@@ -261,7 +259,6 @@ const char *lua_getstring (lua_State *L, lua_Object obj) {
261} 259}
262 260
263long lua_strlen (lua_State *L, lua_Object obj) { 261long lua_strlen (lua_State *L, lua_Object obj) {
264 UNUSED(L);
265 if (obj == LUA_NOOBJECT || tostring(L, obj)) 262 if (obj == LUA_NOOBJECT || tostring(L, obj))
266 return 0L; 263 return 0L;
267 else return (tsvalue(obj)->u.s.len); 264 else return (tsvalue(obj)->u.s.len);
@@ -309,7 +306,7 @@ void lua_pushstring (lua_State *L, const char *s) {
309void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 306void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
310 if (fn == NULL) 307 if (fn == NULL)
311 lua_error(L, "API error - attempt to push a NULL Cfunction"); 308 lua_error(L, "API error - attempt to push a NULL Cfunction");
312 checkCparams(L, n); 309 luaA_checkCparams(L, n);
313 ttype(L->top) = LUA_T_CPROTO; 310 ttype(L->top) = LUA_T_CPROTO;
314 fvalue(L->top) = fn; 311 fvalue(L->top) = fn;
315 incr_top; 312 incr_top;
@@ -334,7 +331,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
334void lua_pushobject (lua_State *L, lua_Object o) { 331void lua_pushobject (lua_State *L, lua_Object o) {
335 if (o == LUA_NOOBJECT) 332 if (o == LUA_NOOBJECT)
336 lua_error(L, "API error - attempt to push a NOOBJECT"); 333 lua_error(L, "API error - attempt to push a NOOBJECT");
337 set_normalized(L->top, o); 334 luaA_setnormalized(L->top, o);
338 incr_top; 335 incr_top;
339} 336}
340 337
@@ -368,7 +365,7 @@ int lua_tag (lua_State *L, lua_Object o) {
368 365
369 366
370void lua_settag (lua_State *L, int tag) { 367void lua_settag (lua_State *L, int tag) {
371 checkCparams(L, 1); 368 luaA_checkCparams(L, 1);
372 luaT_realtag(L, tag); 369 luaT_realtag(L, tag);
373 switch (ttype(L->top-1)) { 370 switch (ttype(L->top-1)) {
374 case LUA_T_ARRAY: 371 case LUA_T_ARRAY:
@@ -379,7 +376,7 @@ void lua_settag (lua_State *L, int tag) {
379 break; 376 break;
380 default: 377 default:
381 luaL_verror(L, "cannot change the tag of a %.20s", 378 luaL_verror(L, "cannot change the tag of a %.20s",
382 luaO_typename(L, L->top-1)); 379 luaO_typename(L->top-1));
383 } 380 }
384 L->top--; 381 L->top--;
385} 382}
@@ -442,187 +439,3 @@ int lua_next (lua_State *L, lua_Object t, int i) {
442} 439}
443 440
444 441
445
446/*
447** {======================================================
448** To manipulate some state information
449** =======================================================
450*/
451
452
453
454lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
455 lua_LHFunction old = L->linehook;
456 L->linehook = func;
457 return old;
458}
459
460lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
461 lua_CHFunction old = L->callhook;
462 L->callhook = func;
463 return old;
464}
465
466int lua_setdebug (lua_State *L, int debug) {
467 int old = L->debug;
468 L->debug = debug;
469 return old;
470}
471
472/* }====================================================== */
473
474
475/*
476** {======================================================
477** Debug interface
478** =======================================================
479*/
480
481
482lua_Function lua_stackedfunction (lua_State *L, int level) {
483 int i;
484 for (i = (L->top-1)-L->stack; i>=0; i--) {
485 int t = L->stack[i].ttype;
486 if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
487 if (level-- == 0)
488 return L->stack+i;
489 }
490 return LUA_NOOBJECT;
491}
492
493
494int lua_nups (lua_State *L, lua_Function f) {
495 UNUSED(L);
496 return (!f || normalized_type(f) != LUA_T_CLOSURE) ? 0 : f->value.cl->nelems;
497}
498
499
500int lua_currentline (lua_State *L, lua_Function f) {
501 return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
502}
503
504
505lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
506 const char **name) {
507 /* check whether `f' is a Lua function */
508 if (lua_tag(L, f) != LUA_T_PROTO)
509 return LUA_NOOBJECT;
510 else {
511 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
512 *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
513 if (*name) {
514 /* if "*name", there must be a LUA_T_LINE */
515 /* therefore, f+2 points to function base */
516 return put_luaObject(L, (f+2)+(local_number-1));
517 }
518 else
519 return LUA_NOOBJECT;
520 }
521}
522
523
524int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
525 /* check whether `f' is a Lua function */
526 if (lua_tag(L, f) != LUA_T_PROTO)
527 return 0;
528 else {
529 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
530 const char *name = luaF_getlocalname(fp, local_number,
531 lua_currentline(L, f));
532 checkCparams(L, 1);
533 --L->top;
534 if (name) {
535 /* if "name", there must be a LUA_T_LINE */
536 /* therefore, f+2 points to function base */
537 *((f+2)+(local_number-1)) = *L->top;
538 return 1;
539 }
540 else
541 return 0;
542 }
543}
544
545
546void lua_funcinfo (lua_State *L, lua_Object func,
547 const char **source, int *linedefined) {
548 if (!lua_isfunction(L, func))
549 lua_error(L, "API error - `funcinfo' called with a non-function value");
550 else {
551 const TObject *f = luaA_protovalue(func);
552 if (normalized_type(f) == LUA_T_PROTO) {
553 *source = tfvalue(f)->source->str;
554 *linedefined = tfvalue(f)->lineDefined;
555 }
556 else {
557 *source = "(C)";
558 *linedefined = -1;
559 }
560 }
561}
562
563
564static int checkfunc (lua_State *L, TObject *o) {
565 return luaO_equalObj(o, L->top);
566}
567
568
569const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
570 /* try to find a name for given function */
571 GlobalVar *g;
572 set_normalized(L->top, o); /* to be used by `checkfunc' */
573 for (g=L->rootglobal; g; g=g->next) {
574 if (checkfunc(L, &g->value)) {
575 *name = g->name->str;
576 return "global";
577 }
578 }
579 /* not found: try tag methods */
580 if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
581 return "tag-method";
582 else return ""; /* not found at all */
583}
584
585/* }====================================================== */
586
587
588/*
589** {======================================================
590** BLOCK mechanism
591** =======================================================
592*/
593
594
595void lua_beginblock (lua_State *L) {
596 luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
597 "too many nested blocks", L->stacksize);
598 L->Cblocks[L->numCblocks] = L->Cstack;
599 L->numCblocks++;
600}
601
602void lua_endblock (lua_State *L) {
603 if (L->numCblocks <= 0)
604 lua_error(L, "API error - no block to end");
605 --L->numCblocks;
606 L->Cstack = L->Cblocks[L->numCblocks];
607 L->top = L->Cstack.base;
608}
609
610
611
612int lua_ref (lua_State *L, int lock) {
613 int ref;
614 checkCparams(L, 1);
615 ref = luaR_ref(L, L->top-1, lock);
616 L->top--;
617 return ref;
618}
619
620
621
622lua_Object lua_getref (lua_State *L, int ref) {
623 const TObject *o = luaR_getref(L, ref);
624 return (o ? put_luaObject(L, o) : LUA_NOOBJECT);
625}
626
627/* }====================================================== */
628
diff --git a/lapi.h b/lapi.h
index 96969134..6a69dd15 100644
--- a/lapi.h
+++ b/lapi.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.h,v 1.9 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lapi.h,v 1.10 1999/12/02 16:24:45 roberto Exp roberto $
3** Auxiliary functions from Lua API 3** Auxiliary functions from Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -11,9 +11,14 @@
11#include "lobject.h" 11#include "lobject.h"
12 12
13 13
14lua_Type luaA_normalizedtype (const TObject *o);
15void luaA_setnormalized (TObject *d, const TObject *s);
16void luaA_checkCparams (lua_State *L, int nParams);
17const TObject *luaA_protovalue (const TObject *o);
14void luaA_pushobject (lua_State *L, const TObject *o); 18void luaA_pushobject (lua_State *L, const TObject *o);
15GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g); 19GlobalVar *luaA_nextvar (lua_State *L, TaggedString *g);
16int luaA_next (lua_State *L, const Hash *t, int i); 20int luaA_next (lua_State *L, const Hash *t, int i);
21lua_Object luaA_putluaObject (lua_State *L, const TObject *o);
17lua_Object luaA_putObjectOnTop (lua_State *L); 22lua_Object luaA_putObjectOnTop (lua_State *L);
18 23
19#endif 24#endif
diff --git a/lbuiltin.c b/lbuiltin.c
index 77e1bc56..a28db531 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.82 1999/12/06 11:42:18 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.83 1999/12/07 12:05:34 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*/
@@ -29,6 +29,17 @@
29 29
30 30
31 31
32#ifdef DEBUG
33/*
34** function defined in ltests.c, to open some internal-test functions
35*/
36void luaB_opentests (lua_State *L);
37#else
38#define luaB_opentests(L) /* do nothing */
39#endif
40
41
42
32/* 43/*
33** {====================================================== 44** {======================================================
34** Auxiliary functions 45** Auxiliary functions
@@ -87,7 +98,7 @@ static Hash *gettable (lua_State *L, int arg) {
87** If your system does not support "stderr", redefine this function, or 98** If your system does not support "stderr", redefine this function, or
88** redefine _ERRORMESSAGE so that it won't need _ALERT. 99** redefine _ERRORMESSAGE so that it won't need _ALERT.
89*/ 100*/
90static void luaB_alert (lua_State *L) { 101void luaB_alert (lua_State *L) {
91 fputs(luaL_check_string(L, 1), stderr); 102 fputs(luaL_check_string(L, 1), stderr);
92} 103}
93 104
@@ -96,7 +107,7 @@ static void luaB_alert (lua_State *L) {
96** Standard implementation of _ERRORMESSAGE. 107** Standard implementation of _ERRORMESSAGE.
97** The library "iolib" redefines _ERRORMESSAGE for better error information. 108** The library "iolib" redefines _ERRORMESSAGE for better error information.
98*/ 109*/
99static void error_message (lua_State *L) { 110void luaB_ERRORMESSAGE (lua_State *L) {
100 lua_Object al = lua_rawgetglobal(L, "_ALERT"); 111 lua_Object al = lua_rawgetglobal(L, "_ALERT");
101 if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ 112 if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */
102 char buff[600]; 113 char buff[600];
@@ -117,7 +128,7 @@ static void error_message (lua_State *L) {
117#define MAXPRINT 40 /* arbitrary limit */ 128#define MAXPRINT 40 /* arbitrary limit */
118#endif 129#endif
119 130
120static void luaB_print (lua_State *L) { 131void luaB_print (lua_State *L) {
121 lua_Object args[MAXPRINT]; 132 lua_Object args[MAXPRINT];
122 lua_Object obj; 133 lua_Object obj;
123 int n = 0; 134 int n = 0;
@@ -140,7 +151,7 @@ static void luaB_print (lua_State *L) {
140} 151}
141 152
142 153
143static void luaB_tonumber (lua_State *L) { 154void luaB_tonumber (lua_State *L) {
144 int base = luaL_opt_int(L, 2, 10); 155 int base = luaL_opt_int(L, 2, 10);
145 if (base == 10) { /* standard conversion */ 156 if (base == 10) { /* standard conversion */
146 lua_Object o = lua_getparam(L, 1); 157 lua_Object o = lua_getparam(L, 1);
@@ -161,66 +172,66 @@ static void luaB_tonumber (lua_State *L) {
161} 172}
162 173
163 174
164static void luaB_error (lua_State *L) { 175void luaB_error (lua_State *L) {
165 lua_error(L, lua_getstring(L, lua_getparam(L, 1))); 176 lua_error(L, lua_getstring(L, lua_getparam(L, 1)));
166} 177}
167 178
168static void luaB_setglobal (lua_State *L) { 179void luaB_setglobal (lua_State *L) {
169 const char *n = luaL_check_string(L, 1); 180 const char *n = luaL_check_string(L, 1);
170 lua_Object value = luaL_nonnullarg(L, 2); 181 lua_Object value = luaL_nonnullarg(L, 2);
171 lua_pushobject(L, value); 182 lua_pushobject(L, value);
172 lua_setglobal(L, n); 183 lua_setglobal(L, n);
173} 184}
174 185
175static void luaB_rawsetglobal (lua_State *L) { 186void luaB_rawsetglobal (lua_State *L) {
176 const char *n = luaL_check_string(L, 1); 187 const char *n = luaL_check_string(L, 1);
177 lua_Object value = luaL_nonnullarg(L, 2); 188 lua_Object value = luaL_nonnullarg(L, 2);
178 lua_pushobject(L, value); 189 lua_pushobject(L, value);
179 lua_rawsetglobal(L, n); 190 lua_rawsetglobal(L, n);
180} 191}
181 192
182static void luaB_getglobal (lua_State *L) { 193void luaB_getglobal (lua_State *L) {
183 lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); 194 lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1)));
184} 195}
185 196
186static void luaB_rawgetglobal (lua_State *L) { 197void luaB_rawgetglobal (lua_State *L) {
187 lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); 198 lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1)));
188} 199}
189 200
190static void luaB_tag (lua_State *L) { 201void luaB_tag (lua_State *L) {
191 lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1))); 202 lua_pushnumber(L, lua_tag(L, lua_getparam(L, 1)));
192} 203}
193 204
194static void luaB_settag (lua_State *L) { 205void luaB_settag (lua_State *L) {
195 lua_Object o = luaL_tablearg(L, 1); 206 lua_Object o = luaL_tablearg(L, 1);
196 lua_pushobject(L, o); 207 lua_pushobject(L, o);
197 lua_settag(L, luaL_check_int(L, 2)); 208 lua_settag(L, luaL_check_int(L, 2));
198 lua_pushobject(L, o); /* return first argument */ 209 lua_pushobject(L, o); /* return first argument */
199} 210}
200 211
201static void luaB_newtag (lua_State *L) { 212void luaB_newtag (lua_State *L) {
202 lua_pushnumber(L, lua_newtag(L)); 213 lua_pushnumber(L, lua_newtag(L));
203} 214}
204 215
205static void luaB_copytagmethods (lua_State *L) { 216void luaB_copytagmethods (lua_State *L) {
206 lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), 217 lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
207 luaL_check_int(L, 2))); 218 luaL_check_int(L, 2)));
208} 219}
209 220
210static void luaB_rawgettable (lua_State *L) { 221void luaB_rawgettable (lua_State *L) {
211 lua_pushobject(L, luaL_nonnullarg(L, 1)); 222 lua_pushobject(L, luaL_nonnullarg(L, 1));
212 lua_pushobject(L, luaL_nonnullarg(L, 2)); 223 lua_pushobject(L, luaL_nonnullarg(L, 2));
213 lua_pushobject(L, lua_rawgettable(L)); 224 lua_pushobject(L, lua_rawgettable(L));
214} 225}
215 226
216static void luaB_rawsettable (lua_State *L) { 227void luaB_rawsettable (lua_State *L) {
217 lua_pushobject(L, luaL_nonnullarg(L, 1)); 228 lua_pushobject(L, luaL_nonnullarg(L, 1));
218 lua_pushobject(L, luaL_nonnullarg(L, 2)); 229 lua_pushobject(L, luaL_nonnullarg(L, 2));
219 lua_pushobject(L, luaL_nonnullarg(L, 3)); 230 lua_pushobject(L, luaL_nonnullarg(L, 3));
220 lua_rawsettable(L); 231 lua_rawsettable(L);
221} 232}
222 233
223static void luaB_settagmethod (lua_State *L) { 234void luaB_settagmethod (lua_State *L) {
224 int tag = luaL_check_int(L, 1); 235 int tag = luaL_check_int(L, 1);
225 const char *event = luaL_check_string(L, 2); 236 const char *event = luaL_check_string(L, 2);
226 lua_Object nf = luaL_nonnullarg(L, 3); 237 lua_Object nf = luaL_nonnullarg(L, 3);
@@ -232,23 +243,23 @@ static void luaB_settagmethod (lua_State *L) {
232 lua_pushobject(L, lua_settagmethod(L, tag, event)); 243 lua_pushobject(L, lua_settagmethod(L, tag, event));
233} 244}
234 245
235static void luaB_gettagmethod (lua_State *L) { 246void luaB_gettagmethod (lua_State *L) {
236 lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1), 247 lua_pushobject(L, lua_gettagmethod(L, luaL_check_int(L, 1),
237 luaL_check_string(L, 2))); 248 luaL_check_string(L, 2)));
238} 249}
239 250
240static void luaB_seterrormethod (lua_State *L) { 251void luaB_seterrormethod (lua_State *L) {
241 lua_Object nf = luaL_functionarg(L, 1); 252 lua_Object nf = luaL_functionarg(L, 1);
242 lua_pushobject(L, nf); 253 lua_pushobject(L, nf);
243 lua_pushobject(L, lua_seterrormethod(L)); 254 lua_pushobject(L, lua_seterrormethod(L));
244} 255}
245 256
246static void luaB_collectgarbage (lua_State *L) { 257void luaB_collectgarbage (lua_State *L) {
247 lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0))); 258 lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
248} 259}
249 260
250 261
251static void luaB_type (lua_State *L) { 262void luaB_type (lua_State *L) {
252 lua_Object o = luaL_nonnullarg(L, 1); 263 lua_Object o = luaL_nonnullarg(L, 1);
253 lua_pushstring(L, lua_type(L, o)); 264 lua_pushstring(L, lua_type(L, o));
254} 265}
@@ -270,7 +281,7 @@ static void passresults (lua_State *L) {
270 lua_pushuserdata(L, NULL); /* at least one result to signal no errors */ 281 lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
271} 282}
272 283
273static void luaB_dostring (lua_State *L) { 284void luaB_dostring (lua_State *L) {
274 long l; 285 long l;
275 const char *s = luaL_check_lstr(L, 1, &l); 286 const char *s = luaL_check_lstr(L, 1, &l);
276 if (*s == ID_CHUNK) 287 if (*s == ID_CHUNK)
@@ -281,7 +292,7 @@ static void luaB_dostring (lua_State *L) {
281} 292}
282 293
283 294
284static void luaB_dofile (lua_State *L) { 295void luaB_dofile (lua_State *L) {
285 const char *fname = luaL_opt_string(L, 1, NULL); 296 const char *fname = luaL_opt_string(L, 1, NULL);
286 if (lua_dofile(L, fname) == 0) 297 if (lua_dofile(L, fname) == 0)
287 passresults(L); 298 passresults(L);
@@ -289,7 +300,7 @@ static void luaB_dofile (lua_State *L) {
289} 300}
290 301
291 302
292static void luaB_call (lua_State *L) { 303void luaB_call (lua_State *L) {
293 lua_Object f = luaL_nonnullarg(L, 1); 304 lua_Object f = luaL_nonnullarg(L, 1);
294 const Hash *arg = gettable(L, 2); 305 const Hash *arg = gettable(L, 2);
295 const char *options = luaL_opt_string(L, 3, ""); 306 const char *options = luaL_opt_string(L, 3, "");
@@ -328,7 +339,7 @@ static void luaB_call (lua_State *L) {
328} 339}
329 340
330 341
331static void luaB_nextvar (lua_State *L) { 342void luaB_nextvar (lua_State *L) {
332 lua_Object o = luaL_nonnullarg(L, 1); 343 lua_Object o = luaL_nonnullarg(L, 1);
333 TaggedString *g; 344 TaggedString *g;
334 if (ttype(o) == LUA_T_NIL) 345 if (ttype(o) == LUA_T_NIL)
@@ -342,7 +353,7 @@ static void luaB_nextvar (lua_State *L) {
342} 353}
343 354
344 355
345static void luaB_next (lua_State *L) { 356void luaB_next (lua_State *L) {
346 const Hash *a = gettable(L, 1); 357 const Hash *a = gettable(L, 1);
347 lua_Object k = luaL_nonnullarg(L, 2); 358 lua_Object k = luaL_nonnullarg(L, 2);
348 int i; /* will get first element after `i' */ 359 int i; /* will get first element after `i' */
@@ -357,7 +368,7 @@ static void luaB_next (lua_State *L) {
357} 368}
358 369
359 370
360static void luaB_tostring (lua_State *L) { 371void luaB_tostring (lua_State *L) {
361 lua_Object o = lua_getparam(L, 1); 372 lua_Object o = lua_getparam(L, 1);
362 char buff[64]; 373 char buff[64];
363 switch (ttype(o)) { 374 switch (ttype(o)) {
@@ -405,14 +416,14 @@ static void luaB_tostring (lua_State *L) {
405** ======================================================= 416** =======================================================
406*/ 417*/
407 418
408static void luaB_assert (lua_State *L) { 419void luaB_assert (lua_State *L) {
409 lua_Object p = lua_getparam(L, 1); 420 lua_Object p = lua_getparam(L, 1);
410 if (p == LUA_NOOBJECT || lua_isnil(L, p)) 421 if (p == LUA_NOOBJECT || lua_isnil(L, p))
411 luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); 422 luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
412} 423}
413 424
414 425
415static void luaB_foreachi (lua_State *L) { 426void luaB_foreachi (lua_State *L) {
416 const Hash *t = gettable(L, 1); 427 const Hash *t = gettable(L, 1);
417 int n = (int)getnarg(L, t); 428 int n = (int)getnarg(L, t);
418 int i; 429 int i;
@@ -430,7 +441,7 @@ static void luaB_foreachi (lua_State *L) {
430} 441}
431 442
432 443
433static void luaB_foreach (lua_State *L) { 444void luaB_foreach (lua_State *L) {
434 const Hash *a = gettable(L, 1); 445 const Hash *a = gettable(L, 1);
435 lua_Object f = luaL_functionarg(L, 2); 446 lua_Object f = luaL_functionarg(L, 2);
436 int i; 447 int i;
@@ -450,7 +461,7 @@ static void luaB_foreach (lua_State *L) {
450} 461}
451 462
452 463
453static void luaB_foreachvar (lua_State *L) { 464void luaB_foreachvar (lua_State *L) {
454 lua_Object f = luaL_functionarg(L, 1); 465 lua_Object f = luaL_functionarg(L, 1);
455 GlobalVar *gv; 466 GlobalVar *gv;
456 luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ 467 luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */
@@ -472,12 +483,12 @@ static void luaB_foreachvar (lua_State *L) {
472} 483}
473 484
474 485
475static void luaB_getn (lua_State *L) { 486void luaB_getn (lua_State *L) {
476 lua_pushnumber(L, getnarg(L, gettable(L, 1))); 487 lua_pushnumber(L, getnarg(L, gettable(L, 1)));
477} 488}
478 489
479 490
480static void luaB_tinsert (lua_State *L) { 491void luaB_tinsert (lua_State *L) {
481 Hash *a = gettable(L, 1); 492 Hash *a = gettable(L, 1);
482 lua_Object v = lua_getparam(L, 3); 493 lua_Object v = lua_getparam(L, 3);
483 int n = (int)getnarg(L, a); 494 int n = (int)getnarg(L, a);
@@ -495,7 +506,7 @@ static void luaB_tinsert (lua_State *L) {
495} 506}
496 507
497 508
498static void luaB_tremove (lua_State *L) { 509void luaB_tremove (lua_State *L) {
499 Hash *a = gettable(L, 1); 510 Hash *a = gettable(L, 1);
500 int n = (int)getnarg(L, a); 511 int n = (int)getnarg(L, a);
501 int pos = luaL_opt_int(L, 2, n); 512 int pos = luaL_opt_int(L, 2, n);
@@ -583,7 +594,7 @@ static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
583 L->top--; /* remove pivot from stack */ 594 L->top--; /* remove pivot from stack */
584} 595}
585 596
586static void luaB_sort (lua_State *L) { 597void luaB_sort (lua_State *L) {
587 lua_Object t = lua_getparam(L, 1); 598 lua_Object t = lua_getparam(L, 1);
588 Hash *a = gettable(L, 1); 599 Hash *a = gettable(L, 1);
589 int n = (int)getnarg(L, a); 600 int n = (int)getnarg(L, a);
@@ -601,255 +612,9 @@ static void luaB_sort (lua_State *L) {
601/* }====================================================== */ 612/* }====================================================== */
602 613
603 614
604
605#ifdef DEBUG
606/*
607** {======================================================
608** some DEBUG functions
609** (for internal debugging of the Lua implementation)
610** =======================================================
611*/
612
613static void mem_query (lua_State *L) {
614 lua_pushnumber(L, totalmem);
615 lua_pushnumber(L, numblocks);
616}
617
618
619static void hash_query (lua_State *L) {
620 lua_Object o = luaL_nonnullarg(L, 1);
621 if (lua_getparam(L, 2) == LUA_NOOBJECT) {
622 luaL_arg_check(L, ttype(o) == LUA_T_STRING, 1, "string expected");
623 lua_pushnumber(L, tsvalue(o)->hash);
624 }
625 else {
626 const Hash *t = avalue(luaL_tablearg(L, 2));
627 lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
628 }
629}
630
631
632static void table_query (lua_State *L) {
633 const Hash *t = avalue(luaL_tablearg(L, 1));
634 int i = luaL_opt_int(L, 2, -1);
635 if (i == -1) {
636 lua_pushnumber(L, t->size);
637 lua_pushnumber(L, t->firstfree - t->node);
638 }
639 else if (i < t->size) {
640 luaA_pushobject(L, &t->node[i].key);
641 luaA_pushobject(L, &t->node[i].val);
642 if (t->node[i].next)
643 lua_pushnumber(L, t->node[i].next - t->node);
644 }
645}
646
647
648static void query_strings (lua_State *L) {
649 int h = luaL_check_int(L, 1) - 1;
650 int s = luaL_opt_int(L, 2, 0) - 1;
651 if (s==-1) {
652 if (h < NUM_HASHS) {
653 lua_pushnumber(L, L->string_root[h].nuse);
654 lua_pushnumber(L, L->string_root[h].size);
655 }
656 }
657 else {
658 TaggedString *ts = L->string_root[h].hash[s];
659 for (ts = L->string_root[h].hash[s]; ts; ts = ts->nexthash) {
660 if (ts->constindex == -1) lua_pushstring(L, "<USERDATA>");
661 else lua_pushstring(L, ts->str);
662 }
663 }
664}
665
666
667static const char *delimits = " \t\n,;";
668
669static void skip (const char **pc) {
670 while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
671}
672
673static int getnum (const char **pc) {
674 int res = 0;
675 skip(pc);
676 while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
677 return res;
678}
679
680static int getreg (lua_State *L, const char **pc) {
681 skip(pc);
682 if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
683 return getnum(pc);
684}
685
686static const char *getname (const char **pc) {
687 static char buff[30];
688 int i = 0;
689 skip(pc);
690 while (**pc != '\0' && !strchr(delimits, **pc))
691 buff[i++] = *(*pc)++;
692 buff[i] = '\0';
693 return buff;
694}
695
696
697#define EQ(s1) (strcmp(s1, inst) == 0)
698
699static void testC (lua_State *L) {
700 lua_Object reg[10];
701 const char *pc = luaL_check_string(L, 1);
702 for (;;) {
703 const char *inst = getname(&pc);
704 if EQ("") return;
705 else if EQ("pushnum") {
706 lua_pushnumber(L, getnum(&pc));
707 }
708 else if EQ("createtable") {
709 reg[getreg(L, &pc)] = lua_createtable(L);
710 }
711 else if EQ("closure") {
712 lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
713 lua_pushcclosure(L, f, getnum(&pc));
714 }
715 else if EQ("pop") {
716 reg[getreg(L, &pc)] = lua_pop(L);
717 }
718 else if EQ("getglobal") {
719 int n = getreg(L, &pc);
720 reg[n] = lua_getglobal(L, getname(&pc));
721 }
722 else if EQ("rawgetglobal") {
723 int n = getreg(L, &pc);
724 reg[n] = lua_rawgetglobal(L, getname(&pc));
725 }
726 else if EQ("ref") {
727 lua_pushnumber(L, lua_ref(L, 0));
728 reg[getreg(L, &pc)] = lua_pop(L);
729 }
730 else if EQ("reflock") {
731 lua_pushnumber(L, lua_ref(L, 1));
732 reg[getreg(L, &pc)] = lua_pop(L);
733 }
734 else if EQ("getref") {
735 int n = getreg(L, &pc);
736 reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
737 }
738 else if EQ("unref") {
739 lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
740 }
741 else if EQ("getparam") {
742 int n = getreg(L, &pc);
743 reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the commmand itself */
744 }
745 else if EQ("getresult") {
746 int n = getreg(L, &pc);
747 reg[n] = lua_getparam(L, getnum(&pc));
748 }
749 else if EQ("setglobal") {
750 lua_setglobal(L, getname(&pc));
751 }
752 else if EQ("rawsetglobal") {
753 lua_rawsetglobal(L, getname(&pc));
754 }
755 else if EQ("pushstring") {
756 lua_pushstring(L, getname(&pc));
757 }
758 else if EQ("pushreg") {
759 lua_pushobject(L, reg[getreg(L, &pc)]);
760 }
761 else if EQ("call") {
762 lua_call(L, getname(&pc));
763 }
764 else if EQ("gettable") {
765 reg[getreg(L, &pc)] = lua_gettable(L);
766 }
767 else if EQ("rawgettable") {
768 reg[getreg(L, &pc)] = lua_rawgettable(L);
769 }
770 else if EQ("settable") {
771 lua_settable(L);
772 }
773 else if EQ("rawsettable") {
774 lua_rawsettable(L);
775 }
776 else if EQ("nextvar") {
777 lua_pushstring(L, lua_nextvar(L, lua_getstring(L, reg[getreg(L, &pc)])));
778 }
779 else if EQ("next") {
780 int n = getreg(L, &pc);
781 n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
782 lua_pushnumber(L, n);
783 }
784 else if EQ("equal") {
785 int n1 = getreg(L, &pc);
786 int n2 = getreg(L, &pc);
787 lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
788 }
789 else if EQ("pushusertag") {
790 int val = getreg(L, &pc);
791 int tag = getreg(L, &pc);
792 lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
793 (int)lua_getnumber(L, reg[tag]));
794 }
795 else if EQ("udataval") {
796 int n = getreg(L, &pc);
797 lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
798 reg[n] = lua_pop(L);
799 }
800 else if EQ("settagmethod") {
801 int n = getreg(L, &pc);
802 lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
803 }
804 else if EQ("beginblock") {
805 lua_beginblock(L);
806 }
807 else if EQ("endblock") {
808 lua_endblock(L);
809 }
810 else if EQ("newstate") {
811 int stacksize = getnum(&pc);
812 lua_State *L1 = lua_newstate("stack", stacksize,
813 "builtin", getnum(&pc), NULL);
814 lua_pushuserdata(L, L1);
815 }
816 else if EQ("closestate") {
817 lua_close(lua_getuserdata(L, reg[getreg(L, &pc)]));
818 }
819 else if EQ("doremote") {
820 lua_Object ol1 = reg[getreg(L, &pc)];
821 lua_Object str = reg[getreg(L, &pc)];
822 lua_State *L1;
823 lua_Object temp;
824 int i;
825 if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
826 lua_error(L, "bad arguments for `doremote'");
827 L1 = lua_getuserdata(L, ol1);
828 lua_dostring(L1, lua_getstring(L, str));
829 i = 1;
830 while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
831 lua_pushstring(L, lua_getstring(L1, temp));
832 }
833 else luaL_verror(L, "unknown command in `testC': %.20s", inst);
834 }
835}
836
837
838/* }====================================================== */
839#endif
840
841
842
843static const struct luaL_reg builtin_funcs[] = { 615static const struct luaL_reg builtin_funcs[] = {
844#ifdef DEBUG
845 {"hash", hash_query},
846 {"querystr", query_strings},
847 {"querytab", table_query},
848 {"testC", testC},
849 {"totalmem", mem_query},
850#endif
851 {"_ALERT", luaB_alert}, 616 {"_ALERT", luaB_alert},
852 {"_ERRORMESSAGE", error_message}, 617 {"_ERRORMESSAGE", luaB_ERRORMESSAGE},
853 {"call", luaB_call}, 618 {"call", luaB_call},
854 {"collectgarbage", luaB_collectgarbage}, 619 {"collectgarbage", luaB_collectgarbage},
855 {"copytagmethods", luaB_copytagmethods}, 620 {"copytagmethods", luaB_copytagmethods},
@@ -891,6 +656,7 @@ void luaB_predefine (lua_State *L) {
891 luaS_newfixedstring(L, tableEM); 656 luaS_newfixedstring(L, tableEM);
892 luaS_newfixedstring(L, memEM); 657 luaS_newfixedstring(L, memEM);
893 luaL_openl(L, builtin_funcs); 658 luaL_openl(L, builtin_funcs);
659 luaB_opentests(L); /* internal test functions (when DEBUG is on) */
894 lua_pushstring(L, LUA_VERSION); 660 lua_pushstring(L, LUA_VERSION);
895 lua_setglobal(L, "_VERSION"); 661 lua_setglobal(L, "_VERSION");
896} 662}
diff --git a/lbuiltin.h b/lbuiltin.h
index 6a4915d0..0e42c138 100644
--- a/lbuiltin.h
+++ b/lbuiltin.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lbuiltin.h,v 1.2 1999/11/22 13:12:07 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*/
@@ -9,6 +9,42 @@
9 9
10#include "lua.h" 10#include "lua.h"
11 11
12void luaB_alert (lua_State *L);
13void luaB_ERRORMESSAGE (lua_State *L);
14void luaB_print (lua_State *L);
15void luaB_tonumber (lua_State *L);
16void luaB_error (lua_State *L);
17void luaB_setglobal (lua_State *L);
18void luaB_rawsetglobal (lua_State *L);
19void luaB_getglobal (lua_State *L);
20void luaB_rawgetglobal (lua_State *L);
21void luaB_tag (lua_State *L);
22void luaB_settag (lua_State *L);
23void luaB_newtag (lua_State *L);
24void luaB_copytagmethods (lua_State *L);
25void luaB_rawgettable (lua_State *L);
26void luaB_rawsettable (lua_State *L);
27void luaB_settagmethod (lua_State *L);
28void luaB_gettagmethod (lua_State *L);
29void luaB_seterrormethod (lua_State *L);
30void luaB_collectgarbage (lua_State *L);
31void luaB_type (lua_State *L);
32void luaB_dostring (lua_State *L);
33void luaB_dofile (lua_State *L);
34void luaB_call (lua_State *L);
35void luaB_nextvar (lua_State *L);
36void luaB_next (lua_State *L);
37void luaB_tostring (lua_State *L);
38void luaB_assert (lua_State *L);
39void luaB_foreachi (lua_State *L);
40void luaB_foreach (lua_State *L);
41void luaB_foreachvar (lua_State *L);
42void luaB_getn (lua_State *L);
43void luaB_tinsert (lua_State *L);
44void luaB_tremove (lua_State *L);
45void luaB_sort (lua_State *L);
46
47
12void luaB_predefine (lua_State *L); 48void luaB_predefine (lua_State *L);
13 49
14 50
diff --git a/lgc.c b/lgc.c
index 14ef9539..ceade0a4 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.34 1999/11/26 18:59:20 roberto Exp roberto $ 2** $Id: lgc.c,v 1.35 1999/12/01 19:50:08 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*/
@@ -23,7 +23,7 @@
23static int markobject (lua_State *L, TObject *o); 23static int markobject (lua_State *L, TObject *o);
24 24
25 25
26/* mark a string; marks bigger than 1 cannot be changed */ 26/* mark a string; marks larger than 1 cannot be changed */
27#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;} 27#define strmark(L, s) {if ((s)->marked == 0) (s)->marked = 1;}
28 28
29 29
diff --git a/llex.c b/llex.c
index c9250706..13fe627f 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.44 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: llex.c,v 1.45 1999/12/02 16:41:29 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*/
@@ -64,7 +64,7 @@ void luaX_error (LexState *ls, const char *s) {
64 64
65 65
66void luaX_token2str (int token, char *s) { 66void luaX_token2str (int token, char *s) {
67 if (token < 255) { 67 if (token < 256) {
68 s[0] = (char)token; 68 s[0] = (char)token;
69 s[1] = '\0'; 69 s[1] = '\0';
70 } 70 }
@@ -225,7 +225,7 @@ static void inclinenumber (lua_State *L, LexState *LS) {
225 225
226/* 226/*
227** ======================================================= 227** =======================================================
228** LEXICAL ANALIZER 228** LEXICAL ANALYZER
229** ======================================================= 229** =======================================================
230*/ 230*/
231 231
@@ -407,7 +407,7 @@ int luaX_lex (LexState *LS) {
407 } 407 }
408 save(L, '\0'); 408 save(L, '\0');
409 if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r)) 409 if (!luaO_str2d(L->Mbuffer+L->Mbuffbase, &LS->seminfo.r))
410 luaX_error(LS, "invalid numeric format"); 410 luaX_error(LS, "malformed number");
411 return NUMBER; 411 return NUMBER;
412 412
413 case EOZ: 413 case EOZ:
diff --git a/lmathlib.c b/lmathlib.c
index 83c8e0bc..6fc9f78f 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.21 1999/11/22 17:39:51 roberto Exp roberto $
3** Lua standard mathematical library 3** Lua standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -144,7 +144,7 @@ static void math_max (lua_State *L) {
144 144
145static void math_random (lua_State *L) { 145static void math_random (lua_State *L) {
146 /* the '%' avoids the (rare) case of r==1, and is needed also because on 146 /* the '%' avoids the (rare) case of r==1, and is needed also because on
147 some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ 147 some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
148 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; 148 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
149 if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */ 149 if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */
150 lua_pushnumber(L, r); /* real between 0 & 1 */ 150 lua_pushnumber(L, r); /* real between 0 & 1 */
diff --git a/lmem.c b/lmem.c
index 7caa0f3e..47c45156 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.20 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lmem.c,v 1.21 1999/11/29 16:38:48 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*/
@@ -31,10 +31,10 @@ void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
31 int inc, int size, const char *errormsg, unsigned long limit) { 31 int inc, int size, const char *errormsg, unsigned long limit) {
32 unsigned long newn = nelems+inc; 32 unsigned long newn = nelems+inc;
33 if (newn >= limit) lua_error(L, errormsg); 33 if (newn >= limit) lua_error(L, errormsg);
34 if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */ 34 if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
35 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */ 35 (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
36 return block; /* do not need to reallocate */ 36 return block; /* do not need to reallocate */
37 else /* it crossed a power of 2 boundary; grow to next power */ 37 else /* it crossed a power-of-2 boundary; grow to next power */
38 return luaM_realloc(L, block, luaO_power2(newn)*size); 38 return luaM_realloc(L, block, luaO_power2(newn)*size);
39} 39}
40 40
diff --git a/lobject.c b/lobject.c
index e2845127..b8b9247e 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.25 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lobject.c,v 1.26 1999/11/26 18:59:20 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*/
@@ -23,7 +23,7 @@ const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
23 23
24 24
25/* 25/*
26** returns smaller power of 2 bigger 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) { 28unsigned long luaO_power2 (unsigned long n) {
29 unsigned long p = MINPOWER2; 29 unsigned long p = MINPOWER2;
diff --git a/lobject.h b/lobject.h
index d644d2ea..8c6b63f1 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.38 1999/11/26 18:59:20 roberto Exp roberto $ 2** $Id: lobject.h,v 1.39 1999/12/02 16:24:45 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*/
@@ -197,7 +197,7 @@ typedef struct Hash {
197 197
198extern const char *const luaO_typenames[]; 198extern const char *const luaO_typenames[];
199 199
200#define luaO_typename(L, o) luaO_typenames[-ttype(o)] 200#define luaO_typename(o) luaO_typenames[-ttype(o)]
201 201
202#define MINPOWER2 4 /* minimum size for "growing" vectors */ 202#define MINPOWER2 4 /* minimum size for "growing" vectors */
203 203
diff --git a/lref.c b/lref.c
index caaae400..c2e90d38 100644
--- a/lref.c
+++ b/lref.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lref.c,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $ 2** $Id: lref.c,v 1.3 1999/11/22 13:12:07 roberto Exp roberto $
3** REF mechanism 3** REF mechanism
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -7,15 +7,17 @@
7 7
8#define LUA_REENTRANT 8#define LUA_REENTRANT
9 9
10#include "lapi.h"
10#include "lmem.h" 11#include "lmem.h"
11#include "lref.h" 12#include "lref.h"
12#include "lstate.h" 13#include "lstate.h"
13#include "lua.h" 14#include "lua.h"
14 15
15 16
16int luaR_ref (lua_State *L, const TObject *o, int lock) { 17int lua_ref (lua_State *L, int lock) {
17 int ref; 18 int ref;
18 if (ttype(o) == LUA_T_NIL) 19 luaA_checkCparams(L, 1);
20 if (ttype(L->top-1) == LUA_T_NIL)
19 ref = LUA_REFNIL; 21 ref = LUA_REFNIL;
20 else { 22 else {
21 if (L->refFree != NONEXT) { /* is there a free place? */ 23 if (L->refFree != NONEXT) { /* is there a free place? */
@@ -26,9 +28,10 @@ int luaR_ref (lua_State *L, const TObject *o, int lock) {
26 luaM_growvector(L, L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT); 28 luaM_growvector(L, L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
27 ref = L->refSize++; 29 ref = L->refSize++;
28 } 30 }
29 L->refArray[ref].o = *o; 31 L->refArray[ref].o = *(L->top-1);
30 L->refArray[ref].st = lock ? LOCK : HOLD; 32 L->refArray[ref].st = lock ? LOCK : HOLD;
31 } 33 }
34 L->top--;
32 return ref; 35 return ref;
33} 36}
34 37
@@ -43,17 +46,38 @@ void lua_unref (lua_State *L, int ref) {
43} 46}
44 47
45 48
46const TObject *luaR_getref (lua_State *L, int ref) { 49lua_Object lua_getref (lua_State *L, int ref) {
47 if (ref == LUA_REFNIL) 50 if (ref == LUA_REFNIL)
48 return &luaO_nilobject; 51 return luaA_putluaObject(L, &luaO_nilobject);
49 else if (0 <= ref && ref < L->refSize && 52 else if (0 <= ref && ref < L->refSize &&
50 (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) 53 (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
51 return &L->refArray[ref].o; 54 return luaA_putluaObject(L, &L->refArray[ref].o);
52 else 55 else
53 return NULL; 56 return LUA_NOOBJECT;
54} 57}
55 58
56 59
60void lua_beginblock (lua_State *L) {
61 luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
62 "too many nested blocks", L->stacksize);
63 L->Cblocks[L->numCblocks] = L->Cstack;
64 L->numCblocks++;
65}
66
67
68void lua_endblock (lua_State *L) {
69 if (L->numCblocks <= 0)
70 lua_error(L, "API error - no block to end");
71 --L->numCblocks;
72 L->Cstack = L->Cblocks[L->numCblocks];
73 L->top = L->Cstack.base;
74}
75
76
77
78
79
80
57static int ismarked (const TObject *o) { 81static int ismarked (const TObject *o) {
58 /* valid only for locked objects */ 82 /* valid only for locked objects */
59 switch (o->ttype) { 83 switch (o->ttype) {
diff --git a/lref.h b/lref.h
index aff9e185..af4a1431 100644
--- a/lref.h
+++ b/lref.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lref.h,v 1.2 1999/11/10 15:37:50 roberto Exp roberto $ 2** $Id: lref.h,v 1.3 1999/11/22 13:12:07 roberto Exp roberto $
3** REF mechanism 3** REF mechanism
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,9 +22,6 @@ struct ref {
22}; 22};
23 23
24 24
25int luaR_ref (lua_State *L, const TObject *o, int lock);
26const TObject *luaR_getref (lua_State *L, int ref);
27void luaR_invalidaterefs (lua_State *L); 25void luaR_invalidaterefs (lua_State *L);
28 26
29
30#endif 27#endif
diff --git a/lvm.c b/lvm.c
index 4b05c894..026b089a 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.71 1999/12/06 19:30:53 roberto Exp roberto $ 2** $Id: lvm.c,v 1.72 1999/12/09 20:01:48 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*/
@@ -64,7 +64,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
64 if (ttype(obj) != LUA_T_NUMBER) 64 if (ttype(obj) != LUA_T_NUMBER)
65 return 1; 65 return 1;
66 else { 66 else {
67 char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */ 67 char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
68 sprintf(s, "%.16g", (double)nvalue(obj)); 68 sprintf(s, "%.16g", (double)nvalue(obj));
69 tsvalue(obj) = luaS_new(L, s); 69 tsvalue(obj) = luaS_new(L, s);
70 ttype(obj) = LUA_T_STRING; 70 ttype(obj) = LUA_T_STRING;