test-lua-speed/lua-speed.go

35 lines
592 B
Go
Raw Normal View History

2024-11-13 20:00:48 +00:00
package lua_speed
import (
_ "embed"
"github.com/yuin/gopher-lua"
"log"
)
//go:embed speed.lua
var speedLuaScript string
func FibonacciLuaInit() *lua.LState {
L := lua.NewState()
L.OpenLibs()
if err := L.DoString(speedLuaScript); err != nil {
log.Panic("Error running Lua plugin:", err)
}
return L
}
func FibonacciLuaRun(L *lua.LState, n uint64) uint64 {
F := L.GetGlobal("Fibonacci")
err := L.CallByParam(lua.P{
Fn: F,
NRet: 1,
Protect: true,
}, lua.LNumber(n))
if err != nil {
return 0
}
return uint64(L.ToNumber(L.GetTop()))
}