Skip to content

Commit 353622b

Browse files
author
Cathal Garvey
committed
More fixes towards yuin#55, yuin#60.
New private type, luaLib, to contain name and load function, same idea as Lua5.1's luaL_Reg: http://pgl.yoyo.org/luai/i/luaL_Reg luaLibs is now private, is now a slice of luaLibs. Each module register function now returns the module table. luaLibs are now executed by pushing onto the stack, pushing the module name, and calling with args 1 and returns 0. Some comments removed by request.
1 parent 92ce516 commit 353622b

11 files changed

+54
-57
lines changed

baselib.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111

1212
/* basic functions {{{ */
1313

14-
// OpenBase opens the base functions of Lua.
1514
func OpenBase(L *LState) int {
1615
global := L.Get(GlobalsIndex).(*LTable)
1716
L.SetGlobal("_G", global)
1817
L.SetGlobal("_VERSION", LString(PackageName+" "+PackageVersion))
19-
L.RegisterModule("_G", baseFuncs)
18+
basemod := L.RegisterModule("_G", baseFuncs)
2019
global.RawSetString("ipairs", L.NewClosure(baseIpairs, L.NewFunction(ipairsaux)))
2120
global.RawSetString("pairs", L.NewClosure(basePairs, L.NewFunction(pairsaux)))
22-
return 0
21+
L.Push(basemod)
22+
return 1
2323
}
2424

