@@ -38,8 +38,9 @@ type luaPlugin struct {
38
38
cancelFunc context.CancelFunc
39
39
running bool
40
40
error error
41
- body string
42
- filename string
41
+ body * string
42
+ filename * string
43
+ jobPayload * string
43
44
}
44
45
45
46
func (p * luaPlugin ) getError () error {
@@ -68,7 +69,7 @@ func (p *luaPlugin) setRunning(val bool) {
68
69
p .running = val
69
70
}
70
71
71
- // NewPluginState return lua state for plugin
72
+ // NewPluginState return lua state
72
73
func NewPluginState () * lua.LState {
73
74
state := lua .NewState ()
74
75
// preload all
@@ -100,22 +101,27 @@ func NewPluginState() *lua.LState {
100
101
101
102
func (p * luaPlugin ) start () {
102
103
p .Lock ()
103
-
104
104
state := NewPluginState ()
105
105
p .state = state
106
106
p .error = nil
107
107
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
+ }
108
113
ctx , cancelFunc := context .WithCancel (context .Background ())
109
- p .state .SetContext (ctx )
110
114
p .cancelFunc = cancelFunc
111
- isBody := ( p . filename == "" )
115
+ p . state . SetContext ( ctx )
112
116
p .Unlock ()
113
117
114
118
// blocking
115
119
if isBody {
116
- p .setError (p .state .DoString (p .body ))
120
+ body := * p .body
121
+ p .setError (p .state .DoString (body ))
117
122
} else {
118
- p .setError (p .state .DoFile (p .filename ))
123
+ filename := * p .filename
124
+ p .setError (p .state .DoFile (filename ))
119
125
}
120
126
p .setRunning (false )
121
127
}
@@ -132,7 +138,7 @@ func checkPlugin(L *lua.LState, n int) *luaPlugin {
132
138
// DoString lua plugin.do_string(body) returns plugin_ud
133
139
func DoString (L * lua.LState ) int {
134
140
body := L .CheckString (1 )
135
- p := & luaPlugin {body : body }
141
+ p := & luaPlugin {body : & body }
136
142
ud := L .NewUserData ()
137
143
ud .Value = p
138
144
L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
@@ -143,7 +149,31 @@ func DoString(L *lua.LState) int {
143
149
// DoFile lua plugin.do_file(filename) returns plugin_ud
144
150
func DoFile (L * lua.LState ) int {
145
151
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 }
147
177
ud := L .NewUserData ()
148
178
ud .Value = p
149
179
L .SetMetatable (ud , L .GetTypeMetatable (`plugin_ud` ))
0 commit comments