Fix lua wrapper sizeof for static arrays.

Since it actually depends on the element type, it is more tricky.
develop
Alexander Gavrilov 2012-04-20 13:04:03 +04:00
parent 0b32d374db
commit d95cc3435f
4 changed files with 26 additions and 9 deletions

@ -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;
}

@ -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));

@ -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;

@ -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; }