Imported from ../lua-3.1.tar.gz.
[lua.git] / src / lstate.c
blob43015b97a80af088357bef5efc75017b03af53d1
1 /*
2 ** $Id: lstate.c,v 1.6 1998/06/02 20:37:04 roberto Exp $
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
8 #include "lbuiltin.h"
9 #include "ldo.h"
10 #include "lfunc.h"
11 #include "lgc.h"
12 #include "llex.h"
13 #include "lmem.h"
14 #include "lstate.h"
15 #include "lstring.h"
16 #include "ltable.h"
17 #include "ltm.h"
20 lua_State *lua_state = NULL;
23 void lua_open (void)
25 if (lua_state) return;
26 lua_state = luaM_new(lua_State);
27 L->numCblocks = 0;
28 L->Cstack.base = 0;
29 L->Cstack.lua2C = 0;
30 L->Cstack.num = 0;
31 L->errorJmp = NULL;
32 L->rootproto.next = NULL;
33 L->rootproto.marked = 0;
34 L->rootcl.next = NULL;
35 L->rootcl.marked = 0;
36 L->rootglobal.next = NULL;
37 L->rootglobal.marked = 0;
38 L->roottable.next = NULL;
39 L->roottable.marked = 0;
40 L->refArray = NULL;
41 L->refSize = 0;
42 L->Mbuffsize = 0;
43 L->Mbuffnext = 0;
44 L->Mbuffbase = NULL;
45 L->Mbuffer = NULL;
46 L->GCthreshold = GARBAGE_BLOCK;
47 L->nblocks = 0;
48 luaD_init();
49 luaS_init();
50 luaX_init();
51 luaT_init();
52 luaB_predefine();
56 void lua_close (void)
58 TaggedString *alludata = luaS_collectudata();
59 L->GCthreshold = MAX_INT; /* to avoid GC during GC */
60 luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
61 luaC_strcallIM(alludata); /* GC tag methods for userdata */
62 luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
63 luaH_free((Hash *)L->roottable.next);
64 luaF_freeproto((TProtoFunc *)L->rootproto.next);
65 luaF_freeclosure((Closure *)L->rootcl.next);
66 luaS_free(alludata);
67 luaS_freeall();
68 luaM_free(L->stack.stack);
69 luaM_free(L->IMtable);
70 luaM_free(L->refArray);
71 luaM_free(L->Mbuffer);
72 luaM_free(L);
73 L = NULL;
74 #ifdef DEBUG
75 printf("total de blocos: %ld\n", numblocks);
76 printf("total de memoria: %ld\n", totalmem);
77 #endif
81 lua_State *lua_setstate (lua_State *st) {
82 lua_State *old = lua_state;
83 lua_state = st;
84 return old;