3 ** Provide function to realise the input/output function and debugger
5 ** Also provides some predefined lua functions.
8 char *rcs_inout
="$Id: inout.c,v 2.69 1997/06/27 22:38:49 roberto Exp $";
28 /* Exported variables */
33 char *luaI_typenames
[] = { /* ORDER LUA_T */
34 "userdata", "line", "cmark", "mark", "function",
35 "function", "table", "string", "number", "nil",
41 void luaI_setparsedfile (char *name
)
43 lua_parsedfile
= luaI_createfixedstring(name
)->str
;
47 int lua_doFILE (FILE *f
, int bin
)
52 return luaI_undump(&z
);
60 int lua_dofile (char *filename
)
64 FILE *f
= (filename
== NULL
) ? stdin
: fopen(filename
, "r");
67 luaI_setparsedfile(filename
?filename
:"(stdin)");
71 f
= freopen(filename
, "rb", f
); /* set binary mode */
72 status
= lua_doFILE(f
, 1);
76 while ((c
=fgetc(f
)) != '\n') /* skip first line */;
77 status
= lua_doFILE(f
, 0);
86 #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
89 int lua_dobuffer (char *buff
, int size
)
93 luaI_setparsedfile("(buffer)");
94 luaZ_mopen(&z
, buff
, size
);
95 status
= luaI_undump(&z
);
100 int lua_dostring (char *str
)
103 char buff
[SIZE_PREF
+25];
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
);
113 status
= lua_domain();
120 static int passresults (void)
124 while ((obj
= lua_getresult(++arg
)) != LUA_NOOBJECT
)
130 static void packresults (void)
134 lua_Object table
= lua_createtable();
135 while ((obj
= lua_getresult(++arg
)) != LUA_NOOBJECT
) {
136 lua_pushobject(table
);
141 lua_pushobject(table
);
143 lua_pushnumber(arg
-1);
145 lua_pushobject(table
); /* final result */
149 ** Internal function: do a string
151 static void lua_internaldostring (void)
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");
158 err
= lua_seterrormethod();
160 if (lua_dostring(luaL_check_string(1)) == 0)
161 if (passresults() == 0)
162 lua_pushuserdata(NULL
); /* at least one result to signal no errors */
163 if (err
!= LUA_NOOBJECT
) { /* restore old error method */
165 lua_seterrormethod();
170 ** Internal function: do a file
172 static void lua_internaldofile (void)
174 char *fname
= luaL_opt_string(1, NULL
);
175 if (lua_dofile(fname
) == 0)
176 if (passresults() == 0)
177 lua_pushuserdata(NULL
); /* at least one result to signal no errors */
181 static char *tostring (lua_Object obj
)
183 TObject
*o
= luaI_Address(obj
);
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
);
195 default: return "<unknown object>";
199 static void luaI_tostring (void)
201 lua_pushstring(tostring(lua_getparam(1)));
204 static void luaI_print (void)
208 while ((obj
= lua_getparam(i
++)) != LUA_NOOBJECT
)
209 printf("%s\n", tostring(obj
));
212 static void luaI_type (void)
214 lua_Object o
= lua_getparam(1);
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
));
221 ** Internal function: convert an object to a number
223 static void lua_obj2number (void)
225 lua_Object o
= lua_getparam(1);
227 lua_pushnumber(lua_getnumber(o
));
231 static void luaI_error (void)
233 char *s
= lua_getstring(lua_getparam(1));
234 if (s
== NULL
) s
= "(no message)";
238 static void luaI_assert (void)
240 lua_Object p
= lua_getparam(1);
241 if (p
== LUA_NOOBJECT
|| lua_isnil(p
))
242 lua_error("assertion failed!");
245 static void luaI_setglobal (void)
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 */
254 static void luaI_rawsetglobal (void)
256 lua_Object value
= lua_getparam(2);
257 luaL_arg_check(value
!= LUA_NOOBJECT
, 2, NULL
);
258 lua_pushobject(value
);
259 lua_rawsetglobal(luaL_check_string(1));
260 lua_pushobject(value
); /* return given value */
263 static void luaI_getglobal (void)
265 lua_pushobject(lua_getglobal(luaL_check_string(1)));
268 static void luaI_rawgetglobal (void)
270 lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
273 static void luatag (void)
275 lua_pushnumber(lua_tag(lua_getparam(1)));
279 static int getnarg (lua_Object table
)
283 lua_pushobject(table
); lua_pushstring("n"); temp
= lua_gettable();
284 return (lua_isnumber(temp
) ? lua_getnumber(temp
) : MAX_WORD
);
287 static void luaI_call (void)
289 lua_Object f
= lua_getparam(1);
290 lua_Object arg
= lua_getparam(2);
291 int withtable
= (strcmp(luaL_opt_string(3, ""), "pack") == 0);
293 luaL_arg_check(lua_isfunction(f
), 1, "function expected");
294 luaL_arg_check(lua_istable(arg
), 2, "table expected");
296 /* push arg[1...n] */
297 for (i
=0; i
<narg
; i
++) {
299 /* temp = arg[i+1] */
300 lua_pushobject(arg
); lua_pushnumber(i
+1); temp
= lua_gettable();
301 if (narg
== MAX_WORD
&& lua_isnil(temp
))
303 lua_pushobject(temp
);
305 if (lua_callfunction(f
))
313 static void luaIl_settag (void)
315 lua_Object o
= lua_getparam(1);
316 luaL_arg_check(lua_istable(o
), 1, "table expected");
318 lua_settag(luaL_check_number(2));
321 static void luaIl_newtag (void)
323 lua_pushnumber(lua_newtag());
326 static void rawgettable (void)
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
);
334 lua_pushobject(lua_rawgettable());
337 static void rawsettable (void)
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
,
351 static void luaI_collectgarbage (void)
353 lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
358 ** Internal functions
364 {"assert", luaI_assert
},
366 {"collectgarbage", luaI_collectgarbage
},
367 {"dofile", lua_internaldofile
},
368 {"dostring", lua_internaldostring
},
369 {"error", luaI_error
},
370 {"getglobal", luaI_getglobal
},
371 {"newtag", luaIl_newtag
},
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
},
381 {"setfallback", luaI_setfallback
},
383 {"setglobal", luaI_setglobal
},
384 {"settagmethod", luaI_settagmethod
},
385 {"gettagmethod", luaI_gettagmethod
},
386 {"settag", luaIl_settag
},
387 {"tonumber", lua_obj2number
},
388 {"tostring", luaI_tostring
},
393 #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
396 void luaI_predefine (void)
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
;
404 n
= luaI_findsymbolbyname("_VERSION");
405 s_ttype(n
) = LUA_T_STRING
; s_tsvalue(n
) = lua_createstring(LUA_VERSION
);