升级异常定义及处理

This commit is contained in:
2025-05-13 14:45:06 +08:00
parent 90d22fa94f
commit 00b00a2503
4 changed files with 58 additions and 17 deletions

57
vm.go
View File

@ -8,7 +8,7 @@
package lua
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/exception"
libs "github.com/vadv/gopher-lua-libs"
luaCompile "github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/ast"
@ -37,7 +37,7 @@ func InitVM(poolSize int, libPath []string, preComplierScript map[string]string)
pathStr += ";" + libDir
}
pathStr += ";;"
os.Setenv("LUA_PATH", pathStr)
_ = os.Setenv("LUA_PATH", pathStr)
if poolSize <= 0 {
poolSize = 32
}
@ -47,6 +47,9 @@ func InitVM(poolSize int, libPath []string, preComplierScript map[string]string)
pool: make([]*luaCompile.LState, 0),
preComplierScript: make(map[string]*luaCompile.FunctionProto),
}
for i := 0; i < poolSize; i++ {
VMInstance.pool = append(VMInstance.pool, luaCompile.NewState())
}
for scriptID, scriptContent := range preComplierScript {
if err := VMInstance.AddScript(scriptID, scriptContent); nil != err {
return err
@ -90,18 +93,19 @@ func (v *VM) RemoveScript(scriptID string) {
}
// GetVm 获取一个VM实例
func (v *VM) GetVm(scriptParamList ...ScriptParam) *luaCompile.LState {
func (v *VM) GetVm(scriptParamList ...ScriptParam) (*luaCompile.LState, error) {
v.l.Lock()
defer v.l.Unlock()
if len(v.pool) == 0 {
// 基于libs预导入lua的常用模块
vm := luaCompile.NewState()
libs.Preload(vm)
// 设置脚本参数
for _, itemParam := range scriptParamList {
vm.SetGlobal(itemParam.Name, luar.New(vm, itemParam.Value))
}
return vm
return nil, exception.New(ErrLuaVmPoolIsEmpty, nil, "lua虚拟机实例池为空,请检查是否初始化了虚拟机以及虚拟机实例使用之后是否及时关闭")
/* // 基于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的常用模块
@ -111,11 +115,14 @@ func (v *VM) GetVm(scriptParamList ...ScriptParam) *luaCompile.LState {
l.SetGlobal(itemParam.Name, luar.New(l, itemParam.Value))
}
v.pool = v.pool[:len(v.pool)-1]
return l
return l, nil
}
// Close 关闭指定虚拟机
func (v *VM) Close(l *luaCompile.LState) {
if nil == l {
return
}
l.Close()
v.l.Lock()
defer v.l.Unlock()
@ -134,16 +141,24 @@ func (v *VM) Shutdown() {
func (v *VM) RunPreCompileScript(scriptID string, scriptParamList ...ScriptParam) (*luaCompile.LState, error) {
var (
err error
l *luaCompile.LState
proto *luaCompile.FunctionProto
exists bool
)
v.scriptLock.Lock()
if proto, exists = v.preComplierScript[scriptID]; !exists {
v.scriptLock.Unlock()
return nil, fmt.Errorf("preComplier script %s not found", scriptID)
return nil, exception.New(ErrLuaVmPreCompileScriptNotFound, map[string]any{
"script_id": scriptID,
}, "预编译脚本不存在")
}
v.scriptLock.Unlock()
l := v.GetVm(scriptParamList...)
if l, err = v.GetVm(scriptParamList...); nil != err {
return nil, exception.New(ErrLuaVmPreCompileScriptRunFail, map[string]any{
"script_id": scriptID,
"err_msg": err.Error(),
}, "预编译脚本执行失败")
}
lFunc := l.NewFunctionFromProto(proto)
l.Push(lFunc)
if err = l.PCall(0, luaCompile.MultRet, nil); nil != err {
@ -159,11 +174,19 @@ func (v *VM) RunPreCompileScript(scriptID string, scriptParamList ...ScriptParam
//
// Date : 18:08 2024/11/14
func (v *VM) Run(script string, scriptParamList ...ScriptParam) (*luaCompile.LState, error) {
l := v.GetVm(scriptParamList...)
if err := l.DoString(script); err != nil {
var (
err error
l *luaCompile.LState
)
if l, err = v.GetVm(scriptParamList...); nil != err {
return nil, err
}
if err = l.DoString(script); err != nil {
// 直接归还实例
l.Close()
return nil, err
return nil, exception.New(ErrLuaVmScriptRunFail, map[string]any{
"err_msg": err.Error(),
}, "脚本执行出现异常")
}
return l, nil
}