
* Start on lua scripting * Implement evalsha, script load, script exists, and script flush * Type conversions from lua to resp/json. Refactor to make luastate and luascripts persistent in the controller. * Change controller.command and all underlying commands to return resp.Value. Serialize only during the ouput. * First stab at tile38 call from lua * Change tile38 into tile38.call in Lua * Property return errors from scripts * Minor refactoring. No locking on script run * Cleanup/refactoring * Create a pool of 5 lua states, allow for more as needed. Refactor. * Use safe map for scripts. Add a limit for max number of lua states. Refactor. * Refactor * Refactor script commands into atomic, read-only, and non-atomic classes. Proper locking for all three classes. Add tests for scripts * More tests for scripts * Properly escape newlines in lua-produced errors * Better test for readonly failure * Correctly convert ok/err messages between lua and resp. Add pcall, sha1hex, error_reply, status_reply functions to tile38 namespace in lua. * Add pcall test. Change writeErr to work with string argument * Make sure eval/evalsha never attempt to write AOF * Add eval-set and eval-get to benchmarks * Fix eval benchmark tests, add more * Improve benchmarks * Optimizations and refactoring. * Add lua memtest * Typo * Add dependency * golint fixes * gofmt fixes * Add scripting commands to the core/commands.json * Use ARGV for args inside lua
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package lua
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
/* load lib {{{ */
|
|
|
|
var loLoaders = []LGFunction{loLoaderPreload, loLoaderLua}
|
|
|
|
func loGetPath(env string, defpath string) string {
|
|
path := os.Getenv(env)
|
|
if len(path) == 0 {
|
|
path = defpath
|
|
}
|
|
path = strings.Replace(path, ";;", ";"+defpath+";", -1)
|
|
if os.PathSeparator != '/' {
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
path = strings.Replace(path, "!", dir, -1)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func loFindFile(L *LState, name, pname string) (string, string) {
|
|
name = strings.Replace(name, ".", string(os.PathSeparator), -1)
|
|
lv := L.GetField(L.GetField(L.Get(EnvironIndex), "package"), pname)
|
|
path, ok := lv.(LString)
|
|
if !ok {
|
|
L.RaiseError("package.%s must be a string", pname)
|
|
}
|
|
messages := []string{}
|
|
for _, pattern := range strings.Split(string(path), ";") {
|
|
luapath := strings.Replace(pattern, "?", name, -1)
|
|
if _, err := os.Stat(luapath); err == nil {
|
|
return luapath, ""
|
|
} else {
|
|
messages = append(messages, err.Error())
|
|
}
|
|
}
|
|
return "", strings.Join(messages, "\n\t")
|
|
}
|
|
|
|
func OpenPackage(L *LState) int {
|
|
packagemod := L.RegisterModule(LoadLibName, loFuncs)
|
|
|
|
L.SetField(packagemod, "preload", L.NewTable())
|
|
|
|
loaders := L.CreateTable(len(loLoaders), 0)
|
|
for i, loader := range loLoaders {
|
|
L.RawSetInt(loaders, i+1, L.NewFunction(loader))
|
|
}
|
|
L.SetField(packagemod, "loaders", loaders)
|
|
L.SetField(L.Get(RegistryIndex), "_LOADERS", loaders)
|
|
|
|
loaded := L.NewTable()
|
|
L.SetField(packagemod, "loaded", loaded)
|
|
L.SetField(L.Get(RegistryIndex), "_LOADED", loaded)
|
|
|
|
L.SetField(packagemod, "path", LString(loGetPath(LuaPath, LuaPathDefault)))
|
|
L.SetField(packagemod, "cpath", LString(""))
|
|
|
|
L.Push(packagemod)
|
|
return 1
|
|
}
|
|
|
|
var loFuncs = map[string]LGFunction{
|
|
"loadlib": loLoadLib,
|
|
"seeall": loSeeAll,
|
|
}
|
|
|
|
func loLoaderPreload(L *LState) int {
|
|
name := L.CheckString(1)
|
|
preload := L.GetField(L.GetField(L.Get(EnvironIndex), "package"), "preload")
|
|
if _, ok := preload.(*LTable); !ok {
|
|
L.RaiseError("package.preload must be a table")
|
|
}
|
|
lv := L.GetField(preload, name)
|
|
if lv == LNil {
|
|
L.Push(LString(fmt.Sprintf("no field package.preload['%s']", name)))
|
|
return 1
|
|
}
|
|
L.Push(lv)
|
|
return 1
|
|
}
|
|
|
|
func loLoaderLua(L *LState) int {
|
|
name := L.CheckString(1)
|
|
path, msg := loFindFile(L, name, "path")
|
|
if len(path) == 0 {
|
|
L.Push(LString(msg))
|
|
return 1
|
|
}
|
|
fn, err1 := L.LoadFile(path)
|
|
if err1 != nil {
|
|
L.RaiseError(err1.Error())
|
|
}
|
|
L.Push(fn)
|
|
return 1
|
|
}
|
|
|
|
func loLoadLib(L *LState) int {
|
|
L.RaiseError("loadlib is not supported")
|
|
return 0
|
|
}
|
|
|
|
func loSeeAll(L *LState) int {
|
|
mod := L.CheckTable(1)
|
|
mt := L.GetMetatable(mod)
|
|
if mt == LNil {
|
|
mt = L.CreateTable(0, 1)
|
|
L.SetMetatable(mod, mt)
|
|
}
|
|
L.SetField(mt, "__index", L.Get(GlobalsIndex))
|
|
return 0
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
//
|