aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
commitdd22ea4da550c3a158e0f11b928c349336184f85 (patch)
treeb73e2ae0498c4efa30dee04f710fb1a349f164ed
parent5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02 (diff)
downloadlua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.gz
lua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.bz2
lua-dd22ea4da550c3a158e0f11b928c349336184f85.zip
new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb structures lua2C and C2lua.
-rw-r--r--fallback.c21
-rw-r--r--inout.c70
-rw-r--r--lua.h19
-rw-r--r--opcode.c95
-rw-r--r--table.c17
-rw-r--r--tree.c61
-rw-r--r--tree.h16
7 files changed, 163 insertions, 136 deletions
diff --git a/fallback.c b/fallback.c
index 834e2dcd..4aed4690 100644
--- a/fallback.c
+++ b/fallback.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $"; 6char *rcs_fallback="$Id: fallback.c,v 2.5 1997/04/24 22:59:57 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -116,16 +116,20 @@ struct IM *luaI_IMtable = NULL;
116static int IMtable_size = 0; 116static int IMtable_size = 0;
117static int last_tag = LUA_T_NIL; /* ORDER LUA_T */ 117static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
118 118
119
120/* events in LUA_T_LINE are all allowed, since this is used as a
121* 'placeholder' for "default" fallbacks
122*/
119static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */ 123static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
120{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_USERDATA */ 124{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
121{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_LINE */ 125{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
122{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */ 126{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
123{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */ 127{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
124{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */ 128{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
125{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */ 129{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
126{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */ 130{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
127{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */ 131{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
128{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */ 132{1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, /* LUA_T_NUMBER */
129{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */ 133{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0} /* LUA_T_NIL */
130}; 134};
131 135
@@ -186,6 +190,9 @@ void luaI_settag (int tag, TObject *o)
186 case LUA_T_ARRAY: 190 case LUA_T_ARRAY:
187 o->value.a->htag = tag; 191 o->value.a->htag = tag;
188 break; 192 break;
193 case LUA_T_USERDATA:
194 o->value.ts->tag = tag;
195 break;
189 default: 196 default:
190 luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]); 197 luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
191 } 198 }
@@ -318,7 +325,7 @@ void luaI_setfallback (void)
318 break; 325 break;
319 case 2: { /* old arith fallback */ 326 case 2: { /* old arith fallback */
320 int i; 327 int i;
321 oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW); 328 oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
322 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ 329 for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
323 fillvalids(i, luaI_Address(func)); 330 fillvalids(i, luaI_Address(func));
324 replace = typeFB; 331 replace = typeFB;
@@ -326,7 +333,7 @@ void luaI_setfallback (void)
326 } 333 }
327 case 3: { /* old order fallback */ 334 case 3: { /* old order fallback */
328 int i; 335 int i;
329 oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT); 336 oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
330 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ 337 for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
331 fillvalids(i, luaI_Address(func)); 338 fillvalids(i, luaI_Address(func));
332 replace = typeFB; 339 replace = typeFB;
@@ -335,7 +342,7 @@ void luaI_setfallback (void)
335 default: { 342 default: {
336 int e; 343 int e;
337 if ((e = luaI_findstring(name, luaI_eventname)) >= 0) { 344 if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
338 oldfunc = *luaI_getim(LUA_T_USERDATA, e); 345 oldfunc = *luaI_getim(LUA_T_LINE, e);
339 fillvalids(e, luaI_Address(func)); 346 fillvalids(e, luaI_Address(func));
340 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; 347 replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
341 } 348 }
diff --git a/inout.c b/inout.c
index d0bd707e..50782703 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.58 1997/04/15 17:32:47 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
@@ -103,6 +103,7 @@ void lua_closestring (void)
103} 103}
104 104
105 105
106
106static int passresults (void) 107static int passresults (void)
107{ 108{
108 int arg = 0; 109 int arg = 0;
@@ -111,6 +112,25 @@ static int passresults (void)
111 lua_pushobject(obj); 112 lua_pushobject(obj);
112 return arg-1; 113 return arg-1;
113} 114}
115
116
117static void packresults (void)
118{
119 int arg = 0;
120 lua_Object obj;
121 lua_Object table = lua_createtable();
122 while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
123 lua_pushobject(table);
124 lua_pushnumber(arg);
125 lua_pushobject(obj);
126 lua_rawsettable();
127 }
128 lua_pushobject(table);
129 lua_pushstring("n");
130 lua_pushnumber(arg-1);
131 lua_rawsettable();
132 lua_pushobject(table); /* final result */
133}
114 134
115/* 135/*
116** Internal function: do a string 136** Internal function: do a string
@@ -144,14 +164,8 @@ static char *tostring (lua_Object obj)
144 case LUA_T_CFUNCTION: case LUA_T_NIL: 164 case LUA_T_CFUNCTION: case LUA_T_NIL:
145 return luaI_typenames[-ttype(o)]; 165 return luaI_typenames[-ttype(o)];
146 case LUA_T_USERDATA: { 166 case LUA_T_USERDATA: {
147 char *buff = luaI_buffer(100); 167 char *buff = luaI_buffer(30);
148 int size = o->value.ts->size; 168 sprintf(buff, "userdata: %p", o->value.ts->u.v);
149 int i;
150 strcpy(buff, "userdata: ");
151 if (size > 10) size = 10;
152 for (i=0; i<size; i++)
153 sprintf(buff+strlen(buff), "%.2X",
154 (int)(unsigned char)o->value.ts->str[i]);
155 return buff; 169 return buff;
156 } 170 }
157 default: return "<unknown object>"; 171 default: return "<unknown object>";
@@ -237,37 +251,37 @@ static void luatag (void)
237 lua_pushnumber(lua_tag(lua_getparam(1))); 251 lua_pushnumber(lua_tag(lua_getparam(1)));
238} 252}
239 253
240#define MAXPARAMS 256 254
255static int getnarg (lua_Object table)
256{
257 lua_Object temp;
258 /* temp = table.n */
259 lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
260 return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
261}
262
241static void luaI_call (void) 263static void luaI_call (void)
242{ 264{
243 lua_Object f = lua_getparam(1); 265 lua_Object f = lua_getparam(1);
244 lua_Object arg = lua_getparam(2); 266 lua_Object arg = lua_getparam(2);
245 lua_Object temp, params[MAXPARAMS]; 267 int withtable = (luaL_opt_string(3, NULL) != NULL);
246 int narg, i; 268 int narg, i;
247 luaL_arg_check(lua_isfunction(f), 1, "function expected"); 269 luaL_arg_check(lua_isfunction(f), 1, "function expected");
248 luaL_arg_check(lua_istable(arg), 2, "table expected"); 270 luaL_arg_check(lua_istable(arg), 2, "table expected");
249 /* narg = arg.n */ 271 narg = getnarg(arg);
250 lua_pushobject(arg); 272 /* push arg[1...n] */
251 lua_pushstring("n");
252 temp = lua_getsubscript();
253 narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
254 /* read arg[1...n] */
255 for (i=0; i<narg; i++) { 273 for (i=0; i<narg; i++) {
256 if (i>=MAXPARAMS) 274 lua_Object temp;
257 lua_error("argument list too long in function `call'"); 275 /* temp = arg[i+1] */
258 lua_pushobject(arg); 276 lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
259 lua_pushnumber(i+1); 277 if (narg == MAX_WORD && lua_isnil(temp))
260 params[i] = lua_getsubscript();
261 if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
262 narg = i;
263 break; 278 break;
264 } 279 lua_pushobject(temp);
265 } 280 }
266 /* push parameters and do the call */
267 for (i=0; i<narg; i++)
268 lua_pushobject(params[i]);
269 if (lua_callfunction(f)) 281 if (lua_callfunction(f))
270 lua_error(NULL); 282 lua_error(NULL);
283 else if (withtable)
284 packresults();
271 else 285 else
272 passresults(); 286 passresults();
273} 287}
diff --git a/lua.h b/lua.h
index dd766e5b..d183832f 100644
--- a/lua.h
+++ b/lua.h
@@ -2,7 +2,7 @@
2** LUA - An Extensible Extension Language 2** LUA - An Extensible Extension Language
3** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil 3** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
4** e-mail: lua@tecgraf.puc-rio.br 4** e-mail: lua@tecgraf.puc-rio.br
5** $Id: lua.h,v 4.4 1997/05/26 14:42:51 roberto Exp roberto $ 5** $Id: lua.h,v 4.5 1997/06/06 20:54:40 roberto Exp roberto $
6*/ 6*/
7 7
8 8
@@ -16,6 +16,8 @@
16 16
17#define LUA_NOOBJECT 0 17#define LUA_NOOBJECT 0
18 18
19#define LUA_ANYTAG (-1)
20
19typedef void (*lua_CFunction) (void); 21typedef void (*lua_CFunction) (void);
20typedef unsigned int lua_Object; 22typedef unsigned int lua_Object;
21 23
@@ -31,8 +33,6 @@ int lua_dofile (char *filename); /* Out: returns */
31int lua_dostring (char *string); /* Out: returns */ 33int lua_dostring (char *string); /* Out: returns */
32int lua_callfunction (lua_Object f); 34int lua_callfunction (lua_Object f);
33 /* In: parameters; Out: returns */ 35 /* In: parameters; Out: returns */
34int lua_call (char *funcname);
35 /* In: parameters; Out: returns */
36 36
37void lua_beginblock (void); 37void lua_beginblock (void);
38void lua_endblock (void); 38void lua_endblock (void);
@@ -52,17 +52,16 @@ int lua_isfunction (lua_Object object);
52float lua_getnumber (lua_Object object); 52float lua_getnumber (lua_Object object);
53char *lua_getstring (lua_Object object); 53char *lua_getstring (lua_Object object);
54lua_CFunction lua_getcfunction (lua_Object object); 54lua_CFunction lua_getcfunction (lua_Object object);
55void *lua_getbindata (lua_Object object);
56int lua_getbindatasize (lua_Object object);
57 55
58void lua_pushnil (void); 56void lua_pushnil (void);
59void lua_pushnumber (float n); 57void lua_pushnumber (float n);
60void lua_pushstring (char *s); 58void lua_pushstring (char *s);
61void lua_pushcfunction (lua_CFunction fn); 59void lua_pushcfunction (lua_CFunction fn);
62void lua_pushbindata (void *buff, int size, int tag); 60void lua_pushusertag (void *u, int tag);
63void lua_pushusertag (void *u, int tag);
64void lua_pushobject (lua_Object object); 61void lua_pushobject (lua_Object object);
65 62
63lua_Object lua_pop (void);
64
66lua_Object lua_getglobal (char *name); 65lua_Object lua_getglobal (char *name);
67lua_Object lua_rawgetglobal (char *name); 66lua_Object lua_rawgetglobal (char *name);
68void lua_setglobal (char *name); /* In: value */ 67void lua_setglobal (char *name); /* In: value */
@@ -82,6 +81,8 @@ void lua_unref (int ref);
82 81
83lua_Object lua_createtable (void); 82lua_Object lua_createtable (void);
84 83
84lua_Object lua_getudata (void *u, int tag);
85
85 86
86long lua_collectgarbage (long limit); 87long lua_collectgarbage (long limit);
87 88
@@ -89,7 +90,9 @@ long lua_collectgarbage (long limit);
89/* =============================================================== */ 90/* =============================================================== */
90/* some useful macros */ 91/* some useful macros */
91 92
92#define lua_pushref(ref) (lua_pushobject(lua_getref(ref))) 93#define lua_call(name) lua_callfunction(lua_getglobal(name))
94
95#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
93 96
94#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) 97#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
95 98
diff --git a/opcode.c b/opcode.c
index 8f56b8de..b51598ea 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 4.6 1997/06/06 20:54:40 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -52,13 +52,13 @@ static TObject *top = &initial_stack;
52#define incr_top if (++top >= stackLimit) growstack() 52#define incr_top if (++top >= stackLimit) growstack()
53 53
54struct C_Lua_Stack { 54struct C_Lua_Stack {
55 StkId base; /* when Lua calls C or C calls Lua, points to */ 55 StkId base; /* when Lua calls C or C calls Lua, points to */
56 /* the first slot after the last parameter. */ 56 /* the first slot after the last parameter. */
57 int num; /* when Lua calls C, has the number of parameters; */ 57 StkId lua2C; /* points to first element of "array" lua2C */
58 /* when C calls Lua, has the number of results. */ 58 int num; /* size of "array" lua2C */
59}; 59};
60 60
61static struct C_Lua_Stack CLS_current = {0, 0}; 61static struct C_Lua_Stack CLS_current = {0, 0, 0};
62 62
63static jmp_buf *errorJmp = NULL; /* current error recover point */ 63static jmp_buf *errorJmp = NULL; /* current error recover point */
64 64
@@ -228,6 +228,13 @@ static lua_Object put_luaObjectonTop (void)
228} 228}
229 229
230 230
231lua_Object lua_pop (void)
232{
233 checkCparams(1);
234 return put_luaObjectonTop();
235}
236
237
231 238
232/* 239/*
233** call Line hook 240** call Line hook
@@ -235,7 +242,7 @@ static lua_Object put_luaObjectonTop (void)
235static void lineHook (int line) 242static void lineHook (int line)
236{ 243{
237 struct C_Lua_Stack oldCLS = CLS_current; 244 struct C_Lua_Stack oldCLS = CLS_current;
238 StkId old_top = CLS_current.base = top-stack; 245 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
239 CLS_current.num = 0; 246 CLS_current.num = 0;
240 (*lua_linehook)(line); 247 (*lua_linehook)(line);
241 top = stack+old_top; 248 top = stack+old_top;
@@ -250,7 +257,7 @@ static void lineHook (int line)
250static void callHook (StkId base, lua_Type type, int isreturn) 257static void callHook (StkId base, lua_Type type, int isreturn)
251{ 258{
252 struct C_Lua_Stack oldCLS = CLS_current; 259 struct C_Lua_Stack oldCLS = CLS_current;
253 StkId old_top = CLS_current.base = top-stack; 260 StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
254 CLS_current.num = 0; 261 CLS_current.num = 0;
255 if (isreturn) 262 if (isreturn)
256 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0); 263 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@@ -278,6 +285,7 @@ static StkId callC (lua_CFunction func, StkId base)
278 StkId firstResult; 285 StkId firstResult;
279 CLS_current.num = (top-stack) - base; 286 CLS_current.num = (top-stack) - base;
280 /* incorporate parameters on the stack */ 287 /* incorporate parameters on the stack */
288 CLS_current.lua2C = base;
281 CLS_current.base = base+CLS_current.num; /* == top-stack */ 289 CLS_current.base = base+CLS_current.num; /* == top-stack */
282 if (lua_callhook) 290 if (lua_callhook)
283 callHook(base, LUA_T_CMARK, 0); 291 callHook(base, LUA_T_CMARK, 0);
@@ -518,7 +526,7 @@ int lua_setlocal (lua_Function func, int local_number)
518{ 526{
519 TObject *f = Address(func); 527 TObject *f = Address(func);
520 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func)); 528 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
521 adjustC(1); 529 checkCparams(1);
522 --top; 530 --top;
523 if (name) 531 if (name)
524 { 532 {
@@ -537,9 +545,11 @@ int lua_setlocal (lua_Function func, int local_number)
537*/ 545*/
538static void do_callinc (int nResults) 546static void do_callinc (int nResults)
539{ 547{
540 do_call(CLS_current.base+1, nResults); 548 StkId base = CLS_current.base;
541 CLS_current.num = (top-stack) - CLS_current.base; /* number of results */ 549 do_call(base+1, nResults);
542 CLS_current.base += CLS_current.num; /* incorporate results on the stack */ 550 CLS_current.lua2C = base; /* position of the new results */
551 CLS_current.num = (top-stack) - base; /* number of results */
552 CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
543} 553}
544 554
545static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) 555static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
@@ -628,15 +638,6 @@ int lua_callfunction (lua_Object function)
628} 638}
629 639
630 640
631int lua_call (char *funcname)
632{
633 Word n = luaI_findsymbolbyname(funcname);
634 open_stack((top-stack)-CLS_current.base);
635 stack[CLS_current.base] = s_object(n);
636 return do_protectedrun(MULT_RET);
637}
638
639
640/* 641/*
641** Open file, generate opcode and execute global statement. Return 0 on 642** Open file, generate opcode and execute global statement. Return 0 on
642** success or non 0 on error. 643** success or non 0 on error.
@@ -791,9 +792,9 @@ lua_Object lua_createtable (void)
791lua_Object lua_lua2C (int number) 792lua_Object lua_lua2C (int number)
792{ 793{
793 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT; 794 if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
794 /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) == 795 /* Ref(stack+(CLS_current.lua2C+number-1)) ==
795 stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */ 796 stack+(CLS_current.lua2C+number-1)-stack+1 == */
796 return CLS_current.base-CLS_current.num+number; 797 return CLS_current.lua2C+number;
797} 798}
798 799
799int lua_isnil (lua_Object o) 800int lua_isnil (lua_Object o)
@@ -850,33 +851,20 @@ real lua_getnumber (lua_Object object)
850*/ 851*/
851char *lua_getstring (lua_Object object) 852char *lua_getstring (lua_Object object)
852{ 853{
853 if (object == LUA_NOOBJECT) return NULL; 854 if (object == LUA_NOOBJECT || tostring (Address(object)))
854 if (tostring (Address(object))) return NULL;
855 else return (svalue(Address(object)));
856}
857
858void *lua_getbindata (lua_Object object)
859{
860 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
861 return NULL; 855 return NULL;
862 else return svalue(Address(object)); 856 else return (svalue(Address(object)));
863}
864
865void *lua_getuserdata (lua_Object object)
866{
867 void *add = lua_getbindata(object);
868 if (add == NULL) return NULL;
869 else return *(void **)add;
870} 857}
871 858
872 859
873int lua_getbindatasize (lua_Object object) 860void *lua_getuserdata (lua_Object object)
874{ 861{
875 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) 862 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
876 return 0; 863 return NULL;
877 else return (Address(object))->value.ts->size; 864 else return tsvalue(Address(object))->u.v;
878} 865}
879 866
867
880/* 868/*
881** Given an object handle, return its cfuntion pointer. On error, return NULL. 869** Given an object handle, return its cfuntion pointer. On error, return NULL.
882*/ 870*/
@@ -1000,25 +988,14 @@ void lua_pushcfunction (lua_CFunction fn)
1000 incr_top; 988 incr_top;
1001} 989}
1002 990
1003void lua_pushbindata (void *buff, int size, int tag)
1004{
1005 if (buff == NULL)
1006 ttype(top) = LUA_T_NIL;
1007 else {
1008 if (tag < 0)
1009 luaI_realtag(tag);
1010 tsvalue(top) = luaI_createuserdata(buff, size, tag);
1011 ttype(top) = LUA_T_USERDATA;
1012 }
1013 incr_top;
1014}
1015 991
1016/* 992
1017** Push an object (ttype=userdata) to stack.
1018*/
1019void lua_pushusertag (void *u, int tag) 993void lua_pushusertag (void *u, int tag)
1020{ 994{
1021 lua_pushbindata(&u, sizeof(void *), tag); 995 if (tag < 0) luaI_realtag(tag); /* error if tag is not valid */
996 tsvalue(top) = luaI_createudata(u, tag);
997 ttype(top) = LUA_T_USERDATA;
998 incr_top;
1022} 999}
1023 1000
1024/* 1001/*
diff --git a/table.c b/table.c
index 1f65b411..b8b2ced7 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.69 1997/05/14 18:38:29 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.70 1997/05/26 14:42:51 roberto Exp roberto $";
7 7
8#include "luamem.h" 8#include "luamem.h"
9#include "auxlib.h" 9#include "auxlib.h"
@@ -59,17 +59,17 @@ void luaI_initconstant (void)
59*/ 59*/
60Word luaI_findsymbol (TaggedString *t) 60Word luaI_findsymbol (TaggedString *t)
61{ 61{
62 if (t->varindex == NOT_USED) 62 if (t->u.s.varindex == NOT_USED)
63 { 63 {
64 if (lua_ntable == lua_maxsymbol) 64 if (lua_ntable == lua_maxsymbol)
65 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol, 65 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
66 symbolEM, MAX_WORD); 66 symbolEM, MAX_WORD);
67 t->varindex = lua_ntable; 67 t->u.s.varindex = lua_ntable;
68 lua_table[lua_ntable].varname = t; 68 lua_table[lua_ntable].varname = t;
69 s_ttype(lua_ntable) = LUA_T_NIL; 69 s_ttype(lua_ntable) = LUA_T_NIL;
70 lua_ntable++; 70 lua_ntable++;
71 } 71 }
72 return t->varindex; 72 return t->u.s.varindex;
73} 73}
74 74
75 75
@@ -85,16 +85,16 @@ Word luaI_findsymbolbyname (char *name)
85*/ 85*/
86Word luaI_findconstant (TaggedString *t) 86Word luaI_findconstant (TaggedString *t)
87{ 87{
88 if (t->constindex == NOT_USED) 88 if (t->u.s.constindex == NOT_USED)
89 { 89 {
90 if (lua_nconstant == lua_maxconstant) 90 if (lua_nconstant == lua_maxconstant)
91 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *, 91 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
92 constantEM, MAX_WORD); 92 constantEM, MAX_WORD);
93 t->constindex = lua_nconstant; 93 t->u.s.constindex = lua_nconstant;
94 lua_constant[lua_nconstant] = t; 94 lua_constant[lua_nconstant] = t;
95 lua_nconstant++; 95 lua_nconstant++;
96 } 96 }
97 return t->constindex; 97 return t->u.s.constindex;
98} 98}
99 99
100 100
@@ -154,7 +154,7 @@ int luaI_ismarked (TObject *o)
154{ 154{
155 switch (o->ttype) 155 switch (o->ttype)
156 { 156 {
157 case LUA_T_STRING: 157 case LUA_T_STRING: case LUA_T_USERDATA:
158 return o->value.ts->marked; 158 return o->value.ts->marked;
159 case LUA_T_FUNCTION: 159 case LUA_T_FUNCTION:
160 return o->value.tf->marked; 160 return o->value.tf->marked;
@@ -196,6 +196,7 @@ long lua_collectgarbage (long limit)
196 TaggedString *freestr; 196 TaggedString *freestr;
197 TFunc *freefunc; 197 TFunc *freefunc;
198 markall(); 198 markall();
199 luaI_invalidaterefs();
199 freetable = luaI_hashcollector(&recovered); 200 freetable = luaI_hashcollector(&recovered);
200 freestr = luaI_strcollector(&recovered); 201 freestr = luaI_strcollector(&recovered);
201 freefunc = luaI_funccollector(&recovered); 202 freefunc = luaI_funccollector(&recovered);
diff --git a/tree.c b/tree.c
index ea2b2dec..4ed1641a 100644
--- a/tree.c
+++ b/tree.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.25 1997/05/05 20:21:23 roberto Exp roberto $"; 6char *rcs_tree="$Id: tree.c,v 1.26 1997/05/14 18:38:29 roberto Exp roberto $";
7 7
8 8
9#include <string.h> 9#include <string.h>
@@ -29,15 +29,21 @@ static int initialized = 0;
29 29
30static stringtable string_root[NUM_HASHS]; 30static stringtable string_root[NUM_HASHS];
31 31
32static TaggedString EMPTY = {LUA_T_STRING, NULL, 0, NOT_USED, NOT_USED, 32static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
33 0, 2, {0}}; 33 0, 2, {0}};
34 34
35 35
36static unsigned long hash (char *buff, long size) 36static unsigned long hash (char *s, int tag)
37{ 37{
38 unsigned long h = 0; 38 unsigned long h;
39 while (size--) 39 if (tag != LUA_T_STRING)
40 h = ((h<<5)-h)^(unsigned char)*(buff++); 40 h = (unsigned long)s;
41 else {
42 long size = strlen(s);
43 h = 0;
44 while (size--)
45 h = ((h<<5)-h)^(unsigned char)*(s++);
46 }
41 return h; 47 return h;
42} 48}
43 49
@@ -86,10 +92,30 @@ static void grow (stringtable *tb)
86 tb->hash = newhash; 92 tb->hash = newhash;
87} 93}
88 94
89static TaggedString *insert (char *buff, long size, int tag, stringtable *tb) 95
96static TaggedString *newone(char *buff, int tag, unsigned long h)
90{ 97{
91 TaggedString *ts; 98 TaggedString *ts;
92 unsigned long h = hash(buff, size); 99 if (tag == LUA_T_STRING) {
100 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
101 strcpy(ts->str, buff);
102 ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
103 ts->tag = LUA_T_STRING;
104 }
105 else {
106 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
107 ts->u.v = buff;
108 ts->tag = tag == LUA_ANYTAG ? 0 : tag;
109 }
110 ts->marked = 0;
111 ts->hash = h;
112 return ts;
113}
114
115static TaggedString *insert (char *buff, int tag, stringtable *tb)
116{
117 TaggedString *ts;
118 unsigned long h = hash(buff, tag);
93 int i; 119 int i;
94 int j = -1; 120 int j = -1;
95 if ((Long)tb->nuse*3 >= (Long)tb->size*2) 121 if ((Long)tb->nuse*3 >= (Long)tb->size*2)
@@ -103,8 +129,9 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
103 { 129 {
104 if (ts == &EMPTY) 130 if (ts == &EMPTY)
105 j = i; 131 j = i;
106 else if (ts->size == size && ts->tag == tag && 132 else if ((ts->tag == LUA_T_STRING) ?
107 memcmp(buff, ts->str, size) == 0) 133 (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
134 ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
108 return ts; 135 return ts;
109 i = (i+1)%tb->size; 136 i = (i+1)%tb->size;
110 } 137 }
@@ -114,24 +141,18 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
114 i = j; 141 i = j;
115 else 142 else
116 tb->nuse++; 143 tb->nuse++;
117 ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1); 144 ts = tb->hash[i] = newone(buff, tag, h);
118 memcpy(ts->str, buff, size);
119 ts->tag = tag;
120 ts->size = size;
121 ts->marked = 0;
122 ts->hash = h;
123 ts->varindex = ts->constindex = NOT_USED;
124 return ts; 145 return ts;
125} 146}
126 147
127TaggedString *luaI_createuserdata (char *buff, long size, int tag) 148TaggedString *luaI_createudata (void *udata, int tag)
128{ 149{
129 return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]); 150 return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
130} 151}
131 152
132TaggedString *lua_createstring (char *str) 153TaggedString *lua_createstring (char *str)
133{ 154{
134 return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING); 155 return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
135} 156}
136 157
137 158
diff --git a/tree.h b/tree.h
index 15b77b5f..0b501918 100644
--- a/tree.h
+++ b/tree.h
@@ -1,7 +1,7 @@
1/* 1/*
2** tree.h 2** tree.h
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** $Id: tree.h,v 1.16 1997/03/19 19:41:10 roberto Exp roberto $ 4** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef tree_h 7#ifndef tree_h
@@ -16,17 +16,21 @@ typedef struct TaggedString
16{ 16{
17 int tag; /* if != LUA_T_STRING, this is a userdata */ 17 int tag; /* if != LUA_T_STRING, this is a userdata */
18 struct TaggedString *next; 18 struct TaggedString *next;
19 long size; 19 union {
20 Word varindex; /* != NOT_USED if this is a symbol */ 20 struct {
21 Word constindex; /* != NOT_USED if this is a constant */ 21 Word varindex; /* != NOT_USED if this is a symbol */
22 Word constindex; /* != NOT_USED if this is a constant */
23 } s;
24 void *v; /* if this is a userdata, here is its value */
25 } u;
22 unsigned long hash; /* 0 if not initialized */ 26 unsigned long hash; /* 0 if not initialized */
23 int marked; /* for garbage collection; never collect (nor change) if > 1 */ 27 int marked; /* for garbage collection; never collect (nor change) if > 1 */
24 char str[1]; /* \0 byte already reserved; MAY BE NOT 0 TERMINATED!! */ 28 char str[1]; /* \0 byte already reserved */
25} TaggedString; 29} TaggedString;
26 30
27 31
28TaggedString *lua_createstring (char *str); 32TaggedString *lua_createstring (char *str);
29TaggedString *luaI_createuserdata (char *buff, long size, int tag); 33TaggedString *luaI_createudata (void *udata, int tag);
30TaggedString *luaI_strcollector (long *cont); 34TaggedString *luaI_strcollector (long *cont);
31void luaI_strfree (TaggedString *l); 35void luaI_strfree (TaggedString *l);
32void luaI_strcallIM (TaggedString *l); 36void luaI_strcallIM (TaggedString *l);