mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-25 12:08:21 +01:00
commit
c28fa1d309
@ -50,7 +50,7 @@ function socket.protect(f)
|
|||||||
return function(...)
|
return function(...)
|
||||||
local co = coroutine.create(f)
|
local co = coroutine.create(f)
|
||||||
while true do
|
while true do
|
||||||
local results = {coroutine.resume(co, base.unpack(arg))}
|
local results = {coroutine.resume(co, ...)}
|
||||||
local status = table.remove(results, 1)
|
local status = table.remove(results, 1)
|
||||||
if not status then
|
if not status then
|
||||||
if base.type(results[1]) == 'table' then
|
if base.type(results[1]) == 'table' then
|
||||||
@ -104,8 +104,7 @@ local function cowrap(dispatcher, tcp, error)
|
|||||||
-- don't override explicitly.
|
-- don't override explicitly.
|
||||||
local metat = { __index = function(table, key)
|
local metat = { __index = function(table, key)
|
||||||
table[key] = function(...)
|
table[key] = function(...)
|
||||||
arg[1] = tcp
|
return tcp[key](tcp,select(2,...))
|
||||||
return tcp[key](base.unpack(arg))
|
|
||||||
end
|
end
|
||||||
return table[key]
|
return table[key]
|
||||||
end}
|
end}
|
||||||
|
@ -126,8 +126,9 @@ local function chain2(f1, f2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function filter.chain(...)
|
function filter.chain(...)
|
||||||
|
local arg = {...}
|
||||||
local f = arg[1]
|
local f = arg[1]
|
||||||
for i = 2, table.getn(arg) do
|
for i = 2, #arg do
|
||||||
f = chain2(f, arg[i])
|
f = chain2(f, arg[i])
|
||||||
end
|
end
|
||||||
return f
|
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.
|
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(...)
|
function source.cat(...)
|
||||||
|
local arg = {...}
|
||||||
local co = coroutine.create(function()
|
local co = coroutine.create(function()
|
||||||
local i = 1
|
local i = 1
|
||||||
while i <= table.getn(arg) do
|
while i <= #arg do
|
||||||
local chunk, err = arg[i]()
|
local chunk, err = arg[i]()
|
||||||
if chunk then coroutine.yield(chunk)
|
if chunk then coroutine.yield(chunk)
|
||||||
elseif err then return nil, err
|
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:
|
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, ...)
|
local function pack(ok, ...)
|
||||||
return ok, arg
|
return ok, {...}
|
||||||
end
|
end
|
||||||
|
|
||||||
function protect(f)
|
function protect(f)
|
||||||
return function(...)
|
return function(...)
|
||||||
local ok, ret = pack(pcall(f, unpack(arg)))
|
local ok, ret = pack(pcall(f, ...))
|
||||||
if ok then return unpack(ret)
|
if ok then return unpack(ret)
|
||||||
else return nil, ret[1] end
|
else return nil, ret[1] end
|
||||||
end
|
end
|
||||||
@ -157,7 +157,7 @@ function newtry(f)
|
|||||||
if f then f() end
|
if f then f() end
|
||||||
error(arg[2], 0)
|
error(arg[2], 0)
|
||||||
else
|
else
|
||||||
return unpack(arg)
|
return ...
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,24 +4,24 @@ local socket = require"socket.unix"
|
|||||||
host = "luasocket"
|
host = "luasocket"
|
||||||
|
|
||||||
function pass(...)
|
function pass(...)
|
||||||
local s = string.format(unpack(arg))
|
local s = string.format(...)
|
||||||
io.stderr:write(s, "\n")
|
io.stderr:write(s, "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
function fail(...)
|
function fail(...)
|
||||||
local s = string.format(unpack(arg))
|
local s = string.format(...)
|
||||||
io.stderr:write("ERROR: ", s, "!\n")
|
io.stderr:write("ERROR: ", s, "!\n")
|
||||||
socket.sleep(3)
|
socket.sleep(3)
|
||||||
os.exit()
|
os.exit()
|
||||||
end
|
end
|
||||||
|
|
||||||
function warn(...)
|
function warn(...)
|
||||||
local s = string.format(unpack(arg))
|
local s = string.format(...)
|
||||||
io.stderr:write("WARNING: ", s, "\n")
|
io.stderr:write("WARNING: ", s, "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
function remote(...)
|
function remote(...)
|
||||||
local s = string.format(unpack(arg))
|
local s = string.format(...)
|
||||||
s = string.gsub(s, "\n", ";")
|
s = string.gsub(s, "\n", ";")
|
||||||
s = string.gsub(s, "%s+", " ")
|
s = string.gsub(s, "%s+", " ")
|
||||||
s = string.gsub(s, "^%s*", "")
|
s = string.gsub(s, "^%s*", "")
|
||||||
|
Loading…
Reference in New Issue
Block a user