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