Removed a crash bug when the lambdas are absent

This commit is contained in:
2015-07-17 19:02:58 +10:00
parent 8b1a9aee91
commit 0c11e0a98b
7 changed files with 21 additions and 16 deletions
+3 -1
View File
@@ -6,7 +6,9 @@ pager = ...
---[[ ---[[
--BUG: RegionPagerLua fails without these --DOCS: These lambdas should return true or false, depending on if the operation succeeded or not
--DOCS: No return value given is recognized as a failure
--DOCS: OnCreate() and OnUnload() return values are currently ignored
region_pager.SetOnLoad(pager, function(r) region_pager.SetOnLoad(pager, function(r)
print("Calling SetOnLoad's lambda") print("Calling SetOnLoad's lambda")
end) end)
-3
View File
@@ -119,9 +119,6 @@ void Application::Quit() {
BaseScene::SetRenderer(nullptr); BaseScene::SetRenderer(nullptr);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
//TODO: is this necessary?
// SDL_Quit();
} }
//------------------------- //-------------------------
-2
View File
@@ -104,8 +104,6 @@ static int unloadRegion(lua_State* L) {
return 0; return 0;
} }
//TODO: (1) check that parameters are not null
static int setOnLoad(lua_State* L) { static int setOnLoad(lua_State* L) {
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1)); RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetLoadReference()); luaL_unref(L, LUA_REGISTRYINDEX, pager->GetLoadReference());
-2
View File
@@ -28,8 +28,6 @@ RegionPagerBase::~RegionPagerBase() {
UnloadAll(); UnloadAll();
}; };
//TODO: (1) add nullptr checks to the calls to GetRegion()
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
Region* ptr = GetRegion(x, y); Region* ptr = GetRegion(x, y);
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
+15 -7
View File
@@ -23,9 +23,6 @@
#include <stdexcept> #include <stdexcept>
//DOCS: Load, Save and Create fail unless the lua function has been set
//DOCS: UnloadIf and UnloadAll will still continue without the function set
RegionPagerLua::~RegionPagerLua() { RegionPagerLua::~RegionPagerLua() {
//unload all regions //unload all regions
UnloadAll(); UnloadAll();
@@ -44,6 +41,7 @@ Region* RegionPagerLua::LoadRegion(int x, int y) {
//check if this function is available //check if this function is available
if (lua_isnil(lua, -1)) { if (lua_isnil(lua, -1)) {
lua_pop(lua, 1); lua_pop(lua, 1);
//signal that there is no load function
return nullptr; return nullptr;
} }
@@ -57,17 +55,20 @@ Region* RegionPagerLua::LoadRegion(int x, int y) {
} }
//check the return value, success or failure //check the return value, success or failure
if (lua_toboolean(lua, -1)) { if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) {
lua_pop(lua, 1); lua_pop(lua, 1);
//push and return the loaded region
regionList.push_front(tmpRegion); regionList.push_front(tmpRegion);
return &regionList.front(); return &regionList.front();
} }
else { else {
lua_pop(lua, 1); lua_pop(lua, 1);
//signal a failure
return nullptr; return nullptr;
} }
} }
//NOTE: this return value seems strange; could replace it with a boolean
//return the saved region, or nullptr on failure //return the saved region, or nullptr on failure
Region* RegionPagerLua::SaveRegion(int x, int y) { Region* RegionPagerLua::SaveRegion(int x, int y) {
//get the pager's function from the registry //get the pager's function from the registry
@@ -76,6 +77,7 @@ Region* RegionPagerLua::SaveRegion(int x, int y) {
//check if this function is available //check if this function is available
if (lua_isnil(lua, -1)) { if (lua_isnil(lua, -1)) {
lua_pop(lua, 1); lua_pop(lua, 1);
//signal that the region wasn't saved
return nullptr; return nullptr;
} }
@@ -83,6 +85,7 @@ Region* RegionPagerLua::SaveRegion(int x, int y) {
Region* ptr = FindRegion(x, y); Region* ptr = FindRegion(x, y);
if (!ptr) { if (!ptr) {
lua_pop(lua, 1); lua_pop(lua, 1);
//signal that there is no save function
return nullptr; return nullptr;
} }
lua_pushlightuserdata(lua, ptr); lua_pushlightuserdata(lua, ptr);
@@ -93,17 +96,20 @@ Region* RegionPagerLua::SaveRegion(int x, int y) {
} }
//check the return value, success or failure //check the return value, success or failure
if (lua_toboolean(lua, -1)) { if (lua_isboolean(lua, -1) && lua_toboolean(lua, -1)) {
lua_pop(lua, 1); lua_pop(lua, 1);
//return the specified region that was saved
return ptr; return ptr;
} }
else { else {
lua_pop(lua, 1); lua_pop(lua, 1);
//signal a failure
return nullptr; return nullptr;
} }
} }
//return the created region, or nullptr on failure //DOCS: since this method is the last ditch call from GetRegion, it must return a valid region object, even if the create function hasn't been set.
//return a new region, throwing an error on failure
Region* RegionPagerLua::CreateRegion(int x, int y) { Region* RegionPagerLua::CreateRegion(int x, int y) {
if (FindRegion(x, y)) { if (FindRegion(x, y)) {
throw(std::logic_error("Cannot overwrite an existing region")); throw(std::logic_error("Cannot overwrite an existing region"));
@@ -115,7 +121,9 @@ Region* RegionPagerLua::CreateRegion(int x, int y) {
//check if this function is available //check if this function is available
if (lua_isnil(lua, -1)) { if (lua_isnil(lua, -1)) {
lua_pop(lua, 1); lua_pop(lua, 1);
return nullptr; //return an empty region object
regionList.emplace_front(x, y);
return &regionList.front();
} }
//something to work on //something to work on
+1 -1
View File
@@ -28,7 +28,7 @@
#include <functional> #include <functional>
#include <string> #include <string>
//NOTE: set the lua hook before use //DOCS: set the lua hook before use
class RegionPagerLua : public RegionPagerBase { class RegionPagerLua : public RegionPagerBase {
public: public:
+2
View File
@@ -27,6 +27,7 @@
#include <stdexcept> #include <stdexcept>
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::cout << "Beginning " << argv[0] << std::endl;
try { try {
Application app; Application app;
app.Init(argc, argv); app.Init(argc, argv);
@@ -37,5 +38,6 @@ int main(int argc, char** argv) {
std::cerr << "Fatal Error: " << e.what() << std::endl; std::cerr << "Fatal Error: " << e.what() << std::endl;
return 1; return 1;
} }
std::cout << "Clean exit from " << argv[0] << std::endl;
return 0; return 0;
} }