L.SetGlobal("import", luar.New(L, Import))
}
+// LoadFile loads a lua file
func LoadFile(module string, file string, data string) error {
pluginDef := "local P = {};" + module + " = P;setmetatable(" + module + ", {__index = _G});setfenv(1, P);"
}
}
+// Import allows a lua plugin to import a package
func Import(pkg string) *lua.LTable {
switch pkg {
case "fmt":
- return ImportFmt()
+ return importFmt()
case "io":
- return ImportIo()
- case "ioutil":
- return ImportIoUtil()
+ return importIo()
+ case "io/ioutil", "ioutil":
+ return importIoUtil()
case "net":
- return ImportNet()
+ return importNet()
case "math":
- return ImportMath()
+ return importMath()
+ case "math/rand":
+ return importMathRand()
case "os":
- return ImportOs()
+ return importOs()
case "runtime":
- return ImportRuntime()
+ return importRuntime()
case "path":
- return ImportPath()
+ return importPath()
case "filepath":
- return ImportFilePath()
+ return importFilePath()
case "strings":
- return ImportStrings()
+ return importStrings()
case "regexp":
- return ImportRegexp()
+ return importRegexp()
case "errors":
- return ImportErrors()
+ return importErrors()
case "time":
- return ImportTime()
+ return importTime()
default:
return nil
}
}
-func ImportFmt() *lua.LTable {
+func importFmt() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "tErrorf", luar.New(L, fmt.Errorf))
return pkg
}
-func ImportIo() *lua.LTable {
+func importIo() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Copy", luar.New(L, io.Copy))
return pkg
}
-func ImportIoUtil() *lua.LTable {
+func importIoUtil() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "ReadAll", luar.New(L, ioutil.ReadAll))
return pkg
}
-func ImportNet() *lua.LTable {
+func importNet() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "CIDRMask", luar.New(L, net.CIDRMask))
return pkg
}
-func ImportMath() *lua.LTable {
+func importMath() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Abs", luar.New(L, math.Abs))
return pkg
}
-func ImportMathRand() *lua.LTable {
+func importMathRand() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "ExpFloat64", luar.New(L, rand.ExpFloat64))
return pkg
}
-func ImportOs() *lua.LTable {
+func importOs() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Args", luar.New(L, os.Args))
return pkg
}
-func ImportRuntime() *lua.LTable {
+func importRuntime() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "GC", luar.New(L, runtime.GC))
return pkg
}
-func ImportPath() *lua.LTable {
+func importPath() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Base", luar.New(L, path.Base))
return pkg
}
-func ImportFilePath() *lua.LTable {
+func importFilePath() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Join", luar.New(L, filepath.Join))
- L.SetField(pkg, "Clean", luar.New(L, filepath.Join))
L.SetField(pkg, "Abs", luar.New(L, filepath.Abs))
L.SetField(pkg, "Base", luar.New(L, filepath.Base))
L.SetField(pkg, "Clean", luar.New(L, filepath.Clean))
return pkg
}
-func ImportStrings() *lua.LTable {
+func importStrings() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Contains", luar.New(L, strings.Contains))
return pkg
}
-func ImportRegexp() *lua.LTable {
+func importRegexp() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Match", luar.New(L, regexp.Match))
return pkg
}
-func ImportErrors() *lua.LTable {
+func importErrors() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "New", luar.New(L, errors.New))
return pkg
}
-func ImportTime() *lua.LTable {
+func importTime() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "After", luar.New(L, time.After))