History

Lua 3.0

Lua Team committed 29 years ago

Commit 9bc113c1 parent 100a7f178 files · +6569 −11586Browse files

Showing the changes to src (33 of 78 files). Show all

Unified · Split

33 files
  1. src/Makefile +12 −6
  2. src/auxlib.c +81 −0
  3. src/auxlib.h +30 −0
  4. src/fallback.c +286 −109
  5. src/fallback.h +50 −22
  6. src/func.c +26 −16
  7. src/func.h +3 −2
  8. src/hash.c +147 −134
  9. src/hash.h +17 −16
  10. src/inout.c +278 −170
  11. src/inout.h +8 −17
  12. src/lex.c +174 −45
  13. src/lex.h +3 −4
  14. src/lua.stx +29 −34
  15. src/luac/dump.c +11 −19
  16. src/luac/luac.c +112 −27
  17. src/luac/luac.h +6 −5
  18. src/luac/print.c +213 −20
  19. src/luac/print.h +6 −3
  20. src/luamem.c +159 −0
  21. src/luamem.h +3 −3
  22. src/mem.c +0 −58
  23. src/opcode.c +517 −369
  24. src/opcode.h +46 −25
  25. src/parser.c +344 −316
  26. src/table.c +84 −96
  27. src/table.h +6 −5
  28. src/tree.c +92 −26
  29. src/tree.h +14 −4
  30. src/undump.c +71 −74
  31. src/undump.h +11 −4
  32. src/zio.c +79 −0
  33. src/zio.h +48 −0
