-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaScriptThread.lua
78 lines (58 loc) · 1.76 KB
/
LuaScriptThread.lua
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
require "BanateCore"
require "win_kernel32"
local kernel32 = ffi.load("kernel32")
local lua = ffi.load("lua51")
-- Definition of RunLuaThread
ffi.cdef[[
int RunLuaThread(void *s);
]]
--
-- This helper routine will take a pointer
-- to cdata, and return a string that contains
-- the memory address
function CreatePointerString(instance)
if ffi.abi("64bit") then
return string.format("0x%016x", tonumber(ffi.cast("int64_t", ffi.cast("void *", instance))))
elseif ffi.abi("32bit") then
return string.format("0x%08x", tonumber(ffi.cast("int32_t", ffi.cast("void *", instance))))
end
return nil
end
function PrependThreadParam(codechunk, threadparam)
if threadparam == nil or codechunk == nil then return codechunk end
local paramAsString = CreatePointerString(threadparam)
return string.format("local _ThreadParam = %s\n\n%s", paramAsString, codechunk)
end
class.LuaScriptThread()
function LuaScriptThread:_init(codechunk, param, createSuspended)
createSuspended = createSuspended or false
local flags = 0
if createSuspended then
flags = CREATE_SUSPENDED
end
param = param or nil
self.CodeChunk = codechunk
self.ThreadParam = param
self.Flags = flags
-- prepend the param to the code chunk if it was supplied
local threadprogram = PrependThreadParam(codechunk, param)
local threadId = ffi.new("DWORD[1]")
self.Handle = kernel32.CreateThread(nil,
0,
lua.RunLuaThread,
ffi.cast("void *", threadprogram),
flags,
threadId)
threadId = threadId[0]
self.ThreadId = threadId
end
function LuaScriptThread:Resume()
-- need the following thread access right
--THREAD_SUSPEND_RESUME
local result = kernel32.ResumeThread(self.Handle)
end
function LuaScriptThread:Suspend()
end
function LuaScriptThread:Yield()
local result = kernel32.SwitchToThread()
end