Browse Source

Use Rocket::Core::FileInterface to load scripts.

Catches both Lua compilation and runtime errors, where previously it was only runtime.

Fixed the rocket global variable for Lua, because it was not setting up the metatable correctly.

Missed a function definition for Lua's DataSource.new.
Nate Starkey 13 years ago
parent
commit
0734533b08

+ 3 - 1
Source/Controls/Lua/DataSource.cpp

@@ -140,7 +140,9 @@ namespace Core {
 namespace Lua {
 template<> void ExtraInit<Rocket::Controls::Lua::LuaDataSource>(lua_State* L, int metatable_index) 
 { 
-    return; 
+    lua_pushcfunction(L,Rocket::Controls::Lua::DataSourcenew);
+    lua_setfield(L,metatable_index-1,"new");
+    return;
 }
 using Rocket::Controls::Lua::DataSource;
 LUACONTROLSTYPEDEFINE(DataSource,false)

+ 22 - 20
Source/Core/Lua/Interpreter.cpp

@@ -30,6 +30,7 @@
 #include <Rocket/Core/Lua/Utilities.h>
 #include <Rocket/Core/Log.h>
 #include <Rocket/Core/String.h>
+#include <Rocket/Core/FileInterface.h>
 #include <Rocket/Core/Lua/LuaType.h>
 #include "LuaDocumentElementInstancer.h"
 #include <Rocket/Core/Factory.h>
@@ -103,40 +104,41 @@ void Interpreter::RegisterCoreTypes(lua_State* L)
 
 void Interpreter::LoadFile(const String& file)
 {
-    String msg = "Loading";
-    if(luaL_loadfile(_L, file.CString()) != 0)
-    {
-        msg.Append(" failed. Could not load. ").Append(file);
-        Log::Message(Log::LT_ERROR, msg.CString());
-        Report(_L);
-    }
-    else
+    //use the file interface to get the contents of the script
+    Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
+    Rocket::Core::FileHandle handle = file_interface->Open(file);
+    size_t size = file_interface->Length(handle);
+    char* file_contents = new char[size];
+    file_interface->Read(file_contents,size,handle);
+    file_interface->Close(handle);
+
+    if(luaL_loadbuffer(_L,file_contents,size,file.CString()) != 0)
+        Report(_L); 
+    else //if there were no errors loading, then the compiled function is on the top of the stack
     {
         if(lua_pcall(_L,0,0,0) != 0)
-        {
-            msg.Append(" failed. Could not run. ").Append(file);
-            Log::Message(Log::LT_ERROR, msg.CString());
             Report(_L);
-        }
-        else
-        {
-            msg.Append(" was successful. ").Append(file);
-            Log::Message(Log::LT_DEBUG, msg.CString());
-        }
     }
+
+    delete[] file_contents;
 }
 
 
 void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core::String& name)
 {
-    luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
-    if(lua_pcall(_L,0,0,0) != 0)
+    if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
         Report(_L);
+    else
+    {
+        if(lua_pcall(_L,0,0,0) != 0)
+            Report(_L);
+    }
 }
 
 void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name)
 {
-    luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
+    if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
+        Report(_L);
 }
 
 

+ 12 - 2
Source/Core/Lua/LuaEventListener.cpp

@@ -57,9 +57,19 @@ LuaEventListener::LuaEventListener(const String& code, Element* element) : Event
     int tbl = lua_gettop(L);
 
     //compile,execute,and save the function
-    luaL_loadstring(L,function.CString());
-    if(lua_pcall(L,0,1,0) != 0)
+    if(luaL_loadstring(L,function.CString()) != 0)
+    {
         Report(L);
+        return;
+    }
+    else
+    {
+        if(lua_pcall(L,0,1,0) != 0)
+        {
+            Report(L);
+            return;
+        }
+    }
     luaFuncRef = luaL_ref(L,tbl); //creates a reference to the item at the top of the stack in to the table we just created
     lua_pop(L,1); //pop the EVENTLISTENERFUNCTIONS table
 

