Skip to content

Commit 79f827a

Browse files
author
Cathal Garvey
committed
All openers are now LGFunction, all tests pass.
1 parent 2ffa14f commit 79f827a

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

baselib.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111

1212
/* basic functions {{{ */
1313

14-
func baseOpen(L *LState) {
14+
// OpenBase opens the base functions of Lua.
15+
func OpenBase(L *LState) int {
1516
global := L.Get(GlobalsIndex).(*LTable)
1617
L.SetGlobal("_G", global)
1718
L.SetGlobal("_VERSION", LString(PackageName+" "+PackageVersion))
1819
L.RegisterModule("_G", baseFuncs)
1920
global.RawSetString("ipairs", L.NewClosure(baseIpairs, L.NewFunction(ipairsaux)))
2021
global.RawSetString("pairs", L.NewClosure(basePairs, L.NewFunction(pairsaux)))
22+
return 0
2123
}
2224

2325
var baseFuncs = map[string]LGFunction{

linit.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ const (
1919
ChannelLibName = "channel"
2020
// CoroutineLibName is the name of the coroutine Library.
2121
CoroutineLibName = "coroutine"
22+
// BaseLibName is here for consistency; the base functions have no namespace/library.
23+
BaseLibName = "_baseLib"
24+
// LoadLibName is here for consistency; the loading system has no namespace/library.
25+
LoadLibName = "_loadLib"
2226
)
2327

24-
// LuaLibs are the built-in Gopher-lua libraries as opened by LState.OpenLibs().
28+
// LuaLibs are the built-in Gopher-lua libraries as opened by LState.OpenLibs(),
29+
// including Base/Load.
2530
var LuaLibs = map[string]LGFunction{
2631
TabLibName: OpenTable,
2732
IoLibName: OpenIo,
@@ -31,31 +36,23 @@ var LuaLibs = map[string]LGFunction{
3136
DebugLibName: OpenDebug,
3237
ChannelLibName: OpenChannel,
3338
CoroutineLibName: OpenCoroutine,
39+
BaseLibName: OpenBase,
40+
LoadLibName: OpenLoad,
3441
}
3542

36-
// OpenLibs loads the built-in libraries. It is equivalent to iterating over
37-
// LuaLibs, preloading each, and requiring each to its own name.
43+
// OpenLibs loads the built-in libraries. It is equivalent to running OpenLoad,
44+
// then OpenBase, then iterating over the other OpenXXX functions in any order.
3845
func (ls *LState) OpenLibs() {
39-
// TODO: Remove when ready.
40-
ls.oldOpenLibs()
41-
46+
// NB: Map iteration order in Go is deliberately randomised, so must open Load/Base
47+
// prior to iterating.
48+
OpenLoad(ls)
49+
OpenBase(ls)
4250
for name, loader := range LuaLibs {
51+
if name == BaseLibName || name == LoadLibName {
52+
continue
53+
}
4354
ls.PreloadModule(name, loader)
55+
// TODO: Are all built-ins normally "required"
4456
ls.DoString(fmt.Sprintf(`%s = require "%s"`, name, name))
4557
}
4658
}
47-
48-
/// Deprecating for linit.go/OpenLibs for https://github.com/yuin/gopher-lua/issues/55
49-
func (ls *LState) oldOpenLibs() {
50-
// loadlib must be loaded 1st
51-
loadOpen(ls)
52-
baseOpen(ls)
53-
//coroutineOpen(ls)
54-
//ioOpen(ls)
55-
//stringOpen(ls)
56-
//tableOpen(ls)
57-
//mathOpen(ls)
58-
//osOpen(ls)
59-
//debugOpen(ls)
60-
//channelOpen(ls)
61-
} // */

loadlib.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ func loFindFile(L *LState, name, pname string) (string, string) {
4646
return "", strings.Join(messages, "\n\t")
4747
}
4848

49-
func loadOpen(L *LState) {
49+
// OpenLoad opens the loading system of Lua; it is essential to load this before
50+
// other built-ins or third-party modules.
51+
func OpenLoad(L *LState) int {
5052
packagemod := L.RegisterModule("package", loFuncs)
5153

5254
L.SetField(packagemod, "preload", L.NewTable())
@@ -64,6 +66,7 @@ func loadOpen(L *LState) {
6466

6567
L.SetField(packagemod, "path", LString(loGetPath(LuaPath, LuaPathDefault)))
6668
L.SetField(packagemod, "cpath", LString(""))
69+
return 0
6770
}
6871

6972
var loFuncs = map[string]LGFunction{

0 commit comments

Comments
 (0)