aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-14 16:33:29 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-12-14 16:33:29 -0200
commit1b15206cf9aa7005fc3d48f78f60f66838f10eb5 (patch)
tree00a96c96d331417ba781b605c6c10d8daae01938 /lapi.c
parente6d56cd2d844174bd40af4c44c0f68e2115e5876 (diff)
downloadlua-1b15206cf9aa7005fc3d48f78f60f66838f10eb5.tar.gz
lua-1b15206cf9aa7005fc3d48f78f60f66838f10eb5.tar.bz2
lua-1b15206cf9aa7005fc3d48f78f60f66838f10eb5.zip
many details + code redistribution
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c245
1 files changed, 29 insertions, 216 deletions
diff --git a/lapi.c b/lapi.c
index 953c066e..caeb6318 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.62 1999/12/02 16:24:45 roberto Exp roberto $ 2** $Id: lapi.c,v 1.63 1999/12/06 12:03:45 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,16 +22,15 @@
22#include "ltable.h" 22#include "ltable.h"
23#include "ltm.h" 23#include "ltm.h"
24#include "lua.h" 24#include "lua.h"
25#include "luadebug.h"
26#include "lvm.h" 25#include "lvm.h"
27 26
28 27
29const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" 28const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
30 "$Authors: " LUA_AUTHORS " $"; 29 "$Authors: " LUA_AUTHORS " $";
31 30
32 31
33 32
34static lua_Type normalized_type (const TObject *o) { 33lua_Type luaA_normalizedtype (const TObject *o) {
35 int t = ttype(o); 34 int t = ttype(o);
36 switch (t) { 35 switch (t) {
37 case LUA_T_PMARK: 36 case LUA_T_PMARK:
@@ -46,24 +45,24 @@ static lua_Type normalized_type (const TObject *o) {
46} 45}
47 46
48 47
49static void set_normalized (TObject *d, const TObject *s) { 48void luaA_setnormalized (TObject *d, const TObject *s) {
50 d->value = s->value; 49 d->value = s->value;
51 d->ttype = normalized_type(s); 50 d->ttype = luaA_normalizedtype(s);
52} 51}
53 52
54 53
55static const TObject *luaA_protovalue (const TObject *o) { 54const TObject *luaA_protovalue (const TObject *o) {
56 return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o; 55 return (luaA_normalizedtype(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
57} 56}
58 57
59 58
60static void checkCparams (lua_State *L, int nParams) { 59void luaA_checkCparams (lua_State *L, int nParams) {
61 if (nParams > L->top-L->Cstack.base) 60 if (nParams > L->top-L->Cstack.base)
62 lua_error(L, "API error - wrong number of arguments in C2lua stack"); 61 lua_error(L, "API error - wrong number of arguments in C2lua stack");
63} 62}
64 63
65 64
66static lua_Object put_luaObject (lua_State *L, const TObject *o) { 65lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
67 luaD_openstack(L, L->Cstack.base); 66 luaD_openstack(L, L->Cstack.base);
68 *L->Cstack.base++ = *o; 67 *L->Cstack.base++ = *o;
69 return L->Cstack.base-1; 68 return L->Cstack.base-1;
@@ -86,7 +85,7 @@ static void top2LC (lua_State *L, int n) {
86 85
87 86
88lua_Object lua_pop (lua_State *L) { 87lua_Object lua_pop (lua_State *L) {
89 checkCparams(L, 1); 88 luaA_checkCparams(L, 1);
90 return luaA_putObjectOnTop(L); 89 return luaA_putObjectOnTop(L);
91} 90}
92 91
@@ -106,19 +105,19 @@ int lua_callfunction (lua_State *L, lua_Object function) {
106 return 1; 105 return 1;
107 else { 106 else {
108 luaD_openstack(L, L->Cstack.base); 107 luaD_openstack(L, L->Cstack.base);
109 set_normalized(L->Cstack.base, function); 108 luaA_setnormalized(L->Cstack.base, function);
110 return luaD_protectedrun(L); 109 return luaD_protectedrun(L);
111 } 110 }
112} 111}
113 112
114 113
115lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) { 114lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
116 return put_luaObject(L, luaT_gettagmethod(L, tag, event)); 115 return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
117} 116}
118 117
119 118
120lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) { 119lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
121 checkCparams(L, 1); 120 luaA_checkCparams(L, 1);
122 luaT_settagmethod(L, tag, event, L->top-1); 121 luaT_settagmethod(L, tag, event, L->top-1);
123 return luaA_putObjectOnTop(L); 122 return luaA_putObjectOnTop(L);
124} 123}
@@ -126,7 +125,7 @@ lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
126 125
127lua_Object lua_seterrormethod (lua_State *L) { 126lua_Object lua_seterrormethod (lua_State *L) {
128 lua_Object temp; 127 lua_Object temp;
129 checkCparams(L, 1); 128 luaA_checkCparams(L, 1);
130 temp = lua_getglobal(L, "_ERRORMESSAGE"); 129 temp = lua_getglobal(L, "_ERRORMESSAGE");
131 lua_setglobal(L, "_ERRORMESSAGE"); 130 lua_setglobal(L, "_ERRORMESSAGE");
132 return temp; 131 return temp;
@@ -134,14 +133,14 @@ lua_Object lua_seterrormethod (lua_State *L) {
134 133
135 134
136lua_Object lua_gettable (lua_State *L) { 135lua_Object lua_gettable (lua_State *L) {
137 checkCparams(L, 2); 136 luaA_checkCparams(L, 2);
138 luaV_gettable(L); 137 luaV_gettable(L);
139 return luaA_putObjectOnTop(L); 138 return luaA_putObjectOnTop(L);
140} 139}
141 140
142 141
143lua_Object lua_rawgettable (lua_State *L) { 142lua_Object lua_rawgettable (lua_State *L) {
144 checkCparams(L, 2); 143 luaA_checkCparams(L, 2);
145 if (ttype(L->top-2) != LUA_T_ARRAY) 144 if (ttype(L->top-2) != LUA_T_ARRAY)
146 lua_error(L, "indexed expression not a table in rawgettable"); 145 lua_error(L, "indexed expression not a table in rawgettable");
147 *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1); 146 *(L->top-2) = *luaH_get(L, avalue(L->top-2), L->top-1);
@@ -151,14 +150,14 @@ lua_Object lua_rawgettable (lua_State *L) {
151 150
152 151
153void lua_settable (lua_State *L) { 152void lua_settable (lua_State *L) {
154 checkCparams(L, 3); 153 luaA_checkCparams(L, 3);
155 luaV_settable(L, L->top-3); 154 luaV_settable(L, L->top-3);
156 L->top -= 2; /* pop table and index */ 155 L->top -= 2; /* pop table and index */
157} 156}
158 157
159 158
160void lua_rawsettable (lua_State *L) { 159void lua_rawsettable (lua_State *L) {
161 checkCparams(L, 3); 160 luaA_checkCparams(L, 3);
162 luaV_rawsettable(L, L->top-3); 161 luaV_rawsettable(L, L->top-3);
163} 162}
164 163
@@ -168,12 +167,12 @@ lua_Object lua_createtable (lua_State *L) {
168 luaC_checkGC(L); 167 luaC_checkGC(L);
169 avalue(&o) = luaH_new(L, 0); 168 avalue(&o) = luaH_new(L, 0);
170 ttype(&o) = LUA_T_ARRAY; 169 ttype(&o) = LUA_T_ARRAY;
171 return put_luaObject(L, &o); 170 return luaA_putluaObject(L, &o);
172} 171}
173 172
174 173
175lua_Object lua_getglobal (lua_State *L, const char *name) { 174lua_Object lua_getglobal (lua_State *L, const char *name) {
176 luaD_checkstack(L, 2); /* may need that to call T.M. */ 175 luaD_checkstack(L, 2); /* may need that to call a tag method */
177 luaV_getglobal(L, luaS_assertglobalbyname(L, name)); 176 luaV_getglobal(L, luaS_assertglobalbyname(L, name));
178 return luaA_putObjectOnTop(L); 177 return luaA_putObjectOnTop(L);
179} 178}
@@ -181,27 +180,27 @@ lua_Object lua_getglobal (lua_State *L, const char *name) {
181 180
182lua_Object lua_rawgetglobal (lua_State *L, const char *name) { 181lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
183 GlobalVar *gv = luaS_assertglobalbyname(L, name); 182 GlobalVar *gv = luaS_assertglobalbyname(L, name);
184 return put_luaObject(L, &gv->value); 183 return luaA_putluaObject(L, &gv->value);
185} 184}
186 185
187 186
188void lua_setglobal (lua_State *L, const char *name) { 187void lua_setglobal (lua_State *L, const char *name) {
189 checkCparams(L, 1); 188 luaA_checkCparams(L, 1);
190 luaD_checkstack(L, 2); /* may need that to call T.M. */ 189 luaD_checkstack(L, 2); /* may need that to call a tag method */
191 luaV_setglobal(L, luaS_assertglobalbyname(L, name)); 190 luaV_setglobal(L, luaS_assertglobalbyname(L, name));
192} 191}
193 192
194 193
195void lua_rawsetglobal (lua_State *L, const char *name) { 194void lua_rawsetglobal (lua_State *L, const char *name) {
196 GlobalVar *gv = luaS_assertglobalbyname(L, name); 195 GlobalVar *gv = luaS_assertglobalbyname(L, name);
197 checkCparams(L, 1); 196 luaA_checkCparams(L, 1);
198 gv->value = *(--L->top); 197 gv->value = *(--L->top);
199} 198}
200 199
201 200
202const char *lua_type (lua_State *L, lua_Object o) { 201const char *lua_type (lua_State *L, lua_Object o) {
203 UNUSED(L); 202 UNUSED(L);
204 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(L, o); 203 return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
205} 204}
206 205
207int lua_isnil (lua_State *L, lua_Object o) { 206int lua_isnil (lua_State *L, lua_Object o) {
@@ -220,7 +219,6 @@ int lua_isuserdata (lua_State *L, lua_Object o) {
220} 219}
221 220
222int lua_iscfunction (lua_State *L, lua_Object o) { 221int lua_iscfunction (lua_State *L, lua_Object o) {
223 UNUSED(L);
224 return (lua_tag(L, o) == LUA_T_CPROTO); 222 return (lua_tag(L, o) == LUA_T_CPROTO);
225} 223}
226 224
@@ -261,7 +259,6 @@ const char *lua_getstring (lua_State *L, lua_Object obj) {
261} 259}
262 260
263long lua_strlen (lua_State *L, lua_Object obj) { 261long lua_strlen (lua_State *L, lua_Object obj) {
264 UNUSED(L);
265 if (obj == LUA_NOOBJECT || tostring(L, obj)) 262 if (obj == LUA_NOOBJECT || tostring(L, obj))
266 return 0L; 263 return 0L;
267 else return (tsvalue(obj)->u.s.len); 264 else return (tsvalue(obj)->u.s.len);
@@ -309,7 +306,7 @@ void lua_pushstring (lua_State *L, const char *s) {
309void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { 306void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
310 if (fn == NULL) 307 if (fn == NULL)
311 lua_error(L, "API error - attempt to push a NULL Cfunction"); 308 lua_error(L, "API error - attempt to push a NULL Cfunction");
312 checkCparams(L, n); 309 luaA_checkCparams(L, n);
313 ttype(L->top) = LUA_T_CPROTO; 310 ttype(L->top) = LUA_T_CPROTO;
314 fvalue(L->top) = fn; 311 fvalue(L->top) = fn;
315 incr_top; 312 incr_top;
@@ -334,7 +331,7 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
334void lua_pushobject (lua_State *L, lua_Object o) { 331void lua_pushobject (lua_State *L, lua_Object o) {
335 if (o == LUA_NOOBJECT) 332 if (o == LUA_NOOBJECT)
336 lua_error(L, "API error - attempt to push a NOOBJECT"); 333 lua_error(L, "API error - attempt to push a NOOBJECT");
337 set_normalized(L->top, o); 334 luaA_setnormalized(L->top, o);
338 incr_top; 335 incr_top;
339} 336}
340 337
@@ -368,7 +365,7 @@ int lua_tag (lua_State *L, lua_Object o) {
368 365
369 366
370void lua_settag (lua_State *L, int tag) { 367void lua_settag (lua_State *L, int tag) {
371 checkCparams(L, 1); 368 luaA_checkCparams(L, 1);
372 luaT_realtag(L, tag); 369 luaT_realtag(L, tag);
373 switch (ttype(L->top-1)) { 370 switch (ttype(L->top-1)) {
374 case LUA_T_ARRAY: 371 case LUA_T_ARRAY:
@@ -379,7 +376,7 @@ void lua_settag (lua_State *L, int tag) {
379 break; 376 break;
380 default: 377 default:
381 luaL_verror(L, "cannot change the tag of a %.20s", 378 luaL_verror(L, "cannot change the tag of a %.20s",
382 luaO_typename(L, L->top-1)); 379 luaO_typename(L->top-1));
383 } 380 }
384 L->top--; 381 L->top--;
385} 382}
@@ -442,187 +439,3 @@ int lua_next (lua_State *L, lua_Object t, int i) {
442} 439}
443 440
444 441
445
446/*
447** {======================================================
448** To manipulate some state information
449** =======================================================
450*/
451
452
453
454lua_LHFunction lua_setlinehook (lua_State *L, lua_LHFunction func) {
455 lua_LHFunction old = L->linehook;
456 L->linehook = func;
457 return old;
458}
459
460lua_CHFunction lua_setcallhook (lua_State *L, lua_CHFunction func) {
461 lua_CHFunction old = L->callhook;
462 L->callhook = func;
463 return old;
464}
465
466int lua_setdebug (lua_State *L, int debug) {
467 int old = L->debug;
468 L->debug = debug;
469 return old;
470}
471
472/* }====================================================== */
473
474
475/*
476** {======================================================
477** Debug interface
478** =======================================================
479*/
480
481
482lua_Function lua_stackedfunction (lua_State *L, int level) {
483 int i;
484 for (i = (L->top-1)-L->stack; i>=0; i--) {
485 int t = L->stack[i].ttype;
486 if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
487 if (level-- == 0)
488 return L->stack+i;
489 }
490 return LUA_NOOBJECT;
491}
492
493
494int lua_nups (lua_State *L, lua_Function f) {
495 UNUSED(L);
496 return (!f || normalized_type(f) != LUA_T_CLOSURE) ? 0 : f->value.cl->nelems;
497}
498
499
500int lua_currentline (lua_State *L, lua_Function f) {
501 return (f+1 < L->top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
502}
503
504
505lua_Object lua_getlocal (lua_State *L, lua_Function f, int local_number,
506 const char **name) {
507 /* check whether `f' is a Lua function */
508 if (lua_tag(L, f) != LUA_T_PROTO)
509 return LUA_NOOBJECT;
510 else {
511 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
512 *name = luaF_getlocalname(fp, local_number, lua_currentline(L, f));
513 if (*name) {
514 /* if "*name", there must be a LUA_T_LINE */
515 /* therefore, f+2 points to function base */
516 return put_luaObject(L, (f+2)+(local_number-1));
517 }
518 else
519 return LUA_NOOBJECT;
520 }
521}
522
523
524int lua_setlocal (lua_State *L, lua_Function f, int local_number) {
525 /* check whether `f' is a Lua function */
526 if (lua_tag(L, f) != LUA_T_PROTO)
527 return 0;
528 else {
529 TProtoFunc *fp = luaA_protovalue(f)->value.tf;
530 const char *name = luaF_getlocalname(fp, local_number,
531 lua_currentline(L, f));
532 checkCparams(L, 1);
533 --L->top;
534 if (name) {
535 /* if "name", there must be a LUA_T_LINE */
536 /* therefore, f+2 points to function base */
537 *((f+2)+(local_number-1)) = *L->top;
538 return 1;
539 }
540 else
541 return 0;
542 }
543}
544
545
546void lua_funcinfo (lua_State *L, lua_Object func,
547 const char **source, int *linedefined) {
548 if (!lua_isfunction(L, func))
549 lua_error(L, "API error - `funcinfo' called with a non-function value");
550 else {
551 const TObject *f = luaA_protovalue(func);
552 if (normalized_type(f) == LUA_T_PROTO) {
553 *source = tfvalue(f)->source->str;
554 *linedefined = tfvalue(f)->lineDefined;
555 }
556 else {
557 *source = "(C)";
558 *linedefined = -1;
559 }
560 }
561}
562
563
564static int checkfunc (lua_State *L, TObject *o) {
565 return luaO_equalObj(o, L->top);
566}
567
568
569const char *lua_getobjname (lua_State *L, lua_Object o, const char **name) {
570 /* try to find a name for given function */
571 GlobalVar *g;
572 set_normalized(L->top, o); /* to be used by `checkfunc' */
573 for (g=L->rootglobal; g; g=g->next) {
574 if (checkfunc(L, &g->value)) {
575 *name = g->name->str;
576 return "global";
577 }
578 }
579 /* not found: try tag methods */
580 if ((*name = luaT_travtagmethods(L, checkfunc)) != NULL)
581 return "tag-method";
582 else return ""; /* not found at all */
583}
584
585/* }====================================================== */
586
587
588/*
589** {======================================================
590** BLOCK mechanism
591** =======================================================
592*/
593
594
595void lua_beginblock (lua_State *L) {
596 luaM_growvector(L, L->Cblocks, L->numCblocks, 1, struct C_Lua_Stack,
597 "too many nested blocks", L->stacksize);
598 L->Cblocks[L->numCblocks] = L->Cstack;
599 L->numCblocks++;
600}
601
602void lua_endblock (lua_State *L) {
603 if (L->numCblocks <= 0)
604 lua_error(L, "API error - no block to end");
605 --L->numCblocks;
606 L->Cstack = L->Cblocks[L->numCblocks];
607 L->top = L->Cstack.base;
608}
609
610
611
612int lua_ref (lua_State *L, int lock) {
613 int ref;
614 checkCparams(L, 1);
615 ref = luaR_ref(L, L->top-1, lock);
616 L->top--;
617 return ref;
618}
619
620
621
622lua_Object lua_getref (lua_State *L, int ref) {
623 const TObject *o = luaR_getref(L, ref);
624 return (o ? put_luaObject(L, o) : LUA_NOOBJECT);
625}
626
627/* }====================================================== */
628