集成 github.com/vadv/gopher-lua-libs, 并支持 dkjson
This commit is contained in:
20
vm.go
20
vm.go
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user