Browse Source

Fixed the warning on exit.

Made the error messages more useful.

Broke something either in this commit or the last one, and now high scores doesn't work.
Nate Starkey 13 years ago
parent
commit
ed949eb062

+ 4 - 4
Build/RocketLua.vcproj

@@ -305,7 +305,7 @@
 				>
 			</File>
 			<File
-				RelativePath="..\Source\Core\Lua\Interpreter.h"
+				RelativePath="..\Include\Rocket\Core\Lua\Interpreter.h"
 				>
 			</File>
 			<File
@@ -317,7 +317,7 @@
 				>
 			</File>
 			<File
-				RelativePath="..\Source\Core\Lua\lua.hpp"
+				RelativePath="..\Include\Rocket\Core\Lua\lua.hpp"
 				>
 			</File>
 			<File
@@ -333,7 +333,7 @@
 				</FileConfiguration>
 			</File>
 			<File
-				RelativePath="..\Source\Core\Lua\LuaType.h"
+				RelativePath="..\Include\Rocket\Core\Lua\LuaType.h"
 				>
 			</File>
 			<File
@@ -470,7 +470,7 @@
 			</File>
 		</Filter>
 		<File
-			RelativePath="..\Source\Core\Lua\Header.h"
+			RelativePath="..\Include\Rocket\Core\Lua\Header.h"
 			>
 		</File>
 		<File

+ 23 - 2
Include/Rocket/Core/Lua/Interpreter.h

@@ -15,17 +15,38 @@ namespace Lua {
 class ROCKETLUA_API Interpreter : public Plugin
 {
 public:
+    //Somewhat misleading name if you are used to the Lua meaning of Load, but this function calls
+    //luaL_loadfile and then lua_pcall, reporting the errors (if any)
     static void LoadFile(const Rocket::Core::String& file);
-    static void DoString(const Rocket::Core::String& str);
+    //Calls lua_dostring and reports the errors. The first parameter is the string to execute, the
+    //second parameter is the name, which will show up in the console/error log
+    static void DoString(const Rocket::Core::String& code, const Rocket::Core::String& name = "");
+    //Same as DoString, except does NOT call pcall on it. It will leave the compiled (but not executed) string
+    //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 = "");
+    //If there are errors on the top of the stack, this will print those out to the log.
     static void Report();
 
+    //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
     static void BeginCall(int funRef);
+    /*  Before you call this, your stack should look like:
+    [0] function to call
+    [1...top] parameters to pass to the function (if any)
+    Or, in words, make sure to push the function on the stack before the parameters. After this function, the params and function will
+    be popped off, and 'res' number of items will be pushed.
+    */
     static bool ExecuteCall(int params = 0, int res = 0);
+    //removes 'res' number of items from the stack
     static void EndCall(int res = 0);
 
+    //This lacks a SetLuaState for a reason. If you have to use your own Lua binding, then use this lua_State; it will
+    //already have all of the libraries loaded, and all of the types defined
     static lua_State* GetLuaState();
 
+    //Creates the plugin
     static void Initialise();
+    //removes the plugin
 	static void Shutdown();
     
     //From Plugin
@@ -35,7 +56,7 @@ public:
 private:
     //This will populate the global Lua table with all of the Lua types and some global functions
     static inline void RegisterEverything(lua_State* L);
-    void Startup();
+    void Startup(); //loads default libs
 
     static lua_State* _L;
 };

+ 3 - 1
Samples/luainvaders/data/high_score.rml

@@ -43,10 +43,12 @@ HighScore = HighScore or {}
 
 --because we pass it as a plain function in HighScore.OnLoad, we have to pay attention
 --to the order of arguments. We don't need the first two in this case, so the name doesn't matter
-function HighScore.OnRowAdd(_a,_b,document)
+function HighScore.OnRowAdd(_,_,document)
 	input = document:GetElementById('player_input')
 	if input then
 		input:Focus()
+    else
+        Log.Message(Log.logtype.warning, "highscore.rml, input is nil or doesn't exist.")
     end
 end
 

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

@@ -22,8 +22,8 @@ MainMenu = MainMenu or {}
 
 function MainMenu.CloseLogo(document)
 	if document.context then
-        --this will log an error upon exit, but doesn't crash
-		document.context.documents['logo']:Close()
+        local logo = document.context.documents['logo']
+        if logo then logo:Close() end
     end
 end
 		</script>

+ 8 - 2
Source/Core/Lua/Interpreter.cpp

@@ -82,12 +82,18 @@ void Interpreter::LoadFile(const String& file)
 }
 
 
-void Interpreter::DoString(const Rocket::Core::String& str)
+void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core::String& name)
 {
-    if(luaL_dostring(_L,str.CString()) != 0)
+    luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
+    if(lua_pcall(_L,0,0,0) != 0)
         Report();
 }
 
+void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name)
+{
+    luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
+}
+
 
 void Interpreter::Report()
 {

+ 1 - 1
Source/Core/Lua/LuaDocument.cpp

@@ -23,7 +23,7 @@ void LuaDocument::LoadScript(Stream* stream, const String& source_name)
     {
         String buffer = "";
         stream->Read(buffer,stream->Length()); //just do the whole thing
-        Interpreter::DoString(buffer);
+        Interpreter::DoString(buffer, this->GetSourceURL());
     }
 }