| 264 | //============================================================================// |
| 265 | |
| 266 | int LuaVFS::Include(lua_State* L) { |
| 267 | lua_settop(L, 3); |
| 268 | |
| 269 | const char* path = luaL_checkstring(L, 1); |
| 270 | std::string modes = luaL_optstring(L, 2, GetReadFilter(path, L)); |
| 271 | modes = BzVFS::allowModes(modes, L2ES(L)->vfsModes->readAllowed); |
| 272 | |
| 273 | std::string code; |
| 274 | if (!bzVFS.readFile(path, modes, code)) { |
| 275 | luaL_error(L, "file not found"); |
| 276 | } |
| 277 | |
| 278 | // compile the code |
| 279 | int error = luaL_loadbuffer(L, code.data(), code.size(), path); |
| 280 | if (error != 0) { |
| 281 | lua_error(L); |
| 282 | } |
| 283 | const int funcIndex = 4; |
| 284 | |
| 285 | // setup the function's environment |
| 286 | if (lua_istable(L, 3)) { |
| 287 | lua_pushvalue(L, 3); |
| 288 | } |
| 289 | else if (lua_isnil(L, 3)) { |
| 290 | LuaUtils::PushCurrentFuncEnv(L, "Include"); |
| 291 | } |
| 292 | else { |
| 293 | luaL_error(L, "bad fenv parameter"); |
| 294 | } |
| 295 | |
| 296 | if (lua_setfenv(L, -2) == 0) { |
| 297 | luaL_error(L, "Include: error with setfenv"); |
| 298 | } |
| 299 | |
| 300 | // execute the code |
| 301 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
| 302 | if (error != 0) { |
| 303 | lua_error(L); |
| 304 | } |
| 305 | |
| 306 | return lua_gettop(L) - (funcIndex - 1); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | //============================================================================// |
nothing calls this directly
no test coverage detected