aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
commitea169d20835aa7d2a42641decc5b5d802047afca (patch)
tree130c69567896b6b5b61f49c2ff0c053ad5602551
parentc31aa863ac29fad5bb3f44c4e08640f877b65f25 (diff)
downloadlua-ea169d20835aa7d2a42641decc5b5d802047afca.tar.gz
lua-ea169d20835aa7d2a42641decc5b5d802047afca.tar.bz2
lua-ea169d20835aa7d2a42641decc5b5d802047afca.zip
auxiliar functions from Lua API
-rw-r--r--lapi.c564
-rw-r--r--lapi.h20
2 files changed, 584 insertions, 0 deletions
diff --git a/lapi.c b/lapi.c
new file mode 100644
index 00000000..7a7e13e7
--- /dev/null
+++ b/lapi.c
@@ -0,0 +1,564 @@
1/*
2** $Id: lapi.c,v 1.1 1997/08/14 13:40:46 roberto Exp roberto $
3** Lua API
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9#include <string.h>
10
11#include "lapi.h"
12#include "lauxlib.h"
13#include "ldo.h"
14#include "lfunc.h"
15#include "lgc.h"
16#include "lglobal.h"
17#include "lmem.h"
18#include "lobject.h"
19#include "lstring.h"
20#include "ltable.h"
21#include "ltm.h"
22#include "lua.h"
23#include "luadebug.h"
24#include "lvm.h"
25
26
27char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
28 "$Autores: " LUA_AUTHORS " $";
29
30
31
32TObject *luaA_Address (lua_Object o)
33{
34 return Address(o);
35}
36
37
38void luaA_packresults (void)
39{
40 luaV_pack(luaD_Cstack.lua2C, luaD_Cstack.num, luaD_stack.top);
41 incr_top;
42}
43
44
45int luaA_passresults (void)
46{
47 luaD_checkstack(luaD_Cstack.num);
48 memcpy(luaD_stack.top, luaD_Cstack.lua2C+luaD_stack.stack,
49 luaD_Cstack.num*sizeof(TObject));
50 luaD_stack.top += luaD_Cstack.num;
51 return luaD_Cstack.num;
52}
53
54
55static void checkCparams (int nParams)
56{
57 if (luaD_stack.top-luaD_stack.stack < luaD_Cstack.base+nParams)
58 lua_error("API error - wrong number of arguments in C2lua stack");
59}
60
61
62static lua_Object put_luaObject (TObject *o)
63{
64 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
65 luaD_stack.stack[luaD_Cstack.base++] = *o;
66 return luaD_Cstack.base; /* this is +1 real position (see Ref) */
67}
68
69
70static lua_Object put_luaObjectonTop (void)
71{
72 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
73 luaD_stack.stack[luaD_Cstack.base++] = *(--luaD_stack.top);
74 return luaD_Cstack.base; /* this is +1 real position (see Ref) */
75}
76
77
78lua_Object lua_pop (void)
79{
80 checkCparams(1);
81 return put_luaObjectonTop();
82}
83
84
85/*
86** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
87** 'number' must be 1 to get the first parameter.
88*/
89lua_Object lua_lua2C (int number)
90{
91 if (number <= 0 || number > luaD_Cstack.num) return LUA_NOOBJECT;
92 /* Ref(luaD_stack.stack+(luaD_Cstack.lua2C+number-1)) ==
93 luaD_stack.stack+(luaD_Cstack.lua2C+number-1)-luaD_stack.stack+1 == */
94 return luaD_Cstack.lua2C+number;
95}
96
97
98int lua_callfunction (lua_Object function)
99{
100 if (function == LUA_NOOBJECT)
101 return 1;
102 else {
103 luaD_openstack((luaD_stack.top-luaD_stack.stack)-luaD_Cstack.base);
104 luaD_stack.stack[luaD_Cstack.base] = *Address(function);
105 return luaD_protectedrun(MULT_RET);
106 }
107}
108
109
110lua_Object lua_gettagmethod (int tag, char *event)
111{
112 return put_luaObject(luaT_gettagmethod(tag, event));
113}
114
115
116lua_Object lua_settagmethod (int tag, char *event)
117{
118 checkCparams(1);
119 luaT_settagmethod(tag, event, luaD_stack.top-1);
120 return put_luaObjectonTop();
121}
122
123
124lua_Object lua_seterrormethod (void)
125{
126 TObject temp = luaD_errorim;
127 checkCparams(1);
128 luaD_errorim = *(--luaD_stack.top);
129 return put_luaObject(&temp);
130}
131
132
133lua_Object lua_gettable (void)
134{
135 checkCparams(2);
136 luaV_gettable();
137 return put_luaObjectonTop();
138}
139
140
141lua_Object lua_rawgettable (void)
142{
143 checkCparams(2);
144 if (ttype(luaD_stack.top-2) != LUA_T_ARRAY)
145 lua_error("indexed expression not a table in raw gettable");
146 else {
147 TObject *h = luaH_get(avalue(luaD_stack.top-2), luaD_stack.top-1);
148 --luaD_stack.top;
149 if (h != NULL)
150 *(luaD_stack.top-1) = *h;
151 else
152 ttype(luaD_stack.top-1) = LUA_T_NIL;
153 }
154 return put_luaObjectonTop();
155}
156
157
158void lua_settable (void)
159{
160 checkCparams(3);
161 luaV_settable(luaD_stack.top-3, 1);
162}
163
164
165void lua_rawsettable (void)
166{
167 checkCparams(3);
168 luaV_settable(luaD_stack.top-3, 0);
169}
170
171
172lua_Object lua_createtable (void)
173{
174 TObject o;
175 luaC_checkGC();
176 avalue(&o) = luaH_new(0);
177 ttype(&o) = LUA_T_ARRAY;
178 return put_luaObject(&o);
179}
180
181
182lua_Object lua_getglobal (char *name)
183{
184 luaD_checkstack(2); /* may need that to call T.M. */
185 luaV_getglobal(luaG_findsymbolbyname(name));
186 return put_luaObjectonTop();
187}
188
189
190lua_Object lua_rawgetglobal (char *name)
191{
192 return put_luaObject(&luaG_global[luaG_findsymbolbyname(name)].object);
193}
194
195
196void lua_setglobal (char *name)
197{
198 checkCparams(1);
199 luaD_checkstack(2); /* may need that to call T.M. */
200 luaV_setglobal(luaG_findsymbolbyname(name));
201}
202
203
204void lua_rawsetglobal (char *name)
205{
206 Word n = luaG_findsymbolbyname(name);
207 checkCparams(1);
208 s_object(n) = *(--luaD_stack.top);
209}
210
211
212
213int lua_isnil (lua_Object o)
214{
215 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
216}
217
218int lua_istable (lua_Object o)
219{
220 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
221}
222
223int lua_isuserdata (lua_Object o)
224{
225 return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
226}
227
228int lua_iscfunction (lua_Object o)
229{
230 int t = lua_tag(o);
231 return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
232}
233
234int lua_isnumber (lua_Object o)
235{
236 return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
237}
238
239int lua_isstring (lua_Object o)
240{
241 int t = lua_tag(o);
242 return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
243}
244
245int lua_isfunction (lua_Object o)
246{
247 int t = lua_tag(o);
248 return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
249 (t == LUA_T_MARK) || (t == LUA_T_CMARK);
250}
251
252
253real lua_getnumber (lua_Object object)
254{
255 if (object == LUA_NOOBJECT) return 0.0;
256 if (tonumber (Address(object))) return 0.0;
257 else return (nvalue(Address(object)));
258}
259
260char *lua_getstring (lua_Object object)
261{
262 if (object == LUA_NOOBJECT || tostring(Address(object)))
263 return NULL;
264 else return (svalue(Address(object)));
265}
266
267void *lua_getuserdata (lua_Object object)
268{
269 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
270 return NULL;
271 else return tsvalue(Address(object))->u.v;
272}
273
274lua_CFunction lua_getcfunction (lua_Object object)
275{
276 if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
277 (ttype(Address(object)) != LUA_T_CMARK)))
278 return NULL;
279 else return (fvalue(Address(object)));
280}
281
282
283void lua_pushnil (void)
284{
285 ttype(luaD_stack.top) = LUA_T_NIL;
286 incr_top;
287}
288
289void lua_pushnumber (real n)
290{
291 ttype(luaD_stack.top) = LUA_T_NUMBER;
292 nvalue(luaD_stack.top) = n;
293 incr_top;
294}
295
296void lua_pushstring (char *s)
297{
298 if (s == NULL)
299 ttype(luaD_stack.top) = LUA_T_NIL;
300 else {
301 tsvalue(luaD_stack.top) = luaS_new(s);
302 ttype(luaD_stack.top) = LUA_T_STRING;
303 }
304 incr_top;
305 luaC_checkGC();
306}
307
308void lua_pushcfunction (lua_CFunction fn)
309{
310 if (fn == NULL)
311 ttype(luaD_stack.top) = LUA_T_NIL;
312 else {
313 ttype(luaD_stack.top) = LUA_T_CFUNCTION;
314 fvalue(luaD_stack.top) = fn;
315 }
316 incr_top;
317}
318
319void lua_pushusertag (void *u, int tag)
320{
321 if (tag < 0 && tag != LUA_ANYTAG)
322 luaT_realtag(tag); /* error if tag is not valid */
323 tsvalue(luaD_stack.top) = luaS_createudata(u, tag);
324 ttype(luaD_stack.top) = LUA_T_USERDATA;
325 incr_top;
326 luaC_checkGC();
327}
328
329void luaA_pushobject (TObject *o)
330{
331 *luaD_stack.top = *o;
332 incr_top;
333}
334
335void lua_pushobject (lua_Object o)
336{
337 if (o == LUA_NOOBJECT)
338 lua_error("API error - attempt to push a NOOBJECT");
339 *luaD_stack.top = *Address(o);
340 if (ttype(luaD_stack.top) == LUA_T_MARK)
341 ttype(luaD_stack.top) = LUA_T_FUNCTION;
342 else if (ttype(luaD_stack.top) == LUA_T_CMARK)
343 ttype(luaD_stack.top) = LUA_T_CFUNCTION;
344 incr_top;
345}
346
347
348int lua_tag (lua_Object lo)
349{
350 if (lo == LUA_NOOBJECT) return LUA_T_NIL;
351 else {
352 TObject *o = Address(lo);
353 lua_Type t = ttype(o);
354 if (t == LUA_T_USERDATA)
355 return o->value.ts->tag;
356 else if (t == LUA_T_ARRAY)
357 return o->value.a->htag;
358 else return t;
359 }
360}
361
362
363void lua_settag (int tag)
364{
365 checkCparams(1);
366 luaT_realtag(tag);
367 switch (ttype(luaD_stack.top-1)) {
368 case LUA_T_ARRAY:
369 (luaD_stack.top-1)->value.a->htag = tag;
370 break;
371 case LUA_T_USERDATA:
372 (luaD_stack.top-1)->value.ts->tag = tag;
373 break;
374 default:
375 luaL_verror("cannot change the tag of a %s",
376 luaO_typenames[-ttype((luaD_stack.top-1))]);
377 }
378 luaD_stack.top--;
379}
380
381
382/*
383** =======================================================
384** Debug interface
385** =======================================================
386*/
387
388
389/* Hooks */
390lua_CHFunction lua_callhook = NULL;
391lua_LHFunction lua_linehook = NULL;
392
393
394lua_Function lua_stackedfunction (int level)
395{
396 StkId i;
397 for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--)
398 if (luaD_stack.stack[i].ttype == LUA_T_MARK || luaD_stack.stack[i].ttype == LUA_T_CMARK)
399 if (level-- == 0)
400 return Ref(luaD_stack.stack+i);
401 return LUA_NOOBJECT;
402}
403
404
405int lua_currentline (lua_Function func)
406{
407 TObject *f = Address(func);
408 return (f+1 < luaD_stack.top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
409}
410
411
412lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
413{
414 TObject *f = luaA_Address(func);
415 /* check whether func is a Lua function */
416 if (ttype(f) != LUA_T_MARK && ttype(f) != LUA_T_FUNCTION)
417 return LUA_NOOBJECT;
418 *name = luaF_getlocalname(f->value.tf, local_number, lua_currentline(func));
419 if (*name) {
420 /* if "*name", there must be a LUA_T_LINE */
421 /* therefore, f+2 points to function base */
422 return Ref((f+2)+(local_number-1));
423 }
424 else
425 return LUA_NOOBJECT;
426}
427
428
429int lua_setlocal (lua_Function func, int local_number)
430{
431 TObject *f = Address(func);
432 char *name = luaF_getlocalname(f->value.tf, local_number,
433 lua_currentline(func));
434 checkCparams(1);
435 --luaD_stack.top;
436 if (name) {
437 /* if "name", there must be a LUA_T_LINE */
438 /* therefore, f+2 points to function base */
439 *((f+2)+(local_number-1)) = *luaD_stack.top;
440 return 1;
441 }
442 else
443 return 0;
444}
445
446
447void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
448{
449 TObject *f = Address(func);
450 if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
451 {
452 TProtoFunc *fp = f->value.cl->consts[0].value.tf;
453 *filename = fp->fileName->str;
454 *linedefined = fp->lineDefined;
455 }
456 else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
457 {
458 *filename = "(C)";
459 *linedefined = -1;
460 }
461}
462
463
464static TObject *functofind;
465static int checkfunc (TObject *o)
466{
467 if (o->ttype == LUA_T_FUNCTION)
468 return
469 ((functofind->ttype == LUA_T_FUNCTION ||
470 functofind->ttype == LUA_T_MARK) &&
471 (functofind->value.cl == o->value.cl));
472 else if (o->ttype == LUA_T_CFUNCTION)
473 return
474 ((functofind->ttype == LUA_T_CFUNCTION ||
475 functofind->ttype == LUA_T_CMARK) &&
476 (functofind->value.f == o->value.f));
477 else return 0;
478}
479
480
481char *lua_getobjname (lua_Object o, char **name)
482{ /* try to find a name for given function */
483 functofind = Address(o);
484 if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
485 return "tag-method";
486 else if ((*name = luaG_travsymbol(checkfunc)) != NULL)
487 return "global";
488 else return "";
489}
490
491/*
492** =======================================================
493** BLOCK mechanism
494** =======================================================
495*/
496
497#define MAX_C_BLOCKS 10
498
499static int numCblocks = 0;
500static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
501
502void lua_beginblock (void)
503{
504 if (numCblocks >= MAX_C_BLOCKS)
505 lua_error("`lua_beginblock': too many nested blocks");
506 Cblocks[numCblocks] = luaD_Cstack;
507 numCblocks++;
508}
509
510void lua_endblock (void)
511{
512 --numCblocks;
513 luaD_Cstack = Cblocks[numCblocks];
514 luaD_adjusttop(luaD_Cstack.base);
515}
516
517
518
519
520
521int lua_ref (int lock)
522{
523 int ref;
524 checkCparams(1);
525 ref = luaC_ref(luaD_stack.top-1, lock);
526 luaD_stack.top--;
527 return ref;
528}
529
530
531
532lua_Object lua_getref (int ref)
533{
534 TObject *o = luaC_getref(ref);
535 return (o ? put_luaObject(o) : LUA_NOOBJECT);
536}
537
538
539
540
541
542#if LUA_COMPAT2_5
543/*
544** API: set a function as a fallback
545*/
546
547static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
548{
549 StkId base = (luaD_stack.top-luaD_stack.stack)-nParams;
550 luaD_openstack(nParams);
551 luaD_stack.stack[base].ttype = LUA_T_CFUNCTION;
552 luaD_stack.stack[base].value.f = f;
553 luaD_call(base+1, nResults);
554}
555
556lua_Object lua_setfallback (char *name, lua_CFunction fallback)
557{
558 lua_pushstring(name);
559 lua_pushcfunction(fallback);
560 do_unprotectedrun(luaT_setfallback, 2, 1);
561 return put_luaObjectonTop();
562}
563#endif
564
diff --git a/lapi.h b/lapi.h
new file mode 100644
index 00000000..b21f69af
--- /dev/null
+++ b/lapi.h
@@ -0,0 +1,20 @@
1/*
2** $Id: $
3** auxiliar functions from Lua API
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lapi_h
8#define lapi_h
9
10
11#include "lua.h"
12#include "lobject.h"
13
14
15TObject *luaA_Address (lua_Object o);
16void luaA_pushobject (TObject *o);
17void luaA_packresults (void);
18int luaA_passresults (void);
19
20#endif