aboutsummaryrefslogtreecommitdiff
path: root/src/lj_state.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lj_state.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/lj_state.c b/src/lj_state.c
index 8c53d37f..8fd11b2f 100644
--- a/src/lj_state.c
+++ b/src/lj_state.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#include "lj_tab.h" 17#include "lj_tab.h"
17#include "lj_func.h" 18#include "lj_func.h"
@@ -26,6 +27,7 @@
26#include "lj_vm.h" 27#include "lj_vm.h"
27#include "lj_lex.h" 28#include "lj_lex.h"
28#include "lj_alloc.h" 29#include "lj_alloc.h"
30#include "luajit.h"
29 31
30/* -- Stack handling ------------------------------------------------------ */ 32/* -- Stack handling ------------------------------------------------------ */
31 33
@@ -59,7 +61,7 @@ static void resizestack(lua_State *L, MSize n)
59 GCobj *up; 61 GCobj *up;
60 lua_assert((MSize)(tvref(L->maxstack)-oldst)==L->stacksize-LJ_STACK_EXTRA-1); 62 lua_assert((MSize)(tvref(L->maxstack)-oldst)==L->stacksize-LJ_STACK_EXTRA-1);
61 st = (TValue *)lj_mem_realloc(L, tvref(L->stack), 63 st = (TValue *)lj_mem_realloc(L, tvref(L->stack),
62 (MSize)(L->stacksize*sizeof(TValue)), 64 (MSize)(oldsize*sizeof(TValue)),
63 (MSize)(realsize*sizeof(TValue))); 65 (MSize)(realsize*sizeof(TValue)));
64 setmref(L->stack, st); 66 setmref(L->stack, st);
65 delta = (char *)st - (char *)oldst; 67 delta = (char *)st - (char *)oldst;
@@ -67,12 +69,12 @@ static void resizestack(lua_State *L, MSize n)
67 while (oldsize < realsize) /* Clear new slots. */ 69 while (oldsize < realsize) /* Clear new slots. */
68 setnilV(st + oldsize++); 70 setnilV(st + oldsize++);
69 L->stacksize = realsize; 71 L->stacksize = realsize;
72 if ((size_t)(mref(G(L)->jit_base, char) - (char *)oldst) < oldsize)
73 setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta);
70 L->base = (TValue *)((char *)L->base + delta); 74 L->base = (TValue *)((char *)L->base + delta);
71 L->top = (TValue *)((char *)L->top + delta); 75 L->top = (TValue *)((char *)L->top + delta);
72 for (up = gcref(L->openupval); up != NULL; up = gcnext(up)) 76 for (up = gcref(L->openupval); up != NULL; up = gcnext(up))
73 setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta)); 77 setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta));
74 if (obj2gco(L) == gcref(G(L)->jit_L))
75 setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta);
76} 78}
77 79
78/* Relimit stack after error, in case the limit was overdrawn. */ 80/* Relimit stack after error, in case the limit was overdrawn. */
@@ -89,7 +91,8 @@ void lj_state_shrinkstack(lua_State *L, MSize used)
89 return; /* Avoid stack shrinking while handling stack overflow. */ 91 return; /* Avoid stack shrinking while handling stack overflow. */
90 if (4*used < L->stacksize && 92 if (4*used < L->stacksize &&
91 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize && 93 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize &&
92 obj2gco(L) != gcref(G(L)->jit_L)) /* Don't shrink stack of live trace. */ 94 /* Don't shrink stack of live trace. */
95 (tvref(G(L)->jit_base) == NULL || obj2gco(L) != gcref(G(L)->cur_L)))
93 resizestack(L, L->stacksize >> 1); 96 resizestack(L, L->stacksize >> 1);
94} 97}
95 98
@@ -164,7 +167,7 @@ static void close_state(lua_State *L)
164 lj_ctype_freestate(g); 167 lj_ctype_freestate(g);
165#endif 168#endif
166 lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); 169 lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef);
167 lj_str_freebuf(g, &g->tmpbuf); 170 lj_buf_free(g, &g->tmpbuf);
168 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); 171 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);
169 lua_assert(g->gc.total == sizeof(GG_State)); 172 lua_assert(g->gc.total == sizeof(GG_State));
170#ifndef LUAJIT_USE_SYSMALLOC 173#ifndef LUAJIT_USE_SYSMALLOC
@@ -203,7 +206,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
203 setnilV(&g->nilnode.val); 206 setnilV(&g->nilnode.val);
204 setnilV(&g->nilnode.key); 207 setnilV(&g->nilnode.key);
205 setmref(g->nilnode.freetop, &g->nilnode); 208 setmref(g->nilnode.freetop, &g->nilnode);
206 lj_str_initbuf(&g->tmpbuf); 209 lj_buf_init(NULL, &g->tmpbuf);
207 g->gc.state = GCSpause; 210 g->gc.state = GCSpause;
208 setgcref(g->gc.root, obj2gco(L)); 211 setgcref(g->gc.root, obj2gco(L));
209 setmref(g->gc.sweep, &g->gc.root); 212 setmref(g->gc.sweep, &g->gc.root);
@@ -236,6 +239,10 @@ LUA_API void lua_close(lua_State *L)
236 global_State *g = G(L); 239 global_State *g = G(L);
237 int i; 240 int i;
238 L = mainthread(g); /* Only the main thread can be closed. */ 241 L = mainthread(g); /* Only the main thread can be closed. */
242#if LJ_HASPROFILE
243 luaJIT_profile_stop(L);
244#endif
245 setgcrefnull(g->cur_L);
239 lj_func_closeuv(L, tvref(L->stack)); 246 lj_func_closeuv(L, tvref(L->stack));
240 lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */ 247 lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */
241#if LJ_HASJIT 248#if LJ_HASJIT
@@ -246,8 +253,8 @@ LUA_API void lua_close(lua_State *L)
246 for (i = 0;;) { 253 for (i = 0;;) {
247 hook_enter(g); 254 hook_enter(g);
248 L->status = 0; 255 L->status = 0;
249 L->cframe = NULL;
250 L->base = L->top = tvref(L->stack) + 1; 256 L->base = L->top = tvref(L->stack) + 1;
257 L->cframe = NULL;
251 if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) { 258 if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) {
252 if (++i >= 10) break; 259 if (++i >= 10) break;
253 lj_gc_separateudata(g, 1); /* Separate udata again. */ 260 lj_gc_separateudata(g, 1); /* Separate udata again. */
@@ -279,6 +286,8 @@ lua_State *lj_state_new(lua_State *L)
279void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L) 286void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L)
280{ 287{
281 lua_assert(L != mainthread(g)); 288 lua_assert(L != mainthread(g));
289 if (obj2gco(L) == gcref(g->cur_L))
290 setgcrefnull(g->cur_L);
282 lj_func_closeuv(L, tvref(L->stack)); 291 lj_func_closeuv(L, tvref(L->stack));
283 lua_assert(gcref(L->openupval) == NULL); 292 lua_assert(gcref(L->openupval) == NULL);
284 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); 293 lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue);