aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-06-01 00:01:24 +0000
committerterminaldweller <devi@terminaldweller.com>2024-06-01 00:01:24 +0000
commit6931cf779ecdfd42dcb30d6bf5a65065f0f4b84b (patch)
tree15051d6f222ebfdfa2e23dbe1a659cbb719c0182
parentadded gopher-lua to the thank you list (diff)
downloadmilla-6931cf779ecdfd42dcb30d6bf5a65065f0f4b84b.tar.gz
milla-6931cf779ecdfd42dcb30d6bf5a65065f0f4b84b.zip
tables are working now
-rw-r--r--plugins.go12
-rw-r--r--plugins_test.go15
2 files changed, 19 insertions, 8 deletions
diff --git a/plugins.go b/plugins.go
index d64f65b..a6d0858 100644
--- a/plugins.go
+++ b/plugins.go
@@ -27,16 +27,15 @@ func registerStrucAsLuaMetaTable[T any](
),
)
- var zero [0]T
+ var dummyType T
+ tableMethods := luaTableGenFactory(reflect.TypeOf(dummyType), checkStruct)
luaState.SetField(
metaTable,
"__index",
luaState.SetFuncs(
luaState.NewTable(),
- luaTableGenFactory(reflect.TypeOf(zero),
- checkStruct,
- ),
+ tableMethods,
),
)
}
@@ -55,6 +54,7 @@ func newStructFunctionFactory[T any](structType T, metaTableName string) func(*l
func checkStruct[T any](luaState *lua.LState) *T {
userData := luaState.CheckUserData(1)
+
if v, ok := userData.Value.(*T); ok {
return v
}
@@ -143,8 +143,8 @@ func luaTableGenFactory[T any](
}
func RegisterCustomLuaTypes(luaState *lua.LState) {
- registerStrucAsLuaMetaTable(luaState, checkStruct, TomlConfig{}, "toml_config")
- registerStrucAsLuaMetaTable(luaState, checkStruct, CustomCommand{}, "custom_command")
+ registerStrucAsLuaMetaTable[TomlConfig](luaState, checkStruct, TomlConfig{}, "toml_config")
+ registerStrucAsLuaMetaTable[CustomCommand](luaState, checkStruct, CustomCommand{}, "custom_command")
}
func returnAllPlugins(pluginPath string) ([]string, error) {
diff --git a/plugins_test.go b/plugins_test.go
index c6cd7ef..894ff3c 100644
--- a/plugins_test.go
+++ b/plugins_test.go
@@ -6,16 +6,27 @@ import (
lua "github.com/yuin/gopher-lua"
)
-func MetaTableTest(t *testing.T) {
+func TestMetaTable(t *testing.T) {
luaState := lua.NewState()
defer luaState.Close()
RegisterCustomLuaTypes(luaState)
if err := luaState.DoString(`
+ print("Testing MetaTable")
+ print(toml_config)
+
+ for index, data in ipairs(toml_config) do
+ print(index, data)
+ for k,v in pairs(data) do
+ print("one")
+ print(k,v)
+ end
+ end
+
config = toml_config.new()
print(config:IrcServer())
- config:TrcServer("irc.freenode.net")
+ config:IrcServer("irc.freenode.net")
print(config:IrcServer())
`); err != nil {
t.Fatal(err)