Python1320

coroutine useless fun

Jul 29th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. local meta={}
  2. local co=setmetatable({},meta)
  3. _G.co=co
  4.  
  5. local costack=setmetatable({},{__index={
  6.     push=function(tbl,val)
  7.         table.insert(tbl,val)
  8.     end,
  9.     pop=function(tbl)
  10.         return table.remove(tbl)
  11.     end,
  12.     current=function(tbl)
  13.         return tbl[#tbl]
  14.     end,
  15.    
  16. }})
  17.  
  18.  
  19.  
  20. co._re=function(thread,...)
  21.     local status = coroutine.status(thread)
  22.     if status=="running" then
  23.         Msg"[CO] Running??? "print(thread)
  24.        
  25.     elseif status=="dead" then
  26.         Msg"[CO] Dead "print(thread)
  27.    
  28.     elseif status=="suspended" then
  29.    
  30.     else
  31.         error"invalid status?"
  32.     end
  33.    
  34.     costack:push(thread)
  35.     local ok,t,val = coroutine.resume(thread,...)
  36.     costack:pop(thread)
  37.    
  38.     if not ok then
  39.         ErrorNoHalt("Coroutine: "..tostring(t)..'\n')
  40.     end
  41.    
  42.     if t=="sleep" then
  43.         Msg"[CO] Sleep "print(val)
  44.         timer.Simple(val,function()
  45.             co._re(thread,"wait")
  46.         end)
  47.     elseif t=="cb" then
  48.         Msg"[CO] wait for callback "print(val)
  49.         return
  50.     elseif t=="END" then
  51.         Msg"[CO] END "print("OK")
  52.     else
  53.         Msg"[CO] Unhandled "print(t,val)
  54.     end
  55. end
  56.  
  57. function meta:__call(func)
  58.     local thread = coroutine.create(function(...)
  59.         func(...)
  60.         return "END"
  61.     end)
  62.     co._re(thread)
  63. end
  64.  
  65. function co.wait(delay)
  66.     local ret = coroutine.yield("sleep",tonumber(delay) or 0)
  67.     Msg"[CO] End wait "print(ret)
  68. end
  69. co.sleep=co.wait
  70.  
  71. function co.newcb()
  72.     local thread = costack:current()
  73.    
  74.     if not thread then error"wot" end
  75.    
  76.     Msg"[CO] Created cb for thread "print(thread)
  77.     local CB CB = function(...)
  78.         Msg("[CO] Callback called for thread ",thread," with params ")print(...)
  79.         co._re(thread,"cbret",CB,...)
  80.     end
  81.     return CB
  82. end
  83.  
  84. function co.waitcb(cbobj)
  85.     local function wrap(cbret,CB,...)
  86.         Msg"[CO] WaitCB " print(cbret=="cbret" and "OKRET" or ("WOT"..tostring(cbret)),(not cbobj or CB==cbobj) and "right cb" or "wtf cb?!?!?")
  87.         Msg"[CO] WaitCB Params " print(...)
  88.         return ...
  89.     end
  90.     return wrap(coroutine.yield("cb",cbobj))
  91. end
  92.  
  93. -- TESTING --
  94.  
  95. co(function()
  96.  
  97.     print"hello"
  98.     co.wait(1)
  99.     print"world"
  100.     co.wait(1)
  101.     print"!"
  102.    
  103.     local cb = co.newcb()
  104.    
  105.     timer.Simple(1,function()
  106.         cb("callback","returning")
  107.     end)
  108.     local a,b,c,d=co.waitcb(cb)
  109.     print("HTTP",b,c,d)
  110.     co.wait(1)
  111.     print"BYE"
  112.    
  113. end)
Advertisement
Add Comment
Please, Sign In to add comment