@@ -757,7 +757,7 @@ void CLuaMain::SetPackage(lua_State* L, SString &strName )
757757// Push the value of a package of name to the stack
758758//
759759// /////////////////////////////////////////////////////////////
760- void CLuaMain::GetPackage (lua_State* L, SString &strName)
760+ void CLuaMain::GetPackage (lua_State* L, SString &strName )
761761{
762762 if (m_iPackageLoadedRef < 0 )
763763 return ;
@@ -775,7 +775,7 @@ void CLuaMain::GetPackage(lua_State* L, SString &strName)
775775// Load a Lua lib of a given name
776776//
777777// /////////////////////////////////////////////////////////////
778- bool CLuaMain::LoadLuaLib (lua_State *L, SString strName)
778+ bool CLuaMain::LoadLuaLib (lua_State *L, SString strName, SString &strError )
779779{
780780 SString strPath = strName;
781781 // Name format shouldn't include slashes. Subdirs are dots.
@@ -792,10 +792,21 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
792792 FileLoad (strFilePath, buffer);
793793 else
794794 {
795+ // Don't use a format string for safety, so we construct the error by hand
796+ strError += " error loading module " + strName + " from file " + strFilePath +
797+ " :\n\t The specified module could not be found" ;
798+
795799 // Try <resource>/?/init.lua
796800 strFilePath = PathJoin (strResPath, strPath, " init.lua" );
797801 if (FileExists (strFilePath))
798802 FileLoad (strFilePath, buffer);
803+ else
804+ {
805+ strError += " \n " ;
806+ strError += " error loading module " + strName + " from file " + strFilePath +
807+ " :\n\t The specified module could not be found" ;
808+ return false ;
809+ }
799810 }
800811
801812 if (buffer.size () > 0 )
@@ -806,9 +817,19 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
806817 if (lua_gettop (L) > luaSavedTop)
807818 lua_settop (L, luaSavedTop+1 );
808819
820+ if (lua_type (L, -1 ) == LUA_TNIL)
821+ {
822+ strError += " error loading module " + strName + " from file " + strFilePath +
823+ " :\n\t Module didn't return a value" ;
824+ return false ;
825+ }
826+
809827 SetPackage (L, strName); // Store our package into package.loaded
810828 return true ;
811829 }
830+ else
831+ strError += " error loading module " + strName + " from file " + strFilePath +
832+ " :\n\t Error loading script file" ;
812833
813834 return false ;
814835}
@@ -820,7 +841,7 @@ bool CLuaMain::LoadLuaLib(lua_State *L, SString strName)
820841// Load a C lib of a given name
821842//
822843// /////////////////////////////////////////////////////////////
823- bool CLuaMain::LoadClib (lua_State* L, SString strName)
844+ bool CLuaMain::LoadClib (lua_State* L, SString strName, SString &strError )
824845{
825846 SString strPath = strName;
826847 // Name format shouldn't include slashes. Subdirs are dots.
@@ -836,9 +857,19 @@ bool CLuaMain::LoadClib(lua_State* L, SString strName)
836857 strPath += " .so" ;
837858#endif
838859
839- luaL_loader_C (L, strName.c_str (), strPath.c_str ());
840- if (lua_type (L, -1 ) == LUA_TNIL)
860+ if (!FileExists (strPath))
861+ {
862+ strError += " error loading module " + strName + " from file " + strPath +
863+ " :\n\t The specified module could not be found" ;
841864 return false ;
865+ }
866+
867+ if (!luaL_loader_C (L, strName.c_str (), strPath.c_str ()) || lua_type (L, -1 ) == LUA_TNIL)
868+ {
869+ strError += " error loading module " + strName + " from file " + strPath +
870+ " :\n\t Failed to load module" ;
871+ return false ;
872+ }
842873
843874 SetPackage (L, strName);
844875 return true ;
0 commit comments