Browse Source

Restoring Lua support

Michael Ragazzon 6 years ago
parent
commit
1633bc4645
47 changed files with 165 additions and 149 deletions
  1. 1 1
      .travis.yml
  2. 7 9
      Include/RmlUi/Core/Lua/LuaType.h
  3. 14 31
      Include/RmlUi/Core/Lua/LuaType.inl
  4. 9 9
      Samples/luainvaders/data/high_score.rml
  5. 3 3
      Samples/luainvaders/data/options.rml
  6. 2 8
      Samples/luainvaders/src/ElementGameInstancer.cpp
  7. 2 5
      Samples/luainvaders/src/ElementGameInstancer.h
  8. 3 1
      Samples/luainvaders/src/LuaInterface.cpp
  9. 7 6
      Samples/luainvaders/src/main.cpp
  10. 1 1
      Source/Controls/Lua/DataFormatter.cpp
  11. 1 1
      Source/Controls/Lua/DataSource.cpp
  12. 1 1
      Source/Controls/Lua/ElementDataGrid.cpp
  13. 1 1
      Source/Controls/Lua/ElementDataGridRow.cpp
  14. 1 1
      Source/Controls/Lua/ElementForm.cpp
  15. 1 1
      Source/Controls/Lua/ElementFormControl.cpp
  16. 1 1
      Source/Controls/Lua/ElementFormControlDataSelect.cpp
  17. 1 1
      Source/Controls/Lua/ElementFormControlInput.cpp
  18. 1 1
      Source/Controls/Lua/ElementFormControlSelect.cpp
  19. 1 1
      Source/Controls/Lua/ElementFormControlTextArea.cpp
  20. 1 1
      Source/Controls/Lua/ElementTabSet.cpp
  21. 1 1
      Source/Controls/Lua/SelectOptionsProxy.cpp
  22. 1 1
      Source/Core/Lua/Colourb.cpp
  23. 1 1
      Source/Core/Lua/Colourf.cpp
  24. 2 3
      Source/Core/Lua/Context.cpp
  25. 1 1
      Source/Core/Lua/ContextDocumentsProxy.cpp
  26. 5 7
      Source/Core/Lua/Document.cpp
  27. 52 9
      Source/Core/Lua/Element.cpp
  28. 11 0
      Source/Core/Lua/Element.h
  29. 1 1
      Source/Core/Lua/ElementAttributesProxy.cpp
  30. 1 1
      Source/Core/Lua/ElementChildNodesProxy.cpp
  31. 1 2
      Source/Core/Lua/ElementInstancer.cpp
  32. 1 1
      Source/Core/Lua/ElementStyleProxy.cpp
  33. 1 1
      Source/Core/Lua/ElementText.cpp
  34. 1 1
      Source/Core/Lua/Event.cpp
  35. 1 1
      Source/Core/Lua/EventParametersProxy.cpp
  36. 12 2
      Source/Core/Lua/Interpreter.cpp
  37. 1 1
      Source/Core/Lua/Log.cpp
  38. 3 8
      Source/Core/Lua/LuaElementInstancer.cpp
  39. 2 4
      Source/Core/Lua/LuaElementInstancer.h
  40. 1 4
      Source/Core/Lua/LuaEventListener.cpp
  41. 1 1
      Source/Core/Lua/LuaEventListener.h
  42. 0 6
      Source/Core/Lua/LuaEventListenerInstancer.cpp
  43. 1 4
      Source/Core/Lua/LuaEventListenerInstancer.h
  44. 1 1
      Source/Core/Lua/RmlUi.cpp
  45. 1 1
      Source/Core/Lua/RmlUiContextsProxy.cpp
  46. 1 1
      Source/Core/Lua/Vector2f.cpp
  47. 1 1
      Source/Core/Lua/Vector2i.cpp

+ 1 - 1
.travis.yml

@@ -54,7 +54,7 @@ before_install:
 
 install:
   - cd "$TRAVIS_BUILD_DIR"
-  - if [[ "$TRAVIS_OS_NAME" != "osx" ]]; then cmake -DBUILD_LUA_BINDINGS=OFF -DBUILD_SAMPLES=ON .; fi
+  - if [[ "$TRAVIS_OS_NAME" != "osx" ]]; then cmake -DBUILD_LUA_BINDINGS=ON -DBUILD_SAMPLES=ON .; fi
   - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then cmake -G Xcode .; fi
 
 before_script:

+ 7 - 9
Include/RmlUi/Core/Lua/LuaType.h

@@ -58,20 +58,18 @@
  have to have functions named @c ElementMethods, @c ElementGetters, @c ElementSetters that return the appropriate
  types.
  @param is_reference_counted true if the type inherits from Rml::Core::ReferenceCountable, false otherwise*/
