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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
package plugin
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
lua "github.com/yuin/gopher-lua"
"github.com/shiv/internal/logger"
"github.com/shiv/internal/store"
)
const callTimeout = 500 * time.Millisecond
// Plugin is a single loaded Lua script with its own isolated VM.
// mu serialises all VM access — gopher-lua LState is not goroutine-safe.
type Plugin struct {
name string
path string
L *lua.LState
mu sync.Mutex
// enabled controls whether this plugin's hooks are called.
// Uses atomic.Bool so the engine can check it without acquiring mu.
enabled atomic.Bool
// logBuf accumulates log() calls made during a Lua hook execution.
// Written only while mu is held (log() can only be called from within
// a Lua hook, which always runs under mu). Drained by the engine after
// each hook call via drainLogs, which also acquires mu.
logBuf []string
}
// load reads the Lua file, registers the full API, executes the script,
// then calls on_load() if defined.
func load(path string, st *store.Store) (*Plugin, error) {
name := filepath.Base(path)
L := lua.NewState(lua.Options{
SkipOpenLibs: false,
})
p := &Plugin{name: name, path: path, L: L}
p.enabled.Store(true)
// Register API before executing the script so on_load can use it.
// The logFn closure appends to p.logBuf. It is safe without acquiring
// mu because log() is only ever invoked from within a Lua hook, which
// always runs under mu via callNoReturn or callWithBuilder.
registerAPI(L, st, func(msg string) {
p.logBuf = append(p.logBuf, msg)
})
if err := L.DoFile(path); err != nil {
L.Close()
return nil, fmt.Errorf("plugin %s: load: %w", name, err)
}
// Call on_load() if defined — errors are logged but not fatal.
if L.GetGlobal("on_load") != lua.LNil {
if err := p.callNoReturn("on_load", lua.LNil); err != nil {
logger.Error("plugin %s: on_load: %v", name, err)
}
}
return p, nil
}
// callNoReturn invokes a Lua function that returns nothing.
// Holds mu for the duration. Enforces callTimeout. Recovers panics.
func (p *Plugin) callNoReturn(fn string, arg lua.LValue) (retErr error) {
p.mu.Lock()
defer p.mu.Unlock()
defer func() {
if r := recover(); r != nil {
retErr = fmt.Errorf("panic: %v", r)
}
}()
f := p.L.GetGlobal(fn)
if f == lua.LNil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), callTimeout)
defer cancel()
p.L.SetContext(ctx)
defer p.L.SetContext(context.Background())
params := lua.P{Fn: f, NRet: 0, Protect: true}
if arg != lua.LNil {
return p.L.CallByParam(params, arg)
}
return p.L.CallByParam(params)
}
// callWithBuilder builds a Lua table and calls fn with it in a single locked
// section, returning the result table. builder receives the VM and must
// construct and return the argument table — it runs under mu. The Lua call
// also runs under mu, so no VM access escapes the lock at any point.
// Returns the returned table, or nil if the function returned nothing.
func (p *Plugin) callWithBuilder(fn string, builder func(L *lua.LState) *lua.LTable) (ret *lua.LTable, retErr error) {
p.mu.Lock()
defer p.mu.Unlock()
defer func() {
if r := recover(); r != nil {
retErr = fmt.Errorf("panic: %v", r)
}
}()
f := p.L.GetGlobal(fn)
if f == lua.LNil {
return nil, nil
}
arg := builder(p.L)
ctx, cancel := context.WithTimeout(context.Background(), callTimeout)
defer cancel()
p.L.SetContext(ctx)
defer p.L.SetContext(context.Background())
if err := p.L.CallByParam(lua.P{Fn: f, NRet: 1, Protect: true}, arg); err != nil {
return nil, err
}
top := p.L.Get(-1)
p.L.Pop(1)
if tbl, ok := top.(*lua.LTable); ok {
return tbl, nil
}
return nil, nil
}
// drainLogs returns all buffered log lines and clears the buffer.
// Called by the engine after each hook invocation so log lines can be
// emitted onto the bus. Acquires mu independently of the hook call.
func (p *Plugin) drainLogs() []string {
p.mu.Lock()
defer p.mu.Unlock()
if len(p.logBuf) == 0 {
return nil
}
lines := p.logBuf
p.logBuf = nil
return lines
}
// close shuts down the Lua VM.
func (p *Plugin) close() {
p.mu.Lock()
defer p.mu.Unlock()
p.L.Close()
}
// has returns true if the plugin defines the named global function.
func (p *Plugin) has(fn string) bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.L.GetGlobal(fn) != lua.LNil
}
// loadDir scans dir for *.lua files and loads each as a Plugin.
// Files that fail to load are logged and skipped.
func loadDir(dir string, st *store.Store) ([]*Plugin, error) {
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("plugin: read dir %s: %w", dir, err)
}
var plugins []*Plugin
for _, e := range entries {
if e.IsDir() || filepath.Ext(e.Name()) != ".lua" {
continue
}
path := filepath.Join(dir, e.Name())
p, err := load(path, st)
if err != nil {
logger.Error("plugin: skip %s: %v", e.Name(), err)
continue
}
logger.Info("plugin: loaded %s", e.Name())
plugins = append(plugins, p)
}
return plugins, nil
}
|