aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c115
1 files changed, 62 insertions, 53 deletions
diff --git a/lapi.c b/lapi.c
index d67ecd45..c3c4af08 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lapi.c,v 1.2 1997/09/26 15:02:26 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*/
@@ -94,6 +94,18 @@ lua_Object lua_lua2C (int number)
94} 94}
95 95
96 96
97lua_Object lua_upvalue (int n)
98{
99 TObject *f = luaD_stack.stack+luaD_Cstack.lua2C-1;
100 if (ttype(f) != LUA_T_MARK || n <= 0 || n > clvalue(f)->nelems)
101 return LUA_NOOBJECT;
102if (ttype(protovalue(f)) != LUA_T_CPROTO) lua_error("BAD!!");
103 *luaD_stack.top = clvalue(f)->consts[n];
104 incr_top;
105 return put_luaObjectonTop();
106}
107
108
97int lua_callfunction (lua_Object function) 109int lua_callfunction (lua_Object function)
98{ 110{
99 if (function == LUA_NOOBJECT) 111 if (function == LUA_NOOBJECT)
@@ -227,8 +239,7 @@ int lua_isuserdata (lua_Object o)
227 239
228int lua_iscfunction (lua_Object o) 240int lua_iscfunction (lua_Object o)
229{ 241{
230 int t = lua_tag(o); 242 return (o != LUA_NOOBJECT) && (lua_tag(o) == LUA_T_CPROTO);
231 return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
232} 243}
233 244
234int lua_isnumber (lua_Object o) 245int lua_isnumber (lua_Object o)
@@ -244,9 +255,8 @@ int lua_isstring (lua_Object o)
244 255
245int lua_isfunction (lua_Object o) 256int lua_isfunction (lua_Object o)
246{ 257{
247 int t = lua_tag(o); 258 return (o != LUA_NOOBJECT) && ((ttype(Address(o)) == LUA_T_FUNCTION) ||
248 return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) || 259 (ttype(Address(o)) == LUA_T_MARK));
249 (t == LUA_T_MARK) || (t == LUA_T_CMARK);
250} 260}
251 261
252 262
@@ -273,10 +283,9 @@ void *lua_getuserdata (lua_Object object)
273 283
274lua_CFunction lua_getcfunction (lua_Object object) 284lua_CFunction lua_getcfunction (lua_Object object)
275{ 285{
276 if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) && 286 if (!lua_iscfunction(object))
277 (ttype(Address(object)) != LUA_T_CMARK))) 287 return NULL;
278 return NULL; 288 else return fvalue(protovalue(Address(object)));
279 else return (fvalue(Address(object)));
280} 289}
281 290
282 291
@@ -305,15 +314,19 @@ void lua_pushstring (char *s)
305 luaC_checkGC(); 314 luaC_checkGC();
306} 315}
307 316
308void lua_pushcfunction (lua_CFunction fn) 317void lua_pushCclosure (lua_CFunction fn, int n)
309{ 318{
310 if (fn == NULL) 319 if (fn == NULL) {
311 ttype(luaD_stack.top) = LUA_T_NIL; 320 ttype(luaD_stack.top) = LUA_T_NIL;
321 incr_top;
322 }
312 else { 323 else {
313 ttype(luaD_stack.top) = LUA_T_CFUNCTION; 324 checkCparams(n);
325 ttype(luaD_stack.top) = LUA_T_CPROTO;
314 fvalue(luaD_stack.top) = fn; 326 fvalue(luaD_stack.top) = fn;
327 incr_top;
328 luaV_closure(n);
315 } 329 }
316 incr_top;
317} 330}
318 331
319void lua_pushusertag (void *u, int tag) 332void lua_pushusertag (void *u, int tag)
@@ -339,8 +352,6 @@ void lua_pushobject (lua_Object o)
339 *luaD_stack.top = *Address(o); 352 *luaD_stack.top = *Address(o);
340 if (ttype(luaD_stack.top) == LUA_T_MARK) 353 if (ttype(luaD_stack.top) == LUA_T_MARK)
341 ttype(luaD_stack.top) = LUA_T_FUNCTION; 354 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; 355 incr_top;
345} 356}
346 357
@@ -350,12 +361,11 @@ int lua_tag (lua_Object lo)
350 if (lo == LUA_NOOBJECT) return LUA_T_NIL; 361 if (lo == LUA_NOOBJECT) return LUA_T_NIL;
351 else { 362 else {
352 TObject *o = Address(lo); 363 TObject *o = Address(lo);
353 lua_Type t = ttype(o); 364 int t = luaT_efectivetag(o);
354 if (t == LUA_T_USERDATA) 365 if (t == LUA_T_USERDATA)
355 return o->value.ts->u.d.tag; 366 return o->value.ts->u.d.tag;
356 else if (t == LUA_T_ARRAY) 367 else
357 return o->value.a->htag; 368 return t;
358 else return t;
359 } 369 }
360} 370}
361 371
@@ -395,7 +405,7 @@ lua_Function lua_stackedfunction (int level)
395{ 405{
396 StkId i; 406 StkId i;
397 for (i = (luaD_stack.top-1)-luaD_stack.stack; i>=0; i--) 407 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) 408 if (luaD_stack.stack[i].ttype == LUA_T_MARK)
399 if (level-- == 0) 409 if (level-- == 0)
400 return Ref(luaD_stack.stack+i); 410 return Ref(luaD_stack.stack+i);
401 return LUA_NOOBJECT; 411 return LUA_NOOBJECT;
@@ -413,24 +423,27 @@ lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
413{ 423{
414 TObject *f = luaA_Address(func); 424 TObject *f = luaA_Address(func);
415 /* check whether func is a Lua function */ 425 /* check whether func is a Lua function */
416 if (ttype(f) != LUA_T_MARK && ttype(f) != LUA_T_FUNCTION) 426 if (!(f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION))
417 return LUA_NOOBJECT; 427 return LUA_NOOBJECT;
418 *name = luaF_getlocalname(f->value.tf, local_number, lua_currentline(func)); 428 else {
419 if (*name) { 429 TProtoFunc *fp = protovalue(f)->value.tf;
420 /* if "*name", there must be a LUA_T_LINE */ 430 *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
421 /* therefore, f+2 points to function base */ 431 if (*name) {
422 return Ref((f+2)+(local_number-1)); 432 /* if "*name", there must be a LUA_T_LINE */
433 /* therefore, f+2 points to function base */
434 return Ref((f+2)+(local_number-1));
435 }
436 else
437 return LUA_NOOBJECT;
423 } 438 }
424 else
425 return LUA_NOOBJECT;
426} 439}
427 440
428 441
429int lua_setlocal (lua_Function func, int local_number) 442int lua_setlocal (lua_Function func, int local_number)
430{ 443{
431 TObject *f = Address(func); 444 TObject *f = Address(func);
432 char *name = luaF_getlocalname(f->value.tf, local_number, 445 TProtoFunc *fp = protovalue(f)->value.tf;
433 lua_currentline(func)); 446 char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
434 checkCparams(1); 447 checkCparams(1);
435 --luaD_stack.top; 448 --luaD_stack.top;
436 if (name) { 449 if (name) {
@@ -447,16 +460,18 @@ int lua_setlocal (lua_Function func, int local_number)
447void lua_funcinfo (lua_Object func, char **filename, int *linedefined) 460void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
448{ 461{
449 TObject *f = Address(func); 462 TObject *f = Address(func);
450 if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION) 463 if (!(ttype(f) == LUA_T_MARK || ttype(f) == LUA_T_FUNCTION))
451 { 464 lua_error("API - `funcinfo' called with a non-function value");
452 TProtoFunc *fp = f->value.cl->consts[0].value.tf; 465 else {
453 *filename = fp->fileName->str; 466 f = protovalue(f);
454 *linedefined = fp->lineDefined; 467 if (ttype(f) == LUA_T_PROTO) {
455 } 468 *filename = tfvalue(f)->fileName->str;
456 else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION) 469 *linedefined = tfvalue(f)->lineDefined;
457 { 470 }
458 *filename = "(C)"; 471 else {
459 *linedefined = -1; 472 *filename = "(C)";
473 *linedefined = -1;
474 }
460 } 475 }
461} 476}
462 477
@@ -464,17 +479,10 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
464static TObject *functofind; 479static TObject *functofind;
465static int checkfunc (TObject *o) 480static int checkfunc (TObject *o)
466{ 481{
467 if (o->ttype == LUA_T_FUNCTION) 482 return (o->ttype == LUA_T_FUNCTION) &&
468 return 483 ((functofind->ttype == LUA_T_FUNCTION) ||
469 ((functofind->ttype == LUA_T_FUNCTION || 484 (functofind->ttype == LUA_T_MARK)) &&
470 functofind->ttype == LUA_T_MARK) && 485 (functofind->value.cl == o->value.cl);
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} 486}
479 487
480 488
@@ -548,8 +556,9 @@ static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
548{ 556{
549 StkId base = (luaD_stack.top-luaD_stack.stack)-nParams; 557 StkId base = (luaD_stack.top-luaD_stack.stack)-nParams;
550 luaD_openstack(nParams); 558 luaD_openstack(nParams);
551 luaD_stack.stack[base].ttype = LUA_T_CFUNCTION; 559 luaD_stack.stack[base].ttype = LUA_T_CPROTO;
552 luaD_stack.stack[base].value.f = f; 560 luaD_stack.stack[base].value.f = f;
561 luaF_simpleclosure(luaD_stack.stack+base);
553 luaD_call(base+1, nResults); 562 luaD_call(base+1, nResults);
554} 563}
555 564