From 316e120490a67f015e89e6b88268a64de46f84ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 14 Nov 2024 18:22:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=A7=E8=A1=8C=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=20+=20=E8=8E=B7=E5=8F=96=E8=84=9A=E6=9C=AC=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=BB=93=E6=9E=9C=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 24 ++++++++++++++++++++++++ go.mod | 2 +- vm.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ vm_test.go | 17 +++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 vm.go create mode 100644 vm_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8e5d8f --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Created by .ignore support plugin (hsz.mobi) +### Go template +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +.idea +.vscode +.fleet +release +logs +.scannerwork +.env \ No newline at end of file diff --git a/go.mod b/go.mod index 204c149..3c39c5d 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module git.zhangdeman.cn/gateway/lua go 1.23.3 -require github.com/yuin/gopher-lua v1.1.1 // indirect +require github.com/yuin/gopher-lua v1.1.1 diff --git a/vm.go b/vm.go new file mode 100644 index 0000000..a5400de --- /dev/null +++ b/vm.go @@ -0,0 +1,53 @@ +// Package lua ... +// +// Description : lua ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-14 17:46 +package lua + +import ( + luaCompile "github.com/yuin/gopher-lua" +) + +func NewVm() *VM { + return &VM{luaVm: luaCompile.NewState()} +} + +type VM struct { + luaVm *luaCompile.LState +} + +// Run 运行脚本 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:08 2024/11/14 +func (v *VM) Run(script string) error { + if err := v.luaVm.DoString(script); err != nil { + return err + } + return nil +} + +// GetResultAndRemove 获取最后一个返回值, 并从结果中移除 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:20 2024/11/14 +func (v *VM) GetResultAndRemove() luaCompile.LValue { + val := v.luaVm.Get(-1) + v.luaVm.Pop(1) + return val +} + +// GetResultByIndex 根据索引获取返回值 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:21 2024/11/14 +func (v *VM) GetResultByIndex(index int) luaCompile.LValue { + val := v.luaVm.Get(index) + return val +} diff --git a/vm_test.go b/vm_test.go new file mode 100644 index 0000000..b34216d --- /dev/null +++ b/vm_test.go @@ -0,0 +1,17 @@ +// Package lua ... +// +// Description : lua ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-14 18:08 +package lua + +import ( + "testing" +) + +func TestNewVm(t *testing.T) { + v := NewVm() + v.Run(`print("hello world"); return "hahaha", "heiheihei"`) +}