diff options
Diffstat (limited to 'lfunc.c')
| -rw-r--r-- | lfunc.c | 105 |
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 | |||
| 14 | TProtoFunc *luaF_root = NULL; | ||
| 15 | Closure *luaF_rootcl = NULL; | ||
| 16 | |||
| 17 | |||
| 18 | static 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 | |||
| 27 | Closure *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 | |||
| 38 | TProtoFunc *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 | |||
| 54 | static 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 | |||
| 63 | void 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 | |||
| 73 | void 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 | */ | ||
| 87 | char *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 | |||
