@@ -38,8 +38,9 @@ type luaPlugin struct {
3838 cancelFunc context.CancelFunc
3939 running bool
4040 error error
41- body string
42- filename string
41+ body * string
42+ filename * string
43+ jobPayload * string
4344}
4445
4546func (p * luaPlugin ) getError () error {
@@ -68,7 +69,7 @@ func (p *luaPlugin) setRunning(val bool) {
6869 p .running = val
6970}
7071
71- // NewPluginState return lua state for plugin
72+ // NewPluginState return lua state
7273func NewPluginState () * lua.LState {
7374 state := lua .NewState ()
7475 // preload all
@@ -100,22 +101,27 @@ func NewPluginState() *lua.LState {
100101
101102func (p * luaPlugin ) start () {
102103 p .Lock ()
103-
104104 state := NewPluginState ()
105105 p .state = state
106106 p .error = nil
107107 p .running = true
108+ isBody := (p .filename == nil )
109+ if ! (p .jobPayload == nil ) {
110+ payload := * p .jobPayload
111+ state .SetGlobal (`payload` , lua .LString (payload ))
112+ }
108113 ctx , cancelFunc := context .WithCancel (context .Background ())
109- p .state .SetContext (ctx )
110114 p .cancelFunc = cancelFunc
111- isBody := ( p . filename == "" )
115+ p . state . SetContext ( ctx )
112116 p .Unlock ()
113117
114118 // blocking
115119 if isBody {
116- p .setError (p .state .DoString (p .body ))
120+ body := * p .body
121+ p .setError (p .state .DoString (body ))
117122 } else {
118- p .setError (p .state .DoFile (p .filename ))
123+ filename := * p .filename
124+ p .setError (p .state .DoFile (filename ))
119125 }
120126 p .setRunning (false )
121127}
@@ -132,7 +138,7 @@ func checkPlugin(L *lua.LState, n int) *luaPlugin {
132138// DoString lua plugin.do_string(body) returns plugin_ud
133139func DoString (L * lua.LState ) int {
134140 body := L .CheckString (1 )
135- p := & luaPlugin {body : body }
141+ p := & luaPlugin {body : & body }
136142 ud := L .NewUserData ()
137143 ud .Value = p
138144 L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
@@ -143,7 +149,31 @@ func DoString(L *lua.LState) int {
143149// DoFile lua plugin.do_file(filename) returns plugin_ud
144150func DoFile (L * lua.LState ) int {
145151 filename := L .CheckString (1 )
146- p := & luaPlugin {filename : filename }
152+ p := & luaPlugin {filename : & filename }
153+ ud := L .NewUserData ()
154+ ud .Value = p
155+ L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
156+ L .Push (ud )
157+ return 1
158+ }
159+
160+ // DoFileWithPayload lua plugin.async() returns (plugin_ud, err)
161+ func DoFileWithPayload (L * lua.LState ) int {
162+ filename := L .CheckString (1 )
163+ payload := L .CheckString (2 )
164+ p := & luaPlugin {filename : & filename , jobPayload : & payload }
165+ ud := L .NewUserData ()
166+ ud .Value = p
167+ L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
168+ L .Push (ud )
169+ return 1
170+ }
171+
172+ // DoStringWithPayload lua plugin.async() returns (plugin_ud, err)
173+ func DoStringWithPayload (L * lua.LState ) int {
174+ body := L .CheckString (1 )
175+ payload := L .CheckString (2 )
176+ p := & luaPlugin {body : & body , jobPayload : & payload }
147177 ud := L .NewUserData ()
148178 ud .Value = p
149179 L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
0 commit comments