-#define LUACORETYPEDEFINE(type,is_ref_counted) \
+#define LUACORETYPEDEFINE(type) \
     template<> const char* GetTClassName<type>() { return #type; } \
     template<> RegType<type>* GetMethodTable<type>() { return type##Methods; } \
     template<> luaL_Reg* GetAttrTable<type>() { return type##Getters; } \
     template<> luaL_Reg* SetAttrTable<type>() { return type##Setters; } \
-    template<> bool IsReferenceCounted<type>() { return (is_ref_counted); } \
 
 //We can't use LUACORETYPEDEFINE due to namespace issues
-#define LUACONTROLSTYPEDEFINE(type,is_ref_counted) \
+#define LUACONTROLSTYPEDEFINE(type) \
     template<> const char* GetTClassName<type>() { return #type; } \
     template<> RegType<type>* GetMethodTable<type>() { return Rml::Controls::Lua::type##Methods; } \
     template<> luaL_Reg* GetAttrTable<type>() { return Rml::Controls::Lua::type##Getters; } \
     template<> luaL_Reg* SetAttrTable<type>() { return Rml::Controls::Lua::type##Setters; } \
-    template<> bool IsReferenceCounted<type>() { return (is_ref_counted); } \
 
 /** Used to remove repetitive typing at the cost of flexibility. It creates function prototypes for
 getting the name of the type, method tables, and if it is reference counted.
@@ -82,7 +80,6 @@ the LUACORETYPEDEFINE macro, or make sure that the function signatures are @em e
     template<> RMLUILUA_API RegType<type>* GetMethodTable<type>(); \
     template<> RMLUILUA_API luaL_Reg* GetAttrTable<type>(); \
     template<> RMLUILUA_API luaL_Reg* SetAttrTable<type>(); \
-    template<> RMLUILUA_API bool IsReferenceCounted<type>(); \
 
 /** Used to remove repetitive typing at the cost of flexibility. It creates function prototypes for
 getting the name of the type, method tables, and if it is reference counted.
@@ -93,7 +90,6 @@ the LUACORETYPEDEFINE macro, or make sure that the function signatures are @em e
     template<> RMLUILUA_API RegType<type>* GetMethodTable<type>(); \
     template<> RMLUILUA_API luaL_Reg* GetAttrTable<type>(); \
     template<> RMLUILUA_API luaL_Reg* SetAttrTable<type>(); \
-    template<> RMLUILUA_API bool IsReferenceCounted<type>(); \
 
 namespace Rml {
 namespace Core {
@@ -114,8 +110,7 @@ template<typename T> RMLUILUA_API luaL_Reg* GetAttrTable();
 template<typename T> RMLUILUA_API luaL_Reg* SetAttrTable();
 /** String representation of the class */
 template<typename T> RMLUILUA_API const char* GetTClassName();
-/** bool for if it is reference counted */
-template<typename T> RMLUILUA_API bool IsReferenceCounted();
+
 /** gets called from the LuaType<T>::Register function, right before @c _regfunctions.
 If you want to inherit from another class, in the function you would want
 to call @c _regfunctions<superclass>, where method is metatable_index - 1. Anything
@@ -156,7 +151,7 @@ public:
     @return The value that RegType.func returns   */
     static inline int thunk(lua_State* L);
     /** String representation of the pointer. Called by the __tostring metamethod  */
-    static inline void tostring(char* buff, void* obj);
+    static inline void tostring(char* buff, size_t buff_size, void* obj);
     //these are metamethods
     /** The __gc metamethod. If the object was pushed by push(lua_State*,T*,bool) with the third
     argument as true, it will either decrease the reference count or call delete depending on if
@@ -177,7 +172,10 @@ public:
     metatable, and the getters exist in __getters and setters in __setters. The reason for __getters and __setters
     is to have the objects use a 'dot' syntax for properties and a 'colon' syntax for methods.*/
     static inline void _regfunctions(lua_State* L, int meta, int method);
+
 private:
+	static constexpr size_t max_pointer_string_size = 32;
+
     LuaType(); //hide constructor
 
 };

+ 14 - 31
Include/RmlUi/Core/Lua/LuaType.inl

@@ -93,8 +93,8 @@ int LuaType<T>::push(lua_State *L, T* obj, bool gc)
         *ptrHold = obj; 
         lua_pushvalue(L, mt); // ->[3] = copy of [1]
         lua_setmetatable(L, -2); //[-2 = 2] -> [2]'s metatable = [3]; pop [3]
-        char name[32];
-        tostring(name,obj);
+        char name[max_pointer_string_size];
+        tostring(name, max_pointer_string_size, obj);
         lua_getfield(L,LUA_REGISTRYINDEX,"DO NOT TRASH"); //->[3] = value returned from function
         if(lua_isnil(L,-1) ) //if [3] hasn't been created yet, then create it
         {
@@ -116,13 +116,6 @@ int LuaType<T>::push(lua_State *L, T* obj, bool gc)
             lua_setfield(L,-2,name); //represents t[k] = v, [-2 = 3] = t -> v = [4], k = <ClassName>; pop [4]
         }
 
-        if(IsReferenceCounted<T>())
-        {
-            //If you look at the gc_T function, reference countables do not
-            //care about the "DO NOT TRASH" table
-            ((Rml::Core::ReferenceCountable*)obj)->AddReference();
-        }
-
         lua_pop(L,1); // -> pop [3]
     }
     lua_settop(L,ud); //[ud = 2] -> remove everything that is above 2, top = [2]
@@ -166,9 +159,9 @@ int LuaType<T>::thunk(lua_State* L)
 
 
 template<typename T>
-void LuaType<T>::tostring(char* buff, void* obj)
+void LuaType<T>::tostring(char* buff, size_t buff_size, void* obj)
 {
-    sprintf(buff,"%p",obj);
+    snprintf(buff, buff_size, "%p", obj);
 }
 
 
@@ -178,28 +171,18 @@ int LuaType<T>::gc_T(lua_State* L)
     T * obj = check(L,1); //[1] = this userdata
     if(obj == nullptr)
         return 0;
-    if(IsReferenceCounted<T>())
-    {
-        // ReferenceCountables do not care about the "DO NOT TRASH" table.
-        // Because userdata is pushed which contains a pointer to the pointer
-        // of 'obj', 'obj' will be garbage collected for every time 'obj' was pushed.
-        ((Rml::Core::ReferenceCountable*)obj)->RemoveReference();
-        return 0;
-    }
+
     lua_getfield(L,LUA_REGISTRYINDEX,"DO NOT TRASH"); //->[2] = return value from this
     if(lua_istable(L,-1) ) //[-1 = 2], if it is a table
     {
-        char name[32];
-        tostring(name,obj);
-        lua_getfield(L,-1, std::string(name).c_str()); //[-1 = 2] -> [3] = the value returned from if <ClassName> exists in the table to not gc
+        char name[max_pointer_string_size];
+        tostring(name, max_pointer_string_size, obj);
+        lua_getfield(L,-1,name); //[-1 = 2] -> [3] = the value returned from if <ClassName> exists in the table to not gc
         if(lua_isnoneornil(L,-1) ) //[-1 = 3] if it doesn't exist, then we are free to garbage collect c++ side
-        {
-            if(!IsReferenceCounted<T>())
-            {
-                delete obj;
-                obj = nullptr;
-            }
-        }
+		{
+			delete obj;
+			obj = nullptr;
+		}
     }
     lua_pop(L,3); //balance function
     return 0;
@@ -209,10 +192,10 @@ int LuaType<T>::gc_T(lua_State* L)
 template<typename T>
 int LuaType<T>::tostring_T(lua_State* L)
 {
-    char buff[32];
+    char buff[max_pointer_string_size];
     T** ptrHold = (T**)lua_touserdata(L,1);
     T *obj = *ptrHold;
-    sprintf(buff, "%p", obj);
+    snprintf(buff, max_pointer_string_size, "%p", obj);
     lua_pushfstring(L, "%s (%s)", GetTClassName<T>(), buff);
     return 1;
 }

+ 9 - 9
Samples/luainvaders/data/high_score.rml

@@ -21,7 +21,7 @@
 				margin-bottom: 20px;
 			}
 			
-			datagrid data_grid_body
+			datagrid datagridbody
 			{
 				min-height: 200px;
 			}
@@ -44,7 +44,7 @@ function HighScore.OnRowAdd(_,_,document)
 	input = document:GetElementById('player_input')
 	if input then
 		input:Focus()
-    end
+	end
 end
 
 function HighScore.OnLoad(window)
@@ -55,18 +55,18 @@ end
 	
 function HighScore.OnKeyDown(event)
 	if event.parameters['key_identifier'] == rmlui.key_identifier.RETURN then
-        Game.SetHighScoreName(Element.As.ElementFormControlInput(event.current_element).value)
+		Game.SetHighScoreName(Element.As.ElementFormControlInput(event.current_element).value)
 		--Game.SetHighScoreName(event.current_element:AsType(Element.etype.input).value)
-    end
+	end
 end
 		</script>
 	</head>
 	<body template="luawindow" onload="HighScore.OnLoad(document) Game.SubmitHighScore()" onunload="Game.SetHighScoreName('Anon')">
-		<datagrid id="datagrid" source="high_scores.scores" min-rows"10">
-			<col fields="name,name_required" formatter="name" width="40%">Pilot:</col>
-			<col fields="colour" formatter="ship" width="20%">Ship:</col>
-			<col fields="wave" width="20%">Wave:</col>
-			<col fields="score" width="20%">Score:</col>
+		<datagrid id="datagrid" source="high_scores.scores" min-rows="10">
+			<col fields="name,name_required" formatter="name" width="40%">Pilot</col>
+			<col fields="colour" formatter="ship" width="20%">Ship</col>
+			<col fields="wave" width="20%">Wave</col>
+			<col fields="score" width="20%">Score</col>
 		</datagrid>
 		<button onclick="Window.LoadMenu('main_menu',document)">Main Menu</button>
 	</body>

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

@@ -89,7 +89,7 @@ function Options.DisplayBadGraphics(document, display)
 		document:GetElementById('bad_warning').style.display = 'block'
 	else
 		document:GetElementById('bad_warning').style.display = 'none'
-    end
+	end
 end
 
 	</script>
@@ -99,8 +99,8 @@ end
 			<div>
 				<p>
 					Graphics:<br />
-					<input id="good" type="radio" name="graphics" value="good"/> Good<br />
-					<input id="ok" type="radio" name="graphics" value="ok" checked="true"/> OK<br />
+					<input id="good" type="radio" name="graphics" value="good" onchange="Options.DisplayBadGraphics(document, false)"/> Good<br />
+					<input id="ok" type="radio" name="graphics" value="ok" checked="true" onchange="Options.DisplayBadGraphics(document, false)"/> OK<br />
 					<input id="bad" type="radio" name="graphics" value="bad" onchange="Options.DisplayBadGraphics(document, true)" /> Bad<br />
 				</p>
 				<p id="bad_warning" style="display: none;">Are you sure about this? Bad graphics are just plain <em>bad.</em></p>

+ 2 - 8
Samples/luainvaders/src/ElementGameInstancer.cpp

@@ -6,9 +6,9 @@ ElementGameInstancer::~ElementGameInstancer()
 }
 	
 // Instances an element given the tag name and attributes
-Rml::Core::Element* ElementGameInstancer::InstanceElement(Rml::Core::Element* /*parent*/, const Rml::Core::String& tag, const Rml::Core::XMLAttributes& /*attributes*/)
+Rml::Core::ElementPtr ElementGameInstancer::InstanceElement(Rml::Core::Element* /*parent*/, const Rml::Core::String& tag, const Rml::Core::XMLAttributes& /*attributes*/)
 {
-	return new ElementGame(tag);
+	return Rml::Core::ElementPtr(new ElementGame(tag));
 }
 
 
@@ -20,9 +20,3 @@ void ElementGameInstancer::ReleaseElement(Rml::Core::Element* element)
 }
 
 
-
-// Release the instancer
-void ElementGameInstancer::Release()
-{
-	delete this;
-}

+ 2 - 5
Samples/luainvaders/src/ElementGameInstancer.h

@@ -10,14 +10,11 @@ public:
 	/// Instances an element given the tag name and attributes
 	/// @param tag Name of the element to instance
 	/// @param attributes vector of name value pairs
-    virtual Rml::Core::Element* InstanceElement(Rml::Core::Element* parent, const Rml::Core::String& tag, const Rml::Core::XMLAttributes& attributes);
+    Rml::Core::ElementPtr InstanceElement(Rml::Core::Element* parent, const Rml::Core::String& tag, const Rml::Core::XMLAttributes& attributes) override;
 
 	/// Releases the given element
 	/// @param element to release
-	virtual void ReleaseElement(Rml::Core::Element* element);
-
-	/// Release the instancer
-	virtual void Release();
+	void ReleaseElement(Rml::Core::Element* element) override;
 };
 
 #endif

+ 3 - 1
Samples/luainvaders/src/LuaInterface.cpp

@@ -19,10 +19,12 @@ int GameSetDefenderColour(lua_State* L);
 int GameSubmitHighScore(lua_State* L);
 int GameSetHighScoreName(lua_State* L);
 
+static ElementGameInstancer game_instancer;
+
 void LuaInterface::Initialise(lua_State* L)
 {
     InitGame(L);
-    Rml::Core::Factory::RegisterElementInstancer("game",new ElementGameInstancer())->RemoveReference();
+    Rml::Core::Factory::RegisterElementInstancer("game", &game_instancer);
 }
 
 void LuaInterface::InitGame(lua_State *L)

+ 7 - 6
Samples/luainvaders/src/main.cpp

@@ -115,8 +115,10 @@ int main(int, char**)
 	Shell::LoadFonts("assets/");
 
 	// Register Invader's custom decorator instancers.
-	Rml::Core::Factory::RegisterDecoratorInstancer("starfield", std::make_unique<DecoratorInstancerStarfield>());
-	Rml::Core::Factory::RegisterDecoratorInstancer("defender", std::make_unique<DecoratorInstancerDefender>());
+	DecoratorInstancerStarfield decorator_starfield;
+	DecoratorInstancerDefender decorator_defender;
+	Rml::Core::Factory::RegisterDecoratorInstancer("starfield", &decorator_starfield);
+	Rml::Core::Factory::RegisterDecoratorInstancer("defender", &decorator_defender);
 
 	// Construct the game singletons.
 	HighScores::Initialise();
@@ -127,10 +129,9 @@ int main(int, char**)
 
 	Shell::EventLoop(GameLoop);	
 
-	// Shutdown the RmlUi contexts.	
-	context->RemoveReference();
-	
-	// Shutdown Lua  before we shut down RmlUi.
+	Rml::Core::RemoveContext(context->GetName());
+
+	// Shutdown Lua before we shut down RmlUi.
 	Rml::Core::Lua::Interpreter::Shutdown();
 
 	// Shut down the game singletons.

+ 1 - 1
Source/Controls/Lua/DataFormatter.cpp

@@ -116,7 +116,7 @@ template<> void ExtraInit<DataFormatter>(lua_State* L, int metatable_index)
     lua_setfield(L,metatable_index-1,"new");
     return;
 }
-LUACONTROLSTYPEDEFINE(DataFormatter,false)
+LUACONTROLSTYPEDEFINE(DataFormatter)
 }
 }
 }

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

