aboutsummaryrefslogtreecommitdiff
path: root/lglobal.c
blob: 9f2b9f90c53a724129432b02e889d6267c0d868b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
** $Id: $
** Global variables
** See Copyright Notice in lua.h
*/

#include <stdlib.h>

#include "lbuiltin.h"
#include "lglobal.h"
#include "lmem.h"
#include "lobject.h"
#include "lstring.h"


Symbol *luaG_global = NULL;
int luaG_nglobal = 0;
static int maxglobal = 0;



Word luaG_findsymbol (TaggedString *t)
{
  if (maxglobal == 0) {  /* first time? */
    maxglobal = 50;
    luaG_global = luaM_newvector(maxglobal, Symbol);
    luaB_predefine();
  }
  if (t->u.s.varindex == NOT_USED) {
    if (!t->marked) t->marked = 2;  /* avoid GC of global variable names */
    if (luaG_nglobal >= maxglobal)
      maxglobal = luaM_growvector(&luaG_global, maxglobal, Symbol,
                             symbolEM, MAX_WORD);
    t->u.s.varindex = luaG_nglobal;
    luaG_global[luaG_nglobal].varname = t;
    s_ttype(luaG_nglobal) = LUA_T_NIL;
    luaG_nglobal++;
  }
  return t->u.s.varindex;
}


Word luaG_findsymbolbyname (char *name)
{
  return luaG_findsymbol(luaS_new(name));
}


int luaG_globaldefined (char *name)
{
  return s_ttype(luaG_findsymbolbyname(name)) != LUA_T_NIL;
}


int luaG_nextvar (Word next)
{
  while (next < luaG_nglobal && s_ttype(next) == LUA_T_NIL)
    next++;
  return (next < luaG_nglobal ? next : -1);
}


char *luaG_travsymbol (int (*fn)(TObject *))
{
  int i;
  for (i=0; i<luaG_nglobal; i++)
    if (fn(&s_object(i)))
      return luaG_global[i].varname->str;
  return NULL;
}