MCPcopy Create free account
hub / github.com/DFHack/dfhack / meta_assign

Function meta_assign

library/LuaWrapper.cpp:822–933  ·  view source on GitHub ↗

* Method: assign data between objects. */

Source from the content-addressed store, hash-verified

820 * Method: assign data between objects.
821 */
822static int meta_assign(lua_State *state)
823{
824 int argc = lua_gettop(state);
825
826 if (argc != 2)
827 luaL_error(state, "Usage: target:assign(src) or df.assign(target,src)");
828
829 if (!lua_istable(state, 2))
830 {
831 const type_identity *id1, *id2;
832 check_type_compatible(state, 1, 2, &id1, &id2, "df.assign()", false, false);
833
834 if (!id1->copy(get_object_ref(state, 1), get_object_ref(state, 2)))
835 luaL_error(state, "No copy support for %s", id1->getFullName().c_str());
836 }
837 else
838 {
839 const type_identity *id = get_object_identity(state, 1, "df.assign()", false);
840
841 if (lua_getmetatable(state, 2))
842 luaL_error(state, "cannot use lua tables with metatable in df.assign()");
843
844 int base = lua_gettop(state);
845
846 // x:assign{ assign = foo } => x:assign(foo)
847 bool has_assign = false;
848
849 lua_pushstring(state, "assign");
850 lua_dup(state);
851 lua_rawget(state, 2);
852
853 if (!lua_isnil(state,-1))
854 {
855 has_assign = true;
856 lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME);
857 lua_pushvalue(state, 1);
858 lua_pushvalue(state, base+2);
859 lua_call(state, 2, 0);
860 }
861
862 lua_pop(state, 1);
863
864 // new is used by autovivification and should be skipped
865 lua_pushstring(state, "new");
866
867 if (id->isContainer())
868 {
869 // check resize field
870 lua_pushstring(state, "resize");
871 lua_dup(state);
872 lua_rawget(state, 2);
873
874 if (lua_isnil(state,-1) && !has_assign)
875 {
876 /*
877 * no assign && nil or missing resize field => 1-based lua array
878 */
879 int size = lua_rawlen(state, 2);

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
luaL_errorFunction · 0.85
check_type_compatibleFunction · 0.85
lua_getmetatableFunction · 0.85
lua_pushstringFunction · 0.85
lua_dupFunction · 0.85
lua_rawgetFunction · 0.85
lua_getfieldFunction · 0.85
lua_pushvalueFunction · 0.85
lua_rawlenFunction · 0.85
invoke_resizeFunction · 0.85
lua_pushintegerFunction · 0.85

Tested by

no test coverage detected