daily pastebin goal
23%
SHARE
TWEET

01_process.lua

a guest Aug 10th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local process = require("process")
  2.  
  3. --Initialize coroutine library--
  4. local _coroutine = coroutine -- real coroutine backend
  5.  
  6. _G.coroutine = setmetatable(
  7.   {
  8.     resume = function(co, ...)
  9.       local proc = process.info(co)
  10.       -- proc is nil if the process closed, natural resume will likely complain the coroutine is dead
  11.       -- but if proc is dead and an aborted coroutine is alive, it doesn't have any proc data like stack info
  12.       -- if the user really wants to resume it, let them
  13.       return (proc and proc.data.coroutine_handler.resume or _coroutine.resume)(co, ...)
  14.     end
  15.   },
  16.   {
  17.     __index = function(_, key)
  18.       return assert(process.info(_coroutine.running()), "thread has no proc").data.coroutine_handler[key]
  19.     end
  20.   }
  21. )
  22.  
  23. package.loaded.coroutine = _G.coroutine
  24.  
  25. local kernel_load = _G.load
  26. local intercept_load
  27. intercept_load = function(source, label, mode, env)
  28.   local prev_load = env and env.load or _G.load
  29.   local e = env and setmetatable({
  30.     load = function(_source, _label, _mode, _env)
  31.       return prev_load(_source, _label, _mode, _env or env)
  32.     end}, {
  33.       __index = env,
  34.       __pairs = function(...) return pairs(env, ...) end,
  35.       __newindex = function(_, key, value) env[key] = value end,
  36.   })
  37.   return kernel_load(source, label, mode, e or process.info().env)
  38. end
  39. _G.load = intercept_load
  40.  
  41. local kernel_create = _coroutine.create
  42. _coroutine.create = function(f,standAlone)
  43.   local co = kernel_create(f)
  44.   if not standAlone then
  45.     table.insert(process.findProcess().instances, co)
  46.   end
  47.   return co
  48. end
  49.  
  50. _coroutine.wrap = function(f)
  51.   local thread = coroutine.create(f)
  52.   return function(...)
  53.     return select(2, coroutine.resume(thread, ...))
  54.   end
  55. end
  56.  
  57. local init_thread = _coroutine.running()
  58. process.list[init_thread] = {
  59.   path = "/init.lua",
  60.   command = "init",
  61.   env = _ENV,
  62.   data =
  63.   {
  64.     vars={},
  65.     io={}, --init will populate this
  66.     coroutine_handler = _coroutine
  67.   },
  68.   instances = setmetatable({}, {__mode="v"})
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top