调试脚本传参 + 全局包预导入

This commit is contained in:
2025-05-12 21:56:00 +08:00
parent da61086725
commit 90d22fa94f
3 changed files with 29 additions and 4 deletions

10
vm.go
View File

@ -94,7 +94,14 @@ func (v *VM) GetVm(scriptParamList ...ScriptParam) *luaCompile.LState {
v.l.Lock()
defer v.l.Unlock()
if len(v.pool) == 0 {
return luaCompile.NewState()
// 基于libs预导入lua的常用模块
vm := luaCompile.NewState()
libs.Preload(vm)
// 设置脚本参数
for _, itemParam := range scriptParamList {
vm.SetGlobal(itemParam.Name, luar.New(vm, itemParam.Value))
}
return vm
}
l := v.pool[len(v.pool)-1]
// 基于libs预导入lua的常用模块
@ -102,7 +109,6 @@ func (v *VM) GetVm(scriptParamList ...ScriptParam) *luaCompile.LState {
// 设置脚本参数
for _, itemParam := range scriptParamList {
l.SetGlobal(itemParam.Name, luar.New(l, itemParam.Value))
}
v.pool = v.pool[:len(v.pool)-1]
return l