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 | |
| parent | c1801e623f75dab3ccc4444ebe76417e1ef88afb (diff) | |
| download | lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.gz lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.tar.bz2 lua-ff08b0f4069e322ec4c2b02aa5553424227357ba.zip | |
Lua Function structures
| -rw-r--r-- | func.c | 146 | ||||
| -rw-r--r-- | func.h | 41 | ||||
| -rw-r--r-- | lfunc.c | 105 | ||||
| -rw-r--r-- | lfunc.h | 26 |
4 files changed, 131 insertions, 187 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 | |||
diff --git a/func.h b/func.h deleted file mode 100644 index 5af43056..00000000 --- a/func.h +++ /dev/null | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: func.h,v 1.11 1997/07/29 20:38:45 roberto Exp roberto $ | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef func_h | ||
| 6 | #define func_h | ||
| 7 | |||
| 8 | #include "types.h" | ||
| 9 | #include "lua.h" | ||
| 10 | #include "tree.h" | ||
| 11 | |||
| 12 | typedef struct LocVar | ||
| 13 | { | ||
| 14 | TaggedString *varname; /* NULL signals end of scope */ | ||
| 15 | int line; | ||
| 16 | } LocVar; | ||
| 17 | |||
| 18 | |||
| 19 | /* | ||
| 20 | ** Function Headers | ||
| 21 | */ | ||
| 22 | typedef struct TFunc | ||
| 23 | { | ||
| 24 | struct TFunc *next; | ||
| 25 | int marked; | ||
| 26 | Byte *code; | ||
| 27 | int lineDefined; | ||
| 28 | TaggedString *fileName; | ||
| 29 | struct TObject *consts; | ||
| 30 | int nconsts; | ||
| 31 | LocVar *locvars; | ||
| 32 | } TFunc; | ||
| 33 | |||
| 34 | TFunc *luaI_funccollector (long *cont); | ||
| 35 | void luaI_funcfree (TFunc *l); | ||
| 36 | void luaI_funcmark (TFunc *f); | ||
| 37 | void luaI_initTFunc (TFunc *f); | ||
| 38 | |||
| 39 | char *luaI_getlocalname (TFunc *func, int local_number, int line); | ||
| 40 | |||
| 41 | #endif | ||
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 | |||
diff --git a/lfunc.h b/lfunc.h new file mode 100644 index 00000000..ed5a085b --- /dev/null +++ b/lfunc.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: $ | ||
| 3 | ** Lua Function structures | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef lfunc_h | ||
| 8 | #define lfunc_h | ||
| 9 | |||
| 10 | |||
| 11 | #include "lobject.h" | ||
| 12 | |||
| 13 | |||
| 14 | extern TProtoFunc *luaF_root; | ||
| 15 | extern Closure *luaF_rootcl; | ||
| 16 | |||
| 17 | |||
| 18 | TProtoFunc *luaF_newproto (void); | ||
| 19 | Closure *luaF_newclosure (int nelems); | ||
| 20 | void luaF_freeproto (TProtoFunc *l); | ||
| 21 | void luaF_freeclosure (Closure *l); | ||
| 22 | |||
| 23 | char *luaF_getlocalname (TProtoFunc *func, int local_number, int line); | ||
| 24 | |||
| 25 | |||
| 26 | #endif | ||
