aboutsummaryrefslogtreecommitdiff
path: root/lbuiltin.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbuiltin.c')
-rw-r--r--lbuiltin.c404
1 files changed, 404 insertions, 0 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
new file mode 100644
index 00000000..eeb3daf8
--- /dev/null
+++ b/lbuiltin.c
@@ -0,0 +1,404 @@
1/*
2** $Id: $
3** Built-in functions
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9
10#include "lapi.h"
11#include "lauxlib.h"
12#include "lbuiltin.h"
13#include "lglobal.h"
14#include "lmem.h"
15#include "lstring.h"
16#include "ltable.h"
17#include "ltm.h"
18#include "lua.h"
19
20
21static void nextvar (void)
22{
23 int i = luaG_nextvar(lua_isnil(lua_getparam(1)) ? 0 :
24 luaG_findsymbolbyname(luaL_check_string(1))+1);
25 if (i >= 0) {
26 lua_pushstring(luaG_global[i].varname->str);
27 luaA_pushobject(&s_object(i));
28 }
29}
30
31
32static void next (void)
33{
34 lua_Object o = lua_getparam(1);
35 lua_Object r = lua_getparam(2);
36 Node *n;
37 luaL_arg_check(lua_istable(o), 1, "table expected");
38 luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
39 n = luaH_next(luaA_Address(o), luaA_Address(r));
40 if (n) {
41 luaA_pushobject(&n->ref);
42 luaA_pushobject(&n->val);
43 }
44}
45
46
47static void internaldostring (void)
48{
49 lua_Object err = lua_getparam(2);
50 if (err != LUA_NOOBJECT) { /* set new error method */
51 lua_pushobject(err);
52 err = lua_seterrormethod();
53 }
54 if (lua_dostring(luaL_check_string(1)) == 0)
55 if (luaA_passresults() == 0)
56 lua_pushuserdata(NULL); /* at least one result to signal no errors */
57 if (err != LUA_NOOBJECT) { /* restore old error method */
58 lua_pushobject(err);
59 lua_seterrormethod();
60 }
61}
62
63
64static void internaldofile (void)
65{
66 char *fname = luaL_opt_string(1, NULL);
67 if (lua_dofile(fname) == 0)
68 if (luaA_passresults() == 0)
69 lua_pushuserdata(NULL); /* at least one result to signal no errors */
70}
71
72
73static char *to_string (lua_Object obj)
74{
75 char *buff = luaM_buffer(30);
76 TObject *o = luaA_Address(obj);
77 switch (ttype(o)) {
78 case LUA_T_NUMBER: case LUA_T_STRING:
79 return lua_getstring(obj);
80 case LUA_T_ARRAY: {
81 sprintf(buff, "table: %p", o->value.a);
82 return buff;
83 }
84 case LUA_T_FUNCTION: {
85 sprintf(buff, "function: %p", o->value.cl);
86 return buff;
87 }
88 case LUA_T_CFUNCTION: {
89 sprintf(buff, "cfunction: %p", o->value.f);
90 return buff;
91 }
92 case LUA_T_USERDATA: {
93 sprintf(buff, "userdata: %p", o->value.ts->u.v);
94 return buff;
95 }
96 case LUA_T_NIL:
97 return "nil";
98 default: return "<unknown object>";
99 }
100}
101
102static void bi_tostring (void)
103{
104 lua_pushstring(to_string(lua_getparam(1)));
105}
106
107
108static void luaI_print (void)
109{
110 int i = 1;
111 lua_Object obj;
112 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
113 printf("%s\n", to_string(obj));
114}
115
116
117static void luaI_type (void)
118{
119 lua_Object o = lua_getparam(1);
120 luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
121 lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
122 lua_pushnumber(lua_tag(o));
123}
124
125
126static void lua_obj2number (void)
127{
128 lua_Object o = lua_getparam(1);
129 if (lua_isnumber(o))
130 lua_pushnumber(lua_getnumber(o));
131}
132
133
134static void luaI_error (void)
135{
136 char *s = lua_getstring(lua_getparam(1));
137 if (s == NULL) s = "(no message)";
138 lua_error(s);
139}
140
141
142static void luaI_assert (void)
143{
144 lua_Object p = lua_getparam(1);
145 if (p == LUA_NOOBJECT || lua_isnil(p))
146 lua_error("assertion failed!");
147}
148
149
150static void setglobal (void)
151{
152 lua_Object value = lua_getparam(2);
153 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
154 lua_pushobject(value);
155 lua_setglobal(luaL_check_string(1));
156 lua_pushobject(value); /* return given value */
157}
158
159static void rawsetglobal (void)
160{
161 lua_Object value = lua_getparam(2);
162 luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
163 lua_pushobject(value);
164 lua_rawsetglobal(luaL_check_string(1));
165 lua_pushobject(value); /* return given value */
166}
167
168static void getglobal (void)
169{
170 lua_pushobject(lua_getglobal(luaL_check_string(1)));
171}
172
173static void rawgetglobal (void)
174{
175 lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
176}
177
178static void luatag (void)
179{
180 lua_pushnumber(lua_tag(lua_getparam(1)));
181}
182
183
184static int getnarg (lua_Object table)
185{
186 lua_Object temp;
187 /* temp = table.n */
188 lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
189 return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
190}
191
192static void luaI_call (void)
193{
194 lua_Object f = lua_getparam(1);
195 lua_Object arg = lua_getparam(2);
196 int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
197 int narg, i;
198 luaL_arg_check(lua_isfunction(f), 1, "function expected");
199 luaL_arg_check(lua_istable(arg), 2, "table expected");
200 narg = getnarg(arg);
201 /* push arg[1...n] */
202 for (i=0; i<narg; i++) {
203 lua_Object temp;
204 /* temp = arg[i+1] */
205 lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
206 if (narg == MAX_WORD && lua_isnil(temp))
207 break;
208 lua_pushobject(temp);
209 }
210 if (lua_callfunction(f))
211 lua_error(NULL);
212 else if (withtable)
213 luaA_packresults();
214 else
215 luaA_passresults();
216}
217
218
219static void settag (void)
220{
221 lua_Object o = lua_getparam(1);
222 luaL_arg_check(lua_istable(o), 1, "table expected");
223 lua_pushobject(o);
224 lua_settag(luaL_check_number(2));
225}
226
227
228static void newtag (void)
229{
230 lua_pushnumber(lua_newtag());
231}
232
233
234static void rawgettable (void)
235{
236 lua_Object t = lua_getparam(1);
237 lua_Object i = lua_getparam(2);
238 luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
239 luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
240 lua_pushobject(t);
241 lua_pushobject(i);
242 lua_pushobject(lua_rawgettable());
243}
244
245
246static void rawsettable (void)
247{
248 lua_Object t = lua_getparam(1);
249 lua_Object i = lua_getparam(2);
250 lua_Object v = lua_getparam(3);
251 luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
252 0, NULL);
253 lua_pushobject(t);
254 lua_pushobject(i);
255 lua_pushobject(v);
256 lua_rawsettable();
257}
258
259
260static void settagmethod (void)
261{
262 lua_Object nf = lua_getparam(3);
263 luaL_arg_check(nf != LUA_NOOBJECT, 3, "value expected");
264 lua_pushobject(nf);
265 lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
266 luaL_check_string(2)));
267}
268
269
270static void gettagmethod (void)
271{
272 lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
273 luaL_check_string(2)));
274}
275
276
277static void seterrormethod (void)
278{
279 lua_Object nf = lua_getparam(1);
280 luaL_arg_check(nf != LUA_NOOBJECT, 1, "value expected");
281 lua_pushobject(nf);
282 lua_pushobject(lua_seterrormethod());
283}
284
285
286static void luaI_collectgarbage (void)
287{
288 lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
289}
290
291
292/*
293** =======================================================
294** some DEBUG functions
295** =======================================================
296*/
297#ifdef DEBUG
298
299static void testC (void)
300{
301#define getnum(s) ((*s++) - '0')
302#define getname(s) (nome[0] = *s++, nome)
303
304 static int locks[10];
305 lua_Object reg[10];
306 char nome[2];
307 char *s = luaL_check_string(1);
308 nome[1] = 0;
309 while (1) {
310 switch (*s++) {
311 case '0': case '1': case '2': case '3': case '4':
312 case '5': case '6': case '7': case '8': case '9':
313 lua_pushnumber(*(s-1) - '0');
314 break;
315
316 case 'c': reg[getnum(s)] = lua_createtable(); break;
317 case 'P': reg[getnum(s)] = lua_pop(); break;
318 case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
319 case 'G': { int n = getnum(s);
320 reg[n] = lua_rawgetglobal(getname(s));
321 break;
322 }
323 case 'l': locks[getnum(s)] = lua_ref(1); break;
324 case 'L': locks[getnum(s)] = lua_ref(0); break;
325 case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
326 case 'u': lua_unref(locks[getnum(s)]); break;
327 case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
328 case '=': lua_setglobal(getname(s)); break;
329 case 's': lua_pushstring(getname(s)); break;
330 case 'o': lua_pushobject(reg[getnum(s)]); break;
331 case 'f': lua_call(getname(s)); break;
332 case 'i': reg[getnum(s)] = lua_gettable(); break;
333 case 'I': reg[getnum(s)] = lua_rawgettable(); break;
334 case 't': lua_settable(); break;
335 case 'T': lua_rawsettable(); break;
336 default: luaL_verror("unknown command in `testC': %c", *(s-1));
337 }
338 if (*s == 0) return;
339 if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
340 }
341}
342
343#endif
344
345
346/*
347** Internal functions
348*/
349static struct luaL_reg int_funcs[] = {
350#if LUA_COMPAT2_5
351 {"setfallback", luaT_setfallback},
352#endif
353#ifdef DEBUG
354 {"testC", testC},
355 {"totalmem", luaM_query},
356#endif
357 {"assert", luaI_assert},
358 {"call", luaI_call},
359 {"collectgarbage", luaI_collectgarbage},
360 {"dofile", internaldofile},
361 {"dostring", internaldostring},
362 {"error", luaI_error},
363 {"getglobal", getglobal},
364 {"newtag", newtag},
365 {"next", next},
366 {"nextvar", nextvar},
367 {"print", luaI_print},
368 {"rawgetglobal", rawgetglobal},
369 {"rawgettable", rawgettable},
370 {"rawsetglobal", rawsetglobal},
371 {"rawsettable", rawsettable},
372 {"seterrormethod", seterrormethod},
373 {"setglobal", setglobal},
374 {"settagmethod", settagmethod},
375 {"gettagmethod", gettagmethod},
376 {"settag", settag},
377 {"tonumber", lua_obj2number},
378 {"tostring", bi_tostring},
379 {"tag", luatag},
380 {"type", luaI_type}
381};
382
383
384#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
385
386
387void luaB_predefine (void)
388{
389 int i;
390 Word n;
391 /* pre-register mem error messages, to avoid loop when error arises */
392 luaS_newfixedstring(tableEM);
393 luaS_newfixedstring(memEM);
394 for (i=0; i<INTFUNCSIZE; i++) {
395 n = luaG_findsymbolbyname(int_funcs[i].name);
396 s_ttype(n) = LUA_T_CFUNCTION;
397 fvalue(&s_object(n)) = int_funcs[i].func;
398 }
399 n = luaG_findsymbolbyname("_VERSION");
400 s_ttype(n) = LUA_T_STRING;
401 tsvalue(&s_object(n)) = luaS_new(LUA_VERSION);
402}
403
404