* Method: sizeof for DF object references. * * Returns: size[, address] */
| 580 | * Returns: size[, address] |
| 581 | */ |
| 582 | static int meta_sizeof(lua_State *state) |
| 583 | { |
| 584 | int argc = lua_gettop(state); |
| 585 | |
| 586 | if (argc != 1) |
| 587 | luaL_error(state, "Usage: object:sizeof() or df.sizeof(object)"); |
| 588 | |
| 589 | // Two special cases: nil and lightuserdata for NULL and void* |
| 590 | if (lua_isnil(state, 1) || lua_islightuserdata(state, 1)) |
| 591 | { |
| 592 | lua_pushnil(state); |
| 593 | lua_pushinteger(state, (size_t)lua_touserdata(state, 1)); |
| 594 | return 2; |
| 595 | } |
| 596 | |
| 597 | const type_identity *id = get_object_identity(state, 1, "df.sizeof()", true, true); |
| 598 | |
| 599 | // Static arrays need special handling |
| 600 | if (id->type() == IDTYPE_BUFFER) |
| 601 | { |
| 602 | auto buf = (const df::buffer_container_identity*)id; |
| 603 | const type_identity *item = buf->getItemType(); |
| 604 | int count = buf->getSize(); |
| 605 | |
| 606 | fetch_container_details(state, lua_gettop(state), &item, &count); |
| 607 | |
| 608 | lua_pushinteger(state, item->byte_size() * count); |
| 609 | } |
| 610 | else |
| 611 | lua_pushinteger(state, id->byte_size()); |
| 612 | |
| 613 | // Add the address |
| 614 | if (lua_isuserdata(state, 1)) |
| 615 | { |
| 616 | lua_pushinteger(state, (size_t)get_object_ref(state, 1)); |
| 617 | return 2; |
| 618 | } |
| 619 | else |
| 620 | return 1; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Method: displace for DF object references. |
nothing calls this directly
no test coverage detected