@@ -146,7 +146,7 @@ template<> void ExtraInit<Rml::Controls::Lua::LuaDataSource>(lua_State* L, int m
     return;
 }
 using Rml::Controls::Lua::DataSource;
-LUACONTROLSTYPEDEFINE(DataSource,false)
+LUACONTROLSTYPEDEFINE(DataSource)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementDataGrid.cpp

@@ -114,7 +114,7 @@ template<> void ExtraInit<Rml::Controls::ElementDataGrid>(lua_State* L, int meta
     AddTypeToElementAsTable<Rml::Controls::ElementDataGrid>(L);
 }
 using Rml::Controls::ElementDataGrid;
-LUACONTROLSTYPEDEFINE(ElementDataGrid,true)
+LUACONTROLSTYPEDEFINE(ElementDataGrid)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementDataGridRow.cpp

@@ -128,7 +128,7 @@ template<> void ExtraInit<Rml::Controls::ElementDataGridRow>(lua_State* L, int m
     AddTypeToElementAsTable<Rml::Controls::ElementDataGridRow>(L);
 }
 using Rml::Controls::ElementDataGridRow;
-LUACONTROLSTYPEDEFINE(ElementDataGridRow,true)
+LUACONTROLSTYPEDEFINE(ElementDataGridRow)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementForm.cpp

