forked from edolphin-ydf/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadlib.go
More file actions
148 lines (126 loc) · 3.28 KB
/
loadlib.go
File metadata and controls
148 lines (126 loc) · 3.28 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
package lua
import (
"fmt"
"math"
"os"
"path/filepath"
"strings"
)
/* load lib {{{ */
var loLoaders = []LGFunction{loLoaderPreload, loLoaderLua}
const (
InsertPosFirst = 0 // insert loader at first position
InsertPosLast = math.MaxInt64 // insert loader at last position
)
func InsertLoader(idx int, loader LGFunction) {
if idx >= len(loLoaders) {
loLoaders = append(loLoaders, loader)
return
}
if idx < 0 {
idx = 0
}
loLoaders = append(loLoaders, nil)
copy(loLoaders[idx+1:], loLoaders[idx:])
loLoaders[idx] = loader
}
func loGetPath(env string, defpath string) string {
path := os.Getenv(env)
if len(path) == 0 {
path = defpath
}
path = strings.Replace(path, ";;", ";"+defpath+";", -1)
if os.PathSeparator != '/' {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
path = strings.Replace(path, "!", dir, -1)
}
return path
}
func loFindFile(L *LState, name, pname string) (string, string) {
name = strings.Replace(name, ".", string(os.PathSeparator), -1)
lv := L.GetField(L.GetField(L.Get(EnvironIndex), "package"), pname)
path, ok := lv.(LString)
if !ok {
L.RaiseError("package.%s must be a string", pname)
}
messages := []string{}
for _, pattern := range strings.Split(string(path), ";") {
luapath := strings.Replace(pattern, "?", name, -1)
if _, err := os.Stat(luapath); err == nil {
return luapath, ""
} else {
messages = append(messages, err.Error())
}
}
return "", strings.Join(messages, "\n\t")
}
func OpenPackage(L *LState) int {
packagemod := L.RegisterModule(LoadLibName, loFuncs)
L.SetField(packagemod, "preload", L.NewTable())
loaders := L.CreateTable(len(loLoaders), 0)
for i, loader := range loLoaders {
L.RawSetInt(loaders, i+1, L.NewFunction(loader))
}
L.SetField(packagemod, "loaders", loaders)
L.SetField(L.Get(RegistryIndex), "_LOADERS", loaders)
loaded := L.NewTable()
L.SetField(packagemod, "loaded", loaded)
L.SetField(L.Get(RegistryIndex), "_LOADED", loaded)
L.SetField(packagemod, "path", LString(loGetPath(LuaPath, LuaPathDefault)))
L.SetField(packagemod, "cpath", emptyLString)
L.SetField(packagemod, "config", LString(LuaDirSep+"\n"+LuaPathSep+
"\n"+LuaPathMark+"\n"+LuaExecDir+"\n"+LuaIgMark+"\n"))
L.Push(packagemod)
return 1
}
var loFuncs = map[string]LGFunction{
"loadlib": loLoadLib,
"seeall": loSeeAll,
}
func loLoaderPreload(L *LState) int {
name := L.CheckString(1)
preload := L.GetField(L.GetField(L.Get(EnvironIndex), "package"), "preload")
if _, ok := preload.(*LTable); !ok {
L.RaiseError("package.preload must be a table")
}
lv := L.GetField(preload, name)
if lv == LNil {
L.Push(LString(fmt.Sprintf("no field package.preload['%s']", name)))
return 1
}
L.Push(lv)
return 1
}
func loLoaderLua(L *LState) int {
name := L.CheckString(1)
path, msg := loFindFile(L, name, "path")
if len(path) == 0 {
L.Push(LString(msg))
return 1
}
fn, err1 := L.LoadFile(path)
if err1 != nil {
L.RaiseError(err1.Error())
}
L.Push(fn)
return 1
}
func loLoadLib(L *LState) int {
L.RaiseError("loadlib is not supported")
return 0
}
func loSeeAll(L *LState) int {
mod := L.CheckTable(1)
mt := L.GetMetatable(mod)
if mt == LNil {
mt = L.CreateTable(0, 1)
L.SetMetatable(mod, mt)
}
L.SetField(mt, "__index", L.Get(GlobalsIndex))
return 0
}
/* }}} */
//