src/Makefile+12−6
@@ -4,19 +4,23 @@ LUA= .. · 3 unmodified lines
44
55 include $(LUA)/config
66
7−OBJS= fallback.o\
7+OBJS= auxlib.o\
8+ fallback.o\
89 func.o\
910 hash.o\
1011 inout.o\
1112 lex.o\
12− mem.o\
13+ luamem.o\
1314 opcode.o\
1415 parser.o\
1516 table.o\
1617 tree.o\
17− undump.o
18+ undump.o\
19+ zio.o
1820
19−SRCS= fallback.c\
21+SRCS= auxlib.c\
22+ auxlib.h\
23+ fallback.c\
2024 fallback.h\
2125 func.c\
2226 func.h\
@@ -26,8 +30,8 @@ SRCS= fallback.c\ · 3 unmodified lines
2630 inout.h\
2731 lex.c\
2832 lex.h\
29− mem.c\
30− mem.h\
33+ luamem.c\
34+ luamem.h\
3135 opcode.c\
3236 opcode.h\
3337 parser.c\
@@ -39,6 +43,8 @@ SRCS= fallback.c\ · 5 unmodified lines
3943 types.h\
4044 undump.c\
4145 undump.h\
46+ zio.c\
47+ zio.h\
4248 lua.stx
4349
4450 SLIB= $(LIB)/liblua.a
src/auxlib.c+81−0
@@ -0,0 +1,81 @@
1+char *rcs_auxlib="$Id: auxlib.c,v 1.5 1997/04/14 15:30:03 roberto Exp $";
2+
3+#include <stdio.h>
4+#include <stdarg.h>
5+#include <string.h>
6+
7+#include "lua.h"
8+#include "auxlib.h"
9+#include "luadebug.h"
10+
11+
12+
13+int luaI_findstring (char *name, char *list[])
14+{
15+ int i;
16+ for (i=0; list[i]; i++)
17+ if (strcmp(list[i], name) == 0)
18+ return i;
19+ return -1; /* name not found */
20+}
21+
22+
23+void luaL_arg_check(int cond, int numarg, char *extramsg)
24+{
25+ if (!cond) {
26+ char *funcname;
27+ lua_getobjname(lua_stackedfunction(0), &funcname);
28+ if (funcname == NULL)
29+ funcname = "???";
30+ if (extramsg == NULL)
31+ luaL_verror("bad argument #%d to function `%s'", numarg, funcname);
32+ else
33+ luaL_verror("bad argument #%d to function `%s' (%s)",
34+ numarg, funcname, extramsg);
35+ }
36+}
37+
38+char *luaL_check_string (int numArg)
39+{
40+ lua_Object o = lua_getparam(numArg);
41+ luaL_arg_check(lua_isstring(o), numArg, "string expected");
42+ return lua_getstring(o);
43+}
44+
45+char *luaL_opt_string (int numArg, char *def)
46+{
47+ return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
48+ luaL_check_string(numArg);
49+}
50+
51+double luaL_check_number (int numArg)
52+{
53+ lua_Object o = lua_getparam(numArg);
54+ luaL_arg_check(lua_isnumber(o), numArg, "number expected");
55+ return lua_getnumber(o);
56+}
57+
58+
59+double luaL_opt_number (int numArg, double def)
60+{
61+ return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
62+ luaL_check_number(numArg);
63+}
64+
65+void luaL_openlib (struct luaL_reg *l, int n)
66+{
67+ int i;
68+ for (i=0; i<n; i++)
69+ lua_register(l[i].name, l[i].func);
70+}
71+
72+
73+void luaL_verror (char *fmt, ...)
74+{
75+ char buff[1000];
76+ va_list argp;
77+ va_start(argp, fmt);
78+ vsprintf(buff, fmt, argp);
79+ va_end(argp);
80+ lua_error(buff);
81+}
src/auxlib.h+30−0
@@ -0,0 +1,30 @@
1+/*
2+** $Id: auxlib.h,v 1.3 1997/04/07 14:48:53 roberto Exp $
3+*/
4+
5+#ifndef auxlib_h
6+#define auxlib_h
7+
8+#include "lua.h"
9+
10+struct luaL_reg {
11+ char *name;
12+ lua_CFunction func;
13+};
14+
15+void luaL_openlib (struct luaL_reg *l, int n);
16+void luaL_arg_check(int cond, int numarg, char *extramsg);
17+char *luaL_check_string (int numArg);
18+char *luaL_opt_string (int numArg, char *def);
19+double luaL_check_number (int numArg);
20+double luaL_opt_number (int numArg, double def);
21+void luaL_verror (char *fmt, ...);
22+
23+
24+
25+/* -- private part (only for Lua modules */
26+
27+int luaI_findstring (char *name, char *list[]);
28+
29+
30+#endif
src/fallback.c+286−109
@@ -3,130 +3,37 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_fallback="$Id: fallback.c,v 1.25 1996/04/25 14:10:00 roberto Exp $";
6+char *rcs_fallback="$Id: fallback.c,v 2.9 1997/06/23 18:27:53 roberto Exp $";
77
88 #include <stdio.h>
99 #include <string.h>
1010
11−#include "mem.h"
11+#include "auxlib.h"
12+#include "luamem.h"
1213 #include "fallback.h"
1314 #include "opcode.h"
1415 #include "lua.h"
1516 #include "table.h"
17+#include "tree.h"
18+#include "hash.h"
1619
1720
18−static void errorFB (void);
19−static void indexFB (void);
20−static void gettableFB (void);
21−static void arithFB (void);
22−static void concatFB (void);
23−static void orderFB (void);
24−static void GDFB (void);
25−static void funcFB (void);
2621
27−
28−/*
29−** Warning: This list must be in the same order as the #define's
30−*/
31−struct FB luaI_fallBacks[] = {
32−{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
33−{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
34−{"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
35−{"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
36−{"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
37−{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
38−{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
39−{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
40−{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
41− /* no fixed number of params or results */
42−{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
43− /* same default behavior of index FB */
44−};
45−
46−#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
47−
48−void luaI_setfallback (void)
49−{
50− int i;
51− char *name = lua_getstring(lua_getparam(1));
52− lua_Object func = lua_getparam(2);
53− if (name == NULL || !lua_isfunction(func))
54− lua_error("incorrect argument to function `setfallback'");
55− for (i=0; i<N_FB; i++)
56− {
57− if (strcmp(luaI_fallBacks[i].kind, name) == 0)
58− {
59− luaI_pushobject(&luaI_fallBacks[i].function);
60− luaI_fallBacks[i].function = *luaI_Address(func);
61− return;
62− }
63− }
64− /* name not found */
65− lua_error("incorrect argument to function `setfallback'");
66−}
67−
68−
69−static void errorFB (void)
70−{
71− lua_Object o = lua_getparam(1);
72− if (lua_isstring(o))
73− fprintf (stderr, "lua: %s\n", lua_getstring(o));
74− else
75− fprintf(stderr, "lua: unknown error\n");
76−}
77−
78−
79−static void indexFB (void)
80−{
81− lua_pushnil();
82−}
83−
84−
85−static void gettableFB (void)
86−{
87− lua_error("indexed expression not a table");
88−}
89−
90−
91−static void arithFB (void)
92−{
93− lua_error("unexpected type at conversion to number");
94−}
95−
96−static void concatFB (void)
97−{
98− lua_error("unexpected type at conversion to string");
99−}
100−
101−
102−static void orderFB (void)
103−{
104− lua_error("unexpected type at comparison");
105−}
106−
107−static void GDFB (void) { }
108−
109−static void funcFB (void)
110−{
111− lua_error("call expression not a function");
112−}
113−
114−
115−/*
22+/* -------------------------------------------
11623 ** Reference routines
11724 */
11825
11926 static struct ref {
120− Object o;
27+ TObject o;
12128 enum {LOCK, HOLD, FREE, COLLECTED} status;
12229 } *refArray = NULL;
12330 static int refSize = 0;
12431
125−int luaI_ref (Object *object, int lock)
32+int luaI_ref (TObject *object, int lock)
12633 {
12734 int i;
12835 int oldSize;
129− if (tag(object) == LUA_T_NIL)
36+ if (ttype(object) == LUA_T_NIL)
13037 return -1; /* special ref for nil */
13138 for (i=0; i<refSize; i++)
13239 if (refArray[i].status == FREE)
@@ -151,9 +58,9 @@ void lua_unref (int ref) · 18 unmodified lines
15158 }
15259
15360
154−Object *luaI_getref (int ref)
61+TObject *luaI_getref (int ref)
15562 {
156− static Object nul = {LUA_T_NIL, {0}};
63+ static TObject nul = {LUA_T_NIL, {0}};
15764 if (ref == -1)
15865 return &nul;
15966 if (ref >= 0 && ref < refSize &&
@@ -164,7 +71,7 @@ Object *luaI_getref (int ref) · 4 unmodified lines
16471 }
16572
16673
167−void luaI_travlock (int (*fn)(Object *))
74+void luaI_travlock (int (*fn)(TObject *))
16875 {
16976 int i;
17077 for (i=0; i<refSize; i++)
@@ -181,11 +88,281 @@ void luaI_invalidaterefs (void) · 10 unmodified lines
18188 refArray[i].status = COLLECTED;
18289 }
18390
184−char *luaI_travfallbacks (int (*fn)(Object *))
91+
92+/* -------------------------------------------
93+* Internal Methods
94+*/
95+
96+char *luaI_eventname[] = { /* ORDER IM */
97+ "gettable", "settable", "index", "getglobal", "setglobal", "add",
98+ "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
99+ "concat", "gc", "function",
100+ NULL
101+};
102+
103+
104+
105+static int luaI_checkevent (char *name, char *list[])
106+{
107+ int e = luaI_findstring(name, list);
108+ if (e < 0)
109+ luaL_verror("`%s' is not a valid event name", name);
110+ return e;
111+}
112+
113+
114+struct IM *luaI_IMtable = NULL;
115+
116+static int IMtable_size = 0;
117+static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
118+
119+
120+/* events in LUA_T_LINE are all allowed, since this is used as a
121+* 'placeholder' for "default" fallbacks
122+*/
123+static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
124+{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
125+{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
126+{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
127+{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
128+{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
129+{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
130+{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
131+{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
132+{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
133+{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
134+};
135+
136+static int validevent (lua_Type t, int e)
137+{ /* ORDER LUA_T */
138+ return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
139+}
140+
141+
142+static void init_entry (int tag)
185143 {
186144 int i;
187− for (i=0; i<N_FB; i++)
188− if (fn(&luaI_fallBacks[i].function))
189− return luaI_fallBacks[i].kind;
145+ for (i=0; i<IM_N; i++)
146+ ttype(luaI_getim(tag, i)) = LUA_T_NIL;
147+}
148+
149+void luaI_initfallbacks (void)
150+{
151+ if (luaI_IMtable == NULL) {
152+ int i;
153+ IMtable_size = NUM_TYPES+10;
154+ luaI_IMtable = newvector(IMtable_size, struct IM);
155+ for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
156+ init_entry(i);
157+ }
158+}
159+
160+int lua_newtag (void)
161+{
162+ --last_tag;
163+ if ((-last_tag) >= IMtable_size) {
164+ luaI_initfallbacks();
165+ IMtable_size = growvector(&luaI_IMtable, IMtable_size,
166+ struct IM, memEM, MAX_INT);
167+ }
168+ init_entry(last_tag);
169+ return last_tag;
170+}
171+
172+
173+static void checktag (int tag)
174+{
175+ if (!(last_tag <= tag && tag <= 0))
176+ luaL_verror("%d is not a valid tag", tag);
177+}
178+
179+void luaI_realtag (int tag)
180+{
181+ if (!(last_tag <= tag && tag < LUA_T_NIL))
182+ luaL_verror("tag %d is not result of `newtag'", tag);
183+}
184+
185+
186+void luaI_settag (int tag, TObject *o)
187+{
188+ luaI_realtag(tag);
189+ switch (ttype(o)) {
190+ case LUA_T_ARRAY:
191+ o->value.a->htag = tag;
192+ break;
193+ case LUA_T_USERDATA:
194+ o->value.ts->tag = tag;
195+ break;
196+ default:
197+ luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
198+ }
199+}
200+
201+
202+int luaI_efectivetag (TObject *o)
203+{
204+ lua_Type t = ttype(o);
205+ if (t == LUA_T_USERDATA) {
206+ int tag = o->value.ts->tag;
207+ return (tag >= 0) ? LUA_T_USERDATA : tag;
208+ }
209+ else if (t == LUA_T_ARRAY)
210+ return o->value.a->htag;
211+ else return t;
212+}
213+
214+
215+void luaI_gettagmethod (void)
216+{
217+ int t = (int)luaL_check_number(1);
218+ int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
219+ checktag(t);
220+ if (validevent(t, e))
221+ luaI_pushobject(luaI_getim(t,e));
222+}
223+
224+
225+void luaI_settagmethod (void)
226+{
227+ int t = (int)luaL_check_number(1);
228+ int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
229+ lua_Object func = lua_getparam(3);
230+ checktag(t);
231+ if (!validevent(t, e))
232+ luaL_verror("cannot change internal method `%s' for tag %d",
233+ luaI_eventname[e], t);
234+ luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
235+ 3, "function expected");
236+ luaI_pushobject(luaI_getim(t,e));
237+ *luaI_getim(t, e) = *luaI_Address(func);
238+}
239+
240+
241+static void stderrorim (void)
242+{
243+ lua_Object s = lua_getparam(1);
244+ if (lua_isstring(s))
245+ fprintf(stderr, "lua: %s\n", lua_getstring(s));
246+}
247+
248+static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
249+
250+
251+TObject *luaI_geterrorim (void)
252+{
253+ return &errorim;
254+}
255+
256+void luaI_seterrormethod (void)
257+{
258+ lua_Object func = lua_getparam(1);
259+ luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
260+ 1, "function expected");
261+ luaI_pushobject(&errorim);
262+ errorim = *luaI_Address(func);
263+}
264+
265+char *luaI_travfallbacks (int (*fn)(TObject *))
266+{
267+ int e;
268+ if (fn(&errorim))
269+ return "error";
270+ for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
271+ int t;
272+ for (t=0; t>=last_tag; t--)
273+ if (fn(luaI_getim(t,e)))
274+ return luaI_eventname[e];
275+ }
190276 return NULL;
191277 }
278+
279+
280+/*
281+* ===================================================================
282+* compatibility with old fallback system
283+*/
284+#if LUA_COMPAT2_5
285+
286+static void errorFB (void)
287+{
288+ lua_Object o = lua_getparam(1);
289+ if (lua_isstring(o))
290+ fprintf (stderr, "lua: %s\n", lua_getstring(o));
291+ else
292+ fprintf(stderr, "lua: unknown error\n");
293+}
294+
295+
296+static void nilFB (void) { }
297+
298+
299+static void typeFB (void)
300+{
301+ lua_error("unexpected type");
302+}
303+
304+
305+static void fillvalids (IMS e, TObject *func)
306+{
307+ int t;
308+ for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
309+ if (validevent(t, e))
310+ *luaI_getim(t, e) = *func;
311+}
312+
313+
314+void luaI_setfallback (void)
315+{
316+ static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
317+ TObject oldfunc;
318+ lua_CFunction replace;
319+ char *name = luaL_check_string(1);
320+ lua_Object func = lua_getparam(2);
321+ luaI_initfallbacks();
322+ luaL_arg_check(lua_isfunction(func), 2, "function expected");
323+ switch (luaI_findstring(name, oldnames)) {
324+ case 0: /* old error fallback */
325+ oldfunc = errorim;
326+ errorim = *luaI_Address(func);
327+ replace = errorFB;
328+ break;
329+ case 1: /* old getglobal fallback */
330+ oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
331+ *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
332+ replace = nilFB;
333+ break;
334+ case 2: { /* old arith fallback */
335+ int i;
336+ oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
337+ for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
338+ fillvalids(i, luaI_Address(func));
339+ replace = typeFB;
340+ break;
341+ }
342+ case 3: { /* old order fallback */
343+ int i;
344+ oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
345+ for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
346+ fillvalids(i, luaI_Address(func));
347+ replace = typeFB;
348+ break;
349+ }
350+ default: {
351+ int e;
352+ if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
353+ oldfunc = *luaI_getim(LUA_T_LINE, e);
354+ fillvalids(e, luaI_Address(func));
355+ replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
356+ }
357+ else {
358+ luaL_verror("`%s' is not a valid fallback name", name);
359+ replace = NULL; /* to avoid warnings */
360+ }
361+ }
362+ }
363+ if (oldfunc.ttype != LUA_T_NIL)
364+ luaI_pushobject(&oldfunc);
365+ else
366+ lua_pushcfunction(replace);
367+}
368+#endif
src/fallback.h+50−22
@@ -1,5 +1,5 @@
11 /*
2−** $Id: fallback.h,v 1.13 1996/04/25 14:10:00 roberto Exp $
2+** $Id: fallback.h,v 1.23 1997/04/24 22:59:57 roberto Exp $
33 */
44
55 #ifndef fallback_h
@@ -8,30 +8,58 @@ · 2 unmodified lines
88 #include "lua.h"
99 #include "opcode.h"
1010
11−extern struct FB {
12− char *kind;
13− Object function;
14− int nParams;
15− int nResults;
16−} luaI_fallBacks[];
17−
18−#define FB_ERROR 0
19−#define FB_INDEX 1
20−#define FB_GETTABLE 2
21−#define FB_ARITH 3
22−#define FB_ORDER 4
23−#define FB_CONCAT 5
24−#define FB_SETTABLE 6
25−#define FB_GC 7
26−#define FB_FUNCTION 8
27−#define FB_GETGLOBAL 9
11+/*
12+* WARNING: if you change the order of this enumeration,
13+* grep "ORDER IM"
14+*/
15+typedef enum {
16+ IM_GETTABLE = 0,
17+ IM_SETTABLE,
18+ IM_INDEX,
19+ IM_GETGLOBAL,
20+ IM_SETGLOBAL,
21+ IM_ADD,
22+ IM_SUB,
23+ IM_MUL,
24+ IM_DIV,
25+ IM_POW,
26+ IM_UNM,
27+ IM_LT,
28+ IM_LE,
29+ IM_GT,
30+ IM_GE,
31+ IM_CONCAT,
32+ IM_GC,
33+ IM_FUNCTION
34+} IMS;
35+
36+#define IM_N 18
37+
38+
39+extern struct IM {
40+ TObject int_method[IM_N];
41+} *luaI_IMtable;
42+
43+extern char *luaI_eventname[];
44+
45+#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
46+#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
2847
2948 void luaI_setfallback (void);
30−int luaI_ref (Object *object, int lock);
31−Object *luaI_getref (int ref);
32−void luaI_travlock (int (*fn)(Object *));
49+int luaI_ref (TObject *object, int lock);
50+TObject *luaI_getref (int ref);
51+void luaI_travlock (int (*fn)(TObject *));
3352 void luaI_invalidaterefs (void);
34−char *luaI_travfallbacks (int (*fn)(Object *));
53+char *luaI_travfallbacks (int (*fn)(TObject *));
54+
55+void luaI_settag (int tag, TObject *o);
56+void luaI_realtag (int tag);
57+TObject *luaI_geterrorim (void);
58+int luaI_efectivetag (TObject *o);
59+void luaI_settagmethod (void);
60+void luaI_gettagmethod (void);
61+void luaI_seterrormethod (void);
62+void luaI_initfallbacks (void);
3563
3664 #endif
3765
src/func.c+26−16
@@ -2,9 +2,10 @@ · 1 unmodified line
22
33 #include "luadebug.h"
44 #include "table.h"
5−#include "mem.h"
5+#include "luamem.h"
66 #include "func.h"
77 #include "opcode.h"
8+#include "inout.h"
89
910
1011 static TFunc *function_root = NULL;
@@ -23,7 +24,7 @@ void luaI_initTFunc (TFunc *f) · 12 unmodified lines
2324 f->size = 0;
2425 f->code = NULL;
2526 f->lineDefined = 0;
26− f->fileName = NULL;
27+ f->fileName = lua_parsedfile;
2728 f->locvars = NULL;
2829 }
2930
@@ -49,47 +50,56 @@ void luaI_freefunc (TFunc *f) · 19 unmodified lines
4950 luaI_free (f);
5051 }
5152
53+
54+void luaI_funcfree (TFunc *l)
55+{
56+ while (l) {
57+ TFunc *next = l->next;
58+ luaI_freefunc(l);
59+ l = next;
60+ }
61+}
62+
5263 /*
5364 ** Garbage collection function.
54−** This function traverse the function list freeing unindexed functions
5565 */
56−Long luaI_funccollector (void)
66+TFunc *luaI_funccollector (long *acum)
5767 {
5868 TFunc *curr = function_root;
5969 TFunc *prev = NULL;
60− Long counter = 0;
61− while (curr)
62− {
70+ TFunc *frees = NULL;
71+ long counter = 0;
72+ while (curr) {
6373 TFunc *next = curr->next;
64− if (!curr->marked)
65− {
74+ if (!curr->marked) {
6675 if (prev == NULL)
6776 function_root = next;
6877 else
6978 prev->next = next;
70− luaI_freefunc (curr);
79+ curr->next = frees;
80+ frees = curr;
7181 ++counter;
7282 }
73− else
74− {
83+ else {
7584 curr->marked = 0;
7685 prev = curr;
7786 }
7887 curr = next;
7988 }
80− return counter;
89+ *acum += counter;
90+ return frees;
8191 }
8292
8393
8494 void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
8595 {
86− Object *f = luaI_Address(func);
87− if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
96+ TObject *f = luaI_Address(func);
97+ if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
8898 {
8999 *filename = f->value.tf->fileName;
90100 *linedefined = f->value.tf->lineDefined;
91101 }
92− else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)
102+ else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
93103 {
94104 *filename = "(C)";
95105 *linedefined = -1;
src/func.h+3−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp $
2+** $Id: func.h,v 1.9 1997/05/14 18:38:29 roberto Exp $
33 */
44
55 #ifndef func_h
@@ -30,7 +30,8 @@ typedef struct TFunc · 24 unmodified lines
3030 LocVar *locvars;
3131 } TFunc;
3232
33−Long luaI_funccollector (void);
33+TFunc *luaI_funccollector (long *cont);
34+void luaI_funcfree (TFunc *l);
3435 void luaI_insertfunction (TFunc *f);
3536
3637 void luaI_initTFunc (TFunc *f);
src/hash.c+147−134
@@ -3,14 +3,15 @@ · 2 unmodified lines
33 ** hash manager for lua
44 */
55
6−char *rcs_hash="$Id: hash.c,v 2.32 1996/11/18 13:48:44 roberto Exp $";
6+char *rcs_hash="$Id: hash.c,v 2.43 1997/05/14 18:38:29 roberto Exp $";
77
88
9−#include "mem.h"
9+#include "luamem.h"
1010 #include "opcode.h"
1111 #include "hash.h"
1212 #include "table.h"
1313 #include "lua.h"
14+#include "auxlib.h"
1415
1516
1617 #define nhash(t) ((t)->nhash)
@@ -24,13 +25,15 @@ char *rcs_hash="$Id: hash.c,v 2.32 1996/11/18 13:48:44 roberto Exp $"; · 7 unmodified lines
2425
2526 #define REHASH_LIMIT 0.70 /* avoid more than this % full */
2627
28+#define TagDefault LUA_T_ARRAY;
29+
2730
2831 static Hash *listhead = NULL;
2932
3033
3134 /* hash dimensions values */
3235 static Long dimensions[] =
33− {3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
36+ {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
3437 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
3538 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
3639
@@ -46,16 +49,32 @@ int luaI_redimension (int nhash) · 9 unmodified lines
4649 return 0; /* to avoid warnings */
4750 }
4851
49−static int hashindex (Hash *t, Object *ref) /* hash function */
52+
53+int lua_equalObj (TObject *t1, TObject *t2)
54+{
55+ if (ttype(t1) != ttype(t2)) return 0;
56+ switch (ttype(t1))
57+ {
58+ case LUA_T_NIL: return 1;
59+ case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
60+ case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
61+ case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
62+ case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
63+ case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
64+ default:
65+ lua_error("internal error in `lua_equalObj'");
66+ return 0; /* UNREACHEABLE */
67+ }
68+}
69+
70+
71+static long int hashindex (TObject *ref)
5072 {
5173 long int h;
52− switch (tag(ref)) {
53− case LUA_T_NIL:
54− lua_error ("unexpected type to index table");
55− h = 0; /* UNREACHEABLE */
74+ switch (ttype(ref)) {
5675 case LUA_T_NUMBER:
5776 h = (long int)nvalue(ref); break;
58− case LUA_T_STRING:
77+ case LUA_T_STRING: case LUA_T_USERDATA:
5978 h = tsvalue(ref)->hash; break;
6079 case LUA_T_FUNCTION:
6180 h = (IntPoint)ref->value.tf; break;
@@ -63,38 +82,29 @@ static int hashindex (Hash *t, Object *ref) /* hash function */ · 1 unmodified line
6382 h = (IntPoint)fvalue(ref); break;
6483 case LUA_T_ARRAY:
6584 h = (IntPoint)avalue(ref); break;
66− default: /* user data */
67− h = (IntPoint)uvalue(ref); break;
85+ default:
86+ lua_error ("unexpected type to index table");
87+ h = 0; /* UNREACHEABLE */
6888 }
6989 if (h < 0) h = -h;
70− return h%nhash(t); /* make it a valid index */
90+ return h;
7191 }
7292
73−int lua_equalObj (Object *t1, Object *t2)
93+
94+static int present (Hash *t, TObject *key)
7495 {
75− if (tag(t1) != tag(t2)) return 0;
76− switch (tag(t1))
77− {
78− case LUA_T_NIL: return 1;
79− case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
80− case LUA_T_STRING: return svalue(t1) == svalue(t2);
81− case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
82− case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
83− case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
84− default: return uvalue(t1) == uvalue(t2);
96+ long int h = hashindex(key);
97+ int tsize = nhash(t);
98+ int h1 = h%tsize;
99+ TObject *rf = ref(node(t, h1));
100+ if (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf)) {
101+ int h2 = h%(tsize-2) + 1;
102+ do {
103+ h1 = (h1+h2)%tsize;
104+ rf = ref(node(t, h1));
105+ } while (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf));
85106 }
86−}
87−
88−static int present (Hash *t, Object *ref)
89−{
90− int h = hashindex(t, ref);
91− while (tag(ref(node(t, h))) != LUA_T_NIL)
92− {
93− if (lua_equalObj(ref, ref(node(t, h))))
94− return h;
95− h = (h+1) % nhash(t);
96− }
97− return h;
107+ return h1;
98108 }
99109
100110
@@ -106,7 +116,7 @@ static Node *hashnodecreate (int nhash) · 5 unmodified lines
106116 int i;
107117 Node *v = newvector (nhash, Node);
108118 for (i=0; i<nhash; i++)
109− tag(ref(&v[i])) = LUA_T_NIL;
119+ ttype(ref(&v[i])) = LUA_T_NIL;
110120 return v;
111121 }
112122
@@ -121,6 +131,7 @@ static Hash *hashcreate (int nhash) · 8 unmodified lines
121131 nhash(t) = nhash;
122132 nuse(t) = 0;
123133 markarray(t) = 0;
134+ t->htag = TagDefault;
124135 return t;
125136 }
126137
@@ -146,7 +157,7 @@ void lua_hashmark (Hash *h) · 19 unmodified lines
146157 for (i=0; i<nhash(h); i++)
147158 {
148159 Node *n = node(h,i);
149− if (tag(ref(n)) != LUA_T_NIL)
160+ if (ttype(ref(n)) != LUA_T_NIL)
150161 {
151162 lua_markobject(&n->ref);
152163 lua_markobject(&n->val);
@@ -156,49 +167,50 @@ void lua_hashmark (Hash *h) · 3 unmodified lines
156167 }
157168
158169
159−static void call_fallbacks (void)
170+void luaI_hashcallIM (Hash *l)
160171 {
161− Hash *curr_array;
162− Object t;
163− tag(&t) = LUA_T_ARRAY;
164− for (curr_array = listhead; curr_array; curr_array = curr_array->next)
165− if (markarray(curr_array) != 1)
166− {
167− avalue(&t) = curr_array;
168− luaI_gcFB(&t);
169− }
170− tag(&t) = LUA_T_NIL;
171− luaI_gcFB(&t); /* end of list */
172+ TObject t;
173+ ttype(&t) = LUA_T_ARRAY;
174+ for (; l; l=l->next) {
175+ avalue(&t) = l;
176+ luaI_gcIM(&t);
177+ }
172178 }
173179
174−
175−/*
176−** Garbage collection to arrays
177−** Delete all unmarked arrays.
178−*/
179−Long lua_hashcollector (void)
180+
181+void luaI_hashfree (Hash *frees)
180182 {
181− Hash *curr_array = listhead, *prev = NULL;
182− Long counter = 0;
183− call_fallbacks();
184− while (curr_array != NULL)
185− {
186− Hash *next = curr_array->next;
187− if (markarray(curr_array) != 1)
188− {
189− if (prev == NULL) listhead = next;
190− else prev->next = next;
191− hashdelete(curr_array);
192− ++counter;
183+ while (frees) {
184+ Hash *next = frees->next;
185+ hashdelete(frees);
186+ frees = next;
193187 }
194− else
195− {
196− markarray(curr_array) = 0;
197− prev = curr_array;
188+}
189+
190+
191+Hash *luaI_hashcollector (long *acum)
192+{
193+ Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
194+ long counter = 0;
195+ while (curr_array != NULL) {
196+ Hash *next = curr_array->next;
197+ if (markarray(curr_array) != 1) {
198+ if (prev == NULL)
199+ listhead = next;
200+ else
201+ prev->next = next;
202+ curr_array->next = frees;
203+ frees = curr_array;
204+ ++counter;
205+ }
206+ else {
207+ markarray(curr_array) = 0;
208+ prev = curr_array;
209+ }
210+ curr_array = next;
198211 }
199− curr_array = next;
200− }
201− return counter;
212+ *acum += counter;
213+ return frees;
202214 }
203215
204216
@@ -220,32 +232,45 @@ Hash *lua_createarray (int nhash) · 15 unmodified lines
220232
221233
222234 /*
223−** Re-hash
235+** Rehash:
236+** Check if table has deleted slots. It it has, it does not need to
237+** grow, since rehash will reuse them.
224238 */
239+static int emptyslots (Hash *t)
240+{
241+ int i;
242+ for (i=nhash(t)-1; i>=0; i--) {
243+ Node *n = node(t, i);
244+ if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
245+ return 1;
246+ }
247+ return 0;
248+}
249+
225250 static void rehash (Hash *t)
226251 {
227− int i;
228− int nold = nhash(t);
229− Node *vold = nodevector(t);
230− nhash(t) = luaI_redimension(nhash(t));
231− nodevector(t) = hashnodecreate(nhash(t));
232− for (i=0; i<nold; i++)
233− {
234− Node *n = vold+i;
235− if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
236− *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
237− }
238− luaI_free(vold);
252+ int nold = nhash(t);
253+ Node *vold = nodevector(t);
254+ int i;
255+ if (!emptyslots(t))
256+ nhash(t) = luaI_redimension(nhash(t));
257+ nodevector(t) = hashnodecreate(nhash(t));
258+ for (i=0; i<nold; i++) {
259+ Node *n = vold+i;
260+ if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
261+ *node(t, present(t, ref(n))) = *n; /* copy old node to new hash */
262+ }
263+ luaI_free(vold);
239264 }
240265
241266 /*
242267 ** If the hash node is present, return its pointer, otherwise return
243268 ** null.
244269 */
245−Object *lua_hashget (Hash *t, Object *ref)
270+TObject *lua_hashget (Hash *t, TObject *ref)
246271 {
247272 int h = present(t, ref);
248− if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
273+ if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
249274 else return NULL;
250275 }
251276
@@ -254,25 +279,19 @@ Object *lua_hashget (Hash *t, Object *ref) · 2 unmodified lines
254279 ** If the hash node is present, return its pointer, otherwise create a new
255280 ** node for the given reference and also return its pointer.
256281 */
257−Object *lua_hashdefine (Hash *t, Object *ref)
282+TObject *lua_hashdefine (Hash *t, TObject *ref)
258283 {
259− int h;
260− Node *n;
261− h = present(t, ref);
262− n = node(t, h);
263− if (tag(ref(n)) == LUA_T_NIL)
264− {
265− nuse(t)++;
266− if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
267− {
268− rehash(t);
269− h = present(t, ref);
270− n = node(t, h);
284+ Node *n = node(t, present(t, ref));
285+ if (ttype(ref(n)) == LUA_T_NIL) {
286+ nuse(t)++;
287+ if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
288+ rehash(t);
289+ n = node(t, present(t, ref));
290+ }
291+ *ref(n) = *ref;
292+ ttype(val(n)) = LUA_T_NIL;
271293 }
272− *ref(n) = *ref;
273− tag(val(n)) = LUA_T_NIL;
274− }
275− return (val(n));
294+ return (val(n));
276295 }
277296
278297
@@ -284,36 +303,30 @@ Object *lua_hashdefine (Hash *t, Object *ref) · 5 unmodified lines
284303 */
285304 static void hashnext (Hash *t, int i)
286305 {
287− if (i >= nhash(t))
288− return;
289− while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)
290− {
291− if (++i >= nhash(t))
292− return;
293− }
294− luaI_pushobject(ref(node(t,i)));
295− luaI_pushobject(val(node(t,i)));
306+ Node *n;
307+ int tsize = nhash(t);
308+ if (i >= tsize)
309+ return;
310+ n = node(t, i);
311+ while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
312+ if (++i >= tsize)
313+ return;
314+ n = node(t, i);
315+ }
316+ luaI_pushobject(ref(node(t,i)));
317+ luaI_pushobject(val(node(t,i)));
296318 }
297319
298320 void lua_next (void)
299321 {
300− Hash *t;
301− lua_Object o = lua_getparam(1);
302− lua_Object r = lua_getparam(2);
303− if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
304− lua_error ("too few arguments to function `next'");
305− if (lua_getparam(3) != LUA_NOOBJECT)
306− lua_error ("too many arguments to function `next'");
307− if (!lua_istable(o))
308− lua_error ("first argument of function `next' is not a table");
309− t = avalue(luaI_Address(o));
310− if (lua_isnil(r))
311− {
312− hashnext(t, 0);
313− }
314− else
315− {
316− int h = present (t, luaI_Address(r));
317− hashnext(t, h+1);
318− }
322+ Hash *t;
323+ lua_Object o = lua_getparam(1);
324+ lua_Object r = lua_getparam(2);
325+ luaL_arg_check(lua_istable(o), 1, "table expected");
326+ luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
327+ t = avalue(luaI_Address(o));
328+ if (lua_isnil(r))
329+ hashnext(t, 0);
330+ else
331+ hashnext(t, present(t, luaI_Address(r))+1);
319332 }
src/hash.h+17−16
@@ -1,7 +1,7 @@
11 /*
22 ** hash.h
33 ** hash manager for lua
4−** $Id: hash.h,v 2.12 1996/05/06 14:30:27 roberto Exp $
4+** $Id: hash.h,v 2.16 1997/05/14 18:38:29 roberto Exp $
55 */
66
77 #ifndef hash_h
@@ -10,29 +10,30 @@ · 2 unmodified lines
1010 #include "types.h"
1111 #include "opcode.h"
1212
13−typedef struct node
14−{
15− Object ref;
16− Object val;
13+typedef struct node {
14+ TObject ref;
15+ TObject val;
1716 } Node;
1817
19−typedef struct Hash
20−{
21− struct Hash *next;
22− Node *node;
23− int nhash;
24− int nuse;
25− char mark;
18+typedef struct Hash {
19+ struct Hash *next;
20+ Node *node;
21+ int nhash;
22+ int nuse;
23+ int htag;
24+ char mark;
2625 } Hash;
2726
2827
29−int lua_equalObj (Object *t1, Object *t2);
28+int lua_equalObj (TObject *t1, TObject *t2);
3029 int luaI_redimension (int nhash);
3130 Hash *lua_createarray (int nhash);
3231 void lua_hashmark (Hash *h);
33−Long lua_hashcollector (void);
34−Object *lua_hashget (Hash *t, Object *ref);
35−Object *lua_hashdefine (Hash *t, Object *ref);
32+Hash *luaI_hashcollector (long *count);
33+void luaI_hashcallIM (Hash *l);
34+void luaI_hashfree (Hash *frees);
35+TObject *lua_hashget (Hash *t, TObject *ref);
36+TObject *lua_hashdefine (Hash *t, TObject *ref);
3637 void lua_next (void);
3738
3839 #endif
src/inout.c+278−170
@@ -5,18 +5,24 @@ · 4 unmodified lines
55 ** Also provides some predefined lua functions.
66 */
77
8−char *rcs_inout="$Id: inout.c,v 2.43 1996/09/25 12:57:22 roberto Exp $";
8+char *rcs_inout="$Id: inout.c,v 2.69 1997/06/27 22:38:49 roberto Exp $";
99
1010 #include <stdio.h>
1111 #include <string.h>
1212
13+#include "auxlib.h"
14+#include "fallback.h"
15+#include "hash.h"
16+#include "inout.h"
1317 #include "lex.h"
18+#include "lua.h"
19+#include "luamem.h"
20+#include "luamem.h"
1421 #include "opcode.h"
15−#include "inout.h"
1622 #include "table.h"
1723 #include "tree.h"
18−#include "lua.h"
19−#include "mem.h"
24+#include "undump.h"
25+#include "zio.h"
2026
2127
2228 /* Exported variables */
@@ -24,92 +30,93 @@ Word lua_linenumber; · 1 unmodified line
2430 char *lua_parsedfile;
2531
2632
27−static FILE *fp;
28−static char *st;
33+char *luaI_typenames[] = { /* ORDER LUA_T */
34+ "userdata", "line", "cmark", "mark", "function",
35+ "function", "table", "string", "number", "nil",
36+ NULL
37+};
2938
30−/*
31−** Function to get the next character from the input file
32−*/
33−static int fileinput (void)
34−{
35− int c = fgetc(fp);
36− return (c == EOF) ? 0 : c;
37−}
3839
39−/*
40−** Function to get the next character from the input string
41−*/
42−static int stringinput (void)
40+
41+void luaI_setparsedfile (char *name)
4342 {
44− return *st++;
43+ lua_parsedfile = luaI_createfixedstring(name)->str;
4544 }
4645
47−/*
48−** Function to open a file to be input unit.
49−** Return the file.
50−*/
51−FILE *lua_openfile (char *fn)
46+
47+int lua_doFILE (FILE *f, int bin)
5248 {
53− lua_setinput (fileinput);
54− if (fn == NULL)
55− {
56− fp = stdin;
57− fn = "(stdin)";
58− }
59− else
60− fp = fopen (fn, "r");
61− if (fp == NULL)
62− return NULL;
63− lua_parsedfile = luaI_createfixedstring(fn)->str;
64− return fp;
65−}
49+ ZIO z;
50+ luaZ_Fopen(&z, f);
51+ if (bin)
52+ return luaI_undump(&z);
53+ else {
54+ lua_setinput(&z);
55+ return lua_domain();
56+ }
57+}
6658
67−/*
68−** Function to close an opened file
69−*/
70−void lua_closefile (void)
59+
60+int lua_dofile (char *filename)
7161 {
72− if (fp != NULL && fp != stdin)
73− {
74− fclose (fp);
75− fp = NULL;
76− }
77−}
62+ int status;
63+ int c;
64+ FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
65+ if (f == NULL)
66+ return 2;
67+ luaI_setparsedfile(filename?filename:"(stdin)");
68+ c = fgetc(f);
69+ ungetc(c, f);
70+ if (c == ID_CHUNK) {
71+ f = freopen(filename, "rb", f); /* set binary mode */
72+ status = lua_doFILE(f, 1);
73+ }
74+ else {
75+ if (c == '#')
76+ while ((c=fgetc(f)) != '\n') /* skip first line */;
77+ status = lua_doFILE(f, 0);
78+ }
79+ if (f != stdin)
80+ fclose(f);
81+ return status;
82+}
83+
84+
7885
79−/*
80−** Function to open a string to be input unit
81−*/
8286 #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
83−void lua_openstring (char *s)
84−{
85− char buff[SIZE_PREF+25];
86− lua_setinput(stringinput);
87− st = s;
88− strcpy(buff, "(dostring) >> ");
89− strncat(buff, s, SIZE_PREF);
90− if (strlen(s) > SIZE_PREF) strcat(buff, "...");
91− lua_parsedfile = luaI_createfixedstring(buff)->str;
92−}
9387
94−/*
95−** Function to close an opened string
96−*/
97−void lua_closestring (void)
88+
89+int lua_dobuffer (char *buff, int size)
9890 {
91+ int status;
92+ ZIO z;
93+ luaI_setparsedfile("(buffer)");
94+ luaZ_mopen(&z, buff, size);
95+ status = luaI_undump(&z);
96+ return status;
9997 }
10098
10199
102−static void check_arg (int cond, char *func)
100+int lua_dostring (char *str)
103101 {
104− if (!cond)
105− {
106− char buff[100];
107− sprintf(buff, "incorrect argument to function `%s'", func);
108− lua_error(buff);
109− }
102+ int status;
103+ char buff[SIZE_PREF+25];
104+ char *temp;
105+ ZIO z;
106+ if (str == NULL) return 1;
107+ sprintf(buff, "(dostring) >> %.20s", str);
108+ temp = strchr(buff, '\n');
109+ if (temp) *temp = 0; /* end string after first line */
110+ luaI_setparsedfile(buff);
111+ luaZ_sopen(&z, str);
112+ lua_setinput(&z);
113+ status = lua_domain();
114+ return status;
110115 }
111116
112117
118+
119+
113120 static int passresults (void)
114121 {
115122 int arg = 0;
@@ -118,30 +125,53 @@ static int passresults (void) · 2 unmodified lines
118125 lua_pushobject(obj);
119126 return arg-1;
120127 }
128+
129+
130+static void packresults (void)
131+{
132+ int arg = 0;
133+ lua_Object obj;
134+ lua_Object table = lua_createtable();
135+ while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
136+ lua_pushobject(table);
137+ lua_pushnumber(arg);
138+ lua_pushobject(obj);
139+ lua_rawsettable();
140+ }
141+ lua_pushobject(table);
142+ lua_pushstring("n");
143+ lua_pushnumber(arg-1);
144+ lua_rawsettable();
145+ lua_pushobject(table); /* final result */
146+}
121147
122148 /*
123149 ** Internal function: do a string
124150 */
125−void lua_internaldostring (void)
151+static void lua_internaldostring (void)
126152 {
127− lua_Object obj = lua_getparam (1);
128− if (lua_isstring(obj) && lua_dostring(lua_getstring(obj)) == 0)
153+ lua_Object err = lua_getparam(2);
154+ if (err != LUA_NOOBJECT) { /* set new error method */
155+ luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2,
156+ "must be a valid error handler");
157+ lua_pushobject(err);
158+ err = lua_seterrormethod();
159+ }
160+ if (lua_dostring(luaL_check_string(1)) == 0)
129161 if (passresults() == 0)
130162 lua_pushuserdata(NULL); /* at least one result to signal no errors */
163+ if (err != LUA_NOOBJECT) { /* restore old error method */
164+ lua_pushobject(err);
165+ lua_seterrormethod();
166+ }
131167 }
132168
133169 /*
134170 ** Internal function: do a file
135171 */
136−void lua_internaldofile (void)
172+static void lua_internaldofile (void)
137173 {
138− lua_Object obj = lua_getparam (1);
139− char *fname = NULL;
140− if (lua_isstring(obj))
141− fname = lua_getstring(obj);
142− else if (obj != LUA_NOOBJECT)
143− lua_error("invalid argument to function `dofile'");
144− /* else fname = NULL */
174+ char *fname = luaL_opt_string(1, NULL);
145175 if (lua_dofile(fname) == 0)
146176 if (passresults() == 0)
147177 lua_pushuserdata(NULL); /* at least one result to signal no errors */
@@ -150,36 +180,28 @@ void lua_internaldofile (void) · 2 unmodified lines
150180
151181 static char *tostring (lua_Object obj)
152182 {
153− char *buff = luaI_buffer(20);
154− if (lua_isstring(obj)) /* get strings and numbers */
155− return lua_getstring(obj);
156− else switch(lua_type(obj))
157− {
158− case LUA_T_FUNCTION:
159− sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
160− break;
161− case LUA_T_CFUNCTION:
162− sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
163− break;
164− case LUA_T_ARRAY:
165− sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
166− break;
167− case LUA_T_NIL:
168− sprintf(buff, "nil");
169− break;
170− default:
171− sprintf(buff, "userdata: %p", lua_getuserdata(obj));
172− break;
183+ TObject *o = luaI_Address(obj);
184+ switch (ttype(o)) {
185+ case LUA_T_NUMBER: case LUA_T_STRING:
186+ return lua_getstring(obj);
187+ case LUA_T_ARRAY: case LUA_T_FUNCTION:
188+ case LUA_T_CFUNCTION: case LUA_T_NIL:
189+ return luaI_typenames[-ttype(o)];
190+ case LUA_T_USERDATA: {
191+ char *buff = luaI_buffer(30);
192+ sprintf(buff, "userdata: %p", o->value.ts->u.v);
193+ return buff;
194+ }
195+ default: return "<unknown object>";
173196 }
174− return buff;
175197 }
176198
177−void luaI_tostring (void)
199+static void luaI_tostring (void)
178200 {
179201 lua_pushstring(tostring(lua_getparam(1)));
180202 }
181203
182−void luaI_print (void)
204+static void luaI_print (void)
183205 {
184206 int i = 1;
185207 lua_Object obj;
@@ -187,45 +209,18 @@ void luaI_print (void) · 1 unmodified line
187209 printf("%s\n", tostring(obj));
188210 }
189211
190−/*
191−** Internal function: return an object type.
192−*/
193−void luaI_type (void)
212+static void luaI_type (void)
194213 {
195214 lua_Object o = lua_getparam(1);
196− int t;
197− if (o == LUA_NOOBJECT)
198− lua_error("no parameter to function 'type'");
199− t = lua_type(o);
200− switch (t)
201− {
202− case LUA_T_NIL :
203− lua_pushliteral("nil");
204− break;
205− case LUA_T_NUMBER :
206− lua_pushliteral("number");
207− break;
208− case LUA_T_STRING :
209− lua_pushliteral("string");
210− break;
211− case LUA_T_ARRAY :
212− lua_pushliteral("table");
213− break;
214− case LUA_T_FUNCTION :
215− case LUA_T_CFUNCTION :
216− lua_pushliteral("function");
217− break;
218− default :
219− lua_pushliteral("userdata");
220− break;
221− }
222− lua_pushnumber(t);
215+ luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
216+ lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]);
217+ lua_pushnumber(lua_tag(o));
223218 }
224−
219+
225220 /*
226221 ** Internal function: convert an object to a number
227222 */
228−void lua_obj2number (void)
223+static void lua_obj2number (void)
229224 {
230225 lua_Object o = lua_getparam(1);
231226 if (lua_isnumber(o))
@@ -233,68 +228,181 @@ void lua_obj2number (void) · 1 unmodified line
233228 }
234229
235230
236−void luaI_error (void)
231+static void luaI_error (void)
237232 {
238233 char *s = lua_getstring(lua_getparam(1));
239234 if (s == NULL) s = "(no message)";
240235 lua_error(s);
241236 }
242237
243−void luaI_assert (void)
238+static void luaI_assert (void)
244239 {
245240 lua_Object p = lua_getparam(1);
246241 if (p == LUA_NOOBJECT || lua_isnil(p))
247242 lua_error("assertion failed!");
248243 }
249244
250−void luaI_setglobal (void)
245+static void luaI_setglobal (void)
246+{
247+ lua_Object value = lua_getparam(2);
248+ luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
249+ lua_pushobject(value);
250+ lua_setglobal(luaL_check_string(1));
251+ lua_pushobject(value); /* return given value */
252+}
253+
254+static void luaI_rawsetglobal (void)
251255 {
252− lua_Object name = lua_getparam(1);
253256 lua_Object value = lua_getparam(2);
254− check_arg(lua_isstring(name), "setglobal");
257+ luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
255258 lua_pushobject(value);
256− lua_storeglobal(lua_getstring(name));
259+ lua_rawsetglobal(luaL_check_string(1));
257260 lua_pushobject(value); /* return given value */
258261 }
259262
260−void luaI_getglobal (void)
263+static void luaI_getglobal (void)
261264 {
262− lua_Object name = lua_getparam(1);
263− check_arg(lua_isstring(name), "getglobal");
264− lua_pushobject(lua_getglobal(lua_getstring(name)));
265+ lua_pushobject(lua_getglobal(luaL_check_string(1)));
265266 }
266267
267−#define MAXPARAMS 256
268−void luaI_call (void)
268+static void luaI_rawgetglobal (void)
269+{
270+ lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
271+}
272+
273+static void luatag (void)
274+{
275+ lua_pushnumber(lua_tag(lua_getparam(1)));
276+}
277+
278+
279+static int getnarg (lua_Object table)
280+{
281+ lua_Object temp;
282+ /* temp = table.n */
283+ lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
284+ return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
285+}
286+
287+static void luaI_call (void)
269288 {
270289 lua_Object f = lua_getparam(1);
271290 lua_Object arg = lua_getparam(2);
272− lua_Object temp, params[MAXPARAMS];
291+ int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
273292 int narg, i;
274− check_arg(lua_istable(arg), "call");
275− check_arg(lua_isfunction(f), "call");
276− /* narg = arg.n */
277− lua_pushobject(arg);
278− lua_pushstring("n");
279− temp = lua_getsubscript();
280− narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
281− /* read arg[1...n] */
293+ luaL_arg_check(lua_isfunction(f), 1, "function expected");
294+ luaL_arg_check(lua_istable(arg), 2, "table expected");
295+ narg = getnarg(arg);
296+ /* push arg[1...n] */
282297 for (i=0; i<narg; i++) {
283− if (i>=MAXPARAMS)
284− lua_error("argument list too long in function `call'");
285− lua_pushobject(arg);
286− lua_pushnumber(i+1);
287− params[i] = lua_getsubscript();
288− if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
289− narg = i;
298+ lua_Object temp;
299+ /* temp = arg[i+1] */
300+ lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
301+ if (narg == MAX_WORD && lua_isnil(temp))
290302 break;
291− }
303+ lua_pushobject(temp);
292304 }
293− /* push parameters and do the call */
294− for (i=0; i<narg; i++)
295− lua_pushobject(params[i]);
296305 if (lua_callfunction(f))
297306 lua_error(NULL);
307+ else if (withtable)
308+ packresults();
298309 else
299310 passresults();
300311 }
312+
313+static void luaIl_settag (void)
314+{
315+ lua_Object o = lua_getparam(1);
316+ luaL_arg_check(lua_istable(o), 1, "table expected");
317+ lua_pushobject(o);
318+ lua_settag(luaL_check_number(2));
319+}
320+
321+static void luaIl_newtag (void)
322+{
323+ lua_pushnumber(lua_newtag());
324+}
325+
326+static void rawgettable (void)
327+{
328+ lua_Object t = lua_getparam(1);
329+ lua_Object i = lua_getparam(2);
330+ luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
331+ luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
332+ lua_pushobject(t);
333+ lua_pushobject(i);
334+ lua_pushobject(lua_rawgettable());
335+}
336+
337+static void rawsettable (void)
338+{
339+ lua_Object t = lua_getparam(1);
340+ lua_Object i = lua_getparam(2);
341+ lua_Object v = lua_getparam(3);
342+ luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
343+ 0, NULL);
344+ lua_pushobject(t);
345+ lua_pushobject(i);
346+ lua_pushobject(v);
347+ lua_rawsettable();
348+}
349+
350+
351+static void luaI_collectgarbage (void)
352+{
353+ lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
354+}
355+
356+
357+/*
358+** Internal functions
359+*/
360+static struct {
361+ char *name;
362+ lua_CFunction func;
363+} int_funcs[] = {
364+ {"assert", luaI_assert},
365+ {"call", luaI_call},
366+ {"collectgarbage", luaI_collectgarbage},
367+ {"dofile", lua_internaldofile},
368+ {"dostring", lua_internaldostring},
369+ {"error", luaI_error},
370+ {"getglobal", luaI_getglobal},
371+ {"newtag", luaIl_newtag},
372+ {"next", lua_next},
373+ {"nextvar", luaI_nextvar},
374+ {"print", luaI_print},
375+ {"rawgetglobal", luaI_rawgetglobal},
376+ {"rawgettable", rawgettable},
377+ {"rawsetglobal", luaI_rawsetglobal},
378+ {"rawsettable", rawsettable},
379+ {"seterrormethod", luaI_seterrormethod},
380+#if LUA_COMPAT2_5
381+ {"setfallback", luaI_setfallback},
382+#endif
383+ {"setglobal", luaI_setglobal},
384+ {"settagmethod", luaI_settagmethod},
385+ {"gettagmethod", luaI_gettagmethod},
386+ {"settag", luaIl_settag},
387+ {"tonumber", lua_obj2number},
388+ {"tostring", luaI_tostring},
389+ {"tag", luatag},
390+ {"type", luaI_type}
391+};
392+
393+#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
394+
395+
396+void luaI_predefine (void)
397+{
398+ int i;
399+ Word n;
400+ for (i=0; i<INTFUNCSIZE; i++) {
401+ n = luaI_findsymbolbyname(int_funcs[i].name);
402+ s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
403+ }
404+ n = luaI_findsymbolbyname("_VERSION");
405+ s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
406+}
407+
408+
src/inout.h+8−17
@@ -1,5 +1,5 @@
11 /*
2−** $Id: inout.h,v 1.16 1996/05/28 21:07:32 roberto Exp $
2+** $Id: inout.h,v 1.20 1997/06/19 18:04:34 roberto Exp $
33 */
44
55
@@ -14,21 +14,12 @@ extern Word lua_linenumber; · 8 unmodified lines
1414 extern Word lua_debugline;
1515 extern char *lua_parsedfile;
1616
17−FILE *lua_openfile (char *fn);
18−void lua_closefile (void);
19−void lua_openstring (char *s);
20−void lua_closestring (void);
21−
22−void lua_internaldofile (void);
23−void lua_internaldostring (void);
24−void luaI_tostring (void);
25−void luaI_print (void);
26−void luaI_type (void);
27−void lua_obj2number (void);
28−void luaI_error (void);
29−void luaI_assert (void);
30−void luaI_setglobal (void);
31−void luaI_getglobal (void);
32−void luaI_call (void);
17+void luaI_setparsedfile (char *name);
18+
19+void luaI_predefine (void);
20+
21+int lua_dobuffer (char *buff, int size);
22+int lua_doFILE (FILE *f, int bin);
23+
3324
3425 #endif
src/lex.c+174−45
@@ -1,10 +1,11 @@
1−char *rcs_lex = "$Id: lex.c,v 2.39 1996/11/08 19:08:30 roberto Exp $";
1+char *rcs_lex = "$Id: lex.c,v 3.5 1997/06/16 16:50:22 roberto Exp $";
22
33
44 #include <ctype.h>
55 #include <string.h>
66
7−#include "mem.h"
7+#include "auxlib.h"
8+#include "luamem.h"
89 #include "tree.h"
910 #include "table.h"
1011 #include "lex.h"
@@ -12,33 +13,58 @@ char *rcs_lex = "$Id: lex.c,v 2.39 1996/11/08 19:08:30 roberto Exp $"; · 1 unmodified line
1213 #include "luadebug.h"
1314 #include "parser.h"
1415
15−#define MINBUFF 260
16+#define MINBUFF 250
1617
17−#define next() (current = input())
18+static int current; /* look ahead character */
19+static ZIO *lex_z;
20+
21+
22+#define next() (current = zgetc(lex_z))
1823 #define save(x) (yytext[tokensize++] = (x))
1924 #define save_and_next() (save(current), next())
2025
2126
22−static int current; /* look ahead character */
23−static Input input; /* input function */
27+#define MAX_IFS 5
28+
29+/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
30+
31+static struct {
32+ int elsepart; /* true if its in the $else part */
33+ int condition; /* true if $if condition is true */
34+ int skip; /* true if part must be skiped */
35+} ifstate[MAX_IFS];
36+
37+static int iflevel; /* level of nested $if's */
2438
2539
26−void lua_setinput (Input fn)
40+void lua_setinput (ZIO *z)
2741 {
2842 current = '\n';
2943 lua_linenumber = 0;
30− input = fn;
44+ iflevel = 0;
45+ ifstate[0].skip = 0;
46+ ifstate[0].elsepart = 1; /* to avoid a free $else */
47+ lex_z = z;
3148 }
3249
33−void luaI_syntaxerror (char *s)
50+
51+static void luaI_auxsyntaxerror (char *s)
52+{
53+ luaL_verror("%s;\n> at line %d in file %s",
54+ s, lua_linenumber, lua_parsedfile);
55+}
56+
57+static void luaI_auxsynterrbf (char *s, char *token)
3458 {
35− char msg[256];
36− char *token = luaI_buffer(1);
3759 if (token[0] == 0)
3860 token = "<eof>";
39− sprintf (msg,"%s;\n> last token read: \"%s\" at line %d in file %s",
61+ luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
4062 s, token, lua_linenumber, lua_parsedfile);
41− lua_error (msg);
63+}
64+
65+void luaI_syntaxerror (char *s)
66+{
67+ luaI_auxsynterrbf(s, luaI_buffer(1));
4268 }
4369
4470
@@ -78,26 +104,122 @@ void luaI_addReserved (void) · 33 unmodified lines
78104 }
79105 }
80106
81−static int inclinenumber (int pragma_allowed)
107+
108+/*
109+** Pragma handling
110+*/
111+
112+#define PRAGMASIZE 20
113+
114+static void skipspace (void)
115+{
116+ while (current == ' ' || current == '\t') next();
117+}
118+
119+
120+static int checkcond (char *buff)
121+{
122+ static char *opts[] = {"nil", "1"};
123+ int i = luaI_findstring(buff, opts);
124+ if (i >= 0) return i;
125+ else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
126+ return luaI_globaldefined(buff);
127+ else {
128+ luaI_auxsynterrbf("invalid $if condition", buff);
129+ return 0; /* to avoid warnings */
130+ }
131+}
132+
133+
134+static void readname (char *buff)
135+{
136+ int i = 0;
137+ skipspace();
138+ while (isalnum((unsigned char)current) || current == '_') {
139+ if (i >= PRAGMASIZE) {
140+ buff[PRAGMASIZE] = 0;
141+ luaI_auxsynterrbf("pragma too long", buff);
142+ }
143+ buff[i++] = current;
144+ next();
145+ }
146+ buff[i] = 0;
147+}
148+
149+
150+static void inclinenumber (void);
151+
152+
153+static void ifskip (void)
154+{
155+ while (ifstate[iflevel].skip) {
156+ if (current == '\n')
157+ inclinenumber();
158+ else if (current == EOZ)
159+ luaI_auxsyntaxerror("input ends inside a $if");
160+ else next();
161+ }
162+}
163+
164+
165+static void inclinenumber (void)
82166 {
167+ static char *pragmas [] =
168+ {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
169+ next(); /* skip '\n' */
83170 ++lua_linenumber;
84− if (pragma_allowed && current == '$') { /* is a pragma? */
85− char *buff = luaI_buffer(MINBUFF+1);
86− int i = 0;
171+ if (current == '$') { /* is a pragma? */
172+ char buff[PRAGMASIZE+1];
173+ int ifnot = 0;
174+ int skip = ifstate[iflevel].skip;
87175 next(); /* skip $ */
88− while (isalnum(current)) {
89− if (i >= MINBUFF) luaI_syntaxerror("pragma too long");
90− buff[i++] = current;
91− next();
176+ readname(buff);
177+ switch (luaI_findstring(buff, pragmas)) {
178+ case 0: /* debug */
179+ if (!skip) lua_debug = 1;
180+ break;
181+ case 1: /* nodebug */
182+ if (!skip) lua_debug = 0;
183+ break;
184+ case 2: /* endinput */
185+ if (!skip) {
186+ current = EOZ;
187+ iflevel = 0; /* to allow $endinput inside a $if */
188+ }
189+ break;
190+ case 3: /* end */
191+ if (iflevel-- == 0)
192+ luaI_auxsyntaxerror("unmatched $endif");
193+ break;
194+ case 4: /* ifnot */
195+ ifnot = 1;
196+ /* go through */
197+ case 5: /* if */
198+ if (iflevel == MAX_IFS-1)
199+ luaI_auxsyntaxerror("too many nested `$ifs'");
200+ readname(buff);
201+ iflevel++;
202+ ifstate[iflevel].elsepart = 0;
203+ ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
204+ ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
205+ break;
206+ case 6: /* else */
207+ if (ifstate[iflevel].elsepart)
208+ luaI_auxsyntaxerror("unmatched $else");
209+ ifstate[iflevel].elsepart = 1;
210+ ifstate[iflevel].skip =
211+ ifstate[iflevel-1].skip || ifstate[iflevel].condition;
212+ break;
213+ default:
214+ luaI_auxsynterrbf("invalid pragma", buff);
92215 }
93− buff[i] = 0;
94− if (strcmp(buff, "debug") == 0)
95− lua_debug = 1;
96− else if (strcmp(buff, "nodebug") == 0)
97− lua_debug = 0;
98− else luaI_syntaxerror("invalid pragma");
216+ skipspace();
217+ if (current == '\n') /* pragma must end with a '\n' ... */
218+ inclinenumber();
219+ else if (current != EOZ) /* or eof */
220+ luaI_auxsyntaxerror("invalid pragma format");
221+ ifskip();
99222 }
100− return lua_linenumber;
101223 }
102224
103225 static int read_long_string (char *yytext, int buffsize)
@@ -110,7 +232,7 @@ static int read_long_string (char *yytext, int buffsize) · 6 unmodified lines
110232 yytext = luaI_buffer(buffsize *= 2);
111233 switch (current)
112234 {
113− case 0:
235+ case EOZ:
114236 save(0);
115237 return WRONGTOKEN;
116238 case '[':
@@ -131,8 +253,8 @@ static int read_long_string (char *yytext, int buffsize) · 14 unmodified lines
131253 }
132254 continue;
133255 case '\n':
134− save_and_next();
135− inclinenumber(0);
256+ save('\n');
257+ inclinenumber();
136258 continue;
137259 default:
138260 save_and_next();
@@ -162,8 +284,8 @@ int luaY_lex (void) · 23 unmodified lines
162284 switch (current)
163285 {
164286 case '\n':
165− next();
166− linelasttoken = inclinenumber(1);
287+ inclinenumber();
288+ linelasttoken = lua_linenumber;
167289 continue;
168290
169291 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
@@ -173,7 +295,7 @@ int luaY_lex (void) · 3 unmodified lines
173295 case '-':
174296 save_and_next();
175297 if (current != '-') return '-';
176− do { next(); } while (current != '\n' && current != 0);
298+ do { next(); } while (current != '\n' && current != EOZ);
177299 continue;
178300
179301 case '[':
@@ -216,7 +338,7 @@ int luaY_lex (void) · 36 unmodified lines
216338 yytext = luaI_buffer(buffsize *= 2);
217339 switch (current)
218340 {
219− case 0:
341+ case EOZ:
220342 case '\n':
221343 save(0);
222344 return WRONGTOKEN;
@@ -227,7 +349,7 @@ int luaY_lex (void) · 4 unmodified lines
227349 case 'n': save('\n'); next(); break;
228350 case 't': save('\t'); next(); break;
229351 case 'r': save('\r'); next(); break;
230− case '\n': save_and_next(); inclinenumber(0); break;
352+ case '\n': save('\n'); inclinenumber(); break;
231353 default : save_and_next(); break;
232354 }
233355 break;
@@ -258,7 +380,9 @@ int luaY_lex (void) · 24 unmodified lines
258380 case '_':
259381 {
260382 TaggedString *ts;
261− do { save_and_next(); } while (isalnum(current) || current == '_');
383+ do {
384+ save_and_next();
385+ } while (isalnum((unsigned char)current) || current == '_');
262386 save(0);
263387 ts = lua_createstring(yytext);
264388 if (ts->marked > 2)
@@ -280,7 +404,7 @@ int luaY_lex (void) · 15 unmodified lines
280404 }
281405 else return CONC; /* .. */
282406 }
283− else if (!isdigit(current)) return '.';
407+ else if (!isdigit((unsigned char)current)) return '.';
284408 /* current is a digit: goes through to number */
285409 a=0.0;
286410 goto fraction;
@@ -291,7 +415,7 @@ int luaY_lex (void) · 4 unmodified lines
291415 do {
292416 a=10.0*a+(current-'0');
293417 save_and_next();
294− } while (isdigit(current));
418+ } while (isdigit((unsigned char)current));
295419 if (current == '.') {
296420 save_and_next();
297421 if (current == '.')
@@ -300,7 +424,7 @@ int luaY_lex (void) · 2 unmodified lines
300424 }
301425 fraction:
302426 { double da=0.1;
303− while (isdigit(current))
427+ while (isdigit((unsigned char)current))
304428 {
305429 a+=(current-'0')*da;
306430 da/=10.0;
@@ -314,11 +438,12 @@ int luaY_lex (void) · 7 unmodified lines
314438 save_and_next();
315439 neg=(current=='-');
316440 if (current == '+' || current == '-') save_and_next();
317− if (!isdigit(current)) { save(0); return WRONGTOKEN; }
441+ if (!isdigit((unsigned char)current)) {
442+ save(0); return WRONGTOKEN; }
318443 do {
319444 e=10.0*e+(current-'0');
320445 save_and_next();
321− } while (isdigit(current));
446+ } while (isdigit((unsigned char)current));
322447 for (ea=neg?0.1:10.0; e>0; e>>=1)
323448 {
324449 if (e & 1) a*=ea;
@@ -330,11 +455,15 @@ int luaY_lex (void) · 5 unmodified lines
330455 return NUMBER;
331456 }
332457
333− default: /* also end of program (0) */
334− {
458+ case EOZ:
459+ save(0);
460+ if (iflevel > 0)
461+ luaI_syntaxerror("missing $endif");
462+ return 0;
463+
464+ default:
335465 save_and_next();
336466 return yytext[0];
337− }
338467 }
339468 }
340469 }
src/lex.h+3−4
@@ -1,16 +1,15 @@
11 /*
22 ** lex.h
33 ** TecCGraf - PUC-Rio
4−** $Id: lex.h,v 1.3 1996/11/08 12:49:35 roberto Exp $
4+** $Id: lex.h,v 1.4 1997/06/16 16:50:22 roberto Exp $
55 */
66
77 #ifndef lex_h
88 #define lex_h
99
10+#include "zio.h"
1011
11−typedef int (*Input) (void);
12−
13−void lua_setinput (Input fn);
12+void lua_setinput (ZIO *z);
1413 void luaI_syntaxerror (char *s);
1514 int luaY_lex (void);
1615 void luaI_addReserved (void);
src/lua.stx+29−34
@@ -1,13 +1,13 @@
11 %{
22
3−char *rcs_luastx = "$Id: lua.stx,v 3.41 1996/11/08 12:49:35 roberto Exp $";
3+char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp $";
44
55 #include <stdio.h>
66 #include <stdlib.h>
77 #include <string.h>
88
99 #include "luadebug.h"
10−#include "mem.h"
10+#include "luamem.h"
1111 #include "lex.h"
1212 #include "opcode.h"
1313 #include "hash.h"
@@ -50,8 +50,6 @@ static TaggedString *localvar[MAXLOCALS]; /* store local variable names */ · 36 unmodified lines
5050 static int nlocalvar=0; /* number of local variables */
5151
5252 #define MAXFIELDS FIELDS_PER_FLUSH*2
53−static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
54−static int nfields=0;
5553
5654 int lua_debug = 0;
5755
@@ -103,22 +101,11 @@ static void code_word_at (Byte *p, int n) · 45 unmodified lines
103101 memcpy(p, &w, sizeof(Word));
104102 }
105103
106−static void push_field (Word name)
107−{
108− if (nfields < MAXFIELDS)
109− fields[nfields++] = name;
110− else
111− yyerror ("too many fields in nested constructors");
112−}
113−
114104 static void flush_record (int n)
115105 {
116− int i;
117106 if (n == 0) return;
118− code_byte(STORERECORD);
107+ code_byte(STOREMAP);
119108 code_byte(n);
120− for (i=0; i<n; i++)
121− code_word(fields[--nfields]);
122109 }
123110
124111 static void flush_list (int m, int n)
@@ -161,11 +148,22 @@ static void add_varbuffer (Long var) · 36 unmodified lines
161148 yyerror ("variable buffer overflow");
162149 }
163150
151+static void code_string (Word w)
152+{
153+ code_byte(PUSHSTRING);
154+ code_word(w);
155+}
156+
157+static void code_constant (TaggedString *s)
158+{
159+ code_string(luaI_findconstant(s));
160+}
161+
164162 static void code_number (float f)
165163 {
166− Word i = (Word)f;
167− if (f == (float)i) /* f has an (short) integer value */
168− {
164+ Word i;
165+ if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
166+ /* f has an (short) integer value */
169167 if (i <= 2) code_byte(PUSH0 + i);
170168 else if (i <= 255)
171169 {
@@ -459,7 +457,7 @@ void lua_parse (TFunc *tf) · 287 unmodified lines
459457
460458 %% /* beginning of rules section */
461459
462−chunk : chunklist ret
460+chunk : chunklist ret ;
463461
464462 chunklist : /* empty */
465463 | chunklist stat sc
@@ -477,8 +475,7 @@ function : FUNCTION funcname body · 11 unmodified lines
477475 funcname : var { $$ =$1; init_func(); }
478476 | varexp ':' NAME
479477 {
480− code_byte(PUSHSTRING);
481− code_word(luaI_findconstant($3));
478+ code_constant($3);
482479 $$ = 0; /* indexed variable */
483480 init_func();
484481 add_localvar(luaI_createfixedstring("self"));
@@ -492,7 +489,6 @@ body : '(' parlist ')' block END · 7 unmodified lines
492489 luaI_initTFunc($$);
493490 $$->size = pc;
494491 $$->code = newvector(pc, Byte);
495− $$->fileName = lua_parsedfile;
496492 $$->lineDefined = $2;
497493 memcpy($$->code, basepc, pc*sizeof(Byte));
498494 if (lua_debug)
@@ -540,7 +536,7 @@ stat : IF expr1 THEN PrepJump block PrepJump elsepart END · 41 unmodified lines
540536 lua_codeadjust (0);
541537 }
542538 }
543− | functioncall
539+ | functioncall {;}
544540 | LOCAL localdeclist decinit
545541 { nlocalvar += $2;
546542 adjust_mult_assign($2, $3, 0);
@@ -605,9 +601,8 @@ expr : '(' expr ')' { $$ = $2; } · 58 unmodified lines
605601 | NUMBER { code_number($1); $$ = 0; }
606602 | STRING
607603 {
608− code_byte(PUSHSTRING);
609− code_word($1);
610− $$ = 0;
604+ code_string($1);
605+ $$ = 0;
611606 }
612607 | NIL {code_byte(PUSHNIL); $$ = 0; }
613608 | functioncall { $$ = $1; }
@@ -723,12 +718,13 @@ ffieldlist1 : ffield {$$=1;} · 109 unmodified lines
723718 }
724719 ;
725720
726−ffield : NAME '=' expr1
727− {
728− push_field(luaI_findconstant($1));
729− }
721+ffield : ffieldkey '=' expr1
730722 ;
731723
724+ffieldkey : '[' expr1 ']'
725+ | NAME { code_constant($1); }
726+ ;
727+
732728 lfieldlist : /* empty */ { $$ = 0; }
733729 | lfieldlist1 lastcomma { $$ = $1; }
734730 ;
@@ -762,9 +758,8 @@ var : singlevar { $$ = $1; } · 27 unmodified lines
762758 }
763759 | varexp '.' NAME
764760 {
765− code_byte(PUSHSTRING);
766− code_word(luaI_findconstant($3));
767− $$ = 0; /* indexed variable */
761+ code_constant($3);
762+ $$ = 0; /* indexed variable */
768763 }
769764 ;
770765
src/luac/dump.c+11−19
@@ -3,15 +3,13 @@ · 2 unmodified lines
33 ** thread and save bytecodes to file
44 */
55
6−char* rcs_dump="$Id: dump.c,v 1.17 1996/11/18 11:18:29 lhf Exp $";
6+char* rcs_dump="$Id: dump.c,v 1.20 1997/06/19 14:56:04 lhf Exp $";
77
88 #include <stdio.h>
99 #include <stdlib.h>
1010 #include <string.h>
1111 #include "luac.h"
1212
13−static TFunc* lastF=NULL; /* list of functions seen in code */
14−
1513 static int SawVar(int i, int at)
1614 {
1715 int old=VarLoc(i);
@@ -34,7 +32,7 @@ static void ThreadCode(Byte* code, Byte* end) · 16 unmodified lines
3432 for (i=0; i<lua_nconstant; i++) StrLoc(i)=0;
3533 for (p=code; p!=end;)
3634 {
37− OpCode op=(OpCode)*p;
35+ int op=*p;
3836 int at=p-code+1;
3937 switch (op)
4038 {
@@ -89,6 +87,8 @@ static void ThreadCode(Byte* code, Byte* end) · 48 unmodified lines
8987 case STORELIST0:
9088 case ADJUST:
9189 case RETCODE:
90+ case VARARGS:
91+ case STOREMAP:
9292 p+=2;
9393 break;
9494 case PUSHWORD:
@@ -107,8 +107,11 @@ static void ThreadCode(Byte* code, Byte* end) · 12 unmodified lines
107107 case PUSHFLOAT:
108108 p+=5; /* assumes sizeof(float)==4 */
109109 break;
110− case PUSHSELF:
110+ case PUSHFUNCTION:
111+ p+=sizeof(TFunc*)+1;
112+ break;
111113 case PUSHSTRING:
114+ case PUSHSELF:
112115 {
113116 Word w;
114117 p++;
@@ -117,16 +120,6 @@ static void ThreadCode(Byte* code, Byte* end) · 2 unmodified lines
117120 memcpy(p-2,&w,sizeof(w));
118121 break;
119122 }
120− case PUSHFUNCTION:
121− {
122− TFunc* tf;
123− p++;
124− get_code(tf,p);
125− tf->marked=at;
126− tf->next=NULL; /* TODO: remove? */
127− lastF=lastF->next=tf;
128− break;
129− }
130123 case PUSHGLOBAL:
131124 case STOREGLOBAL:
132125 {
@@ -151,8 +144,8 @@ static void ThreadCode(Byte* code, Byte* end) · 18 unmodified lines
151144 }
152145 break;
153146 }
154− default:
155− fprintf(stderr,"luac: cannot happen: opcode=%d",*p);
147+ default: /* cannot happen */
148+ fprintf(stderr,"luac: bad opcode %d at %d\n",*p,(int)(p-code));
156149 exit(1);
157150 break;
158151 }
@@ -186,7 +179,7 @@ static void DumpString(char* s, FILE* D) · 27 unmodified lines
186179 int n=strlen(s)+1;
187180 if ((Word)n != n)
188181 {
189− fprintf(stderr,"luac: string too long: \"%.32s...\"\n",s);
182+ fprintf(stderr,"luac: string too long (%d bytes): \"%.32s...\"\n",n,s);
190183 exit(1);
191184 }
192185 DumpWord(n,D);
@@ -220,7 +213,6 @@ static void DumpStrings(FILE* D) · 27 unmodified lines
220213
221214 void DumpFunction(TFunc* tf, FILE* D)
222215 {
223− lastF=tf;
224216 ThreadCode(tf->code,tf->code+tf->size);
225217 fputc(ID_FUN,D);
226218 DumpSize(tf->size,D);
src/luac/luac.c+112−27
@@ -1,25 +1,39 @@
11 /*
22 ** luac.c
3−** lua compiler (saves bytecodes to files)
3+** lua compiler (saves bytecodes to files; also list binary files)
44 */
55
6−char* rcs_luac="$Id: luac.c,v 1.18 1996/11/16 20:14:23 lhf Exp $";
6+char* rcs_luac="$Id: luac.c,v 1.23 1997/06/20 20:34:04 lhf Exp $";
77
88 #include <stdio.h>
99 #include <stdlib.h>
1010 #include <string.h>
1111 #include "luac.h"
12+#include "lex.h"
13+#include "zio.h"
1214
1315 static void compile(char* filename);
16+static void undump(char* filename);
1417
1518 static int listing=0; /* list bytecodes? */
1619 static int dumping=1; /* dump bytecodes? */
20+static int undumping=0; /* undump bytecodes? */
1721 static FILE* D; /* output file */
1822
1923 static void usage(void)
2024 {
21− fprintf(stderr,"usage: luac [-dlpv] [-o output] file ...\n");
22− exit(0);
25+ fprintf(stderr,
26+ "usage: luac [-c | -u] [-d] [-l] [-p] [-q] [-v] [-o output] file ...\n"
27+ " -c\tcompile (default)\n"
28+ " -u\tundump\n"
29+ " -d\tgenerate debugging information\n"
30+ " -l\tlist (default for -u)\n"
31+ " -o\toutput file for -c (default \"luac.out\")\n"
32+ " -p\tparse only\n"
33+ " -q\tquiet (default for -c)\n"
34+ " -v\tshow version information\n"
35+ );
36+ exit(1);
2337 }
2438
2539 #define IS(s) (strcmp(argv[i],s)==0)
@@ -34,6 +48,11 @@ int main(int argc, char* argv[]) · 8 unmodified lines
3448 break;
3549 else if (IS("-")) /* use stdin */
3650 break;
51+ else if (IS("-c")) /* compile (and dump) */
52+ {
53+ dumping=1;
54+ undumping=0;
55+ }
3756 else if (IS("-d")) /* debug */
3857 lua_debug=1;
3958 else if (IS("-l")) /* list */
@@ -42,6 +61,14 @@ int main(int argc, char* argv[]) · 2 unmodified lines
4261 d=argv[++i];
4362 else if (IS("-p")) /* parse only (for timing purposes) */
4463 dumping=0;
64+ else if (IS("-q")) /* quiet */
65+ listing=0;
66+ else if (IS("-u")) /* undump */
67+ {
68+ dumping=0;
69+ undumping=1;
70+ listing=1;
71+ }
4572 else if (IS("-v")) /* show version */
4673 printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);
4774 else /* unknown option */
@@ -50,56 +77,114 @@ int main(int argc, char* argv[]) · 2 unmodified lines
5077 --i; /* fake new argv[0] */
5178 argc-=i;
5279 argv+=i;
53− if (argc<2) usage();
54− for (i=1; i<argc; i++)
55− if (IS(d))
80+ if (dumping)
81+ {
82+ if (argc<2) usage();
83+ for (i=1; i<argc; i++) /* play safe with output file */
84+ if (IS(d))
85+ {
86+ fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);
87+ exit(1);
88+ }
89+ D=fopen(d,"wb"); /* must open in binary mode */
90+ if (D==NULL)
5691 {
57− fprintf(stderr,"luac: will not overwrite input file \"%s\"\n",d);
92+ fprintf(stderr,"luac: cannot open ");
93+ perror(d);
5894 exit(1);
5995 }
60− D=(dumping) ? fopen(d,"wb") : stdout; /* must open in binary mode */
61− if (D==NULL)
96+ for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
97+ fclose(D);
98+ }
99+ if (undumping)
62100 {
63− fprintf(stderr,"luac: cannot open ");
64− perror(d);
65− exit(1);
101+ if (argc<2)
102+ undump("luac.out");
103+ else
104+ for (i=1; i<argc; i++) undump(IS("-")? NULL : argv[i]);
66105 }
67− for (i=1; i<argc; i++) compile(IS("-")? NULL : argv[i]);
68− fclose(D);
69106 return 0;
70107 }
71108
72−static void do_dump(TFunc* tf) /* only for tf==main */
109+static void do_dump(TFunc* Main)
73110 {
74− if (dumping) DumpHeader(D);
75− while (tf!=NULL)
111+ TFunc* tf;
112+ LinkFunctions(Main);
113+ if (listing)
114+ {
115+ for (tf=Main; tf!=NULL; tf=tf->next) PrintFunction(tf,Main);
116+ }
117+ if (dumping)
76118 {
77− TFunc* nf;
78− if (listing) PrintFunction(tf);
79− if (dumping) DumpFunction(tf,D);
80− nf=tf->next; /* list only built after first main */
119+ DumpHeader(D);
120+ for (tf=Main; tf!=NULL; tf=tf->next) DumpFunction(tf,D);
121+ }
122+ for (tf=Main; tf!=NULL; )
123+ {
124+ TFunc* nf=tf->next;
81125 luaI_freefunc(tf);
82126 tf=nf;
83127 }
84128 }
85129
86−static void do_compile(void)
130+static void do_compile(ZIO* z)
87131 {
88132 TFunc* tf=new(TFunc);
133+ lua_setinput(z);
89134 luaI_initTFunc(tf);
90− tf->fileName = lua_parsedfile;
135+ tf->fileName=lua_parsedfile;
91136 lua_parse(tf);
92137 do_dump(tf);
93138 }
94139
95140 static void compile(char* filename)
96141 {
97− if (lua_openfile(filename)==NULL)
142+ FILE* f= (filename==NULL) ? stdin : fopen(filename, "r");
143+ if (f==NULL)
144+ {
145+ fprintf(stderr,"luac: cannot open ");
146+ perror(filename);
147+ exit(1);
148+ }
149+ else
150+ {
151+ ZIO z;
152+ zFopen(&z,f);
153+ luaI_setparsedfile(filename?filename:"(stdin)");
154+ do_compile(&z);
155+ fclose(f);
156+ }
157+}
158+
159+static void do_undump(ZIO* z)
160+{
161+ TFunc* Main;
162+ while ((Main=luaI_undump1(z)))
163+ {
164+ if (listing)
165+ {
166+ TFunc* tf;
167+ for (tf=Main; tf!=NULL; tf=tf->next)
168+ PrintFunction(tf,Main);
169+ }
170+ luaI_freefunc(Main); /* TODO: free others */
171+ }
172+}
173+
174+static void undump(char* filename)
175+{
176+ FILE* f= (filename==NULL) ? stdin : fopen(filename, "rb");
177+ if (f==NULL)
98178 {
99179 fprintf(stderr,"luac: cannot open ");
100180 perror(filename);
101181 exit(1);
102182 }
103− do_compile();
104− lua_closefile();
183+ else
184+ {
185+ ZIO z;
186+ zFopen(&z,f);
187+ do_undump(&z);
188+ fclose(f);
189+ }
105190 }
src/luac/luac.h+6−5
@@ -1,24 +1,25 @@
11 /*
22 ** luac.h
33 ** definitions for luac compiler
4−** $Id: luac.h,v 1.5 1996/03/08 21:41:47 lhf Exp $
4+** $Id: luac.h,v 1.8 1997/06/19 17:32:08 lhf Exp $
55 */
66
77 #include "inout.h"
8−#include "mem.h"
8+#include "luamem.h"
99 #include "opcode.h"
1010 #include "table.h"
1111 #include "undump.h"
1212
1313 #define VarStr(i) (lua_table[i].varname->str)
14−#define VarLoc(i) (lua_table[i].varname->varindex)
14+#define VarLoc(i) (lua_table[i].varname->u.s.varindex)
1515 #define StrStr(i) (lua_constant[i]->str)
16−#define StrLoc(i) (lua_constant[i]->constindex)
16+#define StrLoc(i) (lua_constant[i]->u.s.constindex)
1717
1818 extern Word lua_ntable;
1919 extern Word lua_nconstant;
2020 extern int lua_debug;
2121
22+void LinkFunctions(TFunc* tf);
23+void PrintFunction(TFunc* tf, TFunc* Main);
2224 void DumpHeader(FILE* D);
2325 void DumpFunction(TFunc* tf, FILE* D);
24−void PrintFunction(TFunc* tf);
src/luac/print.c+213−20
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 ** print bytecodes
44 */
55
6−char* rcs_print="$Id: print.c,v 1.11 1996/11/18 11:24:16 lhf Exp $";
6+char* rcs_print="$Id: print.c,v 1.17 1997/06/25 17:07:28 lhf Exp $";
77
88 #include <stdio.h>
99 #include <stdlib.h>
@@ -11,20 +11,134 @@ char* rcs_print="$Id: print.c,v 1.11 1996/11/18 11:24:16 lhf Exp $"; · 1 unmodified line
1111 #include "luac.h"
1212 #include "print.h"
1313
14−static LocVar* V=NULL;
15−
16−static char* LocStr(int i)
14+void LinkFunctions(TFunc* m)
1715 {
18− if (V==NULL) return ""; else return V[i].varname->str;
16+ static TFunc* lastF; /* list of functions seen in code */
17+ Byte* code=m->code;
18+ Byte* end=code+m->size;
19+ Byte* p;
20+ if (IsMain(m)) lastF=m;
21+ for (p=code; p!=end;)
22+ {
23+ int op=*p;
24+ int at=p-code+1;
25+ switch (op)
26+ {
27+ case PUSHNIL:
28+ case PUSH0:
29+ case PUSH1:
30+ case PUSH2:
31+ case PUSHLOCAL0:
32+ case PUSHLOCAL1:
33+ case PUSHLOCAL2:
34+ case PUSHLOCAL3:
35+ case PUSHLOCAL4:
36+ case PUSHLOCAL5:
37+ case PUSHLOCAL6:
38+ case PUSHLOCAL7:
39+ case PUSHLOCAL8:
40+ case PUSHLOCAL9:
41+ case PUSHINDEXED:
42+ case STORELOCAL0:
43+ case STORELOCAL1:
44+ case STORELOCAL2:
45+ case STORELOCAL3:
46+ case STORELOCAL4:
47+ case STORELOCAL5:
48+ case STORELOCAL6:
49+ case STORELOCAL7:
50+ case STORELOCAL8:
51+ case STORELOCAL9:
52+ case STOREINDEXED0:
53+ case ADJUST0:
54+ case EQOP:
55+ case LTOP:
56+ case LEOP:
57+ case GTOP:
58+ case GEOP:
59+ case ADDOP:
60+ case SUBOP:
61+ case MULTOP:
62+ case DIVOP:
63+ case POWOP:
64+ case CONCOP:
65+ case MINUSOP:
66+ case NOTOP:
67+ case POP:
68+ case RETCODE0:
69+ p++;
70+ break;
71+ case PUSHBYTE:
72+ case PUSHLOCAL:
73+ case STORELOCAL:
74+ case STOREINDEXED:
75+ case STORELIST0:
76+ case ADJUST:
77+ case RETCODE:
78+ case VARARGS:
79+ case STOREMAP:
80+ p+=2;
81+ break;
82+ case PUSHWORD:
83+ case PUSHSTRING:
84+ case PUSHGLOBAL:
85+ case PUSHSELF:
86+ case STOREGLOBAL:
87+ case CREATEARRAY:
88+ case ONTJMP:
89+ case ONFJMP:
90+ case JMP:
91+ case UPJMP:
92+ case IFFJMP:
93+ case IFFUPJMP:
94+ case CALLFUNC:
95+ case SETLINE:
96+ case STORELIST:
97+ p+=3;
98+ break;
99+ case PUSHFLOAT:
100+ p+=5; /* assumes sizeof(float)==4 */
101+ break;
102+ case PUSHFUNCTION:
103+ {
104+ TFunc* tf;
105+ p++;
106+ get_code(tf,p);
107+ tf->marked=at;
108+ tf->next=NULL; /* TODO: remove? */
109+ lastF=lastF->next=tf;
110+ break;
111+ }
112+ case STORERECORD:
113+ {
114+ int n=*++p;
115+ p+=2*n+1;
116+ break;
117+ }
118+ default: /* cannot happen */
119+ fprintf(stderr,"luac: bad opcode %d at %d\n",*p,(int)(p-code));
120+ exit(1);
121+ break;
122+ }
123+ }
19124 }
20125
21−static void PrintCode(Byte* code, Byte* end)
126+#define LocStr(i) luaI_getlocalname(tf,i+1,line)
127+
128+static void PrintCode(TFunc* tf)
22129 {
130+ Byte* code=tf->code;
131+ Byte* end=code+tf->size;
23132 Byte* p;
133+ int line=0;
24134 for (p=code; p!=end;)
25135 {
26− OpCode op=(OpCode)*p;
27− if (op>SETLINE) op=SETLINE+1;
136+ int op=*p;
137+ if (op>=NOPCODES)
138+ {
139+ fprintf(stderr,"luac: bad opcode %d at %d\n",op,(int)(p-code));
140+ exit(1);
141+ }
28142 printf("%6d\t%s",(int)(p-code),OpCodeName[op]);
29143 switch (op)
30144 {
@@ -64,7 +178,7 @@ static void PrintCode(Byte* code, Byte* end) · 33 unmodified lines
64178 case PUSHLOCAL9:
65179 {
66180 int i=op-PUSHLOCAL0;
67− printf("\t%d\t; %s",i,LocStr(i));
181+ if (tf->locvars) printf("\t\t; %s",LocStr(i));
68182 p++;
69183 break;
70184 }
@@ -80,7 +194,7 @@ static void PrintCode(Byte* code, Byte* end) · 9 unmodified lines
80194 case STORELOCAL9:
81195 {
82196 int i=op-STORELOCAL0;
83− printf("\t%d\t; %s",i,LocStr(i));
197+ if (tf->locvars) printf("\t\t; %s",LocStr(i));
84198 p++;
85199 break;
86200 }
@@ -88,7 +202,7 @@ static void PrintCode(Byte* code, Byte* end) · 1 unmodified line
88202 case STORELOCAL:
89203 {
90204 int i=*(p+1);
91− printf("\t%d\t; %s",i,LocStr(i));
205+ if (tf->locvars) printf("\t%d\t; %s",i,LocStr(i));
92206 p+=2;
93207 break;
94208 }
@@ -97,23 +211,40 @@ static void PrintCode(Byte* code, Byte* end) · 2 unmodified lines
97211 case STORELIST0:
98212 case ADJUST:
99213 case RETCODE:
214+ case VARARGS:
215+ case STOREMAP:
100216 printf("\t%d",*(p+1));
101217 p+=2;
102218 break;
103219 case PUSHWORD:
104220 case CREATEARRAY:
221+ case SETLINE:
222+ {
223+ Word w;
224+ p++;
225+ get_word(w,p);
226+ printf("\t%d",w);
227+ if (op==SETLINE) line=w;
228+ break;
229+ }
105230 case ONTJMP:
106231 case ONFJMP:
107232 case JMP:
108− case UPJMP:
109233 case IFFJMP:
234+ { /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
235+ Word w;
236+ p++;
237+ get_word(w,p);
238+ printf("\t%d\t\t; to %d",w,(int)(p-code)+w);
239+ break;
240+ }
241+ case UPJMP:
110242 case IFFUPJMP:
111− case SETLINE:
112− {
243+ { /* suggested by Norman Ramsey <nr@cs.virginia.edu> */
113244 Word w;
114245 p++;
115246 get_word(w,p);
116− printf("\t%d",w);
247+ printf("\t%d\t\t; to %d",w,(int)(p-code)-w);
117248 break;
118249 }
119250 case PUSHFLOAT:
@@ -170,7 +301,8 @@ static void PrintCode(Byte* code, Byte* end) · 50 unmodified lines
170301 break;
171302 }
172303 default:
173− printf("\tcannot happen: opcode=%d",*p);
304+ printf("\tcannot happen: opcode=%d\n",*p);
305+ fprintf(stderr,"luac: bad opcode %d at %d\n",op,(int)(p-code));
174306 exit(1);
175307 break;
176308 }
@@ -178,13 +310,74 @@ static void PrintCode(Byte* code, Byte* end) · 1 unmodified line
178310 }
179311 }
180312
181−void PrintFunction(TFunc* tf)
313+#undef LocStr
314+
315+static void PrintLocals(LocVar* v, int n)
182316 {
317+ int i=0;
318+ if (v==NULL || v->varname==NULL) return;
319+ if (n>0)
320+ {
321+ printf("parameters:");
322+ for (i=0; i<n; v++,i++) printf(" %s[%d@%d]",v->varname->str,i,v->line);
323+ printf("\n");
324+ }
325+ if (v->varname!=NULL)
326+ {
327+ printf("locals:");
328+ for (; v->line>=0; v++)
329+ {
330+ if (v->varname==NULL)
331+#if 0
332+ printf(" %s[%d@%d]","*",--i,v->line);
333+#else
334+ --i;
335+#endif
336+ else
337+ printf(" %s[%d@%d]",v->varname->str,i++,v->line);
338+ }
339+ printf("\n");
340+ }
341+}
342+
343+void PrintFunction(TFunc* tf, TFunc* Main)
344+{
345+ int n=0;
183346 if (IsMain(tf))
184347 printf("\nmain of \"%s\" (%d bytes at %p)\n",tf->fileName,tf->size,tf);
185348 else
186− printf("\nfunction \"%s\":%d (%d bytes at %p); used at main+%d\n",
349+ {
350+ Byte* p;
351+ p=tf->code; /* get number of parameters */
352+ while (*p==SETLINE) p+=3;
353+ if (*p==ADJUST) n=p[1];
354+ p=Main->code+tf->marked+sizeof(TFunc*);
355+ printf("\nfunction ");
356+ switch (*p) /* try to get name */
357+ {
358+ case STOREGLOBAL:
359+ {
360+ Word w;
361+ p++; get_word(w,p); printf("%s defined at ",VarStr(w));
362+ break;
363+ }
364+ case STOREINDEXED0: /* try method definition */
365+ {
366+ if (p[-11]==PUSHGLOBAL && p[-8]==PUSHSTRING)
367+ {
368+ Word w;
369+ Byte* op=p;
370+ int c=(tf->locvars && n>0 && strcmp(tf->locvars->varname->str,"self")==0)
371+ ? ':' : '.';
372+ p=op-11; p++; get_word(w,p); printf("%s%c",VarStr(w),c);
373+ p=op-8; p++; get_word(w,p); printf("%s defined at ",StrStr(w));
374+ }
375+ break;
376+ }
377+ }
378+ printf("\"%s\":%d (%d bytes at %p); used at main+%d\n",
187379 tf->fileName,tf->lineDefined,tf->size,tf,tf->marked);
188− V=tf->locvars;
189− PrintCode(tf->code,tf->code+tf->size);
380+ }
381+ PrintLocals(tf->locvars,n);
382+ PrintCode(tf);
190383 }
src/luac/print.h+6−3
@@ -1,10 +1,10 @@
11 /*
22 ** print.h
33 ** opcode names
4−** $Id: print.h,v 1.1 1996/02/23 19:04:13 lhf Exp $
4+** $Id: print.h,v 1.3 1997/04/14 14:42:50 lhf Exp $
55 */
66
7−static char* OpCodeName[]={ /* attention: same order as enum in opcode.h */
7+static char* OpCodeName[]={ /* ATTENTION: same order as enum in opcode.h */
88 "PUSHNIL",
99 "PUSH0",
1010 "PUSH1",
@@ -72,5 +72,8 @@ static char* OpCodeName[]={ /* attention: same order as enum in opcode.h */ · 61 unmodified lines
7272 "RETCODE0",
7373 "RETCODE",
7474 "SETLINE",
75− ""
75+ "VARARGS",
76+ "STOREMAP"
7677 };
78+
79+#define NOPCODES (sizeof(OpCodeName)/sizeof(OpCodeName[0]))
src/luamem.c+159−0
@@ -0,0 +1,159 @@
1+/*
2+** mem.c
3+** TecCGraf - PUC-Rio
4+*/
5+
6+char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $";
7+
8+#include <stdlib.h>
9+
10+#include "luamem.h"
11+#include "lua.h"
12+
13+
14+#define DEBUG 0
15+
16+#if !DEBUG
17+
18+void luaI_free (void *block)
19+{
20+ if (block)
21+ {
22+ *((char *)block) = -1; /* to catch errors */
23+ free(block);
24+ }
25+}
26+
27+
28+void *luaI_realloc (void *oldblock, unsigned long size)
29+{
30+ void *block;
31+ size_t s = (size_t)size;
32+ if (s != size)
33+ lua_error("Allocation Error: Block too big");
34+ block = oldblock ? realloc(oldblock, s) : malloc(s);
35+ if (block == NULL)
36+ lua_error(memEM);
37+ return block;
38+}
39+
40+
41+int luaI_growvector (void **block, unsigned long nelems, int size,
42+ char *errormsg, unsigned long limit)
43+{
44+ if (nelems >= limit)
45+ lua_error(errormsg);
46+ nelems = (nelems == 0) ? 20 : nelems*2;
47+ if (nelems > limit)
48+ nelems = limit;
49+ *block = luaI_realloc(*block, nelems*size);
50+ return (int)nelems;
51+}
52+
53+
54+void* luaI_buffer (unsigned long size)
55+{
56+ static unsigned long buffsize = 0;
57+ static char* buffer = NULL;
58+ if (size > buffsize)
59+ buffer = luaI_realloc(buffer, buffsize=size);
60+ return buffer;
61+}
62+
63+#else
64+/* DEBUG */
65+
66+#include <stdio.h>
67+
68+# define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
69+ "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
70+
71+#define MARK 55
72+
73+static unsigned long numblocks = 0;
74+static unsigned long totalmem = 0;
75+
76+
77+static void message (void)
78+{
79+#define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
80+ static int count = 0;
81+ static unsigned long lastnumblocks = 0;
82+ static unsigned long lasttotalmem = 0;
83+ if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
84+ || count++ >= 5000)
85+ {
86+ fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
87+ count = 0;
88+ lastnumblocks = numblocks;
89+ lasttotalmem = totalmem;
90+ }
91+}
92+
93+
94+void luaI_free (void *block)
95+{
96+ if (block)
97+ {
98+ unsigned long *b = (unsigned long *)block - 1;
99+ unsigned long size = *b;
100+ assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
101+ numblocks--;
102+ totalmem -= size;
103+ free(b);
104+ message();
105+ }
106+}
107+
108+
109+void *luaI_realloc (void *oldblock, unsigned long size)
110+{
111+ unsigned long *block;
112+ unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
113+ if (realsize != (size_t)realsize)
114+ lua_error("Allocation Error: Block too big");
115+ if (oldblock)
116+ {
117+ unsigned long *b = (unsigned long *)oldblock - 1;
118+ unsigned long oldsize = *b;
119+ assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
120+ totalmem -= oldsize;
121+ numblocks--;
122+ block = (unsigned long *)realloc(b, realsize);
123+ }
124+ else
125+ block = (unsigned long *)malloc(realsize);
126+ if (block == NULL)
127+ lua_error("not enough memory");
128+ totalmem += size;
129+ numblocks++;
130+ *block = size;
131+ *(((char *)block)+size+sizeof(unsigned long)) = MARK;
132+ message();
133+ return block+1;
134+}
135+
136+
137+int luaI_growvector (void **block, unsigned long nelems, int size,
138+ char *errormsg, unsigned long limit)
139+{
140+ if (nelems >= limit)
141+ lua_error(errormsg);
142+ nelems = (nelems == 0) ? 20 : nelems*2;
143+ if (nelems > limit)
144+ nelems = limit;
145+ *block = luaI_realloc(*block, nelems*size);
146+ return (int)nelems;
147+}
148+
149+
150+void* luaI_buffer (unsigned long size)
151+{
152+ static unsigned long buffsize = 0;
153+ static char* buffer = NULL;
154+ if (size > buffsize)
155+ buffer = luaI_realloc(buffer, buffsize=size);
156+ return buffer;
157+}
158+
159+#endif
src/luamem.hrenamed from src/mem.h+3−3
@@ -1,11 +1,11 @@
11 /*
22 ** mem.c
33 ** memory manager for lua
4−** $Id: mem.h,v 1.8 1996/05/24 14:31:10 roberto Exp $
4+** $Id: luamem.h,v 1.9 1997/03/31 14:10:11 roberto Exp $
55 */
66
7−#ifndef mem_h
8−#define mem_h
7+#ifndef luamem_h
8+#define luamem_h
99
1010 #ifndef NULL
1111 #define NULL 0
src/mem.c+0−58
@@ -1,58 +0,0 @@
1−/*
2−** mem.c
3−** TecCGraf - PUC-Rio
4−*/
5−
6−char *rcs_mem = "$Id: mem.c,v 1.13 1996/05/24 14:31:10 roberto Exp $";
7−
8−#include <stdlib.h>
9−
10−#include "mem.h"
11−#include "lua.h"
12−
13−
14−void luaI_free (void *block)
15−{
16− if (block)
17− {
18− *((int *)block) = -1; /* to catch errors */
19− free(block);
20− }
21−}
22−
23−
24−void *luaI_realloc (void *oldblock, unsigned long size)
25−{
26− void *block;
27− size_t s = (size_t)size;
28− if (s != size)
29− lua_error("Allocation Error: Block too big");
30− block = oldblock ? realloc(oldblock, s) : malloc(s);
31− if (block == NULL)
32− lua_error(memEM);
33− return block;
34−}
35−
36−
37−int luaI_growvector (void **block, unsigned long nelems, int size,
38− char *errormsg, unsigned long limit)
39−{
40− if (nelems >= limit)
41− lua_error(errormsg);
42− nelems = (nelems == 0) ? 20 : nelems*2;
43− if (nelems > limit)
44− nelems = limit;
45− *block = luaI_realloc(*block, nelems*size);
46− return (int)nelems;
47−}
48−
49−
50−void* luaI_buffer (unsigned long size)
51−{
52− static unsigned long buffsize = 0;
53− static char* buffer = NULL;
54− if (size > buffsize)
55− buffer = luaI_realloc(buffer, buffsize=size);
56− return buffer;
57−}
58−
src/opcode.c+517−369
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $";
6+char *rcs_opcode="$Id: opcode.c,v 4.15 1997/06/26 21:40:57 roberto Exp $";
77
88 #include <setjmp.h>
99 #include <stdio.h>
@@ -11,17 +11,18 @@ char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $"; · 1 unmodified line
1111 #include <stdlib.h>
1212
1313 #include "luadebug.h"
14−#include "mem.h"
14+#include "luamem.h"
1515 #include "opcode.h"
1616 #include "hash.h"
1717 #include "inout.h"
1818 #include "table.h"
1919 #include "lua.h"
2020 #include "fallback.h"
21−#include "undump.h"
21+#include "auxlib.h"
22+#include "lex.h"
2223
23−#define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
24−#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
24+#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
25+#define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
2526
2627
2728 #define STACK_SIZE 128
@@ -32,14 +33,14 @@ char *rcs_opcode="$Id: opcode.c,v 3.77 1996/11/18 13:48:44 roberto Exp $"; · 4 unmodified lines
3233
3334 typedef int StkId; /* index to stack elements */
3435
35−static Object initial_stack;
36+static TObject initial_stack;
3637
37−static Object *stackLimit = &initial_stack+1;
38−static Object *stack = &initial_stack;
39−static Object *top = &initial_stack;
38+static TObject *stackLimit = &initial_stack+1;
39+static TObject *stack = &initial_stack;
40+static TObject *top = &initial_stack;
4041
4142
42−/* macros to convert from lua_Object to (Object *) and back */
43+/* macros to convert from lua_Object to (TObject *) and back */
4344
4445 #define Address(lo) ((lo)+stack-1)
4546 #define Ref(st) ((st)-stack+1)
@@ -51,13 +52,13 @@ static Object *top = &initial_stack; · 5 unmodified lines
5152 #define incr_top if (++top >= stackLimit) growstack()
5253
5354 struct C_Lua_Stack {
54− StkId base; /* when Lua calls C or C calls Lua, points to */
55− /* the first slot after the last parameter. */
56− int num; /* when Lua calls C, has the number of parameters; */
57− /* when C calls Lua, has the number of results. */
55+ StkId base; /* when Lua calls C or C calls Lua, points to */
56+ /* the first slot after the last parameter. */
57+ StkId lua2C; /* points to first element of "array" lua2C */
58+ int num; /* size of "array" lua2C */
5859 };
5960
60−static struct C_Lua_Stack CLS_current = {0, 0};
61+static struct C_Lua_Stack CLS_current = {0, 0, 0};
6162
6263 static jmp_buf *errorJmp = NULL; /* current error recover point */
6364
@@ -72,7 +73,7 @@ static void do_call (StkId base, int nResults); · 8 unmodified lines
7273
7374
7475
75−Object *luaI_Address (lua_Object o)
76+TObject *luaI_Address (lua_Object o)
7677 {
7778 return Address(o);
7879 }
@@ -84,7 +85,7 @@ Object *luaI_Address (lua_Object o) · 5 unmodified lines
8485 static void lua_initstack (void)
8586 {
8687 Long maxstack = STACK_SIZE;
87− stack = newvector(maxstack, Object);
88+ stack = newvector(maxstack, TObject);
8889 stackLimit = stack+maxstack;
8990 top = stack;
9091 *(top++) = initial_stack;
@@ -105,7 +106,7 @@ static void growstack (void) · 14 unmodified lines
105106 static int limit = STACK_LIMIT;
106107 StkId t = top-stack;
107108 Long stacksize = stackLimit - stack;
108− stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
109+ stacksize = growvector(&stack, stacksize, TObject, stackEM, limit+100);
109110 stackLimit = stack+stacksize;
110111 top = stack + t;
111112 if (stacksize >= limit)
@@ -134,16 +135,16 @@ static char *lua_strconc (char *l, char *r) · 22 unmodified lines
134135 ** Convert, if possible, to a number object.
135136 ** Return 0 if success, not 0 if error.
136137 */
137−static int lua_tonumber (Object *obj)
138+static int lua_tonumber (TObject *obj)
138139 {
139140 float t;
140141 char c;
141− if (tag(obj) != LUA_T_STRING)
142+ if (ttype(obj) != LUA_T_STRING)
142143 return 1;
143144 else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
144145 {
145146 nvalue(obj) = t;
146− tag(obj) = LUA_T_NUMBER;
147+ ttype(obj) = LUA_T_NUMBER;
147148 return 0;
148149 }
149150 else
@@ -152,39 +153,54 @@ static int lua_tonumber (Object *obj) · 2 unmodified lines
152153
153154
154155 /*
155−** Convert, if possible, to a string tag
156+** Convert, if possible, to a string ttype
156157 ** Return 0 in success or not 0 on error.
157158 */
158−static int lua_tostring (Object *obj)
159+static int lua_tostring (TObject *obj)
159160 {
160− char s[256];
161− if (tag(obj) != LUA_T_NUMBER)
162− return 1;
163− if ((int) nvalue(obj) == nvalue(obj))
164− sprintf (s, "%d", (int) nvalue(obj));
165− else
166− sprintf (s, "%g", nvalue(obj));
167− tsvalue(obj) = lua_createstring(s);
168− tag(obj) = LUA_T_STRING;
169− return 0;
161+ if (ttype(obj) != LUA_T_NUMBER)
162+ return 1;
163+ else {
164+ char s[60];
165+ real f = nvalue(obj);
166+ int i;
167+ if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
168+ sprintf (s, "%d", i);
169+ else
170+ sprintf (s, "%g", nvalue(obj));
171+ tsvalue(obj) = lua_createstring(s);
172+ ttype(obj) = LUA_T_STRING;
173+ return 0;
174+ }
170175 }
171176
172177
173178 /*
174179 ** Adjust stack. Set top to the given value, pushing NILs if needed.
175180 */
176−static void adjust_top (StkId newtop)
181+static void adjust_top_aux (StkId newtop)
177182 {
178− Object *nt;
183+ TObject *nt;
179184 lua_checkstack(stack+newtop);
180185 nt = stack+newtop; /* warning: previous call may change stack */
181− while (top < nt) tag(top++) = LUA_T_NIL;
182− top = nt; /* top could be bigger than newtop */
186+ while (top < nt) ttype(top++) = LUA_T_NIL;
183187 }
184188
189+
190+#define adjust_top(newtop) { if (newtop <= top-stack) \
191+ top = stack+newtop; \
192+ else adjust_top_aux(newtop); }
193+
185194 #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
186195
187196
197+static void checkCparams (int nParams)
198+{
199+ if (top-stack < CLS_current.base+nParams)
200+ lua_error("API error - wrong number of arguments in C2lua stack");
201+}
202+
203+
188204 /*
189205 ** Open a hole below "nelems" from the top.
190206 */
@@ -197,13 +213,37 @@ static void open_stack (int nelems) · 6 unmodified lines
197213 }
198214
199215
216+static lua_Object put_luaObject (TObject *o)
217+{
218+ open_stack((top-stack)-CLS_current.base);
219+ stack[CLS_current.base++] = *o;
220+ return CLS_current.base; /* this is +1 real position (see Ref) */
221+}
222+
223+
224+static lua_Object put_luaObjectonTop (void)
225+{
226+ open_stack((top-stack)-CLS_current.base);
227+ stack[CLS_current.base++] = *(--top);
228+ return CLS_current.base; /* this is +1 real position (see Ref) */
229+}
230+
231+
232+lua_Object lua_pop (void)
233+{
234+ checkCparams(1);
235+ return put_luaObjectonTop();
236+}
237+
238+
239+
200240 /*
201241 ** call Line hook
202242 */
203243 static void lineHook (int line)
204244 {
205245 struct C_Lua_Stack oldCLS = CLS_current;
206− StkId old_top = CLS_current.base = top-stack;
246+ StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
207247 CLS_current.num = 0;
208248 (*lua_linehook)(line);
209249 top = stack+old_top;
@@ -218,13 +258,13 @@ static void lineHook (int line) · 8 unmodified lines
218258 static void callHook (StkId base, lua_Type type, int isreturn)
219259 {
220260 struct C_Lua_Stack oldCLS = CLS_current;
221− StkId old_top = CLS_current.base = top-stack;
261+ StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
222262 CLS_current.num = 0;
223263 if (isreturn)
224264 (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
225265 else
226266 {
227− Object *f = stack+base-1;
267+ TObject *f = stack+base-1;
228268 if (type == LUA_T_MARK)
229269 (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
230270 else
@@ -246,6 +286,7 @@ static StkId callC (lua_CFunction func, StkId base) · 15 unmodified lines
246286 StkId firstResult;
247287 CLS_current.num = (top-stack) - base;
248288 /* incorporate parameters on the stack */
289+ CLS_current.lua2C = base;
249290 CLS_current.base = base+CLS_current.num; /* == top-stack */
250291 if (lua_callhook)
251292 callHook(base, LUA_T_CMARK, 0);
@@ -257,15 +298,11 @@ static StkId callC (lua_CFunction func, StkId base) · 5 unmodified lines
257298 return firstResult;
258299 }
259300
260−/*
261−** Call the specified fallback, putting it on the stack below its arguments
262−*/
263−static void callFB (int fb)
301+static void callIM (TObject *f, int nParams, int nResults)
264302 {
265− int nParams = luaI_fallBacks[fb].nParams;
266303 open_stack(nParams);
267− *(top-nParams-1) = luaI_fallBacks[fb].function;
268− do_call((top-stack)-nParams, luaI_fallBacks[fb].nResults);
304+ *(top-nParams-1) = *f;
305+ do_call((top-stack)-nParams, nResults);
269306 }
270307
271308
@@ -278,28 +315,28 @@ static void callFB (int fb) · 6 unmodified lines
278315 static void do_call (StkId base, int nResults)
279316 {
280317 StkId firstResult;
281− Object *func = stack+base-1;
318+ TObject *func = stack+base-1;
282319 int i;
283− if (tag(func) == LUA_T_CFUNCTION)
284− {
285− tag(func) = LUA_T_CMARK;
320+ if (ttype(func) == LUA_T_CFUNCTION) {
321+ ttype(func) = LUA_T_CMARK;
286322 firstResult = callC(fvalue(func), base);
287323 }
288− else if (tag(func) == LUA_T_FUNCTION)
289− {
290− tag(func) = LUA_T_MARK;
324+ else if (ttype(func) == LUA_T_FUNCTION) {
325+ ttype(func) = LUA_T_MARK;
291326 firstResult = lua_execute(func->value.tf->code, base);
292327 }
293− else
294− { /* func is not a function */
295− /* Call the fallback for invalid functions */
328+ else { /* func is not a function */
329+ /* Check the tag method for invalid functions */
330+ TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
331+ if (ttype(im) == LUA_T_NIL)
332+ lua_error("call expression not a function");
296333 open_stack((top-stack)-(base-1));
297− stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;
334+ stack[base-1] = *im;
298335 do_call(base, nResults);
299336 return;
300337 }
301338 /* adjust the number of results */
302− if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
339+ if (nResults != MULT_RET)
303340 adjust_top(firstResult+nResults);
304341 /* move results to base-1 (to erase parameters and function) */
305342 base--;
@@ -316,58 +353,110 @@ static void do_call (StkId base, int nResults) · 10 unmodified lines
316353 */
317354 static void pushsubscript (void)
318355 {
319− if (tag(top-2) != LUA_T_ARRAY)
320− callFB(FB_GETTABLE);
321− else
322− {
323− Object *h = lua_hashget(avalue(top-2), top-1);
324− if (h == NULL || tag(h) == LUA_T_NIL)
325− callFB(FB_INDEX);
326− else
327− {
328− --top;
329− *(top-1) = *h;
356+ TObject *im;
357+ if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
358+ im = luaI_getimbyObj(top-2, IM_GETTABLE);
359+ else { /* object is a table... */
360+ int tg = (top-2)->value.a->htag;
361+ im = luaI_getim(tg, IM_GETTABLE);
362+ if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
363+ TObject *h = lua_hashget(avalue(top-2), top-1);
364+ if (h != NULL && ttype(h) != LUA_T_NIL) {
365+ --top;
366+ *(top-1) = *h;
367+ }
368+ else if (ttype(im=luaI_getim(tg, IM_INDEX)) != LUA_T_NIL)
369+ callIM(im, 2, 1);
370+ else {
371+ --top;
372+ ttype(top-1) = LUA_T_NIL;
373+ }
374+ return;
330375 }
376+ /* else it has a "gettable" method, go through to next command */
377+ }
378+ /* object is not a table, or it has a "gettable" method */
379+ if (ttype(im) != LUA_T_NIL)
380+ callIM(im, 2, 1);
381+ else
382+ lua_error("indexed expression not a table");
383+}
384+
385+
386+lua_Object lua_rawgettable (void)
387+{
388+ checkCparams(2);
389+ if (ttype(top-2) != LUA_T_ARRAY)
390+ lua_error("indexed expression not a table in raw gettable");
391+ else {
392+ TObject *h = lua_hashget(avalue(top-2), top-1);
393+ --top;
394+ if (h != NULL)
395+ *(top-1) = *h;
396+ else
397+ ttype(top-1) = LUA_T_NIL;
331398 }
399+ return put_luaObjectonTop();
332400 }
333401
334402
335403 /*
336404 ** Function to store indexed based on values at the top
405+** mode = 0: raw store (without internal methods)
406+** mode = 1: normal store (with internal methods)
407+** mode = 2: "deep stack" store (with internal methods)
337408 */
338−static void storesubscript (void)
409+static void storesubscript (TObject *t, int mode)
339410 {
340− if (tag(top-3) != LUA_T_ARRAY)
341− callFB(FB_SETTABLE);
342− else
343− {
344− Object *h = lua_hashdefine (avalue(top-3), top-2);
345− *h = *(top-1);
346− top -= 3;
347− }
411+ TObject *im = (mode == 0) ? NULL : luaI_getimbyObj(t, IM_SETTABLE);
412+ if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
413+ TObject *h = lua_hashdefine(avalue(t), t+1);
414+ *h = *(top-1);
415+ top -= (mode == 2) ? 1 : 3;
416+ }
417+ else { /* object is not a table, and/or has a specific "settable" method */
418+ if (im && ttype(im) != LUA_T_NIL) {
419+ if (mode == 2) {
420+ lua_checkstack(top+2);
421+ *(top+1) = *(top-1);
422+ *(top) = *(t+1);
423+ *(top-1) = *t;
424+ top += 2;
425+ }
426+ callIM(im, 3, 0);
427+ }
428+ else
429+ lua_error("indexed expression not a table");
430+ }
348431 }
349432
350433
351434 static void getglobal (Word n)
352435 {
353− *top = lua_table[n].object;
354− incr_top;
355− if (tag(top-1) == LUA_T_NIL)
356− { /* must call getglobal fallback */
357− tag(top-1) = LUA_T_STRING;
358− tsvalue(top-1) = lua_table[n].varname;
359− callFB(FB_GETGLOBAL);
436+ TObject *value = &lua_table[n].object;
437+ TObject *im = luaI_getimbyObj(value, IM_GETGLOBAL);
438+ if (ttype(im) == LUA_T_NIL) { /* default behavior */
439+ *top = *value;
440+ incr_top;
360441 }
442+ else {
443+ ttype(top) = LUA_T_STRING;
444+ tsvalue(top) = lua_table[n].varname;
445+ incr_top;
446+ *top = *value;
447+ incr_top;
448+ callIM(im, 2, 1);
449+ }
361450 }
362451
363452 /*
364453 ** Traverse all objects on stack
365454 */
366−void lua_travstack (int (*fn)(Object *))
455+void lua_travstack (int (*fn)(TObject *))
367456 {
368− Object *o;
369− for (o = top-1; o >= stack; o--)
370− fn (o);
457+ StkId i;
458+ for (i = (top-1)-stack; i>=0; i--)
459+ fn (stack+i);
371460 }
372461
373462
@@ -377,8 +466,11 @@ void lua_travstack (int (*fn)(Object *)) · 3 unmodified lines
377466
378467 static void lua_message (char *s)
379468 {
380− lua_pushstring(s);
381− callFB(FB_ERROR);
469+ TObject *im = luaI_geterrorim();
470+ if (ttype(im) != LUA_T_NIL) {
471+ lua_pushstring(s);
472+ callIM(im, 1, 0);
473+ }
382474 }
383475
384476 /*
@@ -399,25 +491,25 @@ void lua_error (char *s) · 14 unmodified lines
399491
400492 lua_Function lua_stackedfunction (int level)
401493 {
402− Object *p = top;
403− while (--p >= stack)
404− if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)
494+ StkId i;
495+ for (i = (top-1)-stack; i>=0; i--)
496+ if (stack[i].ttype == LUA_T_MARK || stack[i].ttype == LUA_T_CMARK)
405497 if (level-- == 0)
406− return Ref(p);
498+ return Ref(stack+i);
407499 return LUA_NOOBJECT;
408500 }
409501
410502
411503 int lua_currentline (lua_Function func)
412504 {
413− Object *f = Address(func);
414− return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
505+ TObject *f = Address(func);
506+ return (f+1 < top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
415507 }
416508
417509
418510 lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
419511 {
420− Object *f = luaI_Address(func);
512+ TObject *f = luaI_Address(func);
421513 *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
422514 if (*name)
423515 {
@@ -431,9 +523,9 @@ lua_Object lua_getlocal (lua_Function func, int local_number, char **name) · 7 unmodified lines
431523
432524 int lua_setlocal (lua_Function func, int local_number)
433525 {
434− Object *f = Address(func);
526+ TObject *f = Address(func);
435527 char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
436− adjustC(1);
528+ checkCparams(1);
437529 --top;
438530 if (name)
439531 {
@@ -446,6 +538,29 @@ int lua_setlocal (lua_Function func, int local_number) · 6 unmodified lines
446538 return 0;
447539 }
448540
541+/*
542+** Call the function at CLS_current.base, and incorporate results on
543+** the Lua2C structure.
544+*/
545+static void do_callinc (int nResults)
546+{
547+ StkId base = CLS_current.base;
548+ do_call(base+1, nResults);
549+ CLS_current.lua2C = base; /* position of the new results */
550+ CLS_current.num = (top-stack) - base; /* number of results */
551+ CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
552+}
553+
554+
555+static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
556+{
557+ StkId base = (top-stack)-nParams;
558+ open_stack(nParams);
559+ stack[base].ttype = LUA_T_CFUNCTION;
560+ stack[base].value.f = f;
561+ do_call(base+1, nResults);
562+}
563+
449564
450565 /*
451566 ** Execute a protected call. Assumes that function is at CLS_current.base and
@@ -458,15 +573,11 @@ static int do_protectedrun (int nResults) · 6 unmodified lines
458573 struct C_Lua_Stack oldCLS = CLS_current;
459574 jmp_buf *oldErr = errorJmp;
460575 errorJmp = &myErrorJmp;
461− if (setjmp(myErrorJmp) == 0)
462− {
463− do_call(CLS_current.base+1, nResults);
464− CLS_current.num = (top-stack) - CLS_current.base; /* number of results */
465− CLS_current.base += CLS_current.num; /* incorporate results on the stack */
576+ if (setjmp(myErrorJmp) == 0) {
577+ do_callinc(nResults);
466578 status = 0;
467579 }
468− else
469− { /* an error occurred: restore CLS_current and top */
580+ else { /* an error occurred: restore CLS_current and top */
470581 CLS_current = oldCLS;
471582 top = stack+CLS_current.base;
472583 status = 1;
@@ -479,13 +590,14 @@ int luaI_dorun (TFunc *tf) · 6 unmodified lines
479590 {
480591 int status;
481592 adjustC(1); /* one slot for the pseudo-function */
482− stack[CLS_current.base].tag = LUA_T_FUNCTION;
593+ stack[CLS_current.base].ttype = LUA_T_FUNCTION;
483594 stack[CLS_current.base].value.tf = tf;
484595 status = do_protectedrun(MULT_RET);
485596 return status;
486597 }
487598
488−static int do_protectedmain (void)
599+
600+int lua_domain (void)
489601 {
490602 TFunc tf;
491603 int status;
@@ -493,23 +605,21 @@ static int do_protectedmain (void) · 1 unmodified line
493605 jmp_buf *oldErr = errorJmp;
494606 errorJmp = &myErrorJmp;
495607 luaI_initTFunc(&tf);
496− tf.fileName = lua_parsedfile;
497− if (setjmp(myErrorJmp) == 0)
498− {
608+ if (setjmp(myErrorJmp) == 0) {
499609 lua_parse(&tf);
500− status = luaI_dorun(&tf);
610+ status = 0;
501611 }
502− else
503− {
504− status = 1;
612+ else {
505613 adjustC(0); /* erase extra slot */
614+ status = 1;
506615 }
616+ if (status == 0)
617+ status = luaI_dorun(&tf);
507618 errorJmp = oldErr;
508619 luaI_free(tf.code);
509620 return status;
510621 }
511622
512−
513623 /*
514624 ** Execute the given lua function. Return 0 on success or 1 on error.
515625 */
@@ -526,71 +636,31 @@ int lua_callfunction (lua_Object function) · 10 unmodified lines
526636 }
527637
528638
529−int lua_call (char *funcname)
639+lua_Object lua_gettagmethod (int tag, char *event)
530640 {
531− Word n = luaI_findsymbolbyname(funcname);
532− open_stack((top-stack)-CLS_current.base);
533− stack[CLS_current.base] = s_object(n);
534− return do_protectedrun(MULT_RET);
641+ lua_pushnumber(tag);
642+ lua_pushstring(event);
643+ do_unprotectedrun(luaI_gettagmethod, 2, 1);
644+ return put_luaObjectonTop();
535645 }
536646
537−
538−/*
539−** Open file, generate opcode and execute global statement. Return 0 on
540−** success or non 0 on error.
541−*/
542−int lua_dofile (char *filename)
647+lua_Object lua_settagmethod (int tag, char *event)
543648 {
544− int status;
545− int c;
546− FILE *f = lua_openfile(filename);
547− if (f == NULL)
548− return 2;
549− c = fgetc(f);
550− ungetc(c, f);
551− if (c == ID_CHUNK) {
552− f = freopen(filename, "rb", f); /* set binary mode */
553− status = luaI_undump(f);
554− }
555− else {
556− if (c == '#')
557− while ((c=fgetc(f)) != '\n') /* skip first line */;
558− status = do_protectedmain();
559− }
560− lua_closefile();
561− return status;
649+ TObject newmethod;
650+ checkCparams(1);
651+ newmethod = *(--top);
652+ lua_pushnumber(tag);
653+ lua_pushstring(event);
654+ *top = newmethod; incr_top;
655+ do_unprotectedrun(luaI_settagmethod, 3, 1);
656+ return put_luaObjectonTop();
562657 }
563658
564−/*
565−** Generate opcode stored on string and execute global statement. Return 0 on
566−** success or non 0 on error.
567−*/
568−int lua_dostring (char *str)
659+lua_Object lua_seterrormethod (void)
569660 {
570− int status;
571− if (str == NULL)
572− return 1;
573− lua_openstring(str);
574− status = do_protectedmain();
575− lua_closestring();
576− return status;
577−}
578−
579−
580−/*
581−** API: set a function as a fallback
582−*/
583−lua_Object lua_setfallback (char *name, lua_CFunction fallback)
584−{
585− adjustC(1); /* one slot for the pseudo-function */
586− stack[CLS_current.base].tag = LUA_T_CFUNCTION;
587− stack[CLS_current.base].value.f = luaI_setfallback;
588− lua_pushstring(name);
589− lua_pushcfunction(fallback);
590− if (do_protectedrun(1) == 0)
591− return (Ref(top-1));
592− else
593− return LUA_NOOBJECT;
661+ checkCparams(1);
662+ do_unprotectedrun(luaI_seterrormethod, 1, 1);
663+ return put_luaObjectonTop();
594664 }
595665
596666
@@ -598,12 +668,11 @@ lua_Object lua_setfallback (char *name, lua_CFunction fallback) · 1 unmodified line
598668 ** API: receives on the stack the table and the index.
599669 ** returns the value.
600670 */
601−lua_Object lua_getsubscript (void)
671+lua_Object lua_gettable (void)
602672 {
603− adjustC(2);
673+ checkCparams(2);
604674 pushsubscript();
605− CLS_current.base++; /* incorporate object in the stack */
606− return (Ref(top-1));
675+ return put_luaObjectonTop();
607676 }
608677
609678
@@ -633,13 +702,25 @@ void lua_endblock (void) · 23 unmodified lines
633702 adjustC(0);
634703 }
635704
705+void lua_settag (int tag)
706+{
707+ checkCparams(1);
708+ luaI_settag(tag, --top);
709+}
710+
636711 /*
637712 ** API: receives on the stack the table, the index, and the new value.
638713 */
639−void lua_storesubscript (void)
714+void lua_settable (void)
640715 {
641− adjustC(3);
642− storesubscript();
716+ checkCparams(3);
717+ storesubscript(top-3, 1);
718+}
719+
720+void lua_rawsettable (void)
721+{
722+ checkCparams(3);
723+ storesubscript(top-3, 0);
643724 }
644725
645726 /*
@@ -647,40 +728,59 @@ void lua_storesubscript (void) · 1 unmodified line
647728 */
648729 lua_Object lua_createtable (void)
649730 {
650− adjustC(0);
651− avalue(top) = lua_createarray(0);
652− tag(top) = LUA_T_ARRAY;
653− incr_top;
654− CLS_current.base++; /* incorporate object in the stack */
655− return Ref(top-1);
731+ TObject o;
732+ avalue(&o) = lua_createarray(0);
733+ ttype(&o) = LUA_T_ARRAY;
734+ return put_luaObject(&o);
656735 }
657736
658737 /*
659738 ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
660739 ** 'number' must be 1 to get the first parameter.
661740 */
662−lua_Object lua_getparam (int number)
741+lua_Object lua_lua2C (int number)
742+{
743+ if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
744+ /* Ref(stack+(CLS_current.lua2C+number-1)) ==
745+ stack+(CLS_current.lua2C+number-1)-stack+1 == */
746+ return CLS_current.lua2C+number;
747+}
748+
749+int lua_isnil (lua_Object o)
750+{
751+ return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
752+}
753+
754+int lua_istable (lua_Object o)
755+{
756+ return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
757+}
758+
759+int lua_isuserdata (lua_Object o)
663760 {
664− if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
665− /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
666− stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
667− return CLS_current.base-CLS_current.num+number;
761+ return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
668762 }
669763
670−int lua_isnumber (lua_Object object)
764+int lua_iscfunction (lua_Object o)
671765 {
672− return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);
766+ int t = lua_tag(o);
767+ return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
673768 }
674769
675−int lua_isstring (lua_Object object)
770+int lua_isnumber (lua_Object o)
676771 {
677− int t = lua_type(object);
772+ return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
773+}
774+
775+int lua_isstring (lua_Object o)
776+{
777+ int t = lua_tag(o);
678778 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
679779 }
680780
681−int lua_isfunction (lua_Object object)
781+int lua_isfunction (lua_Object o)
682782 {
683− int t = lua_type(object);
783+ int t = lua_tag(o);
684784 return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
685785 (t == LUA_T_MARK) || (t == LUA_T_CMARK);
686786 }
@@ -700,57 +800,44 @@ real lua_getnumber (lua_Object object) · 13 unmodified lines
700800 */
701801 char *lua_getstring (lua_Object object)
702802 {
703− if (object == LUA_NOOBJECT) return NULL;
704− if (tostring (Address(object))) return NULL;
705− else return (svalue(Address(object)));
803+ if (object == LUA_NOOBJECT || tostring (Address(object)))
804+ return NULL;
805+ else return (svalue(Address(object)));
706806 }
707807
708−/*
709−** Given an object handle, return its cfuntion pointer. On error, return NULL.
710−*/
711−lua_CFunction lua_getcfunction (lua_Object object)
808+
809+void *lua_getuserdata (lua_Object object)
712810 {
713− if (object == LUA_NOOBJECT || ((tag(Address(object)) != LUA_T_CFUNCTION) &&
714− (tag(Address(object)) != LUA_T_CMARK)))
715− return NULL;
716− else return (fvalue(Address(object)));
811+ if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
812+ return NULL;
813+ else return tsvalue(Address(object))->u.v;
717814 }
718815
816+
719817 /*
720−** Given an object handle, return its user data. On error, return NULL.
818+** Given an object handle, return its cfuntion pointer. On error, return NULL.
721819 */
722−void *lua_getuserdata (lua_Object object)
820+lua_CFunction lua_getcfunction (lua_Object object)
723821 {
724− if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
822+ if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
823+ (ttype(Address(object)) != LUA_T_CMARK)))
725824 return NULL;
726− else return (uvalue(Address(object)));
825+ else return (fvalue(Address(object)));
727826 }
728827
729828
730829 lua_Object lua_getref (int ref)
731830 {
732− Object *o = luaI_getref(ref);
831+ TObject *o = luaI_getref(ref);
733832 if (o == NULL)
734833 return LUA_NOOBJECT;
735− adjustC(0);
736− luaI_pushobject(o);
737− CLS_current.base++; /* incorporate object in the stack */
738− return Ref(top-1);
739−}
740−
741−
742−void lua_pushref (int ref)
743−{
744− Object *o = luaI_getref(ref);
745− if (o == NULL)
746− lua_error("access to invalid (possibly garbage collected) reference");
747− luaI_pushobject(o);
834+ return put_luaObject(o);
748835 }
749836
750837
751838 int lua_ref (int lock)
752839 {
753− adjustC(1);
840+ checkCparams(1);
754841 return luaI_ref(--top, lock);
755842 }
756843
@@ -761,20 +848,50 @@ int lua_ref (int lock) · 4 unmodified lines
761848 */
762849 lua_Object lua_getglobal (char *name)
763850 {
764− adjustC(0);
765− getglobal(luaI_findsymbolbyname(name));
766− CLS_current.base++; /* incorporate object in the stack */
767− return Ref(top-1);
851+ getglobal(luaI_findsymbolbyname(name));
852+ return put_luaObjectonTop();
853+}
854+
855+
856+lua_Object lua_rawgetglobal (char *name)
857+{
858+ return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
768859 }
769860
861+
770862 /*
771863 ** Store top of the stack at a global variable array field.
772864 */
773−void lua_storeglobal (char *name)
865+static void setglobal (Word n)
866+{
867+ TObject *oldvalue = &lua_table[n].object;
868+ TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
869+ if (ttype(im) == LUA_T_NIL) /* default behavior */
870+ s_object(n) = *(--top);
871+ else {
872+ TObject newvalue = *(top-1);
873+ ttype(top-1) = LUA_T_STRING;
874+ tsvalue(top-1) = lua_table[n].varname;
875+ *top = *oldvalue;
876+ incr_top;
877+ *top = newvalue;
878+ incr_top;
879+ callIM(im, 3, 0);
880+ }
881+}
882+
883+
884+void lua_setglobal (char *name)
774885 {
775− Word n = luaI_findsymbolbyname(name);
776− adjustC(1);
777− s_object(n) = *(--top);
886+ checkCparams(1);
887+ setglobal(luaI_findsymbolbyname(name));
888+}
889+
890+void lua_rawsetglobal (char *name)
891+{
892+ Word n = luaI_findsymbolbyname(name);
893+ checkCparams(1);
894+ s_object(n) = *(--top);
778895 }
779896
780897 /*
@@ -782,60 +899,59 @@ void lua_storeglobal (char *name) · 1 unmodified line
782899 */
783900 void lua_pushnil (void)
784901 {
785− tag(top) = LUA_T_NIL;
786− incr_top;
902+ ttype(top) = LUA_T_NIL;
903+ incr_top;
787904 }
788905
789906 /*
790−** Push an object (tag=number) to stack.
907+** Push an object (ttype=number) to stack.
791908 */
792909 void lua_pushnumber (real n)
793910 {
794− tag(top) = LUA_T_NUMBER; nvalue(top) = n;
911+ ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
795912 incr_top;
796913 }
797914
798915 /*
799−** Push an object (tag=string) to stack.
916+** Push an object (ttype=string) to stack.
800917 */
801918 void lua_pushstring (char *s)
802919 {
803920 if (s == NULL)
804− tag(top) = LUA_T_NIL;
921+ ttype(top) = LUA_T_NIL;
805922 else
806923 {
807924 tsvalue(top) = lua_createstring(s);
808− tag(top) = LUA_T_STRING;
925+ ttype(top) = LUA_T_STRING;
809926 }
810927 incr_top;
811928 }
812−/*>>>>>>>>>#undef lua_pushliteral
813−void lua_pushliteral(char *s) { lua_pushstring(s); }*/
929+
814930
815931 /*
816−** Push an object (tag=cfunction) to stack.
932+** Push an object (ttype=cfunction) to stack.
817933 */
818934 void lua_pushcfunction (lua_CFunction fn)
819935 {
820− tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
936+ ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
821937 incr_top;
822938 }
823939
824−/*
825−** Push an object (tag=userdata) to stack.
826−*/
940+
941+
827942 void lua_pushusertag (void *u, int tag)
828943 {
829− if (tag < LUA_T_USERDATA)
830− lua_error("invalid tag in `lua_pushusertag'");
831− tag(top) = tag; uvalue(top) = u;
832− incr_top;
944+ if (tag < 0 && tag != LUA_ANYTAG)
945+ luaI_realtag(tag); /* error if tag is not valid */
946+ tsvalue(top) = luaI_createudata(u, tag);
947+ ttype(top) = LUA_T_USERDATA;
948+ incr_top;
833949 }
834950
835951 /*
836952 ** Push an object on the stack.
837953 */
838−void luaI_pushobject (Object *o)
954+void luaI_pushobject (TObject *o)
839955 {
840956 *top = *o;
841957 incr_top;
@@ -847,78 +963,102 @@ void luaI_pushobject (Object *o) · 5 unmodified lines
847963 void lua_pushobject (lua_Object o)
848964 {
849965 if (o == LUA_NOOBJECT)
850− lua_error("attempt to push a NOOBJECT");
966+ lua_error("API error - attempt to push a NOOBJECT");
851967 *top = *Address(o);
852− if (tag(top) == LUA_T_MARK) tag(top) = LUA_T_FUNCTION;
853− else if (tag(top) == LUA_T_CMARK) tag(top) = LUA_T_CFUNCTION;
968+ if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
969+ else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
854970 incr_top;
855971 }
856972
857−int lua_type (lua_Object o)
973+int lua_tag (lua_Object lo)
858974 {
859− if (o == LUA_NOOBJECT)
860− return LUA_T_NIL;
861− else
862− return tag(Address(o));
975+ if (lo == LUA_NOOBJECT) return LUA_T_NIL;
976+ else {
977+ TObject *o = Address(lo);
978+ lua_Type t = ttype(o);
979+ if (t == LUA_T_USERDATA)
980+ return o->value.ts->tag;
981+ else if (t == LUA_T_ARRAY)
982+ return o->value.a->htag;
983+ else return t;
984+ }
863985 }
864986
865987
866−void luaI_gcFB (Object *o)
988+void luaI_gcIM (TObject *o)
867989 {
868− *top = *o;
869− incr_top;
870− callFB(FB_GC);
990+ TObject *im = luaI_getimbyObj(o, IM_GC);
991+ if (ttype(im) != LUA_T_NIL) {
992+ *top = *o;
993+ incr_top;
994+ callIM(im, 1, 0);
995+ }
996+}
997+
998+
999+static void call_binTM (IMS event, char *msg)
1000+{
1001+ TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
1002+ if (ttype(im) == LUA_T_NIL) {
1003+ im = luaI_getimbyObj(top-1, event); /* try second operand */
1004+ if (ttype(im) == LUA_T_NIL) {
1005+ im = luaI_getim(0, event); /* try a 'global' i.m. */
1006+ if (ttype(im) == LUA_T_NIL)
1007+ lua_error(msg);
1008+ }
1009+ }
1010+ lua_pushstring(luaI_eventname[event]);
1011+ callIM(im, 3, 1);
8711012 }
8721013
8731014
874−static void call_arith (char *op)
1015+static void call_arith (IMS event)
8751016 {
876− lua_pushstring(op);
877− callFB(FB_ARITH);
1017+ call_binTM(event, "unexpected type at arithmetic operation");
8781018 }
8791019
880−static void comparison (lua_Type tag_less, lua_Type tag_equal,
881− lua_Type tag_great, char *op)
1020+
1021+static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
1022+ lua_Type ttype_great, IMS op)
8821023 {
883− Object *l = top-2;
884− Object *r = top-1;
1024+ TObject *l = top-2;
1025+ TObject *r = top-1;
8851026 int result;
886− if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
1027+ if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
8871028 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
888− else if (tostring(l) || tostring(r))
889− {
890− lua_pushstring(op);
891− callFB(FB_ORDER);
1029+ else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
1030+ result = strcmp(svalue(l), svalue(r));
1031+ else {
1032+ call_binTM(op, "unexpected type at comparison");
8921033 return;
8931034 }
894− else
895− result = strcmp(svalue(l), svalue(r));
8961035 top--;
8971036 nvalue(top-1) = 1;
898− tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
1037+ ttype(top-1) = (result < 0) ? ttype_less :
1038+ (result == 0) ? ttype_equal : ttype_great;
8991039 }
9001040
9011041
9021042 static void adjust_varargs (StkId first_extra_arg)
9031043 {
904− Object arg;
905− Object *firstelem = stack+first_extra_arg;
1044+ TObject arg;
1045+ TObject *firstelem = stack+first_extra_arg;
9061046 int nvararg = top-firstelem;
9071047 int i;
9081048 if (nvararg < 0) nvararg = 0;
9091049 avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
910− tag(&arg) = LUA_T_ARRAY;
1050+ ttype(&arg) = LUA_T_ARRAY;
9111051 for (i=0; i<nvararg; i++) {
912− Object index;
913− tag(&index) = LUA_T_NUMBER;
1052+ TObject index;
1053+ ttype(&index) = LUA_T_NUMBER;
9141054 nvalue(&index) = i+1;
9151055 *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
9161056 }
9171057 /* store counter in field "n" */ {
918− Object index, extra;
919− tag(&index) = LUA_T_STRING;
1058+ TObject index, extra;
1059+ ttype(&index) = LUA_T_STRING;
9201060 tsvalue(&index) = lua_createstring("n");
921− tag(&extra) = LUA_T_NUMBER;
1061+ ttype(&extra) = LUA_T_NUMBER;
9221062 nvalue(&extra) = nvararg;
9231063 *(lua_hashdefine(avalue(&arg), &index)) = extra;
9241064 }
@@ -942,22 +1082,22 @@ static StkId lua_execute (Byte *pc, StkId base) · 17 unmodified lines
9421082 OpCode opcode;
9431083 switch (opcode = (OpCode)*pc++)
9441084 {
945− case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
1085+ case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
9461086
9471087 case PUSH0: case PUSH1: case PUSH2:
948− tag(top) = LUA_T_NUMBER;
1088+ ttype(top) = LUA_T_NUMBER;
9491089 nvalue(top) = opcode-PUSH0;
9501090 incr_top;
9511091 break;
9521092
9531093 case PUSHBYTE:
954− tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
1094+ ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
9551095
9561096 case PUSHWORD:
9571097 {
9581098 Word w;
9591099 get_word(w,pc);
960− tag(top) = LUA_T_NUMBER; nvalue(top) = w;
1100+ ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
9611101 incr_top;
9621102 }
9631103 break;
@@ -966,7 +1106,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
9661106 {
9671107 real num;
9681108 get_float(num,pc);
969− tag(top) = LUA_T_NUMBER; nvalue(top) = num;
1109+ ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
9701110 incr_top;
9711111 }
9721112 break;
@@ -975,7 +1115,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
9751115 {
9761116 Word w;
9771117 get_word(w,pc);
978− tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
1118+ ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
9791119 incr_top;
9801120 }
9811121 break;
@@ -985,7 +1125,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 3 unmodified lines
9851125 TFunc *f;
9861126 get_code(f,pc);
9871127 luaI_insertfunction(f); /* may take part in GC */
988− top->tag = LUA_T_FUNCTION;
1128+ top->ttype = LUA_T_FUNCTION;
9891129 top->value.tf = f;
9901130 incr_top;
9911131 }
@@ -1013,10 +1153,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 21 unmodified lines
10131153
10141154 case PUSHSELF:
10151155 {
1016− Object receiver = *(top-1);
1156+ TObject receiver = *(top-1);
10171157 Word w;
10181158 get_word(w,pc);
1019− tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
1159+ ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
10201160 incr_top;
10211161 pushsubscript();
10221162 *top = receiver;
@@ -1037,47 +1177,32 @@ static StkId lua_execute (Byte *pc, StkId base) · 14 unmodified lines
10371177 {
10381178 Word w;
10391179 get_word(w,pc);
1040− s_object(w) = *(--top);
1180+ setglobal(w);
10411181 }
10421182 break;
10431183
10441184 case STOREINDEXED0:
1045− storesubscript();
1185+ storesubscript(top-3, 1);
10461186 break;
10471187
1048− case STOREINDEXED:
1049− {
1050− int n = *pc++;
1051− if (tag(top-3-n) != LUA_T_ARRAY)
1052− {
1053− lua_checkstack(top+2);
1054− *(top+1) = *(top-1);
1055− *(top) = *(top-2-n);
1056− *(top-1) = *(top-3-n);
1057− top += 2;
1058− callFB(FB_SETTABLE);
1059− }
1060− else
1061− {
1062− Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
1063− *h = *(top-1);
1064− top--;
1065− }
1188+ case STOREINDEXED: {
1189+ int n = *pc++;
1190+ storesubscript(top-3-n, 2);
1191+ break;
10661192 }
1067− break;
10681193
10691194 case STORELIST0:
10701195 case STORELIST:
10711196 {
10721197 int m, n;
1073− Object *arr;
1198+ TObject *arr;
10741199 if (opcode == STORELIST0) m = 0;
10751200 else m = *(pc++) * FIELDS_PER_FLUSH;
10761201 n = *(pc++);
10771202 arr = top-n-1;
10781203 while (n)
10791204 {
1080− tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
1205+ ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
10811206 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
10821207 top--;
10831208 n--;
@@ -1085,15 +1210,15 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
10851210 }
10861211 break;
10871212
1088− case STORERECORD:
1213+ case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
10891214 {
10901215 int n = *(pc++);
1091− Object *arr = top-n-1;
1216+ TObject *arr = top-n-1;
10921217 while (n)
10931218 {
10941219 Word w;
10951220 get_word(w,pc);
1096− tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
1221+ ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
10971222 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
10981223 top--;
10991224 n--;
@@ -1101,13 +1226,25 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
11011226 }
11021227 break;
11031228
1229+ case STOREMAP: {
1230+ int n = *(pc++);
1231+ TObject *arr = top-(2*n)-1;
1232+ while (n--) {
1233+ *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
1234+ top-=2;
1235+ }
1236+ }
1237+ break;
1238+
11041239 case ADJUST0:
11051240 adjust_top(base);
11061241 break;
11071242
1108− case ADJUST:
1109− adjust_top(base + *(pc++));
1243+ case ADJUST: {
1244+ StkId newtop = base + *(pc++);
1245+ adjust_top(newtop);
11101246 break;
1247+ }
11111248
11121249 case VARARGS:
11131250 adjust_varargs(base + *(pc++));
@@ -1118,7 +1255,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
11181255 Word size;
11191256 get_word(size,pc);
11201257 avalue(top) = lua_createarray(size);
1121− tag(top) = LUA_T_ARRAY;
1258+ ttype(top) = LUA_T_ARRAY;
11221259 incr_top;
11231260 }
11241261 break;
@@ -1127,33 +1264,33 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
11271264 {
11281265 int res = lua_equalObj(top-2, top-1);
11291266 --top;
1130− tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
1267+ ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
11311268 nvalue(top-1) = 1;
11321269 }
11331270 break;
11341271
11351272 case LTOP:
1136− comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
1273+ comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
11371274 break;
11381275
11391276 case LEOP:
1140− comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
1277+ comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
11411278 break;
11421279
11431280 case GTOP:
1144− comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
1281+ comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
11451282 break;
11461283
11471284 case GEOP:
1148− comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
1285+ comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
11491286 break;
11501287
11511288 case ADDOP:
11521289 {
1153− Object *l = top-2;
1154− Object *r = top-1;
1290+ TObject *l = top-2;
1291+ TObject *r = top-1;
11551292 if (tonumber(r) || tonumber(l))
1156− call_arith("add");
1293+ call_arith(IM_ADD);
11571294 else
11581295 {
11591296 nvalue(l) += nvalue(r);
@@ -1164,10 +1301,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
11641301
11651302 case SUBOP:
11661303 {
1167− Object *l = top-2;
1168− Object *r = top-1;
1304+ TObject *l = top-2;
1305+ TObject *r = top-1;
11691306 if (tonumber(r) || tonumber(l))
1170− call_arith("sub");
1307+ call_arith(IM_SUB);
11711308 else
11721309 {
11731310 nvalue(l) -= nvalue(r);
@@ -1178,10 +1315,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
11781315
11791316 case MULTOP:
11801317 {
1181− Object *l = top-2;
1182− Object *r = top-1;
1318+ TObject *l = top-2;
1319+ TObject *r = top-1;
11831320 if (tonumber(r) || tonumber(l))
1184− call_arith("mul");
1321+ call_arith(IM_MUL);
11851322 else
11861323 {
11871324 nvalue(l) *= nvalue(r);
@@ -1192,10 +1329,10 @@ static StkId lua_execute (Byte *pc, StkId base) · 4 unmodified lines
11921329
11931330 case DIVOP:
11941331 {
1195− Object *l = top-2;
1196− Object *r = top-1;
1332+ TObject *l = top-2;
1333+ TObject *r = top-1;
11971334 if (tonumber(r) || tonumber(l))
1198− call_arith("div");
1335+ call_arith(IM_DIV);
11991336 else
12001337 {
12011338 nvalue(l) /= nvalue(r);
@@ -1205,36 +1342,34 @@ static StkId lua_execute (Byte *pc, StkId base) · 3 unmodified lines
12051342 break;
12061343
12071344 case POWOP:
1208− call_arith("pow");
1345+ call_arith(IM_POW);
12091346 break;
12101347
1211− case CONCOP:
1212− {
1213− Object *l = top-2;
1214− Object *r = top-1;
1215− if (tostring(r) || tostring(l))
1216− callFB(FB_CONCAT);
1217− else
1218− {
1219− tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
1220− --top;
1221− }
1348+ case CONCOP: {
1349+ TObject *l = top-2;
1350+ TObject *r = top-1;
1351+ if (tostring(l) || tostring(r))
1352+ call_binTM(IM_CONCAT, "unexpected type for concatenation");
1353+ else {
1354+ tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
1355+ --top;
1356+ }
12221357 }
12231358 break;
12241359
12251360 case MINUSOP:
12261361 if (tonumber(top-1))
12271362 {
1228− tag(top) = LUA_T_NIL;
1363+ ttype(top) = LUA_T_NIL;
12291364 incr_top;
1230− call_arith("unm");
1365+ call_arith(IM_UNM);
12311366 }
12321367 else
12331368 nvalue(top-1) = - nvalue(top-1);
12341369 break;
12351370
12361371 case NOTOP:
1237− tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
1372+ ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
12381373 nvalue(top-1) = 1;
12391374 break;
12401375
@@ -1242,15 +1377,15 @@ static StkId lua_execute (Byte *pc, StkId base) · 1 unmodified line
12421377 {
12431378 Word w;
12441379 get_word(w,pc);
1245− if (tag(top-1) != LUA_T_NIL) pc += w;
1380+ if (ttype(top-1) != LUA_T_NIL) pc += w;
12461381 }
12471382 break;
12481383
1249− case ONFJMP:
1384+ case ONFJMP:
12501385 {
12511386 Word w;
12521387 get_word(w,pc);
1253− if (tag(top-1) == LUA_T_NIL) pc += w;
1388+ if (ttype(top-1) == LUA_T_NIL) pc += w;
12541389 }
12551390 break;
12561391
@@ -1275,7 +1410,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 18 unmodified lines
12751410 Word w;
12761411 get_word(w,pc);
12771412 top--;
1278− if (tag(top) == LUA_T_NIL) pc += w;
1413+ if (ttype(top) == LUA_T_NIL) pc += w;
12791414 }
12801415 break;
12811416
@@ -1284,7 +1419,7 @@ static StkId lua_execute (Byte *pc, StkId base) · 2 unmodified lines
12841419 Word w;
12851420 get_word(w,pc);
12861421 top--;
1287− if (tag(top) == LUA_T_NIL) pc -= w;
1422+ if (ttype(top) == LUA_T_NIL) pc -= w;
12881423 }
12891424 break;
12901425
@@ -1309,12 +1444,12 @@ static StkId lua_execute (Byte *pc, StkId base) · 18 unmodified lines
13091444 {
13101445 Word line;
13111446 get_word(line,pc);
1312− if ((stack+base-1)->tag != LUA_T_LINE)
1447+ if ((stack+base-1)->ttype != LUA_T_LINE)
13131448 {
13141449 /* open space for LINE value */
13151450 open_stack((top-stack)-base);
13161451 base++;
1317− (stack+base-1)->tag = LUA_T_LINE;
1452+ (stack+base-1)->ttype = LUA_T_LINE;
13181453 }
13191454 (stack+base-1)->value.i = line;
13201455 if (lua_linehook)
@@ -1328,3 +1463,16 @@ static StkId lua_execute (Byte *pc, StkId base) · 7 unmodified lines
13281463 }
13291464 }
13301465
1466+
1467+#if COMPAT2_5
1468+/*
1469+** API: set a function as a fallback
1470+*/
1471+lua_Object lua_setfallback (char *name, lua_CFunction fallback)
1472+{
1473+ lua_pushstring(name);
1474+ lua_pushcfunction(fallback);
1475+ do_unprotectedrun(luaI_setfallback, 2, 1);
1476+ return put_luaObjectonTop();
1477+}
1478+#endif
src/opcode.h+46−25
@@ -1,6 +1,6 @@
11 /*
22 ** TeCGraf - PUC-Rio
3−** $Id: opcode.h,v 3.24 1996/11/01 12:46:59 roberto Exp $
3+** $Id: opcode.h,v 3.34 1997/06/16 16:50:22 roberto Exp $
44 */
55
66 #ifndef opcode_h
@@ -14,6 +14,28 @@ · 7 unmodified lines
1414
1515 #define FIELDS_PER_FLUSH 40
1616
17+/*
18+* WARNING: if you change the order of this enumeration,
19+* grep "ORDER LUA_T"
20+*/
21+typedef enum
22+{
23+ LUA_T_NIL = -9,
24+ LUA_T_NUMBER = -8,
25+ LUA_T_STRING = -7,
26+ LUA_T_ARRAY = -6, /* array==table */
27+ LUA_T_FUNCTION = -5,
28+ LUA_T_CFUNCTION= -4,
29+ LUA_T_MARK = -3,
30+ LUA_T_CMARK = -2,
31+ LUA_T_LINE = -1,
32+ LUA_T_USERDATA = 0
33+} lua_Type;
34+
35+#define NUM_TYPES 10
36+
37+
38+extern char *luaI_typenames[];
1739
1840 typedef enum {
1941 /* name parm before after side effect
@@ -38,7 +60,7 @@ PUSHLOCAL6,/* - LOC[6] */ · 18 unmodified lines
3860 PUSHLOCAL7,/* - LOC[7] */
3961 PUSHLOCAL8,/* - LOC[8] */
4062 PUSHLOCAL9,/* - LOC[9] */
41−PUSHLOCAL,/* w - LOC[w] */
63+PUSHLOCAL,/* b - LOC[b] */
4264 PUSHGLOBAL,/* w - VAR[w] */
4365 PUSHINDEXED,/* i t t[i] */
4466 PUSHSELF,/* w t t t[STR[w]] */
@@ -52,14 +74,14 @@ STORELOCAL6,/* x - LOC[6]=x */ · 7 unmodified lines
5274 STORELOCAL7,/* x - LOC[7]=x */
5375 STORELOCAL8,/* x - LOC[8]=x */
5476 STORELOCAL9,/* x - LOC[9]=x */
55−STORELOCAL,/* w x - LOC[w]=x */
77+STORELOCAL,/* b x - LOC[b]=x */
5678 STOREGLOBAL,/* w x - VAR[w]=x */
5779 STOREINDEXED0,/* v i t - t[i]=v */
5880 STOREINDEXED,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
59−STORELIST0,/* w v_w...v_1 t - t[i]=v_i */
60−STORELIST,/* w n v_w...v_1 t - t[i+n*FPF]=v_i */
61−STORERECORD,/* n
62− w_n...w_1 v_n...v_1 t - t[STR[w_i]]=v_i */
81+STORELIST0,/* b v_b...v_1 t - t[i]=v_i */
82+STORELIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
83+STORERECORD,/* b
84+ w_b...w_1 v_b...v_1 t - t[STR[w_i]]=v_i */
6385 ADJUST0,/* - - TOP=BASE */
6486 ADJUST,/* b - - TOP=BASE+b */
6587 CREATEARRAY,/* w - newarray(size = w) */
@@ -76,19 +98,19 @@ POWOP,/* y x x^y */ · 10 unmodified lines
7698 CONCOP,/* y x x..y */
7799 MINUSOP,/* x -x */
78100 NOTOP,/* x (x==nil)? 1 : nil */
79−ONTJMP,/* w x - (x!=nil)? PC+=w */
80−ONFJMP,/* w x - (x==nil)? PC+=w */
101+ONTJMP,/* w x - (x!=nil)? PC+=w */
102+ONFJMP,/* w x - (x==nil)? PC+=w */
81103 JMP,/* w - - PC+=w */
82−UPJMP,/* w - - PC-=w */
83−IFFJMP,/* w x - (x==nil)? PC+=w */
104+UPJMP,/* w - - PC-=w */
105+IFFJMP,/* w x - (x==nil)? PC+=w */
84106 IFFUPJMP,/* w x - (x==nil)? PC-=w */
85107 POP,/* x - */
86−CALLFUNC,/* n m v_n...v_1 f r_m...r_1 f(v1,...,v_n) */
108+CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */
87109 RETCODE0,
88110 RETCODE,/* b - - */
89111 SETLINE,/* w - - LINE=w */
90−VARARGS/* b v_n...v_1 {v_1...v_n;n=n} */
91−
112+VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */
113+STOREMAP/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */
92114 } OpCode;
93115
94116
@@ -102,29 +124,27 @@ typedef union · 7 unmodified lines
102124 TaggedString *ts;
103125 TFunc *tf;
104126 struct Hash *a;
105− void *u;
106127 int i;
107128 } Value;
108129
109−typedef struct Object
130+typedef struct TObject
110131 {
111− lua_Type tag;
132+ lua_Type ttype;
112133 Value value;
113−} Object;
134+} TObject;
114135
115136
116137 /* Macros to access structure members */
117−#define tag(o) ((o)->tag)
138+#define ttype(o) ((o)->ttype)
118139 #define nvalue(o) ((o)->value.n)
119140 #define svalue(o) ((o)->value.ts->str)
120141 #define tsvalue(o) ((o)->value.ts)
121142 #define avalue(o) ((o)->value.a)
122143 #define fvalue(o) ((o)->value.f)
123−#define uvalue(o) ((o)->value.u)
124144
125145 /* Macros to access symbol table */
126146 #define s_object(i) (lua_table[i].object)
127−#define s_tag(i) (tag(&s_object(i)))
147+#define s_ttype(i) (ttype(&s_object(i)))
128148 #define s_nvalue(i) (nvalue(&s_object(i)))
129149 #define s_svalue(i) (svalue(&s_object(i)))
130150 #define s_tsvalue(i) (tsvalue(&s_object(i)))
@@ -141,10 +161,11 @@ typedef struct Object · 10 unmodified lines
141161 /* Exported functions */
142162 void lua_parse (TFunc *tf); /* from "lua.stx" module */
143163 void luaI_codedebugline (int line); /* from "lua.stx" module */
144−void lua_travstack (int (*fn)(Object *));
145−Object *luaI_Address (lua_Object o);
146−void luaI_pushobject (Object *o);
147−void luaI_gcFB (Object *o);
164+void lua_travstack (int (*fn)(TObject *));
165+TObject *luaI_Address (lua_Object o);
166+void luaI_pushobject (TObject *o);
167+void luaI_gcIM (TObject *o);
148168 int luaI_dorun (TFunc *tf);
169+int lua_domain (void);
149170
150171 #endif
src/parser.c+344−316
@@ -10,14 +10,14 @@ static char luaY_sccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93"; · 9 unmodified lines
1010 #define YYPREFIX "luaY_"
1111 #line 2 "lua.stx"
1212
13−char *rcs_luastx = "$Id: parser.c,v 1.1 1996/11/21 16:11:40 lhf Exp $";
13+char *rcs_luastx = "$Id: parser.c,v 1.1 1997/06/30 18:59:03 lhf Exp $";
1414
1515 #include <stdio.h>
1616 #include <stdlib.h>
1717 #include <string.h>
1818
1919 #include "luadebug.h"
20−#include "mem.h"
20+#include "luamem.h"
2121 #include "lex.h"
2222 #include "opcode.h"
2323 #include "hash.h"
@@ -60,8 +60,6 @@ static TaggedString *localvar[MAXLOCALS]; /* store local variable names */ · 36 unmodified lines
6060 static int nlocalvar=0; /* number of local variables */
6161
6262 #define MAXFIELDS FIELDS_PER_FLUSH*2
63−static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
64−static int nfields=0;
6563
6664 int lua_debug = 0;
6765
@@ -113,22 +111,11 @@ static void code_word_at (Byte *p, int n) · 45 unmodified lines
113111 memcpy(p, &w, sizeof(Word));
114112 }
115113
116−static void push_field (Word name)
117−{
118− if (nfields < MAXFIELDS)
119− fields[nfields++] = name;
120− else
121− luaY_error ("too many fields in nested constructors");
122−}
123−
124114 static void flush_record (int n)
125115 {
126− int i;
127116 if (n == 0) return;
128− code_byte(STORERECORD);
117+ code_byte(STOREMAP);
129118 code_byte(n);
130− for (i=0; i<n; i++)
131− code_word(fields[--nfields]);
132119 }
133120
134121 static void flush_list (int m, int n)
@@ -171,11 +158,22 @@ static void add_varbuffer (Long var) · 36 unmodified lines
171158 luaY_error ("variable buffer overflow");
172159 }
173160
161+static void code_string (Word w)
162+{
163+ code_byte(PUSHSTRING);
164+ code_word(w);
165+}
166+
167+static void code_constant (TaggedString *s)
168+{
169+ code_string(luaI_findconstant(s));
170+}
171+
174172 static void code_number (float f)
175173 {
176− Word i = (Word)f;
177− if (f == (float)i) /* f has an (short) integer value */
178− {
174+ Word i;
175+ if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
176+ /* f has an (short) integer value */
179177 if (i <= 2) code_byte(PUSH0 + i);
180178 else if (i <= 255)
181179 {
@@ -418,7 +416,7 @@ void lua_parse (TFunc *tf) · 236 unmodified lines
418416 }
419417
420418
421−#line 414 "lua.stx"
419+#line 412 "lua.stx"
422420 typedef union
423421 {
424422 int vInt;
@@ -429,7 +427,7 @@ typedef union · 4 unmodified lines
429427 TFunc *pFunc;
430428 TaggedString *pTStr;
431429 } YYSTYPE;
432−#line 433 "y.tab.c"
430+#line 431 "y.tab.c"
433431 #define WRONGTOKEN 257
434432 #define NIL 258
435433 #define IF 259
@@ -467,8 +465,9 @@ short luaY_lhs[] = { -1, · 31 unmodified lines
467465 38, 5, 39, 5, 40, 37, 4, 8, 8, 7,
468466 7, 2, 2, 3, 41, 3, 17, 17, 18, 18,
469467 19, 19, 42, 9, 9, 14, 14, 43, 43, 12,
470− 12, 13, 13, 44, 15, 15, 16, 16, 6, 6,
471− 20, 20, 20, 21, 29, 10, 10, 11, 11,
468+ 12, 13, 13, 44, 45, 45, 15, 15, 16, 16,
469+ 6, 6, 20, 20, 20, 21, 29, 10, 10, 11,
470+ 11,
472471 };
473472 short luaY_len[] = { 2,
474473 2, 0, 3, 2, 3, 1, 3, 5, 0, 3,
@@ -479,113 +478,114 @@ short luaY_len[] = { 2, · 4 unmodified lines
479478 0, 5, 0, 5, 0, 4, 2, 1, 3, 3,
480479 1, 0, 1, 1, 0, 4, 0, 1, 1, 3,
481480 1, 1, 0, 3, 2, 0, 2, 0, 1, 0,
482− 2, 1, 3, 3, 0, 2, 1, 3, 1, 3,
483− 1, 4, 3, 1, 1, 1, 3, 0, 2,
481+ 2, 1, 3, 3, 3, 1, 0, 2, 1, 3,
482+ 1, 3, 1, 4, 3, 1, 1, 1, 3, 0,
483+ 2,
484484 };
485485 short luaY_defred[] = { 2,
486− 0, 0, 0, 14, 16, 0, 0, 0, 94, 19,
487− 0, 0, 0, 91, 1, 0, 4, 0, 48, 46,
488− 47, 0, 0, 0, 49, 29, 95, 0, 0, 44,
489− 0, 0, 24, 0, 0, 0, 0, 96, 0, 0,
486+ 0, 0, 0, 14, 16, 0, 0, 0, 96, 19,
487+ 0, 0, 0, 93, 1, 0, 4, 0, 48, 46,
488+ 47, 0, 0, 0, 49, 29, 97, 0, 0, 44,
489+ 0, 0, 24, 0, 0, 0, 0, 98, 0, 0,
490490 0, 0, 0, 0, 0, 57, 61, 12, 3, 0,
491491 0, 0, 0, 0, 0, 28, 28, 28, 0, 0,
492492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493493 0, 0, 0, 9, 27, 65, 0, 0, 20, 0,
494− 5, 0, 0, 0, 0, 0, 59, 0, 93, 30,
494+ 5, 0, 0, 0, 0, 0, 59, 0, 95, 30,
495495 24, 51, 53, 0, 0, 0, 0, 0, 0, 0,
496− 0, 0, 0, 0, 0, 0, 0, 0, 73, 0,
497− 0, 82, 28, 0, 0, 0, 0, 97, 72, 71,
498− 0, 0, 69, 7, 60, 92, 28, 0, 0, 0,
499− 56, 0, 75, 0, 0, 86, 24, 0, 25, 0,
500− 0, 24, 0, 0, 0, 0, 0, 0, 83, 0,
501− 74, 0, 28, 17, 10, 0, 70, 24, 0, 0,
502− 77, 0, 0, 8, 22, 0, 13, 81, 15, 28,
503− 24, 28, 0, 23,
496+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 73,
497+ 0, 0, 82, 0, 28, 0, 0, 0, 0, 99,
498+ 72, 71, 0, 0, 69, 7, 60, 94, 28, 0,
499+ 0, 0, 56, 0, 75, 0, 0, 88, 0, 24,
500+ 0, 25, 0, 0, 24, 0, 0, 0, 0, 85,
501+ 86, 83, 0, 74, 0, 0, 28, 17, 10, 0,
502+ 70, 24, 0, 0, 77, 0, 0, 8, 22, 0,
503+ 13, 81, 15, 28, 24, 28, 0, 23,
504504 };
505505 short luaY_dgoto[] = { 1,
506− 91, 34, 35, 25, 26, 11, 46, 12, 107, 39,
507− 79, 161, 108, 151, 109, 110, 121, 122, 123, 27,
506+ 91, 34, 35, 25, 26, 11, 46, 12, 108, 39,
507+ 79, 165, 109, 154, 110, 111, 123, 124, 125, 27,
508508 14, 41, 81, 2, 15, 16, 49, 17, 28, 73,
509− 115, 37, 160, 32, 33, 74, 30, 128, 129, 31,
510− 116, 134, 133, 112,
509+ 117, 37, 164, 32, 33, 74, 30, 130, 131, 31,
510+ 118, 136, 135, 113, 114,
511511 };
512512 short luaY_sindex[] = { 0,
513− 0, 332, -38, 0, 0, -38, -239, -223, 0, 0,
514− -23, 17, 0, 0, 0, 3, 0, 137, 0, 0,
515− 0, -38, -38, -38, 0, 0, 0, 137, 547, 0,
516− -25, -38, 0, 3, 45, 0, 212, 0, -22, 0,
517− 52, 140, -38, -223, -38, 0, 0, 0, 0, -166,
518− -38, -154, 40, 40, 102, 0, 0, 0, -38, -38,
519− -38, -38, -38, -38, -38, -38, -38, -38, -38, -38,
520− -34, 481, -130, 0, 0, 0, -38, -129, 0, -194,
521− 0, -123, 45, 0, -17, 103, 0, 753, 0, 0,
522− 0, 0, 0, -30, -30, -30, -30, -30, -30, 95,
523− 11, 11, 40, 40, 40, 97, 41, 123, 0, 124,
524− 212, 0, 0, -38, -59, -38, 45, 0, 0, 0,
525− 128, 131, 0, 0, 0, 0, 0, -38, -38, -38,
526− 0, -97, 0, 120, -38, 0, 0, 212, 0, 3,
527− 0, 0, -194, -183, 118, 118, 212, 97, 0, -97,
528− 0, 212, 0, 0, 0, -85, 0, 0, -38, -82,
529− 0, 123, -80, 0, 0, 1105, 0, 0, 0, 0,
530− 0, 0, -183, 0,
513+ 0, 288, -34, 0, 0, -34, -238, -223, 0, 0,
514+ -6, 22, 0, 0, 0, 14, 0, 150, 0, 0,
515+ 0, -34, -34, -34, 0, 0, 0, 150, 547, 0,
516+ -46, -34, 0, 14, 34, 0, 212, 0, -5, 0,
517+ 39, 156, -34, -223, -34, 0, 0, 0, 0, -194,
518+ -34, -193, -4, -4, 48, 0, 0, 0, -34, -34,
519+ -34, -34, -34, -34, -34, -34, -34, -34, -34, -34,
520+ -38, 481, -173, 0, 0, 0, -34, -146, 0, -234,
521+ 0, -138, 34, 0, -15, 96, 0, 753, 0, 0,
522+ 0, 0, 0, -30, -30, -30, -30, -30, -30, -20,
523+ -12, -12, -4, -4, -4, 0, -34, 16, 95, 0,
524+ 98, 212, 0, 82, 0, -34, 328, -34, 34, 0,
525+ 0, 0, 103, 101, 0, 0, 0, 0, 0, -34,
526+ -34, 1181, 0, -91, 0, 92, -34, 0, -34, 0,
527+ 212, 0, 14, 0, 0, -234, -233, 118, 118, 0,
528+ 0, 0, -91, 0, 212, 212, 0, 0, 0, -105,
529+ 0, 0, -34, -103, 0, 95, -101, 0, 0, 1105,
530+ 0, 0, 0, 0, 0, 0, -233, 0,
531531 };
532532 short luaY_rindex[] = { 0,
533− 0, 191, 69, 0, 0, 173, 0, 0, 0, 0,
534− 0, 69, 150, 0, 0, 146, 0, -36, 0, 0,
535− 0, 69, 69, 69, 0, 0, 0, 1, 0, 0,
536− 0, 69, 0, 47, 461, 436, 0, 0, 197, 130,
537− 0, 0, 69, 0, -33, 0, 0, 0, 0, 0,
538− 69, 0, 24, 59, 1181, 0, 0, 0, 69, 69,
539− 69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
540− -50, 0, 0, 0, 0, 0, 69, 0, 0, 152,
541− 0, 0, 311, -21, 0, 0, 0, 0, 0, 0,
533+ 0, 169, 52, 0, 0, 173, 0, 0, 0, 0,
534+ 0, 52, 508, 0, 0, 146, 0, -36, 0, 0,
535+ 0, 52, 52, 52, 0, 0, 0, 1, 0, 0,
536+ 0, 52, 0, 47, 461, 436, 0, 0, 197, 76,
537+ 0, 0, 52, 0, -32, 0, 0, 0, 0, 0,
538+ 52, 0, 24, 59, 1195, 0, 0, 0, 52, 52,
539+ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
540+ -27, 0, 0, 0, 0, 0, 52, 0, 0, 129,
541+ 0, 0, 311, 142, 0, 0, 0, 0, 0, 0,
542542 0, 0, 0, 746, 776, 799, 821, 844, 866, 505,
543− 383, 410, 88, 112, 359, 1174, 0, 74, 0, -40,
544− -26, 0, 0, 69, -230, 69, 921, 0, 0, 0,
545− 0, 160, 0, 0, 0, 0, 0, 69, 69, 69,
546− 0, 77, 0, 78, -9, 0, 0, 939, 0, 289,
547− 474, 0, 0, -60, 894, 1137, -16, 0, 0, 89,
548− 0, -10, 0, 0, 0, 0, 0, 0, 69, 0,
549− 0, 74, 0, 0, 0, 0, 0, 0, 0, 0,
550− 0, 0, -60, 0,
543+ 383, 410, 88, 112, 359, 1174, 52, 0, 51, 0,
544+ -40, -10, 0, 0, 0, 52, -153, 52, 921, 0,
545+ 0, 0, 0, 144, 0, 0, 0, 0, 0, 52,
546+ 52, 0, 0, 62, 0, 64, -26, 0, 52, 0,
547+ 939, 0, 682, 474, 0, 0, -77, 894, 1137, 0,
548+ 0, 0, 66, 0, 13, -18, 0, 0, 0, 0,
549+ 0, 0, 52, 0, 0, 51, 0, 0, 0, 0,
550+ 0, 0, 0, 0, 0, 0, -77, 0,
551551 };
552552 short luaY_gindex[] = { 0,
553− -31, 171, 85, -2, 48, 0, 0, 0, 0, 0,
554− 0, 0, 67, 0, 0, 0, 0, 0, 75, 8,
555− 0, 0, 0, 0, 104, 107, -29, 0, 12, -61,
556− 0, 1260, 50, 0, 0, 0, 213, 0, 0, 0,
557− 0, 0, -107, 94,
553+ 53, 147, -23, 3, 134, 0, 0, 0, 0, 0,
554+ 0, 0, 40, 0, 0, 0, 0, 0, 49, 6,
555+ 0, 0, 0, 0, 81, 83, -16, 0, 8, -70,
556+ 0, 1445, 27, 0, 0, 0, 187, 0, 0, 0,
557+ 0, 0, -108, 67, 0,
558558 };
559−#define YYTABLESIZE 1462
560−short luaY_table[] = { 10,
561− 45, 24, 136, 58, 75, 24, 22, 62, 85, 13,
562− 22, 68, 66, 18, 67, 40, 69, 87, 78, 42,
563− 44, 78, 90, 43, 95, 92, 93, 84, 52, 127,
564− 26, 26, 87, 88, 38, 26, 26, 43, 77, 90,
565− 58, 45, 45, 45, 45, 45, 11, 45, 88, 79,
566− 9, 84, 68, 36, 168, 85, 45, 69, 50, 45,
567− 45, 48, 45, 70, 43, 43, 43, 43, 43, 95,
568− 43, 55, 55, 51, 85, 153, 119, 158, 159, 120,
569− 156, 137, 43, 43, 78, 43, 58, 39, 76, 55,
570− 36, 80, 36, 45, 45, 144, 165, 71, 87, 50,
571− 50, 50, 50, 50, 70, 50, 154, 87, 84, 172,
572− 155, 40, 10, 55, 88, 79, 43, 50, 50, 89,
573− 50, 163, 13, 58, 36, 45, 18, 83, 39, 39,
574− 39, 39, 39, 70, 39, 114, 68, 66, 171, 67,
575− 173, 69, 90, 125, 118, 11, 39, 39, 43, 39,
576− 124, 50, 40, 40, 40, 40, 40, 130, 40, 68,
577− 66, 117, 67, 141, 69, 131, 132, 135, 142, 6,
578− 40, 40, 62, 40, 143, 95, 148, 62, 150, 61,
579− 39, 164, 52, 50, 167, 52, 169, 95, 70, 95,
580− 26, 55, 67, 89, 50, 95, 98, 82, 78, 3,
581− 68, 79, 76, 4, 40, 5, 21, 95, 6, 7,
582− 89, 70, 39, 80, 9, 86, 162, 157, 139, 19,
583− 95, 140, 174, 19, 47, 149, 0, 51, 0, 0,
584− 51, 62, 0, 20, 21, 9, 40, 20, 21, 106,
585− 95, 0, 0, 0, 23, 0, 0, 0, 23, 0,
586− 65, 0, 0, 68, 66, 98, 67, 0, 69, 45,
559+#define YYTABLESIZE 1608
560+short luaY_table[] = { 107,
561+ 45, 24, 138, 58, 10, 24, 22, 13, 62, 18,
562+ 22, 68, 66, 40, 67, 42, 69, 75, 78, 83,
563+ 129, 68, 66, 43, 67, 84, 69, 162, 163, 68,
564+ 52, 87, 79, 89, 69, 38, 121, 44, 78, 122,
565+ 58, 45, 45, 45, 45, 45, 11, 45, 89, 84,
566+ 9, 85, 107, 119, 43, 77, 90, 172, 50, 45,
567+ 45, 45, 45, 70, 43, 43, 43, 43, 43, 157,
568+ 43, 90, 48, 70, 160, 51, 71, 76, 80, 87,
569+ 89, 70, 43, 43, 78, 43, 58, 39, 90, 70,
570+ 55, 169, 116, 45, 45, 55, 55, 87, 79, 50,
571+ 50, 50, 50, 50, 176, 50, 84, 26, 26, 92,
572+ 93, 40, 26, 26, 89, 6, 43, 50, 50, 10,
573+ 50, 97, 13, 58, 18, 45, 159, 120, 39, 39,
574+ 39, 39, 39, 97, 39, 126, 127, 90, 134, 36,
575+ 133, 137, 139, 145, 146, 11, 39, 39, 43, 39,
576+ 153, 50, 40, 40, 40, 40, 40, 55, 40, 68,
577+ 66, 168, 67, 171, 69, 173, 97, 140, 26, 67,
578+ 40, 40, 62, 40, 55, 78, 36, 62, 36, 61,
579+ 39, 147, 151, 50, 68, 92, 79, 97, 76, 21,
580+ 80, 86, 166, 158, 161, 52, 100, 142, 47, 143,
581+ 152, 52, 92, 178, 40, 0, 0, 50, 0, 167,
582+ 36, 70, 39, 82, 0, 0, 0, 0, 0, 19,
583+ 0, 0, 0, 19, 0, 0, 175, 0, 177, 0,
584+ 0, 62, 97, 20, 21, 106, 40, 20, 21, 9,
585+ 51, 0, 0, 0, 23, 0, 51, 0, 23, 0,
586+ 65, 144, 0, 68, 66, 100, 67, 0, 69, 45,
587587 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
588− 45, 62, 95, 61, 45, 45, 45, 45, 45, 45,
588+ 45, 62, 0, 61, 45, 45, 45, 45, 45, 45,
589589 45, 45, 43, 43, 43, 43, 43, 43, 43, 43,
590590 43, 43, 43, 43, 0, 55, 0, 43, 43, 43,
591591 43, 43, 43, 43, 43, 70, 0, 11, 11, 0,
@@ -603,25 +603,25 @@ short luaY_table[] = { 10, · 11 unmodified lines
603603 41, 0, 0, 37, 0, 37, 37, 37, 0, 0,
604604 0, 0, 0, 62, 62, 64, 0, 0, 62, 62,
605605 0, 37, 37, 0, 37, 0, 0, 0, 0, 0,
606− 38, 41, 38, 38, 38, 98, 0, 98, 98, 98,
607− 63, 98, 98, 98, 98, 98, 98, 0, 38, 38,
608− 98, 38, 0, 66, 0, 37, 64, 29, 29, 64,
606+ 38, 41, 38, 38, 38, 100, 0, 100, 100, 100,
607+ 63, 100, 100, 100, 100, 100, 100, 0, 38, 38,
608+ 100, 38, 0, 66, 0, 37, 64, 29, 29, 64,
609609 29, 0, 29, 41, 0, 0, 57, 58, 59, 60,
610610 63, 64, 65, 0, 64, 29, 0, 29, 0, 0,
611611 0, 63, 38, 0, 42, 0, 0, 37, 0, 0,
612612 0, 0, 0, 0, 66, 29, 29, 66, 29, 63,
613613 29, 0, 68, 66, 0, 67, 0, 69, 0, 29,
614614 0, 0, 66, 29, 38, 29, 0, 0, 0, 0,
615− 62, 0, 61, 0, 0, 42, 0, 11, 42, 11,
616− 11, 11, 0, 11, 11, 11, 11, 11, 0, 0,
617− 0, 0, 11, 42, 42, 0, 42, 29, 0, 18,
615+ 62, 0, 61, 0, 0, 42, 3, 97, 42, 0,
616+ 4, 91, 5, 97, 0, 6, 7, 8, 0, 0,
617+ 0, 9, 0, 42, 42, 97, 42, 29, 91, 18,
618618 0, 18, 18, 18, 70, 18, 18, 18, 18, 18,
619− 18, 0, 0, 0, 18, 0, 0, 0, 68, 66,
620− 3, 67, 0, 69, 4, 0, 5, 42, 0, 6,
621− 7, 8, 0, 0, 0, 9, 62, 0, 61, 0,
619+ 18, 0, 0, 0, 18, 0, 3, 0, 68, 66,
620+ 4, 67, 5, 69, 0, 6, 7, 42, 97, 0,
621+ 0, 9, 0, 0, 0, 0, 62, 0, 61, 0,
622622 0, 0, 0, 0, 0, 0, 0, 41, 41, 41,
623623 41, 41, 41, 41, 41, 41, 41, 41, 41, 42,
624− 0, 0, 41, 41, 41, 41, 41, 41, 41, 41,
624+ 97, 0, 41, 41, 41, 41, 41, 41, 41, 41,
625625 70, 37, 37, 37, 37, 37, 37, 37, 37, 37,
626626 37, 37, 37, 0, 0, 0, 37, 37, 37, 37,
627627 37, 37, 37, 37, 0, 0, 0, 0, 38, 38,
@@ -632,7 +632,7 @@ short luaY_table[] = { 10, · 4 unmodified lines
632632 29, 29, 29, 29, 29, 29, 29, 0, 0, 0,
633633 0, 63, 63, 0, 0, 0, 63, 63, 0, 0,
634634 0, 0, 66, 0, 66, 66, 66, 0, 66, 66,
635− 66, 66, 66, 66, 113, 31, 0, 66, 29, 29,
635+ 66, 66, 66, 66, 115, 31, 0, 66, 29, 29,
636636 29, 29, 29, 29, 29, 57, 58, 59, 60, 63,
637637 64, 65, 0, 42, 42, 42, 42, 42, 42, 42,
638638 42, 42, 42, 42, 42, 34, 0, 0, 42, 42,
@@ -642,7 +642,7 @@ short luaY_table[] = { 10, · 3 unmodified lines
642642 0, 0, 62, 0, 61, 0, 34, 0, 0, 34,
643643 32, 57, 58, 59, 60, 63, 64, 65, 0, 0,
644644 0, 0, 0, 0, 34, 34, 0, 34, 31, 33,
645− 0, 0, 33, 35, 0, 126, 70, 0, 0, 0,
645+ 0, 0, 33, 35, 0, 128, 70, 0, 0, 0,
646646 0, 0, 0, 0, 0, 0, 0, 33, 33, 0,
647647 33, 32, 0, 0, 32, 36, 0, 0, 34, 0,
648648 31, 0, 0, 0, 0, 0, 0, 0, 0, 32,
@@ -650,12 +650,12 @@ short luaY_table[] = { 10, · 1 unmodified line
650650 0, 33, 0, 52, 0, 0, 0, 0, 0, 0,
651651 34, 0, 35, 35, 0, 35, 36, 0, 0, 36,
652652 0, 0, 0, 32, 0, 0, 0, 0, 0, 0,
653− 99, 0, 0, 33, 36, 36, 0, 36, 0, 0,
653+ 101, 0, 0, 33, 36, 36, 0, 36, 0, 0,
654654 0, 0, 0, 0, 52, 0, 35, 52, 28, 0,
655− 0, 0, 0, 0, 0, 32, 0, 0, 0, 0,
656− 0, 0, 52, 0, 0, 0, 0, 0, 36, 0,
655+ 11, 0, 11, 11, 11, 32, 11, 11, 11, 11,
656+ 11, 0, 52, 0, 0, 11, 0, 0, 36, 0,
657657 0, 0, 0, 0, 0, 0, 0, 0, 35, 0,
658− 0, 0, 0, 0, 0, 0, 0, 0, 0, 99,
658+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 101,
659659 0, 0, 0, 0, 0, 0, 52, 0, 0, 0,
660660 36, 0, 0, 0, 0, 0, 0, 28, 0, 0,
661661 0, 0, 0, 0, 31, 31, 31, 31, 31, 31,
@@ -675,66 +675,80 @@ short luaY_table[] = { 10, · 13 unmodified lines
675675 36, 36, 36, 36, 36, 36, 68, 66, 0, 67,
676676 0, 69, 52, 52, 52, 52, 52, 52, 52, 52,
677677 52, 52, 52, 52, 62, 0, 61, 52, 52, 52,
678− 0, 0, 0, 0, 0, 0, 0, 54, 0, 99,
679− 54, 99, 99, 99, 0, 99, 99, 99, 99, 99,
680− 99, 0, 0, 0, 99, 54, 0, 28, 70, 28,
678+ 0, 0, 0, 0, 0, 0, 0, 54, 0, 101,
679+ 54, 101, 101, 101, 0, 101, 101, 101, 101, 101,
680+ 101, 0, 0, 0, 101, 54, 0, 28, 70, 28,
681681 28, 28, 0, 28, 28, 28, 28, 28, 28, 0,
682− 0, 0, 28, 94, 0, 94, 94, 94, 94, 94,
683− 94, 0, 29, 29, 0, 29, 0, 29, 0, 54,
684− 0, 94, 94, 94, 0, 94, 0, 0, 0, 0,
685− 29, 0, 29, 0, 0, 0, 0, 0, 0, 0,
682+ 0, 0, 28, 96, 0, 96, 96, 96, 96, 96,
683+ 96, 0, 68, 66, 0, 67, 0, 69, 0, 54,
684+ 0, 96, 96, 96, 86, 96, 29, 29, 0, 29,
685+ 62, 29, 61, 0, 0, 0, 0, 0, 0, 0,
686+ 0, 0, 0, 0, 29, 0, 29, 0, 0, 0,
687+ 0, 54, 0, 0, 96, 0, 0, 96, 0, 0,
688+ 0, 0, 0, 150, 70, 0, 0, 0, 0, 0,
689+ 0, 0, 0, 0, 0, 0, 0, 0, 29, 0,
690+ 0, 0, 0, 0, 0, 0, 96, 0, 96, 0,
691+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
686692 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
687− 0, 54, 29, 0, 94, 0, 0, 94, 0, 0,
688− 0, 0, 0, 0, 29, 0, 0, 0, 0, 0,
689− 0, 53, 54, 0, 0, 0, 0, 0, 0, 0,
690− 0, 72, 0, 0, 0, 0, 94, 0, 94, 0,
691693 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
692− 88, 0, 0, 0, 0, 0, 0, 0, 94, 95,
693− 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
694− 111, 0, 0, 0, 0, 0, 0, 0, 0, 0,
695694 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
696695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
697− 0, 0, 0, 0, 170, 0, 0, 0, 0, 0,
698− 0, 0, 0, 138, 0, 0, 0, 0, 0, 57,
699− 58, 59, 60, 63, 64, 65, 0, 145, 146, 147,
700− 0, 0, 0, 0, 152, 54, 54, 54, 54, 54,
696+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
697+ 0, 0, 0, 0, 174, 0, 0, 0, 0, 0,
698+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 57,
699+ 58, 59, 60, 63, 64, 65, 0, 0, 0, 0,
700+ 0, 0, 0, 0, 0, 54, 54, 54, 54, 54,
701701 54, 54, 54, 54, 54, 54, 54, 0, 0, 0,
702− 54, 54, 54, 0, 0, 0, 0, 0, 166, 0,
702+ 54, 54, 54, 0, 0, 0, 0, 0, 0, 0,
703+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
704+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
705+ 0, 0, 0, 0, 0, 0, 0, 29, 96, 96,
706+ 96, 96, 96, 96, 96, 57, 58, 59, 60, 63,
707+ 64, 65, 0, 0, 0, 0, 53, 54, 0, 29,
708+ 29, 29, 29, 29, 29, 29, 72, 0, 0, 0,
703709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
710+ 0, 0, 0, 0, 0, 88, 0, 0, 0, 0,
711+ 0, 0, 0, 94, 95, 96, 97, 98, 99, 100,
712+ 101, 102, 103, 104, 105, 112, 0, 0, 0, 0,
704713 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
705− 0, 0, 0, 0, 0, 0, 0, 0, 94, 94,
706− 94, 94, 94, 94, 94, 29, 29, 29, 29, 29,
707− 29, 29,
714+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
715+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
716+ 0, 132, 0, 0, 0, 0, 0, 0, 0, 0,
717+ 141, 0, 0, 0, 0, 0, 0, 0, 0, 0,
718+ 0, 0, 0, 0, 148, 149, 0, 0, 0, 0,
719+ 0, 155, 0, 156, 0, 0, 0, 0, 0, 0,
720+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
721+ 0, 0, 0, 0, 0, 0, 0, 170,
708722 };
709−short luaY_check[] = { 2,
710− 0, 40, 110, 40, 34, 40, 45, 41, 59, 2,
711− 45, 42, 43, 2, 45, 8, 47, 44, 59, 8,
712− 44, 44, 44, 0, 46, 57, 58, 44, 46, 91,
713− 261, 262, 59, 44, 274, 266, 267, 61, 61, 61,
714− 40, 41, 42, 43, 44, 45, 0, 47, 59, 59,
715− 274, 44, 42, 6, 162, 44, 40, 47, 0, 59,
716− 60, 59, 62, 94, 41, 42, 43, 44, 45, 91,
717− 47, 24, 123, 91, 125, 137, 271, 261, 262, 274,
718− 142, 113, 59, 60, 125, 62, 123, 0, 44, 123,
719− 43, 40, 45, 93, 94, 127, 158, 123, 125, 41,
720− 42, 43, 44, 45, 94, 47, 138, 274, 125, 171,
721− 140, 0, 115, 123, 125, 125, 93, 59, 60, 274,
722− 62, 153, 115, 123, 77, 125, 115, 43, 41, 42,
723− 43, 44, 45, 94, 47, 266, 42, 43, 170, 45,
724− 172, 47, 41, 41, 274, 0, 59, 60, 125, 62,
725− 274, 93, 41, 42, 43, 44, 45, 61, 47, 42,
726− 43, 77, 45, 116, 47, 125, 44, 44, 41, 40,
727− 59, 60, 0, 62, 44, 46, 274, 60, 59, 62,
728− 93, 267, 46, 125, 267, 46, 267, 58, 94, 40,
729− 0, 123, 41, 44, 58, 46, 0, 58, 125, 259,
730− 41, 125, 125, 263, 93, 265, 267, 58, 268, 269,
731− 61, 94, 125, 125, 274, 45, 150, 143, 115, 258,
732− 91, 115, 173, 258, 12, 132, -1, 91, -1, -1,
733− 91, 59, -1, 272, 273, 274, 125, 272, 273, 274,
734− 91, -1, -1, -1, 283, -1, -1, -1, 283, -1,
735− 281, -1, -1, 42, 43, 59, 45, -1, 47, 259,
723+short luaY_check[] = { 91,
724+ 0, 40, 111, 40, 2, 40, 45, 2, 41, 2,
725+ 45, 42, 43, 8, 45, 8, 47, 34, 59, 43,
726+ 91, 42, 43, 0, 45, 44, 47, 261, 262, 42,
727+ 46, 59, 59, 44, 47, 274, 271, 44, 44, 274,
728+ 40, 41, 42, 43, 44, 45, 0, 47, 59, 44,
729+ 274, 44, 91, 77, 61, 61, 44, 166, 0, 59,
730+ 60, 40, 62, 94, 41, 42, 43, 44, 45, 140,
731+ 47, 59, 59, 94, 145, 91, 123, 44, 40, 274,
732+ 274, 94, 59, 60, 125, 62, 123, 0, 41, 94,
733+ 123, 162, 266, 93, 94, 123, 123, 125, 125, 41,
734+ 42, 43, 44, 45, 175, 47, 125, 261, 262, 57,
735+ 58, 0, 266, 267, 125, 40, 93, 59, 60, 117,
736+ 62, 46, 117, 123, 117, 125, 143, 274, 41, 42,
737+ 43, 44, 45, 58, 47, 274, 41, 125, 44, 6,
738+ 125, 44, 61, 41, 44, 0, 59, 60, 125, 62,
739+ 59, 93, 41, 42, 43, 44, 45, 24, 47, 42,
740+ 43, 267, 45, 267, 47, 267, 91, 115, 0, 41,
741+ 59, 60, 0, 62, 123, 125, 43, 60, 45, 62,
742+ 93, 129, 274, 125, 41, 44, 125, 46, 125, 267,
743+ 125, 45, 153, 141, 146, 46, 0, 117, 12, 117,
744+ 134, 46, 61, 177, 93, -1, -1, 58, -1, 157,
745+ 77, 94, 125, 58, -1, -1, -1, -1, -1, 258,
746+ -1, -1, -1, 258, -1, -1, 174, -1, 176, -1,
747+ -1, 59, 91, 272, 273, 274, 125, 272, 273, 274,
748+ 91, -1, -1, -1, 283, -1, 91, -1, 283, -1,
749+ 281, 118, -1, 42, 43, 59, 45, -1, 47, 259,
736750 260, 261, 262, 263, 264, 265, 266, 267, 268, 269,
737− 270, 60, 123, 62, 274, 275, 276, 277, 278, 279,
751+ 270, 60, -1, 62, 274, 275, 276, 277, 278, 279,
738752 280, 281, 259, 260, 261, 262, 263, 264, 265, 266,
739753 267, 268, 269, 270, -1, 123, -1, 274, 275, 276,
740754 277, 278, 279, 280, 281, 94, -1, 261, 262, -1,
@@ -761,16 +775,16 @@ short luaY_check[] = { 2, · 20 unmodified lines
761775 -1, -1, -1, -1, 41, 42, 43, 44, 45, 59,
762776 47, -1, 42, 43, -1, 45, -1, 47, -1, 94,
763777 -1, -1, 59, 60, 125, 62, -1, -1, -1, -1,
764− 60, -1, 62, -1, -1, 41, -1, 259, 44, 261,
765− 262, 263, -1, 265, 266, 267, 268, 269, -1, -1,
766− -1, -1, 274, 59, 60, -1, 62, 94, -1, 259,
778+ 60, -1, 62, -1, -1, 41, 259, 40, 44, -1,
779+ 263, 44, 265, 46, -1, 268, 269, 270, -1, -1,
780+ -1, 274, -1, 59, 60, 58, 62, 94, 61, 259,
767781 -1, 261, 262, 263, 94, 265, 266, 267, 268, 269,
768− 270, -1, -1, -1, 274, -1, -1, -1, 42, 43,
769− 259, 45, -1, 47, 263, -1, 265, 93, -1, 268,
770− 269, 270, -1, -1, -1, 274, 60, -1, 62, -1,
782+ 270, -1, -1, -1, 274, -1, 259, -1, 42, 43,
783+ 263, 45, 265, 47, -1, 268, 269, 93, 91, -1,
784+ -1, 274, -1, -1, -1, -1, 60, -1, 62, -1,
771785 -1, -1, -1, -1, -1, -1, -1, 259, 260, 261,
772786 262, 263, 264, 265, 266, 267, 268, 269, 270, 125,
773− -1, -1, 274, 275, 276, 277, 278, 279, 280, 281,
787+ 123, -1, 274, 275, 276, 277, 278, 279, 280, 281,
774788 94, 259, 260, 261, 262, 263, 264, 265, 266, 267,
775789 268, 269, 270, -1, -1, -1, 274, 275, 276, 277,
776790 278, 279, 280, 281, -1, -1, -1, -1, 259, 260,
@@ -801,8 +815,8 @@ short luaY_check[] = { 2, · 24 unmodified lines
801815 -1, -1, -1, 93, -1, -1, -1, -1, -1, -1,
802816 0, -1, -1, 125, 59, 60, -1, 62, -1, -1,
803817 -1, -1, -1, -1, 41, -1, 93, 44, 0, -1,
804− -1, -1, -1, -1, -1, 125, -1, -1, -1, -1,
805− -1, -1, 59, -1, -1, -1, -1, -1, 93, -1,
818+ 259, -1, 261, 262, 263, 125, 265, 266, 267, 268,
819+ 269, -1, 59, -1, -1, 274, -1, -1, 93, -1,
806820 -1, -1, -1, -1, -1, -1, -1, -1, 125, -1,
807821 -1, -1, -1, -1, -1, -1, -1, -1, -1, 59,
808822 -1, -1, -1, -1, -1, -1, 93, -1, -1, -1,
@@ -830,30 +844,44 @@ short luaY_check[] = { 2, · 21 unmodified lines
830844 262, 263, -1, 265, 266, 267, 268, 269, 270, -1,
831845 -1, -1, 274, 40, -1, 42, 43, 44, 45, 46,
832846 47, -1, 42, 43, -1, 45, -1, 47, -1, 93,
833− -1, 58, 59, 60, -1, 62, -1, -1, -1, -1,
834− 60, -1, 62, -1, -1, -1, -1, -1, -1, -1,
847+ -1, 58, 59, 60, 61, 62, 42, 43, -1, 45,
848+ 60, 47, 62, -1, -1, -1, -1, -1, -1, -1,
849+ -1, -1, -1, -1, 60, -1, 62, -1, -1, -1,
850+ -1, 125, -1, -1, 91, -1, -1, 94, -1, -1,
851+ -1, -1, -1, 93, 94, -1, -1, -1, -1, -1,
852+ -1, -1, -1, -1, -1, -1, -1, -1, 94, -1,
853+ -1, -1, -1, -1, -1, -1, 123, -1, 125, -1,
854+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
855+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
835856 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
836− -1, 125, 3, -1, 91, -1, -1, 94, -1, -1,
837− -1, -1, -1, -1, 94, -1, -1, -1, -1, -1,
838− -1, 22, 23, -1, -1, -1, -1, -1, -1, -1,
839− -1, 32, -1, -1, -1, -1, 123, -1, 125, -1,
840857 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
841− 51, -1, -1, -1, -1, -1, -1, -1, 59, 60,
842− 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
843− 71, -1, -1, -1, -1, -1, -1, -1, -1, -1,
844858 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
845859 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
846860 -1, -1, -1, -1, 260, -1, -1, -1, -1, -1,
847− -1, -1, -1, 114, -1, -1, -1, -1, -1, 275,
848− 276, 277, 278, 279, 280, 281, -1, 128, 129, 130,
849− -1, -1, -1, -1, 135, 259, 260, 261, 262, 263,
861+ -1, -1, -1, -1, -1, -1, -1, -1, -1, 275,
862+ 276, 277, 278, 279, 280, 281, -1, -1, -1, -1,
863+ -1, -1, -1, -1, -1, 259, 260, 261, 262, 263,
850864 264, 265, 266, 267, 268, 269, 270, -1, -1, -1,
851− 274, 275, 276, -1, -1, -1, -1, -1, 159, -1,
865+ 274, 275, 276, -1, -1, -1, -1, -1, -1, -1,
852866 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
853867 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
854− -1, -1, -1, -1, -1, -1, -1, -1, 275, 276,
868+ -1, -1, -1, -1, -1, -1, -1, 3, 275, 276,
855869 277, 278, 279, 280, 281, 275, 276, 277, 278, 279,
856− 280, 281,
870+ 280, 281, -1, -1, -1, -1, 22, 23, -1, 275,
871+ 276, 277, 278, 279, 280, 281, 32, -1, -1, -1,
872+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
873+ -1, -1, -1, -1, -1, 51, -1, -1, -1, -1,
874+ -1, -1, -1, 59, 60, 61, 62, 63, 64, 65,
875+ 66, 67, 68, 69, 70, 71, -1, -1, -1, -1,
876+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
877+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
878+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
879+ -1, 107, -1, -1, -1, -1, -1, -1, -1, -1,
880+ 116, -1, -1, -1, -1, -1, -1, -1, -1, -1,
881+ -1, -1, -1, -1, 130, 131, -1, -1, -1, -1,
882+ -1, 137, -1, 139, -1, -1, -1, -1, -1, -1,
883+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
884+ -1, -1, -1, -1, -1, -1, -1, 163,
857885 };
858886 #define YYFINAL 1
859887 #ifndef YYDEBUG
@@ -958,7 +986,9 @@ char *luaY_rule[] = { · 98 unmodified lines
958986 "ffieldlist : ffieldlist1 lastcomma",
959987 "ffieldlist1 : ffield",
960988 "ffieldlist1 : ffieldlist1 ',' ffield",
961−"ffield : NAME '=' expr1",
989+"ffield : ffieldkey '=' expr1",
990+"ffieldkey : '[' expr1 ']'",
991+"ffieldkey : NAME",
962992 "lfieldlist :",
963993 "lfieldlist : lfieldlist1 lastcomma",
964994 "lfieldlist1 : expr1",
@@ -1139,7 +1169,7 @@ luaY_reduce: · 174 unmodified lines
11391169 switch (luaY_n)
11401170 {
11411171 case 5:
1142−#line 470 "lua.stx"
1172+#line 468 "lua.stx"
11431173 {
11441174 code_byte(PUSHFUNCTION);
11451175 code_code(luaY_vsp[0].pFunc);
@@ -1147,28 +1177,26 @@ case 5: · 1 unmodified line
11471177 }
11481178 break;
11491179 case 6:
1150−#line 477 "lua.stx"
1180+#line 475 "lua.stx"
11511181 { luaY_val.vLong =luaY_vsp[0].vLong; init_func(); }
11521182 break;
11531183 case 7:
1154−#line 479 "lua.stx"
1184+#line 477 "lua.stx"
11551185 {
1156− code_byte(PUSHSTRING);
1157− code_word(luaI_findconstant(luaY_vsp[0].pTStr));
1186+ code_constant(luaY_vsp[0].pTStr);
11581187 luaY_val.vLong = 0; /* indexed variable */
11591188 init_func();
11601189 add_localvar(luaI_createfixedstring("self"));
11611190 }
11621191 break;
11631192 case 8:
1164−#line 489 "lua.stx"
1193+#line 486 "lua.stx"
11651194 {
11661195 codereturn();
11671196 luaY_val.pFunc = new(TFunc);
11681197 luaI_initTFunc(luaY_val.pFunc);
11691198 luaY_val.pFunc->size = pc;
11701199 luaY_val.pFunc->code = newvector(pc, Byte);
1171− luaY_val.pFunc->fileName = lua_parsedfile;
11721200 luaY_val.pFunc->lineDefined = luaY_vsp[-3].vInt;
11731201 memcpy(luaY_val.pFunc->code, basepc, pc*sizeof(Byte));
11741202 if (lua_debug)
@@ -1182,15 +1210,15 @@ case 8: · 7 unmodified lines
11821210 }
11831211 break;
11841212 case 13:
1185−#line 516 "lua.stx"
1213+#line 512 "lua.stx"
11861214 { codeIf(luaY_vsp[-4].vLong, luaY_vsp[-2].vLong); }
11871215 break;
11881216 case 14:
1189−#line 518 "lua.stx"
1217+#line 514 "lua.stx"
11901218 {luaY_val.vLong=pc;}
11911219 break;
11921220 case 15:
1193−#line 519 "lua.stx"
1221+#line 515 "lua.stx"
11941222 {
11951223 basepc[luaY_vsp[-3].vLong] = IFFJMP;
11961224 code_word_at(basepc+luaY_vsp[-3].vLong+1, pc - (luaY_vsp[-3].vLong + sizeof(Word)+1));
@@ -1199,18 +1227,18 @@ case 15: · 2 unmodified lines
11991227 }
12001228 break;
12011229 case 16:
1202−#line 526 "lua.stx"
1230+#line 522 "lua.stx"
12031231 {luaY_val.vLong=pc;}
12041232 break;
12051233 case 17:
1206−#line 527 "lua.stx"
1234+#line 523 "lua.stx"
12071235 {
12081236 basepc[luaY_vsp[0].vLong] = IFFUPJMP;
12091237 code_word_at(basepc+luaY_vsp[0].vLong+1, pc - (luaY_vsp[-4].vLong));
12101238 }
12111239 break;
12121240 case 18:
1213−#line 533 "lua.stx"
1241+#line 529 "lua.stx"
12141242 {
12151243 {
12161244 int i;
@@ -1222,22 +1250,26 @@ case 18: · 5 unmodified lines
12221250 }
12231251 }
12241252 break;
1253+case 19:
1254+#line 539 "lua.stx"
1255+{;}
1256+break;
12251257 case 20:
1226−#line 545 "lua.stx"
1258+#line 541 "lua.stx"
12271259 { nlocalvar += luaY_vsp[-1].vInt;
12281260 adjust_mult_assign(luaY_vsp[-1].vInt, luaY_vsp[0].vInt, 0);
12291261 }
12301262 break;
12311263 case 23:
1232−#line 553 "lua.stx"
1264+#line 549 "lua.stx"
12331265 { codeIf(luaY_vsp[-3].vLong, luaY_vsp[-1].vLong); }
12341266 break;
12351267 case 24:
1236−#line 556 "lua.stx"
1268+#line 552 "lua.stx"
12371269 {luaY_val.vInt = nlocalvar;}
12381270 break;
12391271 case 25:
1240−#line 557 "lua.stx"
1272+#line 553 "lua.stx"
12411273 {
12421274 if (nlocalvar != luaY_vsp[-2].vInt)
12431275 {
@@ -1251,14 +1283,14 @@ case 25: · 7 unmodified lines
12511283 }
12521284 break;
12531285 case 27:
1254−#line 572 "lua.stx"
1286+#line 568 "lua.stx"
12551287 {
12561288 adjust_functioncall(luaY_vsp[-1].vLong, MULT_RET);
12571289 codereturn();
12581290 }
12591291 break;
12601292 case 28:
1261−#line 579 "lua.stx"
1293+#line 575 "lua.stx"
12621294 {
12631295 luaY_val.vLong = pc;
12641296 code_byte(0); /* open space */
@@ -1266,103 +1298,102 @@ case 28: · 1 unmodified line
12661298 }
12671299 break;
12681300 case 29:
1269−#line 586 "lua.stx"
1301+#line 582 "lua.stx"
12701302 { adjust_functioncall(luaY_vsp[0].vLong, 1); }
12711303 break;
12721304 case 30:
1273−#line 589 "lua.stx"
1305+#line 585 "lua.stx"
12741306 { luaY_val.vLong = luaY_vsp[-1].vLong; }
12751307 break;
12761308 case 31:
1277−#line 590 "lua.stx"
1309+#line 586 "lua.stx"
12781310 { code_byte(EQOP); luaY_val.vLong = 0; }
12791311 break;
12801312 case 32:
1281−#line 591 "lua.stx"
1313+#line 587 "lua.stx"
12821314 { code_byte(LTOP); luaY_val.vLong = 0; }
12831315 break;
12841316 case 33:
1285−#line 592 "lua.stx"
1317+#line 588 "lua.stx"
12861318 { code_byte(GTOP); luaY_val.vLong = 0; }
12871319 break;
12881320 case 34:
1289−#line 593 "lua.stx"
1321+#line 589 "lua.stx"
12901322 { code_byte(EQOP); code_byte(NOTOP); luaY_val.vLong = 0; }
12911323 break;
12921324 case 35:
1293−#line 594 "lua.stx"
1325+#line 590 "lua.stx"
12941326 { code_byte(LEOP); luaY_val.vLong = 0; }
12951327 break;
12961328 case 36:
1297−#line 595 "lua.stx"
1329+#line 591 "lua.stx"
12981330 { code_byte(GEOP); luaY_val.vLong = 0; }
12991331 break;
13001332 case 37:
1301−#line 596 "lua.stx"
1333+#line 592 "lua.stx"
13021334 { code_byte(ADDOP); luaY_val.vLong = 0; }
13031335 break;
13041336 case 38:
1305−#line 597 "lua.stx"
1337+#line 593 "lua.stx"
13061338 { code_byte(SUBOP); luaY_val.vLong = 0; }
13071339 break;
13081340 case 39:
1309−#line 598 "lua.stx"
1341+#line 594 "lua.stx"
13101342 { code_byte(MULTOP); luaY_val.vLong = 0; }
13111343 break;
13121344 case 40:
1313−#line 599 "lua.stx"
1345+#line 595 "lua.stx"
13141346 { code_byte(DIVOP); luaY_val.vLong = 0; }
13151347 break;
13161348 case 41:
1317−#line 600 "lua.stx"
1349+#line 596 "lua.stx"
13181350 { code_byte(POWOP); luaY_val.vLong = 0; }
13191351 break;
13201352 case 42:
1321−#line 601 "lua.stx"
1353+#line 597 "lua.stx"
13221354 { code_byte(CONCOP); luaY_val.vLong = 0; }
13231355 break;
13241356 case 43:
1325−#line 602 "lua.stx"
1357+#line 598 "lua.stx"
13261358 { code_byte(MINUSOP); luaY_val.vLong = 0;}
13271359 break;
13281360 case 44:
1329−#line 603 "lua.stx"
1361+#line 599 "lua.stx"
13301362 { luaY_val.vLong = 0; }
13311363 break;
13321364 case 45:
1333−#line 604 "lua.stx"
1365+#line 600 "lua.stx"
13341366 { luaY_val.vLong = 0;}
13351367 break;
13361368 case 46:
1337−#line 605 "lua.stx"
1369+#line 601 "lua.stx"
13381370 { code_number(luaY_vsp[0].vFloat); luaY_val.vLong = 0; }
13391371 break;
13401372 case 47:
1341−#line 607 "lua.stx"
1373+#line 603 "lua.stx"
13421374 {
1343− code_byte(PUSHSTRING);
1344− code_word(luaY_vsp[0].vWord);
1345− luaY_val.vLong = 0;
1375+ code_string(luaY_vsp[0].vWord);
1376+ luaY_val.vLong = 0;
13461377 }
13471378 break;
13481379 case 48:
1349−#line 612 "lua.stx"
1380+#line 607 "lua.stx"
13501381 {code_byte(PUSHNIL); luaY_val.vLong = 0; }
13511382 break;
13521383 case 49:
1353−#line 613 "lua.stx"
1384+#line 608 "lua.stx"
13541385 { luaY_val.vLong = luaY_vsp[0].vLong; }
13551386 break;
13561387 case 50:
1357−#line 614 "lua.stx"
1388+#line 609 "lua.stx"
13581389 { code_byte(NOTOP); luaY_val.vLong = 0;}
13591390 break;
13601391 case 51:
1361−#line 615 "lua.stx"
1392+#line 610 "lua.stx"
13621393 {code_byte(POP); }
13631394 break;
13641395 case 52:
1365−#line 616 "lua.stx"
1396+#line 611 "lua.stx"
13661397 {
13671398 basepc[luaY_vsp[-2].vLong] = ONFJMP;
13681399 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));
@@ -1370,11 +1401,11 @@ case 52: · 1 unmodified line
13701401 }
13711402 break;
13721403 case 53:
1373−#line 621 "lua.stx"
1404+#line 616 "lua.stx"
13741405 {code_byte(POP); }
13751406 break;
13761407 case 54:
1377−#line 622 "lua.stx"
1408+#line 617 "lua.stx"
13781409 {
13791410 basepc[luaY_vsp[-2].vLong] = ONTJMP;
13801411 code_word_at(basepc+luaY_vsp[-2].vLong+1, pc - (luaY_vsp[-2].vLong + sizeof(Word)+1));
@@ -1382,20 +1413,20 @@ case 54: · 1 unmodified line
13821413 }
13831414 break;
13841415 case 55:
1385−#line 630 "lua.stx"
1416+#line 625 "lua.stx"
13861417 {
13871418 code_byte(CREATEARRAY);
13881419 luaY_val.vLong = pc; code_word(0);
13891420 }
13901421 break;
13911422 case 56:
1392−#line 635 "lua.stx"
1423+#line 630 "lua.stx"
13931424 {
13941425 code_word_at(basepc+luaY_vsp[-3].vLong, luaY_vsp[-1].vInt);
13951426 }
13961427 break;
13971428 case 57:
1398−#line 641 "lua.stx"
1429+#line 636 "lua.stx"
13991430 {
14001431 code_byte(CALLFUNC);
14011432 code_byte(luaY_vsp[-1].vInt+luaY_vsp[0].vInt);
@@ -1404,11 +1435,11 @@ case 57: · 2 unmodified lines
14041435 }
14051436 break;
14061437 case 58:
1407−#line 649 "lua.stx"
1438+#line 644 "lua.stx"
14081439 { luaY_val.vInt = 0; }
14091440 break;
14101441 case 59:
1411−#line 651 "lua.stx"
1442+#line 646 "lua.stx"
14121443 {
14131444 code_byte(PUSHSELF);
14141445 code_word(luaI_findconstant(luaY_vsp[0].pTStr));
@@ -1416,31 +1447,31 @@ case 59: · 1 unmodified line
14161447 }
14171448 break;
14181449 case 60:
1419−#line 659 "lua.stx"
1450+#line 654 "lua.stx"
14201451 { luaY_val.vInt = adjust_functioncall(luaY_vsp[-1].vLong, 1); }
14211452 break;
14221453 case 61:
1423−#line 660 "lua.stx"
1454+#line 655 "lua.stx"
14241455 { luaY_val.vInt = 1; }
14251456 break;
14261457 case 62:
1427−#line 663 "lua.stx"
1458+#line 658 "lua.stx"
14281459 { luaY_val.vLong = 0; }
14291460 break;
14301461 case 63:
1431−#line 664 "lua.stx"
1462+#line 659 "lua.stx"
14321463 { luaY_val.vLong = luaY_vsp[0].vLong; }
14331464 break;
14341465 case 64:
1435−#line 667 "lua.stx"
1466+#line 662 "lua.stx"
14361467 { if (luaY_vsp[0].vLong != 0) luaY_val.vLong = luaY_vsp[0].vLong; else luaY_val.vLong = -1; }
14371468 break;
14381469 case 65:
1439−#line 668 "lua.stx"
1470+#line 663 "lua.stx"
14401471 { luaY_val.vLong = adjust_functioncall(luaY_vsp[-1].vLong, 1); }
14411472 break;
14421473 case 66:
1443−#line 669 "lua.stx"
1474+#line 664 "lua.stx"
14441475 {
14451476 if (luaY_vsp[0].vLong == 0) luaY_val.vLong = -(luaY_vsp[-1].vLong + 1); /* -length */
14461477 else
@@ -1451,19 +1482,19 @@ case 66: · 4 unmodified lines
14511482 }
14521483 break;
14531484 case 67:
1454−#line 679 "lua.stx"
1485+#line 674 "lua.stx"
14551486 { luaY_val.vInt = close_parlist(0); }
14561487 break;
14571488 case 68:
1458−#line 680 "lua.stx"
1489+#line 675 "lua.stx"
14591490 { luaY_val.vInt = close_parlist(luaY_vsp[0].vInt); }
14601491 break;
14611492 case 69:
1462−#line 683 "lua.stx"
1493+#line 678 "lua.stx"
14631494 { luaY_val.vInt = luaY_vsp[0].vInt; }
14641495 break;
14651496 case 70:
1466−#line 685 "lua.stx"
1497+#line 680 "lua.stx"
14671498 {
14681499 if (luaY_vsp[-2].vInt)
14691500 lua_error("invalid parameter list");
@@ -1471,113 +1502,110 @@ case 70: · 1 unmodified line
14711502 }
14721503 break;
14731504 case 71:
1474−#line 692 "lua.stx"
1505+#line 687 "lua.stx"
14751506 { add_localvar(luaY_vsp[0].pTStr); luaY_val.vInt = 0; }
14761507 break;
14771508 case 72:
1478−#line 693 "lua.stx"
1509+#line 688 "lua.stx"
14791510 { luaY_val.vInt = 1; }
14801511 break;
14811512 case 73:
1482−#line 697 "lua.stx"
1513+#line 692 "lua.stx"
14831514 { flush_list(luaY_vsp[0].vInt/FIELDS_PER_FLUSH, luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }
14841515 break;
14851516 case 74:
1486−#line 699 "lua.stx"
1517+#line 694 "lua.stx"
14871518 { luaY_val.vInt = luaY_vsp[-2].vInt+luaY_vsp[0].vInt; }
14881519 break;
14891520 case 75:
1490−#line 701 "lua.stx"
1521+#line 696 "lua.stx"
14911522 { luaY_val.vInt = luaY_vsp[-1].vInt; flush_record(luaY_vsp[-1].vInt%FIELDS_PER_FLUSH); }
14921523 break;
14931524 case 76:
1494−#line 705 "lua.stx"
1525+#line 700 "lua.stx"
14951526 { luaY_val.vInt = 0; }
14961527 break;
14971528 case 77:
1498−#line 707 "lua.stx"
1529+#line 702 "lua.stx"
14991530 { luaY_val.vInt = luaY_vsp[0].vInt; flush_record(luaY_vsp[0].vInt%FIELDS_PER_FLUSH); }
15001531 break;
15011532 case 80:
1502−#line 714 "lua.stx"
1533+#line 709 "lua.stx"
15031534 { luaY_val.vInt = 0; }
15041535 break;
15051536 case 81:
1506−#line 715 "lua.stx"
1537+#line 710 "lua.stx"
15071538 { luaY_val.vInt = luaY_vsp[-1].vInt; }
15081539 break;
15091540 case 82:
1510−#line 718 "lua.stx"
1541+#line 713 "lua.stx"
15111542 {luaY_val.vInt=1;}
15121543 break;
15131544 case 83:
1514−#line 720 "lua.stx"
1545+#line 715 "lua.stx"
15151546 {
15161547 luaY_val.vInt=luaY_vsp[-2].vInt+1;
15171548 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
15181549 }
15191550 break;
1520−case 84:
1521−#line 727 "lua.stx"
1522−{
1523− push_field(luaI_findconstant(luaY_vsp[-2].pTStr));
1524− }
1551+case 86:
1552+#line 725 "lua.stx"
1553+{ code_constant(luaY_vsp[0].pTStr); }
15251554 break;
1526−case 85:
1527−#line 732 "lua.stx"
1555+case 87:
1556+#line 728 "lua.stx"
15281557 { luaY_val.vInt = 0; }
15291558 break;
1530−case 86:
1531−#line 733 "lua.stx"
1559+case 88:
1560+#line 729 "lua.stx"
15321561 { luaY_val.vInt = luaY_vsp[-1].vInt; }
15331562 break;
1534−case 87:
1535−#line 736 "lua.stx"
1563+case 89:
1564+#line 732 "lua.stx"
15361565 {luaY_val.vInt=1;}
15371566 break;
1538−case 88:
1539−#line 738 "lua.stx"
1567+case 90:
1568+#line 734 "lua.stx"
15401569 {
15411570 luaY_val.vInt=luaY_vsp[-2].vInt+1;
15421571 if (luaY_val.vInt%FIELDS_PER_FLUSH == 0)
15431572 flush_list(luaY_val.vInt/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
15441573 }
15451574 break;
1546−case 89:
1547−#line 746 "lua.stx"
1575+case 91:
1576+#line 742 "lua.stx"
15481577 {
15491578 nvarbuffer = 0;
15501579 add_varbuffer(luaY_vsp[0].vLong);
15511580 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? 1 : 0;
15521581 }
15531582 break;
1554−case 90:
1555−#line 752 "lua.stx"
1583+case 92:
1584+#line 748 "lua.stx"
15561585 {
15571586 add_varbuffer(luaY_vsp[0].vLong);
15581587 luaY_val.vInt = (luaY_vsp[0].vLong == 0) ? luaY_vsp[-2].vInt + 1 : luaY_vsp[-2].vInt;
15591588 }
15601589 break;
1561−case 91:
1562−#line 758 "lua.stx"
1590+case 93:
1591+#line 754 "lua.stx"
15631592 { luaY_val.vLong = luaY_vsp[0].vLong; }
15641593 break;
1565−case 92:
1566−#line 760 "lua.stx"
1594+case 94:
1595+#line 756 "lua.stx"
15671596 {
15681597 luaY_val.vLong = 0; /* indexed variable */
15691598 }
15701599 break;
1571−case 93:
1572−#line 764 "lua.stx"
1600+case 95:
1601+#line 760 "lua.stx"
15731602 {
1574− code_byte(PUSHSTRING);
1575− code_word(luaI_findconstant(luaY_vsp[0].pTStr));
1576− luaY_val.vLong = 0; /* indexed variable */
1603+ code_constant(luaY_vsp[0].pTStr);
1604+ luaY_val.vLong = 0; /* indexed variable */
15771605 }
15781606 break;
1579−case 94:
1580−#line 772 "lua.stx"
1607+case 96:
1608+#line 767 "lua.stx"
15811609 {
15821610 int local = lua_localname(luaY_vsp[0].pTStr);
15831611 if (local == -1) /* global var */
@@ -1586,30 +1614,30 @@ case 94: · 2 unmodified lines
15861614 luaY_val.vLong = -(local+1); /* return negative value */
15871615 }
15881616 break;
1589−case 95:
1590−#line 781 "lua.stx"
1617+case 97:
1618+#line 776 "lua.stx"
15911619 { lua_pushvar(luaY_vsp[0].vLong); }
15921620 break;
1593−case 96:
1594−#line 784 "lua.stx"
1621+case 98:
1622+#line 779 "lua.stx"
15951623 {store_localvar(luaY_vsp[0].pTStr, 0); luaY_val.vInt = 1;}
15961624 break;
1597−case 97:
1598−#line 786 "lua.stx"
1625+case 99:
1626+#line 781 "lua.stx"
15991627 {
16001628 store_localvar(luaY_vsp[0].pTStr, luaY_vsp[-2].vInt);
16011629 luaY_val.vInt = luaY_vsp[-2].vInt+1;
16021630 }
16031631 break;
1604−case 98:
1605−#line 792 "lua.stx"
1632+case 100:
1633+#line 787 "lua.stx"
16061634 { luaY_val.vInt = 0; }
16071635 break;
1608−case 99:
1609−#line 793 "lua.stx"
1636+case 101:
1637+#line 788 "lua.stx"
16101638 { luaY_val.vInt = luaY_vsp[0].vLong; }
16111639 break;
1612−#line 1613 "y.tab.c"
1640+#line 1641 "y.tab.c"
16131641 }
16141642 luaY_ssp -= luaY_m;
16151643 luaY_state = *luaY_ssp;
src/table.c+84−96
@@ -3,9 +3,11 @@ · 2 unmodified lines
33 ** Module to control static tables
44 */
55
6−char *rcs_table="$Id: table.c,v 2.58 1996/11/01 12:47:45 roberto Exp $";
6+char *rcs_table="$Id: table.c,v 2.72 1997/06/17 18:09:31 roberto Exp $";
77
8−#include "mem.h"
8+#include "luamem.h"
9+#include "auxlib.h"
10+#include "func.h"
911 #include "opcode.h"
1012 #include "tree.h"
1113 #include "hash.h"
@@ -27,49 +29,14 @@ Word lua_nconstant = 0; · 15 unmodified lines
2729 static Long lua_maxconstant = 0;
2830
2931
30−#define GARBAGE_BLOCK 50
31−
32−static void lua_nextvar (void);
33−
34−/*
35−** Internal functions
36−*/
37−static struct {
38− char *name;
39− lua_CFunction func;
40−} int_funcs[] = {
41− {"assert", luaI_assert},
42− {"call", luaI_call},
43− {"dofile", lua_internaldofile},
44− {"dostring", lua_internaldostring},
45− {"error", luaI_error},
46− {"getglobal", luaI_getglobal},
47− {"next", lua_next},
48− {"nextvar", lua_nextvar},
49− {"print", luaI_print},
50− {"setfallback", luaI_setfallback},
51− {"setglobal", luaI_setglobal},
52− {"tonumber", lua_obj2number},
53− {"tostring", luaI_tostring},
54− {"type", luaI_type}
55−};
56−
57−#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
32+#define GARBAGE_BLOCK 100
5833
5934
6035 void luaI_initsymbol (void)
6136 {
62− int i;
63− Word n;
6437 lua_maxsymbol = BUFFER_BLOCK;
6538 lua_table = newvector(lua_maxsymbol, Symbol);
66− for (i=0; i<INTFUNCSIZE; i++)
67− {
68− n = luaI_findsymbolbyname(int_funcs[i].name);
69− s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
70− }
71− n = luaI_findsymbolbyname("_VERSION_");
72− s_tag(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
39+ luaI_predefine();
7340 }
7441
7542
@@ -92,17 +59,17 @@ void luaI_initconstant (void) · 16 unmodified lines
9259 */
9360 Word luaI_findsymbol (TaggedString *t)
9461 {
95− if (t->varindex == NOT_USED)
62+ if (t->u.s.varindex == NOT_USED)
9663 {
9764 if (lua_ntable == lua_maxsymbol)
9865 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
9966 symbolEM, MAX_WORD);
100− t->varindex = lua_ntable;
67+ t->u.s.varindex = lua_ntable;
10168 lua_table[lua_ntable].varname = t;
102− s_tag(lua_ntable) = LUA_T_NIL;
69+ s_ttype(lua_ntable) = LUA_T_NIL;
10370 lua_ntable++;
10471 }
105− return t->varindex;
72+ return t->u.s.varindex;
10673 }
10774
10875
@@ -118,16 +85,16 @@ Word luaI_findsymbolbyname (char *name) · 9 unmodified lines
11885 */
11986 Word luaI_findconstant (TaggedString *t)
12087 {
121− if (t->constindex == NOT_USED)
88+ if (t->u.s.constindex == NOT_USED)
12289 {
12390 if (lua_nconstant == lua_maxconstant)
12491 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
12592 constantEM, MAX_WORD);
126− t->constindex = lua_nconstant;
93+ t->u.s.constindex = lua_nconstant;
12794 lua_constant[lua_nconstant] = t;
12895 lua_nconstant++;
12996 }
130− return t->constindex;
97+ return t->u.s.constindex;
13198 }
13299
133100
@@ -145,10 +112,16 @@ TaggedString *luaI_createfixedstring (char *name) · 11 unmodified lines
145112 }
146113
147114
115+int luaI_globaldefined (char *name)
116+{
117+ return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL;
118+}
119+
120+
148121 /*
149122 ** Traverse symbol table objects
150123 */
151−static char *lua_travsymbol (int (*fn)(Object *))
124+static char *lua_travsymbol (int (*fn)(TObject *))
152125 {
153126 Word i;
154127 for (i=0; i<lua_ntable; i++)
@@ -161,13 +134,14 @@ static char *lua_travsymbol (int (*fn)(Object *)) · 6 unmodified lines
161134 /*
162135 ** Mark an object if it is a string or a unmarked array.
163136 */
164−int lua_markobject (Object *o)
137+int lua_markobject (TObject *o)
165138 {/* if already marked, does not change mark value */
166− if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
139+ if (ttype(o) == LUA_T_USERDATA ||
140+ (ttype(o) == LUA_T_STRING && !tsvalue(o)->marked))
167141 tsvalue(o)->marked = 1;
168− else if (tag(o) == LUA_T_ARRAY)
142+ else if (ttype(o) == LUA_T_ARRAY)
169143 lua_hashmark (avalue(o));
170− else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
144+ else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
171145 && !o->value.tf->marked)
172146 o->value.tf->marked = 1;
173147 return 0;
@@ -176,11 +150,11 @@ int lua_markobject (Object *o) · 2 unmodified lines
176150 /*
177151 * returns 0 if the object is going to be (garbage) collected
178152 */
179−int luaI_ismarked (Object *o)
153+int luaI_ismarked (TObject *o)
180154 {
181− switch (o->tag)
155+ switch (o->ttype)
182156 {
183− case LUA_T_STRING:
157+ case LUA_T_STRING: case LUA_T_USERDATA:
184158 return o->value.ts->marked;
185159 case LUA_T_FUNCTION:
186160 return o->value.tf->marked;
@@ -192,76 +166,90 @@ int luaI_ismarked (Object *o) · 5 unmodified lines
192166 }
193167
194168
169+static void call_nilIM (void)
170+{ /* signals end of garbage collection */
171+ TObject t;
172+ ttype(&t) = LUA_T_NIL;
173+ luaI_gcIM(&t); /* end of list */
174+}
175+
195176 /*
196177 ** Garbage collection.
197178 ** Delete all unused strings and arrays.
198179 */
199−Long luaI_collectgarbage (void)
180+static long gc_block = GARBAGE_BLOCK;
181+static long gc_nentity = 0; /* total of strings, arrays, etc */
182+
183+static void markall (void)
200184 {
201− Long recovered = 0;
202185 lua_travstack(lua_markobject); /* mark stack objects */
203186 lua_travsymbol(lua_markobject); /* mark symbol table objects */
204187 luaI_travlock(lua_markobject); /* mark locked objects */
205188 luaI_travfallbacks(lua_markobject); /* mark fallbacks */
189+}
190+
191+
192+long lua_collectgarbage (long limit)
193+{
194+ long recovered = 0;
195+ Hash *freetable;
196+ TaggedString *freestr;
197+ TFunc *freefunc;
198+ markall();
206199 luaI_invalidaterefs();
207− recovered += lua_strcollector();
208− recovered += lua_hashcollector();
209− recovered += luaI_funccollector();
200+ freetable = luaI_hashcollector(&recovered);
201+ freestr = luaI_strcollector(&recovered);
202+ freefunc = luaI_funccollector(&recovered);
203+ gc_nentity -= recovered;
204+ gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit;
205+ luaI_hashcallIM(freetable);
206+ luaI_strcallIM(freestr);
207+ call_nilIM();
208+ luaI_hashfree(freetable);
209+ luaI_strfree(freestr);
210+ luaI_funcfree(freefunc);
210211 return recovered;
211212 }
212213
214+
213215 void lua_pack (void)
214216 {
215− static unsigned long block = GARBAGE_BLOCK;
216− static unsigned long nentity = 0; /* total of strings, arrays, etc */
217− unsigned long recovered = 0;
218− if (nentity++ < block) return;
219− recovered = luaI_collectgarbage();
220− block = 2*(block-recovered);
221− nentity -= recovered;
217+ if (++gc_nentity >= gc_block)
218+ lua_collectgarbage(0);
222219 }
223220
224221
225222 /*
226223 ** Internal function: return next global variable
227224 */
228−static void lua_nextvar (void)
225+void luaI_nextvar (void)
229226 {
230− Word next;
231− lua_Object o = lua_getparam(1);
232− if (o == LUA_NOOBJECT)
233− lua_error("too few arguments to function `nextvar'");
234− if (lua_getparam(2) != LUA_NOOBJECT)
235− lua_error("too many arguments to function `nextvar'");
236− if (lua_isnil(o))
237− next = 0;
238− else if (!lua_isstring(o))
239− {
240− lua_error("incorrect argument to function `nextvar'");
241− return; /* to avoid warnings */
242− }
243− else
244− next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
245− while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
246− if (next < lua_ntable)
247− {
248− lua_pushstring(lua_table[next].varname->str);
249− luaI_pushobject(&s_object(next));
250− }
227+ Word next;
228+ if (lua_isnil(lua_getparam(1)))
229+ next = 0;
230+ else
231+ next = luaI_findsymbolbyname(luaL_check_string(1)) + 1;
232+ while (next < lua_ntable && s_ttype(next) == LUA_T_NIL)
233+ next++;
234+ if (next < lua_ntable) {
235+ lua_pushstring(lua_table[next].varname->str);
236+ luaI_pushobject(&s_object(next));
237+ }
251238 }
252239
253240
254−static Object *functofind;
255−static int checkfunc (Object *o)
241+static TObject *functofind;
242+static int checkfunc (TObject *o)
256243 {
257− if (o->tag == LUA_T_FUNCTION)
244+ if (o->ttype == LUA_T_FUNCTION)
258245 return
259− ((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
246+ ((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
260247 && (functofind->value.tf == o->value.tf));
261− if (o->tag == LUA_T_CFUNCTION)
248+ if (o->ttype == LUA_T_CFUNCTION)
262249 return
263− ((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
264− && (functofind->value.f == o->value.f));
250+ ((functofind->ttype == LUA_T_CFUNCTION ||
251+ functofind->ttype == LUA_T_CMARK) &&
252+ (functofind->value.f == o->value.f));
265253 return 0;
266254 }
267255
@@ -270,7 +258,7 @@ char *lua_getobjname (lua_Object o, char **name) · 2 unmodified lines
270258 { /* try to find a name for given function */
271259 functofind = luaI_Address(o);
272260 if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
273− return "fallback";
261+ return "tag-method";
274262 else if ((*name = lua_travsymbol(checkfunc)) != NULL)
275263 return "global";
276264 else return "";
src/table.h+6−5
@@ -1,7 +1,7 @@
11 /*
22 ** Module to control static tables
33 ** TeCGraf - PUC-Rio
4−** $Id: table.h,v 2.21 1996/04/22 18:00:37 roberto Exp $
4+** $Id: table.h,v 2.25 1997/05/26 14:42:36 roberto Exp $
55 */
66
77 #ifndef table_h
@@ -12,7 +12,7 @@ · 4 unmodified lines
1212
1313 typedef struct
1414 {
15− Object object;
15+ TObject object;
1616 TaggedString *varname;
1717 } Symbol;
1818
@@ -28,10 +28,11 @@ Word luaI_findsymbolbyname (char *name); · 9 unmodified lines
2828 Word luaI_findsymbol (TaggedString *t);
2929 Word luaI_findconstant (TaggedString *t);
3030 Word luaI_findconstantbyname (char *name);
31+int luaI_globaldefined (char *name);
32+void luaI_nextvar (void);
3133 TaggedString *luaI_createfixedstring (char *str);
32−int lua_markobject (Object *o);
33−int luaI_ismarked (Object *o);
34−Long luaI_collectgarbage (void);
34+int lua_markobject (TObject *o);
35+int luaI_ismarked (TObject *o);
3536 void lua_pack (void);
3637
3738
src/tree.c+92−26
@@ -3,17 +3,18 @@ · 2 unmodified lines
33 ** TecCGraf - PUC-Rio
44 */
55
6−char *rcs_tree="$Id: tree.c,v 1.20 1996/03/14 15:56:26 roberto Exp $";
6+char *rcs_tree="$Id: tree.c,v 1.28 1997/06/11 14:24:40 roberto Exp $";
77
88
99 #include <string.h>
1010
11−#include "mem.h"
11+#include "luamem.h"
1212 #include "lua.h"
1313 #include "tree.h"
1414 #include "lex.h"
1515 #include "hash.h"
1616 #include "table.h"
17+#include "fallback.h"
1718
1819
1920 #define NUM_HASHS 64
@@ -28,23 +29,43 @@ static int initialized = 0; · 8 unmodified lines
2829
2930 static stringtable string_root[NUM_HASHS];
3031
31−static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
32+static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
33+ 0, 2, {0}};
3234
3335
34−static unsigned long hash (char *str)
36+static unsigned long hash (char *s, int tag)
3537 {
36− unsigned long h = 0;
37− while (*str)
38− h = ((h<<5)-h)^(unsigned char)*(str++);
38+ unsigned long h;
39+ if (tag != LUA_T_STRING)
40+ h = (unsigned long)s;
41+ else {
42+ h = 0;
43+ while (*s)
44+ h = ((h<<5)-h)^(unsigned char)*(s++);
45+ }
3946 return h;
4047 }
4148
49+
50+static void luaI_inittree (void)
51+{
52+ int i;
53+ for (i=0; i<NUM_HASHS; i++) {
54+ string_root[i].size = 0;
55+ string_root[i].nuse = 0;
56+ string_root[i].hash = NULL;
57+ }
58+}
59+
60+
4261 static void initialize (void)
4362 {
4463 initialized = 1;
64+ luaI_inittree();
4565 luaI_addReserved();
4666 luaI_initsymbol();
4767 luaI_initconstant();
68+ luaI_initfallbacks();
4869 }
4970
5071
@@ -58,8 +79,7 @@ static void grow (stringtable *tb) · 7 unmodified lines
5879 /* rehash */
5980 tb->nuse = 0;
6081 for (i=0; i<tb->size; i++)
61− if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
62− {
82+ if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
6383 int h = tb->hash[i]->hash%newsize;
6484 while (newhash[h])
6585 h = (h+1)%newsize;
@@ -71,10 +91,30 @@ static void grow (stringtable *tb) · 5 unmodified lines
7191 tb->hash = newhash;
7292 }
7393
74−static TaggedString *insert (char *str, stringtable *tb)
94+
95+static TaggedString *newone(char *buff, int tag, unsigned long h)
96+{
97+ TaggedString *ts;
98+ if (tag == LUA_T_STRING) {
99+ ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
100+ strcpy(ts->str, buff);
101+ ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
102+ ts->tag = LUA_T_STRING;
103+ }
104+ else {
105+ ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
106+ ts->u.v = buff;
107+ ts->tag = tag == LUA_ANYTAG ? 0 : tag;
108+ }
109+ ts->marked = 0;
110+ ts->hash = h;
111+ return ts;
112+}
113+
114+static TaggedString *insert (char *buff, int tag, stringtable *tb)
75115 {
76116 TaggedString *ts;
77− unsigned long h = hash(str);
117+ unsigned long h = hash(buff, tag);
78118 int i;
79119 int j = -1;
80120 if ((Long)tb->nuse*3 >= (Long)tb->size*2)
@@ -84,12 +124,14 @@ static TaggedString *insert (char *str, stringtable *tb) · 3 unmodified lines
84124 grow(tb);
85125 }
86126 i = h%tb->size;
87− while (tb->hash[i])
127+ while ((ts = tb->hash[i]) != NULL)
88128 {
89− if (tb->hash[i] == &EMPTY)
129+ if (ts == &EMPTY)
90130 j = i;
91− else if (strcmp(str, tb->hash[i]->str) == 0)
92− return tb->hash[i];
131+ else if ((ts->tag == LUA_T_STRING) ?
132+ (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
133+ ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
134+ return ts;
93135 i = (i+1)%tb->size;
94136 }
95137 /* not found */
@@ -98,27 +140,49 @@ static TaggedString *insert (char *str, stringtable *tb) · 2 unmodified lines
98140 i = j;
99141 else
100142 tb->nuse++;
101− ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
102− strcpy(ts->str, str);
103− ts->marked = 0;
104− ts->hash = h;
105− ts->varindex = ts->constindex = NOT_USED;
143+ ts = tb->hash[i] = newone(buff, tag, h);
106144 return ts;
107145 }
108146
109−TaggedString *lua_createstring (char *str)
147+TaggedString *luaI_createudata (void *udata, int tag)
110148 {
111− return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
149+ return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
150+}
151+
152+TaggedString *lua_createstring (char *str)
153+{
154+ return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
155+}
156+
157+
158+void luaI_strcallIM (TaggedString *l)
159+{
160+ TObject o;
161+ ttype(&o) = LUA_T_USERDATA;
162+ for (; l; l=l->next) {
163+ tsvalue(&o) = l;
164+ luaI_gcIM(&o);
165+ }
166+}
167+
168+
169+void luaI_strfree (TaggedString *l)
170+{
171+ while (l) {
172+ TaggedString *next = l->next;
173+ luaI_free(l);
174+ l = next;
175+ }
112176 }
113177
114178
115179 /*
116180 ** Garbage collection function.
117−** This function traverse the string list freeing unindexed strings
118181 */
119−Long lua_strcollector (void)
182+TaggedString *luaI_strcollector (long *acum)
120183 {
121184 Long counter = 0;
185+ TaggedString *frees = NULL;
122186 int i;
123187 for (i=0; i<NUM_HASHS; i++)
124188 {
@@ -133,13 +197,15 @@ Long lua_strcollector (void) · 8 unmodified lines
133197 t->marked = 0;
134198 else
135199 {
136− luaI_free(t);
200+ t->next = frees;
201+ frees = t;
137202 tb->hash[j] = &EMPTY;
138203 counter++;
139204 }
140205 }
141206 }
142207 }
143− return counter;
208+ *acum += counter;
209+ return frees;
144210 }
145211
src/tree.h+14−4
@@ -1,7 +1,7 @@
11 /*
22 ** tree.h
33 ** TecCGraf - PUC-Rio
4−** $Id: tree.h,v 1.14 1996/02/26 17:07:49 roberto Exp $
4+** $Id: tree.h,v 1.18 1997/06/09 17:28:14 roberto Exp $
55 */
66
77 #ifndef tree_h
@@ -14,8 +14,15 @@ · 6 unmodified lines
1414
1515 typedef struct TaggedString
1616 {
17− Word varindex; /* != NOT_USED if this is a symbol */
18− Word constindex; /* != NOT_USED if this is a constant */
17+ int tag; /* if != LUA_T_STRING, this is a userdata */
18+ struct TaggedString *next;
19+ union {
20+ struct {
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;
1926 unsigned long hash; /* 0 if not initialized */
2027 int marked; /* for garbage collection; never collect (nor change) if > 1 */
2128 char str[1]; /* \0 byte already reserved */
@@ -23,6 +30,9 @@ typedef struct TaggedString · 1 unmodified line
2330
2431
2532 TaggedString *lua_createstring (char *str);
26−Long lua_strcollector (void);
33+TaggedString *luaI_createudata (void *udata, int tag);
34+TaggedString *luaI_strcollector (long *cont);
35+void luaI_strfree (TaggedString *l);
36+void luaI_strcallIM (TaggedString *l);
2737
2838 #endif
src/undump.c+71−74
@@ -3,33 +3,28 @@ · 2 unmodified lines
33 ** load bytecodes from files
44 */
55
6−char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp $";
6+char* rcs_undump="$Id: undump.c,v 1.24 1997/06/17 18:19:17 roberto Exp $";
77
88 #include <stdio.h>
99 #include <string.h>
10+#include "auxlib.h"
1011 #include "opcode.h"
11−#include "mem.h"
12+#include "luamem.h"
1213 #include "table.h"
1314 #include "undump.h"
15+#include "zio.h"
1416
1517 static int swapword=0;
1618 static int swapfloat=0;
1719 static TFunc* Main=NULL; /* functions in a chunk */
1820 static TFunc* lastF=NULL;
1921
20−static void warn(char* s) /* TODO: remove */
21−{
22−#if 0
23− fprintf(stderr,"undump: %s\n",s);
24−#endif
25−}
26−
2722 static void FixCode(Byte* code, Byte* end) /* swap words */
2823 {
2924 Byte* p;
3025 for (p=code; p!=end;)
3126 {
32− OpCode op=(OpCode)*p;
27+ int op=*p;
3328 switch (op)
3429 {
3530 case PUSHNIL:
@@ -83,6 +78,8 @@ static void FixCode(Byte* code, Byte* end) /* swap words */ · 47 unmodified lines
8378 case STORELIST0:
8479 case ADJUST:
8580 case RETCODE:
81+ case VARARGS:
82+ case STOREMAP:
8683 p+=2;
8784 break;
8885 case STORELIST:
@@ -132,7 +129,8 @@ static void FixCode(Byte* code, Byte* end) /* swap words */ · 43 unmodified lines
132129 break;
133130 }
134131 default:
135− lua_error("corrupt binary file");
132+ luaL_verror("corrupt binary file: bad opcode %d at %d\n",
133+ op,(int)(p-code));
136134 break;
137135 }
138136 }
@@ -150,149 +148,152 @@ static void Unthread(Byte* code, int i, int v) · 11 unmodified lines
150148 }
151149 }
152150
153−static int LoadWord(FILE* D)
151+static int LoadWord(ZIO* Z)
154152 {
155153 Word w;
156− fread(&w,sizeof(w),1,D);
154+ zread(Z,&w,sizeof(w));
157155 if (swapword)
158156 {
159− Byte* p=(Byte*)&w; /* TODO: need union? */
157+ Byte* p=(Byte*)&w;
160158 Byte t;
161159 t=p[0]; p[0]=p[1]; p[1]=t;
162160 }
163161 return w;
164162 }
165163
166−static int LoadSize(FILE* D)
164+static int LoadSize(ZIO* Z)
167165 {
168− Word hi=LoadWord(D);
169− Word lo=LoadWord(D);
166+ Word hi=LoadWord(Z);
167+ Word lo=LoadWord(Z);
170168 int s=(hi<<16)|lo;
171169 if ((Word)s != s) lua_error("code too long");
172170 return s;
173171 }
174172
175−static void* LoadBlock(int size, FILE* D)
173+static void* LoadBlock(int size, ZIO* Z)
176174 {
177175 void* b=luaI_malloc(size);
178− fread(b,size,1,D);
176+ zread(Z,b,size);
179177 return b;
180178 }
181179
182−static char* LoadString(FILE* D)
180+static char* LoadString(ZIO* Z)
183181 {
184− int size=LoadWord(D);
182+ int size=LoadWord(Z);
185183 char *b=luaI_buffer(size);
186− fread(b,size,1,D);
184+ zread(Z,b,size);
187185 return b;
188186 }
189187
190−static char* LoadNewString(FILE* D)
188+static char* LoadNewString(ZIO* Z)
191189 {
192− return LoadBlock(LoadWord(D),D);
190+ return LoadBlock(LoadWord(Z),Z);
193191 }
194192
195−static void LoadFunction(FILE* D)
193+static void LoadFunction(ZIO* Z)
196194 {
197195 TFunc* tf=new(TFunc);
198196 tf->next=NULL;
199197 tf->locvars=NULL;
200− tf->size=LoadSize(D);
201− tf->lineDefined=LoadWord(D);
198+ tf->size=LoadSize(Z);
199+ tf->lineDefined=LoadWord(Z);
202200 if (IsMain(tf)) /* new main */
203201 {
204− tf->fileName=LoadNewString(D);
202+ tf->fileName=LoadNewString(Z);
205203 Main=lastF=tf;
206204 }
207205 else /* fix PUSHFUNCTION */
208206 {
209− tf->marked=LoadWord(D);
207+ tf->marked=LoadWord(Z);
210208 tf->fileName=Main->fileName;
211209 memcpy(Main->code+tf->marked,&tf,sizeof(tf));
212210 lastF=lastF->next=tf;
213211 }
214− tf->code=LoadBlock(tf->size,D);
212+ tf->code=LoadBlock(tf->size,Z);
215213 if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
216214 while (1) /* unthread */
217215 {
218− int c=getc(D);
216+ int c=zgetc(Z);
219217 if (c==ID_VAR) /* global var */
220218 {
221− int i=LoadWord(D);
222− char* s=LoadString(D);
219+ int i=LoadWord(Z);
220+ char* s=LoadString(Z);
223221 int v=luaI_findsymbolbyname(s);
224222 Unthread(tf->code,i,v);
225223 }
226224 else if (c==ID_STR) /* constant string */
227225 {
228− int i=LoadWord(D);
229− char* s=LoadString(D);
226+ int i=LoadWord(Z);
227+ char* s=LoadString(Z);
230228 int v=luaI_findconstantbyname(s);
231229 Unthread(tf->code,i,v);
232230 }
233231 else
234232 {
235− ungetc(c,D);
233+ zungetc(Z);
236234 break;
237235 }
238236 }
239237 }
240238
241−static void LoadSignature(FILE* D)
239+static void LoadSignature(ZIO* Z)
242240 {
243241 char* s=SIGNATURE;
244− while (*s!=0 && getc(D)==*s)
242+ while (*s!=0 && zgetc(Z)==*s)
245243 ++s;
246− if (*s!=0) lua_error("bad signature");
244+ if (*s!=0) lua_error("cannot load binary file: bad signature");
247245 }
248246
249−static void LoadHeader(FILE* D) /* TODO: error handling */
247+static void LoadHeader(ZIO* Z)
250248 {
251249 Word w,tw=TEST_WORD;
252250 float f,tf=TEST_FLOAT;
253251 int version;
254− LoadSignature(D);
255− version=getc(D);
252+ LoadSignature(Z);
253+ version=zgetc(Z);
256254 if (version>0x23) /* after 2.5 */
257255 {
258− int oldsizeofW=getc(D);
259− int oldsizeofF=getc(D);
260− int oldsizeofP=getc(D);
256+ int oldsizeofW=zgetc(Z);
257+ int oldsizeofF=zgetc(Z);
258+ int oldsizeofP=zgetc(Z);
261259 if (oldsizeofW!=2)
262− lua_error("cannot load binary file created on machine with sizeof(Word)!=2");
260+ luaL_verror(
261+ "cannot load binary file created on machine with sizeof(Word)=%d; "
262+ "expected 2",oldsizeofW);
263263 if (oldsizeofF!=4)
264− lua_error("cannot load binary file created on machine with sizeof(float)!=4. not an IEEE machine?");
265− if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack */
266− lua_error("cannot load binary file: different pointer sizes");
264+ luaL_verror(
265+ "cannot load binary file created on machine with sizeof(float)=%d; "
266+ "expected 4\nnot an IEEE machine?",oldsizeofF);
267+ if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
268+ luaL_verror(
269+ "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
270+ "expected %d",oldsizeofP,(int)sizeof(TFunc*));
267271 }
268− fread(&w,sizeof(w),1,D); /* test word */
272+ zread(Z,&w,sizeof(w)); /* test word */
269273 if (w!=tw)
270274 {
271275 swapword=1;
272− warn("different byte order");
273276 }
274− fread(&f,sizeof(f),1,D); /* test float */
277+ zread(Z,&f,sizeof(f)); /* test float */
275278 if (f!=tf)
276279 {
277− Byte* p=(Byte*)&f; /* TODO: need union? */
280+ Byte* p=(Byte*)&f;
278281 Byte t;
279282 swapfloat=1;
280283 t=p[0]; p[0]=p[3]; p[3]=t;
281284 t=p[1]; p[1]=p[2]; p[2]=t;
282285 if (f!=tf) /* TODO: try another perm? */
283− lua_error("different float representation");
284− else
285− warn("different byte order in floats");
286+ lua_error("cannot load binary file: unknown float representation");
286287 }
287288 }
288289
289−static void LoadChunk(FILE* D)
290+static void LoadChunk(ZIO* Z)
290291 {
291− LoadHeader(D);
292+ LoadHeader(Z);
292293 while (1)
293294 {
294− int c=getc(D);
295− if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
295+ int c=zgetc(Z);
296+ if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
296297 }
297298 }
298299
@@ -300,30 +301,26 @@ static void LoadChunk(FILE* D) · 1 unmodified line
300301 ** load one chunk from a file.
301302 ** return list of functions found, headed by main, or NULL at EOF.
302303 */
303−TFunc* luaI_undump1(FILE* D)
304+TFunc* luaI_undump1(ZIO* Z)
304305 {
305− while (1)
306+ int c=zgetc(Z);
307+ if (c==ID_CHUNK)
306308 {
307− int c=getc(D);
308− if (c==ID_CHUNK)
309− {
310− LoadChunk(D);
311− return Main;
312− }
313− else if (c==EOF)
314− return NULL;
315− else
316− lua_error("not a lua binary file");
309+ LoadChunk(Z);
310+ return Main;
317311 }
312+ else if (c!=EOZ)
313+ lua_error("not a lua binary file");
314+ return NULL;
318315 }
319316
320317 /*
321318 ** load and run all chunks in a file
322319 */
323−int luaI_undump(FILE* D)
320+int luaI_undump(ZIO* Z)
324321 {
325322 TFunc* m;
326− while ((m=luaI_undump1(D)))
323+ while ((m=luaI_undump1(Z)))
327324 {
328325 int status=luaI_dorun(m);
329326 luaI_freefunc(m);
src/undump.h+11−4
@@ -1,10 +1,14 @@
11 /*
22 ** undump.h
33 ** definitions for lua decompiler
4−** $Id: undump.h,v 1.3 1996/11/14 11:44:34 lhf Exp $
4+** $Id: undump.h,v 1.6 1997/06/17 18:19:17 roberto Exp $
55 */
66
7+#ifndef undump_h
8+#define undump_h
9+
710 #include "func.h"
11+#include "zio.h"
812
913 #define IsMain(f) (f->lineDefined==0)
1014
@@ -15,9 +19,12 @@ · 4 unmodified lines
1519 #define ID_VAR 'V'
1620 #define ID_STR 'S'
1721 #define SIGNATURE "Lua"
18−#define VERSION 0x25 /* 2.5 */
22+#define VERSION 0x25 /* last format change was in 2.5 */
1923 #define TEST_WORD 0x1234 /* a word for testing byte ordering */
2024 #define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
2125
22−TFunc* luaI_undump1(FILE* D); /* load one chunk */
23−int luaI_undump(FILE* D); /* load all chunks */
26+
27+TFunc* luaI_undump1(ZIO* Z);
28+int luaI_undump(ZIO* Z); /* load all chunks */
29+
30+#endif
src/zio.c+79−0
@@ -0,0 +1,79 @@
1+/*
2+* zio.c
3+* a generic input stream interface
4+* $Id: zio.c,v 1.2 1997/06/20 19:25:54 roberto Exp $
5+*/
6+
7+#include <stdio.h>
8+#include <stdlib.h>
9+#include <string.h>
10+#include "zio.h"
11+
12+
13+/* ----------------------------------------------------- memory buffers --- */
14+
15+static int zmfilbuf(ZIO* z)
16+{
17+ return EOZ;
18+}
19+
20+ZIO* zmopen(ZIO* z, char* b, int size)
21+{
22+ if (b==NULL) return NULL;
23+ z->n=size;
24+ z->p= (unsigned char *)b;
25+ z->filbuf=zmfilbuf;
26+ z->u=NULL;
27+ return z;
28+}
29+
30+/* ------------------------------------------------------------ strings --- */
31+
32+ZIO* zsopen(ZIO* z, char* s)
33+{
34+ if (s==NULL) return NULL;
35+ return zmopen(z,s,strlen(s));
36+}
37+
38+/* -------------------------------------------------------------- FILEs --- */
39+
40+static int zffilbuf(ZIO* z)
41+{
42+ int n=fread(z->buffer,1,ZBSIZE,z->u);
43+ if (n==0) return EOZ;
44+ z->n=n-1;
45+ z->p=z->buffer;
46+ return *(z->p++);
47+}
48+
49+
50+ZIO* zFopen(ZIO* z, FILE* f)
51+{
52+ if (f==NULL) return NULL;
53+ z->n=0;
54+ z->p=z->buffer;
55+ z->filbuf=zffilbuf;
56+ z->u=f;
57+ return z;
58+}
59+
60+
61+/* --------------------------------------------------------------- read --- */
62+int zread(ZIO *z, void *b, int n)
63+{
64+ while (n) {
65+ int m;
66+ if (z->n == 0) {
67+ if (z->filbuf(z) == EOZ)
68+ return n; /* retorna quantos faltaram ler */
69+ zungetc(z); /* poe o resultado de filbuf no buffer */
70+ }
71+ m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
72+ memcpy(b, z->p, m);
73+ z->n -= m;
74+ z->p += m;
75+ b = (char *)b + m;
76+ n -= m;
77+ }
78+ return 0;
79+}
src/zio.h+48−0
@@ -0,0 +1,48 @@
1+/*
2+* zio.h
3+* a generic input stream interface
4+* $Id: zio.h,v 1.5 1997/06/20 19:25:54 roberto Exp $
5+*/
6+
7+#ifndef zio_h
8+#define zio_h
9+
10+#include <stdio.h>
11+
12+
13+
14+/* For Lua only */
15+#define zFopen luaZ_Fopen
16+#define zsopen luaZ_sopen
17+#define zmopen luaZ_mopen
18+#define zread luaZ_read
19+
20+#define EOZ (-1) /* end of stream */
21+
22+typedef struct zio ZIO;
23+
24+ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
25+ZIO* zsopen(ZIO* z, char* s); /* string */
26+ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
27+
28+int zread(ZIO* z, void* b, int n); /* read next n bytes */
29+
30+#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
31+#define zungetc(z) (++(z)->n,--(z)->p)
32+
33+
34+
35+/* --------- Private Part ------------------ */
36+
37+#define ZBSIZE 256 /* buffer size */
38+
39+struct zio {
40+ int n; /* bytes still unread */
41+ unsigned char* p; /* current position in buffer */
42+ int (*filbuf)(ZIO* z);
43+ void* u; /* additional data */
44+ unsigned char buffer[ZBSIZE]; /* buffer */
45+};
46+
47+
48+#endif