| 773 | } |
| 774 | |
| 775 | int 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 | } |
nothing calls this directly
no test coverage detected