+ 21 - 6
Source/Core/Lua/Rocket.cpp

@@ -39,20 +39,21 @@ namespace Lua {
 #define ROCKETLUA_INPUTENUM(keyident,tbl) lua_pushinteger(L,Input::KI_##keyident); lua_setfield(L,(tbl),#keyident);
 #define ROCKETLUA_INPUTMODIFIERENUM(keymod,tbl) lua_pushinteger(L,Input::KM_##keymod); lua_setfield(L,(tbl),#keymod);
 
+//c++ representation of the global variable in Lua so that the syntax is consistent
+LuaRocket lua_global_rocket;
 
 void LuaRocketPushrocketGlobal(lua_State* L)
 {
-    lua_newtable(L);
+    luaL_getmetatable(L,GetTClassName<LuaRocket>());
     LuaRocketEnumkey_identifier(L);
-    lua_setfield(L,-2,"key_identifier");
+    lua_global_rocket.key_identifier_ref = luaL_ref(L,-2);
     LuaRocketEnumkey_modifier(L);
-    lua_setfield(L,-2,"key_modifier");
-    luaL_getmetatable(L,GetTClassName<LuaRocket>());
-    lua_setmetatable(L,-2); //metatable of the new table = LuaRocket
+    lua_global_rocket.key_modifier_ref = luaL_ref(L,-2);
+    LuaType<LuaRocket>::push(L,&lua_global_rocket,false);
     lua_setglobal(L,"rocket");
 }
 
-template<> void ExtraInit<LuaRocket>(lua_State* L, int metatable_index){ return; }
+template<> void ExtraInit<LuaRocket>(lua_State* L, int metatable_index) { return; }
 
 int LuaRocketCreateContext(lua_State* L, LuaRocket* obj)
 {
@@ -93,6 +94,20 @@ int LuaRocketGetAttrcontexts(lua_State* L)
     return 1;
 }
 
+int LuaRocketGetAttrkey_identifier(lua_State* L)
+{
+    luaL_getmetatable(L,GetTClassName<LuaRocket>());
+    lua_rawgeti(L,-1,lua_global_rocket.key_identifier_ref);
+    return 1;
+}
+
+int LuaRocketGetAttrkey_modifier(lua_State* L)
+{
+    luaL_getmetatable(L,GetTClassName<LuaRocket>());
+    lua_rawgeti(L,-1,lua_global_rocket.key_modifier_ref);
+    return 1;
+}
+
 void LuaRocketEnumkey_identifier(lua_State* L)
 {
     lua_newtable(L);

+ 11 - 2
Source/Core/Lua/Rocket.h

@@ -35,8 +35,15 @@ namespace Rocket {
 namespace Core {
 namespace Lua {
 
-//just need a class to take up a type name, and a single object to be able to be pushed to Lua
-class LuaRocket { int to_remove_warning; }; //instance of object defined in Interpreter.cpp
+
+class LuaRocket 
+{ 
+public:
+    //reference to the table defined in LuaRocketEnumkey_identifier
+    int key_identifier_ref;
+    //reference to the table defined in LuaRocketEnumkey_modifier
+    int key_modifier_ref;
+}; 
 
 void LuaRocketPushrocketGlobal(lua_State* L);
 
@@ -46,6 +53,8 @@ int LuaRocketLoadFontFace(lua_State* L, LuaRocket* obj);
 int LuaRocketRegisterTag(lua_State* L, LuaRocket* obj);
 
 int LuaRocketGetAttrcontexts(lua_State* L);
+int LuaRocketGetAttrkey_identifier(lua_State* L);
+int LuaRocketGetAttrkey_modifier(lua_State* L);
 
 void LuaRocketEnumkey_identifier(lua_State* L);
 void LuaRocketEnumkey_modifier(lua_State* L);