-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathlua-question.go
More file actions
77 lines (71 loc) · 1.79 KB
/
lua-question.go
File metadata and controls
77 lines (71 loc) · 1.79 KB
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
package rdns
import (
"fmt"
"github.com/miekg/dns"
lua "github.com/yuin/gopher-lua"
)
// Question functions
const luaQuestionMetatableName = "Question"
func (s *LuaScript) RegisterQuestionType() {
L := s.L
mt := L.NewTypeMetatable(luaQuestionMetatableName)
L.SetGlobal(luaQuestionMetatableName, mt)
// static attributes
L.SetField(mt, "new", L.NewFunction(
func(L *lua.LState) int {
q := &dns.Question{Qclass: dns.ClassINET}
nArgs := L.GetTop()
if nArgs >= 1 { // Name provided
q.Name = L.CheckString(1)
}
if nArgs >= 2 { // Name and type
q.Qtype = uint16(L.CheckNumber(2))
}
if nArgs >= 3 { // Name, type and class
q.Qclass = uint16(L.CheckNumber(3))
}
L.Push(userDataWithMetatable(L, luaQuestionMetatableName, q))
return 1
}))
// methods
L.SetField(mt, "__index", L.NewFunction(
func(L *lua.LState) int {
question, ok := getUserDataArg[*dns.Question](L, 1)
if !ok {
return 0
}
fieldName := L.CheckString(2)
switch fieldName {
case "name":
L.Push(lua.LString(question.Name))
case "qtype":
L.Push(lua.LNumber(question.Qtype))
case "qclass":
L.Push(lua.LNumber(question.Qclass))
default:
L.ArgError(2, fmt.Sprintf("question does not have field %q", fieldName))
return 0
}
return 1
}))
L.SetField(mt, "__newindex", L.NewFunction(
func(L *lua.LState) int {
question, ok := getUserDataArg[*dns.Question](L, 1)
if !ok {
return 0
}
fieldName := L.CheckString(2)
switch fieldName {
case "name":
question.Name = L.CheckString(3)
case "qtype":
question.Qtype = uint16(L.CheckNumber(3))
case "qclass":
question.Qclass = uint16(L.CheckNumber(3))
default:
L.ArgError(2, fmt.Sprintf("question does not have field %q", fieldName))
return 0
}
return 0
}))
}