增加执行脚本 + 获取脚本返回结果的方法
This commit is contained in:
53
vm.go
Normal file
53
vm.go
Normal file
@ -0,0 +1,53 @@
|
||||
// Package lua ...
|
||||
//
|
||||
// Description : lua ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-11-14 17:46
|
||||
package lua
|
||||
|
||||
import (
|
||||
luaCompile "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func NewVm() *VM {
|
||||
return &VM{luaVm: luaCompile.NewState()}
|
||||
}
|
||||
|
||||
type VM struct {
|
||||
luaVm *luaCompile.LState
|
||||
}
|
||||
|
||||
// Run 运行脚本
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:08 2024/11/14
|
||||
func (v *VM) Run(script string) error {
|
||||
if err := v.luaVm.DoString(script); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetResultAndRemove 获取最后一个返回值, 并从结果中移除
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:20 2024/11/14
|
||||
func (v *VM) GetResultAndRemove() luaCompile.LValue {
|
||||
val := v.luaVm.Get(-1)
|
||||
v.luaVm.Pop(1)
|
||||
return val
|
||||
}
|
||||
|
||||
// GetResultByIndex 根据索引获取返回值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:21 2024/11/14
|
||||
func (v *VM) GetResultByIndex(index int) luaCompile.LValue {
|
||||
val := v.luaVm.Get(index)
|
||||
return val
|
||||
}
|
||||
Reference in New Issue
Block a user