diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
| commit | ea169d20835aa7d2a42641decc5b5d802047afca (patch) | |
| tree | 130c69567896b6b5b61f49c2ff0c053ad5602551 | |
| parent | c31aa863ac29fad5bb3f44c4e08640f877b65f25 (diff) | |
| download | lua-ea169d20835aa7d2a42641decc5b5d802047afca.tar.gz lua-ea169d20835aa7d2a42641decc5b5d802047afca.tar.bz2 lua-ea169d20835aa7d2a42641decc5b5d802047afca.zip | |
auxiliar functions from Lua API
| -rw-r--r-- | lapi.c | 564 | ||||
| -rw-r--r-- | lapi.h | 20 |
2 files changed, 584 insertions, 0 deletions
| @@ -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 | |||
| 27 | char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | ||
| 28 | "$Autores: " LUA_AUTHORS " $"; | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | TObject *luaA_Address (lua_Object o) | ||
| 33 | { | ||
| 34 | return Address(o); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | void luaA_packresults (void) | ||
| 39 | { | ||
| 40 | luaV_pack(luaD_Cstack.lua2C, luaD_Cstack.num, luaD_stack.top); | ||
| 41 | incr_top; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | int 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 | |||
| 55 | static 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 | |||
| 62 | static 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 | |||
| 70 | static 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 | |||
| 78 | lua_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 | */ | ||
| 89 | lua_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 | |||
| 98 | int 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 | |||
| 110 | lua_Object lua_gettagmethod (int tag, char *event) | ||
| 111 | { | ||
| 112 | return put_luaObject(luaT_gettagmethod(tag, event)); | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | lua_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 | |||
| 124 | lua_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 | |||
| 133 | lua_Object lua_gettable (void) | ||
| 134 | { | ||
| 135 | checkCparams(2); | ||
| 136 | luaV_gettable(); | ||
| 137 | return put_luaObjectonTop(); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | lua_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 | |||
| 158 | void lua_settable (void) | ||
| 159 | { | ||
| 160 | checkCparams(3); | ||
| 161 | luaV_settable(luaD_stack.top-3, 1); | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | void lua_rawsettable (void) | ||
| 166 | { | ||
| 167 | checkCparams(3); | ||
| 168 | luaV_settable(luaD_stack.top-3, 0); | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | lua_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 | |||
| 182 | lua_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 | |||
| 190 | lua_Object lua_rawgetglobal (char *name) | ||
| 191 | { | ||
| 192 | return put_luaObject(&luaG_global[luaG_findsymbolbyname(name)].object); | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | void 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 | |||
| 204 | void 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 | |||
| 213 | int lua_isnil (lua_Object o) | ||
| 214 | { | ||
| 215 | return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL); | ||
| 216 | } | ||
| 217 | |||
| 218 | int lua_istable (lua_Object o) | ||
| 219 | { | ||
| 220 | return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY); | ||
| 221 | } | ||
| 222 | |||
| 223 | int lua_isuserdata (lua_Object o) | ||
| 224 | { | ||
| 225 | return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA); | ||
| 226 | } | ||
| 227 | |||
| 228 | int lua_iscfunction (lua_Object o) | ||
| 229 | { | ||
| 230 | int t = lua_tag(o); | ||
| 231 | return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION); | ||
| 232 | } | ||
| 233 | |||
| 234 | int lua_isnumber (lua_Object o) | ||
| 235 | { | ||
| 236 | return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0); | ||
| 237 | } | ||
| 238 | |||
| 239 | int lua_isstring (lua_Object o) | ||
| 240 | { | ||
| 241 | int t = lua_tag(o); | ||
| 242 | return (t == LUA_T_STRING) || (t == LUA_T_NUMBER); | ||
| 243 | } | ||
| 244 | |||
| 245 | int 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 | |||
| 253 | real 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 | |||
| 260 | char *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 | |||
| 267 | void *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 | |||
| 274 | lua_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 | |||
| 283 | void lua_pushnil (void) | ||
| 284 | { | ||
| 285 | ttype(luaD_stack.top) = LUA_T_NIL; | ||
| 286 | incr_top; | ||
| 287 | } | ||
| 288 | |||
| 289 | void lua_pushnumber (real n) | ||
| 290 | { | ||
| 291 | ttype(luaD_stack.top) = LUA_T_NUMBER; | ||
| 292 | nvalue(luaD_stack.top) = n; | ||
| 293 | incr_top; | ||
| 294 | } | ||
| 295 | |||
| 296 | void 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 | |||
| 308 | void 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 | |||
| 319 | void 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 | |||
| 329 | void luaA_pushobject (TObject *o) | ||
| 330 | { | ||
| 331 | *luaD_stack.top = *o; | ||
| 332 | incr_top; | ||
| 333 | } | ||
| 334 | |||
| 335 | void 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 | |||
| 348 | int 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 | |||
| 363 | void 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 */ | ||
| 390 | lua_CHFunction lua_callhook = NULL; | ||
| 391 | lua_LHFunction lua_linehook = NULL; | ||
| 392 | |||
| 393 | |||
| 394 | lua_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 | |||
| 405 | int 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 | |||
| 412 | lua_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 | |||
| 429 | int 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 | |||
| 447 | void 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 | |||
| 464 | static TObject *functofind; | ||
| 465 | static 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 | |||
| 481 | char *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 | |||
| 499 | static int numCblocks = 0; | ||
| 500 | static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS]; | ||
| 501 | |||
| 502 | void 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 | |||
| 510 | void lua_endblock (void) | ||
| 511 | { | ||
| 512 | --numCblocks; | ||
| 513 | luaD_Cstack = Cblocks[numCblocks]; | ||
| 514 | luaD_adjusttop(luaD_Cstack.base); | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | |||
| 521 | int 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 | |||
| 532 | lua_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 | |||
| 547 | static 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 | |||
| 556 | lua_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 | |||
| @@ -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 | |||
| 15 | TObject *luaA_Address (lua_Object o); | ||
| 16 | void luaA_pushobject (TObject *o); | ||
| 17 | void luaA_packresults (void); | ||
| 18 | int luaA_passresults (void); | ||
| 19 | |||
| 20 | #endif | ||
