annotate lib/lua/lua-5.2.2/lfunc.c @ 2287:4e3236a4ea63

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