summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/BLua.h23
-rw-r--r--lib/BLua.cc7
2 files changed, 19 insertions, 11 deletions
diff --git a/include/BLua.h b/include/BLua.h
index c12a8b7..1cc74f4 100644
--- a/include/BLua.h
+++ b/include/BLua.h
@@ -82,21 +82,22 @@ class Lua : public Base {
void declarefunc(const String &, lua_CFunction, int = LUA_GLOBALSINDEX);
void call(const String &, int = LUA_GLOBALSINDEX, int = 0, int = 0);
void call(int = 0, int = 0) throw (GeneralException);
- void push() { lua_pushnil(L); }
- void push(lua_Number n) { lua_pushnumber(L, n); }
- void push(const String & s) { lua_pushlstring(L, s.to_charp(), s.strlen()); }
- void push(bool b) { lua_pushboolean(L, b); }
+ void push() { checkstack(); lua_pushnil(L); }
+ void push(lua_Number n) { checkstack(); lua_pushnumber(L, n); }
+ void push(const String & s) { checkstack(); lua_pushlstring(L, s.to_charp(), s.strlen()); }
+ void push(bool b) { checkstack(); lua_pushboolean(L, b); }
void push(const char *, int size = -1);
- void push(void * p) { lua_pushlightuserdata(L, p); }
- void push(lua_CFunction f, int n = 0) { lua_pushcclosure(L, f, n); }
+ void push(void * p) { checkstack(); lua_pushlightuserdata(L, p); }
+ void push(lua_CFunction f, int n = 0) { checkstack(); lua_pushcclosure(L, f, n); }
void pop(int n = 1) { lua_pop(L, n); }
int next(int t = -2) { lua_next(L, t); }
- void copy(int n = -1) { lua_pushvalue(L, n); }
+ int checkstack(int extra = 1) { return lua_checkstack(L, extra); }
+ void copy(int n = -1) { checkstack(); lua_pushvalue(L, n); }
void remove(int n = 1) { lua_remove(L, n); }
- void insert(int n = 1) { lua_insert(L, n); }
+ void insert(int n = 1) { checkstack(); lua_insert(L, n); }
void replace(int n = 1) { lua_replace(L, n); }
- void newtable() { lua_newtable(L); }
- void * newuser(size_t s) { return lua_newuserdata(L, s); }
+ void newtable() { checkstack(); lua_newtable(L); }
+ void * newuser(size_t s) { checkstack(); return lua_newuserdata(L, s); }
void settable(int = -3, bool raw = false);
void gettable(int = -2, bool raw = false);
void setvar() { lua_settable(L, LUA_GLOBALSINDEX); }
@@ -138,7 +139,7 @@ class Lua : public Base {
static Lua * find(lua_State *) throw (GeneralException);
void showstack(int level = M_INFO);
void showerror();
- int getmetatable(int i = -1) { return lua_getmetatable(L, i); }
+ int getmetatable(int i = -1) { checkstack(); return lua_getmetatable(L, i); }
int setmetatable(int i = -2) { return lua_setmetatable(L, i); }
int sethook(lua_Hook func, int mask, int count) { return lua_sethook(L, func, mask, count); }
diff --git a/lib/BLua.cc b/lib/BLua.cc
index ba5497a..8f9f59d 100644
--- a/lib/BLua.cc
+++ b/lib/BLua.cc
@@ -642,12 +642,14 @@ void Lua::open_bit() {
}
void Lua::declarefunc(const String & name, lua_CFunction f, int i) {
+ checkstack(2);
lua_pushstring(L, name.to_charp());
lua_pushcfunction(L, f);
lua_settable(L, i);
}
void Lua::call(const String & f, int i, int nargs, int nresults) {
+ checkstack(1);
lua_pushstring(L, f.to_charp());
lua_gettable(L, i);
lua_insert(L, -1 - nargs - nresults);
@@ -657,6 +659,7 @@ void Lua::call(const String & f, int i, int nargs, int nresults) {
void Lua::call(int nargs, int nresults) throw(GeneralException) {
int r;
+ checkstack(1);
lua_pushcfunction(L, LuaStatics::luaerror);
lua_insert(L, 1);
_protected = true;
@@ -681,6 +684,7 @@ void Lua::call(int nargs, int nresults) throw(GeneralException) {
}
void Lua::push(const char * s, int size) {
+ checkstack();
if (size < 0) {
lua_pushstring(L, s);
} else {
@@ -830,6 +834,7 @@ void Lua::load(Handle * h, bool docall) throw (GeneralException) {
lf.f = h;
+ checkstack();
status = lua_load(L, LuaStatics::getF, &lf, h->GetName().to_charp());
if (status) {
@@ -895,6 +900,7 @@ void Lua::dumpvars_r(Handle * h, int i, int depth) throw (GeneralException) {
depth++;
+ checkstack();
while(lua_next(L, i) != 0) {
if (lua_type(L, -2) == LUA_TSTRING) {
if (String(lua_tostring(L, -2)) == String("_G")) {
@@ -999,6 +1005,7 @@ void Lua::dumpvars_r(Handle * h, int i, int depth) throw (GeneralException) {
}
Lua * Lua::thread(bool saveit) {
+ checkstack();
lua_State * L1 = lua_newthread(L);
Lua * Lt = find(L1);
if (saveit) {