mirror of
				https://github.com/lunarmodules/luasocket.git
				synced 2025-10-31 02:15:38 +01:00 
			
		
		
		
	| @@ -50,7 +50,7 @@ function socket.protect(f) | ||||
|   return function(...) | ||||
|     local co = coroutine.create(f) | ||||
|     while true do | ||||
|       local results = {coroutine.resume(co, base.unpack(arg))} | ||||
|       local results = {coroutine.resume(co, ...)} | ||||
|       local status = table.remove(results, 1) | ||||
|       if not status then | ||||
|         if base.type(results[1]) == 'table' then | ||||
| @@ -104,8 +104,7 @@ local function cowrap(dispatcher, tcp, error) | ||||
|     -- don't override explicitly. | ||||
|     local metat = { __index = function(table, key) | ||||
|         table[key] = function(...) | ||||
|             arg[1] = tcp | ||||
|             return tcp[key](base.unpack(arg)) | ||||
|             return tcp[key](tcp,select(2,...)) | ||||
|         end | ||||
|         return table[key] | ||||
|     end} | ||||
|   | ||||
| @@ -126,8 +126,9 @@ local function chain2(f1, f2) | ||||
| end | ||||
|  | ||||
| function filter.chain(...) | ||||
|     local arg = {...} | ||||
|     local f = arg[1] | ||||
|     for i = 2, table.getn(arg) do | ||||
|     for i = 2, #arg do | ||||
|         f = chain2(f, arg[i]) | ||||
|     end | ||||
|     return f | ||||
| @@ -235,9 +236,10 @@ end | ||||
| We can make these ideas even more powerful if we use a new feature of Lua 5.0: coroutines.  Coroutines suffer from a great lack of advertisement, and I am going to play my part here.  Just like lexical scoping, coroutines taste odd at first, but once you get used with the concept, it can save your day. I have to admit that using coroutines to implement our file source would be overkill, so let's implement a concatenated source factory instead. | ||||
|         {{{ | ||||
| function source.cat(...) | ||||
|     local arg = {...} | ||||
|     local co = coroutine.create(function() | ||||
|         local i = 1 | ||||
|         while i <= table.getn(arg) do | ||||
|         while i <= #arg do | ||||
|             local chunk, err = arg[i]() | ||||
|             if chunk then coroutine.yield(chunk) | ||||
|             elseif err then return nil, err  | ||||
|   | ||||
| @@ -73,12 +73,12 @@ Fortunately, all these problems are very easy to solve and that's what we do in | ||||
| We used the {{pcall}} function to shield the user from errors that could be raised by the underlying implementation.  Instead of directly using {{pcall}} (and thus duplicating code) every time we prefer a factory that does the same job: | ||||
|         {{{ | ||||
| local function pack(ok, ...) | ||||
|     return ok, arg | ||||
|     return ok, {...} | ||||
| end | ||||
|  | ||||
| function protect(f) | ||||
|     return function(...) | ||||
|         local ok, ret = pack(pcall(f, unpack(arg))) | ||||
|         local ok, ret = pack(pcall(f, ...)) | ||||
|         if ok then return unpack(ret) | ||||
|         else return nil, ret[1] end | ||||
|     end | ||||
| @@ -157,7 +157,7 @@ function newtry(f) | ||||
|             if f then f() end  | ||||
|             error(arg[2], 0)   | ||||
|         else  | ||||
|             return unpack(arg)  | ||||
|             return ... | ||||
|         end | ||||
|     end | ||||
| end | ||||
|   | ||||
| @@ -4,24 +4,24 @@ local socket = require"socket.unix" | ||||
| host = "luasocket" | ||||
|  | ||||
| function pass(...) | ||||
|     local s = string.format(unpack(arg)) | ||||
|     local s = string.format(...) | ||||
|     io.stderr:write(s, "\n") | ||||
| end | ||||
|  | ||||
| function fail(...) | ||||
|     local s = string.format(unpack(arg)) | ||||
|     local s = string.format(...) | ||||
|     io.stderr:write("ERROR: ", s, "!\n") | ||||
| socket.sleep(3) | ||||
|     os.exit() | ||||
| end | ||||
|  | ||||
| function warn(...) | ||||
|     local s = string.format(unpack(arg)) | ||||
|     local s = string.format(...) | ||||
|     io.stderr:write("WARNING: ", s, "\n") | ||||
| end | ||||
|  | ||||
| function remote(...) | ||||
|     local s = string.format(unpack(arg)) | ||||
|     local s = string.format(...) | ||||
|     s = string.gsub(s, "\n", ";") | ||||
|     s = string.gsub(s, "%s+", " ") | ||||
|     s = string.gsub(s, "^%s*", "") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user