Browse Source

More informative error messages.

Nate Starkey 13 years ago
parent
commit
533fcfd2f4

+ 4 - 1
Include/Rocket/Core/Lua/Interpreter.h

@@ -25,7 +25,10 @@ public:
     //on top of the stack. It is just like luaL_loadstring, but you get to specify the name
     //on top of the stack. It is just like luaL_loadstring, but you get to specify the name
     static void LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name = "");
     static void LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name = "");
     //If there are errors on the top of the stack, this will print those out to the log.
     //If there are errors on the top of the stack, this will print those out to the log.
-    static void Report();
+    //L is a Lua state, and if not passed in, will use the Interpreter's state
+    //place is a string that will be printed to the log right before the error message seperated by a space. Set
+    //this when you would get no information about where the error happens.
+    static void Report(lua_State* L = NULL, const Rocket::Core::String& place = "");
 
 
     //clears all of the items on the stack, and pushes the function from funRef on top of the stack. Only use
     //clears all of the items on the stack, and pushes the function from funRef on top of the stack. Only use
     //this if you used lua_ref instead of luaL_ref
     //this if you used lua_ref instead of luaL_ref

+ 1 - 1
Samples/luainvaders/data/main_menu.rml

@@ -28,7 +28,7 @@ function MainMenu.CloseLogo(document)
 end
 end
 		</script>
 		</script>
 	</head>
 	</head>
-	<body template="luawindow" onload="Window.OnWindowLoad(element) document.context:LoadDocument('data/logo.rml'):Show()" onunload="MainMenu.CloseLogo(document)">
+	<body template="luawindow" onload="Window.OnWindowLoad(document) document.context:LoadDocument('data/logo.rml'):Show()" onunload="MainMenu.CloseLogo(document)">
 		<button onclick="document.context:LoadDocument('data/start_game.rml'):Show() document:Close()">Start Game</button><br />
 		<button onclick="document.context:LoadDocument('data/start_game.rml'):Show() document:Close()">Start Game</button><br />
 		<button onclick="Window.LoadMenu('high_score',document)">High Scores</button><br />
 		<button onclick="Window.LoadMenu('high_score',document)">High Scores</button><br />
 		<button onclick="Window.LoadMenu('options',document)">Options</button><br />
 		<button onclick="Window.LoadMenu('options',document)">Options</button><br />

+ 3 - 3
Samples/luainvaders/data/window.rml

@@ -4,8 +4,8 @@
 	<script>
 	<script>
 Window = Window or {} --namespace
 Window = Window or {} --namespace
 
 
-function Window.OnWindowLoad(element)
-	element.owner_document:GetElementById('title').inner_rml = element.title
+function Window.OnWindowLoad(document)
+	document:GetElementById('title').inner_rml = document.title
 end
 end
 	
 	
 function Window.LoadMenu(name,document)
 function Window.LoadMenu(name,document)
@@ -19,6 +19,6 @@ function Window.LoadMenu(name,document)
 end
 end
 	</script>
 	</script>
 </head>
 </head>
-<body template="window" onload="Window.OnWindowLoad(element)">
+<body template="window" onload="Window.OnWindowLoad(document)">
 </body>
 </body>
 </template>
 </template>

+ 10 - 3
Source/Core/Lua/Interpreter.cpp

@@ -95,13 +95,20 @@ void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Cor
 }
 }
 
 
 
 
-void Interpreter::Report()
+void Interpreter::Report(lua_State* L, const Rocket::Core::String& place)
 {
 {
+    if(L == NULL)
+        L = _L; //use the original state of Interpreter
     const char * msg= lua_tostring(_L,-1);
     const char * msg= lua_tostring(_L,-1);
+    String strmsg;
     while(msg)
     while(msg)
     {
     {
         lua_pop(_L,1);
         lua_pop(_L,1);
-        Log::Message(Log::LT_WARNING, msg);
+        if(place == "")
+            strmsg = msg;
+        else
+            strmsg = String(place).Append(" ").Append(msg);
+        Log::Message(Log::LT_WARNING, strmsg.CString());
         msg=lua_tostring(_L,-1);
         msg=lua_tostring(_L,-1);
     }
     }
 }
 }
