-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdata.go
41 lines (33 loc) · 926 Bytes
/
userdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package glua64
import (
"github.com/yuin/gopher-lua"
)
type UserDataType[T any] struct{ Name string }
func (udt *UserDataType[T]) New(ls *lua.LState, val T) *lua.LUserData {
return &lua.LUserData{
Value: val,
Env: ls.Env,
Metatable: udt.Metatable(ls),
}
}
func (udt *UserDataType[T]) NewMetatable(ls *lua.LState) *lua.LTable {
return ls.NewTypeMetatable(udt.Name)
}
func (udt *UserDataType[T]) Metatable(ls *lua.LState) lua.LValue {
return ls.GetTypeMetatable(udt.Name)
}
func (udt *UserDataType[T]) Check(ls *lua.LState, idx int) (*lua.LUserData, T) {
ud := ls.CheckUserData(idx)
val, ok := ud.Value.(T)
if !ok {
ls.RaiseError("Invalid " + udt.Name)
}
return ud, val
}
func (udt *UserDataType[T]) StartMethod(ls *lua.LState) T {
_, val := udt.Check(ls, 1)
return val
}
func (udt *UserDataType[T]) StartIndex(ls *lua.LState) (T, string) {
return udt.StartMethod(ls), ls.CheckString(2)
}