From d95cc3435fe249e1c86e5af36b8a10e9009edd97 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Fri, 20 Apr 2012 13:04:03 +0400 Subject: [PATCH] Fix lua wrapper sizeof for static arrays. Since it actually depends on the element type, it is more tricky. --- library/DataDefs.cpp | 12 +++++++----- library/LuaWrapper.cpp | 17 +++++++++++++++-- library/include/DataDefs.h | 2 +- library/include/DataIdentity.h | 4 +++- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/library/DataDefs.cpp b/library/DataDefs.cpp index 05988e419..062635182 100644 --- a/library/DataDefs.cpp +++ b/library/DataDefs.cpp @@ -42,13 +42,14 @@ using namespace DFHack; void *type_identity::do_allocate_pod() { - void *p = malloc(size); - memset(p, 0, size); + size_t sz = byte_size(); + void *p = malloc(sz); + memset(p, 0, sz); return p; } void type_identity::do_copy_pod(void *tgt, const void *src) { - memmove(tgt, src, size); + memmove(tgt, src, byte_size()); }; bool type_identity::do_destroy_pod(void *obj) { @@ -81,8 +82,9 @@ bool type_identity::destroy(void *obj) { } void *enum_identity::do_allocate() { - void *p = malloc(byte_size()); - memcpy(p, &first_item_value, std::min(byte_size(), sizeof(int64_t))); + size_t sz = byte_size(); + void *p = malloc(sz); + memcpy(p, &first_item_value, std::min(sz, sizeof(int64_t))); return p; } diff --git a/library/LuaWrapper.cpp b/library/LuaWrapper.cpp index 1c3b61c15..0b958f37c 100644 --- a/library/LuaWrapper.cpp +++ b/library/LuaWrapper.cpp @@ -542,10 +542,23 @@ static int meta_sizeof(lua_State *state) return 2; } - type_identity *id = get_object_identity(state, 1, "df.sizeof()", true); + type_identity *id = get_object_identity(state, 1, "df.sizeof()", true, true); - lua_pushinteger(state, id->byte_size()); + // Static arrays need special handling + if (id->type() == IDTYPE_BUFFER) + { + auto buf = (df::buffer_container_identity*)id; + type_identity *item = buf->getItemType(); + int count = buf->getSize(); + + fetch_container_details(state, lua_gettop(state), &item, &count); + + lua_pushinteger(state, item->byte_size() * count); + } + else + lua_pushinteger(state, id->byte_size()); + // Add the address if (lua_isuserdata(state, 1)) { lua_pushnumber(state, (size_t)get_object_ref(state, 1)); diff --git a/library/include/DataDefs.h b/library/include/DataDefs.h index d4d757d94..02f988248 100644 --- a/library/include/DataDefs.h +++ b/library/include/DataDefs.h @@ -83,7 +83,7 @@ namespace DFHack public: virtual ~type_identity() {} - size_t byte_size() { return size; } + virtual size_t byte_size() { return size; } virtual identity_type type() = 0; diff --git a/library/include/DataIdentity.h b/library/include/DataIdentity.h index a775c85b2..e4197a506 100644 --- a/library/include/DataIdentity.h +++ b/library/include/DataIdentity.h @@ -289,9 +289,11 @@ namespace df {} buffer_container_identity(int size, type_identity *item, enum_identity *ienum = NULL) - : container_identity(item->byte_size()*size, NULL, item, ienum), size(size) + : container_identity(0, NULL, item, ienum), size(size) {} + size_t byte_size() { return getItemType()->byte_size()*size; } + std::string getFullName(type_identity *item); int getSize() { return size; }