Skip to content

Commit

Permalink
refactor: add a vm file
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 8, 2024
1 parent 6617df1 commit f107888
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
File renamed without changes.
66 changes: 66 additions & 0 deletions internal/luai/vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package luai

import (
_ "embed"

"github.com/version-fox/vfox/internal/config"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/module"
lua "github.com/yuin/gopher-lua"
)

//go:embed fixtures/preload.lua
var preloadScript string

type LuaVM struct {
Instance *lua.LState
}

func NewLuaVM() *LuaVM {
instance := lua.NewState()

return &LuaVM{
Instance: instance,
}
}

type PrepareOptions struct {
Config *config.Config
}

func (vm *LuaVM) Prepare(options *PrepareOptions) error {
vm.Instance.DoString(preloadScript)
module.Preload(vm.Instance, options.Config)

return nil
}

func (vm *LuaVM) ReturnedValue() *lua.LTable {
table := vm.Instance.ToTable(-1) // returned value
vm.Instance.Pop(1) // remove received value
return table
}

func (vm *LuaVM) CallFunction(function lua.LValue, args ...lua.LValue) error {
logger.Debugf("CallFunction: %s", function.String())

if err := vm.Instance.CallByParam(lua.P{
Fn: function.(*lua.LFunction),
NRet: 1,
Protect: true,
}, args...); err != nil {
return err
}
return nil
}

func (vm *LuaVM) GetTableString(table *lua.LTable, key string) string {
if value := table.RawGetString(key); value.Type() != lua.LTNil {
return value.String()
}
return ""
}

func (vm *LuaVM) Close() {
vm.Instance.Close()
}
68 changes: 9 additions & 59 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ import (
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/luai"
"github.com/version-fox/vfox/internal/module"
"github.com/version-fox/vfox/internal/plugin"
lua "github.com/yuin/gopher-lua"
)

//go:embed fixtures/preload.lua
var preloadScript string

const (
LuaPluginObjKey = "PLUGIN"
OsType = "OS_TYPE"
ArchType = "ARCH_TYPE"
)

type LuaPlugin struct {
vm *LuaVM
vm *luai.LuaVM
pluginObj *lua.LTable
// plugin source path
Filepath string
Expand Down Expand Up @@ -377,9 +373,15 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
}

func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
vm := NewLuaVM()
vm := luai.NewLuaVM()

vm.Prepare(&luai.PrepareOptions{
Config: manager.Config,
})

vm.Prepare(manager)
// set OS_TYPE and ARCH_TYPE
vm.Instance.SetGlobal(OsType, lua.LString(manager.osType))
vm.Instance.SetGlobal(ArchType, lua.LString(manager.archType))

if err := vm.Instance.DoString(content); err != nil {
return nil, err
Expand Down Expand Up @@ -435,55 +437,3 @@ func isValidName(name string) bool {
re := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`)
return re.MatchString(name)
}

type LuaVM struct {
Instance *lua.LState
}

func NewLuaVM() *LuaVM {
instance := lua.NewState()

return &LuaVM{
Instance: instance,
}
}

func (vm *LuaVM) Prepare(manager *Manager) error {
vm.Instance.DoString(preloadScript)
module.Preload(vm.Instance, manager.Config)

// set OS_TYPE and ARCH_TYPE
vm.Instance.SetGlobal(OsType, lua.LString(manager.osType))
vm.Instance.SetGlobal(ArchType, lua.LString(manager.archType))
return nil
}

func (vm *LuaVM) ReturnedValue() *lua.LTable {
table := vm.Instance.ToTable(-1) // returned value
vm.Instance.Pop(1) // remove received value
return table
}

func (vm *LuaVM) CallFunction(function lua.LValue, args ...lua.LValue) error {
logger.Debugf("CallFunction: %s", function.String())

if err := vm.Instance.CallByParam(lua.P{
Fn: function.(*lua.LFunction),
NRet: 1,
Protect: true,
}, args...); err != nil {
return err
}
return nil
}

func (vm *LuaVM) GetTableString(table *lua.LTable, key string) string {
if value := table.RawGetString(key); value.Type() != lua.LTNil {
return value.String()
}
return ""
}

func (vm *LuaVM) Close() {
vm.Instance.Close()
}

0 comments on commit f107888

Please sign in to comment.