summaryrefslogtreecommitdiff
path: root/library/LuaTypes.cpp
diff options
context:
space:
mode:
authorQuietust2012-03-28 11:51:49 -0500
committerQuietust2012-03-28 11:51:49 -0500
commit95ec363a7f58df567dd9098e88cc1135524262f1 (patch)
tree9ffe0934a1953521b83ccea21c0d10b67e3bf653 /library/LuaTypes.cpp
parentabebb7fd2d178479422dfd6fe668c3881b58f904 (diff)
parent06188da380fe80cb7907a30b999075080b93615d (diff)
downloaddfhack-95ec363a7f58df567dd9098e88cc1135524262f1.tar.gz
dfhack-95ec363a7f58df567dd9098e88cc1135524262f1.tar.bz2
dfhack-95ec363a7f58df567dd9098e88cc1135524262f1.tar.xz
Merge branch 'master' of https://github.com/peterix/dfhack
Conflicts: library/LuaTypes.cpp library/LuaWrapper.cpp
Diffstat (limited to 'library/LuaTypes.cpp')
-rw-r--r--library/LuaTypes.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp
index 1e33ed71..76586bf7 100644
--- a/library/LuaTypes.cpp
+++ b/library/LuaTypes.cpp
@@ -1,4 +1,4 @@
-/*
+/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
@@ -168,10 +168,16 @@ static void autovivify_ptr(lua_State *state, int fname_idx, void **pptr,
{
lua_getfield(state, val_index, "new");
- if (!lua_isnil(state, -1))
+ // false or nil => bail out
+ if (!lua_toboolean(state, -1))
+ field_error(state, fname_idx, "null and autovivify not requested", "write");
+
+ // not 'true' => call df.new()
+ if (!lua_isboolean(state, -1))
{
int top = lua_gettop(state);
+ // Verify new points to a reasonable type of object
type_identity *suggested = get_object_identity(state, top, "autovivify", true, true);
if (!is_type_compatible(state, target, 0, suggested, top+1, false))
@@ -179,17 +185,22 @@ static void autovivify_ptr(lua_State *state, int fname_idx, void **pptr,
lua_pop(state, 1);
+ // Invoke df.new()
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_NEW_NAME);
lua_swap(state);
lua_call(state, 1, 1);
+ // Retrieve the pointer
void *nval = get_object_internal(state, target, top, false);
+ // shouldn't happen: this means suggested type is compatible,
+ // but its new() result isn't for some reason.
if (!nval)
field_error(state, fname_idx, "inconsistent autovivify type", "write");
*pptr = nval;
}
+ // otherwise use the target type
else
{
if (!target)