xref: /illumos-gate/usr/src/uts/common/fs/zfs/lua/lfunc.c (revision dec98d2aa2912b1fbd0abd2574307a695b566692)
1 /*
2 ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $
3 ** Auxiliary functions to manipulate prototypes and closures
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #include <sys/zfs_context.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 Closure *luaF_newCclosure (lua_State *L, int n) {
24   Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl;
25   c->c.nupvalues = cast_byte(n);
26   return c;
27 }
28 
29 
30 Closure *luaF_newLclosure (lua_State *L, int n) {
31   Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl;
32   c->l.p = NULL;
33   c->l.nupvalues = cast_byte(n);
34   while (n--) c->l.upvals[n] = NULL;
35   return c;
36 }
37 
38 
39 UpVal *luaF_newupval (lua_State *L) {
40   UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
41   uv->v = &uv->u.value;
42   setnilvalue(uv->v);
43   return uv;
44 }
45 
46 
47 UpVal *luaF_findupval (lua_State *L, StkId level) {
48   global_State *g = G(L);
49   GCObject **pp = &L->openupval;
50   UpVal *p;
51   UpVal *uv;
52   while (*pp != NULL && (p = gco2uv(*pp))->v >= level) {
53     GCObject *o = obj2gco(p);
54     lua_assert(p->v != &p->u.value);
55     lua_assert(!isold(o) || isold(obj2gco(L)));
56     if (p->v == level) {  /* found a corresponding upvalue? */
57       if (isdead(g, o))  /* is it dead? */
58         changewhite(o);  /* resurrect it */
59       return p;
60     }
61     pp = &p->next;
62   }
63   /* not found: create a new one */
64   uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv;
65   uv->v = level;  /* current value lives in the stack */
66   uv->u.l.prev = &g->uvhead;  /* double link it in `uvhead' list */
67   uv->u.l.next = g->uvhead.u.l.next;
68   uv->u.l.next->u.l.prev = uv;
69   g->uvhead.u.l.next = uv;
70   lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
71   return uv;
72 }
73 
74 
75 static void unlinkupval (UpVal *uv) {
76   lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
77   uv->u.l.next->u.l.prev = uv->u.l.prev;  /* remove from `uvhead' list */
78   uv->u.l.prev->u.l.next = uv->u.l.next;
79 }
80 
81 
82 void luaF_freeupval (lua_State *L, UpVal *uv) {
83   if (uv->v != &uv->u.value)  /* is it open? */
84     unlinkupval(uv);  /* remove from open list */
85   luaM_free(L, uv);  /* free upvalue */
86 }
87 
88 
89 void luaF_close (lua_State *L, StkId level) {
90   UpVal *uv;
91   global_State *g = G(L);
92   while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) {
93     GCObject *o = obj2gco(uv);
94     lua_assert(!isblack(o) && uv->v != &uv->u.value);
95     L->openupval = uv->next;  /* remove from `open' list */
96     if (isdead(g, o))
97       luaF_freeupval(L, uv);  /* free upvalue */
98     else {
99       unlinkupval(uv);  /* remove upvalue from 'uvhead' list */
100       setobj(L, &uv->u.value, uv->v);  /* move value to upvalue slot */
101       uv->v = &uv->u.value;  /* now current value lives here */
102       gch(o)->next = g->allgc;  /* link upvalue into 'allgc' list */
103       g->allgc = o;
104       luaC_checkupvalcolor(g, uv);
105     }
106   }
107 }
108 
109 
110 Proto *luaF_newproto (lua_State *L) {
111   Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
112   f->k = NULL;
113   f->sizek = 0;
114   f->p = NULL;
115   f->sizep = 0;
116   f->code = NULL;
117   f->cache = NULL;
118   f->sizecode = 0;
119   f->lineinfo = NULL;
120   f->sizelineinfo = 0;
121   f->upvalues = NULL;
122   f->sizeupvalues = 0;
123   f->numparams = 0;
124   f->is_vararg = 0;
125   f->maxstacksize = 0;
126   f->locvars = NULL;
127   f->sizelocvars = 0;
128   f->linedefined = 0;
129   f->lastlinedefined = 0;
130   f->source = NULL;
131   return f;
132 }
133 
134 
135 void luaF_freeproto (lua_State *L, Proto *f) {
136   luaM_freearray(L, f->code, f->sizecode);
137   luaM_freearray(L, f->p, f->sizep);
138   luaM_freearray(L, f->k, f->sizek);
139   luaM_freearray(L, f->lineinfo, f->sizelineinfo);
140   luaM_freearray(L, f->locvars, f->sizelocvars);
141   luaM_freearray(L, f->upvalues, f->sizeupvalues);
142   luaM_free(L, f);
143 }
144 
145 
146 /*
147 ** Look for n-th local variable at line `line' in function `func'.
148 ** Returns NULL if not found.
149 */
150 const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
151   int i;
152   for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
153     if (pc < f->locvars[i].endpc) {  /* is variable active? */
154       local_number--;
155       if (local_number == 0)
156         return getstr(f->locvars[i].varname);
157     }
158   }
159   return NULL;  /* not found */
160 }
161 
162