@@ -83,7 +83,7 @@ template<> void ExtraInit<Rml::Controls::ElementForm>(lua_State* L, int metatabl
     AddTypeToElementAsTable<Rml::Controls::ElementForm>(L);
 }
 using Rml::Controls::ElementForm;
-LUACONTROLSTYPEDEFINE(ElementForm,true)
+LUACONTROLSTYPEDEFINE(ElementForm)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementFormControl.cpp

@@ -126,7 +126,7 @@ template<> void ExtraInit<Rml::Controls::ElementFormControl>(lua_State* L, int m
     AddTypeToElementAsTable<Rml::Controls::ElementFormControl>(L);
 }
 using Rml::Controls::ElementFormControl;
-LUACONTROLSTYPEDEFINE(ElementFormControl,true)
+LUACONTROLSTYPEDEFINE(ElementFormControl)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementFormControlDataSelect.cpp

@@ -77,7 +77,7 @@ template<> void ExtraInit<Rml::Controls::ElementFormControlDataSelect>(lua_State
 }
 
 using Rml::Controls::ElementFormControlDataSelect;
-LUACONTROLSTYPEDEFINE(ElementFormControlDataSelect,true)
+LUACONTROLSTYPEDEFINE(ElementFormControlDataSelect)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementFormControlInput.cpp

@@ -186,7 +186,7 @@ template<> void ExtraInit<Rml::Controls::ElementFormControlInput>(lua_State* L,
     AddTypeToElementAsTable<Rml::Controls::ElementFormControlInput>(L);
 }
 using Rml::Controls::ElementFormControlInput;
-LUACONTROLSTYPEDEFINE(ElementFormControlInput,true)
+LUACONTROLSTYPEDEFINE(ElementFormControlInput)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementFormControlSelect.cpp

@@ -129,7 +129,7 @@ template<> void ExtraInit<Rml::Controls::ElementFormControlSelect>(lua_State* L,
     AddTypeToElementAsTable<Rml::Controls::ElementFormControlSelect>(L);
 }
 using Rml::Controls::ElementFormControlSelect;
-LUACONTROLSTYPEDEFINE(ElementFormControlSelect,true)
+LUACONTROLSTYPEDEFINE(ElementFormControlSelect)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementFormControlTextArea.cpp

@@ -145,7 +145,7 @@ template<> void ExtraInit<Rml::Controls::ElementFormControlTextArea>(lua_State*
 }
 
 using Rml::Controls::ElementFormControlTextArea;
-LUACONTROLSTYPEDEFINE(ElementFormControlTextArea,true)
+LUACONTROLSTYPEDEFINE(ElementFormControlTextArea)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/ElementTabSet.cpp

@@ -126,7 +126,7 @@ template<> void ExtraInit<Rml::Controls::ElementTabSet>(lua_State* L, int metata
 }
 
 using Rml::Controls::ElementTabSet;
-LUACONTROLSTYPEDEFINE(ElementTabSet,true)
+LUACONTROLSTYPEDEFINE(ElementTabSet)
 }
 }
 }

+ 1 - 1
Source/Controls/Lua/SelectOptionsProxy.cpp

@@ -131,7 +131,7 @@ template<> void ExtraInit<Rml::Controls::Lua::SelectOptionsProxy>(lua_State* L,
 }
 
 using Rml::Controls::Lua::SelectOptionsProxy;
-LUACONTROLSTYPEDEFINE(SelectOptionsProxy,false);
+LUACONTROLSTYPEDEFINE(SelectOptionsProxy);
 }
 }
 }

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

@@ -233,7 +233,7 @@ luaL_Reg ColourbSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Colourb,false)
+LUACORETYPEDEFINE(Colourb)
 }
 }
 }

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

@@ -203,7 +203,7 @@ luaL_Reg ColourfSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Colourf,false)
+LUACORETYPEDEFINE(Colourf)
 
 
 }

+ 2 - 3
Source/Core/Lua/Context.cpp

@@ -86,7 +86,7 @@ int ContextCreateDocument(lua_State* L, Context* obj)
     else
         tag = luaL_checkstring(L,1);
     Document* doc = obj->CreateDocument(tag);
-    LuaType<Document>::push(L,doc,true);
+    LuaType<Document>::push(L,doc,false);
     return 1;
 }
 
@@ -95,7 +95,6 @@ int ContextLoadDocument(lua_State* L, Context* obj)
     const char* path = luaL_checkstring(L,1);
     Document* doc = obj->LoadDocument(path);
     LuaType<Document>::push(L,doc,false);
-	doc->RemoveReference();
     return 1;
 }
 
@@ -221,7 +220,7 @@ luaL_Reg ContextSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Context,true)
+LUACORETYPEDEFINE(Context)
 }
 }
 }

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

@@ -148,7 +148,7 @@ luaL_Reg ContextDocumentsProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ContextDocumentsProxy,false)
+LUACORETYPEDEFINE(ContextDocumentsProxy)
 
 }
 }

+ 5 - 7
Source/Core/Lua/Document.cpp

@@ -102,9 +102,8 @@ int DocumentClose(lua_State* L, Document* obj)
 int DocumentCreateElement(lua_State* L, Document* obj)
 {
     const char* tag = luaL_checkstring(L,1);
-    Element* ele = obj->CreateElement(tag);
-    LuaType<Element>::push(L,ele,true);
-    ele->RemoveReference();
+    ElementPtr* ele = new ElementPtr( obj->CreateElement(tag) );
+    LuaType<ElementPtr>::push(L,ele,true);
     return 1;
 }
 
@@ -112,9 +111,8 @@ int DocumentCreateTextNode(lua_State* L, Document* obj)
 {
     //need ElementText object first
     const char* text = luaL_checkstring(L,1);
-    ElementText* et = obj->CreateTextNode(text);
-    LuaType<ElementText>::push(L, et, true);
-    et->RemoveReference();
+    ElementPtr* et = new ElementPtr( obj->CreateTextNode(text) );
+    LuaType<ElementPtr>::push(L, et, true);
 	return 1;
 }
 
@@ -173,7 +171,7 @@ luaL_Reg DocumentSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Document,true)
+LUACORETYPEDEFINE(Document)
 }
 }
 }

+ 52 - 9
Source/Core/Lua/Element.cpp

@@ -61,7 +61,6 @@ int Elementnew(lua_State* L)
     const char* tag = luaL_checkstring(L,1);
     Element* ele = new Element(tag);
     LuaType<Element>::push(L,ele,true);
-    ele->RemoveReference();
     return 1;
 }
 
@@ -101,8 +100,11 @@ int ElementAddEventListener(lua_State* L, Element* obj)
 
 int ElementAppendChild(lua_State* L, Element* obj)
 {
-    Element* ele = LuaType<Element>::check(L,1);
-    obj->AppendChild(ele);
+	ElementPtr* element = LuaType<ElementPtr>::check(L, 1);
+	if (*element)
+		obj->AppendChild(std::move(*element));
+	else
+		Log::Message(Log::LT_WARNING, "Could not append child to element '%s', as the child was null. Was it already moved from?", obj->GetAddress().c_str());
     return 0;
 }
 
@@ -203,9 +205,12 @@ int ElementHasChildNodes(lua_State* L, Element* obj)
 
 int ElementInsertBefore(lua_State* L, Element* obj)
 {
-    Element* element = LuaType<Element>::check(L,1);
+    ElementPtr* element = LuaType<ElementPtr>::check(L,1);
     Element* adjacent = LuaType<Element>::check(L,2);
-    obj->InsertBefore(element,adjacent);
+	if(*element)
+		obj->InsertBefore(std::move(*element), adjacent);
+	else
+		Log::Message(Log::LT_WARNING, "Could not insert child to element '%s', as the child was null. Was it already moved from?", obj->GetAddress().c_str());
     return 0;
 }
 
@@ -226,15 +231,18 @@ int ElementRemoveAttribute(lua_State* L, Element* obj)
 int ElementRemoveChild(lua_State* L, Element* obj)
 {
     Element* element = LuaType<Element>::check(L,1);
-    lua_pushboolean(L,obj->RemoveChild(element));
+    lua_pushboolean(L,static_cast<bool>(obj->RemoveChild(element)));
     return 1;
 }
 
 int ElementReplaceChild(lua_State* L, Element* obj)
 {
-    Element* inserted = LuaType<Element>::check(L,1);
+    ElementPtr* inserted = LuaType<ElementPtr>::check(L,1);
     Element* replaced = LuaType<Element>::check(L,2);
-    lua_pushboolean(L,obj->ReplaceChild(inserted,replaced));
+	if(*inserted)
+		lua_pushboolean(L, static_cast<bool>(obj->ReplaceChild(std::move(*inserted),replaced)));
+	else
+		Log::Message(Log::LT_WARNING, "Could not replace child in element '%s', as the child was null. Was it already moved from?", obj->GetAddress().c_str());
     return 1;
 }
 
@@ -614,7 +622,42 @@ luaL_Reg ElementSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Element,true)
+LUACORETYPEDEFINE(Element)
+
+
+
+
+
+
+
+
+
+
+
+template<> void ExtraInit<ElementPtr>(lua_State* L, int metatable_index)
+{
+	return;
+}
+
+RegType<ElementPtr> ElementPtrMethods[] =
+{
+	{ nullptr, nullptr },
+};
+
+luaL_Reg ElementPtrGetters[] =
+{
+	{ nullptr, nullptr },
+};
+
+luaL_Reg ElementPtrSetters[] =
+{
+	{ nullptr, nullptr },
+};
+
+LUACORETYPEDEFINE(ElementPtr)
+
+
+
 }
 }
 }

+ 11 - 0
Source/Core/Lua/Element.h

@@ -102,6 +102,17 @@ extern luaL_Reg ElementGetters[];
 extern luaL_Reg ElementSetters[];
 
 LUACORETYPEDECLARE(Element)
+
+
+template<> void ExtraInit<ElementPtr>(lua_State* L, int metatable_index);
+int LogMessage(lua_State* L);
+
+extern RegType<ElementPtr> ElementPtrMethods[];
+extern luaL_Reg ElementPtrGetters[];
+extern luaL_Reg ElementPtrSetters[];
+
+LUACORETYPEDECLARE(ElementPtr)
+
 }
 }
 }

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

@@ -108,7 +108,7 @@ luaL_Reg ElementAttributesProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ElementAttributesProxy,false)
+LUACORETYPEDEFINE(ElementAttributesProxy)
 }
 }
 }

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

@@ -104,7 +104,7 @@ luaL_Reg ElementChildNodesProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ElementChildNodesProxy,false)
+LUACORETYPEDEFINE(ElementChildNodesProxy)
 }
 }
 }

+ 1 - 2
Source/Core/Lua/ElementInstancer.cpp

@@ -45,7 +45,6 @@ int ElementInstancernew(lua_State* L)
 {
     LuaElementInstancer* lei = new LuaElementInstancer(L);
     LuaType<ElementInstancer>::push(L,lei,true);
-    lei->RemoveReference();
     return 1;
 }
 
@@ -83,7 +82,7 @@ luaL_Reg ElementInstancerSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ElementInstancer,true)
+LUACORETYPEDEFINE(ElementInstancer)
 }
 }
 }

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

@@ -156,7 +156,7 @@ luaL_Reg ElementStyleProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ElementStyleProxy,false)
+LUACORETYPEDEFINE(ElementStyleProxy)
 }
 }
 }

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

@@ -76,7 +76,7 @@ luaL_Reg ElementTextSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(ElementText,true)
+LUACORETYPEDEFINE(ElementText)
 }
 }
 }

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

@@ -105,7 +105,7 @@ luaL_Reg EventSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Event,true)
+LUACORETYPEDEFINE(Event)
 }
 }
 }

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

@@ -111,7 +111,7 @@ luaL_Reg EventParametersProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(EventParametersProxy,false)
+LUACORETYPEDEFINE(EventParametersProxy)
 }
 }
 }

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

@@ -88,6 +88,7 @@ void Interpreter::RegisterCoreTypes(lua_State* L)
         //things that inherit from Element
         LuaType<Document>::Register(L);
         LuaType<ElementText>::Register(L);
+    LuaType<ElementPtr>::Register(L);
     LuaType<Event>::Register(L);
     LuaType<Context>::Register(L);
     LuaType<LuaRmlUi>::Register(L);
@@ -210,15 +211,24 @@ int Interpreter::GetEventClasses()
     return EVT_BASIC;
 }
 
+static LuaDocumentElementInstancer* g_lua_document_element_instancer = nullptr;
+static LuaEventListenerInstancer* g_lua_event_listener_instancer = nullptr;
+
 void Interpreter::OnInitialise()
 {
     Startup();
-    Factory::RegisterElementInstancer("body",new LuaDocumentElementInstancer())->RemoveReference();
-    Factory::RegisterEventListenerInstancer(new LuaEventListenerInstancer())->RemoveReference();
+	g_lua_document_element_instancer = new LuaDocumentElementInstancer();
+	g_lua_event_listener_instancer = new LuaEventListenerInstancer();
+    Factory::RegisterElementInstancer("body", g_lua_document_element_instancer);
+	Factory::RegisterEventListenerInstancer(g_lua_event_listener_instancer);
 }
 
 void Interpreter::OnShutdown()
 {
+	delete g_lua_document_element_instancer;
+	delete g_lua_event_listener_instancer;
+	g_lua_document_element_instancer = nullptr;
+	g_lua_event_listener_instancer = nullptr;
 }
 
 void Interpreter::Initialise()

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

@@ -95,7 +95,7 @@ luaL_Reg LogSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Log,false)
+LUACORETYPEDEFINE(Log)
 }
 }
 }

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

@@ -50,21 +50,21 @@ LuaElementInstancer::LuaElementInstancer(lua_State* L) : ElementInstancer(), ref
     lua_pop(L,1); //pop the ELEMENTINSTANCERFUNCTIONS table
 }
 
-Element* LuaElementInstancer::InstanceElement(Element* RMLUI_UNUSED_PARAMETER(parent), const String& tag, const XMLAttributes& RMLUI_UNUSED_PARAMETER(attributes))
+ElementPtr LuaElementInstancer::InstanceElement(Element* RMLUI_UNUSED_PARAMETER(parent), const String& tag, const XMLAttributes& RMLUI_UNUSED_PARAMETER(attributes))
 {
     RMLUI_UNUSED(parent);
     RMLUI_UNUSED(attributes);
 
     lua_State* L = Interpreter::GetLuaState();
     int top = lua_gettop(L);
-    Element* ret = nullptr;
+    ElementPtr ret = nullptr;
     if(ref_InstanceElement != LUA_REFNIL && ref_InstanceElement != LUA_NOREF)
     {
         PushFunctionsTable(L);
         lua_rawgeti(L,-1,ref_InstanceElement); //push the function
         lua_pushstring(L,tag.c_str()); //push the tag
         Interpreter::ExecuteCall(1,1); //we pass in a string, and we want to get an Element back
-        ret = LuaType<Element>::check(L,-1);
+        ret = std::move(*LuaType<ElementPtr>::check(L,-1));
     }
     else
     {
@@ -79,11 +79,6 @@ void LuaElementInstancer::ReleaseElement(Element* element)
     delete element;
 }
 
-void LuaElementInstancer::Release()
-{
-    delete this;
-}
-
 void LuaElementInstancer::PushFunctionsTable(lua_State* L)
 {
     //make sure there is an area to save the function

+ 2 - 4
Source/Core/Lua/LuaElementInstancer.h

@@ -45,12 +45,10 @@ public:
 	/// @param[in] parent The element the new element is destined to be parented to.
 	/// @param[in] tag The tag of the element to instance.
 	/// @param[in] attributes Dictionary of attributes.
-	virtual Element* InstanceElement(Element* parent, const String& tag, const XMLAttributes& attributes);
+	ElementPtr InstanceElement(Element* parent, const String& tag, const XMLAttributes& attributes) override;
 	/// Releases an element instanced by this instancer.
 	/// @param[in] element The element to release.
-	virtual void ReleaseElement(Element* element);
-	/// Release the instancer.
-	virtual void Release();
+	void ReleaseElement(Element* element) override;
 
     int ref_InstanceElement;
 

+ 1 - 4
Source/Core/Lua/LuaEventListener.cpp

@@ -109,10 +109,7 @@ LuaEventListener::LuaEventListener(lua_State* L, int narg, Element* element)
 
 LuaEventListener::~LuaEventListener()
 {
-    if(attached)
-        attached->RemoveReference();
-    if(parent)
-        parent->RemoveReference();
+
 }
 
 /// Process the incoming Event

+ 1 - 1
Source/Core/Lua/LuaEventListener.h

@@ -53,7 +53,7 @@ public:
     virtual ~LuaEventListener();
 
     /// Process the incoming Event
-	virtual void ProcessEvent(Event& event) override;
+	void ProcessEvent(Event& event) override;
 private:
     //the lua-side function to call when ProcessEvent is called
     int luaFuncRef;

+ 0 - 6
Source/Core/Lua/LuaEventListenerInstancer.cpp

@@ -42,12 +42,6 @@ EventListener* LuaEventListenerInstancer::InstanceEventListener(const String& va
     return new LuaEventListener(value,element);
 }
 
-/// Releases this event listener instancer.
-void LuaEventListenerInstancer::Release()
-{
-    delete this;
-}
-
 }
 }
 }

+ 1 - 4
Source/Core/Lua/LuaEventListenerInstancer.h

@@ -40,10 +40,7 @@ public:
     /// Instance an event listener object.
 	/// @param value Value of the event.
 	/// @param element Element that triggers the events.
-	virtual EventListener* InstanceEventListener(const String& value, Element* element);
-
-	/// Releases this event listener instancer.
-	virtual void Release();
+	EventListener* InstanceEventListener(const String& value, Element* element) override;
 };
 
 }

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

@@ -325,7 +325,7 @@ luaL_Reg LuaRmlUiSetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(LuaRmlUi,false)
+LUACORETYPEDEFINE(LuaRmlUi)
 }
 }
 }

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

@@ -136,7 +136,7 @@ luaL_Reg RmlUiContextsProxySetters[] =
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(RmlUiContextsProxy,false)
+LUACORETYPEDEFINE(RmlUiContextsProxy)
 }
 }
 }

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

@@ -238,7 +238,7 @@ luaL_Reg Vector2fSetters[]=
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Vector2f,false)
+LUACORETYPEDEFINE(Vector2f)
 
 }
 }

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

@@ -202,7 +202,7 @@ luaL_Reg Vector2iSetters[]=
     { nullptr, nullptr },
 };
 
-LUACORETYPEDEFINE(Vector2i,false)
+LUACORETYPEDEFINE(Vector2i)
 
 }
 }