Skip to content

Commit

Permalink
chore: some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Mar 11, 2024
1 parent f43ddb9 commit ad6f9e8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 35 deletions.
85 changes: 59 additions & 26 deletions internal/luai/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestEncoding(t *testing.T) {

s := []any{"value1", 2, true}

t.Run("Struct", (func(t *testing.T) {
t.Run("Struct", func(t *testing.T) {
luaVm := lua.NewState()
defer luaVm.Close()

Expand All @@ -85,11 +85,11 @@ func TestEncoding(t *testing.T) {
luaVm.SetGlobal("table", _table)

if err := luaVm.DoString(`
assert(table.Field1 == "test")
assert(table.Field2 == 1)
assert(table.Field3 == true)
print("lua Struct done")
`); err != nil {
assert(table.Field1 == "test")
assert(table.Field2 == 1)
assert(table.Field3 == true)
print("lua Struct done")
`); err != nil {
t.Fatal(err)
}

Expand All @@ -102,9 +102,9 @@ func TestEncoding(t *testing.T) {
if !reflect.DeepEqual(test, struct2) {
t.Errorf("expected %+v, got %+v", test, struct2)
}
}))
})

t.Run("Struct with Tag", (func(t *testing.T) {
t.Run("Struct with Tag", func(t *testing.T) {
luaVm := lua.NewState()
defer luaVm.Close()

Expand All @@ -123,11 +123,11 @@ func TestEncoding(t *testing.T) {

luaVm.SetGlobal("table", table)
if err := luaVm.DoString(`
assert(table.field1 == "test")
assert(table.field2 == 1)
assert(table.field3 == true)
print("lua Struct with Tag done")
`); err != nil {
assert(table.field1 == "test")
assert(table.field2 == 1)
assert(table.field3 == true)
print("lua Struct with Tag done")
`); err != nil {
t.Fatal(err)
}

Expand All @@ -140,9 +140,9 @@ func TestEncoding(t *testing.T) {
if !reflect.DeepEqual(test, struct2) {
t.Errorf("expected %+v, got %+v", test, struct2)
}
}))
})

t.Run("Support Map, Slice and Any", (func(t *testing.T) {
t.Run("Support Map, Slice and Any", func(t *testing.T) {
L := lua.NewState()
defer L.Close()
table, err := Marshal(L, m)
Expand All @@ -151,11 +151,11 @@ func TestEncoding(t *testing.T) {
}
L.SetGlobal("m", table)
if err := L.DoString(`
assert(m.key1 == "value1")
assert(m.key2 == 2)
assert(m.key3 == true)
print("lua Map done")
`); err != nil {
assert(m.key1 == "value1")
assert(m.key2 == 2)
assert(m.key3 == true)
print("lua Map done")
`); err != nil {
t.Errorf("map test failed: %v", err)
}

Expand All @@ -166,11 +166,11 @@ func TestEncoding(t *testing.T) {

L.SetGlobal("s", slice)
if err := L.DoString(`
assert(s[1] == "value1")
assert(s[2] == 2)
assert(s[3] == true)
print("lua Slice done")
`); err != nil {
assert(s[1] == "value1")
assert(s[2] == 2)
assert(s[3] == true)
print("lua Slice done")
`); err != nil {
t.Errorf("slice test failed: %v", err)
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func TestEncoding(t *testing.T) {
if !reflect.DeepEqual(s, s3) {
t.Errorf("expected %+v, got %+v", s, s3)
}
}))
})

t.Run("MapSliceStructUnified", func(t *testing.T) {
L := lua.NewState()
Expand Down Expand Up @@ -277,4 +277,37 @@ func TestEncoding(t *testing.T) {
t.Errorf("expected %+v, got %+v", input, output)
}
})

t.Run("TableWithEmptyField", func(t *testing.T) {
L := lua.NewState()
defer L.Close()

output := struct {
Field1 string `luai:"field1"`
Field2 *string `luai:"field2"`
}{}

if err := L.DoString(`
return {
field1 = "value1",
}
`); err != nil {
t.Errorf("map test failed: %v", err)
}

table := L.ToTable(-1) // returned value
L.Pop(1)
// Unmarshal
err := Unmarshal(table, &output)
if err != nil {
t.Fatalf("unmarshal map failed: %v", err)
}
fmt.Printf("output: %+v\n", output)
if output.Field1 != "value1" {
t.Errorf("expected %+v, got %+v", "value1", output.Field1)
}
if output.Field2 != nil {
t.Errorf("expected %+v, got %+v", nil, output.Field2)
}
})
}
4 changes: 3 additions & 1 deletion internal/luai/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ type PrepareOptions struct {
}

func (vm *LuaVM) Prepare(options *PrepareOptions) error {
vm.Instance.DoString(preloadScript)
if err := vm.Instance.DoString(preloadScript); err != nil {
return err
}
module.Preload(vm.Instance, options.Config)

return nil
Expand Down
18 changes: 10 additions & 8 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func (l *LuaPlugin) checkValid() error {
if l.vm == nil || l.vm.Instance == nil {
return fmt.Errorf("lua vm is nil")
}
obj := l.pluginObj
if obj.RawGetString("Available") == lua.LNil {

if !l.HasFunction("Available") {
return fmt.Errorf("[Available] function not found")
}
if obj.RawGetString("PreInstall") == lua.LNil {
if !l.HasFunction("PreInstall") {
return fmt.Errorf("[PreInstall] function not found")
}
if obj.RawGetString("EnvKeys") == lua.LNil {
if !l.HasFunction("EnvKeys") {
return fmt.Errorf("[EnvKeys] function not found")
}
return nil
Expand Down Expand Up @@ -215,7 +215,7 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
// TODO Will be deprecated in future versions
Path: mainInfo.Path,
RuntimeVersion: RuntimeVersion,
Main: (mainInfo),
Main: mainInfo,
SdkInfo: make(map[string]*Info),
}

Expand All @@ -240,7 +240,7 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {

envKeys := make(env.Envs)

items := []*EnvKeysHookResultItem{}
var items []*EnvKeysHookResultItem
err = luai.Unmarshal(table, &items)
if err != nil {
return nil, err
Expand Down Expand Up @@ -311,9 +311,11 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
vm := luai.NewLuaVM()

vm.Prepare(&luai.PrepareOptions{
if err := vm.Prepare(&luai.PrepareOptions{
Config: manager.Config,
})
}); err != nil {
return nil, err
}

// set OS_TYPE and ARCH_TYPE
vm.Instance.SetGlobal(OsType, lua.LString(manager.osType))
Expand Down

0 comments on commit ad6f9e8

Please sign in to comment.