@@ -135,7 +142,7 @@ bool Interpreter::ExecuteCall(int params, int res)
     }
     }
     else
     else
     {
     {
-        if(lua_pcall(_L,params,res,0))
+        if(lua_pcall(_L,params,res,0) != 0)
         {
         {
             Report();
             Report();
             ret = false;
             ret = false;

+ 8 - 3
Source/Core/Lua/LuaType.cpp

@@ -1,6 +1,7 @@
 #include "precompiled.h"
 #include "precompiled.h"
 #include <Rocket/Controls/Controls.h>
 #include <Rocket/Controls/Controls.h>
 #include <Rocket/Core/Core.h>
 #include <Rocket/Core/Core.h>
+#include <Rocket/Core/Lua/Interpreter.h>
 
 
 namespace Rocket {
 namespace Rocket {
 namespace Core {
 namespace Core {
@@ -126,6 +127,7 @@ int LuaType<T>::thunk(lua_State* L)
 }
 }
 
 
 
 
+
 template<typename T>
 template<typename T>
 void LuaType<T>::tostring(char* buff, void* obj)
 void LuaType<T>::tostring(char* buff, void* obj)
 {
 {
@@ -199,7 +201,8 @@ int LuaType<T>::index(lua_State* L)
             if(lua_type(L,-1) == LUA_TFUNCTION) //[-1 = 5]
             if(lua_type(L,-1) == LUA_TFUNCTION) //[-1 = 5]
             {
             {
                 lua_pushvalue(L,1); //push the userdata to the stack [6]
                 lua_pushvalue(L,1); //push the userdata to the stack [6]
-                lua_pcall(L,1,1,0); //remove one, result is at [6]
+                if(lua_pcall(L,1,1,0) != 0) //remove one, result is at [6]
+                    Interpreter::Report(L, String(GetTClassName<T>()).Append(".__index for ").Append(lua_tostring(L,2)).Append(": "));
             }
             }
             else
             else
             {
             {
@@ -212,7 +215,8 @@ int LuaType<T>::index(lua_State* L)
                     {
                     {
                         lua_pushvalue(L,1); //[1] = object -> [7] = object
                         lua_pushvalue(L,1); //[1] = object -> [7] = object
                         lua_pushvalue(L,2); //[2] = key -> [8] = key
                         lua_pushvalue(L,2); //[2] = key -> [8] = key
-                        lua_pcall(L,2,1,0); //call function at top of stack (__index) -> pop top 2 as args; [7] = return value
+                        if(lua_pcall(L,2,1,0) != 0) //call function at top of stack (__index) -> pop top 2 as args; [7] = return value
+                            Interpreter::Report(L, String(GetTClassName<T>()).Append(".__index for ").Append(lua_tostring(L,2)).Append(": "));
                     }
                     }
                     else if(lua_istable(L,-1) )
                     else if(lua_istable(L,-1) )
                         lua_getfield(L,-1,key); //shorthand version of above -> [7] = return value
                         lua_getfield(L,-1,key); //shorthand version of above -> [7] = return value
@@ -253,7 +257,8 @@ int LuaType<T>::newindex(lua_State* L)
     {
     {
         lua_pushvalue(L,1); //userdata at [7]
         lua_pushvalue(L,1); //userdata at [7]
         lua_pushvalue(L,3); //[8] = copy of [3]
         lua_pushvalue(L,3); //[8] = copy of [3]
-        lua_pcall(L,2,0,0); //call function, pop 2 off push 0 on
+        if(lua_pcall(L,2,0,0) != 0) //call function, pop 2 off push 0 on
+            Interpreter::Report(L, String(GetTClassName<T>()).Append(".__newindex for ").Append(lua_tostring(L,2)).Append(": ")); 
     }
     }
     else
     else
         lua_pop(L,1); //not a setter function.
         lua_pop(L,1); //not a setter function.