Reset thread before panicking

Before panicking, it is simpler to reset the thread instead of closing
its variables and adjust the top manually.
diff --git a/ldo.c b/ldo.c
index 59391f7..d39edab 100644
--- a/ldo.c
+++ b/ldo.c
@@ -119,17 +119,13 @@
   }
   else {  /* thread has no error handler */
     global_State *g = G(L);
-    errcode = luaD_closeprotected(L, 0, errcode);  /* close all upvalues */
-    L->status = cast_byte(errcode);  /* mark it as dead */
+    errcode = luaE_resetthread(L, errcode);  /* close all upvalues */
     if (g->mainthread->errorJmp) {  /* main thread has a handler? */
       setobjs2s(L, g->mainthread->top++, L->top - 1);  /* copy error obj. */
       luaD_throw(g->mainthread, errcode);  /* re-throw in main thread */
     }
     else {  /* no handler at all; abort */
       if (g->panic) {  /* panic function? */
-        luaD_seterrorobj(L, errcode, L->top);  /* assume EXTRA_STACK */
-        if (L->ci->top < L->top)
-          L->ci->top = L->top;  /* pushing msg. can break this invariant */
         lua_unlock(L);
         g->panic(L);  /* call panic function (last chance to jump out) */
       }
diff --git a/lstate.c b/lstate.c
index e01a7e7..a6ef82a 100644
--- a/lstate.c
+++ b/lstate.c
@@ -321,11 +321,8 @@
 }
 
 
-int lua_resetthread (lua_State *L) {
-  CallInfo *ci;
-  int status = L->status;
-  lua_lock(L);
-  L->ci = ci = &L->base_ci;  /* unwind CallInfo list */
+int luaE_resetthread (lua_State *L, int status) {
+  CallInfo *ci = L->ci = &L->base_ci;  /* unwind CallInfo list */
   setnilvalue(s2v(L->stack));  /* 'function' entry for basic 'ci' */
   ci->func = L->stack;
   ci->callstatus = CIST_C;
@@ -334,12 +331,19 @@
   status = luaD_closeprotected(L, 0, status);
   if (status != LUA_OK)  /* errors? */
     luaD_seterrorobj(L, status, L->stack + 1);
-  else {
-    status = LUA_OK;
+  else
     L->top = L->stack + 1;
-  }
   ci->top = L->top + LUA_MINSTACK;
-  L->status = status;
+  L->status = cast_byte(status);
+  luaD_reallocstack(L, cast_int(ci->top - L->stack), 0);
+  return status;
+}
+
+
+LUA_API int lua_resetthread (lua_State *L) {
+  int status;
+  lua_lock(L);
+  status = luaE_resetthread(L, L->status);
   lua_unlock(L);
   return status;
 }
diff --git a/lstate.h b/lstate.h
index cbcf07e..38a6c9b 100644
--- a/lstate.h
+++ b/lstate.h
@@ -359,6 +359,7 @@
 LUAI_FUNC void luaE_incCstack (lua_State *L);
 LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
 LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
+LUAI_FUNC int luaE_resetthread (lua_State *L, int status);
 
 
 #endif