aboutsummaryrefslogtreecommitdiff
path: root/lglobal.c
diff options
context:
space:
mode:
Diffstat (limited to 'lglobal.c')
-rw-r--r--lglobal.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/lglobal.c b/lglobal.c
new file mode 100644
index 00000000..9f2b9f90
--- /dev/null
+++ b/lglobal.c
@@ -0,0 +1,71 @@
1/*
2** $Id: $
3** Global variables
4** See Copyright Notice in lua.h
5*/
6
7#include <stdlib.h>
8
9#include "lbuiltin.h"
10#include "lglobal.h"
11#include "lmem.h"
12#include "lobject.h"
13#include "lstring.h"
14
15
16Symbol *luaG_global = NULL;
17int luaG_nglobal = 0;
18static int maxglobal = 0;
19
20
21
22Word luaG_findsymbol (TaggedString *t)
23{
24 if (maxglobal == 0) { /* first time? */
25 maxglobal = 50;
26 luaG_global = luaM_newvector(maxglobal, Symbol);
27 luaB_predefine();
28 }
29 if (t->u.s.varindex == NOT_USED) {
30 if (!t->marked) t->marked = 2; /* avoid GC of global variable names */
31 if (luaG_nglobal >= maxglobal)
32 maxglobal = luaM_growvector(&luaG_global, maxglobal, Symbol,
33 symbolEM, MAX_WORD);
34 t->u.s.varindex = luaG_nglobal;
35 luaG_global[luaG_nglobal].varname = t;
36 s_ttype(luaG_nglobal) = LUA_T_NIL;
37 luaG_nglobal++;
38 }
39 return t->u.s.varindex;
40}
41
42
43Word luaG_findsymbolbyname (char *name)
44{
45 return luaG_findsymbol(luaS_new(name));
46}
47
48
49int luaG_globaldefined (char *name)
50{
51 return s_ttype(luaG_findsymbolbyname(name)) != LUA_T_NIL;
52}
53
54
55int luaG_nextvar (Word next)
56{
57 while (next < luaG_nglobal && s_ttype(next) == LUA_T_NIL)
58 next++;
59 return (next < luaG_nglobal ? next : -1);
60}
61
62
63char *luaG_travsymbol (int (*fn)(TObject *))
64{
65 int i;
66 for (i=0; i<luaG_nglobal; i++)
67 if (fn(&s_object(i)))
68 return luaG_global[i].varname->str;
69 return NULL;
70}
71