ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lfunc.cpp

Go to the documentation of this file.
00001 /*
00002 ** Auxiliary functions to manipulate prototypes and closures
00003 ** See Copyright Notice in lua.h
00004 */
00005 
00006 #define FORBIDDEN_SYMBOL_EXCEPTION_setjmp
00007 #define FORBIDDEN_SYMBOL_EXCEPTION_longjmp
00008 
00009 #include "engines/grim/lua/lfunc.h"
00010 #include "engines/grim/lua/lmem.h"
00011 #include "engines/grim/lua/lstate.h"
00012 
00013 namespace Grim {
00014 
00015 #define gcsizeproto(p)  5  /* approximate "weight" for a prototype */
00016 #define gcsizeclosure(c) 1  /* approximate "weight" for a closure */
00017 
00018 
00019 
00020 Closure *luaF_newclosure(int32 nelems) {
00021     Closure *c = (Closure *)luaM_malloc(sizeof(Closure) + nelems * sizeof(TObject));
00022     luaO_insertlist(&rootcl, (GCnode *)c);
00023     nblocks += gcsizeclosure(c);
00024     c->nelems = nelems;
00025     return c;
00026 }
00027 
00028 TProtoFunc *luaF_newproto() {
00029     TProtoFunc *f = luaM_new(TProtoFunc);
00030     f->code = nullptr;
00031     f->lineDefined = 0;
00032     f->fileName = nullptr;
00033     f->consts = nullptr;
00034     f->nconsts = 0;
00035     f->locvars = nullptr;
00036     luaO_insertlist(&rootproto, (GCnode *)f);
00037     nblocks += gcsizeproto(f);
00038     return f;
00039 }
00040 
00041 static void freefunc(TProtoFunc *f) {
00042     luaM_free(f->code);
00043     luaM_free(f->locvars);
00044     luaM_free(f->consts);
00045     luaM_free(f);
00046 }
00047 
00048 void luaF_freeproto(TProtoFunc *l) {
00049     while (l) {
00050         TProtoFunc *next = (TProtoFunc *)l->head.next;
00051         nblocks -= gcsizeproto(l);
00052         freefunc(l);
00053         l = next;
00054     }
00055 }
00056 
00057 
00058 void luaF_freeclosure(Closure *l) {
00059     while (l) {
00060         Closure *next = (Closure *)l->head.next;
00061         nblocks -= gcsizeclosure(l);
00062         luaM_free(l);
00063         l = next;
00064     }
00065 }
00066 
00067 
00068 /*
00069 ** Look for n-th local variable at line "line" in function "func".
00070 ** Returns NULL if not found.
00071 */
00072 char *luaF_getlocalname (TProtoFunc *func, int32 local_number, int32 line) {
00073     int32 count = 0;
00074     char *varname = nullptr;
00075     LocVar *lv = func->locvars;
00076     if (!lv)
00077         return nullptr;
00078     for (; lv->line != -1 && lv->line < line; lv++) {
00079         if (lv->varname) {  // register
00080             if (++count == local_number)
00081                 varname = lv->varname->str;
00082         } else {  // unregister
00083             if (--count < local_number) {
00084                 varname = nullptr;
00085             }
00086         }
00087     }
00088     return varname;
00089 }
00090 
00091 } // end of namespace Grim


Generated on Sat Feb 9 2019 05:00:35 for ResidualVM by doxygen 1.7.1
curved edge   curved edge