summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c23
-rw-r--r--lapi.h3
-rw-r--r--lbuiltin.c8
-rw-r--r--ldebug.c28
-rw-r--r--ldo.c63
-rw-r--r--ldo.h4
-rw-r--r--lfunc.c5
-rw-r--r--lgc.c15
-rw-r--r--lobject.c15
-rw-r--r--lobject.h61
-rw-r--r--lparser.c7
-rw-r--r--lref.c6
-rw-r--r--ltable.c8
-rw-r--r--ltm.c19
-rw-r--r--lvm.c50
-rw-r--r--lvm.h7
16 files changed, 128 insertions, 194 deletions
diff --git a/lapi.c b/lapi.c
index 7c6cb7ef..c4d14652 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.75 2000/03/20 19:14:54 roberto Exp roberto $ 2** $Id: lapi.c,v 1.76 2000/03/27 20:10:21 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*/
@@ -30,17 +30,6 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
30 30
31 31
32 32
33
34const TObject *luaA_protovalue (const TObject *o) {
35 switch (ttype(o)) {
36 case TAG_CCLOSURE: case TAG_LCLOSURE:
37 return protovalue(o);
38 default:
39 return o;
40 }
41}
42
43
44void luaA_checkCargs (lua_State *L, int nargs) { 33void luaA_checkCargs (lua_State *L, int nargs) {
45 if (nargs > L->top-L->Cstack.base) 34 if (nargs > L->top-L->Cstack.base)
46 luaL_verror(L, "Lua API error - " 35 luaL_verror(L, "Lua API error - "
@@ -210,7 +199,8 @@ int lua_isuserdata (lua_State *L, lua_Object o) {
210} 199}
211 200
212int lua_iscfunction (lua_State *L, lua_Object o) { 201int lua_iscfunction (lua_State *L, lua_Object o) {
213 return (lua_tag(L, o) == TAG_CPROTO); 202 UNUSED(L);
203 return (o != LUA_NOOBJECT) && (ttype(o) == TAG_CCLOSURE);
214} 204}
215 205
216int lua_isnumber (lua_State *L, lua_Object o) { 206int lua_isnumber (lua_State *L, lua_Object o) {
@@ -266,7 +256,7 @@ void *lua_getuserdata (lua_State *L, lua_Object obj) {
266lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) { 256lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
267 if (!lua_iscfunction(L, obj)) 257 if (!lua_iscfunction(L, obj))
268 return NULL; 258 return NULL;
269 else return fvalue(luaA_protovalue(obj)); 259 else return clvalue(obj)->f.c;
270} 260}
271 261
272 262
@@ -299,10 +289,7 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
299 if (fn == NULL) 289 if (fn == NULL)
300 lua_error(L, "Lua API error - attempt to push a NULL Cfunction"); 290 lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
301 luaA_checkCargs(L, n); 291 luaA_checkCargs(L, n);
302 ttype(L->top) = TAG_CPROTO; 292 luaV_Cclosure(L, fn, n);
303 fvalue(L->top) = fn;
304 incr_top;
305 luaV_closure(L, n);
306 luaC_checkGC(L); 293 luaC_checkGC(L);
307} 294}
308 295
diff --git a/lapi.h b/lapi.h
index 835009d7..7f43c2a0 100644
--- a/lapi.h
+++ b/lapi.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.h,v 1.14 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: lapi.h,v 1.15 2000/03/10 18:37:44 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*/
@@ -12,7 +12,6 @@
12 12
13 13
14void luaA_checkCargs (lua_State *L, int nargs); 14void luaA_checkCargs (lua_State *L, int nargs);
15const TObject *luaA_protovalue (const TObject *o);
16void luaA_pushobject (lua_State *L, const TObject *o); 15void luaA_pushobject (lua_State *L, const TObject *o);
17GlobalVar *luaA_nextvar (lua_State *L, TString *g); 16GlobalVar *luaA_nextvar (lua_State *L, TString *g);
18int luaA_next (lua_State *L, const Hash *t, int i); 17int luaA_next (lua_State *L, const Hash *t, int i);
diff --git a/lbuiltin.c b/lbuiltin.c
index eadce4ec..8bd20248 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.98 2000/03/27 20:08:02 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.99 2000/03/27 20:10:21 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*/
@@ -386,12 +386,6 @@ void luaB_tostring (lua_State *L) {
386 case TAG_LCLOSURE: case TAG_CCLOSURE: 386 case TAG_LCLOSURE: case TAG_CCLOSURE:
387 sprintf(buff, "function: %p", o->value.cl); 387 sprintf(buff, "function: %p", o->value.cl);
388 break; 388 break;
389 case TAG_LPROTO:
390 sprintf(buff, "function: %p", o->value.tf);
391 break;
392 case TAG_CPROTO:
393 sprintf(buff, "function: %p", o->value.f);
394 break;
395 case TAG_USERDATA: 389 case TAG_USERDATA:
396 sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value, 390 sprintf(buff, "userdata: %p(%d)", o->value.ts->u.d.value,
397 o->value.ts->u.d.tag); 391 o->value.ts->u.d.tag);
diff --git a/ldebug.c b/ldebug.c
index 33823257..40bcb6e6 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.12 2000/03/20 19:14:54 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.13 2000/03/27 20:10:21 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -24,10 +24,8 @@
24 24
25static const lua_Type normtype[] = { /* ORDER LUA_T */ 25static const lua_Type normtype[] = { /* ORDER LUA_T */
26 TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE, 26 TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE,
27 TAG_LPROTO, TAG_CPROTO, TAG_NIL, 27 TAG_LCLOSURE, TAG_CCLOSURE, TAG_NIL,
28 TAG_LCLOSURE, TAG_CCLOSURE, 28 TAG_LCLOSURE, TAG_CCLOSURE /* TAG_LCLMARK, TAG_CCLMARK */
29 TAG_LCLOSURE, TAG_CCLOSURE, /* TAG_LCLMARK, TAG_CCLMARK */
30 TAG_LPROTO, TAG_CPROTO /* TAG_LMARK, TAG_CMARK */
31}; 29};
32 30
33 31
@@ -104,11 +102,7 @@ static int lua_currentline (lua_State *L, StkId f) {
104 102
105 103
106static Proto *getluaproto (StkId f) { 104static Proto *getluaproto (StkId f) {
107 if (ttype(f) == TAG_LMARK) 105 return (ttype(f) == TAG_LCLMARK) ? clvalue(f)->f.l : NULL;
108 return f->value.tf;
109 else if (ttype(f) == TAG_LCLMARK)
110 return protovalue(f)->value.tf;
111 else return NULL;
112} 106}
113 107
114 108
@@ -141,20 +135,18 @@ int lua_setlocal (lua_State *L, const lua_Dbgactreg *ar, lua_Dbglocvar *v) {
141static void lua_funcinfo (lua_Dbgactreg *ar) { 135static void lua_funcinfo (lua_Dbgactreg *ar) {
142 StkId func = ar->_func; 136 StkId func = ar->_func;
143 switch (ttype(func)) { 137 switch (ttype(func)) {
144 case TAG_LPROTO: case TAG_LMARK:
145 ar->source = tfvalue(func)->source->str;
146 ar->linedefined = tfvalue(func)->lineDefined;
147 ar->what = "Lua";
148 break;
149 case TAG_LCLOSURE: case TAG_LCLMARK: 138 case TAG_LCLOSURE: case TAG_LCLMARK:
150 ar->source = tfvalue(protovalue(func))->source->str; 139 ar->source = clvalue(func)->f.l->source->str;
151 ar->linedefined = tfvalue(protovalue(func))->lineDefined; 140 ar->linedefined = clvalue(func)->f.l->lineDefined;
152 ar->what = "Lua"; 141 ar->what = "Lua";
153 break; 142 break;
154 default: 143 case TAG_CCLOSURE: case TAG_CCLMARK:
155 ar->source = "(C)"; 144 ar->source = "(C)";
156 ar->linedefined = -1; 145 ar->linedefined = -1;
157 ar->what = "C"; 146 ar->what = "C";
147 break;
148 default:
149 LUA_INTERNALERROR(L, "invalid `func' value");
158 } 150 }
159 if (ar->linedefined == 0) 151 if (ar->linedefined == 0)
160 ar->what = "main"; 152 ar->what = "main";
diff --git a/ldo.c b/ldo.c
index cde94514..d9f6d14a 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.68 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: ldo.c,v 1.69 2000/03/10 18:37:44 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*/
@@ -119,7 +119,7 @@ void luaD_lineHook (lua_State *L, StkId func, int line) {
119} 119}
120 120
121 121
122void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook, 122static void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook,
123 const char *event) { 123 const char *event) {
124 if (L->allowhooks) { 124 if (L->allowhooks) {
125 lua_Dbgactreg ar; 125 lua_Dbgactreg ar;
@@ -137,40 +137,31 @@ void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook,
137} 137}
138 138
139 139
140/* 140static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
141** Call a C function. 141 int nup = cl->nelems; /* number of upvalues */
142** Cstack.num is the number of arguments; Cstack.lua2C points to the 142 int numarg = L->top-base;
143** first argument. Returns an index to the first result from C.
144*/
145static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
146 struct C_Lua_Stack oldCLS = L->Cstack; 143 struct C_Lua_Stack oldCLS = L->Cstack;
147 StkId firstResult; 144 StkId firstResult;
148 int numarg = L->top - base; 145 if (nup > 0) {
146 int n = numarg;
147 luaD_checkstack(L, nup);
148 /* open space for upvalues as extra arguments */
149 while (n--) *(base+nup+n) = *(base+n);
150 L->top += nup;
151 numarg += nup;
152 /* copy upvalues into stack */
153 while (nup--) *(base+nup) = cl->consts[nup];
154 }
149 L->Cstack.num = numarg; 155 L->Cstack.num = numarg;
150 L->Cstack.lua2C = base; 156 L->Cstack.lua2C = base;
151 L->Cstack.base = L->top; 157 L->Cstack.base = L->top;
152 if (L->callhook) 158 (*cl->f.c)(L); /* do the actual call */
153 luaD_callHook(L, base-1, L->callhook, "call");
154 (*f)(L); /* do the actual call */
155 firstResult = L->Cstack.base; 159 firstResult = L->Cstack.base;
156 L->Cstack = oldCLS; 160 L->Cstack = oldCLS;
157 return firstResult; 161 return firstResult;
158} 162}
159 163
160 164
161static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
162 int nup = cl->nelems; /* number of upvalues */
163 int n = L->top-base; /* number of arguments (to move up) */
164 luaD_checkstack(L, nup);
165 /* open space for upvalues as extra arguments */
166 while (n--) *(base+nup+n) = *(base+n);
167 L->top += nup;
168 /* copy upvalues into stack */
169 while (nup--) *(base+nup) = cl->consts[nup+1];
170 return callC(L, fvalue(cl->consts), base);
171}
172
173
174void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) { 165void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
175 StkId base = L->top - nParams; 166 StkId base = L->top - nParams;
176 luaD_openstack(L, base); 167 luaD_openstack(L, base);
@@ -191,24 +182,18 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
191 lua_Dbghook callhook = L->callhook; 182 lua_Dbghook callhook = L->callhook;
192 retry: /* for `function' tag method */ 183 retry: /* for `function' tag method */
193 switch (ttype(func)) { 184 switch (ttype(func)) {
194 case TAG_CPROTO:
195 ttype(func) = TAG_CMARK;
196 firstResult = callC(L, fvalue(func), func+1);
197 break;
198 case TAG_LPROTO:
199 ttype(func) = TAG_LMARK;
200 firstResult = luaV_execute(L, NULL, tfvalue(func), func+1);
201 break;
202 case TAG_LCLOSURE: { 185 case TAG_LCLOSURE: {
203 Closure *c = clvalue(func);
204 ttype(func) = TAG_LCLMARK; 186 ttype(func) = TAG_LCLMARK;
205 firstResult = luaV_execute(L, c, tfvalue(c->consts), func+1); 187 if (callhook)
188 luaD_callHook(L, func, callhook, "call");
189 firstResult = luaV_execute(L, clvalue(func), func+1);
206 break; 190 break;
207 } 191 }
208 case TAG_CCLOSURE: { 192 case TAG_CCLOSURE: {
209 Closure *c = clvalue(func);
210 ttype(func) = TAG_CCLMARK; 193 ttype(func) = TAG_CCLMARK;
211 firstResult = callCclosure(L, c, func+1); 194 if (callhook)
195 luaD_callHook(L, func, callhook, "call");
196 firstResult = callCclosure(L, clvalue(func), func+1);
212 break; 197 break;
213 } 198 }
214 default: { /* `func' is not a function; check the `function' tag method */ 199 default: { /* `func' is not a function; check the `function' tag method */
@@ -316,9 +301,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
316 L->errorJmp = oldErr; 301 L->errorJmp = oldErr;
317 if (status) return 1; /* error code */ 302 if (status) return 1; /* error code */
318 if (tf == NULL) return 2; /* `natural' end */ 303 if (tf == NULL) return 2; /* `natural' end */
319 L->top->ttype = TAG_LPROTO; /* push new function on the stack */ 304 luaV_Lclosure(L, tf, 0);
320 L->top->value.tf = tf;
321 incr_top;
322 return 0; 305 return 0;
323} 306}
324 307
diff --git a/ldo.h b/ldo.h
index 502b2399..f2e6ce5f 100644
--- a/ldo.h
+++ b/ldo.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.h,v 1.17 2000/01/19 12:00:45 roberto Exp roberto $ 2** $Id: ldo.h,v 1.18 2000/03/24 17:26:08 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*/
@@ -22,8 +22,6 @@
22void luaD_init (lua_State *L, int stacksize); 22void luaD_init (lua_State *L, int stacksize);
23void luaD_adjusttop (lua_State *L, StkId base, int extra); 23void luaD_adjusttop (lua_State *L, StkId base, int extra);
24void luaD_openstack (lua_State *L, StkId pos); 24void luaD_openstack (lua_State *L, StkId pos);
25void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook,
26 const char *event);
27void luaD_lineHook (lua_State *L, StkId func, int line); 25void luaD_lineHook (lua_State *L, StkId func, int line);
28void luaD_call (lua_State *L, StkId func, int nResults); 26void luaD_call (lua_State *L, StkId func, int nResults);
29void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults); 27void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
diff --git a/lfunc.c b/lfunc.c
index 18ca5ca2..ffb456d5 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.19 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.20 2000/03/10 18:37:44 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*/
@@ -19,7 +19,8 @@
19 19
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)+nelems*sizeof(TObject)); 22 Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
23 sizeof(TObject)*(nelems-1));
23 c->next = L->rootcl; 24 c->next = L->rootcl;
24 L->rootcl = c; 25 L->rootcl = c;
25 c->marked = 0; 26 c->marked = 0;
diff --git a/lgc.c b/lgc.c
index ae14c9ab..9adbc385 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.43 2000/03/27 20:08:02 roberto Exp roberto $ 2** $Id: lgc.c,v 1.44 2000/03/27 20:10:21 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*/
@@ -43,9 +43,9 @@ static void protomark (lua_State *L, Proto *f) {
43 43
44static void closuremark (lua_State *L, Closure *f) { 44static void closuremark (lua_State *L, Closure *f) {
45 if (!f->marked) { 45 if (!f->marked) {
46 int i; 46 int i = f->nelems;
47 f->marked = 1; 47 f->marked = 1;
48 for (i=f->nelems; i>=0; i--) 48 while (i--)
49 markobject(L, &f->consts[i]); 49 markobject(L, &f->consts[i]);
50 } 50 }
51} 51}
@@ -103,13 +103,12 @@ static int markobject (lua_State *L, TObject *o) {
103 hashmark(L, avalue(o)); 103 hashmark(L, avalue(o));
104 break; 104 break;
105 case TAG_LCLOSURE: case TAG_LCLMARK: 105 case TAG_LCLOSURE: case TAG_LCLMARK:
106 protomark(L, clvalue(o)->f.l);
107 /* go trhough */
106 case TAG_CCLOSURE: case TAG_CCLMARK: 108 case TAG_CCLOSURE: case TAG_CCLMARK:
107 closuremark(L, o->value.cl); 109 closuremark(L, clvalue(o));
108 break;
109 case TAG_LPROTO: case TAG_LMARK:
110 protomark(L, o->value.tf);
111 break; 110 break;
112 default: break; /* numbers, cprotos, etc */ 111 default: break; /* numbers, etc */
113 } 112 }
114 return 0; 113 return 0;
115} 114}
diff --git a/lobject.c b/lobject.c
index b3f0da34..edfed796 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.33 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: lobject.c,v 1.34 2000/03/27 20:10:21 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*/
@@ -15,8 +15,7 @@
15 15
16const char *const luaO_typenames[] = { /* ORDER LUA_T */ 16const char *const luaO_typenames[] = { /* ORDER LUA_T */
17 "userdata", "number", "string", "table", "function", "function", "nil", 17 "userdata", "number", "string", "table", "function", "function", "nil",
18 "function", "function", "function", "function", "function", "function", 18 "function", "function", "line"
19 "line", NULL
20}; 19};
21 20
22 21
@@ -35,20 +34,16 @@ unsigned long luaO_power2 (unsigned long n) {
35 34
36int luaO_equalval (const TObject *t1, const TObject *t2) { 35int luaO_equalval (const TObject *t1, const TObject *t2) {
37 switch (ttype(t1)) { 36 switch (ttype(t1)) {
38 case TAG_NIL:
39 return 1;
40 case TAG_NUMBER: 37 case TAG_NUMBER:
41 return nvalue(t1) == nvalue(t2); 38 return nvalue(t1) == nvalue(t2);
42 case TAG_STRING: case TAG_USERDATA: 39 case TAG_STRING: case TAG_USERDATA:
43 return svalue(t1) == svalue(t2); 40 return svalue(t1) == svalue(t2);
44 case TAG_TABLE: 41 case TAG_TABLE:
45 return avalue(t1) == avalue(t2); 42 return avalue(t1) == avalue(t2);
46 case TAG_LPROTO:
47 return tfvalue(t1) == tfvalue(t2);
48 case TAG_CPROTO:
49 return fvalue(t1) == fvalue(t2);
50 case TAG_CCLOSURE: case TAG_LCLOSURE: 43 case TAG_CCLOSURE: case TAG_LCLOSURE:
51 return t1->value.cl == t2->value.cl; 44 return clvalue(t1) == clvalue(t2);
45 case TAG_NIL:
46 return 1;
52 default: 47 default:
53 LUA_INTERNALERROR(L, "invalid type"); 48 LUA_INTERNALERROR(L, "invalid type");
54 return 0; /* UNREACHABLE */ 49 return 0; /* UNREACHABLE */
diff --git a/lobject.h b/lobject.h
index 49b0f0d2..844111ff 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.55 2000/03/24 19:49:23 roberto Exp roberto $ 2** $Id: lobject.h,v 1.56 2000/03/27 20:10:21 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*/
@@ -23,7 +23,12 @@
23#endif 23#endif
24 24
25 25
26#ifdef DEBUG
27/* to avoid warnings and make sure is is really unused */
28#define UNUSED(x) (x=0, (void)x)
29#else
26#define UNUSED(x) (void)x /* to avoid warnings */ 30#define UNUSED(x) (void)x /* to avoid warnings */
31#endif
27 32
28 33
29/* 34/*
@@ -36,17 +41,12 @@ typedef enum {
36 TAG_NUMBER, /* fixed tag for numbers */ 41 TAG_NUMBER, /* fixed tag for numbers */
37 TAG_STRING, /* fixed tag for strings */ 42 TAG_STRING, /* fixed tag for strings */
38 TAG_TABLE, /* default tag for tables */ 43 TAG_TABLE, /* default tag for tables */
39 TAG_LPROTO, /* fixed tag for Lua functions */ 44 TAG_LCLOSURE, /* fixed tag for Lua closures */
40 TAG_CPROTO, /* fixed tag for C functions */ 45 TAG_CCLOSURE, /* fixed tag for C closures */
41 TAG_NIL, /* last "pre-defined" tag */ 46 TAG_NIL, /* last "pre-defined" tag */
42 47
43 TAG_LCLOSURE, /* Lua closure */
44 TAG_CCLOSURE, /* C closure */
45
46 TAG_LCLMARK, /* mark for Lua closures */ 48 TAG_LCLMARK, /* mark for Lua closures */
47 TAG_CCLMARK, /* mark for C closures */ 49 TAG_CCLMARK, /* mark for C closures */
48 TAG_LMARK, /* mark for Lua prototypes */
49 TAG_CMARK, /* mark for C prototypes */
50 50
51 TAG_LINE 51 TAG_LINE
52} lua_Type; 52} lua_Type;
@@ -58,20 +58,27 @@ typedef enum {
58/* 58/*
59** check whether `t' is a mark 59** check whether `t' is a mark
60*/ 60*/
61#define is_T_MARK(t) (TAG_LCLMARK <= (t) && (t) <= TAG_CMARK) 61#define is_T_MARK(t) ((t) == TAG_LCLMARK || (t) == TAG_CCLMARK)
62 62
63 63
64typedef union { 64typedef union {
65 lua_CFunction f; /* TAG_CPROTO, TAG_CMARK */ 65 struct TString *ts; /* TAG_STRING, TAG_USERDATA */
66 Number n; /* TAG_NUMBER */ 66 struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]CLMARK */
67 struct TString *ts; /* TAG_STRING, TAG_USERDATA */ 67 struct Hash *a; /* TAG_TABLE */
68 struct Proto *tf; /* TAG_LPROTO, TAG_LMARK */ 68 Number n; /* TAG_NUMBER */
69 struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_[CL]CLMARK */ 69 int i; /* TAG_LINE */
70 struct Hash *a; /* TAG_TABLE */
71 int i; /* TAG_LINE */
72} Value; 70} Value;
73 71
74 72
73/* Macros to access values */
74#define ttype(o) ((o)->ttype)
75#define nvalue(o) ((o)->value.n)
76#define svalue(o) ((o)->value.ts->str)
77#define tsvalue(o) ((o)->value.ts)
78#define clvalue(o) ((o)->value.cl)
79#define avalue(o) ((o)->value.a)
80
81
75typedef struct TObject { 82typedef struct TObject {
76 lua_Type ttype; 83 lua_Type ttype;
77 Value value; 84 Value value;
@@ -135,28 +142,18 @@ typedef struct LocVar {
135} LocVar; 142} LocVar;
136 143
137 144
138
139/* Macros to access structure members */
140#define ttype(o) ((o)->ttype)
141#define nvalue(o) ((o)->value.n)
142#define svalue(o) ((o)->value.ts->str)
143#define tsvalue(o) ((o)->value.ts)
144#define clvalue(o) ((o)->value.cl)
145#define avalue(o) ((o)->value.a)
146#define fvalue(o) ((o)->value.f)
147#define tfvalue(o) ((o)->value.tf)
148
149#define protovalue(o) ((o)->value.cl->consts)
150
151
152/* 145/*
153** Closures 146** Closures
154*/ 147*/
155typedef struct Closure { 148typedef struct Closure {
156 struct Closure *next; 149 struct Closure *next;
157 int marked; 150 int marked;
158 int nelems; /* not including the first one (always the prototype) */ 151 union {
159 TObject consts[1]; /* at least one for prototype */ 152 lua_CFunction c; /* C functions */
153 struct Proto *l; /* Lua functions */
154 } f;
155 int nelems;
156 TObject consts[1];
160} Closure; 157} Closure;
161 158
162 159
diff --git a/lparser.c b/lparser.c
index 8238b996..5f0b6adb 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.72 2000/03/24 12:17:53 roberto Exp roberto $ 2** $Id: lparser.c,v 1.73 2000/03/24 17:26:08 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*/
@@ -345,10 +345,6 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) {
345 f->numparams = 0; /* default for main chunk */ 345 f->numparams = 0; /* default for main chunk */
346 f->is_vararg = 0; /* default for main chunk */ 346 f->is_vararg = 0; /* default for main chunk */
347 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */ 347 fs->nvars = (L->debug) ? 0 : -1; /* flag no debug information? */
348 /* push function (to avoid GC) */
349 tfvalue(L->top) = f;
350 ttype(L->top) = TAG_LPROTO;
351 incr_top;
352} 348}
353 349
354 350
@@ -366,7 +362,6 @@ static void close_func (LexState *ls) {
366 luaM_reallocvector(L, f->locvars, fs->nvars, LocVar); 362 luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
367 } 363 }
368 ls->fs = fs->prev; 364 ls->fs = fs->prev;
369 L->top--; /* pop function */
370} 365}
371 366
372 367
diff --git a/lref.c b/lref.c
index 4cbe4b48..99b6fda5 100644
--- a/lref.c
+++ b/lref.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lref.c,v 1.9 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: lref.c,v 1.10 2000/03/27 20:10:21 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*/
@@ -88,9 +88,7 @@ static int ismarked (const TObject *o) {
88 return o->value.a->marked; 88 return o->value.a->marked;
89 case TAG_LCLOSURE: case TAG_CCLOSURE: 89 case TAG_LCLOSURE: case TAG_CCLOSURE:
90 return o->value.cl->marked; 90 return o->value.cl->marked;
91 case TAG_LPROTO: 91 default: /* number */
92 return o->value.tf->marked;
93 default: /* number or cproto */
94 return 1; 92 return 1;
95 } 93 }
96} 94}
diff --git a/ltable.c b/ltable.c
index afd17b26..86357920 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.36 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: ltable.c,v 1.37 2000/03/27 20:10:21 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*/
@@ -52,12 +52,6 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
52 case TAG_TABLE: 52 case TAG_TABLE:
53 h = IntPoint(L, avalue(key)); 53 h = IntPoint(L, avalue(key));
54 break; 54 break;
55 case TAG_LPROTO:
56 h = IntPoint(L, tfvalue(key));
57 break;
58 case TAG_CPROTO:
59 h = IntPoint(L, fvalue(key));
60 break;
61 case TAG_LCLOSURE: case TAG_CCLOSURE: 55 case TAG_LCLOSURE: case TAG_CCLOSURE:
62 h = IntPoint(L, clvalue(key)); 56 h = IntPoint(L, clvalue(key));
63 break; 57 break;
diff --git a/ltm.c b/ltm.c
index 10c94829..3bf2f1fd 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.36 2000/03/27 20:08:02 roberto Exp roberto $ 2** $Id: ltm.c,v 1.37 2000/03/27 20:10:21 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*/
@@ -47,8 +47,8 @@ static const char luaT_validevents[NUM_TAGS][IM_N] = {
47 {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */ 47 {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
48 {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */ 48 {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
49 {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */ 49 {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
50 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LPROTO */ 50 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
51 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CPROTO */ 51 {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
52 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */ 52 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
53}; 53};
54 54
@@ -105,19 +105,14 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
105 105
106 106
107int luaT_effectivetag (lua_State *L, const TObject *o) { 107int luaT_effectivetag (lua_State *L, const TObject *o) {
108 static const int realtag[] = { /* ORDER LUA_T */ 108 lua_Type t = ttype(o);
109 TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_TABLE, 109 switch (t) {
110 TAG_LPROTO, TAG_CPROTO, TAG_NIL,
111 TAG_LPROTO, TAG_CPROTO, /* TAG_LCLOSURE, TAG_CCLOSURE */
112 };
113 lua_Type t;
114 switch (t = ttype(o)) {
115 case TAG_USERDATA: { 110 case TAG_USERDATA: {
116 int tag = o->value.ts->u.d.tag; 111 int tag = o->value.ts->u.d.tag;
117 return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */ 112 return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
118 } 113 }
119 case TAG_TABLE: return o->value.a->htag; 114 case TAG_TABLE: return o->value.a->htag;
120 default: return realtag[t]; 115 default: return t;
121 } 116 }
122} 117}
123 118
diff --git a/lvm.c b/lvm.c
index 9baee90e..96159c0c 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.96 2000/03/17 13:09:12 roberto Exp roberto $ 2** $Id: lvm.c,v 1.97 2000/03/27 20:10:21 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*/
@@ -72,17 +72,27 @@ void luaV_setn (lua_State *L, Hash *t, int val) {
72} 72}
73 73
74 74
75void luaV_closure (lua_State *L, int nelems) { 75static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
76 if (nelems > 0) { 76 Closure *c = luaF_newclosure(L, nelems);
77 Closure *c = luaF_newclosure(L, nelems); 77 L->top -= nelems;
78 c->consts[0] = *(L->top-1); 78 while (nelems--)
79 L->top -= nelems; 79 c->consts[nelems] = *(L->top+nelems);
80 while (nelems--) 80 ttype(L->top) = t;
81 c->consts[nelems+1] = *(L->top-1+nelems); 81 clvalue(L->top) = c;
82 ttype(L->top-1) = (ttype(&c->consts[0]) == TAG_CPROTO) ? 82 incr_top;
83 TAG_CCLOSURE : TAG_LCLOSURE; 83 return c;
84 (L->top-1)->value.cl = c; 84}
85 } 85
86
87void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
88 Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
89 cl->f.c = c;
90}
91
92
93void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
94 Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
95 cl->f.l = l;
86} 96}
87 97
88 98
@@ -317,13 +327,11 @@ static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
317** Executes the given Lua function. Parameters are between [base,top). 327** Executes the given Lua function. Parameters are between [base,top).
318** Returns n such that the the results are between [n,top). 328** Returns n such that the the results are between [n,top).
319*/ 329*/
320StkId luaV_execute (lua_State *L, const Closure *cl, const Proto *tf, 330StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) {
321 register StkId base) { 331 const Proto *tf = cl->f.l;
322 register StkId top; /* keep top local, for performance */ 332 register StkId top; /* keep top local, for performance */
323 register const Instruction *pc = tf->code; 333 register const Instruction *pc = tf->code;
324 TString **kstr = tf->kstr; 334 TString **kstr = tf->kstr;
325 if (L->callhook)
326 luaD_callHook(L, base-1, L->callhook, "call");
327 luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); 335 luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
328 if (tf->is_vararg) { /* varargs? */ 336 if (tf->is_vararg) { /* varargs? */
329 adjust_varargs(L, base, tf->numparams); 337 adjust_varargs(L, base, tf->numparams);
@@ -392,7 +400,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const Proto *tf,
392 break; 400 break;
393 401
394 case OP_PUSHUPVALUE: 402 case OP_PUSHUPVALUE:
395 *top++ = cl->consts[GETARG_U(i)+1]; 403 *top++ = cl->consts[GETARG_U(i)];
396 break; 404 break;
397 405
398 case OP_PUSHLOCAL: 406 case OP_PUSHLOCAL:
@@ -604,11 +612,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const Proto *tf,
604 break; 612 break;
605 613
606 case OP_CLOSURE: 614 case OP_CLOSURE:
607 ttype(top) = TAG_LPROTO; 615 L->top = top;
608 tfvalue(top) = tf->kproto[GETARG_A(i)]; 616 luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
609 L->top = ++top; 617 top = L->top;
610 luaV_closure(L, GETARG_B(i));
611 top -= GETARG_B(i);
612 luaC_checkGC(L); 618 luaC_checkGC(L);
613 break; 619 break;
614 620
diff --git a/lvm.h b/lvm.h
index 1a733a44..c1facfc1 100644
--- a/lvm.h
+++ b/lvm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.h,v 1.18 2000/03/09 00:19:22 roberto Exp roberto $ 2** $Id: lvm.h,v 1.19 2000/03/10 18:37:44 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*/
@@ -26,8 +26,9 @@ void luaV_settable (lua_State *L, StkId t, StkId top);
26void luaV_rawsettable (lua_State *L, StkId t); 26void luaV_rawsettable (lua_State *L, StkId t);
27void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top); 27void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top);
28void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top); 28void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top);
29StkId luaV_execute (lua_State *L, const Closure *cl, const Proto *tf, StkId base); 29StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base);
30void luaV_closure (lua_State *L, int nelems); 30void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
31void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
31int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top); 32int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top);
32 33
33#endif 34#endif