summaryrefslogtreecommitdiff
path: root/lfunc.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
commitff08b0f4069e322ec4c2b02aa5553424227357ba (patch)
tree5510334dd14e5cdc40aeb4511f924d227bb6927f /lfunc.c
parentc1801e623f75dab3ccc4444ebe76417e1ef88afb (diff)
downloadlua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.gz
lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.bz2
lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.zip
Lua Function structures
Diffstat (limited to 'lfunc.c')
-rw-r--r--lfunc.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/lfunc.c b/lfunc.c
new file mode 100644
index 00000000..c84ab973
--- /dev/null
+++ b/lfunc.c
@@ -0,0 +1,105 @@
1/*
2** $Id: $
3** Lua Funcion auxiliar
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9
10#include "lfunc.h"
11#include "lmem.h"
12
13
14TProtoFunc *luaF_root = NULL;
15Closure *luaF_rootcl = NULL;
16
17
18static void luaI_insertfunction (TProtoFunc *f)
19{
20 ++luaO_nentities;
21 f->head.next = (GCnode *)luaF_root;
22 luaF_root = f;
23 f->head.marked = 0;
24}
25
26
27Closure *luaF_newclosure (int nelems)
28{
29 Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
30 ++luaO_nentities;
31 c->head.next = (GCnode *)luaF_rootcl;
32 luaF_rootcl = c;
33 c->head.marked = 0;
34 return c;
35}
36
37
38TProtoFunc *luaF_newproto (void)
39{
40 TProtoFunc *f = luaM_new(TProtoFunc);
41 f->code = NULL;
42 f->lineDefined = 0;
43 f->fileName = NULL;
44 f->consts = NULL;
45 f->nconsts = 0;
46 f->nupvalues = 0;
47 f->locvars = NULL;
48 luaI_insertfunction(f);
49 return f;
50}
51
52
53
54static void freefunc (TProtoFunc *f)
55{
56 luaM_free(f->code);
57 luaM_free(f->locvars);
58 luaM_free(f->consts);
59 luaM_free(f);
60}
61
62
63void luaF_freeproto (TProtoFunc *l)
64{
65 while (l) {
66 TProtoFunc *next = (TProtoFunc *)l->head.next;
67 freefunc(l);
68 l = next;
69 }
70}
71
72
73void luaF_freeclosure (Closure *l)
74{
75 while (l) {
76 Closure *next = (Closure *)l->head.next;
77 luaM_free(l);
78 l = next;
79 }
80}
81
82
83/*
84** Look for n-esim local variable at line "line" in function "func".
85** Returns NULL if not found.
86*/
87char *luaF_getlocalname (TProtoFunc *func, int local_number, int line)
88{
89 int count = 0;
90 char *varname = NULL;
91 LocVar *lv = func->locvars;
92 if (lv == NULL)
93 return NULL;
94 for (; lv->line != -1 && lv->line < line; lv++) {
95 if (lv->varname) { /* register */
96 if (++count == local_number)
97 varname = lv->varname->str;
98 }
99 else /* unregister */
100 if (--count < local_number)
101 varname = NULL;
102 }
103 return varname;
104}
105