forked from layeh/gopher-luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetatable.go
117 lines (101 loc) · 2.96 KB
/
metatable.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package luar
import (
"reflect"
"github.com/yuin/gopher-lua"
)
// Metatable holds the Lua metatable for a Go type.
type Metatable struct {
*lua.LTable
l *lua.LState
}
// MT returns the metatable for value's type. nil is returned if value's type
// does not use a custom metatable.
func MT(L *lua.LState, value interface{}) *Metatable {
val := reflect.ValueOf(value)
switch val.Type().Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice, reflect.Struct:
return &Metatable{
LTable: getMetatableFromValue(L, val),
l: L,
}
default:
return nil
}
}
func (m *Metatable) original() *lua.LTable {
original, ok := m.RawGetString("original").(*lua.LTable)
if !ok {
m.l.RaiseError("gopher-luar: corrupt luar metatable")
}
return original
}
// Reset resets the metatable, restoring any fields or methods removed through
// whitelisting or blacklisting.
func (m *Metatable) Reset() {
original := m.original()
for _, name := range []string{"methods", "ptr_methods", "fields"} {
val := original.RawGetString(name)
if val != lua.LNil {
original.RawSetString(name, lua.LNil)
m.RawSetString(name, val)
}
}
}
func (m *Metatable) list(whitelist bool, names ...string) {
m.Reset()
set := make(map[string]struct{}, len(names))
for _, name := range names {
set[name] = struct{}{}
}
original := m.original()
for _, tblName := range []string{"methods", "ptr_methods", "fields"} {
tbl, ok := m.RawGetString(tblName).(*lua.LTable)
if !ok {
continue
}
original.RawSetString(tblName, tbl)
newTbl := m.l.NewTable()
m.RawSetString(tblName, newTbl)
tbl.ForEach(func(key, value lua.LValue) {
keyStr := lua.LVAsString(key)
if _, ok := set[keyStr]; ok == whitelist {
newTbl.RawSetString(keyStr, value)
}
})
}
}
// Whitelist sets only the gvein fields or methods to be accessed from Lua.
//
// Before the whitelist is applied, the metatable is reset to its original
// state (i.e. any previously applied whitelist or blacklist is reverted).
func (m *Metatable) Whitelist(names ...string) {
m.list(true, names...)
}
// Blacklist disallows the given fields or methods to be accessed from Lua.
//
// Before the blacklist is applied, the metatable is reset to its original
// state (i.e. any previously applied whitelist or blacklist is reverted).
func (m *Metatable) Blacklist(names ...string) {
m.list(false, names...)
}
func (m *Metatable) method(name string) lua.LValue {
methods := m.RawGetString("methods").(*lua.LTable)
if fn := methods.RawGetString(name); fn != lua.LNil {
return fn
}
return nil
}
func (m *Metatable) ptrMethod(name string) lua.LValue {
methods := m.RawGetString("ptr_methods").(*lua.LTable)
if fn := methods.RawGetString(name); fn != lua.LNil {
return fn
}
return nil
}
func (m *Metatable) fieldIndex(name string) []int {
fields := m.RawGetString("fields").(*lua.LTable)
if index := fields.RawGetString(name); index != lua.LNil {
return index.(*lua.LUserData).Value.([]int)
}
return nil
}