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

Function write_field

library/LuaTypes.cpp:586–625  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

584}
585
586static void write_field(lua_State *state, const struct_field_info *field, void *ptr, int value_idx)
587{
588 switch (field->mode)
589 {
590 case struct_field_info::STATIC_STRING:
591 {
592 size_t size;
593 const char *str = lua_tolstring(state, value_idx, &size);
594 if (!str)
595 field_error(state, 2, "string expected", "write");
596 memcpy(ptr, str, std::min(size+1, size_t(field->count)));
597 return;
598 }
599
600 case struct_field_info::OBJ_METHOD:
601 case struct_field_info::CLASS_METHOD:
602 // error
603
604 case struct_field_info::PRIMITIVE:
605 case struct_field_info::SUBSTRUCT:
606 case struct_field_info::CONTAINER:
607 field->type->lua_write(state, 2, ptr, value_idx);
608 return;
609
610 case struct_field_info::POINTER:
611 df::pointer_identity::lua_write(state, 2, ptr, field->type, value_idx);
612 return;
613
614 case struct_field_info::STATIC_ARRAY:
615 case struct_field_info::STL_VECTOR_PTR:
616 lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME);
617 read_field(state, field, ptr);
618 lua_pushvalue(state, value_idx);
619 lua_call(state, 2, 0);
620 return;
621
622 case struct_field_info::END:
623 return;
624 }
625}
626
627/**
628 * Metamethod: __index for structures.

Callers 2

meta_struct_newindexFunction · 0.85
meta_global_newindexFunction · 0.85

Calls 5

lua_tolstringFunction · 0.85
lua_getfieldFunction · 0.85
read_fieldFunction · 0.85
lua_pushvalueFunction · 0.85
lua_writeMethod · 0.80

Tested by

no test coverage detected