2525
var baseFuncs = map[string]LGFunction{

channellib.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ func checkGoroutineSafe(L *LState, idx int) LValue {
1717
return v
1818
}
1919

20-
// OpenChannel opens the 'channel' library in Lua.
2120
func OpenChannel(L *LState) int {
21+
var mod LValue
2222
_, ok := L.G.builtinMts[int(LTChannel)]
2323
if !ok {
24-
L.RegisterModule("channel", channelFuncs)
24+
mod = L.RegisterModule("channel", channelFuncs)
2525
mt := L.SetFuncs(L.NewTable(), channelMethods)
2626
mt.RawSetString("__index", mt)
2727
L.G.builtinMts[int(LTChannel)] = mt
2828
}
29-
return 0
29+
L.Push(mod)
30+
return 1
3031
}
3132

3233
var channelFuncs = map[string]LGFunction{

coroutinelib.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package lua
22

3-
// OpenCoroutine opens the 'coroutine' library in Lua.
43
func OpenCoroutine(L *LState) int {
54
// TODO: Tie module name to contents of linit.go?
6-
L.RegisterModule("coroutine", coFuncs)
7-
return 0
5+
mod := L.RegisterModule("coroutine", coFuncs)
6+
L.Push(mod)
7+
return 1
88
}
99

1010
var coFuncs = map[string]LGFunction{

debuglib.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"fmt"
55
)
66

7-
// OpenDebug opens the 'debug' library.
87
func OpenDebug(L *LState) int {
9-
L.RegisterModule("debug", debugFuncs)
10-
return 0
8+
dbgmod := L.RegisterModule("debug", debugFuncs)
9+
L.Push(dbgmod)
10+
return 1
1111
}
1212

1313
var debugFuncs = map[string]LGFunction{

iolib.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ var stdFiles = []struct {
179179
{"stderr", os.Stderr, true, false},
180180
}
181181

182-
// OpenIo - New way to open IO, as per https://github.com/yuin/gopher-lua/issues/55
183-
// Satisfies LGFunction.
184182
func OpenIo(L *LState) int {
185183
mod := L.RegisterModule("io", map[string]LGFunction{}).(*LTable)
186184
mt := L.NewTypeMetatable(lFileClass)
@@ -200,7 +198,8 @@ func OpenIo(L *LState) int {
200198
}
201199
mod.RawSetString("lines", L.NewClosure(ioLines, uv, L.NewClosure(ioLinesIter, uv)))
202200
// Modifications are being made in-place rather than returned?
203-
return 0
201+
L.Push(mod)
202+
return 1
204203
}
205204

206205
var fileMethods = map[string]LGFunction{

linit.go

+24-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package lua
22

3-
import "fmt"
4-
53
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"
68
// TabLibName is the name of the table Library.
79
TabLibName = "table"
810
// IoLibName is the name of the io Library.
@@ -19,40 +21,34 @@ const (
1921
ChannelLibName = "channel"
2022
// CoroutineLibName is the name of the coroutine Library.
2123
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"
2624
)
2725

28-
// LuaLibs are the built-in Gopher-lua libraries as opened by LState.OpenLibs(),
29-
// including Base/Load.
30-
var LuaLibs = map[string]LGFunction{
31-
TabLibName: OpenTable,
32-
IoLibName: OpenIo,
33-
OsLibName: OpenOs,
34-
StringLibName: OpenString,
35-
MathLibName: OpenMath,
36-
DebugLibName: OpenDebug,
37-
ChannelLibName: OpenChannel,
38-
CoroutineLibName: OpenCoroutine,
39-
BaseLibName: OpenBase,
40-
LoadLibName: OpenLoad,
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},
4142
}
4243

4344
// OpenLibs loads the built-in libraries. It is equivalent to running OpenLoad,
4445
// then OpenBase, then iterating over the other OpenXXX functions in any order.
4546
func (ls *LState) OpenLibs() {
4647
// NB: Map iteration order in Go is deliberately randomised, so must open Load/Base
4748
// prior to iterating.
48-
OpenLoad(ls)
49-
OpenBase(ls)
50-
for name, loader := range LuaLibs {
51-
if name == BaseLibName || name == LoadLibName {
52-
continue
53-
}
54-
ls.PreloadModule(name, loader)
55-
// TODO: Are all built-ins normally "required"
56-
ls.DoString(fmt.Sprintf(`%s = require "%s"`, name, name))
49+
for _, lib := range luaLibs {
50+
ls.Push(ls.NewFunction(lib.libFunc))
51+
ls.Push(LString(lib.libName))
52+
ls.Call(1, 0)
5753
}
5854
}

loadlib.go

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

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 {
49+
func OpenPackage(L *LState) int {
5250
packagemod := L.RegisterModule("package", loFuncs)
5351

5452
L.SetField(packagemod, "preload", L.NewTable())
@@ -66,7 +64,9 @@ func OpenLoad(L *LState) int {
6664

6765
L.SetField(packagemod, "path", LString(loGetPath(LuaPath, LuaPathDefault)))
6866
L.SetField(packagemod, "cpath", LString(""))
69-
return 0
67+
68+
L.Push(packagemod)
69+
return 1
7070
}
7171

7272
var loFuncs = map[string]LGFunction{

mathlib.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"math/rand"
66
)
77

8-
// OpenMath opens the 'math' library.
98
func OpenMath(L *LState) int {
109
mod := L.RegisterModule("math", mathFuncs).(*LTable)
1110
mod.RawSetString("pi", LNumber(math.Pi))
1211
mod.RawSetString("huge", LNumber(math.MaxFloat64))
13-
return 0
12+
L.Push(mod)
13+
return 1
1414
}
1515

1616
var mathFuncs = map[string]LGFunction{

oslib.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func getBoolField(L *LState, tb *LTable, key string, v bool) bool {
2929
return v
3030
}
3131

32-
// OpenOs opens the 'os' library in Lua.
3332
func OpenOs(L *LState) int {
34-
L.RegisterModule("os", osFuncs)
35-
return 0
33+
osmod := L.RegisterModule("os", osFuncs)
34+
L.Push(osmod)
35+
return 1
3636
}
3737

3838
var osFuncs = map[string]LGFunction{

stringlib.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/yuin/gopher-lua/pm"
99
)
1010

11-
// OpenString opens the 'string' library in Lua.
1211
func OpenString(L *LState) int {
12+
var mod LValue
1313
_, ok := L.G.builtinMts[int(LTString)]
1414
if !ok {
1515
mod := L.RegisterModule("string", strFuncs).(*LTable)
@@ -19,7 +19,8 @@ func OpenString(L *LState) int {
1919
mod.RawSetString("__index", mod)
2020
L.G.builtinMts[int(LTString)] = mod
2121
}
22-
return 0
22+
L.Push(mod)
23+
return 1
2324
}
2425

2526
var strFuncs = map[string]LGFunction{

tablelib.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"sort"
55
)
66

7-
// OpenTable opens the 'table' library.
87
func OpenTable(L *LState) int {
9-
L.RegisterModule("table", tableFuncs)
10-
return 0
8+
tabmod := L.RegisterModule("table", tableFuncs)
9+
L.Push(tabmod)
10+
return 1
1111
}
1212

1313
var tableFuncs = map[string]LGFunction{

0 commit comments

Comments
 (0)