Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

base/lua: updates to be compatible with both Lua 5.2 and Lua 5.1

  • Loading branch information...
commit bc87b892f2a70e9614ea514ec818f8f6fee79004 1 parent 73175dd
Thomas Harning Jr authored April 10, 2012
4  CHANGELOG
... ...
@@ -1,4 +1,8 @@
1 1
 0.4.0 - WIP
  2
+ * Makes compatible with Lua 5.2
  3
+   * Remove use of 'module' in sources
  4
+   * Remove assumption that luaevent will set globals
  5
+     - Only 'core' will modify global table (creating luaevent.core)
2 6
  * Fixes bug in merge for receivePartial
3 7
 0.3.2 - 2011-07-06
4 8
  * Fixed recorded version in Lua
23  include/luaevent.h
... ...
@@ -1,9 +1,30 @@
1  
-/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
  1
+/* LuaEvent - Copyright (C) 2007,2012 Thomas Harning <harningt@gmail.com>
2 2
  * Licensed as LGPL - See doc/COPYING for details */
3 3
 #ifndef LUAEVENT_H
4 4
 #define LUAEVENT_H
5 5
 
6 6
 #include <lua.h>
  7
+
  8
+/* Workarounds for Lua 5.2 */
  9
+#if (LUA_VERSION_NUM == 502)
  10
+
  11
+#undef lua_equal
  12
+#define lua_equal(L,idx1,idx2) lua_compare(L, (idx1), (idx2), LUA_OPEQ)
  13
+
  14
+#undef lua_getfenv
  15
+#define lua_getfenv lua_getuservalue
  16
+#undef lua_setfenv
  17
+#define lua_setfenv lua_setuservalue
  18
+
  19
+#undef lua_objlen
  20
+#define lua_objlen lua_rawlen
  21
+
  22
+#undef luaL_register
  23
+#define luaL_register(L, n, f) \
  24
+	{ if ((n) == NULL) luaL_setfuncs(L, f, 0); else luaL_newlib(L, f); }
  25
+
  26
+#endif
  27
+
7 28
 #include <sys/types.h>
8 29
 #ifdef _WIN32
9 30
 #include <winsock2.h>
52  lua/luaevent.lua
@@ -2,15 +2,16 @@
2 2
 	LuaEvent - Copyright (C) 2007,2012 Thomas Harning <harningt@gmail.com>
3 3
 	Licensed as LGPL - See doc/COPYING for details.
4 4
 ]]
5  
-module("luaevent", package.seeall)
6  
-require("luaevent.core")
  5
+local _M = {}
  6
+local core = require("luaevent.core")
7 7
 
8  
-_NAME = "luaevent";
9  
-_VERSION = "0.3.2";
  8
+_M.core = core
  9
+_M._NAME = "luaevent"
  10
+_M._VERSION = "0.4.0"
10 11
 
11  
-local EV_READ = luaevent.core.EV_READ
12  
-local EV_WRITE = luaevent.core.EV_WRITE
13  
-local base = luaevent.core.new()
  12
+local EV_READ = core.EV_READ
  13
+local EV_WRITE = core.EV_WRITE
  14
+local base = core.new()
14 15
 
15 16
 local function addevent(...)
16 17
 	return base:addevent(...)
@@ -30,7 +31,7 @@ local function socketWait(sock, event)
30 31
 end
31 32
 
32 33
 
33  
-function send(sock, data, start, stop)
  34
+function _M.send(sock, data, start, stop)
34 35
 	local s, err
35 36
 	local from = start or 1
36 37
 	local sent = 0
@@ -41,7 +42,7 @@ function send(sock, data, start, stop)
41 42
 		socketWait(sock, EV_WRITE)
42 43
 	until false
43 44
 end
44  
-function receive(sock, pattern, part)
  45
+function _M.receive(sock, pattern, part)
45 46
 	local s, err
46 47
 	pattern = pattern or '*l'
47 48
 	repeat
@@ -52,17 +53,18 @@ function receive(sock, pattern, part)
52 53
 end
53 54
 -- same as above but with special treatment when reading chunks,
54 55
 -- unblocks on any data received.
55  
-function receivePartial(client, pattern)
  56
+function _M.receivePartial(client, pattern)
56 57
 	local s, err, part
57 58
 	pattern = pattern or "*l"
58 59
 	repeat
59  
-	s, err, part = client:receive(pattern)
60  
-	if s or ( (type(pattern)=="number") and part~="" and part ~=nil ) or 
61  
-		err ~= "timeout" then return s, err, part end
  60
+		s, err, part = client:receive(pattern)
  61
+		if s or ( (type(pattern)=="number") and part~="" and part ~=nil ) or err ~= "timeout" then
  62
