* Method: displace for DF object references. * * Returns: a reference with the same type, but modified address */
| 626 | * Returns: a reference with the same type, but modified address |
| 627 | */ |
| 628 | static int meta_displace(lua_State *state) |
| 629 | { |
| 630 | int argc = lua_gettop(state); |
| 631 | |
| 632 | bool has_step = (argc >= 3); |
| 633 | if ((argc < 2 || argc > 3) || |
| 634 | !lua_isnumber(state, 2) || |
| 635 | (has_step && !lua_isnumber(state, 3))) |
| 636 | { |
| 637 | luaL_error(state, "Usage: object:_displace(index[,step]) or df._displace(object,...)"); |
| 638 | } |
| 639 | |
| 640 | int index = lua_tointeger(state, 2); |
| 641 | int step = has_step ? lua_tointeger(state, 3) : 1; |
| 642 | |
| 643 | // Two special cases: nil and lightuserdata for NULL and void* |
| 644 | if (lua_isnil(state, 1)) |
| 645 | { |
| 646 | lua_pushnil(state); |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | if (lua_islightuserdata(state, 1)) |
| 651 | { |
| 652 | if (!has_step) |
| 653 | luaL_error(state, "Step is mandatory in _displace of void*"); |
| 654 | |
| 655 | auto ptr = (uint8_t*)lua_touserdata(state, 1); |
| 656 | lua_pushlightuserdata(state, ptr + index*step); |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | const type_identity *id = get_object_identity(state, 1, "df._displace()"); |
| 661 | |
| 662 | if (!has_step) |
| 663 | step = id->byte_size(); |
| 664 | |
| 665 | if (index == 0 || step == 0) |
| 666 | { |
| 667 | lua_pushvalue(state, 1); |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | auto ptr = (uint8_t*)get_object_ref(state, 1); |
| 672 | lua_getmetatable(state, 1); |
| 673 | push_object_ref(state, ptr + index*step); |
| 674 | } |
| 675 | |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Method: allocation for DF object references. |
nothing calls this directly
no test coverage detected