集成 github.com/vadv/gopher-lua-libs, 并支持 dkjson

This commit is contained in:
2025-05-12 16:40:33 +08:00
parent a95acc7e36
commit 3253191437
14 changed files with 2811 additions and 4 deletions

20
vm.go
View File

@ -9,10 +9,12 @@ package lua
import (
"fmt"
_ "github.com/vadv/gopher-lua-libs"
libs "github.com/vadv/gopher-lua-libs"
luaCompile "github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/ast"
"github.com/yuin/gopher-lua/parse"
"os"
"path/filepath"
"strings"
"sync"
)
@ -22,7 +24,19 @@ var (
)
// InitVM 初始化lua虚拟机
func InitVM(poolSize int, preComplierScript map[string]string) error {
func InitVM(poolSize int, libPath []string, preComplierScript map[string]string) error {
if len(libPath) == 0 {
libPath = []string{"./lib"} // 默认使用内置的lib
}
pathStr := ""
for _, path := range libPath {
// 绝对路径
libDir, _ := filepath.Abs(path)
libDir = filepath.Join(libDir, "?.lua")
pathStr += ";" + libDir
}
pathStr += ";;"
os.Setenv("LUA_PATH", pathStr)
if poolSize <= 0 {
poolSize = 32
}
@ -82,6 +96,8 @@ func (v *VM) GetVm() *luaCompile.LState {
return luaCompile.NewState()
}
l := v.pool[len(v.pool)-1]
// 基于libs预导入lua的常用模块
libs.Preload(l)
v.pool = v.pool[:len(v.pool)-1]
return l
}