commit ff08b0f4069e322ec4c2b02aa5553424227357ba
parent c1801e623f75dab3ccc4444ebe76417e1ef88afb
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 16 Sep 1997 16:25:38 -0300
Lua Function structures
Diffstat:
D | func.c | | | 146 | ------------------------------------------------------------------------------- |
D | func.h | | | 41 | ----------------------------------------- |
A | lfunc.c | | | 105 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | lfunc.h | | | 26 | ++++++++++++++++++++++++++ |
4 files changed, 131 insertions(+), 187 deletions(-)
diff --git a/func.c b/func.c
@@ -1,146 +0,0 @@
-#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;
-}
-
diff --git a/func.h b/func.h
@@ -1,41 +0,0 @@
-/*
-** $Id: func.h,v 1.11 1997/07/29 20:38:45 roberto Exp roberto $
-*/
-
-#ifndef func_h
-#define func_h
-
-#include "types.h"
-#include "lua.h"
-#include "tree.h"
-
-typedef struct LocVar
-{
- TaggedString *varname; /* NULL signals end of scope */
- int line;
-} LocVar;
-
-
-/*
-** Function Headers
-*/
-typedef struct TFunc
-{
- struct TFunc *next;
- int marked;
- Byte *code;
- int lineDefined;
- TaggedString *fileName;
- struct TObject *consts;
- int nconsts;
- LocVar *locvars;
-} TFunc;
-
-TFunc *luaI_funccollector (long *cont);
-void luaI_funcfree (TFunc *l);
-void luaI_funcmark (TFunc *f);
-void luaI_initTFunc (TFunc *f);
-
-char *luaI_getlocalname (TFunc *func, int local_number, int line);
-
-#endif
diff --git a/lfunc.c b/lfunc.c
@@ -0,0 +1,105 @@
+/*
+** $Id: $
+** Lua Funcion auxiliar
+** See Copyright Notice in lua.h
+*/
+
+
+#include <stdlib.h>
+
+#include "lfunc.h"
+#include "lmem.h"
+
+
+TProtoFunc *luaF_root = NULL;
+Closure *luaF_rootcl = NULL;
+
+
+static void luaI_insertfunction (TProtoFunc *f)
+{
+ ++luaO_nentities;
+ f->head.next = (GCnode *)luaF_root;
+ luaF_root = f;
+ f->head.marked = 0;
+}
+
+
+Closure *luaF_newclosure (int nelems)
+{
+ Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
+ ++luaO_nentities;
+ c->head.next = (GCnode *)luaF_rootcl;
+ luaF_rootcl = c;
+ c->head.marked = 0;
+ return c;
+}
+
+
+TProtoFunc *luaF_newproto (void)
+{
+ TProtoFunc *f = luaM_new(TProtoFunc);
+ f->code = NULL;
+ f->lineDefined = 0;
+ f->fileName = NULL;
+ f->consts = NULL;
+ f->nconsts = 0;
+ f->nupvalues = 0;
+ f->locvars = NULL;
+ luaI_insertfunction(f);
+ return f;
+}
+
+
+
+static void freefunc (TProtoFunc *f)
+{
+ luaM_free(f->code);
+ luaM_free(f->locvars);
+ luaM_free(f->consts);
+ luaM_free(f);
+}
+
+
+void luaF_freeproto (TProtoFunc *l)
+{
+ while (l) {
+ TProtoFunc *next = (TProtoFunc *)l->head.next;
+ freefunc(l);
+ l = next;
+ }
+}
+
+
+void luaF_freeclosure (Closure *l)
+{
+ while (l) {
+ Closure *next = (Closure *)l->head.next;
+ luaM_free(l);
+ l = next;
+ }
+}
+
+
+/*
+** Look for n-esim local variable at line "line" in function "func".
+** Returns NULL if not found.
+*/
+char *luaF_getlocalname (TProtoFunc *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;
+}
+
diff --git a/lfunc.h b/lfunc.h
@@ -0,0 +1,26 @@
+/*
+** $Id: $
+** Lua Function structures
+** See Copyright Notice in lua.h
+*/
+
+#ifndef lfunc_h
+#define lfunc_h
+
+
+#include "lobject.h"
+
+
+extern TProtoFunc *luaF_root;
+extern Closure *luaF_rootcl;
+
+
+TProtoFunc *luaF_newproto (void);
+Closure *luaF_newclosure (int nelems);
+void luaF_freeproto (TProtoFunc *l);
+void luaF_freeclosure (Closure *l);
+
+char *luaF_getlocalname (TProtoFunc *func, int local_number, int line);
+
+
+#endif