lfunc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. ** $Id: lfunc.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
  3. ** Lua Funcion auxiliar
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lfunc.h"
  8. #include "lmem.h"
  9. GCnode luaF_root = {NULL, 0};
  10. GCnode luaF_rootcl = {NULL, 0};
  11. Closure *luaF_newclosure (int nelems)
  12. {
  13. Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
  14. luaO_insertlist(&luaF_rootcl, (GCnode *)c);
  15. return c;
  16. }
  17. TProtoFunc *luaF_newproto (void)
  18. {
  19. TProtoFunc *f = luaM_new(TProtoFunc);
  20. f->code = NULL;
  21. f->lineDefined = 0;
  22. f->fileName = NULL;
  23. f->consts = NULL;
  24. f->nconsts = 0;
  25. f->nupvalues = 0;
  26. f->locvars = NULL;
  27. luaO_insertlist(&luaF_root, (GCnode *)f);
  28. return f;
  29. }
  30. static void freefunc (TProtoFunc *f)
  31. {
  32. luaM_free(f->code);
  33. luaM_free(f->locvars);
  34. luaM_free(f->consts);
  35. luaM_free(f);
  36. }
  37. void luaF_freeproto (TProtoFunc *l)
  38. {
  39. while (l) {
  40. TProtoFunc *next = (TProtoFunc *)l->head.next;
  41. freefunc(l);
  42. l = next;
  43. }
  44. }
  45. void luaF_freeclosure (Closure *l)
  46. {
  47. while (l) {
  48. Closure *next = (Closure *)l->head.next;
  49. luaM_free(l);
  50. l = next;
  51. }
  52. }
  53. /*
  54. ** Look for n-esim local variable at line "line" in function "func".
  55. ** Returns NULL if not found.
  56. */
  57. char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
  58. {
  59. int count = 0;
  60. char *varname = NULL;
  61. LocVar *lv = func->locvars;
  62. if (lv == NULL)
  63. return NULL;
  64. for (; lv->line != -1 && lv->line < line; lv++) {
  65. if (lv->varname) { /* register */
  66. if (++count == local_number)
  67. varname = lv->varname->str;
  68. }
  69. else /* unregister */
  70. if (--count < local_number)
  71. varname = NULL;
  72. }
  73. return varname;
  74. }