diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2014-11-10 15:39:34 -0200 |
---|---|---|
committer | Diego Nehab <diego.nehab@gmail.com> | 2014-11-10 15:39:34 -0200 |
commit | 583257c28cdf2204204f93c39f8428e84f06c9f2 (patch) | |
tree | 3f738c514fc9bc1cfb8e5c9a2d2b83001f491576 | |
parent | 6dcecd8f45fd3d0143b33548553c1d1f7dabc0b0 (diff) | |
parent | 7006ae120da2f8ec74323422541cd585c91832d6 (diff) | |
download | luasocket-583257c28cdf2204204f93c39f8428e84f06c9f2.tar.gz luasocket-583257c28cdf2204204f93c39f8428e84f06c9f2.tar.bz2 luasocket-583257c28cdf2204204f93c39f8428e84f06c9f2.zip |
Merge pull request #113 from siffiejoe/yieldable_protect51
fixed yieldable socket.protect in etc/dispatch.lua
-rw-r--r-- | etc/dispatch.lua | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/etc/dispatch.lua b/etc/dispatch.lua index cab7f59..2485415 100644 --- a/etc/dispatch.lua +++ b/etc/dispatch.lua | |||
@@ -5,6 +5,7 @@ | |||
5 | ----------------------------------------------------------------------------- | 5 | ----------------------------------------------------------------------------- |
6 | local base = _G | 6 | local base = _G |
7 | local table = require("table") | 7 | local table = require("table") |
8 | local string = require("string") | ||
8 | local socket = require("socket") | 9 | local socket = require("socket") |
9 | local coroutine = require("coroutine") | 10 | local coroutine = require("coroutine") |
10 | module("dispatch") | 11 | module("dispatch") |
@@ -43,26 +44,32 @@ end | |||
43 | ----------------------------------------------------------------------------- | 44 | ----------------------------------------------------------------------------- |
44 | -- Mega hack. Don't try to do this at home. | 45 | -- Mega hack. Don't try to do this at home. |
45 | ----------------------------------------------------------------------------- | 46 | ----------------------------------------------------------------------------- |
46 | -- we can't yield across calls to protect, so we rewrite it with coxpcall | 47 | -- we can't yield across calls to protect on Lua 5.1, so we rewrite it with |
48 | -- coroutines | ||
47 | -- make sure you don't require any module that uses socket.protect before | 49 | -- make sure you don't require any module that uses socket.protect before |
48 | -- loading our hack | 50 | -- loading our hack |
49 | function socket.protect(f) | 51 | if string.sub(base._VERSION, -3) == "5.1" then |
50 | return function(...) | 52 | local function _protect(co, status, ...) |
51 | local co = coroutine.create(f) | 53 | if not status then |
52 | while true do | 54 | local msg = ... |
53 | local results = {coroutine.resume(co, ...)} | 55 | if base.type(msg) == 'table' then |
54 | local status = table.remove(results, 1) | 56 | return nil, msg[1] |
55 | if not status then | ||
56 | if base.type(results[1]) == 'table' then | ||
57 | return nil, results[1][1] | ||
58 | else base.error(results[1]) end | ||
59 | end | ||
60 | if coroutine.status(co) == "suspended" then | ||
61 | arg = {coroutine.yield(base.unpack(results))} | ||
62 | else | 57 | else |
63 | return base.unpack(results) | 58 | base.error(msg, 0) |
64 | end | 59 | end |
65 | end | 60 | end |
61 | if coroutine.status(co) == "suspended" then | ||
62 | return _protect(co, coroutine.resume(co, coroutine.yield(...))) | ||
63 | else | ||
64 | return ... | ||
65 | end | ||
66 | end | ||
67 | |||
68 | function socket.protect(f) | ||
69 | return function(...) | ||
70 | local co = coroutine.create(f) | ||
71 | return _protect(co, coroutine.resume(co, ...)) | ||
72 | end | ||
66 | end | 73 | end |
67 | end | 74 | end |
68 | 75 | ||