+			return s, err, part
  63
+		end
62 64
 		socketWait(client, EV_READ)
63 65
 	until false
64 66
 end
65  
-function connect(sock, ...)
  67
+function _M.connect(sock, ...)
66 68
 	sock:settimeout(0)
67 69
 	local ret, err = sock:connect(...)
68 70
 	if ret or err ~= "timeout" then return ret, err end
@@ -74,7 +76,7 @@ function connect(sock, ...)
74 76
 	return ret, err
75 77
 end
76 78
 -- Deprecated..
77  
-function flush(sock)
  79
+function _M.flush(sock)
78 80
 end
79 81
 local function clientCoroutine(sock, handler)
80 82
 	-- Figure out what to do ......
@@ -96,30 +98,30 @@ local function serverCoroutine(sock, callback)
96 98
 		end
97 99
 	until false
98 100
 end
99  
-function addserver(sock, callback)
  101
+function _M.addserver(sock, callback)
100 102
 	local coro = coroutine.create(serverCoroutine)
101 103
 	assert(coroutine.resume(coro, sock, callback))
102 104
 end
103  
-function addthread(func, ...)
  105
+function _M.addthread(func, ...)
104 106
 	return coroutine.resume(coroutine.create(func), ...)
105 107
 end
106 108
 local _skt_mt = {__index = {
107 109
 	connect = function(self, ...)
108  
-		return connect(self.socket, ...)
  110
+		return _M.connect(self.socket, ...)
109 111
 	end,
110 112
 	send = function (self, data)
111  
-		return send(self.socket, data)
  113
+		return _M.send(self.socket, data)
112 114
 	end,
113 115
 	
114 116
 	receive = function (self, pattern)
115 117
 		if (self.timeout==0) then
116  
-  			return receivePartial(self.socket, pattern)
  118
+			return _M.receivePartial(self.socket, pattern)
117 119
   		end
118  
-		return receive(self.socket, pattern)
  120
+		return _M.receive(self.socket, pattern)
119 121
 	end,
120 122
 	
121 123
 	flush = function (self)
122  
-		return flush(self.socket)
  124
+		return _M.flush(self.socket)
123 125
 	end,
124 126
 	
125 127
 	settimeout = function (self,time)
@@ -132,7 +134,9 @@ local _skt_mt = {__index = {
132 134
 		self.socket:close()
133 135
 	end
134 136
 }}
135  
-function wrap(sock)
  137
+function _M.wrap(sock)
136 138
 	return setmetatable({socket = sock}, _skt_mt)
137 139
 end
138  
-loop = function(...) base:loop(...) end
  140
+_M.loop = function(...) base:loop(...) end
  141
+
  142
+return _M
2  test/basic.lua
... ...
@@ -1,4 +1,4 @@
1  
-require "luaevent"
  1
+local luaevent = require("luaevent")
2 2
 
3 3
 print("Version:", luaevent._NAME.." "..luaevent._VERSION)
4 4
 print("libevent version:", luaevent.core.libevent_version())
5  test/test.lua
@@ -3,8 +3,9 @@
3 3
 -- Run the test file and the connect to the server by telnet on the used port
4 4
 -- to stop the test just send the command "quit"
5 5
 
6  
-require"luaevent"
7  
-require"socket"
  6
+local luaevent = require("luaevent")
  7
+local socket = require("socket")
  8
+
8 9
 local oldPrint = print
9 10
 print = function(...)
10 11
 	oldPrint("SRV", ...)
5  test/testClient.lua
... ...
@@ -1,5 +1,6 @@
1  
-require"luaevent"
2  
-require"socket"
  1
+local luaevent = require("luaevent")
  2
+local socket = require("socket")
  3
+
3 4
 local oldPrint = print
4 5
 print = function(...)
5 6
 	oldPrint("CLT", ...)
6  test/timertest.lua
... ...
@@ -1,9 +1,9 @@
1  
-require("luaevent.core")
  1
+local core = require("luaevent.core")
2 2
 
3  
-c = luaevent.core.new()
  3
+c = core.new()
4 4
 local f = 100
5 5
 local function createEvent()
6  
-	return c:addevent(nil, luaevent.core.EV_TIMEOUT, function(ev) io.write(".." .. f) f = f - 1 if f < 0 then return -1 end collectgarbage() end, 0.01)
  6
+	return c:addevent(nil, core.EV_TIMEOUT, function(ev) io.write(".." .. f) f = f - 1 if f < 0 then return -1 end collectgarbage() end, 0.01)
7 7
 end
8 8
 ev = createEvent()
9 9
 print("TESTING Garbage-collect-safe version")

0 notes on commit bc87b89

Please sign in to comment.
Something went wrong with that request. Please try again.