mirror of
https://github.com/hbahadorzadeh/stunning.git
synced 2025-12-24 13:38:08 +08:00
48 lines
773 B
Go
48 lines
773 B
Go
package common
|
|
|
|
import (
|
|
"github.com/yuin/gopher-lua"
|
|
"sync"
|
|
)
|
|
|
|
type lStatePool struct {
|
|
m sync.Mutex
|
|
saved []*lua.LState
|
|
}
|
|
|
|
func (pl *lStatePool) Get() *lua.LState {
|
|
pl.m.Lock()
|
|
defer pl.m.Unlock()
|
|
n := len(pl.saved)
|
|
if n == 0 {
|
|
return pl.New()
|
|
}
|
|
x := pl.saved[n-1]
|
|
pl.saved = pl.saved[0 : n-1]
|
|
return x
|
|
}
|
|
|
|
func (pl *lStatePool) New() *lua.LState {
|
|
L := lua.NewState()
|
|
// setting the L up here.
|
|
// load scripts, set global variables, share channels, etc...
|
|
return L
|
|
}
|
|
|
|
func (pl *lStatePool) Put(L *lua.LState) {
|
|
pl.m.Lock()
|
|
defer pl.m.Unlock()
|
|
pl.saved = append(pl.saved, L)
|
|
}
|
|
|
|
func (pl *lStatePool) Shutdown() {
|
|
for _, L := range pl.saved {
|
|
L.Close()
|
|
}
|
|
}
|
|
|
|
// Global LState pool
|
|
var luaPool = &lStatePool{
|
|
saved: make([]*lua.LState, 0, 4),
|
|
}
|