Repository navigation
Expand file tree
/
Copy pathlua.go
More file actions
151 lines (129 loc) · 3.33 KB
/
Copy pathlua.go
File metadata and controls
151 lines (129 loc) · 3.33 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// pdgo Lua API - unified CGO implementation
package pdgo
/*
// Lua API
int pd_lua_getArgCount(void);
int pd_lua_getArgType(int pos, const char** outClass);
int pd_lua_argIsNil(int pos);
int pd_lua_getArgBool(int pos);
int pd_lua_getArgInt(int pos);
float pd_lua_getArgFloat(int pos);
const char* pd_lua_getArgString(int pos);
void pd_lua_pushNil(void);
void pd_lua_pushBool(int val);
void pd_lua_pushInt(int val);
void pd_lua_pushFloat(float val);
void pd_lua_pushString(const char* str);
*/
import "C"
import "unsafe"
// Lua provides access to Lua scripting
type Lua struct{}
func newLua() *Lua {
return &Lua{}
}
// AddFunction adds a Go function callable from Lua
func (l *Lua) AddFunction(name string, callback func(args []interface{}) interface{}) error {
// Not implemented - requires complex callback registration
return &luaError{op: "addFunction", msg: "not supported"}
}
// GetArgCount returns number of Lua arguments
func (l *Lua) GetArgCount() int {
return int(C.pd_lua_getArgCount())
}
// GetArgType returns type of argument at index and class name
func (l *Lua) GetArgType(pos int) (LuaType, string) {
var outClass *C.char
t := C.pd_lua_getArgType(C.int(pos), &outClass)
className := ""
if outClass != nil {
className = goStringFromCStr(outClass)
}
return LuaType(t), className
}
// ArgIsNil checks if argument is nil
func (l *Lua) ArgIsNil(pos int) bool {
return C.pd_lua_argIsNil(C.int(pos)) != 0
}
// GetArgBool gets boolean argument
func (l *Lua) GetArgBool(pos int) bool {
return C.pd_lua_getArgBool(C.int(pos)) != 0
}
// GetArgInt gets integer argument
func (l *Lua) GetArgInt(pos int) int {
return int(C.pd_lua_getArgInt(C.int(pos)))
}
// GetArgFloat gets float argument
func (l *Lua) GetArgFloat(pos int) float32 {
return float32(C.pd_lua_getArgFloat(C.int(pos)))
}
// GetArgString gets string argument
func (l *Lua) GetArgString(pos int) string {
cstr := C.pd_lua_getArgString(C.int(pos))
if cstr == nil {
return ""
}
return goStringFromCStr(cstr)
}
// PushNil pushes nil to Lua stack
func (l *Lua) PushNil() {
C.pd_lua_pushNil()
}
// PushBool pushes bool to Lua stack
func (l *Lua) PushBool(val bool) {
var v C.int
if val {
v = 1
}
C.pd_lua_pushBool(v)
}
// PushInt pushes int to Lua stack
func (l *Lua) PushInt(val int) {
C.pd_lua_pushInt(C.int(val))
}
// PushFloat pushes float to Lua stack
func (l *Lua) PushFloat(val float32) {
C.pd_lua_pushFloat(C.float(val))
}
// PushString pushes string to Lua stack
func (l *Lua) PushString(val string) {
cstr := make([]byte, len(val)+1)
copy(cstr, val)
C.pd_lua_pushString((*C.char)(unsafe.Pointer(&cstr[0])))
}
// LuaType represents Lua value types
type LuaType int32
const (
LuaTypeNil LuaType = 0
LuaTypeBool LuaType = 1
LuaTypeInt LuaType = 2
LuaTypeFloat LuaType = 3
LuaTypeString LuaType = 4
LuaTypeTable LuaType = 5
LuaTypeFunction LuaType = 6
LuaTypeThread LuaType = 7
LuaTypeObject LuaType = 8
)
type luaError struct {
op string
msg string
}
func (e *luaError) Error() string {
return "lua " + e.op + ": " + e.msg
}
// Helper to convert C string to Go string
func goStringFromCStr(cstr *C.char) string {
if cstr == nil {
return ""
}
var buf []byte
ptr := unsafe.Pointer(cstr)
for i := uintptr(0); ; i++ {
b := *(*byte)(unsafe.Pointer(uintptr(ptr) + i))
if b == 0 {
break
}
buf = append(buf, b)
}
return string(buf)
}