aboutsummaryrefslogtreecommitdiff
path: root/lstate.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-25 13:22:42 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-25 13:22:42 -0300
commit13635f7de7c51b26c447ce444a2c045cba83fe7c (patch)
treeb9461626b368e8ca1161b01e2771447898fdecd8 /lstate.c
parentd8a442206d2845e1749cc36dd59208428ef1d117 (diff)
downloadlua-13635f7de7c51b26c447ce444a2c045cba83fe7c.tar.gz
lua-13635f7de7c51b26c447ce444a2c045cba83fe7c.tar.bz2
lua-13635f7de7c51b26c447ce444a2c045cba83fe7c.zip
new version of protected execution
Diffstat (limited to 'lstate.c')
-rw-r--r--lstate.c49
1 files changed, 29 insertions, 20 deletions
diff --git a/lstate.c b/lstate.c
index d315e976..71671785 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.39 2000/09/12 18:42:32 roberto Exp roberto $ 2** $Id: lstate.c,v 1.40 2000/09/21 14:41:25 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -20,7 +20,7 @@
20 20
21 21
22#ifdef DEBUG 22#ifdef DEBUG
23extern lua_State *lua_state; 23static lua_State *lua_state = NULL;
24void luaB_opentests (lua_State *L); 24void luaB_opentests (lua_State *L);
25#endif 25#endif
26 26
@@ -38,8 +38,29 @@ static int errormessage (lua_State *L) {
38} 38}
39 39
40 40
41/*
42** open parts that may cause memory-allocation errors
43*/
44static void f_luaopen (lua_State *L, void *ud) {
45 int stacksize = *(int *)ud;
46 if (stacksize == 0)
47 stacksize = DEFAULT_STACK_SIZE;
48 else
49 stacksize += LUA_MINSTACK;
50 L->gt = luaH_new(L, 10);
51 luaD_init(L, stacksize);
52 luaS_init(L);
53 luaX_init(L);
54 luaT_init(L);
55 lua_register(L, LUA_ERRORMESSAGE, errormessage);
56#ifdef DEBUG
57 luaB_opentests(L);
58 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
59#endif
60}
61
62
41lua_State *lua_open (int stacksize) { 63lua_State *lua_open (int stacksize) {
42 struct lua_longjmp myErrorJmp;
43 lua_State *L = luaM_new(NULL, lua_State); 64 lua_State *L = luaM_new(NULL, lua_State);
44 if (L == NULL) return NULL; /* memory allocation error */ 65 if (L == NULL) return NULL; /* memory allocation error */
45 L->stack = NULL; 66 L->stack = NULL;
@@ -62,26 +83,14 @@ lua_State *lua_open (int stacksize) {
62 L->callhook = NULL; 83 L->callhook = NULL;
63 L->linehook = NULL; 84 L->linehook = NULL;
64 L->allowhooks = 1; 85 L->allowhooks = 1;
65 L->errorJmp = &myErrorJmp; 86 L->errorJmp = NULL;
66 if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */ 87 if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) {
67 L->gt = luaH_new(L, 10); 88 /* memory allocation error: free partial state */
68 luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
69 stacksize+LUA_MINSTACK);
70 luaS_init(L);
71 luaX_init(L);
72 luaT_init(L);
73 lua_register(L, LUA_ERRORMESSAGE, errormessage);
74#ifdef DEBUG
75 luaB_opentests(L);
76#endif
77 L->GCthreshold = L->nblocks*4;
78 L->errorJmp = NULL;
79 return L;
80 }
81 else { /* memory allocation error: free partial state */
82 lua_close(L); 89 lua_close(L);
83 return NULL; 90 return NULL;
84 } 91 }
92 L->GCthreshold = L->nblocks*4;
93 return L;
85} 94}
86 95
87 96