github.com/bitxmesh/gopher-lua@v0.0.0-20190327085718-93c344ef97a4/linit.go (about)

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