Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ceguilua/src/lua-5.0.3/lua/lfunc.c @ 1803

Last change on this file since 1803 was 1803, checked in by rgrieder, 16 years ago

added files for lua 5.1.3, lua 5.0.3, CEGUILua-0.6.1 and CEGUILua-0.5.0b

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1/*
2** $Id: lfunc.c,v 1.67a 2003/03/18 12:50:04 roberto Exp $
3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9
10#define lfunc_c
11
12#include "lua.h"
13
14#include "lfunc.h"
15#include "lgc.h"
16#include "lmem.h"
17#include "lobject.h"
18#include "lstate.h"
19
20
21Closure *luaF_newCclosure (lua_State *L, int nelems) {
22  Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
23  luaC_link(L, valtogco(c), LUA_TFUNCTION);
24  c->c.isC = 1;
25  c->c.nupvalues = cast(lu_byte, nelems);
26  return c;
27}
28
29
30Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e) {
31  Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
32  luaC_link(L, valtogco(c), LUA_TFUNCTION);
33  c->l.isC = 0;
34  c->l.g = *e;
35  c->l.nupvalues = cast(lu_byte, nelems);
36  return c;
37}
38
39
40UpVal *luaF_findupval (lua_State *L, StkId level) {
41  GCObject **pp = &L->openupval;
42  UpVal *p;
43  UpVal *v;
44  while ((p = ngcotouv(*pp)) != NULL && p->v >= level) {
45    if (p->v == level) return p;
46    pp = &p->next;
47  }
48  v = luaM_new(L, UpVal);  /* not found: create a new one */
49  v->tt = LUA_TUPVAL;
50  v->marked = 1;  /* open upvalues should not be collected */
51  v->v = level;  /* current value lives in the stack */
52  v->next = *pp;  /* chain it in the proper position */
53  *pp = valtogco(v);
54  return v;
55}
56
57
58void luaF_close (lua_State *L, StkId level) {
59  UpVal *p;
60  while ((p = ngcotouv(L->openupval)) != NULL && p->v >= level) {
61    setobj(&p->value, p->v);  /* save current value (write barrier) */
62    p->v = &p->value;  /* now current value lives here */
63    L->openupval = p->next;  /* remove from `open' list */
64    luaC_link(L, valtogco(p), LUA_TUPVAL);
65  }
66}
67
68
69Proto *luaF_newproto (lua_State *L) {
70  Proto *f = luaM_new(L, Proto);
71  luaC_link(L, valtogco(f), LUA_TPROTO);
72  f->k = NULL;
73  f->sizek = 0;
74  f->p = NULL;
75  f->sizep = 0;
76  f->code = NULL;
77  f->sizecode = 0;
78  f->sizelineinfo = 0;
79  f->sizeupvalues = 0;
80  f->nups = 0;
81  f->upvalues = NULL;
82  f->numparams = 0;
83  f->is_vararg = 0;
84  f->maxstacksize = 0;
85  f->lineinfo = NULL;
86  f->sizelocvars = 0;
87  f->locvars = NULL;
88  f->lineDefined = 0;
89  f->source = NULL;
90  return f;
91}
92
93
94void luaF_freeproto (lua_State *L, Proto *f) {
95  luaM_freearray(L, f->code, f->sizecode, Instruction);
96  luaM_freearray(L, f->p, f->sizep, Proto *);
97  luaM_freearray(L, f->k, f->sizek, TObject);
98  luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
99  luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
100  luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
101  luaM_freelem(L, f);
102}
103
104
105void luaF_freeclosure (lua_State *L, Closure *c) {
106  int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
107                          sizeLclosure(c->l.nupvalues);
108  luaM_free(L, c, size);
109}
110
111
112/*
113** Look for n-th local variable at line `line' in function `func'.
114** Returns NULL if not found.
115*/
116const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
117  int i;
118  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
119    if (pc < f->locvars[i].endpc) {  /* is variable active? */
120      local_number--;
121      if (local_number == 0)
122        return getstr(f->locvars[i].varname);
123    }
124  }
125  return NULL;  /* not found */
126}
127
Note: See TracBrowser for help on using the repository browser.