LCOV - code coverage report
Current view: top level - src - lfunc.c Coverage Total Hit
Test: Lua 5.1.5 Lines: 93.4 % 106 99
Test Date: 2024-04-28 10:23:09
Legend: Lines: hit not hit

            Line data    Source code
       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        12941 : Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
      24        12941 :   Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
      25        12941 :   luaC_link(L, obj2gco(c), LUA_TFUNCTION);
      26        12941 :   c->c.isC = 1;
      27        12941 :   c->c.env = e;
      28        12941 :   c->c.nupvalues = cast_byte(nelems);
      29        12941 :   return c;
      30              : }
      31              : 
      32              : 
      33         3351 : Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
      34         3351 :   Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
      35         3351 :   luaC_link(L, obj2gco(c), LUA_TFUNCTION);
      36         3351 :   c->l.isC = 0;
      37         3351 :   c->l.env = e;
      38         3351 :   c->l.nupvalues = cast_byte(nelems);
      39        11776 :   while (nelems--) c->l.upvals[nelems] = NULL;
      40         3351 :   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         6874 : UpVal *luaF_findupval (lua_State *L, StkId level) {
      54         6874 :   global_State *g = G(L);
      55         6874 :   GCObject **pp = &L->openupval;
      56              :   UpVal *p;
      57              :   UpVal *uv;
      58        15169 :   while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {
      59              :     lua_assert(p->v != &p->u.value);
      60         9460 :     if (p->v == level) {  /* found a corresponding upvalue? */
      61         1165 :       if (isdead(g, obj2gco(p)))  /* is it dead? */
      62            0 :         changewhite(obj2gco(p));  /* ressurect it */
      63         1165 :       return p;
      64              :     }
      65         8295 :     pp = &p->next;
      66              :   }
      67         5709 :   uv = luaM_new(L, UpVal);  /* not found: create a new one */
      68         5709 :   uv->tt = LUA_TUPVAL;
      69         5709 :   uv->marked = luaC_white(g);
      70         5709 :   uv->v = level;  /* current value lives in the stack */
      71         5709 :   uv->next = *pp;  /* chain it in the proper position */
      72         5709 :   *pp = obj2gco(uv);
      73         5709 :   uv->u.l.prev = &g->uvhead;  /* double link it in `uvhead' list */
      74         5709 :   uv->u.l.next = g->uvhead.u.l.next;
      75         5709 :   uv->u.l.next->u.l.prev = uv;
      76         5709 :   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         5709 :   return uv;
      79              : }
      80              : 
      81              : 
      82         5709 : 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         5709 :   uv->u.l.next->u.l.prev = uv->u.l.prev;  /* remove from `uvhead' list */
      85         5709 :   uv->u.l.prev->u.l.next = uv->u.l.next;
      86         5709 : }
      87              : 
      88              : 
      89         5583 : void luaF_freeupval (lua_State *L, UpVal *uv) {
      90         5583 :   if (uv->v != &uv->u.value)  /* is it open? */
      91            1 :     unlinkupval(uv);  /* remove from open list */
      92         5583 :   luaM_free(L, uv);  /* free upvalue */
      93         5583 : }
      94              : 
      95              : 
      96        10986 : void luaF_close (lua_State *L, StkId level) {
      97              :   UpVal *uv;
      98        10986 :   global_State *g = G(L);
      99        16695 :   while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {
     100         5709 :     GCObject *o = obj2gco(uv);
     101              :     lua_assert(!isblack(o) && uv->v != &uv->u.value);
     102         5709 :     L->openupval = uv->next;  /* remove from `open' list */
     103         5709 :     if (isdead(g, o))
     104            1 :       luaF_freeupval(L, uv);  /* free upvalue */
     105              :     else {
     106         5708 :       unlinkupval(uv);
     107         5708 :       setobj(L, &uv->u.value, uv->v);
     108         5708 :       uv->v = &uv->u.value;  /* now current value lives here */
     109         5708 :       luaC_linkupval(L, uv);  /* link upvalue into `gcroot' list */
     110              :     }
     111              :   }
     112        10986 : }
     113              : 
     114              : 
     115         2192 : Proto *luaF_newproto (lua_State *L) {
     116         2192 :   Proto *f = luaM_new(L, Proto);
     117         2192 :   luaC_link(L, obj2gco(f), LUA_TPROTO);
     118         2192 :   f->k = NULL;
     119         2192 :   f->sizek = 0;
     120         2192 :   f->p = NULL;
     121         2192 :   f->sizep = 0;
     122         2192 :   f->code = NULL;
     123         2192 :   f->sizecode = 0;
     124         2192 :   f->sizelineinfo = 0;
     125         2192 :   f->sizeupvalues = 0;
     126         2192 :   f->nups = 0;
     127         2192 :   f->upvalues = NULL;
     128         2192 :   f->numparams = 0;
     129         2192 :   f->is_vararg = 0;
     130         2192 :   f->maxstacksize = 0;
     131         2192 :   f->lineinfo = NULL;
     132         2192 :   f->sizelocvars = 0;
     133         2192 :   f->locvars = NULL;
     134         2192 :   f->linedefined = 0;
     135         2192 :   f->lastlinedefined = 0;
     136         2192 :   f->source = NULL;
     137         2192 :   return f;
     138              : }
     139              : 
     140              : 
     141         2006 : void luaF_freeproto (lua_State *L, Proto *f) {
     142         2006 :   luaM_freearray(L, f->code, f->sizecode, Instruction);
     143         2006 :   luaM_freearray(L, f->p, f->sizep, Proto *);
     144         2006 :   luaM_freearray(L, f->k, f->sizek, TValue);
     145         2006 :   luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
     146         2006 :   luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
     147         2006 :   luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
     148         2006 :   luaM_free(L, f);
     149         2006 : }
     150              : 
     151              : 
     152        14734 : void luaF_freeclosure (lua_State *L, Closure *c) {
     153        14734 :   int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
     154         3129 :                           sizeLclosure(c->l.nupvalues);
     155        14734 :   luaM_freemem(L, c, size);
     156        14734 : }
     157              : 
     158              : 
     159              : /*
     160              : ** Look for n-th local variable at line `line' in function `func'.
     161              : ** Returns NULL if not found.
     162              : */
     163          117 : const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
     164              :   int i;
     165          156 :   for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
     166           50 :     if (pc < f->locvars[i].endpc) {  /* is variable active? */
     167           33 :       local_number--;
     168           33 :       if (local_number == 0)
     169           11 :         return getstr(f->locvars[i].varname);
     170              :     }
     171              :   }
     172          106 :   return NULL;  /* not found */
     173              : }
     174              : 
        

Generated by: LCOV version 2.0-1