Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make plugin more easier to maintain #83

Merged
merged 33 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
09b7e1c
refactor: add lua vm struct
bytemain Mar 6, 2024
6f462ae
feat: support struct marshal
bytemain Mar 6, 2024
9250116
feat: add more ctx
bytemain Mar 6, 2024
5732fb5
fix: encoding tag map error
bytemain Mar 6, 2024
620e1c0
feat: add logger
bytemain Mar 6, 2024
bad1f1e
feat: add debug flag
bytemain Mar 6, 2024
a9f4f03
fix: fix decode in mixed struct
bytemain Mar 6, 2024
882bb9a
fix: unmarshal to interface is not work
bytemain Mar 6, 2024
0edeb1d
feat: ctx use marshal
bytemain Mar 6, 2024
f99b172
feat: unmarshal hook result
bytemain Mar 6, 2024
4dc97fe
ci: update test cases
bytemain Mar 6, 2024
d76a19c
test: streamline encoding unit test
bytemain Mar 6, 2024
f4b0631
Merge remote-tracking branch 'origin/main' into refactor/make-plugin-…
bytemain Mar 7, 2024
4f7b5d3
chore: remove print
bytemain Mar 7, 2024
1a98f94
test: add more testcases
bytemain Mar 7, 2024
95afe00
chore: revert hook name enum
bytemain Mar 7, 2024
a0feebe
chore: add license
bytemain Mar 7, 2024
92e7071
Merge remote-tracking branch 'origin/main' into refactor/make-plugin-…
bytemain Mar 7, 2024
82d0e17
chore: update log message
bytemain Mar 7, 2024
6617df1
refactor: add plugin module
bytemain Mar 8, 2024
f107888
refactor: add a vm file
bytemain Mar 8, 2024
cb64f1a
chore: update logger
bytemain Mar 8, 2024
2cae92c
chore: add license
bytemain Mar 8, 2024
29560d5
feat: support marshal nil
bytemain Mar 8, 2024
7061f28
fix: preinstall should work
bytemain Mar 8, 2024
4984ff0
fix: lua checksum
bytemain Mar 8, 2024
37a961c
chore: update code
bytemain Mar 8, 2024
f43ddb9
refactor: unmarshal plugin info
bytemain Mar 10, 2024
ad6f9e8
chore: some modifications
aooohan Mar 11, 2024
3729e40
mod: remove debug log in encoding func
aooohan Mar 11, 2024
275bcdb
mod: Optimize function calls
aooohan Mar 11, 2024
2d33ebf
bugfix
aooohan Mar 11, 2024
c44f873
mod
aooohan Mar 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions internal/luai/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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"
)
Expand Down Expand Up @@ -60,8 +59,6 @@ func (vm *LuaVM) ReturnedValue() *lua.LTable {
}

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,
Expand Down
24 changes: 15 additions & 9 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
return nil, err
}

if err := l.vm.CallFunction(l.pluginObj.RawGetString("Available"), l.pluginObj, ctxTable); err != nil {
if err = l.CallFunction("Available", l.pluginObj, ctxTable); err != nil {
return nil, err
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
return nil, err
}

if err := l.vm.CallFunction(l.pluginObj.RawGetString("PreInstall"), l.pluginObj, ctxTable); err != nil {
if err = l.CallFunction("PreInstall", l.pluginObj, ctxTable); err != nil {
return nil, err
}

Expand Down Expand Up @@ -180,8 +180,7 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
L := l.vm.Instance

function := l.pluginObj.RawGetString("PostInstall")
if function.Type() == lua.LTNil {
if !l.HasFunction("PostInstall") {
return nil
}

Expand All @@ -200,7 +199,7 @@ func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
return err
}

if err := l.vm.CallFunction(function, l.pluginObj, ctxTable); err != nil {
if err = l.CallFunction("PostInstall", l.pluginObj, ctxTable); err != nil {
return err
}

Expand Down Expand Up @@ -228,7 +227,7 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
return nil, err
}

if err = l.vm.CallFunction(l.pluginObj.RawGetString("EnvKeys"), l.pluginObj, ctxTable); err != nil {
if err = l.CallFunction("EnvKeys", l.pluginObj, ctxTable); err != nil {
return nil, err
}

Expand Down Expand Up @@ -285,12 +284,11 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
return "", err
}

function := l.pluginObj.RawGetString("PreUse")
if function.Type() == lua.LTNil {
if !l.HasFunction("PreUse") {
return "", nil
}

if err := l.vm.CallFunction(function, l.pluginObj, ctxTable); err != nil {
if err = l.CallFunction("PreUse", l.pluginObj, ctxTable); err != nil {
bytemain marked this conversation as resolved.
Show resolved Hide resolved
return "", err
}

Expand All @@ -308,6 +306,14 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
return Version(result.Version), nil
}

func (l *LuaPlugin) CallFunction(funcName string, args ...lua.LValue) error {
logger.Debugf("CallFunction: %s\n", funcName)
if err := l.vm.CallFunction(l.pluginObj.RawGetString(funcName), args...); err != nil {
return err
}
return nil
}

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

Expand Down
Loading