LCOV - code coverage report
Current view: directory - src - lfunc.c Found Hit Coverage
Test: Lua 5.1.4 Lines: 105 96 91.4 %
Date: 2009-09-13
Colors: not hit hit

       1                 : /*
       2                 : ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $
       3                 : ** Auxiliary functions to manipulate prototypes and closures
       4                 : ** See Copyright Notice in lua.h
       5                 : */
       6                 : 
       7                 : 
       8                 : #include <stddef.h>
       9                 : 
      10                 : #define lfunc_c
      11                 : #define LUA_CORE
      12                 : 
      13                 : #include "lua.h"
      14                 : 
      15                 : #include "lfunc.h"
      16                 : #include "lgc.h"
      17                 : #include "lmem.h"
      18                 : #include "lobject.h"
      19                 : #include "lstate.h"
      20                 : 
      21                 : 
      22                 : 
      23            5436 : Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
      24            5436 :   Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
      25            5436 :   luaC_link(L, obj2gco(c), LUA_TFUNCTION);
      26            5436 :   c->c.isC = 1;
      27            5436 :   c->c.env = e;
      28            5436 :   c->c.nupvalues = cast_byte(nelems);
      29            5436 :   return c;
      30                 : }
      31                 : 
      32                 : 
      33            2461 : Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
      34            2461 :   Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
      35            2461 :   luaC_link(L, obj2gco(c), LUA_TFUNCTION);
      36            2461 :   c->l.isC = 0;
      37            2461 :   c->l.env = e;
      38            2461 :   c->l.nupvalues = cast_byte(nelems);
      39            2461 :   while (nelems--) c->l.upvals[nelems] = NULL;
      40            2461 :   return c;
      41                 : }
      42                 : 
      43                 : 
      44               0 : UpVal *luaF_newupval (lua_State *L) {
      45               0 :   UpVal *uv = luaM_new(L, UpVal);
      46               0 :   luaC_link(L, obj2gco(uv), LUA_TUPVAL);
      47               0 :   uv->v = &uv->u.value;
      48               0 :   setnilvalue(uv->v);
      49               0 :   return uv;
      50                 : }
      51                 : 
      52                 : 
      53            6252 : UpVal *luaF_findupval (lua_State *L, StkId level) {
      54            6252 :   global_State *g = G(L);
      55            6252 :   GCObject **pp = &L->openupval;
      56                 :   UpVal *p;
      57                 :   UpVal *uv;
      58           19669 :   while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {
      59                 :     lua_assert(p->v != &p->u.value);
      60            8524 :     if (p->v == level) {  /* found a corresponding upvalue? */
      61            1359 :       if (isdead(g, obj2gco(p)))  /* is it dead? */
      62               0 :         changewhite(obj2gco(p));  /* ressurect it */
      63            1359 :       return p;
      64                 :     }
      65            7165 :     pp = &p->next;
      66                 :   }
      67            4893 :   uv = luaM_new(L, UpVal);  /* not found: create a new one */
      68            4893 :   uv->tt = LUA_TUPVAL;
      69            4893 :   uv->marked = luaC_white(g);
      70            4893 :   uv->v = level;  /* current value lives in the stack */
      71            4893 :   uv->next = *pp;  /* chain it in the proper position */
      72            4893 :   *pp = obj2gco(uv);
      73            4893 :   uv->u.l.prev = &g->uvhead;  /* double link it in `uvhead' list */
      74            4893 :   uv->u.l.next = g->uvhead.u.l.next;
      75            4893 :   uv->u.l.next->u.l.prev = uv;
      76            4893 :   g->uvhead.u.l.next = uv;
      77                 :   lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
      78            4893 :   return uv;
      79                 : }
      80                 : 
      81                 : 
      82            4893 : static void unlinkupval (UpVal *uv) {
      83                 :   lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
      84            4893 :   uv->u.l.next->u.l.prev = uv->u.l.prev;  /* remove from `uvhead' list */
      85            4893 :   uv->u.l.prev->u.l.next = uv->u.l.next;
      86            4893 : }
      87                 : 
      88                 : 
      89            4893 : void luaF_freeupval (lua_State *L, UpVal *uv) {
      90            4893 :   if (uv->v != &uv->u.value)  /* is it open? */
      91               0 :     unlinkupval(uv);  /* remove from open list */
      92            4893 :   luaM_free(L, uv);  /* free upvalue */
      93            4893 : }
      94                 : 
      95                 : 
      96            2785 : void luaF_close (lua_State *L, StkId level) {
      97                 :   UpVal *uv;
      98            2785 :   global_State *g = G(L);
      99           10463 :   while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {
     100            4893 :     GCObject *o = obj2gco(uv);
     101                 :     lua_assert(!isblack(o) && uv->v != &uv->u.value);
     102            4893 :     L->openupval = uv->next;  /* remove from `open' list */
     103            4893 :     if (isdead(g, o))
     104               0 :       luaF_freeupval(L, uv);  /* free upvalue */
     105                 :     else {
     106            4893 :       unlinkupval(uv);
     107            4893 :       setobj(L, &uv->u.value, uv->v);
     108            4893 :       uv->v = &uv->u.value;  /* now current value lives here */
     109            4893 :       luaC_linkupval(L, uv);  /* link upvalue into `gcroot' list */
     110                 :     }
     111                 :   }
     112            2785 : }
     113                 : 
     114                 : 
     115            1737 : Proto *luaF_newproto (lua_State *L) {
     116            1737 :   Proto *f = luaM_new(L, Proto);
     117            1737 :   luaC_link(L, obj2gco(f), LUA_TPROTO);
     118            1737 :   f->k = NULL;
     119            1737 :   f->sizek = 0;
     120            1737 :   f->p = NULL;
     121            1737 :   f->sizep = 0;
     122            1737 :   f->code = NULL;
     123            1737 :   f->sizecode = 0;
     124            1737 :   f->sizelineinfo = 0;
     125            1737 :   f->sizeupvalues = 0;
     126            1737 :   f->nups = 0;
     127            1737 :   f->upvalues = NULL;
     128            1737 :   f->numparams = 0;
     129            1737 :   f->is_vararg = 0;
     130            1737 :   f->maxstacksize = 0;
     131            1737 :   f->lineinfo = NULL;
     132            1737 :   f->sizelocvars = 0;
     133            1737 :   f->locvars = NULL;
     134            1737 :   f->linedefined = 0;
     135            1737 :   f->lastlinedefined = 0;
     136            1737 :   f->source = NULL;
     137            1737 :   return f;
     138                 : }
     139                 : 
     140                 : 
     141            1737 : void luaF_freeproto (lua_State *L, Proto *f) {
     142            1737 :   luaM_freearray(L, f->code, f->sizecode, Instruction);
     143            1737 :   luaM_freearray(L, f->p, f->sizep, Proto *);
     144            1737 :   luaM_freearray(L, f->k, f->sizek, TValue);
     145            1737 :   luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
     146            1737 :   luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
     147            1737 :   luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
     148            1737 :   luaM_free(L, f);
     149            1737 : }
     150                 : 
     151                 : 
     152            7897 : void luaF_freeclosure (lua_State *L, Closure *c) {
     153                 :   int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
     154            7897 :                           sizeLclosure(c->l.nupvalues);
     155            7897 :   luaM_freemem(L, c, size);
     156            7897 : }
     157                 : 
     158                 : 
     159                 : /*
     160                 : ** Look for n-th local variable at line `line' in function `func'.
     161                 : ** Returns NULL if not found.
     162                 : */
     163              83 : const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
     164                 :   int i;
     165              83 :   for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
     166               1 :     if (pc < f->locvars[i].endpc) {  /* is variable active? */
     167               1 :       local_number--;
     168               1 :       if (local_number == 0)
     169               1 :         return getstr(f->locvars[i].varname);
     170                 :     }
     171                 :   }
     172              82 :   return NULL;  /* not found */
     173                 : }
     174                 : 

Generated by: LCOV version 1.7