linit.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package lua
  2. const (
  3. // BaseLibName is here for consistency; the base functions have no namespace/library.
  4. BaseLibName = ""
  5. // LoadLibName is here for consistency; the loading system has no namespace/library.
  6. LoadLibName = "package"
  7. // TabLibName is the name of the table Library.
  8. TabLibName = "table"
  9. // IoLibName is the name of the io Library.
  10. IoLibName = "io"
  11. // OsLibName is the name of the os Library.
  12. OsLibName = "os"
  13. // StringLibName is the name of the string Library.
  14. StringLibName = "string"
  15. // MathLibName is the name of the math Library.
  16. MathLibName = "math"
  17. // DebugLibName is the name of the debug Library.
  18. DebugLibName = "debug"
  19. // ChannelLibName is the name of the channel Library.
  20. ChannelLibName = "channel"
  21. // CoroutineLibName is the name of the coroutine Library.
  22. CoroutineLibName = "coroutine"
  23. )
  24. type luaLib struct {
  25. libName string
  26. libFunc LGFunction
  27. }
  28. var luaLibs = []luaLib{
  29. luaLib{LoadLibName, OpenPackage},
  30. luaLib{BaseLibName, OpenBase},
  31. luaLib{TabLibName, OpenTable},
  32. luaLib{IoLibName, OpenIo},
  33. luaLib{OsLibName, OpenOs},
  34. luaLib{StringLibName, OpenString},
  35. luaLib{MathLibName, OpenMath},
  36. luaLib{DebugLibName, OpenDebug},
  37. luaLib{ChannelLibName, OpenChannel},
  38. luaLib{CoroutineLibName, OpenCoroutine},
  39. }
  40. // OpenLibs loads the built-in libraries. It is equivalent to running OpenLoad,
  41. // then OpenBase, then iterating over the other OpenXXX functions in any order.
  42. func (ls *LState) OpenLibs() {
  43. // NB: Map iteration order in Go is deliberately randomised, so must open Load/Base
  44. // prior to iterating.
  45. for _, lib := range luaLibs {
  46. ls.Push(ls.NewFunction(lib.libFunc))
  47. ls.Push(LString(lib.libName))
  48. ls.Call(1, 0)
  49. }
  50. }