执行lua脚本增加传参能力
This commit is contained in:
parent
6df50488ba
commit
da61086725
14
define.go
Normal file
14
define.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// Package lua ...
|
||||||
|
//
|
||||||
|
// Description : lua ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-05-12 21:13
|
||||||
|
package lua
|
||||||
|
|
||||||
|
// ScriptParam 脚本参数
|
||||||
|
type ScriptParam struct {
|
||||||
|
Name string `json:"name"` // 参数名称
|
||||||
|
Value string `json:"value"` // 参数值
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -40,6 +40,7 @@ require (
|
|||||||
google.golang.org/protobuf v1.36.6 // indirect
|
google.golang.org/protobuf v1.36.6 // indirect
|
||||||
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc // indirect
|
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
layeh.com/gopher-luar v1.0.11 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/alessio/shellescape => al.essio.dev/pkg/shellescape v1.4.2
|
replace github.com/alessio/shellescape => al.essio.dev/pkg/shellescape v1.4.2
|
||||||
|
3
go.sum
3
go.sum
@ -172,6 +172,7 @@ github.com/vadv/gopher-lua-libs v0.5.0 h1:m0hhWia1A1U3PIRmtdHWBj88ogzuIjm6HUBmtU
|
|||||||
github.com/vadv/gopher-lua-libs v0.5.0/go.mod h1:mlSOxmrjug7DwisiH7xBFnBellHobPbvAIhVeI/4SYY=
|
github.com/vadv/gopher-lua-libs v0.5.0/go.mod h1:mlSOxmrjug7DwisiH7xBFnBellHobPbvAIhVeI/4SYY=
|
||||||
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 h1:noHsffKZsNfU38DwcXWEPldrTjIZ8FPNKx8mYMGnqjs=
|
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7 h1:noHsffKZsNfU38DwcXWEPldrTjIZ8FPNKx8mYMGnqjs=
|
||||||
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7/go.mod h1:bbMEM6aU1WDF1ErA5YJ0p91652pGv140gGw4Ww3RGp8=
|
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7/go.mod h1:bbMEM6aU1WDF1ErA5YJ0p91652pGv140gGw4Ww3RGp8=
|
||||||
|
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
|
||||||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
||||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
@ -243,3 +244,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
|||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
layeh.com/gopher-luar v1.0.11 h1:8zJudpKI6HWkoh9eyyNFaTM79PY6CAPcIr6X/KTiliw=
|
||||||
|
layeh.com/gopher-luar v1.0.11/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=
|
||||||
|
16
vm.go
16
vm.go
@ -13,6 +13,7 @@ import (
|
|||||||
luaCompile "github.com/yuin/gopher-lua"
|
luaCompile "github.com/yuin/gopher-lua"
|
||||||
"github.com/yuin/gopher-lua/ast"
|
"github.com/yuin/gopher-lua/ast"
|
||||||
"github.com/yuin/gopher-lua/parse"
|
"github.com/yuin/gopher-lua/parse"
|
||||||
|
luar "layeh.com/gopher-luar"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -89,7 +90,7 @@ func (v *VM) RemoveScript(scriptID string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetVm 获取一个VM实例
|
// GetVm 获取一个VM实例
|
||||||
func (v *VM) GetVm() *luaCompile.LState {
|
func (v *VM) GetVm(scriptParamList ...ScriptParam) *luaCompile.LState {
|
||||||
v.l.Lock()
|
v.l.Lock()
|
||||||
defer v.l.Unlock()
|
defer v.l.Unlock()
|
||||||
if len(v.pool) == 0 {
|
if len(v.pool) == 0 {
|
||||||
@ -98,6 +99,11 @@ func (v *VM) GetVm() *luaCompile.LState {
|
|||||||
l := v.pool[len(v.pool)-1]
|
l := v.pool[len(v.pool)-1]
|
||||||
// 基于libs预导入lua的常用模块
|
// 基于libs预导入lua的常用模块
|
||||||
libs.Preload(l)
|
libs.Preload(l)
|
||||||
|
// 设置脚本参数
|
||||||
|
for _, itemParam := range scriptParamList {
|
||||||
|
l.SetGlobal(itemParam.Name, luar.New(l, itemParam.Value))
|
||||||
|
|
||||||
|
}
|
||||||
v.pool = v.pool[:len(v.pool)-1]
|
v.pool = v.pool[:len(v.pool)-1]
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
@ -119,7 +125,7 @@ func (v *VM) Shutdown() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunPreCompileScript 执行预编译过的脚本
|
// RunPreCompileScript 执行预编译过的脚本
|
||||||
func (v *VM) RunPreCompileScript(scriptID string) (*luaCompile.LState, error) {
|
func (v *VM) RunPreCompileScript(scriptID string, scriptParamList ...ScriptParam) (*luaCompile.LState, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
proto *luaCompile.FunctionProto
|
proto *luaCompile.FunctionProto
|
||||||
@ -131,7 +137,7 @@ func (v *VM) RunPreCompileScript(scriptID string) (*luaCompile.LState, error) {
|
|||||||
return nil, fmt.Errorf("preComplier script %s not found", scriptID)
|
return nil, fmt.Errorf("preComplier script %s not found", scriptID)
|
||||||
}
|
}
|
||||||
v.scriptLock.Unlock()
|
v.scriptLock.Unlock()
|
||||||
l := v.GetVm()
|
l := v.GetVm(scriptParamList...)
|
||||||
lFunc := l.NewFunctionFromProto(proto)
|
lFunc := l.NewFunctionFromProto(proto)
|
||||||
l.Push(lFunc)
|
l.Push(lFunc)
|
||||||
if err = l.PCall(0, luaCompile.MultRet, nil); nil != err {
|
if err = l.PCall(0, luaCompile.MultRet, nil); nil != err {
|
||||||
@ -146,8 +152,8 @@ func (v *VM) RunPreCompileScript(scriptID string) (*luaCompile.LState, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:08 2024/11/14
|
// Date : 18:08 2024/11/14
|
||||||
func (v *VM) Run(script string) (*luaCompile.LState, error) {
|
func (v *VM) Run(script string, scriptParamList ...ScriptParam) (*luaCompile.LState, error) {
|
||||||
l := v.GetVm()
|
l := v.GetVm(scriptParamList...)
|
||||||
if err := l.DoString(script); err != nil {
|
if err := l.DoString(script); err != nil {
|
||||||
// 直接归还实例
|
// 直接归还实例
|
||||||
l.Close()
|
l.Close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user