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

Method doRunLuaFunction

library/RemoteTools.cpp:775–831  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

773}
774
775int CoreService::doRunLuaFunction(lua_State *L)
776{
777 color_ostream &out = *Lua::GetOutput(L);
778 auto &args = *(LuaFunctionData*)lua_touserdata(L, 1);
779
780 // Verify module name
781 std::string module = args.in->module();
782 size_t len = module.size();
783
784 bool valid = false;
785
786 if (len > 4)
787 {
788 if (module.substr(0,4) == "rpc.")
789 valid = true;
790 else if ((module[len-4] == '.' || module[len-4] == '-') && module.substr(len-3) != "rpc")
791 valid = true;
792 }
793
794 if (!valid)
795 {
796 args.rv = CR_WRONG_USAGE;
797 out.printerr("Only modules named rpc.* or *.rpc or *-rpc may be called.\n");
798 return 0;
799 }
800
801 // Prepare function and arguments
802 lua_settop(L, 0);
803
804 if (!Lua::PushModulePublic(out, L, module.c_str(), args.in->function().c_str())
805 || lua_isnil(L, 1))
806 {
807 args.rv = CR_NOT_FOUND;
808 return 0;
809 }
810
811 luaL_checkstack(L, args.in->arguments_size(), "too many arguments");
812
813 for (int i = 0; i < args.in->arguments_size(); i++)
814 lua_pushstring(L, args.in->arguments(i).c_str());
815
816 // Call
817 lua_call(L, args.in->arguments_size(), LUA_MULTRET);
818
819 // Store results
820 int nresults = lua_gettop(L);
821
822 for (int i = 1; i <= nresults; i++)
823 {
824 size_t len;
825 const char *data = lua_tolstring(L, i, &len);
826 args.out->add_value(std::string(data, len));
827 }
828
829 args.rv = CR_OK;
830 return 0;
831}

Callers

nothing calls this directly

Calls 10

lua_touserdataFunction · 0.85
lua_settopFunction · 0.85
PushModulePublicFunction · 0.85
luaL_checkstackFunction · 0.85
lua_pushstringFunction · 0.85
lua_gettopFunction · 0.85
lua_tolstringFunction · 0.85
c_strMethod · 0.80
add_valueMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected