Browse Source

Removed love.event.addListener/removeListener/clearListeners.

They take away too much control from the main game code, which makes it harder to implement things like gamestates without modifying library code.
Alex Szpakowski 9 years ago
parent
commit
925eebdaf0

+ 0 - 2
platform/xcode/liblove.xcodeproj/project.pbxproj

@@ -920,7 +920,6 @@
 
 /* Begin PBXFileReference section */
 		503971A86B7167A91B670FBA /* boot.lua.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = boot.lua.h; sourceTree = "<group>"; };
-		FA024C1C1C0D81CD00567EB0 /* wrap_Event.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = wrap_Event.lua; sourceTree = "<group>"; };
 		FA08F5AE16C7525600F007B5 /* liblove-macosx.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "liblove-macosx.plist"; path = "macosx/liblove-macosx.plist"; sourceTree = "<group>"; };
 		FA0B78DD1A958B90000E1D17 /* liblove.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblove.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		FA0B78F71A958E3B000E1D17 /* b64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b64.cpp; sourceTree = "<group>"; };
@@ -2171,7 +2170,6 @@
 				FA0B7B551A95902C000E1D17 /* sdl */,
 				FA8951A01AA2EDF300EC385A /* wrap_Event.cpp */,
 				FA8951A11AA2EDF300EC385A /* wrap_Event.h */,
-				FA024C1C1C0D81CD00567EB0 /* wrap_Event.lua */,
 			);
 			path = event;
 			sourceTree = "<group>";

+ 1 - 11
src/common/runtime.cpp

@@ -243,17 +243,7 @@ int luax_register_module(lua_State *L, const WrappedModule &m)
 
 	// Register all the functions.
 	if (m.functions != nullptr)
-	{
-		const luaL_Reg *l = m.functions;
-
-		for (; l->name != nullptr; l++)
-		{
-			// Set the module's table as an upvalue for the module's functions.
-			lua_pushvalue(L, -1);
-			lua_pushcclosure(L, l->func, 1);
-			lua_setfield(L, -2, l->name);
-		}
-	}
+		luax_setfuncs(L, m.functions);
 
 	// Register types.
 	if (m.types != nullptr)

+ 0 - 2
src/modules/event/Event.h

@@ -49,8 +49,6 @@ public:
 	int toLua(lua_State *L);
 	static Message *fromLua(lua_State *L, int n);
 
-	const std::string &getName() const { return name; }
-
 private:
 
 	std::string name;

+ 14 - 59
src/modules/event/wrap_Event.cpp

@@ -25,11 +25,6 @@
 
 #include "sdl/Event.h"
 
-// Put the Lua code directly into a raw string literal.
-static const char event_lua[] =
-#include "wrap_Event.lua"
-;
-
 namespace love
 {
 namespace event
@@ -37,50 +32,16 @@ namespace event
 
 #define instance() (Module::getInstance<Event>(Module::M_EVENT))
 
-static const char *listenersKey = "_listeners";
-
-static int processMessage(lua_State *L, Message *m)
-{
-	if (m == nullptr)
-		return 0;
-
-	int args = m->toLua(L);
-
-	// Push the love.event[listenersKey][eventname] table onto the stack.
-	lua_getfield(L, lua_upvalueindex(1), listenersKey);
-	lua_getfield(L, -1, m->getName().c_str());
-
-	if (lua_istable(L, -1))
-	{
-		int len = (int) luax_objlen(L, -1);
-
-		// Each array entry in the table is a listener function we should call.
-		for (int i = 1; i <= len; i++)
-		{
-			lua_rawgeti(L, -1, i);
-
-			// Push all message args (including the event name), in order.
-			for (int j = 1; j <= args; j++)
-				lua_pushvalue(L, -(args + 3));
-
-			lua_call(L, args, 0);
-		}
-	}
-
-	lua_pop(L, 2);
-
-	m->release();
-
-	// Leave the message's args on the stack.
-	return args;
-}
-
 static int w_poll_i(lua_State *L)
 {
 	Message *m = nullptr;
 
 	if (instance()->poll(m))
-		return processMessage(L, m);
+	{
+		int args = m->toLua(L);
+		m->release();
+		return args;
+	}
 
 	// No pending events.
 	return 0;
@@ -88,9 +49,7 @@ static int w_poll_i(lua_State *L)
 
 int w_poll(lua_State *L)
 {
-	// w_poll_i needs access to the love.event table via an upvalue.
-	lua_pushvalue(L, lua_upvalueindex(1));
-	lua_pushcclosure(L, w_poll_i, 1);
+	lua_pushcclosure(L, &w_poll_i, 0);
 	return 1;
 }
 
@@ -103,7 +62,14 @@ int w_pump(lua_State *)
 int w_wait(lua_State *L)
 {
 	Message *m = instance()->wait();
-	return processMessage(L, m);
+	if (m)
+	{
+		int args = m->toLua(L);
+		m->release();
+		return args;
+	}
+
+	return 0;
 }
 
 int w_push(lua_State *L)
@@ -149,7 +115,6 @@ int w_quit(lua_State *L)
 // List of functions to wrap.
 static const luaL_Reg functions[] =
 {
-	// addListener and removeListener are defined in wrap_Event.lua
 	{ "pump", w_pump },
 	{ "poll", w_poll },
 	{ "wait", w_wait },
@@ -178,16 +143,6 @@ extern "C" int luaopen_love_event(lua_State *L)
 
 	int ret = luax_register_module(L, w);
 
-	// love.event[listenersKey] = {}
-	lua_newtable(L);
-	lua_setfield(L, -2, listenersKey);
-
-	// Execute wrap_Event.lua, sending the event and listeners tables as args.
-	luaL_loadbuffer(L, event_lua, sizeof(event_lua), "wrap_Event.lua");
-	lua_pushvalue(L, -2);
-	lua_getfield(L, -1, listenersKey);
-	lua_call(L, 2, 0);
-
 	return ret;
 }
 

+ 0 - 103
src/modules/event/wrap_Event.lua

@@ -1,103 +0,0 @@
-R"luastring"--(
--- DO NOT REMOVE THE ABOVE LINE. It is used to load this file as a C++ string.
--- There is a matching delimiter at the bottom of the file.
-
---[[
-Copyright (c) 2006-2015 LOVE Development Team
-
-This software is provided 'as-is', without any express or implied
-warranty.  In no event will the authors be held liable for any damages
-arising from the use of this software.
-
-Permission is granted to anyone to use this software for any purpose,
-including commercial applications, and to alter it and redistribute it
-freely, subject to the following restrictions:
-
-1. The origin of this software must not be misrepresented; you must not
-claim that you wrote the original software. If you use this software
-in a product, an acknowledgment in the product documentation would be
-appreciated but is not required.
-2. Altered source versions must be plainly marked as such, and must not be
-misrepresented as being the original software.
-3. This notice may not be removed or altered from any source distribution.
---]]
-
-local event, listeners = ...
-
-local type, select, error = type, select, error
-local pairs, ipairs = pairs, ipairs
-local table_insert, table_remove = table.insert, table.remove
-local max = math.max
-
-local function addListenerForEvent(event, func)
-	listeners[event] = listeners[event] or {}
-
-	-- Don't add a function to the event's array more than once.
-	for i,v in ipairs(listeners[event]) do
-		if v == func then
-			return
-		end
-	end
-
-	table_insert(listeners[event], func)
-end
-
-function event.addListener(func, ...)
-	if type(func) ~= "function" then
-		error("Invalid argument #1 to addListener (expected function)", 2)
-	end
-
-	local t = (...)
-	if type(t) == "table" then
-		for i, name in ipairs(t) do
-			if type(name) ~= "string" then
-				error("Invalid table element #"..i.." in argument #2 to addListener (expected string)", 2)
-			end
-
-			addListenerForEvent(name, func)
-		end
-	else
-		local n = select("#", ...)
-		for i = 1, max(n, 1) do
-			local name = select(i, ...)
-
-			if type(name) ~= "string" then
-				error("Invalid argument #"..(i+1).." to addListener (expected string)", 2)
-			end
-
-			addListenerForEvent(name, func)
-		end
-	end
-
-	return func
-end
-
-function event.removeListener(func)
-	if type(func) ~= "function" then
-		error("Invalid argument #1 to removeListener (expected function)", 2)
-	end
-
-	local success = false
-
-	-- Remove the listener function from all event names it was subscribed to.
-	for eventname, eventlisteners in pairs(listeners) do
-		for i=#eventlisteners, 1, -1 do
-			if eventlisteners[i] == func then
-				table_remove(eventlisteners, i)
-				success = true
-				break
-			end
-		end
-	end
-
-	return success
-end
-
-function event.clearListeners()
-	for k in pairs(listeners) do
-		listeners[k] = nil
-	end
-end
-
--- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
---)luastring"--"

+ 0 - 1
src/scripts/boot.lua

@@ -591,7 +591,6 @@ function love.errhand(msg)
 			v:setVibration()
 		end
 	end
-	love.event.clearListeners()
 	if love.audio then love.audio.stop() end
 	love.graphics.reset()
 	local font = love.graphics.setNewFont(math.floor(love.window.toPixels(14)))

+ 0 - 2
src/scripts/boot.lua.h

@@ -1076,8 +1076,6 @@ const unsigned char boot_lua[] =
 	0x29, 0x0a,
 	0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
 	0x09, 0x65, 0x6e, 0x64, 0x0a,
-	0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x63, 0x6c, 0x65, 0x61, 0x72, 0x4c, 
-	0x69, 0x73, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x73, 0x28, 0x29, 0x0a,
 	0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x74, 0x68, 0x65, 
 	0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x2e, 0x73, 0x74, 0x6f, 0x70, 0x28, 
 	0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a,