| 1241 | } |
| 1242 | |
| 1243 | void LuaParser::LoadScript(std::string filename, std::string package_name) { |
| 1244 | auto iter = loaded_.find(package_name); |
| 1245 | if(iter != loaded_.end()) { |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | auto top = lua_gettop(L); |
| 1250 | PushErrorHandler(L); |
| 1251 | if(luaL_loadfile(L, filename.c_str())) { |
| 1252 | std::string error = lua_tostring(L, -1); |
| 1253 | AddError(error); |
| 1254 | lua_pop(L, 2); |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | //This makes an env table named: package_name |
| 1259 | //And makes it so we can see the global table _G from it |
| 1260 | //Then sets it so this script is called from that table as an env |
| 1261 | |
| 1262 | lua_createtable(L, 0, 0); // anon table |
| 1263 | lua_getglobal(L, "_G"); // get _G |
| 1264 | lua_setfield(L, -2, "__index"); //anon table.__index = _G |
| 1265 | |
| 1266 | lua_pushvalue(L, -1); //copy table to top of stack |
| 1267 | lua_setmetatable(L, -2); //setmetatable(anon_table, copied table) |
| 1268 | |
| 1269 | lua_pushvalue(L, -1); //put the table we made into the registry |
| 1270 | lua_setfield(L, LUA_REGISTRYINDEX, package_name.c_str()); |
| 1271 | |
| 1272 | lua_setfenv(L, -2); //set the env to the table we made |
| 1273 | |
| 1274 | if(lua_pcall(L, 0, 0, top + 1)) { |
| 1275 | std::string error = lua_tostring(L, -1); |
| 1276 | AddError(error); |
| 1277 | lua_pop(L, 2); |
| 1278 | } |
| 1279 | else { |
| 1280 | loaded_[package_name] = true; |
| 1281 | } |
| 1282 | |
| 1283 | auto end = lua_gettop(L); |
| 1284 | int n = end - top; |
| 1285 | if (n > 0) { |
| 1286 | lua_pop(L, n); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | bool LuaParser::HasFunction(std::string subname, std::string package_name) { |
| 1291 | //std::transform(subname.begin(), subname.end(), subname.begin(), ::tolower); |
nothing calls this directly
no test coverage detected