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 | ff08b0f4069e322ec4c2b02aa5553424227357ba (patch) | |
tree | 5510334dd14e5cdc40aeb4511f924d227bb6927f /func.c | |
parent | c1801e623f75dab3ccc4444ebe76417e1ef88afb (diff) | |
download | lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.gz lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.bz2 lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.zip |
Lua Function structures
Diffstat (limited to 'func.c')
-rw-r--r-- | func.c | 146 |
1 files changed, 0 insertions, 146 deletions
diff --git a/func.c b/func.c deleted file mode 100644 index a78122da..00000000 --- a/func.c +++ /dev/null | |||
@@ -1,146 +0,0 @@ | |||
1 | #include <string.h> | ||
2 | |||
3 | #include "luadebug.h" | ||
4 | #include "table.h" | ||
5 | #include "luamem.h" | ||
6 | #include "func.h" | ||
7 | #include "opcode.h" | ||
8 | #include "inout.h" | ||
9 | |||
10 | |||
11 | static TFunc *function_root = NULL; | ||
12 | |||
13 | |||
14 | static void luaI_insertfunction (TFunc *f) | ||
15 | { | ||
16 | lua_pack(); | ||
17 | f->next = function_root; | ||
18 | function_root = f; | ||
19 | f->marked = 0; | ||
20 | } | ||
21 | |||
22 | /* | ||
23 | ** Initialize TFunc struct | ||
24 | */ | ||
25 | void luaI_initTFunc (TFunc *f) | ||
26 | { | ||
27 | f->next = NULL; | ||
28 | f->marked = 0; | ||
29 | f->code = NULL; | ||
30 | f->lineDefined = 0; | ||
31 | f->fileName = lua_parsedfile; | ||
32 | f->consts = NULL; | ||
33 | f->nconsts = 0; | ||
34 | f->locvars = NULL; | ||
35 | luaI_insertfunction(f); | ||
36 | } | ||
37 | |||
38 | |||
39 | |||
40 | /* | ||
41 | ** Free function | ||
42 | */ | ||
43 | static void luaI_freefunc (TFunc *f) | ||
44 | { | ||
45 | luaI_free(f->code); | ||
46 | luaI_free(f->locvars); | ||
47 | luaI_free(f->consts); | ||
48 | luaI_free(f); | ||
49 | } | ||
50 | |||
51 | |||
52 | void luaI_funcfree (TFunc *l) | ||
53 | { | ||
54 | while (l) { | ||
55 | TFunc *next = l->next; | ||
56 | luaI_freefunc(l); | ||
57 | l = next; | ||
58 | } | ||
59 | } | ||
60 | |||
61 | |||
62 | void luaI_funcmark (TFunc *f) | ||
63 | { | ||
64 | f->marked = 1; | ||
65 | if (!f->fileName->marked) | ||
66 | f->fileName->marked = 1; | ||
67 | if (f->consts) { | ||
68 | int i; | ||
69 | for (i=0; i<f->nconsts; i++) | ||
70 | lua_markobject(&f->consts[i]); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | |||
75 | /* | ||
76 | ** Garbage collection function. | ||
77 | */ | ||
78 | TFunc *luaI_funccollector (long *acum) | ||
79 | { | ||
80 | TFunc *curr = function_root; | ||
81 | TFunc *prev = NULL; | ||
82 | TFunc *frees = NULL; | ||
83 | long counter = 0; | ||
84 | while (curr) { | ||
85 | TFunc *next = curr->next; | ||
86 | if (!curr->marked) { | ||
87 | if (prev == NULL) | ||
88 | function_root = next; | ||
89 | else | ||
90 | prev->next = next; | ||
91 | curr->next = frees; | ||
92 | frees = curr; | ||
93 | ++counter; | ||
94 | } | ||
95 | else { | ||
96 | curr->marked = 0; | ||
97 | prev = curr; | ||
98 | } | ||
99 | curr = next; | ||
100 | } | ||
101 | *acum += counter; | ||
102 | return frees; | ||
103 | } | ||
104 | |||
105 | |||
106 | void lua_funcinfo (lua_Object func, char **filename, int *linedefined) | ||
107 | { | ||
108 | TObject *f = luaI_Address(func); | ||
109 | if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION) | ||
110 | { | ||
111 | *filename = f->value.tf->fileName->str; | ||
112 | *linedefined = f->value.tf->lineDefined; | ||
113 | } | ||
114 | else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION) | ||
115 | { | ||
116 | *filename = "(C)"; | ||
117 | *linedefined = -1; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | |||
122 | /* | ||
123 | ** Look for n-esim local variable at line "line" in function "func". | ||
124 | ** Returns NULL if not found. | ||
125 | */ | ||
126 | char *luaI_getlocalname (TFunc *func, int local_number, int line) | ||
127 | { | ||
128 | int count = 0; | ||
129 | char *varname = NULL; | ||
130 | LocVar *lv = func->locvars; | ||
131 | if (lv == NULL) | ||
132 | return NULL; | ||
133 | for (; lv->line != -1 && lv->line < line; lv++) | ||
134 | { | ||
135 | if (lv->varname) /* register */ | ||
136 | { | ||
137 | if (++count == local_number) | ||
138 | varname = lv->varname->str; | ||
139 | } | ||
140 | else /* unregister */ | ||
141 | if (--count < local_number) | ||
142 | varname = NULL; | ||
143 | } | ||
144 | return varname; | ||
145 | } | ||
146 | |||