aboutsummaryrefslogtreecommitdiff
path: root/func.c
blob: a78122da703772f62b0497a8e6039c626c855e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <string.h>

#include "luadebug.h"
#include "table.h"
#include "luamem.h"
#include "func.h"
#include "opcode.h"
#include "inout.h"


static TFunc *function_root = NULL;


static void luaI_insertfunction (TFunc *f)
{
  lua_pack();
  f->next = function_root;
  function_root = f;
  f->marked = 0;
}

/*
** Initialize TFunc struct
*/
void luaI_initTFunc (TFunc *f)
{
  f->next = NULL;
  f->marked = 0;
  f->code = NULL;
  f->lineDefined = 0;
  f->fileName = lua_parsedfile;
  f->consts = NULL;
  f->nconsts = 0;
  f->locvars = NULL;
  luaI_insertfunction(f);
}



/*
** Free function
*/
static void luaI_freefunc (TFunc *f)
{
  luaI_free(f->code);
  luaI_free(f->locvars);
  luaI_free(f->consts);
  luaI_free(f);
}


void luaI_funcfree (TFunc *l)
{
  while (l) {
    TFunc *next = l->next;
    luaI_freefunc(l);
    l = next;
  }
}


void luaI_funcmark (TFunc *f)
{
  f->marked = 1;
  if (!f->fileName->marked)
    f->fileName->marked = 1;
  if (f->consts) {
    int i;
    for (i=0; i<f->nconsts; i++)
      lua_markobject(&f->consts[i]);
  }
}


/*
** Garbage collection function.
*/
TFunc *luaI_funccollector (long *acum)
{
  TFunc *curr = function_root;
  TFunc *prev = NULL;
  TFunc *frees = NULL;
  long counter = 0;
  while (curr) {
    TFunc *next = curr->next;
    if (!curr->marked) {
      if (prev == NULL)
        function_root = next;
      else
        prev->next = next;
      curr->next = frees;
      frees = curr;
      ++counter;
    }
    else {
      curr->marked = 0;
      prev = curr;
    } 
    curr = next;
  }
  *acum += counter;
  return frees;
}


void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
{
  TObject *f = luaI_Address(func);
  if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
  {
    *filename = f->value.tf->fileName->str;
    *linedefined = f->value.tf->lineDefined;
  }
  else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
  {
    *filename = "(C)";
    *linedefined = -1;
  }
}


/*
** Look for n-esim local variable at line "line" in function "func".
** Returns NULL if not found.
*/
char *luaI_getlocalname (TFunc *func, int local_number, int line)
{
  int count = 0;
  char *varname = NULL;
  LocVar *lv = func->locvars;
  if (lv == NULL)
    return NULL;
  for (; lv->line != -1 && lv->line < line; lv++)
  {
    if (lv->varname)               /* register */
    {
      if (++count == local_number)
        varname = lv->varname->str;
    }
    else                           /* unregister */
      if (--count < local_number)
        varname = NULL;
  }
  return varname;
}