summaryrefslogtreecommitdiff
path: root/src/lua/llthreads2/ex.lua
blob: 26e1a46238ff486b0bc97deee43f7a66ab694054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
--
-- wraps the low-level threads object.
--

--
-- Note! Define this function prior all `local` definitions
--       to prevent use upvalue by accident
--
local bootstrap_code = require"string".dump(function(lua_init, prelude, code, ...)
  local loadstring = loadstring or load
  local unpack     = table.unpack or unpack

  local function load_src(str)
    local f, n
    if str:sub(1,1) == '@' then
      n = str:sub(2)
      f = assert(loadfile(n))
    else
      n = '=(loadstring)'
      f = assert(loadstring(str))
    end
    return f, n
  end

  local function pack_n(...)
    return { n = select("#", ...), ... }
  end

  local function unpack_n(t)
    return unpack(t, 1, t.n)
  end

  if lua_init and #lua_init > 0 then
    local init = load_src(lua_init)
    init()
  end

  local args

  if prelude and #prelude > 0 then
    prelude = load_src(prelude)
    args = pack_n(prelude(...))
  else
    args = pack_n(...)
  end

  local func
  func, args[0] = load_src(code)

  _G.arg = args
     arg = args

  return func(unpack_n(args))
end)

local ok, llthreads = pcall(require, "llthreads2")
if not ok then llthreads = require"llthreads" end

local os        = require"os"
local string    = require"string"
local table     = require"table"

local setmetatable, tonumber, assert = setmetatable, tonumber, assert

-------------------------------------------------------------------------------
local LUA_INIT = "LUA_INIT" do

local lua_version_t
local function lua_version()
  if not lua_version_t then 
    local version = assert(_G._VERSION)
    local maj,min = version:match("^Lua (%d+)%.(%d+)$")
    if maj then                         lua_version_t = {tonumber(maj),tonumber(min)}
    elseif not math.mod then            lua_version_t = {5,2}
    elseif table.pack and not pack then lua_version_t = {5,2}
    else                                lua_version_t = {5,2} end
  end
  return lua_version_t[1], lua_version_t[2]
end

local LUA_MAJOR, LUA_MINOR = lua_version()
local IS_LUA_51 = (LUA_MAJOR == 5) and (LUA_MINOR == 1)

local LUA_INIT_VER
if not IS_LUA_51 then
  LUA_INIT_VER = LUA_INIT .. "_" .. LUA_MAJOR .. "_" .. LUA_MINOR
end

LUA_INIT = LUA_INIT_VER and os.getenv( LUA_INIT_VER ) or os.getenv( LUA_INIT ) or ""

end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
local thread_mt = {} do
thread_mt.__index = thread_mt

function thread_mt:start(...)
  local ok, err = self.thread:start(...)
  if not ok then return nil, err end
  return self
end

function thread_mt:join(...)
  return self.thread:join(...)
end

function thread_mt:alive()
  return self.thread:alive()
end

end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
local threads = {} do

local function new_thread(prelude, lua_init, code, ...)
  if type(lua_init) == "function" then
    lua_init = string.dump(lua_init)
  end

  if type(prelude) == "function" then
    prelude = string.dump(prelude)
  end

  if type(code) == "function" then
    code = string.dump(code)
  end

  local thread = llthreads.new(bootstrap_code, lua_init, prelude, code, ...)
  return setmetatable({
    thread = thread,
  }, thread_mt)
end

threads.new = function (code, ...)
  assert(code)

  if type(code) == "table" then
    local source = assert(code.source or code[1])
    local init = (code.lua_init == nil) and LUA_INIT or code.lua_init
    return new_thread(code.prelude, init, source, ...)
  end

  return new_thread(nil, LUA_INIT, code, ...)
end

end
-------------------------------------------------------------------------------

return threads