forked from camsiabor/golua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra.go
176 lines (148 loc) · 4.04 KB
/
extra.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package luar
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"github.com/negbie/golua/lua"
)
const LUA_PATH = "LUA_PATH"
const LUA_CPATH = "LUA_CPATH"
const LUA_PATH_ABS = "LUA_PATH_ABS"
const LUA_CPATH_ABS = "LUA_CPATH_ABS"
func GetLuaPath(luaPath, luaCPath string) (luaPathAbs, luaCPathAbs, luaPathFull, luaCPathFull string) {
var err error
var luaVersion = lua.GetVersionNumber()
var luaVersionWithoutDot = lua.GetVersionNumberWithoutDot()
if luaPath, err = filepath.Abs(luaPath); err != nil {
panic(err)
}
if luaCPath, err = filepath.Abs(luaCPath); err != nil {
panic(err)
}
var luaLibSuffix = "so"
if runtime.GOOS == "windows" {
luaPath = strings.Replace(luaPath, "\\", "/", -1)
luaCPath = strings.Replace(luaCPath, "\\", "/", -1)
luaLibSuffix = "dll"
}
if luaPath[:len(luaPath)-1] != "/" {
luaPath = luaPath + "/"
}
if luaCPath[:len(luaCPath)-1] != "/" {
luaCPath = luaCPath + "/"
}
luaPathFull = luaPath + "?.lua;" +
luaPath + "?init.lua;" +
luaPath + "?;" +
luaPath + "lib/?.lua;" +
luaPath + "lib/?init.lua;" +
luaPath + "lib/?;"
luaCPathFull = luaCPath + "lib/?." + luaLibSuffix + ";" +
luaCPath + "lib/?" + luaVersion + "." + luaLibSuffix + ";" +
luaCPath + "lib/?" + luaVersionWithoutDot + "." + luaLibSuffix + ";" +
luaCPath + "lib/load.all." + luaLibSuffix + ";" +
luaCPath + "lib/?;"
return luaPath, luaCPath, luaPathFull, luaCPathFull
}
func SetLuaPath(L *lua.State, luaPath, luaCPath string) (err error) {
var luaPathAbs, luaCPathAbs, luaPathFull, luaCPathFull = GetLuaPath(luaPath, luaCPath)
L.PushString(luaPathFull)
L.SetGlobal(LUA_PATH)
L.PushString(luaCPathFull)
L.SetGlobal(LUA_CPATH)
L.PushString(luaPathAbs)
L.SetGlobal(LUA_PATH_ABS)
L.PushString(luaCPathAbs)
L.SetGlobal(LUA_CPATH_ABS)
L.GetGlobal("package")
if !L.IsTable(-1) {
return fmt.Errorf("package is not a table? why? en? @_@?")
}
L.PushString(luaPathFull)
L.SetField(-2, "path")
L.PushString(luaCPathFull)
L.SetField(-2, "cpath")
L.Pop(-1)
return nil
}
func GetVal(L *lua.State, idx int) (interface{}, error) {
if L.IsNoneOrNil(idx) {
return nil, nil
}
var ltype = int(L.Type(idx))
switch ltype {
case int(lua.LUA_TNUMBER):
return L.ToNumber(idx), nil
case int(lua.LUA_TSTRING):
return L.ToString(idx), nil
case int(lua.LUA_TBOOLEAN):
return L.ToBoolean(idx), nil
}
var r interface{}
var err = LuaToGo(L, idx, &r)
return r, err
}
func FormatStack(stacks []lua.LuaStackEntry) []lua.LuaStackEntry {
var count = len(stacks)
var clones = make([]lua.LuaStackEntry, count)
for i := 0; i < count; i++ {
var stack = stacks[i]
var clone = lua.LuaStackEntry{
Name: stack.Name,
}
var linenum = stack.CurrentLine
if linenum >= 0 {
var lines = strings.Split(stack.Source, "\n")
if linenum < len(lines) {
clone.ShortSource = lines[linenum-1]
} else {
clone.ShortSource = stack.ShortSource
}
}
clone.Source = ""
clone.CurrentLine = linenum
clones[i] = clone
}
return clones
}
func FormatStackToString(stacks []lua.LuaStackEntry, prefix string, suffix string) string {
var str = ""
var count = len(stacks)
for i := 0; i < count; i++ {
var stack = stacks[i]
var source = stack.ShortSource
var funcname = stack.Name
var linenum = stack.CurrentLine
if linenum >= 0 {
var lines = strings.Split(stack.Source, "\n")
if linenum < len(lines) {
source = lines[linenum-1]
}
}
var one = fmt.Sprintf("%s%s %s %d\n%s", prefix, source, funcname, linenum, suffix)
str = str + one
}
return str
}
func FormatStackToMap(stacks []lua.LuaStackEntry) []map[string]interface{} {
var count = len(stacks)
var clones = make([]map[string]interface{}, count)
for i := 0; i < count; i++ {
var stack = stacks[i]
var clone = make(map[string]interface{})
var linenum = stack.CurrentLine
if linenum >= 0 {
var lines = strings.Split(stack.Source, "\n")
if linenum < len(lines) {
clone["linesrc"] = lines[linenum-1]
} else {
clone["linesrc"] = stack.ShortSource
}
}
clone["line"] = linenum
clone["func"] = stack.Name
clones[i] = clone
}
return clones
}