Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lstate.c
blob3b98d72976ee81a59c63fa7e93c4887f25b2bdb7
1 /*
2 ** $Id: lstate.c,v 1.12 1999/05/11 20:08:20 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->Cstack.base = 0;
28 L->Cstack.lua2C = 0;
29 L->Cstack.num = 0;
30 L->errorJmp = NULL;
31 L->Mbuffer = NULL;
32 L->Mbuffbase = 0;
33 L->Mbuffsize = 0;
34 L->Mbuffnext = 0;
35 L->Cblocks = NULL;
36 L->numCblocks = 0;
37 L->debug = 0;
38 L->callhook = NULL;
39 L->linehook = NULL;
40 L->rootproto.next = NULL;
41 L->rootproto.marked = 0;
42 L->rootcl.next = NULL;
43 L->rootcl.marked = 0;
44 L->rootglobal.next = NULL;
45 L->rootglobal.marked = 0;
46 L->roottable.next = NULL;
47 L->roottable.marked = 0;
48 L->IMtable = NULL;
49 L->refArray = NULL;
50 L->refSize = 0;
51 L->GCthreshold = GARBAGE_BLOCK;
52 L->nblocks = 0;
53 luaD_init();
54 luaS_init();
55 luaX_init();
56 luaT_init();
57 luaB_predefine();
61 void lua_close (void)
63 TaggedString *alludata = luaS_collectudata();
64 L->GCthreshold = MAX_INT; /* to avoid GC during GC */
65 luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
66 luaC_strcallIM(alludata); /* GC tag methods for userdata */
67 luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
68 luaH_free((Hash *)L->roottable.next);
69 luaF_freeproto((TProtoFunc *)L->rootproto.next);
70 luaF_freeclosure((Closure *)L->rootcl.next);
71 luaS_free(alludata);
72 luaS_freeall();
73 luaM_free(L->stack.stack);
74 luaM_free(L->IMtable);
75 luaM_free(L->refArray);
76 luaM_free(L->Mbuffer);
77 luaM_free(L->Cblocks);
78 luaM_free(L);
79 L = NULL;
80 #ifdef DEBUG
81 printf("total de blocos: %ld\n", numblocks);
82 printf("total de memoria: %ld\n", totalmem);
83 #endif